From 0ed7898089c7beb47db6fa225d34f663a51715bc Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 9 Nov 2013 19:15:01 +0100 Subject: [PATCH] added TokenStream::nextIf() --- lib/Twig/ExpressionParser.php | 30 ++++++++++-------------------- lib/Twig/TokenParser/Block.php | 8 +++----- lib/Twig/TokenParser/For.php | 3 +-- lib/Twig/TokenParser/From.php | 8 ++------ lib/Twig/TokenParser/Include.php | 11 +++-------- lib/Twig/TokenParser/Macro.php | 4 ++-- lib/Twig/TokenParser/Set.php | 3 +-- lib/Twig/TokenParser/Use.php | 12 +++--------- lib/Twig/TokenStream.php | 12 ++++++++++++ 9 files changed, 37 insertions(+), 54 deletions(-) diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index 96f9224..bd8ee45 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -86,18 +86,15 @@ class Twig_ExpressionParser 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(); } @@ -190,12 +187,10 @@ class Twig_ExpressionParser // 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; @@ -261,8 +256,7 @@ 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 ($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(); @@ -395,9 +389,8 @@ class Twig_ExpressionParser $arg = $this->parseExpression(); } - if ($stream->test(Twig_Token::PUNCTUATION_TYPE, ':')) { + if ($stream->nextIf(Twig_Token::PUNCTUATION_TYPE, ':')) { $slice = true; - $stream->next(); } if ($slice) { @@ -480,8 +473,7 @@ class Twig_ExpressionParser } $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()); } @@ -527,10 +519,9 @@ class Twig_ExpressionParser } $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); @@ -541,10 +532,9 @@ class Twig_ExpressionParser $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); diff --git a/lib/Twig/TokenParser/Block.php b/lib/Twig/TokenParser/Block.php index a2e017f..81e6b1c 100644 --- a/lib/Twig/TokenParser/Block.php +++ b/lib/Twig/TokenParser/Block.php @@ -41,12 +41,10 @@ class Twig_TokenParser_Block extends Twig_TokenParser $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()); diff --git a/lib/Twig/TokenParser/For.php b/lib/Twig/TokenParser/For.php index 98a6d07..5c07d63 100644 --- a/lib/Twig/TokenParser/For.php +++ b/lib/Twig/TokenParser/For.php @@ -39,8 +39,7 @@ class Twig_TokenParser_For extends Twig_TokenParser $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(); } diff --git a/lib/Twig/TokenParser/From.php b/lib/Twig/TokenParser/From.php index a54054d..dd73f99 100644 --- a/lib/Twig/TokenParser/From.php +++ b/lib/Twig/TokenParser/From.php @@ -36,19 +36,15 @@ class Twig_TokenParser_From extends Twig_TokenParser $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); diff --git a/lib/Twig/TokenParser/Include.php b/lib/Twig/TokenParser/Include.php index 4a31786..9c3099a 100644 --- a/lib/Twig/TokenParser/Include.php +++ b/lib/Twig/TokenParser/Include.php @@ -42,24 +42,19 @@ class Twig_TokenParser_Include extends Twig_TokenParser $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; } diff --git a/lib/Twig/TokenParser/Macro.php b/lib/Twig/TokenParser/Macro.php index 82b4fa6..87a299d 100644 --- a/lib/Twig/TokenParser/Macro.php +++ b/lib/Twig/TokenParser/Macro.php @@ -38,8 +38,8 @@ class Twig_TokenParser_Macro extends Twig_TokenParser $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()); diff --git a/lib/Twig/TokenParser/Set.php b/lib/Twig/TokenParser/Set.php index 70e0b41..84f7e94 100644 --- a/lib/Twig/TokenParser/Set.php +++ b/lib/Twig/TokenParser/Set.php @@ -42,8 +42,7 @@ class Twig_TokenParser_Set extends Twig_TokenParser $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); diff --git a/lib/Twig/TokenParser/Use.php b/lib/Twig/TokenParser/Use.php index bc0e09e..3ea68b1 100644 --- a/lib/Twig/TokenParser/Use.php +++ b/lib/Twig/TokenParser/Use.php @@ -42,26 +42,20 @@ class Twig_TokenParser_Use extends Twig_TokenParser } $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); } diff --git a/lib/Twig/TokenStream.php b/lib/Twig/TokenStream.php index a78189f..bf8e2d7 100644 --- a/lib/Twig/TokenStream.php +++ b/lib/Twig/TokenStream.php @@ -64,6 +64,18 @@ class Twig_TokenStream } /** + * 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 -- 1.7.2.5