| 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 |
namespace ElementorDeps\Twig; |
| 13 |
|
| 14 |
use ElementorDeps\Twig\Error\SyntaxError; |
| 15 |
use ElementorDeps\Twig\Node\BlockNode; |
| 16 |
use ElementorDeps\Twig\Node\BlockReferenceNode; |
| 17 |
use ElementorDeps\Twig\Node\BodyNode; |
| 18 |
use ElementorDeps\Twig\Node\Expression\AbstractExpression; |
| 19 |
use ElementorDeps\Twig\Node\MacroNode; |
| 20 |
use ElementorDeps\Twig\Node\ModuleNode; |
| 21 |
use ElementorDeps\Twig\Node\Node; |
| 22 |
use ElementorDeps\Twig\Node\NodeCaptureInterface; |
| 23 |
use ElementorDeps\Twig\Node\NodeOutputInterface; |
| 24 |
use ElementorDeps\Twig\Node\PrintNode; |
| 25 |
use ElementorDeps\Twig\Node\TextNode; |
| 26 |
use ElementorDeps\Twig\TokenParser\TokenParserInterface; |
| 27 |
use ElementorDeps\Twig\Util\ReflectionCallable; |
| 28 |
/** |
| 29 |
* @author Fabien Potencier <fabien@symfony.com> |
| 30 |
*/ |
| 31 |
class Parser |
| 32 |
{ |
| 33 |
private $stack = []; |
| 34 |
private $stream; |
| 35 |
private $parent; |
| 36 |
private $visitors; |
| 37 |
private $expressionParser; |
| 38 |
private $blocks; |
| 39 |
private $blockStack; |
| 40 |
private $macros; |
| 41 |
private $env; |
| 42 |
private $importedSymbols; |
| 43 |
private $traits; |
| 44 |
private $embeddedTemplates = []; |
| 45 |
private $varNameSalt = 0; |
| 46 |
public function __construct(Environment $env) |
| 47 |
{ |
| 48 |
$this->env = $env; |
| 49 |
} |
| 50 |
public function getVarName() : string |
| 51 |
{ |
| 52 |
return \sprintf('__internal_parse_%d', $this->varNameSalt++); |
| 53 |
} |
| 54 |
public function parse(TokenStream $stream, $test = null, bool $dropNeedle = \false) : ModuleNode |
| 55 |
{ |
| 56 |
$vars = \get_object_vars($this); |
| 57 |
unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser'], $vars['reservedMacroNames'], $vars['varNameSalt']); |
| 58 |
$this->stack[] = $vars; |
| 59 |
// node visitors |
| 60 |
if (null === $this->visitors) { |
| 61 |
$this->visitors = $this->env->getNodeVisitors(); |
| 62 |
} |
| 63 |
if (null === $this->expressionParser) { |
| 64 |
$this->expressionParser = new ExpressionParser($this, $this->env); |
| 65 |
} |
| 66 |
$this->stream = $stream; |
| 67 |
$this->parent = null; |
| 68 |
$this->blocks = []; |
| 69 |
$this->macros = []; |
| 70 |
$this->traits = []; |
| 71 |
$this->blockStack = []; |
| 72 |
$this->importedSymbols = [[]]; |
| 73 |
$this->embeddedTemplates = []; |
| 74 |
try { |
| 75 |
$body = $this->subparse($test, $dropNeedle); |
| 76 |
if (null !== $this->parent && null === ($body = $this->filterBodyNodes($body))) { |
| 77 |
$body = new Node(); |
| 78 |
} |
| 79 |
} catch (SyntaxError $e) { |
| 80 |
if (!$e->getSourceContext()) { |
| 81 |
$e->setSourceContext($this->stream->getSourceContext()); |
| 82 |
} |
| 83 |
if (!$e->getTemplateLine()) { |
| 84 |
$e->setTemplateLine($this->stream->getCurrent()->getLine()); |
| 85 |
} |
| 86 |
throw $e; |
| 87 |
} |
| 88 |
$node = new ModuleNode(new BodyNode([$body]), $this->parent, new Node($this->blocks), new Node($this->macros), new Node($this->traits), $this->embeddedTemplates, $stream->getSourceContext()); |
| 89 |
$traverser = new NodeTraverser($this->env, $this->visitors); |
| 90 |
/** |
| 91 |
* @var ModuleNode $node |
| 92 |
*/ |
| 93 |
$node = $traverser->traverse($node); |
| 94 |
// restore previous stack so previous parse() call can resume working |
| 95 |
foreach (\array_pop($this->stack) as $key => $val) { |
| 96 |
$this->{$key} = $val; |
| 97 |
} |
| 98 |
return $node; |
| 99 |
} |
| 100 |
public function subparse($test, bool $dropNeedle = \false) : Node |
| 101 |
{ |
| 102 |
$lineno = $this->getCurrentToken()->getLine(); |
| 103 |
$rv = []; |
| 104 |
while (!$this->stream->isEOF()) { |
| 105 |
switch ($this->getCurrentToken()->getType()) { |
| 106 |
case 0: |
| 107 |
$token = $this->stream->next(); |
| 108 |
$rv[] = new TextNode($token->getValue(), $token->getLine()); |
| 109 |
break; |
| 110 |
case 2: |
| 111 |
$token = $this->stream->next(); |
| 112 |
$expr = $this->expressionParser->parseExpression(); |
| 113 |
$this->stream->expect( |
| 114 |
/* Token::VAR_END_TYPE */ |
| 115 |
4 |
| 116 |
); |
| 117 |
$rv[] = new PrintNode($expr, $token->getLine()); |
| 118 |
break; |
| 119 |
case 1: |
| 120 |
$this->stream->next(); |
| 121 |
$token = $this->getCurrentToken(); |
| 122 |
if (5 !== $token->getType()) { |
| 123 |
throw new SyntaxError('A block must start with a tag name.', $token->getLine(), $this->stream->getSourceContext()); |
| 124 |
} |
| 125 |
if (null !== $test && $test($token)) { |
| 126 |
if ($dropNeedle) { |
| 127 |
$this->stream->next(); |
| 128 |
} |
| 129 |
if (1 === \count($rv)) { |
| 130 |
return $rv[0]; |
| 131 |
} |
| 132 |
return new Node($rv, [], $lineno); |
| 133 |
} |
| 134 |
if (!($subparser = $this->env->getTokenParser($token->getValue()))) { |
| 135 |
if (null !== $test) { |
| 136 |
$e = new SyntaxError(\sprintf('Unexpected "%s" tag', $token->getValue()), $token->getLine(), $this->stream->getSourceContext()); |
| 137 |
$callable = (new ReflectionCallable($test))->getCallable(); |
| 138 |
if (\is_array($callable) && $callable[0] instanceof TokenParserInterface) { |
| 139 |
$e->appendMessage(\sprintf(' (expecting closing tag for the "%s" tag defined near line %s).', $callable[0]->getTag(), $lineno)); |
| 140 |
} |
| 141 |
} else { |
| 142 |
$e = new SyntaxError(\sprintf('Unknown "%s" tag.', $token->getValue()), $token->getLine(), $this->stream->getSourceContext()); |
| 143 |
$e->addSuggestions($token->getValue(), \array_keys($this->env->getTokenParsers())); |
| 144 |
} |
| 145 |
throw $e; |
| 146 |
} |
| 147 |
$this->stream->next(); |
| 148 |
$subparser->setParser($this); |
| 149 |
$node = $subparser->parse($token); |
| 150 |
if (null !== $node) { |
| 151 |
$rv[] = $node; |
| 152 |
} |
| 153 |
break; |
| 154 |
default: |
| 155 |
throw new SyntaxError('Lexer or parser ended up in unsupported state.', $this->getCurrentToken()->getLine(), $this->stream->getSourceContext()); |
| 156 |
} |
| 157 |
} |
| 158 |
if (1 === \count($rv)) { |
| 159 |
return $rv[0]; |
| 160 |
} |
| 161 |
return new Node($rv, [], $lineno); |
| 162 |
} |
| 163 |
public function getBlockStack() : array |
| 164 |
{ |
| 165 |
return $this->blockStack; |
| 166 |
} |
| 167 |
public function peekBlockStack() |
| 168 |
{ |
| 169 |
return $this->blockStack[\count($this->blockStack) - 1] ?? null; |
| 170 |
} |
| 171 |
public function popBlockStack() : void |
| 172 |
{ |
| 173 |
\array_pop($this->blockStack); |
| 174 |
} |
| 175 |
public function pushBlockStack($name) : void |
| 176 |
{ |
| 177 |
$this->blockStack[] = $name; |
| 178 |
} |
| 179 |
public function hasBlock(string $name) : bool |
| 180 |
{ |
| 181 |
return isset($this->blocks[$name]); |
| 182 |
} |
| 183 |
public function getBlock(string $name) : Node |
| 184 |
{ |
| 185 |
return $this->blocks[$name]; |
| 186 |
} |
| 187 |
public function setBlock(string $name, BlockNode $value) : void |
| 188 |
{ |
| 189 |
$this->blocks[$name] = new BodyNode([$value], [], $value->getTemplateLine()); |
| 190 |
} |
| 191 |
public function hasMacro(string $name) : bool |
| 192 |
{ |
| 193 |
return isset($this->macros[$name]); |
| 194 |
} |
| 195 |
public function setMacro(string $name, MacroNode $node) : void |
| 196 |
{ |
| 197 |
$this->macros[$name] = $node; |
| 198 |
} |
| 199 |
public function addTrait($trait) : void |
| 200 |
{ |
| 201 |
$this->traits[] = $trait; |
| 202 |
} |
| 203 |
public function hasTraits() : bool |
| 204 |
{ |
| 205 |
return \count($this->traits) > 0; |
| 206 |
} |
| 207 |
public function embedTemplate(ModuleNode $template) |
| 208 |
{ |
| 209 |
$template->setIndex(\mt_rand()); |
| 210 |
$this->embeddedTemplates[] = $template; |
| 211 |
} |
| 212 |
public function addImportedSymbol(string $type, string $alias, ?string $name = null, ?AbstractExpression $node = null) : void |
| 213 |
{ |
| 214 |
$this->importedSymbols[0][$type][$alias] = ['name' => $name, 'node' => $node]; |
| 215 |
} |
| 216 |
public function getImportedSymbol(string $type, string $alias) |
| 217 |
{ |
| 218 |
// if the symbol does not exist in the current scope (0), try in the main/global scope (last index) |
| 219 |
return $this->importedSymbols[0][$type][$alias] ?? $this->importedSymbols[\count($this->importedSymbols) - 1][$type][$alias] ?? null; |
| 220 |
} |
| 221 |
public function isMainScope() : bool |
| 222 |
{ |
| 223 |
return 1 === \count($this->importedSymbols); |
| 224 |
} |
| 225 |
public function pushLocalScope() : void |
| 226 |
{ |
| 227 |
\array_unshift($this->importedSymbols, []); |
| 228 |
} |
| 229 |
public function popLocalScope() : void |
| 230 |
{ |
| 231 |
\array_shift($this->importedSymbols); |
| 232 |
} |
| 233 |
public function getExpressionParser() : ExpressionParser |
| 234 |
{ |
| 235 |
return $this->expressionParser; |
| 236 |
} |
| 237 |
public function getParent() : ?Node |
| 238 |
{ |
| 239 |
return $this->parent; |
| 240 |
} |
| 241 |
public function setParent(?Node $parent) : void |
| 242 |
{ |
| 243 |
$this->parent = $parent; |
| 244 |
} |
| 245 |
public function getStream() : TokenStream |
| 246 |
{ |
| 247 |
return $this->stream; |
| 248 |
} |
| 249 |
public function getCurrentToken() : Token |
| 250 |
{ |
| 251 |
return $this->stream->getCurrent(); |
| 252 |
} |
| 253 |
private function filterBodyNodes(Node $node, bool $nested = \false) : ?Node |
| 254 |
{ |
| 255 |
// check that the body does not contain non-empty output nodes |
| 256 |
if ($node instanceof TextNode && !\ctype_space($node->getAttribute('data')) || !$node instanceof TextNode && !$node instanceof BlockReferenceNode && $node instanceof NodeOutputInterface) { |
| 257 |
if (\str_contains((string) $node, \chr(0xef) . \chr(0xbb) . \chr(0xbf))) { |
| 258 |
$t = \substr($node->getAttribute('data'), 3); |
| 259 |
if ('' === $t || \ctype_space($t)) { |
| 260 |
// bypass empty nodes starting with a BOM |
| 261 |
return null; |
| 262 |
} |
| 263 |
} |
| 264 |
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()); |
| 265 |
} |
| 266 |
// bypass nodes that "capture" the output |
| 267 |
if ($node instanceof NodeCaptureInterface) { |
| 268 |
// a "block" tag in such a node will serve as a block definition AND be displayed in place as well |
| 269 |
return $node; |
| 270 |
} |
| 271 |
// "block" tags that are not captured (see above) are only used for defining |
| 272 |
// the content of the block. In such a case, nesting it does not work as |
| 273 |
// expected as the definition is not part of the default template code flow. |
| 274 |
if ($nested && $node instanceof BlockReferenceNode) { |
| 275 |
throw new SyntaxError('A block definition cannot be nested under non-capturing nodes.', $node->getTemplateLine(), $this->stream->getSourceContext()); |
| 276 |
} |
| 277 |
if ($node instanceof NodeOutputInterface) { |
| 278 |
return null; |
| 279 |
} |
| 280 |
// here, $nested means "being at the root level of a child template" |
| 281 |
// we need to discard the wrapping "Node" for the "body" node |
| 282 |
$nested = $nested || Node::class !== \get_class($node); |
| 283 |
foreach ($node as $k => $n) { |
| 284 |
if (null !== $n && null === $this->filterBodyNodes($n, $nested)) { |
| 285 |
$node->removeNode($k); |
| 286 |
} |
| 287 |
} |
| 288 |
return $node; |
| 289 |
} |
| 290 |
} |
| 291 |
|