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

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