Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(297)

Unified Diff: tools/gn/parser.cc

Issue 1048913003: tools/gn: don't allow trailing junk or non-literals in "value" conversion (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: clang-format Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/gn/parser.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 {
« no previous file with comments | « tools/gn/parser.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698