From afabfdfe9de33dc40efcdd6cf535b1a38e83f97a Mon Sep 17 00:00:00 2001 From: Jan Tvrdik Date: Sat, 18 Nov 2017 16:15:23 +0100 Subject: [PATCH] TypeParser: ignore unclosed array notation --- src/Parser/TypeParser.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Parser/TypeParser.php b/src/Parser/TypeParser.php index 29aa0f5..4ad409a 100644 --- a/src/Parser/TypeParser.php +++ b/src/Parser/TypeParser.php @@ -35,7 +35,7 @@ class TypeParser $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PARENTHESES); if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) { - $type = $this->parseArray($tokens, $type); + $type = $this->tryParseArray($tokens, $type); } } elseif ($tokens->tryConsumeTokenType(Lexer::TOKEN_THIS_VARIABLE)) { @@ -49,7 +49,7 @@ class TypeParser $type = $this->parseGeneric($tokens, $type); } elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) { - $type = $this->parseArray($tokens, $type); + $type = $this->tryParseArray($tokens, $type); } } @@ -110,11 +110,19 @@ class TypeParser } - private function parseArray(TokenIterator $tokens, Ast\Type\TypeNode $type): Ast\Type\TypeNode + private function tryParseArray(TokenIterator $tokens, Ast\Type\TypeNode $type): Ast\Type\TypeNode { - while ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) { - $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_SQUARE_BRACKET); - $type = new Ast\Type\ArrayTypeNode($type); + try { + while ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) { + $tokens->pushSavePoint(); + $tokens->consumeTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET); + $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_SQUARE_BRACKET); + $tokens->dropSavePoint(); + $type = new Ast\Type\ArrayTypeNode($type); + } + + } catch (ParserException $e) { + $tokens->rollback(); } return $type;