minor #2650 Optimize parser (fabpot)

This PR was merged into the 2.x branch.

Discussion
----------

Optimize parser

Depending on the complexity of the template, I see up to 50% faster template compilation.

Commits
-------

510e5eba optimized the parser by inlining the constant values
This commit is contained in:
Fabien Potencier
2018-03-04 10:02:05 -08:00
22 changed files with 138 additions and 138 deletions
+56 -56
View File
@@ -81,10 +81,10 @@ class Twig_ExpressionParser
$class = $operator['class'];
return $this->parsePostfixExpression(new $class($expr, $token->getLine()));
} elseif ($token->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
} elseif ($token->test(/* Twig_Token::PUNCTUATION_TYPE */ 9, '(')) {
$this->parser->getStream()->next();
$expr = $this->parseExpression();
$this->parser->getStream()->expect(Twig_Token::PUNCTUATION_TYPE, ')', 'An opened parenthesis is not properly closed');
$this->parser->getStream()->expect(/* Twig_Token::PUNCTUATION_TYPE */ 9, ')', 'An opened parenthesis is not properly closed');
return $this->parsePostfixExpression($expr);
}
@@ -94,10 +94,10 @@ class Twig_ExpressionParser
private function parseConditionalExpression($expr)
{
while ($this->parser->getStream()->nextIf(Twig_Token::PUNCTUATION_TYPE, '?')) {
if (!$this->parser->getStream()->nextIf(Twig_Token::PUNCTUATION_TYPE, ':')) {
while ($this->parser->getStream()->nextIf(/* Twig_Token::PUNCTUATION_TYPE */ 9, '?')) {
if (!$this->parser->getStream()->nextIf(/* Twig_Token::PUNCTUATION_TYPE */ 9, ':')) {
$expr2 = $this->parseExpression();
if ($this->parser->getStream()->nextIf(Twig_Token::PUNCTUATION_TYPE, ':')) {
if ($this->parser->getStream()->nextIf(/* Twig_Token::PUNCTUATION_TYPE */ 9, ':')) {
$expr3 = $this->parseExpression();
} else {
$expr3 = new Twig_Node_Expression_Constant('', $this->parser->getCurrentToken()->getLine());
@@ -115,19 +115,19 @@ class Twig_ExpressionParser
private function isUnary(Twig_Token $token)
{
return $token->test(Twig_Token::OPERATOR_TYPE) && isset($this->unaryOperators[$token->getValue()]);
return $token->test(/* Twig_Token::OPERATOR_TYPE */ 8) && isset($this->unaryOperators[$token->getValue()]);
}
private function isBinary(Twig_Token $token)
{
return $token->test(Twig_Token::OPERATOR_TYPE) && isset($this->binaryOperators[$token->getValue()]);
return $token->test(/* Twig_Token::OPERATOR_TYPE */ 8) && isset($this->binaryOperators[$token->getValue()]);
}
public function parsePrimaryExpression()
{
$token = $this->parser->getCurrentToken();
switch ($token->getType()) {
case Twig_Token::NAME_TYPE:
case /* Twig_Token::NAME_TYPE */ 5:
$this->parser->getStream()->next();
switch ($token->getValue()) {
case 'true':
@@ -156,17 +156,17 @@ class Twig_ExpressionParser
}
break;
case Twig_Token::NUMBER_TYPE:
case /* Twig_Token::NUMBER_TYPE */ 6:
$this->parser->getStream()->next();
$node = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
break;
case Twig_Token::STRING_TYPE:
case Twig_Token::INTERPOLATION_START_TYPE:
case /* Twig_Token::STRING_TYPE */ 7:
case /* Twig_Token::INTERPOLATION_START_TYPE */ 10:
$node = $this->parseStringExpression();
break;
case Twig_Token::OPERATOR_TYPE:
case /* Twig_Token::OPERATOR_TYPE */ 8:
if (preg_match(Twig_Lexer::REGEX_NAME, $token->getValue(), $matches) && $matches[0] == $token->getValue()) {
// in this context, string operators are variable names
$this->parser->getStream()->next();
@@ -191,11 +191,11 @@ class Twig_ExpressionParser
// no break
default:
if ($token->test(Twig_Token::PUNCTUATION_TYPE, '[')) {
if ($token->test(/* Twig_Token::PUNCTUATION_TYPE */ 9, '[')) {
$node = $this->parseArrayExpression();
} elseif ($token->test(Twig_Token::PUNCTUATION_TYPE, '{')) {
} elseif ($token->test(/* Twig_Token::PUNCTUATION_TYPE */ 9, '{')) {
$node = $this->parseHashExpression();
} elseif ($token->test(Twig_Token::OPERATOR_TYPE, '=') && ('==' === $this->parser->getStream()->look(-1)->getValue() || '!=' === $this->parser->getStream()->look(-1)->getValue())) {
} elseif ($token->test(/* Twig_Token::OPERATOR_TYPE */ 8, '=') && ('==' === $this->parser->getStream()->look(-1)->getValue() || '!=' === $this->parser->getStream()->look(-1)->getValue())) {
throw new Twig_Error_Syntax(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 {
throw new Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s".', Twig_Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext());
@@ -213,12 +213,12 @@ class Twig_ExpressionParser
// a string cannot be followed by another string in a single expression
$nextCanBeString = true;
while (true) {
if ($nextCanBeString && $token = $stream->nextIf(Twig_Token::STRING_TYPE)) {
if ($nextCanBeString && $token = $stream->nextIf(/* Twig_Token::STRING_TYPE */ 7)) {
$nodes[] = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
$nextCanBeString = false;
} elseif ($stream->nextIf(Twig_Token::INTERPOLATION_START_TYPE)) {
} elseif ($stream->nextIf(/* Twig_Token::INTERPOLATION_START_TYPE */ 10)) {
$nodes[] = $this->parseExpression();
$stream->expect(Twig_Token::INTERPOLATION_END_TYPE);
$stream->expect(/* Twig_Token::INTERPOLATION_END_TYPE */ 11);
$nextCanBeString = true;
} else {
break;
@@ -236,16 +236,16 @@ class Twig_ExpressionParser
public function parseArrayExpression()
{
$stream = $this->parser->getStream();
$stream->expect(Twig_Token::PUNCTUATION_TYPE, '[', 'An array element was expected');
$stream->expect(/* Twig_Token::PUNCTUATION_TYPE */ 9, '[', 'An array element was expected');
$node = new Twig_Node_Expression_Array(array(), $stream->getCurrent()->getLine());
$first = true;
while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, ']')) {
while (!$stream->test(/* Twig_Token::PUNCTUATION_TYPE */ 9, ']')) {
if (!$first) {
$stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'An array element must be followed by a comma');
$stream->expect(/* Twig_Token::PUNCTUATION_TYPE */ 9, ',', 'An array element must be followed by a comma');
// trailing ,?
if ($stream->test(Twig_Token::PUNCTUATION_TYPE, ']')) {
if ($stream->test(/* Twig_Token::PUNCTUATION_TYPE */ 9, ']')) {
break;
}
}
@@ -253,7 +253,7 @@ class Twig_ExpressionParser
$node->addElement($this->parseExpression());
}
$stream->expect(Twig_Token::PUNCTUATION_TYPE, ']', 'An opened array is not properly closed');
$stream->expect(/* Twig_Token::PUNCTUATION_TYPE */ 9, ']', 'An opened array is not properly closed');
return $node;
}
@@ -261,16 +261,16 @@ class Twig_ExpressionParser
public function parseHashExpression()
{
$stream = $this->parser->getStream();
$stream->expect(Twig_Token::PUNCTUATION_TYPE, '{', 'A hash element was expected');
$stream->expect(/* Twig_Token::PUNCTUATION_TYPE */ 9, '{', 'A hash element was expected');
$node = new Twig_Node_Expression_Array(array(), $stream->getCurrent()->getLine());
$first = true;
while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) {
while (!$stream->test(/* Twig_Token::PUNCTUATION_TYPE */ 9, '}')) {
if (!$first) {
$stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'A hash value must be followed by a comma');
$stream->expect(/* Twig_Token::PUNCTUATION_TYPE */ 9, ',', 'A hash value must be followed by a comma');
// trailing ,?
if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) {
if ($stream->test(/* Twig_Token::PUNCTUATION_TYPE */ 9, '}')) {
break;
}
}
@@ -282,9 +282,9 @@ class Twig_ExpressionParser
// * a string -- 'a'
// * a name, which is equivalent to a string -- a
// * an expression, which must be enclosed in parentheses -- (1 + 2)
if (($token = $stream->nextIf(Twig_Token::STRING_TYPE)) || ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) || $token = $stream->nextIf(Twig_Token::NUMBER_TYPE)) {
if (($token = $stream->nextIf(/* Twig_Token::STRING_TYPE */ 7)) || ($token = $stream->nextIf(/* Twig_Token::NAME_TYPE */ 5)) || $token = $stream->nextIf(/* Twig_Token::NUMBER_TYPE */ 6)) {
$key = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
} elseif ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
} elseif ($stream->test(/* Twig_Token::PUNCTUATION_TYPE */ 9, '(')) {
$key = $this->parseExpression();
} else {
$current = $stream->getCurrent();
@@ -292,12 +292,12 @@ class Twig_ExpressionParser
throw new Twig_Error_Syntax(sprintf('A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "%s" of value "%s".', Twig_Token::typeToEnglish($current->getType()), $current->getValue()), $current->getLine(), $stream->getSourceContext());
}
$stream->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'A hash key must be followed by a colon (:)');
$stream->expect(/* Twig_Token::PUNCTUATION_TYPE */ 9, ':', 'A hash key must be followed by a colon (:)');
$value = $this->parseExpression();
$node->addElement($value, $key);
}
$stream->expect(Twig_Token::PUNCTUATION_TYPE, '}', 'An opened hash is not properly closed');
$stream->expect(/* Twig_Token::PUNCTUATION_TYPE */ 9, '}', 'An opened hash is not properly closed');
return $node;
}
@@ -306,7 +306,7 @@ class Twig_ExpressionParser
{
while (true) {
$token = $this->parser->getCurrentToken();
if (Twig_Token::PUNCTUATION_TYPE == $token->getType()) {
if (/* Twig_Token::PUNCTUATION_TYPE */ 9 == $token->getType()) {
if ('.' == $token->getValue() || '[' == $token->getValue()) {
$node = $this->parseSubscriptExpression($node);
} elseif ('|' == $token->getValue()) {
@@ -380,15 +380,15 @@ class Twig_ExpressionParser
if ('.' == $token->getValue()) {
$token = $stream->next();
if (
Twig_Token::NAME_TYPE == $token->getType()
/* Twig_Token::NAME_TYPE */ 5 == $token->getType()
||
Twig_Token::NUMBER_TYPE == $token->getType()
/* Twig_Token::NUMBER_TYPE */ 6 == $token->getType()
||
(Twig_Token::OPERATOR_TYPE == $token->getType() && preg_match(Twig_Lexer::REGEX_NAME, $token->getValue()))
(/* Twig_Token::OPERATOR_TYPE */ 8 == $token->getType() && preg_match(Twig_Lexer::REGEX_NAME, $token->getValue()))
) {
$arg = new Twig_Node_Expression_Constant($token->getValue(), $lineno);
if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
if ($stream->test(/* Twig_Token::PUNCTUATION_TYPE */ 9, '(')) {
$type = Twig_Template::METHOD_CALL;
foreach ($this->parseArguments() as $n) {
$arguments->addElement($n);
@@ -415,19 +415,19 @@ class Twig_ExpressionParser
// slice?
$slice = false;
if ($stream->test(Twig_Token::PUNCTUATION_TYPE, ':')) {
if ($stream->test(/* Twig_Token::PUNCTUATION_TYPE */ 9, ':')) {
$slice = true;
$arg = new Twig_Node_Expression_Constant(0, $token->getLine());
} else {
$arg = $this->parseExpression();
}
if ($stream->nextIf(Twig_Token::PUNCTUATION_TYPE, ':')) {
if ($stream->nextIf(/* Twig_Token::PUNCTUATION_TYPE */ 9, ':')) {
$slice = true;
}
if ($slice) {
if ($stream->test(Twig_Token::PUNCTUATION_TYPE, ']')) {
if ($stream->test(/* Twig_Token::PUNCTUATION_TYPE */ 9, ']')) {
$length = new Twig_Node_Expression_Constant(null, $token->getLine());
} else {
$length = $this->parseExpression();
@@ -437,12 +437,12 @@ class Twig_ExpressionParser
$arguments = new Twig_Node(array($arg, $length));
$filter = new $class($node, new Twig_Node_Expression_Constant('slice', $token->getLine()), $arguments, $token->getLine());
$stream->expect(Twig_Token::PUNCTUATION_TYPE, ']');
$stream->expect(/* Twig_Token::PUNCTUATION_TYPE */ 9, ']');
return $filter;
}
$stream->expect(Twig_Token::PUNCTUATION_TYPE, ']');
$stream->expect(/* Twig_Token::PUNCTUATION_TYPE */ 9, ']');
}
return new Twig_Node_Expression_GetAttr($node, $arg, $arguments, $type, $lineno);
@@ -458,10 +458,10 @@ class Twig_ExpressionParser
public function parseFilterExpressionRaw($node, $tag = null)
{
while (true) {
$token = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE);
$token = $this->parser->getStream()->expect(/* Twig_Token::NAME_TYPE */ 5);
$name = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
if (!$this->parser->getStream()->test(/* Twig_Token::PUNCTUATION_TYPE */ 9, '(')) {
$arguments = new Twig_Node();
} else {
$arguments = $this->parseArguments(true);
@@ -471,7 +471,7 @@ class Twig_ExpressionParser
$node = new $class($node, $name, $arguments, $token->getLine(), $tag);
if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '|')) {
if (!$this->parser->getStream()->test(/* Twig_Token::PUNCTUATION_TYPE */ 9, '|')) {
break;
}
@@ -496,21 +496,21 @@ class Twig_ExpressionParser
$args = array();
$stream = $this->parser->getStream();
$stream->expect(Twig_Token::PUNCTUATION_TYPE, '(', 'A list of arguments must begin with an opening parenthesis');
while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, ')')) {
$stream->expect(/* Twig_Token::PUNCTUATION_TYPE */ 9, '(', 'A list of arguments must begin with an opening parenthesis');
while (!$stream->test(/* Twig_Token::PUNCTUATION_TYPE */ 9, ')')) {
if (!empty($args)) {
$stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'Arguments must be separated by a comma');
$stream->expect(/* Twig_Token::PUNCTUATION_TYPE */ 9, ',', 'Arguments must be separated by a comma');
}
if ($definition) {
$token = $stream->expect(Twig_Token::NAME_TYPE, null, 'An argument must be a name');
$token = $stream->expect(/* Twig_Token::NAME_TYPE */ 5, null, 'An argument must be a name');
$value = new Twig_Node_Expression_Name($token->getValue(), $this->parser->getCurrentToken()->getLine());
} else {
$value = $this->parseExpression();
}
$name = null;
if ($namedArguments && $token = $stream->nextIf(Twig_Token::OPERATOR_TYPE, '=')) {
if ($namedArguments && $token = $stream->nextIf(/* Twig_Token::OPERATOR_TYPE */ 8, '=')) {
if (!$value instanceof Twig_Node_Expression_Name) {
throw new Twig_Error_Syntax(sprintf('A parameter name must be a string, "%s" given.', get_class($value)), $token->getLine(), $stream->getSourceContext());
}
@@ -541,7 +541,7 @@ class Twig_ExpressionParser
}
}
}
$stream->expect(Twig_Token::PUNCTUATION_TYPE, ')', 'A list of arguments must be closed by a parenthesis');
$stream->expect(/* Twig_Token::PUNCTUATION_TYPE */ 9, ')', 'A list of arguments must be closed by a parenthesis');
return new Twig_Node($args);
}
@@ -551,14 +551,14 @@ class Twig_ExpressionParser
$stream = $this->parser->getStream();
$targets = array();
while (true) {
$token = $stream->expect(Twig_Token::NAME_TYPE, null, 'Only variables can be assigned to');
$token = $stream->expect(/* Twig_Token::NAME_TYPE */ 5, null, 'Only variables can be assigned to');
$value = $token->getValue();
if (in_array(strtolower($value), array('true', 'false', 'none', 'null'))) {
throw new Twig_Error_Syntax(sprintf('You cannot assign a value to "%s".', $value), $token->getLine(), $stream->getSourceContext());
}
$targets[] = new Twig_Node_Expression_AssignName($value, $token->getLine());
if (!$stream->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
if (!$stream->nextIf(/* Twig_Token::PUNCTUATION_TYPE */ 9, ',')) {
break;
}
}
@@ -571,7 +571,7 @@ class Twig_ExpressionParser
$targets = array();
while (true) {
$targets[] = $this->parseExpression();
if (!$this->parser->getStream()->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
if (!$this->parser->getStream()->nextIf(/* Twig_Token::PUNCTUATION_TYPE */ 9, ',')) {
break;
}
}
@@ -591,7 +591,7 @@ class Twig_ExpressionParser
$class = $this->getTestNodeClass($test);
$arguments = null;
if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
if ($stream->test(/* Twig_Token::PUNCTUATION_TYPE */ 9, '(')) {
$arguments = $this->parser->getExpressionParser()->parseArguments(true);
}
@@ -601,13 +601,13 @@ class Twig_ExpressionParser
private function getTest($line)
{
$stream = $this->parser->getStream();
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
$name = $stream->expect(/* Twig_Token::NAME_TYPE */ 5)->getValue();
if ($test = $this->env->getTest($name)) {
return array($name, $test);
}
if ($stream->test(Twig_Token::NAME_TYPE)) {
if ($stream->test(/* Twig_Token::NAME_TYPE */ 5)) {
// try 2-words tests
$name = $name.' '.$this->parser->getCurrentToken()->getValue();
+17 -17
View File
@@ -115,7 +115,7 @@ class Twig_Lexer
}
}
$this->pushToken(Twig_Token::EOF_TYPE);
$this->pushToken(/* Twig_Token::EOF_TYPE */ -1);
if (!empty($this->brackets)) {
list($expect, $lineno) = array_pop($this->brackets);
@@ -129,7 +129,7 @@ class Twig_Lexer
{
// if no matches are left we return the rest of the template as simple text token
if ($this->position == count($this->positions[0]) - 1) {
$this->pushToken(Twig_Token::TEXT_TYPE, substr($this->code, $this->cursor));
$this->pushToken(/* Twig_Token::TEXT_TYPE */ 0, substr($this->code, $this->cursor));
$this->cursor = $this->end;
return;
@@ -149,7 +149,7 @@ class Twig_Lexer
if (isset($this->positions[2][$this->position][0])) {
$text = rtrim($text);
}
$this->pushToken(Twig_Token::TEXT_TYPE, $text);
$this->pushToken(/* Twig_Token::TEXT_TYPE */ 0, $text);
$this->moveCursor($textContent.$position[0]);
switch ($this->positions[1][$this->position][0]) {
@@ -167,14 +167,14 @@ class Twig_Lexer
$this->moveCursor($match[0]);
$this->lineno = (int) $match[1];
} else {
$this->pushToken(Twig_Token::BLOCK_START_TYPE);
$this->pushToken(/* Twig_Token::BLOCK_START_TYPE */ 1);
$this->pushState(self::STATE_BLOCK);
$this->currentVarBlockLine = $this->lineno;
}
break;
case $this->options['tag_variable'][0]:
$this->pushToken(Twig_Token::VAR_START_TYPE);
$this->pushToken(/* Twig_Token::VAR_START_TYPE */ 2);
$this->pushState(self::STATE_VAR);
$this->currentVarBlockLine = $this->lineno;
break;
@@ -184,7 +184,7 @@ class Twig_Lexer
private function lexBlock()
{
if (empty($this->brackets) && preg_match($this->regexes['lex_block'], $this->code, $match, null, $this->cursor)) {
$this->pushToken(Twig_Token::BLOCK_END_TYPE);
$this->pushToken(/* Twig_Token::BLOCK_END_TYPE */ 3);
$this->moveCursor($match[0]);
$this->popState();
} else {
@@ -195,7 +195,7 @@ class Twig_Lexer
private function lexVar()
{
if (empty($this->brackets) && preg_match($this->regexes['lex_var'], $this->code, $match, null, $this->cursor)) {
$this->pushToken(Twig_Token::VAR_END_TYPE);
$this->pushToken(/* Twig_Token::VAR_END_TYPE */ 4);
$this->moveCursor($match[0]);
$this->popState();
} else {
@@ -216,12 +216,12 @@ class Twig_Lexer
// operators
if (preg_match($this->regexes['operator'], $this->code, $match, null, $this->cursor)) {
$this->pushToken(Twig_Token::OPERATOR_TYPE, preg_replace('/\s+/', ' ', $match[0]));
$this->pushToken(/* Twig_Token::OPERATOR_TYPE */ 8, preg_replace('/\s+/', ' ', $match[0]));
$this->moveCursor($match[0]);
}
// names
elseif (preg_match(self::REGEX_NAME, $this->code, $match, null, $this->cursor)) {
$this->pushToken(Twig_Token::NAME_TYPE, $match[0]);
$this->pushToken(/* Twig_Token::NAME_TYPE */ 5, $match[0]);
$this->moveCursor($match[0]);
}
// numbers
@@ -230,7 +230,7 @@ class Twig_Lexer
if (ctype_digit($match[0]) && $number <= PHP_INT_MAX) {
$number = (int) $match[0]; // integers lower than the maximum
}
$this->pushToken(Twig_Token::NUMBER_TYPE, $number);
$this->pushToken(/* Twig_Token::NUMBER_TYPE */ 6, $number);
$this->moveCursor($match[0]);
}
// punctuation
@@ -251,12 +251,12 @@ class Twig_Lexer
}
}
$this->pushToken(Twig_Token::PUNCTUATION_TYPE, $this->code[$this->cursor]);
$this->pushToken(/* Twig_Token::PUNCTUATION_TYPE */ 9, $this->code[$this->cursor]);
++$this->cursor;
}
// strings
elseif (preg_match(self::REGEX_STRING, $this->code, $match, null, $this->cursor)) {
$this->pushToken(Twig_Token::STRING_TYPE, stripcslashes(substr($match[0], 1, -1)));
$this->pushToken(/* Twig_Token::STRING_TYPE */ 7, stripcslashes(substr($match[0], 1, -1)));
$this->moveCursor($match[0]);
}
// opening double quoted string
@@ -284,7 +284,7 @@ class Twig_Lexer
$text = rtrim($text);
}
$this->pushToken(Twig_Token::TEXT_TYPE, $text);
$this->pushToken(/* Twig_Token::TEXT_TYPE */ 0, $text);
}
private function lexComment()
@@ -300,11 +300,11 @@ class Twig_Lexer
{
if (preg_match($this->regexes['interpolation_start'], $this->code, $match, null, $this->cursor)) {
$this->brackets[] = array($this->options['interpolation'][0], $this->lineno);
$this->pushToken(Twig_Token::INTERPOLATION_START_TYPE);
$this->pushToken(/* Twig_Token::INTERPOLATION_START_TYPE */ 10);
$this->moveCursor($match[0]);
$this->pushState(self::STATE_INTERPOLATION);
} elseif (preg_match(self::REGEX_DQ_STRING_PART, $this->code, $match, null, $this->cursor) && strlen($match[0]) > 0) {
$this->pushToken(Twig_Token::STRING_TYPE, stripcslashes($match[0]));
$this->pushToken(/* Twig_Token::STRING_TYPE */ 7, stripcslashes($match[0]));
$this->moveCursor($match[0]);
} elseif (preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, null, $this->cursor)) {
list($expect, $lineno) = array_pop($this->brackets);
@@ -322,7 +322,7 @@ class Twig_Lexer
$bracket = end($this->brackets);
if ($this->options['interpolation'][0] === $bracket[0] && preg_match($this->regexes['interpolation_end'], $this->code, $match, null, $this->cursor)) {
array_pop($this->brackets);
$this->pushToken(Twig_Token::INTERPOLATION_END_TYPE);
$this->pushToken(/* Twig_Token::INTERPOLATION_END_TYPE */ 11);
$this->moveCursor($match[0]);
$this->popState();
} else {
@@ -333,7 +333,7 @@ class Twig_Lexer
private function pushToken($type, $value = '')
{
// do not push empty text tokens
if (Twig_Token::TEXT_TYPE === $type && '' === $value) {
if (/* Twig_Token::TEXT_TYPE */ 0 === $type && '' === $value) {
return;
}
+5 -5
View File
@@ -115,23 +115,23 @@ class Twig_Parser
$rv = array();
while (!$this->stream->isEOF()) {
switch ($this->getCurrentToken()->getType()) {
case Twig_Token::TEXT_TYPE:
case /* Twig_Token::TEXT_TYPE */ 0:
$token = $this->stream->next();
$rv[] = new Twig_Node_Text($token->getValue(), $token->getLine());
break;
case Twig_Token::VAR_START_TYPE:
case /* Twig_Token::VAR_START_TYPE */ 2:
$token = $this->stream->next();
$expr = $this->expressionParser->parseExpression();
$this->stream->expect(Twig_Token::VAR_END_TYPE);
$this->stream->expect(/* Twig_Token::VAR_END_TYPE */ 4);
$rv[] = new Twig_Node_Print($expr, $token->getLine());
break;
case Twig_Token::BLOCK_START_TYPE:
case /* Twig_Token::BLOCK_START_TYPE */ 1:
$this->stream->next();
$token = $this->getCurrentToken();
if (Twig_Token::NAME_TYPE !== $token->getType()) {
if (/* Twig_Token::NAME_TYPE */ 5 !== $token->getType()) {
throw new Twig_Error_Syntax('A block must start with a tag name.', $token->getLine(), $this->stream->getSourceContext());
}
+3 -3
View File
@@ -19,7 +19,7 @@ final class Twig_TokenParser_AutoEscape extends Twig_TokenParser
$lineno = $token->getLine();
$stream = $this->parser->getStream();
if ($stream->test(Twig_Token::BLOCK_END_TYPE)) {
if ($stream->test(/* Twig_Token::BLOCK_END_TYPE */ 3)) {
$value = 'html';
} else {
$expr = $this->parser->getExpressionParser()->parseExpression();
@@ -29,9 +29,9 @@ final class Twig_TokenParser_AutoEscape extends Twig_TokenParser
$value = $expr->getAttribute('value');
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
return new Twig_Node_AutoEscape($value, $body, $lineno, $this->getTag());
}
+4 -4
View File
@@ -26,7 +26,7 @@ final class Twig_TokenParser_Block extends Twig_TokenParser
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
$name = $stream->expect(/* Twig_Token::NAME_TYPE */ 5)->getValue();
if ($this->parser->hasBlock($name)) {
throw new Twig_Error_Syntax(sprintf("The block '%s' has already been defined line %d.", $name, $this->parser->getBlock($name)->getTemplateLine()), $stream->getCurrent()->getLine(), $stream->getSourceContext());
}
@@ -34,9 +34,9 @@ final class Twig_TokenParser_Block extends Twig_TokenParser
$this->parser->pushLocalScope();
$this->parser->pushBlockStack($name);
if ($stream->nextIf(Twig_Token::BLOCK_END_TYPE)) {
if ($stream->nextIf(/* Twig_Token::BLOCK_END_TYPE */ 3)) {
$body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
if ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) {
if ($token = $stream->nextIf(/* Twig_Token::NAME_TYPE */ 5)) {
$value = $token->getValue();
if ($value != $name) {
@@ -48,7 +48,7 @@ final class Twig_TokenParser_Block extends Twig_TokenParser
new Twig_Node_Print($this->parser->getExpressionParser()->parseExpression(), $lineno),
));
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$block->setNode('body', $body);
$this->parser->popBlockStack();
+1 -1
View File
@@ -18,7 +18,7 @@ final class Twig_TokenParser_Do extends Twig_TokenParser
{
$expr = $this->parser->getExpressionParser()->parseExpression();
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
$this->parser->getStream()->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
return new Twig_Node_Do($expr, $token->getLine(), $this->getTag());
}
+7 -7
View File
@@ -22,19 +22,19 @@ final class Twig_TokenParser_Embed extends Twig_TokenParser_Include
list($variables, $only, $ignoreMissing) = $this->parseArguments();
$parentToken = $fakeParentToken = new Twig_Token(Twig_Token::STRING_TYPE, '__parent__', $token->getLine());
$parentToken = $fakeParentToken = new Twig_Token(/* Twig_Token::STRING_TYPE */ 7, '__parent__', $token->getLine());
if ($parent instanceof Twig_Node_Expression_Constant) {
$parentToken = new Twig_Token(Twig_Token::STRING_TYPE, $parent->getAttribute('value'), $token->getLine());
$parentToken = new Twig_Token(/* Twig_Token::STRING_TYPE */ 7, $parent->getAttribute('value'), $token->getLine());
} elseif ($parent instanceof Twig_Node_Expression_Name) {
$parentToken = new Twig_Token(Twig_Token::NAME_TYPE, $parent->getAttribute('name'), $token->getLine());
$parentToken = new Twig_Token(/* Twig_Token::NAME_TYPE */ 5, $parent->getAttribute('name'), $token->getLine());
}
// inject a fake parent to make the parent() function work
$stream->injectTokens(array(
new Twig_Token(Twig_Token::BLOCK_START_TYPE, '', $token->getLine()),
new Twig_Token(Twig_Token::NAME_TYPE, 'extends', $token->getLine()),
new Twig_Token(/* Twig_Token::BLOCK_START_TYPE */ 1, '', $token->getLine()),
new Twig_Token(/* Twig_Token::NAME_TYPE */ 5, 'extends', $token->getLine()),
$parentToken,
new Twig_Token(Twig_Token::BLOCK_END_TYPE, '', $token->getLine()),
new Twig_Token(/* Twig_Token::BLOCK_END_TYPE */ 3, '', $token->getLine()),
));
$module = $this->parser->parse($stream, array($this, 'decideBlockEnd'), true);
@@ -46,7 +46,7 @@ final class Twig_TokenParser_Embed extends Twig_TokenParser_Include
$this->parser->embedTemplate($module);
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
return new Twig_Node_Embed($module->getTemplateName(), $module->getAttribute('index'), $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag());
}
+1 -1
View File
@@ -32,7 +32,7 @@ final class Twig_TokenParser_Extends extends Twig_TokenParser
}
$this->parser->setParent($this->parser->getExpressionParser()->parseExpression());
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
}
public function getTag()
+2 -2
View File
@@ -26,10 +26,10 @@ final class Twig_TokenParser_Filter extends Twig_TokenParser
$ref = new Twig_Node_Expression_BlockReference(new Twig_Node_Expression_Constant($name, $token->getLine()), null, $token->getLine(), $this->getTag());
$filter = $this->parser->getExpressionParser()->parseFilterExpressionRaw($ref, $this->getTag());
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
$this->parser->getStream()->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
$this->parser->getStream()->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$block = new Twig_Node_Block($name, $body, $token->getLine());
$this->parser->setBlock($name, $block);
+1 -1
View File
@@ -18,7 +18,7 @@ final class Twig_TokenParser_Flush extends Twig_TokenParser
{
public function parse(Twig_Token $token)
{
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
$this->parser->getStream()->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
return new Twig_Node_Flush($token->getLine(), $this->getTag());
}
+5 -5
View File
@@ -28,23 +28,23 @@ final class Twig_TokenParser_For extends Twig_TokenParser
$lineno = $token->getLine();
$stream = $this->parser->getStream();
$targets = $this->parser->getExpressionParser()->parseAssignmentExpression();
$stream->expect(Twig_Token::OPERATOR_TYPE, 'in');
$stream->expect(/* Twig_Token::OPERATOR_TYPE */ 8, 'in');
$seq = $this->parser->getExpressionParser()->parseExpression();
$ifexpr = null;
if ($stream->nextIf(Twig_Token::NAME_TYPE, 'if')) {
if ($stream->nextIf(/* Twig_Token::NAME_TYPE */ 5, 'if')) {
$ifexpr = $this->parser->getExpressionParser()->parseExpression();
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$body = $this->parser->subparse(array($this, 'decideForFork'));
if ('else' == $stream->next()->getValue()) {
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$else = $this->parser->subparse(array($this, 'decideForEnd'), true);
} else {
$else = null;
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
if (count($targets) > 1) {
$keyTarget = $targets->getNode(0);
+4 -4
View File
@@ -26,21 +26,21 @@ final class Twig_TokenParser_From extends Twig_TokenParser
$targets = array();
do {
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
$name = $stream->expect(/* Twig_Token::NAME_TYPE */ 5)->getValue();
$alias = $name;
if ($stream->nextIf('as')) {
$alias = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
$alias = $stream->expect(/* Twig_Token::NAME_TYPE */ 5)->getValue();
}
$targets[$name] = $alias;
if (!$stream->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
if (!$stream->nextIf(/* Twig_Token::PUNCTUATION_TYPE */ 9, ',')) {
break;
}
} while (true);
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$node = new Twig_Node_Import($macro, new Twig_Node_Expression_AssignName($this->parser->getVarName(), $token->getLine()), $token->getLine(), $this->getTag());
+4 -4
View File
@@ -30,7 +30,7 @@ final class Twig_TokenParser_If extends Twig_TokenParser
$lineno = $token->getLine();
$expr = $this->parser->getExpressionParser()->parseExpression();
$stream = $this->parser->getStream();
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$body = $this->parser->subparse(array($this, 'decideIfFork'));
$tests = array($expr, $body);
$else = null;
@@ -39,13 +39,13 @@ final class Twig_TokenParser_If extends Twig_TokenParser
while (!$end) {
switch ($stream->next()->getValue()) {
case 'else':
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$else = $this->parser->subparse(array($this, 'decideIfEnd'));
break;
case 'elseif':
$expr = $this->parser->getExpressionParser()->parseExpression();
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$body = $this->parser->subparse(array($this, 'decideIfFork'));
$tests[] = $expr;
$tests[] = $body;
@@ -60,7 +60,7 @@ final class Twig_TokenParser_If extends Twig_TokenParser
}
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
return new Twig_Node_If(new Twig_Node($tests), $else, $lineno, $this->getTag());
}
+2 -2
View File
@@ -22,8 +22,8 @@ final class Twig_TokenParser_Import extends Twig_TokenParser
{
$macro = $this->parser->getExpressionParser()->parseExpression();
$this->parser->getStream()->expect('as');
$var = new Twig_Node_Expression_AssignName($this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue(), $token->getLine());
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
$var = new Twig_Node_Expression_AssignName($this->parser->getStream()->expect(/* Twig_Token::NAME_TYPE */ 5)->getValue(), $token->getLine());
$this->parser->getStream()->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$this->parser->addImportedSymbol('template', $var->getAttribute('name'));
+5 -5
View File
@@ -35,23 +35,23 @@ class Twig_TokenParser_Include extends Twig_TokenParser
$stream = $this->parser->getStream();
$ignoreMissing = false;
if ($stream->nextIf(Twig_Token::NAME_TYPE, 'ignore')) {
$stream->expect(Twig_Token::NAME_TYPE, 'missing');
if ($stream->nextIf(/* Twig_Token::NAME_TYPE */ 5, 'ignore')) {
$stream->expect(/* Twig_Token::NAME_TYPE */ 5, 'missing');
$ignoreMissing = true;
}
$variables = null;
if ($stream->nextIf(Twig_Token::NAME_TYPE, 'with')) {
if ($stream->nextIf(/* Twig_Token::NAME_TYPE */ 5, 'with')) {
$variables = $this->parser->getExpressionParser()->parseExpression();
}
$only = false;
if ($stream->nextIf(Twig_Token::NAME_TYPE, 'only')) {
if ($stream->nextIf(/* Twig_Token::NAME_TYPE */ 5, 'only')) {
$only = true;
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
return array($variables, $only, $ignoreMissing);
}
+4 -4
View File
@@ -24,14 +24,14 @@ final class Twig_TokenParser_Macro extends Twig_TokenParser
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
$name = $stream->expect(/* Twig_Token::NAME_TYPE */ 5)->getValue();
$arguments = $this->parser->getExpressionParser()->parseArguments(true, true);
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$this->parser->pushLocalScope();
$body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
if ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) {
if ($token = $stream->nextIf(/* Twig_Token::NAME_TYPE */ 5)) {
$value = $token->getValue();
if ($value != $name) {
@@ -39,7 +39,7 @@ final class Twig_TokenParser_Macro extends Twig_TokenParser
}
}
$this->parser->popLocalScope();
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$this->parser->setMacro($name, new Twig_Node_Macro($name, new Twig_Node_Body(array($body)), $arguments, $lineno, $this->getTag()));
}
+2 -2
View File
@@ -25,9 +25,9 @@ final class Twig_TokenParser_Sandbox extends Twig_TokenParser
public function parse(Twig_Token $token)
{
$stream = $this->parser->getStream();
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
// in a sandbox tag, only include tags are allowed
if (!$body instanceof Twig_Node_Include) {
+4 -4
View File
@@ -35,10 +35,10 @@ final class Twig_TokenParser_Set extends Twig_TokenParser
$names = $this->parser->getExpressionParser()->parseAssignmentExpression();
$capture = false;
if ($stream->nextIf(Twig_Token::OPERATOR_TYPE, '=')) {
if ($stream->nextIf(/* Twig_Token::OPERATOR_TYPE */ 8, '=')) {
$values = $this->parser->getExpressionParser()->parseMultitargetExpression();
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
if (count($names) !== count($values)) {
throw new Twig_Error_Syntax('When using set, you must have the same number of variables and assignments.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
@@ -50,10 +50,10 @@ final class Twig_TokenParser_Set extends Twig_TokenParser
throw new Twig_Error_Syntax('When using set with a block, you cannot have a multi-target.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$values = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
}
return new Twig_Node_Set($capture, $names, $values, $lineno, $this->getTag());
+2 -2
View File
@@ -28,9 +28,9 @@ final class Twig_TokenParser_Spaceless extends Twig_TokenParser
{
$lineno = $token->getLine();
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
$this->parser->getStream()->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$body = $this->parser->subparse(array($this, 'decideSpacelessEnd'), true);
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
$this->parser->getStream()->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
return new Twig_Node_Spaceless($body, $lineno, $this->getTag());
}
+4 -4
View File
@@ -37,22 +37,22 @@ final class Twig_TokenParser_Use extends Twig_TokenParser
$targets = array();
if ($stream->nextIf('with')) {
do {
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
$name = $stream->expect(/* Twig_Token::NAME_TYPE */ 5)->getValue();
$alias = $name;
if ($stream->nextIf('as')) {
$alias = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
$alias = $stream->expect(/* Twig_Token::NAME_TYPE */ 5)->getValue();
}
$targets[$name] = new Twig_Node_Expression_Constant($alias, -1);
if (!$stream->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
if (!$stream->nextIf(/* Twig_Token::PUNCTUATION_TYPE */ 9, ',')) {
break;
}
} while (true);
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$this->parser->addTrait(new Twig_Node(array('template' => $template, 'targets' => new Twig_Node($targets))));
+4 -4
View File
@@ -22,16 +22,16 @@ final class Twig_TokenParser_With extends Twig_TokenParser
$variables = null;
$only = false;
if (!$stream->test(Twig_Token::BLOCK_END_TYPE)) {
if (!$stream->test(/* Twig_Token::BLOCK_END_TYPE */ 3)) {
$variables = $this->parser->getExpressionParser()->parseExpression();
$only = $stream->nextIf(Twig_Token::NAME_TYPE, 'only');
$only = $stream->nextIf(/* Twig_Token::NAME_TYPE */ 5, 'only');
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
$body = $this->parser->subparse(array($this, 'decideWithEnd'), true);
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
return new Twig_Node_With($body, $variables, $only, $token->getLine(), $this->getTag());
}
+1 -1
View File
@@ -123,7 +123,7 @@ final class Twig_TokenStream
*/
public function isEOF()
{
return Twig_Token::EOF_TYPE === $this->tokens[$this->current]->getType();
return /* Twig_Token::EOF_TYPE */ -1 === $this->tokens[$this->current]->getType();
}
/**