Security update for Core, with self-updated composer
[yaffs-website] / vendor / twig / twig / lib / Twig / ExpressionParser.php
1 <?php
2
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  * (c) Armin Ronacher
8  *
9  * For the full copyright and license information, please view the LICENSE
10  * file that was distributed with this source code.
11  */
12
13 /**
14  * Parses expressions.
15  *
16  * This parser implements a "Precedence climbing" algorithm.
17  *
18  * @see http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm
19  * @see http://en.wikipedia.org/wiki/Operator-precedence_parser
20  *
21  * @author Fabien Potencier <fabien@symfony.com>
22  *
23  * @internal
24  */
25 class Twig_ExpressionParser
26 {
27     const OPERATOR_LEFT = 1;
28     const OPERATOR_RIGHT = 2;
29
30     protected $parser;
31     protected $unaryOperators;
32     protected $binaryOperators;
33
34     private $env;
35
36     public function __construct(Twig_Parser $parser, $env = null)
37     {
38         $this->parser = $parser;
39
40         if ($env instanceof Twig_Environment) {
41             $this->env = $env;
42             $this->unaryOperators = $env->getUnaryOperators();
43             $this->binaryOperators = $env->getBinaryOperators();
44         } else {
45             @trigger_error('Passing the operators as constructor arguments to '.__METHOD__.' is deprecated since version 1.27. Pass the environment instead.', E_USER_DEPRECATED);
46
47             $this->env = $parser->getEnvironment();
48             $this->unaryOperators = func_get_arg(1);
49             $this->binaryOperators = func_get_arg(2);
50         }
51     }
52
53     public function parseExpression($precedence = 0)
54     {
55         $expr = $this->getPrimary();
56         $token = $this->parser->getCurrentToken();
57         while ($this->isBinary($token) && $this->binaryOperators[$token->getValue()]['precedence'] >= $precedence) {
58             $op = $this->binaryOperators[$token->getValue()];
59             $this->parser->getStream()->next();
60
61             if ('is not' === $token->getValue()) {
62                 $expr = $this->parseNotTestExpression($expr);
63             } elseif ('is' === $token->getValue()) {
64                 $expr = $this->parseTestExpression($expr);
65             } elseif (isset($op['callable'])) {
66                 $expr = call_user_func($op['callable'], $this->parser, $expr);
67             } else {
68                 $expr1 = $this->parseExpression(self::OPERATOR_LEFT === $op['associativity'] ? $op['precedence'] + 1 : $op['precedence']);
69                 $class = $op['class'];
70                 $expr = new $class($expr, $expr1, $token->getLine());
71             }
72
73             $token = $this->parser->getCurrentToken();
74         }
75
76         if (0 === $precedence) {
77             return $this->parseConditionalExpression($expr);
78         }
79
80         return $expr;
81     }
82
83     protected function getPrimary()
84     {
85         $token = $this->parser->getCurrentToken();
86
87         if ($this->isUnary($token)) {
88             $operator = $this->unaryOperators[$token->getValue()];
89             $this->parser->getStream()->next();
90             $expr = $this->parseExpression($operator['precedence']);
91             $class = $operator['class'];
92
93             return $this->parsePostfixExpression(new $class($expr, $token->getLine()));
94         } elseif ($token->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
95             $this->parser->getStream()->next();
96             $expr = $this->parseExpression();
97             $this->parser->getStream()->expect(Twig_Token::PUNCTUATION_TYPE, ')', 'An opened parenthesis is not properly closed');
98
99             return $this->parsePostfixExpression($expr);
100         }
101
102         return $this->parsePrimaryExpression();
103     }
104
105     protected function parseConditionalExpression($expr)
106     {
107         while ($this->parser->getStream()->nextIf(Twig_Token::PUNCTUATION_TYPE, '?')) {
108             if (!$this->parser->getStream()->nextIf(Twig_Token::PUNCTUATION_TYPE, ':')) {
109                 $expr2 = $this->parseExpression();
110                 if ($this->parser->getStream()->nextIf(Twig_Token::PUNCTUATION_TYPE, ':')) {
111                     $expr3 = $this->parseExpression();
112                 } else {
113                     $expr3 = new Twig_Node_Expression_Constant('', $this->parser->getCurrentToken()->getLine());
114                 }
115             } else {
116                 $expr2 = $expr;
117                 $expr3 = $this->parseExpression();
118             }
119
120             $expr = new Twig_Node_Expression_Conditional($expr, $expr2, $expr3, $this->parser->getCurrentToken()->getLine());
121         }
122
123         return $expr;
124     }
125
126     protected function isUnary(Twig_Token $token)
127     {
128         return $token->test(Twig_Token::OPERATOR_TYPE) && isset($this->unaryOperators[$token->getValue()]);
129     }
130
131     protected function isBinary(Twig_Token $token)
132     {
133         return $token->test(Twig_Token::OPERATOR_TYPE) && isset($this->binaryOperators[$token->getValue()]);
134     }
135
136     public function parsePrimaryExpression()
137     {
138         $token = $this->parser->getCurrentToken();
139         switch ($token->getType()) {
140             case Twig_Token::NAME_TYPE:
141                 $this->parser->getStream()->next();
142                 switch ($token->getValue()) {
143                     case 'true':
144                     case 'TRUE':
145                         $node = new Twig_Node_Expression_Constant(true, $token->getLine());
146                         break;
147
148                     case 'false':
149                     case 'FALSE':
150                         $node = new Twig_Node_Expression_Constant(false, $token->getLine());
151                         break;
152
153                     case 'none':
154                     case 'NONE':
155                     case 'null':
156                     case 'NULL':
157                         $node = new Twig_Node_Expression_Constant(null, $token->getLine());
158                         break;
159
160                     default:
161                         if ('(' === $this->parser->getCurrentToken()->getValue()) {
162                             $node = $this->getFunctionNode($token->getValue(), $token->getLine());
163                         } else {
164                             $node = new Twig_Node_Expression_Name($token->getValue(), $token->getLine());
165                         }
166                 }
167                 break;
168
169             case Twig_Token::NUMBER_TYPE:
170                 $this->parser->getStream()->next();
171                 $node = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
172                 break;
173
174             case Twig_Token::STRING_TYPE:
175             case Twig_Token::INTERPOLATION_START_TYPE:
176                 $node = $this->parseStringExpression();
177                 break;
178
179             case Twig_Token::OPERATOR_TYPE:
180                 if (preg_match(Twig_Lexer::REGEX_NAME, $token->getValue(), $matches) && $matches[0] == $token->getValue()) {
181                     // in this context, string operators are variable names
182                     $this->parser->getStream()->next();
183                     $node = new Twig_Node_Expression_Name($token->getValue(), $token->getLine());
184                     break;
185                 } elseif (isset($this->unaryOperators[$token->getValue()])) {
186                     $class = $this->unaryOperators[$token->getValue()]['class'];
187
188                     $ref = new ReflectionClass($class);
189                     $negClass = 'Twig_Node_Expression_Unary_Neg';
190                     $posClass = 'Twig_Node_Expression_Unary_Pos';
191                     if (!(in_array($ref->getName(), array($negClass, $posClass)) || $ref->isSubclassOf($negClass) || $ref->isSubclassOf($posClass))) {
192                         throw new Twig_Error_Syntax(sprintf('Unexpected unary operator "%s".', $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext());
193                     }
194
195                     $this->parser->getStream()->next();
196                     $expr = $this->parsePrimaryExpression();
197
198                     $node = new $class($expr, $token->getLine());
199                     break;
200                 }
201
202             default:
203                 if ($token->test(Twig_Token::PUNCTUATION_TYPE, '[')) {
204                     $node = $this->parseArrayExpression();
205                 } elseif ($token->test(Twig_Token::PUNCTUATION_TYPE, '{')) {
206                     $node = $this->parseHashExpression();
207                 } elseif ($token->test(Twig_Token::OPERATOR_TYPE, '=') && ($this->parser->getStream()->look(-1)->getValue() === '==' || $this->parser->getStream()->look(-1)->getValue() === '!=')) {
208                     throw new Twig_Error_Syntax(sprintf('Unexpected operator of value "%s". Did you try to use "===" or "!==" for strict comparison? Use "is same as(value)" instead.', $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext());
209                 } else {
210                     throw new Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s".', Twig_Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext());
211                 }
212         }
213
214         return $this->parsePostfixExpression($node);
215     }
216
217     public function parseStringExpression()
218     {
219         $stream = $this->parser->getStream();
220
221         $nodes = array();
222         // a string cannot be followed by another string in a single expression
223         $nextCanBeString = true;
224         while (true) {
225             if ($nextCanBeString && $token = $stream->nextIf(Twig_Token::STRING_TYPE)) {
226                 $nodes[] = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
227                 $nextCanBeString = false;
228             } elseif ($stream->nextIf(Twig_Token::INTERPOLATION_START_TYPE)) {
229                 $nodes[] = $this->parseExpression();
230                 $stream->expect(Twig_Token::INTERPOLATION_END_TYPE);
231                 $nextCanBeString = true;
232             } else {
233                 break;
234             }
235         }
236
237         $expr = array_shift($nodes);
238         foreach ($nodes as $node) {
239             $expr = new Twig_Node_Expression_Binary_Concat($expr, $node, $node->getTemplateLine());
240         }
241
242         return $expr;
243     }
244
245     public function parseArrayExpression()
246     {
247         $stream = $this->parser->getStream();
248         $stream->expect(Twig_Token::PUNCTUATION_TYPE, '[', 'An array element was expected');
249
250         $node = new Twig_Node_Expression_Array(array(), $stream->getCurrent()->getLine());
251         $first = true;
252         while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, ']')) {
253             if (!$first) {
254                 $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'An array element must be followed by a comma');
255
256                 // trailing ,?
257                 if ($stream->test(Twig_Token::PUNCTUATION_TYPE, ']')) {
258                     break;
259                 }
260             }
261             $first = false;
262
263             $node->addElement($this->parseExpression());
264         }
265         $stream->expect(Twig_Token::PUNCTUATION_TYPE, ']', 'An opened array is not properly closed');
266
267         return $node;
268     }
269
270     public function parseHashExpression()
271     {
272         $stream = $this->parser->getStream();
273         $stream->expect(Twig_Token::PUNCTUATION_TYPE, '{', 'A hash element was expected');
274
275         $node = new Twig_Node_Expression_Array(array(), $stream->getCurrent()->getLine());
276         $first = true;
277         while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) {
278             if (!$first) {
279                 $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'A hash value must be followed by a comma');
280
281                 // trailing ,?
282                 if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) {
283                     break;
284                 }
285             }
286             $first = false;
287
288             // a hash key can be:
289             //
290             //  * a number -- 12
291             //  * a string -- 'a'
292             //  * a name, which is equivalent to a string -- a
293             //  * an expression, which must be enclosed in parentheses -- (1 + 2)
294             if (($token = $stream->nextIf(Twig_Token::STRING_TYPE)) || ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) || $token = $stream->nextIf(Twig_Token::NUMBER_TYPE)) {
295                 $key = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
296             } elseif ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
297                 $key = $this->parseExpression();
298             } else {
299                 $current = $stream->getCurrent();
300
301                 throw new Twig_Error_Syntax(sprintf('A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "%s" of value "%s".', Twig_Token::typeToEnglish($current->getType()), $current->getValue()), $current->getLine(), $stream->getSourceContext());
302             }
303
304             $stream->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'A hash key must be followed by a colon (:)');
305             $value = $this->parseExpression();
306
307             $node->addElement($value, $key);
308         }
309         $stream->expect(Twig_Token::PUNCTUATION_TYPE, '}', 'An opened hash is not properly closed');
310
311         return $node;
312     }
313
314     public function parsePostfixExpression($node)
315     {
316         while (true) {
317             $token = $this->parser->getCurrentToken();
318             if ($token->getType() == Twig_Token::PUNCTUATION_TYPE) {
319                 if ('.' == $token->getValue() || '[' == $token->getValue()) {
320                     $node = $this->parseSubscriptExpression($node);
321                 } elseif ('|' == $token->getValue()) {
322                     $node = $this->parseFilterExpression($node);
323                 } else {
324                     break;
325                 }
326             } else {
327                 break;
328             }
329         }
330
331         return $node;
332     }
333
334     public function getFunctionNode($name, $line)
335     {
336         switch ($name) {
337             case 'parent':
338                 $this->parseArguments();
339                 if (!count($this->parser->getBlockStack())) {
340                     throw new Twig_Error_Syntax('Calling "parent" outside a block is forbidden.', $line, $this->parser->getStream()->getSourceContext());
341                 }
342
343                 if (!$this->parser->getParent() && !$this->parser->hasTraits()) {
344                     throw new Twig_Error_Syntax('Calling "parent" on a template that does not extend nor "use" another template is forbidden.', $line, $this->parser->getStream()->getSourceContext());
345                 }
346
347                 return new Twig_Node_Expression_Parent($this->parser->peekBlockStack(), $line);
348             case 'block':
349                 $args = $this->parseArguments();
350                 if (count($args) < 1) {
351                     throw new Twig_Error_Syntax('The "block" function takes one argument (the block name).', $line, $this->parser->getStream()->getSourceContext());
352                 }
353
354                 return new Twig_Node_Expression_BlockReference($args->getNode(0), count($args) > 1 ? $args->getNode(1) : null, $line);
355             case 'attribute':
356                 $args = $this->parseArguments();
357                 if (count($args) < 2) {
358                     throw new Twig_Error_Syntax('The "attribute" function takes at least two arguments (the variable and the attributes).', $line, $this->parser->getStream()->getSourceContext());
359                 }
360
361                 return new Twig_Node_Expression_GetAttr($args->getNode(0), $args->getNode(1), count($args) > 2 ? $args->getNode(2) : null, Twig_Template::ANY_CALL, $line);
362             default:
363                 if (null !== $alias = $this->parser->getImportedSymbol('function', $name)) {
364                     $arguments = new Twig_Node_Expression_Array(array(), $line);
365                     foreach ($this->parseArguments() as $n) {
366                         $arguments->addElement($n);
367                     }
368
369                     $node = new Twig_Node_Expression_MethodCall($alias['node'], $alias['name'], $arguments, $line);
370                     $node->setAttribute('safe', true);
371
372                     return $node;
373                 }
374
375                 $args = $this->parseArguments(true);
376                 $class = $this->getFunctionNodeClass($name, $line);
377
378                 return new $class($name, $args, $line);
379         }
380     }
381
382     public function parseSubscriptExpression($node)
383     {
384         $stream = $this->parser->getStream();
385         $token = $stream->next();
386         $lineno = $token->getLine();
387         $arguments = new Twig_Node_Expression_Array(array(), $lineno);
388         $type = Twig_Template::ANY_CALL;
389         if ($token->getValue() == '.') {
390             $token = $stream->next();
391             if (
392                 $token->getType() == Twig_Token::NAME_TYPE
393                 ||
394                 $token->getType() == Twig_Token::NUMBER_TYPE
395                 ||
396                 ($token->getType() == Twig_Token::OPERATOR_TYPE && preg_match(Twig_Lexer::REGEX_NAME, $token->getValue()))
397             ) {
398                 $arg = new Twig_Node_Expression_Constant($token->getValue(), $lineno);
399
400                 if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
401                     $type = Twig_Template::METHOD_CALL;
402                     foreach ($this->parseArguments() as $n) {
403                         $arguments->addElement($n);
404                     }
405                 }
406             } else {
407                 throw new Twig_Error_Syntax('Expected name or number.', $lineno, $stream->getSourceContext());
408             }
409
410             if ($node instanceof Twig_Node_Expression_Name && null !== $this->parser->getImportedSymbol('template', $node->getAttribute('name'))) {
411                 if (!$arg instanceof Twig_Node_Expression_Constant) {
412                     throw new Twig_Error_Syntax(sprintf('Dynamic macro names are not supported (called on "%s").', $node->getAttribute('name')), $token->getLine(), $stream->getSourceContext());
413                 }
414
415                 $name = $arg->getAttribute('value');
416
417                 if ($this->parser->isReservedMacroName($name)) {
418                     throw new Twig_Error_Syntax(sprintf('"%s" cannot be called as macro as it is a reserved keyword.', $name), $token->getLine(), $stream->getSourceContext());
419                 }
420
421                 $node = new Twig_Node_Expression_MethodCall($node, 'get'.$name, $arguments, $lineno);
422                 $node->setAttribute('safe', true);
423
424                 return $node;
425             }
426         } else {
427             $type = Twig_Template::ARRAY_CALL;
428
429             // slice?
430             $slice = false;
431             if ($stream->test(Twig_Token::PUNCTUATION_TYPE, ':')) {
432                 $slice = true;
433                 $arg = new Twig_Node_Expression_Constant(0, $token->getLine());
434             } else {
435                 $arg = $this->parseExpression();
436             }
437
438             if ($stream->nextIf(Twig_Token::PUNCTUATION_TYPE, ':')) {
439                 $slice = true;
440             }
441
442             if ($slice) {
443                 if ($stream->test(Twig_Token::PUNCTUATION_TYPE, ']')) {
444                     $length = new Twig_Node_Expression_Constant(null, $token->getLine());
445                 } else {
446                     $length = $this->parseExpression();
447                 }
448
449                 $class = $this->getFilterNodeClass('slice', $token->getLine());
450                 $arguments = new Twig_Node(array($arg, $length));
451                 $filter = new $class($node, new Twig_Node_Expression_Constant('slice', $token->getLine()), $arguments, $token->getLine());
452
453                 $stream->expect(Twig_Token::PUNCTUATION_TYPE, ']');
454
455                 return $filter;
456             }
457
458             $stream->expect(Twig_Token::PUNCTUATION_TYPE, ']');
459         }
460
461         return new Twig_Node_Expression_GetAttr($node, $arg, $arguments, $type, $lineno);
462     }
463
464     public function parseFilterExpression($node)
465     {
466         $this->parser->getStream()->next();
467
468         return $this->parseFilterExpressionRaw($node);
469     }
470
471     public function parseFilterExpressionRaw($node, $tag = null)
472     {
473         while (true) {
474             $token = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE);
475
476             $name = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
477             if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
478                 $arguments = new Twig_Node();
479             } else {
480                 $arguments = $this->parseArguments(true);
481             }
482
483             $class = $this->getFilterNodeClass($name->getAttribute('value'), $token->getLine());
484
485             $node = new $class($node, $name, $arguments, $token->getLine(), $tag);
486
487             if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '|')) {
488                 break;
489             }
490
491             $this->parser->getStream()->next();
492         }
493
494         return $node;
495     }
496
497     /**
498      * Parses arguments.
499      *
500      * @param bool $namedArguments Whether to allow named arguments or not
501      * @param bool $definition     Whether we are parsing arguments for a function definition
502      *
503      * @return Twig_Node
504      *
505      * @throws Twig_Error_Syntax
506      */
507     public function parseArguments($namedArguments = false, $definition = false)
508     {
509         $args = array();
510         $stream = $this->parser->getStream();
511
512         $stream->expect(Twig_Token::PUNCTUATION_TYPE, '(', 'A list of arguments must begin with an opening parenthesis');
513         while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, ')')) {
514             if (!empty($args)) {
515                 $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'Arguments must be separated by a comma');
516             }
517
518             if ($definition) {
519                 $token = $stream->expect(Twig_Token::NAME_TYPE, null, 'An argument must be a name');
520                 $value = new Twig_Node_Expression_Name($token->getValue(), $this->parser->getCurrentToken()->getLine());
521             } else {
522                 $value = $this->parseExpression();
523             }
524
525             $name = null;
526             if ($namedArguments && $token = $stream->nextIf(Twig_Token::OPERATOR_TYPE, '=')) {
527                 if (!$value instanceof Twig_Node_Expression_Name) {
528                     throw new Twig_Error_Syntax(sprintf('A parameter name must be a string, "%s" given.', get_class($value)), $token->getLine(), $stream->getSourceContext());
529                 }
530                 $name = $value->getAttribute('name');
531
532                 if ($definition) {
533                     $value = $this->parsePrimaryExpression();
534
535                     if (!$this->checkConstantExpression($value)) {
536                         throw new Twig_Error_Syntax(sprintf('A default value for an argument must be a constant (a boolean, a string, a number, or an array).'), $token->getLine(), $stream->getSourceContext());
537                     }
538                 } else {
539                     $value = $this->parseExpression();
540                 }
541             }
542
543             if ($definition) {
544                 if (null === $name) {
545                     $name = $value->getAttribute('name');
546                     $value = new Twig_Node_Expression_Constant(null, $this->parser->getCurrentToken()->getLine());
547                 }
548                 $args[$name] = $value;
549             } else {
550                 if (null === $name) {
551                     $args[] = $value;
552                 } else {
553                     $args[$name] = $value;
554                 }
555             }
556         }
557         $stream->expect(Twig_Token::PUNCTUATION_TYPE, ')', 'A list of arguments must be closed by a parenthesis');
558
559         return new Twig_Node($args);
560     }
561
562     public function parseAssignmentExpression()
563     {
564         $stream = $this->parser->getStream();
565         $targets = array();
566         while (true) {
567             $token = $stream->expect(Twig_Token::NAME_TYPE, null, 'Only variables can be assigned to');
568             $value = $token->getValue();
569             if (in_array(strtolower($value), array('true', 'false', 'none', 'null'))) {
570                 throw new Twig_Error_Syntax(sprintf('You cannot assign a value to "%s".', $value), $token->getLine(), $stream->getSourceContext());
571             }
572             $targets[] = new Twig_Node_Expression_AssignName($value, $token->getLine());
573
574             if (!$stream->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
575                 break;
576             }
577         }
578
579         return new Twig_Node($targets);
580     }
581
582     public function parseMultitargetExpression()
583     {
584         $targets = array();
585         while (true) {
586             $targets[] = $this->parseExpression();
587             if (!$this->parser->getStream()->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
588                 break;
589             }
590         }
591
592         return new Twig_Node($targets);
593     }
594
595     private function parseNotTestExpression(Twig_NodeInterface $node)
596     {
597         return new Twig_Node_Expression_Unary_Not($this->parseTestExpression($node), $this->parser->getCurrentToken()->getLine());
598     }
599
600     private function parseTestExpression(Twig_NodeInterface $node)
601     {
602         $stream = $this->parser->getStream();
603         list($name, $test) = $this->getTest($node->getTemplateLine());
604
605         $class = $this->getTestNodeClass($test);
606         $arguments = null;
607         if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
608             $arguments = $this->parser->getExpressionParser()->parseArguments(true);
609         }
610
611         return new $class($node, $name, $arguments, $this->parser->getCurrentToken()->getLine());
612     }
613
614     private function getTest($line)
615     {
616         $stream = $this->parser->getStream();
617         $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
618
619         if ($test = $this->env->getTest($name)) {
620             return array($name, $test);
621         }
622
623         if ($stream->test(Twig_Token::NAME_TYPE)) {
624             // try 2-words tests
625             $name = $name.' '.$this->parser->getCurrentToken()->getValue();
626
627             if ($test = $this->env->getTest($name)) {
628                 $stream->next();
629
630                 return array($name, $test);
631             }
632         }
633
634         $e = new Twig_Error_Syntax(sprintf('Unknown "%s" test.', $name), $line, $stream->getSourceContext());
635         $e->addSuggestions($name, array_keys($this->env->getTests()));
636
637         throw $e;
638     }
639
640     private function getTestNodeClass($test)
641     {
642         if ($test instanceof Twig_SimpleTest && $test->isDeprecated()) {
643             $stream = $this->parser->getStream();
644             $message = sprintf('Twig Test "%s" is deprecated', $test->getName());
645             if (!is_bool($test->getDeprecatedVersion())) {
646                 $message .= sprintf(' since version %s', $test->getDeprecatedVersion());
647             }
648             if ($test->getAlternative()) {
649                 $message .= sprintf('. Use "%s" instead', $test->getAlternative());
650             }
651             $src = $stream->getSourceContext();
652             $message .= sprintf(' in %s at line %d.', $src->getPath() ? $src->getPath() : $src->getName(), $stream->getCurrent()->getLine());
653
654             @trigger_error($message, E_USER_DEPRECATED);
655         }
656
657         if ($test instanceof Twig_SimpleTest) {
658             return $test->getNodeClass();
659         }
660
661         return $test instanceof Twig_Test_Node ? $test->getClass() : 'Twig_Node_Expression_Test';
662     }
663
664     protected function getFunctionNodeClass($name, $line)
665     {
666         if (false === $function = $this->env->getFunction($name)) {
667             $e = new Twig_Error_Syntax(sprintf('Unknown "%s" function.', $name), $line, $this->parser->getStream()->getSourceContext());
668             $e->addSuggestions($name, array_keys($this->env->getFunctions()));
669
670             throw $e;
671         }
672
673         if ($function instanceof Twig_SimpleFunction && $function->isDeprecated()) {
674             $message = sprintf('Twig Function "%s" is deprecated', $function->getName());
675             if (!is_bool($function->getDeprecatedVersion())) {
676                 $message .= sprintf(' since version %s', $function->getDeprecatedVersion());
677             }
678             if ($function->getAlternative()) {
679                 $message .= sprintf('. Use "%s" instead', $function->getAlternative());
680             }
681             $src = $this->parser->getStream()->getSourceContext();
682             $message .= sprintf(' in %s at line %d.', $src->getPath() ? $src->getPath() : $src->getName(), $line);
683
684             @trigger_error($message, E_USER_DEPRECATED);
685         }
686
687         if ($function instanceof Twig_SimpleFunction) {
688             return $function->getNodeClass();
689         }
690
691         return $function instanceof Twig_Function_Node ? $function->getClass() : 'Twig_Node_Expression_Function';
692     }
693
694     protected function getFilterNodeClass($name, $line)
695     {
696         if (false === $filter = $this->env->getFilter($name)) {
697             $e = new Twig_Error_Syntax(sprintf('Unknown "%s" filter.', $name), $line, $this->parser->getStream()->getSourceContext());
698             $e->addSuggestions($name, array_keys($this->env->getFilters()));
699
700             throw $e;
701         }
702
703         if ($filter instanceof Twig_SimpleFilter && $filter->isDeprecated()) {
704             $message = sprintf('Twig Filter "%s" is deprecated', $filter->getName());
705             if (!is_bool($filter->getDeprecatedVersion())) {
706                 $message .= sprintf(' since version %s', $filter->getDeprecatedVersion());
707             }
708             if ($filter->getAlternative()) {
709                 $message .= sprintf('. Use "%s" instead', $filter->getAlternative());
710             }
711             $src = $this->parser->getStream()->getSourceContext();
712             $message .= sprintf(' in %s at line %d.', $src->getPath() ? $src->getPath() : $src->getName(), $line);
713
714             @trigger_error($message, E_USER_DEPRECATED);
715         }
716
717         if ($filter instanceof Twig_SimpleFilter) {
718             return $filter->getNodeClass();
719         }
720
721         return $filter instanceof Twig_Filter_Node ? $filter->getClass() : 'Twig_Node_Expression_Filter';
722     }
723
724     // checks that the node only contains "constant" elements
725     protected function checkConstantExpression(Twig_NodeInterface $node)
726     {
727         if (!($node instanceof Twig_Node_Expression_Constant || $node instanceof Twig_Node_Expression_Array
728             || $node instanceof Twig_Node_Expression_Unary_Neg || $node instanceof Twig_Node_Expression_Unary_Pos
729         )) {
730             return false;
731         }
732
733         foreach ($node as $n) {
734             if (!$this->checkConstantExpression($n)) {
735                 return false;
736             }
737         }
738
739         return true;
740     }
741 }
742
743 class_alias('Twig_ExpressionParser', 'Twig\ExpressionParser', false);