OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/gn/parser.h" | 5 #include "tools/gn/parser.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "tools/gn/functions.h" | 8 #include "tools/gn/functions.h" |
9 #include "tools/gn/operators.h" | 9 #include "tools/gn/operators.h" |
10 #include "tools/gn/token.h" | 10 #include "tools/gn/token.h" |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 {nullptr, nullptr, -1}, // COMMA | 167 {nullptr, nullptr, -1}, // COMMA |
168 {nullptr, nullptr, -1}, // UNCLASSIFIED_COMMENT | 168 {nullptr, nullptr, -1}, // UNCLASSIFIED_COMMENT |
169 {nullptr, nullptr, -1}, // LINE_COMMENT | 169 {nullptr, nullptr, -1}, // LINE_COMMENT |
170 {nullptr, nullptr, -1}, // SUFFIX_COMMENT | 170 {nullptr, nullptr, -1}, // SUFFIX_COMMENT |
171 {&Parser::BlockComment, nullptr, -1}, // BLOCK_COMMENT | 171 {&Parser::BlockComment, nullptr, -1}, // BLOCK_COMMENT |
172 }; | 172 }; |
173 | 173 |
174 Parser::Parser(const std::vector<Token>& tokens, Err* err) | 174 Parser::Parser(const std::vector<Token>& tokens, Err* err) |
175 : err_(err), cur_(0) { | 175 : err_(err), cur_(0) { |
176 for (const auto& token : tokens) { | 176 for (const auto& token : tokens) { |
177 switch(token.type()) { | 177 switch (token.type()) { |
178 case Token::LINE_COMMENT: | 178 case Token::LINE_COMMENT: |
179 line_comment_tokens_.push_back(token); | 179 line_comment_tokens_.push_back(token); |
180 break; | 180 break; |
181 case Token::SUFFIX_COMMENT: | 181 case Token::SUFFIX_COMMENT: |
182 suffix_comment_tokens_.push_back(token); | 182 suffix_comment_tokens_.push_back(token); |
183 break; | 183 break; |
184 default: | 184 default: |
185 // Note that BLOCK_COMMENTs (top-level standalone comments) are passed | 185 // Note that BLOCK_COMMENTs (top-level standalone comments) are passed |
186 // through the real parser. | 186 // through the real parser. |
187 tokens_.push_back(token); | 187 tokens_.push_back(token); |
188 break; | 188 break; |
189 } | 189 } |
190 } | 190 } |
191 } | 191 } |
192 | 192 |
193 Parser::~Parser() { | 193 Parser::~Parser() { |
194 } | 194 } |
195 | 195 |
196 // static | 196 // static |
197 scoped_ptr<ParseNode> Parser::Parse(const std::vector<Token>& tokens, | 197 scoped_ptr<ParseNode> Parser::Parse(const std::vector<Token>& tokens, |
198 Err* err) { | 198 Err* err) { |
199 Parser p(tokens, err); | 199 Parser p(tokens, err); |
200 return p.ParseFile(); | 200 return p.ParseFile(); |
201 } | 201 } |
202 | 202 |
203 // static | 203 // static |
204 scoped_ptr<ParseNode> Parser::ParseExpression(const std::vector<Token>& tokens, | 204 scoped_ptr<ParseNode> Parser::ParseExpression(const std::vector<Token>& tokens, |
205 Err* err) { | 205 Err* err) { |
206 Parser p(tokens, err); | 206 Parser p(tokens, err); |
207 return p.ParseExpression().Pass(); | 207 scoped_ptr<ParseNode> expr = p.ParseExpression(); |
| 208 if (!p.at_end() && !err->has_error()) { |
| 209 *err = Err(p.cur_token(), "Trailing garbage"); |
| 210 return nullptr; |
| 211 } |
| 212 return expr.Pass(); |
| 213 } |
| 214 |
| 215 // static |
| 216 scoped_ptr<ParseNode> Parser::ParseValue(const std::vector<Token>& tokens, |
| 217 Err* err) { |
| 218 for (const Token& token : tokens) { |
| 219 switch (token.type()) { |
| 220 case Token::INTEGER: |
| 221 case Token::STRING: |
| 222 case Token::TRUE_TOKEN: |
| 223 case Token::FALSE_TOKEN: |
| 224 case Token::LEFT_BRACKET: |
| 225 case Token::RIGHT_BRACKET: |
| 226 case Token::COMMA: |
| 227 continue; |
| 228 default: |
| 229 *err = Err(token, "Invalid token in literal value"); |
| 230 return nullptr; |
| 231 } |
| 232 } |
| 233 |
| 234 return ParseExpression(tokens, err); |
208 } | 235 } |
209 | 236 |
210 bool Parser::IsAssignment(const ParseNode* node) const { | 237 bool Parser::IsAssignment(const ParseNode* node) const { |
211 return node && node->AsBinaryOp() && | 238 return node && node->AsBinaryOp() && |
212 (node->AsBinaryOp()->op().type() == Token::EQUAL || | 239 (node->AsBinaryOp()->op().type() == Token::EQUAL || |
213 node->AsBinaryOp()->op().type() == Token::PLUS_EQUALS || | 240 node->AsBinaryOp()->op().type() == Token::PLUS_EQUALS || |
214 node->AsBinaryOp()->op().type() == Token::MINUS_EQUALS); | 241 node->AsBinaryOp()->op().type() == Token::MINUS_EQUALS); |
215 } | 242 } |
216 | 243 |
217 bool Parser::IsStatementBreak(Token::Type token_type) const { | 244 bool Parser::IsStatementBreak(Token::Type token_type) const { |
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
707 break; | 734 break; |
708 } | 735 } |
709 } | 736 } |
710 | 737 |
711 // Suffix comments were assigned in reverse, so if there were multiple on | 738 // Suffix comments were assigned in reverse, so if there were multiple on |
712 // the same node, they need to be reversed. | 739 // the same node, they need to be reversed. |
713 if ((*i)->comments() && !(*i)->comments()->suffix().empty()) | 740 if ((*i)->comments() && !(*i)->comments()->suffix().empty()) |
714 const_cast<ParseNode*>(*i)->comments_mutable()->ReverseSuffix(); | 741 const_cast<ParseNode*>(*i)->comments_mutable()->ReverseSuffix(); |
715 } | 742 } |
716 } | 743 } |
OLD | NEW |