Remove const optimization

This commit is contained in:
Fabien Potencier
2024-08-12 09:39:57 +02:00
parent 93e66ad141
commit cd0ac3cc0a
13 changed files with 106 additions and 106 deletions
+70 -70
View File
@@ -105,52 +105,52 @@ class ExpressionParser
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
// short array syntax (one argument, no parentheses)? // short array syntax (one argument, no parentheses)?
if ($stream->look(1)->test(/* Token::ARROW_TYPE */ 12)) { if ($stream->look(1)->test(Token::ARROW_TYPE)) {
$line = $stream->getCurrent()->getLine(); $line = $stream->getCurrent()->getLine();
$token = $stream->expect(/* Token::NAME_TYPE */ 5); $token = $stream->expect(Token::NAME_TYPE);
$names = [new AssignNameExpression($token->getValue(), $token->getLine())]; $names = [new AssignNameExpression($token->getValue(), $token->getLine())];
$stream->expect(/* Token::ARROW_TYPE */ 12); $stream->expect(Token::ARROW_TYPE);
return new ArrowFunctionExpression($this->parseExpression(0), new Node($names), $line); return new ArrowFunctionExpression($this->parseExpression(0), new Node($names), $line);
} }
// first, determine if we are parsing an arrow function by finding => (long form) // first, determine if we are parsing an arrow function by finding => (long form)
$i = 0; $i = 0;
if (!$stream->look($i)->test(/* Token::PUNCTUATION_TYPE */ 9, '(')) { if (!$stream->look($i)->test(Token::PUNCTUATION_TYPE, '(')) {
return null; return null;
} }
++$i; ++$i;
while (true) { while (true) {
// variable name // variable name
++$i; ++$i;
if (!$stream->look($i)->test(/* Token::PUNCTUATION_TYPE */ 9, ',')) { if (!$stream->look($i)->test(Token::PUNCTUATION_TYPE, ',')) {
break; break;
} }
++$i; ++$i;
} }
if (!$stream->look($i)->test(/* Token::PUNCTUATION_TYPE */ 9, ')')) { if (!$stream->look($i)->test(Token::PUNCTUATION_TYPE, ')')) {
return null; return null;
} }
++$i; ++$i;
if (!$stream->look($i)->test(/* Token::ARROW_TYPE */ 12)) { if (!$stream->look($i)->test(Token::ARROW_TYPE)) {
return null; return null;
} }
// yes, let's parse it properly // yes, let's parse it properly
$token = $stream->expect(/* Token::PUNCTUATION_TYPE */ 9, '('); $token = $stream->expect(Token::PUNCTUATION_TYPE, '(');
$line = $token->getLine(); $line = $token->getLine();
$names = []; $names = [];
while (true) { while (true) {
$token = $stream->expect(/* Token::NAME_TYPE */ 5); $token = $stream->expect(Token::NAME_TYPE);
$names[] = new AssignNameExpression($token->getValue(), $token->getLine()); $names[] = new AssignNameExpression($token->getValue(), $token->getLine());
if (!$stream->nextIf(/* Token::PUNCTUATION_TYPE */ 9, ',')) { if (!$stream->nextIf(Token::PUNCTUATION_TYPE, ',')) {
break; break;
} }
} }
$stream->expect(/* Token::PUNCTUATION_TYPE */ 9, ')'); $stream->expect(Token::PUNCTUATION_TYPE, ')');
$stream->expect(/* Token::ARROW_TYPE */ 12); $stream->expect(Token::ARROW_TYPE);
return new ArrowFunctionExpression($this->parseExpression(0), new Node($names), $line); return new ArrowFunctionExpression($this->parseExpression(0), new Node($names), $line);
} }
@@ -166,10 +166,10 @@ class ExpressionParser
$class = $operator['class']; $class = $operator['class'];
return $this->parsePostfixExpression(new $class($expr, $token->getLine())); return $this->parsePostfixExpression(new $class($expr, $token->getLine()));
} elseif ($token->test(/* Token::PUNCTUATION_TYPE */ 9, '(')) { } elseif ($token->test(Token::PUNCTUATION_TYPE, '(')) {
$this->parser->getStream()->next(); $this->parser->getStream()->next();
$expr = $this->parseExpression(); $expr = $this->parseExpression();
$this->parser->getStream()->expect(/* Token::PUNCTUATION_TYPE */ 9, ')', 'An opened parenthesis is not properly closed'); $this->parser->getStream()->expect(Token::PUNCTUATION_TYPE, ')', 'An opened parenthesis is not properly closed');
return $this->parsePostfixExpression($expr); return $this->parsePostfixExpression($expr);
} }
@@ -179,10 +179,10 @@ class ExpressionParser
private function parseConditionalExpression($expr): AbstractExpression private function parseConditionalExpression($expr): AbstractExpression
{ {
while ($this->parser->getStream()->nextIf(/* Token::PUNCTUATION_TYPE */ 9, '?')) { while ($this->parser->getStream()->nextIf(Token::PUNCTUATION_TYPE, '?')) {
if (!$this->parser->getStream()->nextIf(/* Token::PUNCTUATION_TYPE */ 9, ':')) { if (!$this->parser->getStream()->nextIf(Token::PUNCTUATION_TYPE, ':')) {
$expr2 = $this->parseExpression(); $expr2 = $this->parseExpression();
if ($this->parser->getStream()->nextIf(/* Token::PUNCTUATION_TYPE */ 9, ':')) { if ($this->parser->getStream()->nextIf(Token::PUNCTUATION_TYPE, ':')) {
// Ternary operator (expr ? expr2 : expr3) // Ternary operator (expr ? expr2 : expr3)
$expr3 = $this->parseExpression(); $expr3 = $this->parseExpression();
} else { } else {
@@ -203,19 +203,19 @@ class ExpressionParser
private function isUnary(Token $token): bool private function isUnary(Token $token): bool
{ {
return $token->test(/* Token::OPERATOR_TYPE */ 8) && isset($this->unaryOperators[$token->getValue()]); return $token->test(Token::OPERATOR_TYPE) && isset($this->unaryOperators[$token->getValue()]);
} }
private function isBinary(Token $token): bool private function isBinary(Token $token): bool
{ {
return $token->test(/* Token::OPERATOR_TYPE */ 8) && isset($this->binaryOperators[$token->getValue()]); return $token->test(Token::OPERATOR_TYPE) && isset($this->binaryOperators[$token->getValue()]);
} }
public function parsePrimaryExpression() public function parsePrimaryExpression()
{ {
$token = $this->parser->getCurrentToken(); $token = $this->parser->getCurrentToken();
switch ($token->getType()) { switch ($token->getType()) {
case /* Token::NAME_TYPE */ 5: case Token::NAME_TYPE:
$this->parser->getStream()->next(); $this->parser->getStream()->next();
switch ($token->getValue()) { switch ($token->getValue()) {
case 'true': case 'true':
@@ -244,17 +244,17 @@ class ExpressionParser
} }
break; break;
case /* Token::NUMBER_TYPE */ 6: case Token::NUMBER_TYPE:
$this->parser->getStream()->next(); $this->parser->getStream()->next();
$node = new ConstantExpression($token->getValue(), $token->getLine()); $node = new ConstantExpression($token->getValue(), $token->getLine());
break; break;
case /* Token::STRING_TYPE */ 7: case Token::STRING_TYPE:
case /* Token::INTERPOLATION_START_TYPE */ 10: case /* Token::INTERPOLATION_START_TYPE */ 10:
$node = $this->parseStringExpression(); $node = $this->parseStringExpression();
break; break;
case /* Token::OPERATOR_TYPE */ 8: case Token::OPERATOR_TYPE:
if (preg_match(Lexer::REGEX_NAME, $token->getValue(), $matches) && $matches[0] == $token->getValue()) { if (preg_match(Lexer::REGEX_NAME, $token->getValue(), $matches) && $matches[0] == $token->getValue()) {
// in this context, string operators are variable names // in this context, string operators are variable names
$this->parser->getStream()->next(); $this->parser->getStream()->next();
@@ -277,11 +277,11 @@ class ExpressionParser
// no break // no break
default: default:
if ($token->test(/* Token::PUNCTUATION_TYPE */ 9, '[')) { if ($token->test(Token::PUNCTUATION_TYPE, '[')) {
$node = $this->parseSequenceExpression(); $node = $this->parseSequenceExpression();
} elseif ($token->test(/* Token::PUNCTUATION_TYPE */ 9, '{')) { } elseif ($token->test(Token::PUNCTUATION_TYPE, '{')) {
$node = $this->parseMappingExpression(); $node = $this->parseMappingExpression();
} elseif ($token->test(/* Token::OPERATOR_TYPE */ 8, '=') && ('==' === $this->parser->getStream()->look(-1)->getValue() || '!=' === $this->parser->getStream()->look(-1)->getValue())) { } elseif ($token->test(Token::OPERATOR_TYPE, '=') && ('==' === $this->parser->getStream()->look(-1)->getValue() || '!=' === $this->parser->getStream()->look(-1)->getValue())) {
throw new SyntaxError(\sprintf('Unexpected operator of value "%s". Did you try to use "===" or "!==" for strict comparison? Use "is same as(value)" instead.', $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext()); throw new SyntaxError(\sprintf('Unexpected operator of value "%s". Did you try to use "===" or "!==" for strict comparison? Use "is same as(value)" instead.', $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext());
} else { } else {
throw new SyntaxError(\sprintf('Unexpected token "%s" of value "%s".', Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext()); throw new SyntaxError(\sprintf('Unexpected token "%s" of value "%s".', Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext());
@@ -299,7 +299,7 @@ class ExpressionParser
// a string cannot be followed by another string in a single expression // a string cannot be followed by another string in a single expression
$nextCanBeString = true; $nextCanBeString = true;
while (true) { while (true) {
if ($nextCanBeString && $token = $stream->nextIf(/* Token::STRING_TYPE */ 7)) { if ($nextCanBeString && $token = $stream->nextIf(Token::STRING_TYPE)) {
$nodes[] = new ConstantExpression($token->getValue(), $token->getLine()); $nodes[] = new ConstantExpression($token->getValue(), $token->getLine());
$nextCanBeString = false; $nextCanBeString = false;
} elseif ($stream->nextIf(/* Token::INTERPOLATION_START_TYPE */ 10)) { } elseif ($stream->nextIf(/* Token::INTERPOLATION_START_TYPE */ 10)) {
@@ -332,22 +332,22 @@ class ExpressionParser
public function parseSequenceExpression() public function parseSequenceExpression()
{ {
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
$stream->expect(/* Token::PUNCTUATION_TYPE */ 9, '[', 'A sequence element was expected'); $stream->expect(Token::PUNCTUATION_TYPE, '[', 'A sequence element was expected');
$node = new ArrayExpression([], $stream->getCurrent()->getLine()); $node = new ArrayExpression([], $stream->getCurrent()->getLine());
$first = true; $first = true;
while (!$stream->test(/* Token::PUNCTUATION_TYPE */ 9, ']')) { while (!$stream->test(Token::PUNCTUATION_TYPE, ']')) {
if (!$first) { if (!$first) {
$stream->expect(/* Token::PUNCTUATION_TYPE */ 9, ',', 'A sequence element must be followed by a comma'); $stream->expect(Token::PUNCTUATION_TYPE, ',', 'A sequence element must be followed by a comma');
// trailing ,? // trailing ,?
if ($stream->test(/* Token::PUNCTUATION_TYPE */ 9, ']')) { if ($stream->test(Token::PUNCTUATION_TYPE, ']')) {
break; break;
} }
} }
$first = false; $first = false;
if ($stream->test(/* Token::SPREAD_TYPE */ 13)) { if ($stream->test(Token::SPREAD_TYPE)) {
$stream->next(); $stream->next();
$expr = $this->parseExpression(); $expr = $this->parseExpression();
$expr->setAttribute('spread', true); $expr->setAttribute('spread', true);
@@ -356,7 +356,7 @@ class ExpressionParser
$node->addElement($this->parseExpression()); $node->addElement($this->parseExpression());
} }
} }
$stream->expect(/* Token::PUNCTUATION_TYPE */ 9, ']', 'An opened sequence is not properly closed'); $stream->expect(Token::PUNCTUATION_TYPE, ']', 'An opened sequence is not properly closed');
return $node; return $node;
} }
@@ -374,22 +374,22 @@ class ExpressionParser
public function parseMappingExpression() public function parseMappingExpression()
{ {
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
$stream->expect(/* Token::PUNCTUATION_TYPE */ 9, '{', 'A mapping element was expected'); $stream->expect(Token::PUNCTUATION_TYPE, '{', 'A mapping element was expected');
$node = new ArrayExpression([], $stream->getCurrent()->getLine()); $node = new ArrayExpression([], $stream->getCurrent()->getLine());
$first = true; $first = true;
while (!$stream->test(/* Token::PUNCTUATION_TYPE */ 9, '}')) { while (!$stream->test(Token::PUNCTUATION_TYPE, '}')) {
if (!$first) { if (!$first) {
$stream->expect(/* Token::PUNCTUATION_TYPE */ 9, ',', 'A mapping value must be followed by a comma'); $stream->expect(Token::PUNCTUATION_TYPE, ',', 'A mapping value must be followed by a comma');
// trailing ,? // trailing ,?
if ($stream->test(/* Token::PUNCTUATION_TYPE */ 9, '}')) { if ($stream->test(Token::PUNCTUATION_TYPE, '}')) {
break; break;
} }
} }
$first = false; $first = false;
if ($stream->test(/* Token::SPREAD_TYPE */ 13)) { if ($stream->test(Token::SPREAD_TYPE)) {
$stream->next(); $stream->next();
$value = $this->parseExpression(); $value = $this->parseExpression();
$value->setAttribute('spread', true); $value->setAttribute('spread', true);
@@ -403,7 +403,7 @@ class ExpressionParser
// * a string -- 'a' // * a string -- 'a'
// * a name, which is equivalent to a string -- a // * a name, which is equivalent to a string -- a
// * an expression, which must be enclosed in parentheses -- (1 + 2) // * an expression, which must be enclosed in parentheses -- (1 + 2)
if ($token = $stream->nextIf(/* Token::NAME_TYPE */ 5)) { if ($token = $stream->nextIf(Token::NAME_TYPE)) {
$key = new ConstantExpression($token->getValue(), $token->getLine()); $key = new ConstantExpression($token->getValue(), $token->getLine());
// {a} is a shortcut for {a:a} // {a} is a shortcut for {a:a}
@@ -412,9 +412,9 @@ class ExpressionParser
$node->addElement($value, $key); $node->addElement($value, $key);
continue; continue;
} }
} elseif (($token = $stream->nextIf(/* Token::STRING_TYPE */ 7)) || $token = $stream->nextIf(/* Token::NUMBER_TYPE */ 6)) { } elseif (($token = $stream->nextIf(Token::STRING_TYPE)) || $token = $stream->nextIf(Token::NUMBER_TYPE)) {
$key = new ConstantExpression($token->getValue(), $token->getLine()); $key = new ConstantExpression($token->getValue(), $token->getLine());
} elseif ($stream->test(/* Token::PUNCTUATION_TYPE */ 9, '(')) { } elseif ($stream->test(Token::PUNCTUATION_TYPE, '(')) {
$key = $this->parseExpression(); $key = $this->parseExpression();
} else { } else {
$current = $stream->getCurrent(); $current = $stream->getCurrent();
@@ -422,12 +422,12 @@ class ExpressionParser
throw new SyntaxError(\sprintf('A mapping key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "%s" of value "%s".', Token::typeToEnglish($current->getType()), $current->getValue()), $current->getLine(), $stream->getSourceContext()); throw new SyntaxError(\sprintf('A mapping key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "%s" of value "%s".', Token::typeToEnglish($current->getType()), $current->getValue()), $current->getLine(), $stream->getSourceContext());
} }
$stream->expect(/* Token::PUNCTUATION_TYPE */ 9, ':', 'A mapping key must be followed by a colon (:)'); $stream->expect(Token::PUNCTUATION_TYPE, ':', 'A mapping key must be followed by a colon (:)');
$value = $this->parseExpression(); $value = $this->parseExpression();
$node->addElement($value, $key); $node->addElement($value, $key);
} }
$stream->expect(/* Token::PUNCTUATION_TYPE */ 9, '}', 'An opened mapping is not properly closed'); $stream->expect(Token::PUNCTUATION_TYPE, '}', 'An opened mapping is not properly closed');
return $node; return $node;
} }
@@ -436,7 +436,7 @@ class ExpressionParser
{ {
while (true) { while (true) {
$token = $this->parser->getCurrentToken(); $token = $this->parser->getCurrentToken();
if (/* Token::PUNCTUATION_TYPE */ 9 == $token->getType()) { if (Token::PUNCTUATION_TYPE == $token->getType()) {
if ('.' == $token->getValue() || '[' == $token->getValue()) { if ('.' == $token->getValue() || '[' == $token->getValue()) {
$node = $this->parseSubscriptExpression($node); $node = $this->parseSubscriptExpression($node);
} elseif ('|' == $token->getValue()) { } elseif ('|' == $token->getValue()) {
@@ -510,15 +510,15 @@ class ExpressionParser
if ('.' == $token->getValue()) { if ('.' == $token->getValue()) {
$token = $stream->next(); $token = $stream->next();
if ( if (
/* Token::NAME_TYPE */ 5 == $token->getType() Token::NAME_TYPE == $token->getType()
|| ||
/* Token::NUMBER_TYPE */ 6 == $token->getType() Token::NUMBER_TYPE == $token->getType()
|| ||
(/* Token::OPERATOR_TYPE */ 8 == $token->getType() && preg_match(Lexer::REGEX_NAME, $token->getValue())) (Token::OPERATOR_TYPE == $token->getType() && preg_match(Lexer::REGEX_NAME, $token->getValue()))
) { ) {
$arg = new ConstantExpression($token->getValue(), $lineno); $arg = new ConstantExpression($token->getValue(), $lineno);
if ($stream->test(/* Token::PUNCTUATION_TYPE */ 9, '(')) { if ($stream->test(Token::PUNCTUATION_TYPE, '(')) {
$type = Template::METHOD_CALL; $type = Template::METHOD_CALL;
foreach ($this->parseArguments() as $n) { foreach ($this->parseArguments() as $n) {
$arguments->addElement($n); $arguments->addElement($n);
@@ -541,19 +541,19 @@ class ExpressionParser
// slice? // slice?
$slice = false; $slice = false;
if ($stream->test(/* Token::PUNCTUATION_TYPE */ 9, ':')) { if ($stream->test(Token::PUNCTUATION_TYPE, ':')) {
$slice = true; $slice = true;
$arg = new ConstantExpression(0, $token->getLine()); $arg = new ConstantExpression(0, $token->getLine());
} else { } else {
$arg = $this->parseExpression(); $arg = $this->parseExpression();
} }
if ($stream->nextIf(/* Token::PUNCTUATION_TYPE */ 9, ':')) { if ($stream->nextIf(Token::PUNCTUATION_TYPE, ':')) {
$slice = true; $slice = true;
} }
if ($slice) { if ($slice) {
if ($stream->test(/* Token::PUNCTUATION_TYPE */ 9, ']')) { if ($stream->test(Token::PUNCTUATION_TYPE, ']')) {
$length = new ConstantExpression(null, $token->getLine()); $length = new ConstantExpression(null, $token->getLine());
} else { } else {
$length = $this->parseExpression(); $length = $this->parseExpression();
@@ -563,12 +563,12 @@ class ExpressionParser
$arguments = new Node([$arg, $length]); $arguments = new Node([$arg, $length]);
$filter = new $class($node, new ConstantExpression('slice', $token->getLine()), $arguments, $token->getLine()); $filter = new $class($node, new ConstantExpression('slice', $token->getLine()), $arguments, $token->getLine());
$stream->expect(/* Token::PUNCTUATION_TYPE */ 9, ']'); $stream->expect(Token::PUNCTUATION_TYPE, ']');
return $filter; return $filter;
} }
$stream->expect(/* Token::PUNCTUATION_TYPE */ 9, ']'); $stream->expect(Token::PUNCTUATION_TYPE, ']');
} }
return new GetAttrExpression($node, $arg, $arguments, $type, $lineno); return new GetAttrExpression($node, $arg, $arguments, $type, $lineno);
@@ -584,10 +584,10 @@ class ExpressionParser
public function parseFilterExpressionRaw($node, $tag = null) public function parseFilterExpressionRaw($node, $tag = null)
{ {
while (true) { while (true) {
$token = $this->parser->getStream()->expect(/* Token::NAME_TYPE */ 5); $token = $this->parser->getStream()->expect(Token::NAME_TYPE);
$name = new ConstantExpression($token->getValue(), $token->getLine()); $name = new ConstantExpression($token->getValue(), $token->getLine());
if (!$this->parser->getStream()->test(/* Token::PUNCTUATION_TYPE */ 9, '(')) { if (!$this->parser->getStream()->test(Token::PUNCTUATION_TYPE, '(')) {
$arguments = new Node(); $arguments = new Node();
} else { } else {
$arguments = $this->parseArguments(true, false, true); $arguments = $this->parseArguments(true, false, true);
@@ -597,7 +597,7 @@ class ExpressionParser
$node = new $class($node, $name, $arguments, $token->getLine(), $tag); $node = new $class($node, $name, $arguments, $token->getLine(), $tag);
if (!$this->parser->getStream()->test(/* Token::PUNCTUATION_TYPE */ 9, '|')) { if (!$this->parser->getStream()->test(Token::PUNCTUATION_TYPE, '|')) {
break; break;
} }
@@ -622,26 +622,26 @@ class ExpressionParser
$args = []; $args = [];
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
$stream->expect(/* Token::PUNCTUATION_TYPE */ 9, '(', 'A list of arguments must begin with an opening parenthesis'); $stream->expect(Token::PUNCTUATION_TYPE, '(', 'A list of arguments must begin with an opening parenthesis');
while (!$stream->test(/* Token::PUNCTUATION_TYPE */ 9, ')')) { while (!$stream->test(Token::PUNCTUATION_TYPE, ')')) {
if (!empty($args)) { if (!empty($args)) {
$stream->expect(/* Token::PUNCTUATION_TYPE */ 9, ',', 'Arguments must be separated by a comma'); $stream->expect(Token::PUNCTUATION_TYPE, ',', 'Arguments must be separated by a comma');
// if the comma above was a trailing comma, early exit the argument parse loop // if the comma above was a trailing comma, early exit the argument parse loop
if ($stream->test(/* Token::PUNCTUATION_TYPE */ 9, ')')) { if ($stream->test(Token::PUNCTUATION_TYPE, ')')) {
break; break;
} }
} }
if ($definition) { if ($definition) {
$token = $stream->expect(/* Token::NAME_TYPE */ 5, null, 'An argument must be a name'); $token = $stream->expect(Token::NAME_TYPE, null, 'An argument must be a name');
$value = new NameExpression($token->getValue(), $this->parser->getCurrentToken()->getLine()); $value = new NameExpression($token->getValue(), $this->parser->getCurrentToken()->getLine());
} else { } else {
$value = $this->parseExpression(0, $allowArrow); $value = $this->parseExpression(0, $allowArrow);
} }
$name = null; $name = null;
if ($namedArguments && $token = $stream->nextIf(/* Token::OPERATOR_TYPE */ 8, '=')) { if ($namedArguments && $token = $stream->nextIf(Token::OPERATOR_TYPE, '=')) {
if (!$value instanceof NameExpression) { if (!$value instanceof NameExpression) {
throw new SyntaxError(\sprintf('A parameter name must be a string, "%s" given.', \get_class($value)), $token->getLine(), $stream->getSourceContext()); throw new SyntaxError(\sprintf('A parameter name must be a string, "%s" given.', \get_class($value)), $token->getLine(), $stream->getSourceContext());
} }
@@ -672,7 +672,7 @@ class ExpressionParser
} }
} }
} }
$stream->expect(/* Token::PUNCTUATION_TYPE */ 9, ')', 'A list of arguments must be closed by a parenthesis'); $stream->expect(Token::PUNCTUATION_TYPE, ')', 'A list of arguments must be closed by a parenthesis');
return new Node($args); return new Node($args);
} }
@@ -683,11 +683,11 @@ class ExpressionParser
$targets = []; $targets = [];
while (true) { while (true) {
$token = $this->parser->getCurrentToken(); $token = $this->parser->getCurrentToken();
if ($stream->test(/* Token::OPERATOR_TYPE */ 8) && preg_match(Lexer::REGEX_NAME, $token->getValue())) { if ($stream->test(Token::OPERATOR_TYPE) && preg_match(Lexer::REGEX_NAME, $token->getValue())) {
// in this context, string operators are variable names // in this context, string operators are variable names
$this->parser->getStream()->next(); $this->parser->getStream()->next();
} else { } else {
$stream->expect(/* Token::NAME_TYPE */ 5, null, 'Only variables can be assigned to'); $stream->expect(Token::NAME_TYPE, null, 'Only variables can be assigned to');
} }
$value = $token->getValue(); $value = $token->getValue();
if (\in_array(strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), ['true', 'false', 'none', 'null'])) { if (\in_array(strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), ['true', 'false', 'none', 'null'])) {
@@ -695,7 +695,7 @@ class ExpressionParser
} }
$targets[] = new AssignNameExpression($value, $token->getLine()); $targets[] = new AssignNameExpression($value, $token->getLine());
if (!$stream->nextIf(/* Token::PUNCTUATION_TYPE */ 9, ',')) { if (!$stream->nextIf(Token::PUNCTUATION_TYPE, ',')) {
break; break;
} }
} }
@@ -708,7 +708,7 @@ class ExpressionParser
$targets = []; $targets = [];
while (true) { while (true) {
$targets[] = $this->parseExpression(); $targets[] = $this->parseExpression();
if (!$this->parser->getStream()->nextIf(/* Token::PUNCTUATION_TYPE */ 9, ',')) { if (!$this->parser->getStream()->nextIf(Token::PUNCTUATION_TYPE, ',')) {
break; break;
} }
} }
@@ -728,7 +728,7 @@ class ExpressionParser
$class = $this->getTestNodeClass($test); $class = $this->getTestNodeClass($test);
$arguments = null; $arguments = null;
if ($stream->test(/* Token::PUNCTUATION_TYPE */ 9, '(')) { if ($stream->test(Token::PUNCTUATION_TYPE, '(')) {
$arguments = $this->parseArguments(true); $arguments = $this->parseArguments(true);
} elseif ($test->hasOneMandatoryArgument()) { } elseif ($test->hasOneMandatoryArgument()) {
$arguments = new Node([0 => $this->parsePrimaryExpression()]); $arguments = new Node([0 => $this->parsePrimaryExpression()]);
@@ -745,13 +745,13 @@ class ExpressionParser
private function getTest(int $line): array private function getTest(int $line): array
{ {
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
$name = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue(); $name = $stream->expect(Token::NAME_TYPE)->getValue();
if ($test = $this->env->getTest($name)) { if ($test = $this->env->getTest($name)) {
return [$name, $test]; return [$name, $test];
} }
if ($stream->test(/* Token::NAME_TYPE */ 5)) { if ($stream->test(Token::NAME_TYPE)) {
// try 2-words tests // try 2-words tests
$name = $name.' '.$this->parser->getCurrentToken()->getValue(); $name = $name.' '.$this->parser->getCurrentToken()->getValue();
+10 -10
View File
@@ -229,7 +229,7 @@ class Lexer
{ {
// if no matches are left we return the rest of the template as simple text token // if no matches are left we return the rest of the template as simple text token
if ($this->position == \count($this->positions[0]) - 1) { if ($this->position == \count($this->positions[0]) - 1) {
$this->pushToken(/* Token::TEXT_TYPE */ 0, substr($this->code, $this->cursor)); $this->pushToken(Token::TEXT_TYPE, substr($this->code, $this->cursor));
$this->cursor = $this->end; $this->cursor = $this->end;
return; return;
@@ -258,7 +258,7 @@ class Lexer
$text = rtrim($text, " \t\0\x0B"); $text = rtrim($text, " \t\0\x0B");
} }
} }
$this->pushToken(/* Token::TEXT_TYPE */ 0, $text); $this->pushToken(Token::TEXT_TYPE, $text);
$this->moveCursor($textContent.$position[0]); $this->moveCursor($textContent.$position[0]);
switch ($this->positions[1][$this->position][0]) { switch ($this->positions[1][$this->position][0]) {
@@ -335,12 +335,12 @@ class Lexer
} }
// operators // operators
elseif (preg_match($this->regexes['operator'], $this->code, $match, 0, $this->cursor)) { elseif (preg_match($this->regexes['operator'], $this->code, $match, 0, $this->cursor)) {
$this->pushToken(/* Token::OPERATOR_TYPE */ 8, preg_replace('/\s+/', ' ', $match[0])); $this->pushToken(Token::OPERATOR_TYPE, preg_replace('/\s+/', ' ', $match[0]));
$this->moveCursor($match[0]); $this->moveCursor($match[0]);
} }
// names // names
elseif (preg_match(self::REGEX_NAME, $this->code, $match, 0, $this->cursor)) { elseif (preg_match(self::REGEX_NAME, $this->code, $match, 0, $this->cursor)) {
$this->pushToken(/* Token::NAME_TYPE */ 5, $match[0]); $this->pushToken(Token::NAME_TYPE, $match[0]);
$this->moveCursor($match[0]); $this->moveCursor($match[0]);
} }
// numbers // numbers
@@ -349,7 +349,7 @@ class Lexer
if (ctype_digit($match[0]) && $number <= \PHP_INT_MAX) { if (ctype_digit($match[0]) && $number <= \PHP_INT_MAX) {
$number = (int) $match[0]; // integers lower than the maximum $number = (int) $match[0]; // integers lower than the maximum
} }
$this->pushToken(/* Token::NUMBER_TYPE */ 6, $number); $this->pushToken(Token::NUMBER_TYPE, $number);
$this->moveCursor($match[0]); $this->moveCursor($match[0]);
} }
// punctuation // punctuation
@@ -370,12 +370,12 @@ class Lexer
} }
} }
$this->pushToken(/* Token::PUNCTUATION_TYPE */ 9, $this->code[$this->cursor]); $this->pushToken(Token::PUNCTUATION_TYPE, $this->code[$this->cursor]);
++$this->cursor; ++$this->cursor;
} }
// strings // strings
elseif (preg_match(self::REGEX_STRING, $this->code, $match, 0, $this->cursor)) { elseif (preg_match(self::REGEX_STRING, $this->code, $match, 0, $this->cursor)) {
$this->pushToken(/* Token::STRING_TYPE */ 7, $this->stripcslashes(substr($match[0], 1, -1), substr($match[0], 0, 1))); $this->pushToken(Token::STRING_TYPE, $this->stripcslashes(substr($match[0], 1, -1), substr($match[0], 0, 1)));
$this->moveCursor($match[0]); $this->moveCursor($match[0]);
} }
// opening double quoted string // opening double quoted string
@@ -468,7 +468,7 @@ class Lexer
} }
} }
$this->pushToken(/* Token::TEXT_TYPE */ 0, $text); $this->pushToken(Token::TEXT_TYPE, $text);
} }
private function lexComment(): void private function lexComment(): void
@@ -488,7 +488,7 @@ class Lexer
$this->moveCursor($match[0]); $this->moveCursor($match[0]);
$this->pushState(self::STATE_INTERPOLATION); $this->pushState(self::STATE_INTERPOLATION);
} elseif (preg_match(self::REGEX_DQ_STRING_PART, $this->code, $match, 0, $this->cursor) && '' !== $match[0]) { } elseif (preg_match(self::REGEX_DQ_STRING_PART, $this->code, $match, 0, $this->cursor) && '' !== $match[0]) {
$this->pushToken(/* Token::STRING_TYPE */ 7, $this->stripcslashes($match[0], '"')); $this->pushToken(Token::STRING_TYPE, $this->stripcslashes($match[0], '"'));
$this->moveCursor($match[0]); $this->moveCursor($match[0]);
} elseif (preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, 0, $this->cursor)) { } elseif (preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, 0, $this->cursor)) {
[$expect, $lineno] = array_pop($this->brackets); [$expect, $lineno] = array_pop($this->brackets);
@@ -520,7 +520,7 @@ class Lexer
private function pushToken($type, $value = ''): void private function pushToken($type, $value = ''): void
{ {
// do not push empty text tokens // do not push empty text tokens
if (/* Token::TEXT_TYPE */ 0 === $type && '' === $value) { if (Token::TEXT_TYPE === $type && '' === $value) {
return; return;
} }
+2 -2
View File
@@ -121,7 +121,7 @@ class Parser
$rv = []; $rv = [];
while (!$this->stream->isEOF()) { while (!$this->stream->isEOF()) {
switch ($this->getCurrentToken()->getType()) { switch ($this->getCurrentToken()->getType()) {
case /* Token::TEXT_TYPE */ 0: case Token::TEXT_TYPE:
$token = $this->stream->next(); $token = $this->stream->next();
$rv[] = new TextNode($token->getValue(), $token->getLine()); $rv[] = new TextNode($token->getValue(), $token->getLine());
break; break;
@@ -137,7 +137,7 @@ class Parser
$this->stream->next(); $this->stream->next();
$token = $this->getCurrentToken(); $token = $this->getCurrentToken();
if (/* Token::NAME_TYPE */ 5 !== $token->getType()) { if (Token::NAME_TYPE !== $token->getType()) {
throw new SyntaxError('A block must start with a tag name.', $token->getLine(), $this->stream->getSourceContext()); throw new SyntaxError('A block must start with a tag name.', $token->getLine(), $this->stream->getSourceContext());
} }
+2 -2
View File
@@ -35,7 +35,7 @@ final class BlockTokenParser extends AbstractTokenParser
{ {
$lineno = $token->getLine(); $lineno = $token->getLine();
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
$name = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue(); $name = $stream->expect(Token::NAME_TYPE)->getValue();
if ($this->parser->hasBlock($name)) { if ($this->parser->hasBlock($name)) {
throw new SyntaxError(\sprintf("The block '%s' has already been defined line %d.", $name, $this->parser->getBlock($name)->getTemplateLine()), $stream->getCurrent()->getLine(), $stream->getSourceContext()); throw new SyntaxError(\sprintf("The block '%s' has already been defined line %d.", $name, $this->parser->getBlock($name)->getTemplateLine()), $stream->getCurrent()->getLine(), $stream->getSourceContext());
} }
@@ -45,7 +45,7 @@ final class BlockTokenParser extends AbstractTokenParser
if ($stream->nextIf(/* Token::BLOCK_END_TYPE */ 3)) { if ($stream->nextIf(/* Token::BLOCK_END_TYPE */ 3)) {
$body = $this->parser->subparse([$this, 'decideBlockEnd'], true); $body = $this->parser->subparse([$this, 'decideBlockEnd'], true);
if ($token = $stream->nextIf(/* Token::NAME_TYPE */ 5)) { if ($token = $stream->nextIf(Token::NAME_TYPE)) {
$value = $token->getValue(); $value = $token->getValue();
if ($value != $name) { if ($value != $name) {
+4 -4
View File
@@ -32,17 +32,17 @@ final class EmbedTokenParser extends IncludeTokenParser
[$variables, $only, $ignoreMissing] = $this->parseArguments(); [$variables, $only, $ignoreMissing] = $this->parseArguments();
$parentToken = $fakeParentToken = new Token(/* Token::STRING_TYPE */ 7, '__parent__', $token->getLine()); $parentToken = $fakeParentToken = new Token(Token::STRING_TYPE, '__parent__', $token->getLine());
if ($parent instanceof ConstantExpression) { if ($parent instanceof ConstantExpression) {
$parentToken = new Token(/* Token::STRING_TYPE */ 7, $parent->getAttribute('value'), $token->getLine()); $parentToken = new Token(Token::STRING_TYPE, $parent->getAttribute('value'), $token->getLine());
} elseif ($parent instanceof NameExpression) { } elseif ($parent instanceof NameExpression) {
$parentToken = new Token(/* Token::NAME_TYPE */ 5, $parent->getAttribute('name'), $token->getLine()); $parentToken = new Token(Token::NAME_TYPE, $parent->getAttribute('name'), $token->getLine());
} }
// inject a fake parent to make the parent() function work // inject a fake parent to make the parent() function work
$stream->injectTokens([ $stream->injectTokens([
new Token(/* Token::BLOCK_START_TYPE */ 1, '', $token->getLine()), new Token(/* Token::BLOCK_START_TYPE */ 1, '', $token->getLine()),
new Token(/* Token::NAME_TYPE */ 5, 'extends', $token->getLine()), new Token(Token::NAME_TYPE, 'extends', $token->getLine()),
$parentToken, $parentToken,
new Token(/* Token::BLOCK_END_TYPE */ 3, '', $token->getLine()), new Token(/* Token::BLOCK_END_TYPE */ 3, '', $token->getLine()),
]); ]);
+1 -1
View File
@@ -35,7 +35,7 @@ final class ForTokenParser extends AbstractTokenParser
$lineno = $token->getLine(); $lineno = $token->getLine();
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
$targets = $this->parser->getExpressionParser()->parseAssignmentExpression(); $targets = $this->parser->getExpressionParser()->parseAssignmentExpression();
$stream->expect(/* Token::OPERATOR_TYPE */ 8, 'in'); $stream->expect(Token::OPERATOR_TYPE, 'in');
$seq = $this->parser->getExpressionParser()->parseExpression(); $seq = $this->parser->getExpressionParser()->parseExpression();
$stream->expect(/* Token::BLOCK_END_TYPE */ 3); $stream->expect(/* Token::BLOCK_END_TYPE */ 3);
+4 -4
View File
@@ -29,20 +29,20 @@ final class FromTokenParser extends AbstractTokenParser
{ {
$macro = $this->parser->getExpressionParser()->parseExpression(); $macro = $this->parser->getExpressionParser()->parseExpression();
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
$stream->expect(/* Token::NAME_TYPE */ 5, 'import'); $stream->expect(Token::NAME_TYPE, 'import');
$targets = []; $targets = [];
while (true) { while (true) {
$name = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue(); $name = $stream->expect(Token::NAME_TYPE)->getValue();
$alias = $name; $alias = $name;
if ($stream->nextIf('as')) { if ($stream->nextIf('as')) {
$alias = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue(); $alias = $stream->expect(Token::NAME_TYPE)->getValue();
} }
$targets[$name] = $alias; $targets[$name] = $alias;
if (!$stream->nextIf(/* Token::PUNCTUATION_TYPE */ 9, ',')) { if (!$stream->nextIf(Token::PUNCTUATION_TYPE, ',')) {
break; break;
} }
} }
+2 -2
View File
@@ -28,8 +28,8 @@ final class ImportTokenParser extends AbstractTokenParser
public function parse(Token $token): Node public function parse(Token $token): Node
{ {
$macro = $this->parser->getExpressionParser()->parseExpression(); $macro = $this->parser->getExpressionParser()->parseExpression();
$this->parser->getStream()->expect(/* Token::NAME_TYPE */ 5, 'as'); $this->parser->getStream()->expect(Token::NAME_TYPE, 'as');
$var = new AssignNameExpression($this->parser->getStream()->expect(/* Token::NAME_TYPE */ 5)->getValue(), $token->getLine()); $var = new AssignNameExpression($this->parser->getStream()->expect(Token::NAME_TYPE)->getValue(), $token->getLine());
$this->parser->getStream()->expect(/* Token::BLOCK_END_TYPE */ 3); $this->parser->getStream()->expect(/* Token::BLOCK_END_TYPE */ 3);
$this->parser->addImportedSymbol('template', $var->getAttribute('name')); $this->parser->addImportedSymbol('template', $var->getAttribute('name'));
+4 -4
View File
@@ -41,19 +41,19 @@ class IncludeTokenParser extends AbstractTokenParser
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
$ignoreMissing = false; $ignoreMissing = false;
if ($stream->nextIf(/* Token::NAME_TYPE */ 5, 'ignore')) { if ($stream->nextIf(Token::NAME_TYPE, 'ignore')) {
$stream->expect(/* Token::NAME_TYPE */ 5, 'missing'); $stream->expect(Token::NAME_TYPE, 'missing');
$ignoreMissing = true; $ignoreMissing = true;
} }
$variables = null; $variables = null;
if ($stream->nextIf(/* Token::NAME_TYPE */ 5, 'with')) { if ($stream->nextIf(Token::NAME_TYPE, 'with')) {
$variables = $this->parser->getExpressionParser()->parseExpression(); $variables = $this->parser->getExpressionParser()->parseExpression();
} }
$only = false; $only = false;
if ($stream->nextIf(/* Token::NAME_TYPE */ 5, 'only')) { if ($stream->nextIf(Token::NAME_TYPE, 'only')) {
$only = true; $only = true;
} }
+2 -2
View File
@@ -32,14 +32,14 @@ final class MacroTokenParser extends AbstractTokenParser
{ {
$lineno = $token->getLine(); $lineno = $token->getLine();
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
$name = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue(); $name = $stream->expect(Token::NAME_TYPE)->getValue();
$arguments = $this->parser->getExpressionParser()->parseArguments(true, true); $arguments = $this->parser->getExpressionParser()->parseArguments(true, true);
$stream->expect(/* Token::BLOCK_END_TYPE */ 3); $stream->expect(/* Token::BLOCK_END_TYPE */ 3);
$this->parser->pushLocalScope(); $this->parser->pushLocalScope();
$body = $this->parser->subparse([$this, 'decideBlockEnd'], true); $body = $this->parser->subparse([$this, 'decideBlockEnd'], true);
if ($token = $stream->nextIf(/* Token::NAME_TYPE */ 5)) { if ($token = $stream->nextIf(Token::NAME_TYPE)) {
$value = $token->getValue(); $value = $token->getValue();
if ($value != $name) { if ($value != $name) {
+1 -1
View File
@@ -37,7 +37,7 @@ final class SetTokenParser extends AbstractTokenParser
$names = $this->parser->getExpressionParser()->parseAssignmentExpression(); $names = $this->parser->getExpressionParser()->parseAssignmentExpression();
$capture = false; $capture = false;
if ($stream->nextIf(/* Token::OPERATOR_TYPE */ 8, '=')) { if ($stream->nextIf(Token::OPERATOR_TYPE, '=')) {
$values = $this->parser->getExpressionParser()->parseMultitargetExpression(); $values = $this->parser->getExpressionParser()->parseMultitargetExpression();
$stream->expect(/* Token::BLOCK_END_TYPE */ 3); $stream->expect(/* Token::BLOCK_END_TYPE */ 3);
+3 -3
View File
@@ -44,16 +44,16 @@ final class UseTokenParser extends AbstractTokenParser
$targets = []; $targets = [];
if ($stream->nextIf('with')) { if ($stream->nextIf('with')) {
while (true) { while (true) {
$name = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue(); $name = $stream->expect(Token::NAME_TYPE)->getValue();
$alias = $name; $alias = $name;
if ($stream->nextIf('as')) { if ($stream->nextIf('as')) {
$alias = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue(); $alias = $stream->expect(Token::NAME_TYPE)->getValue();
} }
$targets[$name] = new ConstantExpression($alias, -1); $targets[$name] = new ConstantExpression($alias, -1);
if (!$stream->nextIf(/* Token::PUNCTUATION_TYPE */ 9, ',')) { if (!$stream->nextIf(Token::PUNCTUATION_TYPE, ',')) {
break; break;
} }
} }
+1 -1
View File
@@ -32,7 +32,7 @@ final class WithTokenParser extends AbstractTokenParser
$only = false; $only = false;
if (!$stream->test(/* Token::BLOCK_END_TYPE */ 3)) { if (!$stream->test(/* Token::BLOCK_END_TYPE */ 3)) {
$variables = $this->parser->getExpressionParser()->parseExpression(); $variables = $this->parser->getExpressionParser()->parseExpression();
$only = (bool) $stream->nextIf(/* Token::NAME_TYPE */ 5, 'only'); $only = (bool) $stream->nextIf(Token::NAME_TYPE, 'only');
} }
$stream->expect(/* Token::BLOCK_END_TYPE */ 3); $stream->expect(/* Token::BLOCK_END_TYPE */ 3);