vendor/twig/twig/src/Parser.php line 373

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