Index: tools/gn/parser.cc |
diff --git a/tools/gn/parser.cc b/tools/gn/parser.cc |
index 1fa432e3a8061704d7cac1ea900948327140f582..bbf6d198e2a2752362c266a19973e3a89c1438e1 100644 |
--- a/tools/gn/parser.cc |
+++ b/tools/gn/parser.cc |
@@ -174,7 +174,7 @@ ParserHelper Parser::expressions_[] = { |
Parser::Parser(const std::vector<Token>& tokens, Err* err) |
: err_(err), cur_(0) { |
for (const auto& token : tokens) { |
- switch(token.type()) { |
+ switch (token.type()) { |
case Token::LINE_COMMENT: |
line_comment_tokens_.push_back(token); |
break; |
@@ -204,7 +204,34 @@ scoped_ptr<ParseNode> Parser::Parse(const std::vector<Token>& tokens, |
scoped_ptr<ParseNode> Parser::ParseExpression(const std::vector<Token>& tokens, |
Err* err) { |
Parser p(tokens, err); |
- return p.ParseExpression().Pass(); |
+ scoped_ptr<ParseNode> expr = p.ParseExpression(); |
+ if (!p.at_end() && !err->has_error()) { |
+ *err = Err(p.cur_token(), "Trailing garbage"); |
+ return nullptr; |
+ } |
+ return expr.Pass(); |
+} |
+ |
+// static |
+scoped_ptr<ParseNode> Parser::ParseValue(const std::vector<Token>& tokens, |
+ Err* err) { |
+ for (const Token& token : tokens) { |
+ switch (token.type()) { |
+ case Token::INTEGER: |
+ case Token::STRING: |
+ case Token::TRUE_TOKEN: |
+ case Token::FALSE_TOKEN: |
+ case Token::LEFT_BRACKET: |
+ case Token::RIGHT_BRACKET: |
+ case Token::COMMA: |
+ continue; |
+ default: |
+ *err = Err(token, "Invalid token in literal value"); |
+ return nullptr; |
+ } |
+ } |
+ |
+ return ParseExpression(tokens, err); |
} |
bool Parser::IsAssignment(const ParseNode* node) const { |