Security update to Drupal 8.4.6
[yaffs-website] / vendor / twig / twig / lib / Twig / Parser.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  * Default parser implementation.
15  *
16  * @author Fabien Potencier <fabien@symfony.com>
17  */
18 class Twig_Parser implements Twig_ParserInterface
19 {
20     protected $stack = array();
21     protected $stream;
22     protected $parent;
23     protected $handlers;
24     protected $visitors;
25     protected $expressionParser;
26     protected $blocks;
27     protected $blockStack;
28     protected $macros;
29     protected $env;
30     protected $reservedMacroNames;
31     protected $importedSymbols;
32     protected $traits;
33     protected $embeddedTemplates = array();
34     private $varNameSalt = 0;
35
36     public function __construct(Twig_Environment $env)
37     {
38         $this->env = $env;
39     }
40
41     /**
42      * @deprecated since 1.27 (to be removed in 2.0)
43      */
44     public function getEnvironment()
45     {
46         @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0.', E_USER_DEPRECATED);
47
48         return $this->env;
49     }
50
51     public function getVarName()
52     {
53         return sprintf('__internal_%s', hash('sha256', __METHOD__.$this->stream->getSourceContext()->getCode().$this->varNameSalt++));
54     }
55
56     /**
57      * @deprecated since 1.27 (to be removed in 2.0). Use $parser->getStream()->getSourceContext()->getPath() instead.
58      */
59     public function getFilename()
60     {
61         @trigger_error(sprintf('The "%s" method is deprecated since version 1.27 and will be removed in 2.0. Use $parser->getStream()->getSourceContext()->getPath() instead.', __METHOD__), E_USER_DEPRECATED);
62
63         return $this->stream->getSourceContext()->getName();
64     }
65
66     public function parse(Twig_TokenStream $stream, $test = null, $dropNeedle = false)
67     {
68         // push all variables into the stack to keep the current state of the parser
69         // using get_object_vars() instead of foreach would lead to https://bugs.php.net/71336
70         // This hack can be removed when min version if PHP 7.0
71         $vars = array();
72         foreach ($this as $k => $v) {
73             $vars[$k] = $v;
74         }
75
76         unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser'], $vars['reservedMacroNames']);
77         $this->stack[] = $vars;
78
79         // tag handlers
80         if (null === $this->handlers) {
81             $this->handlers = $this->env->getTokenParsers();
82             $this->handlers->setParser($this);
83         }
84
85         // node visitors
86         if (null === $this->visitors) {
87             $this->visitors = $this->env->getNodeVisitors();
88         }
89
90         if (null === $this->expressionParser) {
91             $this->expressionParser = new Twig_ExpressionParser($this, $this->env);
92         }
93
94         $this->stream = $stream;
95         $this->parent = null;
96         $this->blocks = array();
97         $this->macros = array();
98         $this->traits = array();
99         $this->blockStack = array();
100         $this->importedSymbols = array(array());
101         $this->embeddedTemplates = array();
102         $this->varNameSalt = 0;
103
104         try {
105             $body = $this->subparse($test, $dropNeedle);
106
107             if (null !== $this->parent && null === $body = $this->filterBodyNodes($body)) {
108                 $body = new Twig_Node();
109             }
110         } catch (Twig_Error_Syntax $e) {
111             if (!$e->getSourceContext()) {
112                 $e->setSourceContext($this->stream->getSourceContext());
113             }
114
115             if (!$e->getTemplateLine()) {
116                 $e->setTemplateLine($this->stream->getCurrent()->getLine());
117             }
118
119             throw $e;
120         }
121
122         $node = new Twig_Node_Module(new Twig_Node_Body(array($body)), $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), $this->embeddedTemplates, $stream->getSourceContext());
123
124         $traverser = new Twig_NodeTraverser($this->env, $this->visitors);
125
126         $node = $traverser->traverse($node);
127
128         // restore previous stack so previous parse() call can resume working
129         foreach (array_pop($this->stack) as $key => $val) {
130             $this->$key = $val;
131         }
132
133         return $node;
134     }
135
136     public function subparse($test, $dropNeedle = false)
137     {
138         $lineno = $this->getCurrentToken()->getLine();
139         $rv = array();
140         while (!$this->stream->isEOF()) {
141             switch ($this->getCurrentToken()->getType()) {
142                 case Twig_Token::TEXT_TYPE:
143                     $token = $this->stream->next();
144                     $rv[] = new Twig_Node_Text($token->getValue(), $token->getLine());
145                     break;
146
147                 case Twig_Token::VAR_START_TYPE:
148                     $token = $this->stream->next();
149                     $expr = $this->expressionParser->parseExpression();
150                     $this->stream->expect(Twig_Token::VAR_END_TYPE);
151                     $rv[] = new Twig_Node_Print($expr, $token->getLine());
152                     break;
153
154                 case Twig_Token::BLOCK_START_TYPE:
155                     $this->stream->next();
156                     $token = $this->getCurrentToken();
157
158                     if (Twig_Token::NAME_TYPE !== $token->getType()) {
159                         throw new Twig_Error_Syntax('A block must start with a tag name.', $token->getLine(), $this->stream->getSourceContext());
160                     }
161
162                     if (null !== $test && call_user_func($test, $token)) {
163                         if ($dropNeedle) {
164                             $this->stream->next();
165                         }
166
167                         if (1 === count($rv)) {
168                             return $rv[0];
169                         }
170
171                         return new Twig_Node($rv, array(), $lineno);
172                     }
173
174                     $subparser = $this->handlers->getTokenParser($token->getValue());
175                     if (null === $subparser) {
176                         if (null !== $test) {
177                             $e = new Twig_Error_Syntax(sprintf('Unexpected "%s" tag', $token->getValue()), $token->getLine(), $this->stream->getSourceContext());
178
179                             if (is_array($test) && isset($test[0]) && $test[0] instanceof Twig_TokenParserInterface) {
180                                 $e->appendMessage(sprintf(' (expecting closing tag for the "%s" tag defined near line %s).', $test[0]->getTag(), $lineno));
181                             }
182                         } else {
183                             $e = new Twig_Error_Syntax(sprintf('Unknown "%s" tag.', $token->getValue()), $token->getLine(), $this->stream->getSourceContext());
184                             $e->addSuggestions($token->getValue(), array_keys($this->env->getTags()));
185                         }
186
187                         throw $e;
188                     }
189
190                     $this->stream->next();
191
192                     $node = $subparser->parse($token);
193                     if (null !== $node) {
194                         $rv[] = $node;
195                     }
196                     break;
197
198                 default:
199                     throw new Twig_Error_Syntax('Lexer or parser ended up in unsupported state.', $this->getCurrentToken()->getLine(), $this->stream->getSourceContext());
200             }
201         }
202
203         if (1 === count($rv)) {
204             return $rv[0];
205         }
206
207         return new Twig_Node($rv, array(), $lineno);
208     }
209
210     /**
211      * @deprecated since 1.27 (to be removed in 2.0)
212      */
213     public function addHandler($name, $class)
214     {
215         @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0.', E_USER_DEPRECATED);
216
217         $this->handlers[$name] = $class;
218     }
219
220     /**
221      * @deprecated since 1.27 (to be removed in 2.0)
222      */
223     public function addNodeVisitor(Twig_NodeVisitorInterface $visitor)
224     {
225         @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0.', E_USER_DEPRECATED);
226
227         $this->visitors[] = $visitor;
228     }
229
230     public function getBlockStack()
231     {
232         return $this->blockStack;
233     }
234
235     public function peekBlockStack()
236     {
237         return $this->blockStack[count($this->blockStack) - 1];
238     }
239
240     public function popBlockStack()
241     {
242         array_pop($this->blockStack);
243     }
244
245     public function pushBlockStack($name)
246     {
247         $this->blockStack[] = $name;
248     }
249
250     public function hasBlock($name)
251     {
252         return isset($this->blocks[$name]);
253     }
254
255     public function getBlock($name)
256     {
257         return $this->blocks[$name];
258     }
259
260     public function setBlock($name, Twig_Node_Block $value)
261     {
262         $this->blocks[$name] = new Twig_Node_Body(array($value), array(), $value->getTemplateLine());
263     }
264
265     public function hasMacro($name)
266     {
267         return isset($this->macros[$name]);
268     }
269
270     public function setMacro($name, Twig_Node_Macro $node)
271     {
272         if ($this->isReservedMacroName($name)) {
273             throw new Twig_Error_Syntax(sprintf('"%s" cannot be used as a macro name as it is a reserved keyword.', $name), $node->getTemplateLine(), $this->stream->getSourceContext());
274         }
275
276         $this->macros[$name] = $node;
277     }
278
279     public function isReservedMacroName($name)
280     {
281         if (null === $this->reservedMacroNames) {
282             $this->reservedMacroNames = array();
283             $r = new ReflectionClass($this->env->getBaseTemplateClass());
284             foreach ($r->getMethods() as $method) {
285                 $methodName = strtolower($method->getName());
286
287                 if ('get' === substr($methodName, 0, 3) && isset($methodName[3])) {
288                     $this->reservedMacroNames[] = substr($methodName, 3);
289                 }
290             }
291         }
292
293         return in_array(strtolower($name), $this->reservedMacroNames);
294     }
295
296     public function addTrait($trait)
297     {
298         $this->traits[] = $trait;
299     }
300
301     public function hasTraits()
302     {
303         return count($this->traits) > 0;
304     }
305
306     public function embedTemplate(Twig_Node_Module $template)
307     {
308         $template->setIndex(mt_rand());
309
310         $this->embeddedTemplates[] = $template;
311     }
312
313     public function addImportedSymbol($type, $alias, $name = null, Twig_Node_Expression $node = null)
314     {
315         $this->importedSymbols[0][$type][$alias] = array('name' => $name, 'node' => $node);
316     }
317
318     public function getImportedSymbol($type, $alias)
319     {
320         foreach ($this->importedSymbols as $functions) {
321             if (isset($functions[$type][$alias])) {
322                 return $functions[$type][$alias];
323             }
324         }
325     }
326
327     public function isMainScope()
328     {
329         return 1 === count($this->importedSymbols);
330     }
331
332     public function pushLocalScope()
333     {
334         array_unshift($this->importedSymbols, array());
335     }
336
337     public function popLocalScope()
338     {
339         array_shift($this->importedSymbols);
340     }
341
342     /**
343      * @return Twig_ExpressionParser
344      */
345     public function getExpressionParser()
346     {
347         return $this->expressionParser;
348     }
349
350     public function getParent()
351     {
352         return $this->parent;
353     }
354
355     public function setParent($parent)
356     {
357         $this->parent = $parent;
358     }
359
360     /**
361      * @return Twig_TokenStream
362      */
363     public function getStream()
364     {
365         return $this->stream;
366     }
367
368     /**
369      * @return Twig_Token
370      */
371     public function getCurrentToken()
372     {
373         return $this->stream->getCurrent();
374     }
375
376     protected function filterBodyNodes(Twig_NodeInterface $node)
377     {
378         // check that the body does not contain non-empty output nodes
379         if (
380             ($node instanceof Twig_Node_Text && !ctype_space($node->getAttribute('data')))
381             ||
382             (!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && $node instanceof Twig_NodeOutputInterface)
383         ) {
384             if (false !== strpos((string) $node, chr(0xEF).chr(0xBB).chr(0xBF))) {
385                 throw new Twig_Error_Syntax('A template that extends another one cannot start with a byte order mark (BOM); it must be removed.', $node->getTemplateLine(), $this->stream->getSourceContext());
386             }
387
388             throw new Twig_Error_Syntax('A template that extends another one cannot include content outside Twig blocks. Did you forget to put the content inside a {% block %} tag?', $node->getTemplateLine(), $this->stream->getSourceContext());
389         }
390
391         // bypass nodes that will "capture" the output
392         if ($node instanceof Twig_NodeCaptureInterface) {
393             return $node;
394         }
395
396         if ($node instanceof Twig_NodeOutputInterface) {
397             return;
398         }
399
400         foreach ($node as $k => $n) {
401             if (null !== $n && null === $this->filterBodyNodes($n)) {
402                 $node->removeNode($k);
403             }
404         }
405
406         return $node;
407     }
408 }
409
410 class_alias('Twig_Parser', 'Twig\Parser', false);
411 class_exists('Twig_Node');
412 class_exists('Twig_TokenStream');