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

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