protected function parseConditionalExpression($expr)
{
- while ($this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '?')) {
- $this->parser->getStream()->next();
- if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, ':')) {
+ while ($this->parser->getStream()->nextIf(Twig_Token::PUNCTUATION_TYPE, '?')) {
+ if (!$this->parser->getStream()->nextIf(Twig_Token::PUNCTUATION_TYPE, ':')) {
$expr2 = $this->parseExpression();
- if ($this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, ':')) {
- $this->parser->getStream()->next();
+ if ($this->parser->getStream()->nextIf(Twig_Token::PUNCTUATION_TYPE, ':')) {
$expr3 = $this->parseExpression();
} else {
$expr3 = new Twig_Node_Expression_Constant('', $this->parser->getCurrentToken()->getLine());
}
} else {
- $this->parser->getStream()->next();
$expr2 = $expr;
$expr3 = $this->parseExpression();
}
// a string cannot be followed by another string in a single expression
$nextCanBeString = true;
while (true) {
- if ($stream->test(Twig_Token::STRING_TYPE) && $nextCanBeString) {
- $token = $stream->next();
+ if ($nextCanBeString && $token = $stream->nextIf(Twig_Token::STRING_TYPE)) {
$nodes[] = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
$nextCanBeString = false;
- } elseif ($stream->test(Twig_Token::INTERPOLATION_START_TYPE)) {
- $stream->next();
+ } elseif ($stream->nextIf(Twig_Token::INTERPOLATION_START_TYPE)) {
$nodes[] = $this->parseExpression();
$stream->expect(Twig_Token::INTERPOLATION_END_TYPE);
$nextCanBeString = true;
// * a string -- 'a'
// * a name, which is equivalent to a string -- a
// * an expression, which must be enclosed in parentheses -- (1 + 2)
- if ($stream->test(Twig_Token::STRING_TYPE) || $stream->test(Twig_Token::NAME_TYPE) || $stream->test(Twig_Token::NUMBER_TYPE)) {
- $token = $stream->next();
+ if (($token = $stream->nextIf(Twig_Token::STRING_TYPE)) || ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) || $token = $stream->nextIf(Twig_Token::NUMBER_TYPE)) {
$key = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
} elseif ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
$key = $this->parseExpression();
$arg = $this->parseExpression();
}
- if ($stream->test(Twig_Token::PUNCTUATION_TYPE, ':')) {
+ if ($stream->nextIf(Twig_Token::PUNCTUATION_TYPE, ':')) {
$slice = true;
- $stream->next();
}
if ($slice) {
}
$name = null;
- if ($namedArguments && $stream->test(Twig_Token::OPERATOR_TYPE, '=')) {
- $token = $stream->next();
+ if ($namedArguments && $token = $stream->nextIf(Twig_Token::OPERATOR_TYPE, '=')) {
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(), $this->parser->getFilename());
}
}
$targets[] = new Twig_Node_Expression_AssignName($token->getValue(), $token->getLine());
- if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, ',')) {
+ if (!$this->parser->getStream()->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
break;
}
- $this->parser->getStream()->next();
}
return new Twig_Node($targets);
$targets = array();
while (true) {
$targets[] = $this->parseExpression();
- if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, ',')) {
+ if (!$this->parser->getStream()->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
break;
}
- $this->parser->getStream()->next();
}
return new Twig_Node($targets);
$this->parser->pushLocalScope();
$this->parser->pushBlockStack($name);
- if ($stream->test(Twig_Token::BLOCK_END_TYPE)) {
- $stream->next();
-
+ if ($stream->nextIf(Twig_Token::BLOCK_END_TYPE)) {
$body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
- if ($stream->test(Twig_Token::NAME_TYPE)) {
- $value = $stream->next()->getValue();
+ if ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) {
+ $value = $token->getValue();
if ($value != $name) {
throw new Twig_Error_Syntax(sprintf("Expected endblock for block '$name' (but %s given)", $value), $stream->getCurrent()->getLine(), $stream->getFilename());
$seq = $this->parser->getExpressionParser()->parseExpression();
$ifexpr = null;
- if ($stream->test(Twig_Token::NAME_TYPE, 'if')) {
- $stream->next();
+ if ($stream->nextIf(Twig_Token::NAME_TYPE, 'if')) {
$ifexpr = $this->parser->getExpressionParser()->parseExpression();
}
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
$alias = $name;
- if ($stream->test('as')) {
- $stream->next();
-
+ if ($stream->nextIf('as')) {
$alias = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
}
$targets[$name] = $alias;
- if (!$stream->test(Twig_Token::PUNCTUATION_TYPE, ',')) {
+ if (!$stream->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
break;
}
-
- $stream->next();
} while (true);
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$stream = $this->parser->getStream();
$ignoreMissing = false;
- if ($stream->test(Twig_Token::NAME_TYPE, 'ignore')) {
- $stream->next();
+ if ($stream->nextIf(Twig_Token::NAME_TYPE, 'ignore')) {
$stream->expect(Twig_Token::NAME_TYPE, 'missing');
$ignoreMissing = true;
}
$variables = null;
- if ($stream->test(Twig_Token::NAME_TYPE, 'with')) {
- $stream->next();
-
+ if ($stream->nextIf(Twig_Token::NAME_TYPE, 'with')) {
$variables = $this->parser->getExpressionParser()->parseExpression();
}
$only = false;
- if ($stream->test(Twig_Token::NAME_TYPE, 'only')) {
- $stream->next();
-
+ if ($stream->nextIf(Twig_Token::NAME_TYPE, 'only')) {
$only = true;
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$this->parser->pushLocalScope();
$body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
- if ($stream->test(Twig_Token::NAME_TYPE)) {
- $value = $stream->next()->getValue();
+ if ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) {
+ $value = $token->getValue();
if ($value != $name) {
throw new Twig_Error_Syntax(sprintf("Expected endmacro for macro '$name' (but %s given)", $value), $stream->getCurrent()->getLine(), $stream->getFilename());
$names = $this->parser->getExpressionParser()->parseAssignmentExpression();
$capture = false;
- if ($stream->test(Twig_Token::OPERATOR_TYPE, '=')) {
- $stream->next();
+ if ($stream->nextIf(Twig_Token::OPERATOR_TYPE, '=')) {
$values = $this->parser->getExpressionParser()->parseMultitargetExpression();
$stream->expect(Twig_Token::BLOCK_END_TYPE);
}
$targets = array();
- if ($stream->test('with')) {
- $stream->next();
-
+ if ($stream->nextIf('with')) {
do {
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
$alias = $name;
- if ($stream->test('as')) {
- $stream->next();
-
+ if ($stream->nextIf('as')) {
$alias = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
}
$targets[$name] = new Twig_Node_Expression_Constant($alias, -1);
- if (!$stream->test(Twig_Token::PUNCTUATION_TYPE, ',')) {
+ if (!$stream->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
break;
}
-
- $stream->next();
} while (true);
}
}
/**
+ * Tests a token, sets the pointer to the next one and returns it or throws a syntax error.
+ *
+ * @return Twig_Token
+ */
+ public function nextIf($primary, $secondary = null)
+ {
+ if ($this->tokens[$this->current]->test($primary, $secondary)) {
+ return $this->next();
+ }
+ }
+
+ /**
* Tests a token and returns it or throws a syntax error.
*
* @return Twig_Token