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 13namespace Twig; 14 15use Twig\Error\SyntaxError; 16use Twig\ExpressionParser\ExpressionParserInterface; 17use Twig\ExpressionParser\ExpressionParsers; 18use Twig\ExpressionParser\ExpressionParserType; 19use Twig\ExpressionParser\InfixExpressionParserInterface; 20use Twig\ExpressionParser\Prefix\LiteralExpressionParser; 21use Twig\ExpressionParser\PrefixExpressionParserInterface; 22use Twig\Node\BlockNode; 23use Twig\Node\BlockReferenceNode; 24use Twig\Node\BodyNode; 25use Twig\Node\EmptyNode; 26use Twig\Node\Expression\AbstractExpression; 27use Twig\Node\Expression\Variable\AssignTemplateVariable; 28use Twig\Node\Expression\Variable\TemplateVariable; 29use Twig\Node\MacroNode; 30use Twig\Node\ModuleNode; 31use Twig\Node\Node; 32use Twig\Node\NodeCaptureInterface; 33use Twig\Node\NodeOutputInterface; 34use Twig\Node\Nodes; 35use Twig\Node\PrintNode; 36use Twig\Node\TextNode; 37use Twig\TokenParser\TokenParserInterface; 38use Twig\Util\ReflectionCallable; 39 40/** 41 * @author Fabien Potencier <fabien@symfony.com> 42 */ 43class Parser 44{ 45 private $stack = []; 46 private ?\WeakMap $expressionRefs = null; 47 private $stream; 48 private $parent; 49 private $visitors; 50 private $expressionParser; 51 private $blocks; 52 private $blockStack; 53 private $macros; 54 private $importedSymbols; 55 private $traits; 56 private $embeddedTemplates = []; 57 private int $lastEmbedIndex = 0; 58 private $varNameSalt = 0; 59 private $ignoreUnknownTwigCallables = false; 60 private ExpressionParsers $parsers; 61 62 public function __construct( 63 private Environment $env, 64 ) { 65 $this->parsers = $env->getExpressionParsers(); 66 } 67 68 public function getEnvironment(): Environment 69 { 70 return $this->env; 71 } 72 73 public function getVarName(): string 74 { 75 trigger_deprecation('twig/twig', '3.15', 'The "%s()" method is deprecated.', __METHOD__); 76 77 return \sprintf('__internal_parse_%d', $this->varNameSalt++); 78 } 79 80 /** 81 * @throws SyntaxError 82 */ 83 public function parse(TokenStream $stream, $test = null, bool $dropNeedle = false): ModuleNode 84 { 85 // reset on root parse() calls only, so the counter spans nested/reentrant parses 86 if (!$this->stack) { 87 $this->lastEmbedIndex = 0; 88 } 89 90 $vars = get_object_vars($this); 91 unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser'], $vars['reservedMacroNames'], $vars['lastEmbedIndex'], $vars['varNameSalt']); 92 $this->stack[] = $vars; 93 94 // node visitors 95 if (null === $this->visitors) { 96 $this->visitors = $this->env->getNodeVisitors(); 97 } 98 99 $this->stream = $stream; 100 $this->parent = null; 101 $this->blocks = []; 102 $this->macros = []; 103 $this->traits = []; 104 $this->blockStack = []; 105 $this->importedSymbols = [[]]; 106 $this->embeddedTemplates = []; 107 $this->expressionRefs = new \WeakMap(); 108 109 try { 110 $body = $this->subparse($test, $dropNeedle); 111 112 if (null !== $this->parent && null === $body = $this->filterBodyNodes($body)) { 113 $body = new EmptyNode(); 114 } 115 } catch (SyntaxError $e) { 116 if (!$e->getSourceContext()) { 117 $e->setSourceContext($this->stream->getSourceContext()); 118 } 119 120 if (!$e->getTemplateLine()) { 121 $e->setTemplateLine($this->getCurrentToken()->getLine()); 122 } 123 124 throw $e; 125 } finally { 126 $this->expressionRefs = null; 127 } 128 129 $node = new ModuleNode( 130 new BodyNode([$body]), 131 $this->parent, 132 $this->blocks ? new Nodes($this->blocks) : new EmptyNode(), 133 $this->macros ? new Nodes($this->macros) : new EmptyNode(), 134 $this->traits ? new Nodes($this->traits) : new EmptyNode(), 135 $this->embeddedTemplates ? new Nodes($this->embeddedTemplates) : new EmptyNode(), 136 $stream->getSourceContext(), 137 ); 138 139 $traverser = new NodeTraverser($this->env, $this->visitors); 140 141 /** 142 * @var ModuleNode $node 143 */ 144 $node = $traverser->traverse($node); 145 146 // restore previous stack so previous parse() call can resume working 147 foreach (array_pop($this->stack) as $key => $val) { 148 $this->$key = $val; 149 } 150 151 return $node; 152 } 153 154 public function shouldIgnoreUnknownTwigCallables(): bool 155 { 156 return $this->ignoreUnknownTwigCallables; 157 } 158 159 public function subparseIgnoreUnknownTwigCallables($test, bool $dropNeedle = false): void 160 { 161 $previous = $this->ignoreUnknownTwigCallables; 162 $this->ignoreUnknownTwigCallables = true; 163 try { 164 $this->subparse($test, $dropNeedle); 165 } finally { 166 $this->ignoreUnknownTwigCallables = $previous; 167 } 168 } 169 170 /** 171 * @throws SyntaxError 172 */ 173 public function subparse($test, bool $dropNeedle = false): Node 174 { 175 $lineno = $this->getCurrentToken()->getLine(); 176 $rv = []; 177 while (!$this->stream->isEOF()) { 178 switch (true) { 179 case $this->stream->getCurrent()->test(Token::TEXT_TYPE): 180 $token = $this->stream->next(); 181 $rv[] = new TextNode($token->getValue(), $token->getLine()); 182 break; 183 184 case $this->stream->getCurrent()->test(Token::VAR_START_TYPE): 185 $token = $this->stream->next(); 186 $expr = $this->parseExpression(); 187 $this->stream->expect(Token::VAR_END_TYPE); 188 $rv[] = new PrintNode($expr, $token->getLine()); 189 break; 190 191 case $this->stream->getCurrent()->test(Token::BLOCK_START_TYPE): 192 $this->stream->next(); 193 $token = $this->getCurrentToken(); 194 195 if (!$token->test(Token::NAME_TYPE)) { 196 throw new SyntaxError('A block must start with a tag name.', $token->getLine(), $this->stream->getSourceContext()); 197 } 198 199 if (null !== $test && $test($token)) { 200 if ($dropNeedle) { 201 $this->stream->next(); 202 } 203 204 if (1 === \count($rv)) { 205 return $rv[0]; 206 } 207 208 return new Nodes($rv, $lineno); 209 } 210 211 if (!$subparser = $this->env->getTokenParser($token->getValue())) { 212 if (null !== $test) { 213 $e = new SyntaxError(\sprintf('Unexpected "%s" tag', $token->getValue()), $token->getLine(), $this->stream->getSourceContext()); 214 215 $callable = (new ReflectionCallable(new TwigTest('decision', $test)))->getCallable(); 216 if (\is_array($callable) && $callable[0] instanceof TokenParserInterface) { 217 $e->appendMessage(\sprintf(' (expecting closing tag for the "%s" tag defined near line %s).', $callable[0]->getTag(), $lineno)); 218 } 219 } else { 220 $e = new SyntaxError(\sprintf('Unknown "%s" tag.', $token->getValue()), $token->getLine(), $this->stream->getSourceContext()); 221 $e->addSuggestions($token->getValue(), array_keys($this->env->getTokenParsers())); 222 } 223 224 throw $e; 225 } 226 227 $this->stream->next(); 228 229 $subparser->setParser($this); 230 $node = $subparser->parse($token); 231 if (!$node) { 232 trigger_deprecation('twig/twig', '3.12', 'Returning "null" from "%s" is deprecated and forbidden by "TokenParserInterface".', $subparser::class); 233 } else { 234 $node->setNodeTag($subparser->getTag()); 235 $rv[] = $node; 236 } 237 break; 238 239 default: 240 throw new SyntaxError('The lexer or the parser ended up in an unsupported state.', $this->getCurrentToken()->getLine(), $this->stream->getSourceContext()); 241 } 242 } 243 244 if (1 === \count($rv)) { 245 return $rv[0]; 246 } 247 248 return new Nodes($rv, $lineno); 249 } 250 251 public function getBlockStack(): array 252 { 253 trigger_deprecation('twig/twig', '3.12', 'Method "%s()" is deprecated.', __METHOD__); 254 255 return $this->blockStack; 256 } 257 258 /** 259 * @return string|null 260 */ 261 public function peekBlockStack() 262 { 263 return $this->blockStack[\count($this->blockStack) - 1] ?? null; 264 } 265 266 public function popBlockStack(): void 267 { 268 array_pop($this->blockStack); 269 } 270 271 public function pushBlockStack($name): void 272 { 273 $this->blockStack[] = $name; 274 } 275 276 public function hasBlock(string $name): bool 277 { 278 trigger_deprecation('twig/twig', '3.12', 'Method "%s()" is deprecated.', __METHOD__); 279 280 return isset($this->blocks[$name]); 281 } 282 283 public function getBlock(string $name): Node 284 { 285 trigger_deprecation('twig/twig', '3.12', 'Method "%s()" is deprecated.', __METHOD__); 286 287 return $this->blocks[$name]; 288 } 289 290 public function setBlock(string $name, BlockNode $value): void 291 { 292 if (isset($this->blocks[$name])) { 293 throw new SyntaxError(\sprintf("The block '%s' has already been defined line %d.", $name, $this->blocks[$name]->getTemplateLine()), $this->getCurrentToken()->getLine(), $this->blocks[$name]->getSourceContext()); 294 } 295 296 $this->blocks[$name] = new BodyNode([$value], [], $value->getTemplateLine()); 297 } 298 299 public function hasMacro(string $name): bool 300 { 301 trigger_deprecation('twig/twig', '3.12', 'Method "%s()" is deprecated.', __METHOD__); 302 303 return isset($this->macros[$name]); 304 } 305 306 public function setMacro(string $name, MacroNode $node): void 307 { 308 $this->macros[$name] = $node; 309 } 310 311 public function addTrait($trait): void 312 { 313 $this->traits[] = $trait; 314 } 315 316 public function hasTraits(): bool 317 { 318 trigger_deprecation('twig/twig', '3.12', 'Method "%s()" is deprecated.', __METHOD__); 319 320 return \count($this->traits) > 0; 321 } 322 323 /** 324 * @return void 325 */ 326 public function embedTemplate(ModuleNode $template) 327 { 328 $template->setIndex(++$this->lastEmbedIndex); 329 330 $this->embeddedTemplates[] = $template; 331 } 332 333 public function addImportedSymbol(string $type, string $alias, ?string $name = null, AbstractExpression|AssignTemplateVariable|null $internalRef = null): void 334 { 335 if ($internalRef && !$internalRef instanceof AssignTemplateVariable) { 336 trigger_deprecation('twig/twig', '3.15', 'Not passing a "%s" instance as an internal reference is deprecated ("%s" given).', __METHOD__, AssignTemplateVariable::class, $internalRef::class); 337 338 $internalRef = new AssignTemplateVariable(new TemplateVariable($internalRef->getAttribute('name'), $internalRef->getTemplateLine()), $internalRef->getAttribute('global')); 339 } 340 341 $this->importedSymbols[0][$type][$alias] = ['name' => $name, 'node' => $internalRef]; 342 } 343 344 /** 345 * @return array{name: string, node: AssignTemplateVariable|null}|null 346 */ 347 public function getImportedSymbol(string $type, string $alias) 348 { 349 // if the symbol does not exist in the current scope (0), try in the main/global scope (last index) 350 return $this->importedSymbols[0][$type][$alias] ?? ($this->importedSymbols[\count($this->importedSymbols) - 1][$type][$alias] ?? null); 351 } 352 353 public function isMainScope(): bool 354 { 355 return 1 === \count($this->importedSymbols); 356 } 357 358 public function pushLocalScope(): void 359 { 360 array_unshift($this->importedSymbols, []); 361 } 362 363 public function popLocalScope(): void 364 { 365 array_shift($this->importedSymbols); 366 } 367 368 /** 369 * @deprecated since Twig 3.21 370 */ 371 public function getExpressionParser(): ExpressionParser 372 { 373 trigger_deprecation('twig/twig', '3.21', 'Method "%s()" is deprecated, use "parseExpression()" instead.', __METHOD__); 374 375 if (null === $this->expressionParser) { 376 $this->expressionParser = new ExpressionParser($this, $this->env); 377 } 378 379 return $this->expressionParser; 380 } 381 382 public function parseExpression(int $precedence = 0): AbstractExpression 383 { 384 $token = $this->getCurrentToken(); 385 if ($token->test(Token::OPERATOR_TYPE) && $ep = $this->parsers->getByName(PrefixExpressionParserInterface::class, $token->getValue())) { 386 $this->getStream()->next(); 387 $expr = $ep->parse($this, $token); 388 $this->checkPrecedenceDeprecations($ep, $expr); 389 } else { 390 $expr = $this->parsers->getByClass(LiteralExpressionParser::class)->parse($this, $token); 391 } 392 393 $token = $this->getCurrentToken(); 394 while ($token->test(Token::OPERATOR_TYPE) && ($ep = $this->parsers->getByName(InfixExpressionParserInterface::class, $token->getValue())) && $ep->getPrecedence() >= $precedence) { 395 $this->getStream()->next(); 396 $expr = $ep->parse($this, $expr, $token); 397 $this->checkPrecedenceDeprecations($ep, $expr); 398 $token = $this->getCurrentToken(); 399 } 400 401 return $expr; 402 } 403 404 public function getParent(): ?Node 405 { 406 trigger_deprecation('twig/twig', '3.12', 'Method "%s()" is deprecated.', __METHOD__); 407 408 return $this->parent; 409 } 410 411 /** 412 * @return bool 413 */ 414 public function hasInheritance() 415 { 416 return $this->parent || 0 < \count($this->traits); 417 } 418 419 public function setParent(?Node $parent): void 420 { 421 if (null === $parent) { 422 trigger_deprecation('twig/twig', '3.12', 'Passing "null" to "%s()" is deprecated.', __METHOD__); 423 } 424 425 if (null !== $parent && !$parent instanceof AbstractExpression) { 426 trigger_deprecation('twig/twig', '3.24', 'Passing a "%s" instance to "%s()" is deprecated, pass an "AbstractExpression" instance instead.', $parent::class, __METHOD__); 427 } 428 429 if (null !== $this->parent) { 430 throw new SyntaxError('Multiple extends tags are forbidden.', $parent->getTemplateLine(), $parent->getSourceContext()); 431 } 432 433 $this->parent = $parent; 434 } 435 436 public function getStream(): TokenStream 437 { 438 return $this->stream; 439 } 440 441 public function getCurrentToken(): Token 442 { 443 return $this->stream->getCurrent(); 444 } 445 446 public function getFunction(string $name, int $line): TwigFunction 447 { 448 try { 449 $function = $this->env->getFunction($name); 450 } catch (SyntaxError $e) { 451 if (!$this->shouldIgnoreUnknownTwigCallables()) { 452 throw $e; 453 } 454 455 $function = null; 456 } 457 458 if (!$function) { 459 if ($this->shouldIgnoreUnknownTwigCallables()) { 460 return new TwigFunction($name, static fn () => ''); 461 } 462 $e = new SyntaxError(\sprintf('Unknown "%s" function.', $name), $line, $this->stream->getSourceContext()); 463 $e->addSuggestions($name, array_keys($this->env->getFunctions())); 464 465 throw $e; 466 } 467 468 if ($function->isDeprecated()) { 469 $src = $this->stream->getSourceContext(); 470 $function->triggerDeprecation($src->getPath() ?: $src->getName(), $line); 471 } 472 473 return $function; 474 } 475 476 public function getFilter(string $name, int $line): TwigFilter 477 { 478 try { 479 $filter = $this->env->getFilter($name); 480 } catch (SyntaxError $e) { 481 if (!$this->shouldIgnoreUnknownTwigCallables()) { 482 throw $e; 483 } 484 485 $filter = null; 486 } 487 if (!$filter) { 488 if ($this->shouldIgnoreUnknownTwigCallables()) { 489 return new TwigFilter($name, static fn () => ''); 490 } 491 $e = new SyntaxError(\sprintf('Unknown "%s" filter.', $name), $line, $this->stream->getSourceContext()); 492 $e->addSuggestions($name, array_keys($this->env->getFilters())); 493 494 throw $e; 495 } 496 497 if ($filter->isDeprecated()) { 498 $src = $this->stream->getSourceContext(); 499 $filter->triggerDeprecation($src->getPath() ?: $src->getName(), $line); 500 } 501 502 return $filter; 503 } 504 505 public function getTest(int $line): TwigTest 506 { 507 $name = $this->stream->expect(Token::NAME_TYPE)->getValue(); 508 509 if ($this->stream->test(Token::NAME_TYPE)) { 510 // try 2-words tests 511 $name = $name.' '.$this->getCurrentToken()->getValue(); 512 513 try { 514 $test = $this->env->getTest($name); 515 } catch (SyntaxError $e) { 516 if (!$this->shouldIgnoreUnknownTwigCallables()) { 517 throw $e; 518 } 519 520 $test = null; 521 } 522 $this->stream->next(); 523 } else { 524 try { 525 $test = $this->env->getTest($name); 526 } catch (SyntaxError $e) { 527 if (!$this->shouldIgnoreUnknownTwigCallables()) { 528 throw $e; 529 } 530 531 $test = null; 532 } 533 } 534 535 if (!$test) { 536 if ($this->shouldIgnoreUnknownTwigCallables()) { 537 return new TwigTest($name, static fn () => ''); 538 } 539 $e = new SyntaxError(\sprintf('Unknown "%s" test.', $name), $line, $this->stream->getSourceContext()); 540 $e->addSuggestions($name, array_keys($this->env->getTests())); 541 542 throw $e; 543 } 544 545 if ($test->isDeprecated()) { 546 $src = $this->stream->getSourceContext(); 547 $test->triggerDeprecation($src->getPath() ?: $src->getName(), $this->stream->getCurrent()->getLine()); 548 } 549 550 return $test; 551 } 552 553 private function filterBodyNodes(Node $node, bool $nested = false): ?Node 554 { 555 // check that the body does not contain non-empty output nodes 556 if ( 557 ($node instanceof TextNode && !ctype_space($node->getAttribute('data'))) 558 || (!$node instanceof TextNode && !$node instanceof BlockReferenceNode && $node instanceof NodeOutputInterface) 559 ) { 560 if (str_contains((string) $node, \chr(0xEF).\chr(0xBB).\chr(0xBF))) { 561 $t = substr($node->getAttribute('data'), 3); 562 if ('' === $t || ctype_space($t)) { 563 // bypass empty nodes starting with a BOM 564 return null; 565 } 566 } 567 568 throw new SyntaxError('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()); 569 } 570 571 // bypass nodes that "capture" the output 572 if ($node instanceof NodeCaptureInterface) { 573 // a "block" tag in such a node will serve as a block definition AND be displayed in place as well 574 return $node; 575 } 576 577 // "block" tags that are not captured (see above) are only used for defining 578 // the content of the block. In such a case, nesting it does not work as 579 // expected as the definition is not part of the default template code flow. 580 if ($nested && $node instanceof BlockReferenceNode) { 581 throw new SyntaxError('A block definition cannot be nested under non-capturing nodes.', $node->getTemplateLine(), $this->stream->getSourceContext()); 582 } 583 584 if ($node instanceof NodeOutputInterface) { 585 return null; 586 } 587 588 // here, $nested means "being at the root level of a child template" 589 // we need to discard the wrapping "Node" for the "body" node 590 // Node::class !== \get_class($node) should be removed in Twig 4.0 591 $nested = $nested || (Node::class !== $node::class && !$node instanceof Nodes); 592 foreach ($node as $k => $n) { 593 if (null !== $n && null === $this->filterBodyNodes($n, $nested)) { 594 $node->removeNode($k); 595 } 596 } 597 598 return $node; 599 } 600 601 private function checkPrecedenceDeprecations(ExpressionParserInterface $expressionParser, AbstractExpression $expr) 602 { 603 $this->expressionRefs[$expr] = $expressionParser; 604 $precedenceChanges = $this->parsers->getPrecedenceChanges(); 605 606 // Check that the all nodes that are between the 2 precedences have explicit parentheses 607 if (!isset($precedenceChanges[$expressionParser])) { 608 return; 609 } 610 611 if ($expr->hasExplicitParentheses()) { 612 return; 613 } 614 615 if ($expressionParser instanceof PrefixExpressionParserInterface) { 616 /** @var AbstractExpression $node */ 617 $node = $expr->getNode('node'); 618 foreach ($precedenceChanges as $ep => $changes) { 619 if (!\in_array($expressionParser, $changes, true)) { 620 continue; 621 } 622 if (isset($this->expressionRefs[$node]) && $ep === $this->expressionRefs[$node]) { 623 $change = $expressionParser->getPrecedenceChange(); 624 trigger_deprecation($change->getPackage(), $change->getVersion(), \sprintf('As the "%s" %s operator will change its precedence in the next major version, add explicit parentheses to avoid behavior change in "%s" at line %d.', $expressionParser->getName(), ExpressionParserType::getType($expressionParser)->value, $this->getStream()->getSourceContext()->getName(), $node->getTemplateLine())); 625 } 626 } 627 } 628 629 foreach ($precedenceChanges[$expressionParser] as $ep) { 630 foreach ($expr as $node) { 631 /** @var AbstractExpression $node */ 632 if (isset($this->expressionRefs[$node]) && $ep === $this->expressionRefs[$node] && !$node->hasExplicitParentheses()) { 633 $change = $ep->getPrecedenceChange(); 634 trigger_deprecation($change->getPackage(), $change->getVersion(), \sprintf('As the "%s" %s operator will change its precedence in the next major version, add explicit parentheses to avoid behavior change in "%s" at line %d.', $ep->getName(), ExpressionParserType::getType($ep)->value, $this->getStream()->getSourceContext()->getName(), $node->getTemplateLine())); 635 } 636 } 637 } 638 } 639} 640