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 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 if (has_error()) | 367 if (has_error()) |
368 return scoped_ptr<ParseNode>(); | 368 return scoped_ptr<ParseNode>(); |
369 Consume(Token::RIGHT_PAREN, "Expected ')'"); | 369 Consume(Token::RIGHT_PAREN, "Expected ')'"); |
370 return expr.Pass(); | 370 return expr.Pass(); |
371 } | 371 } |
372 | 372 |
373 scoped_ptr<ParseNode> Parser::Not(Token token) { | 373 scoped_ptr<ParseNode> Parser::Not(Token token) { |
374 scoped_ptr<ParseNode> expr = ParseExpression(PRECEDENCE_PREFIX + 1); | 374 scoped_ptr<ParseNode> expr = ParseExpression(PRECEDENCE_PREFIX + 1); |
375 if (has_error()) | 375 if (has_error()) |
376 return scoped_ptr<ParseNode>(); | 376 return scoped_ptr<ParseNode>(); |
| 377 if (!expr) { |
| 378 if (!has_error()) |
| 379 *err_ = Err(token, "Expected right-hand side for '!'."); |
| 380 return scoped_ptr<ParseNode>(); |
| 381 } |
377 scoped_ptr<UnaryOpNode> unary_op(new UnaryOpNode); | 382 scoped_ptr<UnaryOpNode> unary_op(new UnaryOpNode); |
378 unary_op->set_op(token); | 383 unary_op->set_op(token); |
379 unary_op->set_operand(expr.Pass()); | 384 unary_op->set_operand(expr.Pass()); |
380 return unary_op.Pass(); | 385 return unary_op.Pass(); |
381 } | 386 } |
382 | 387 |
383 scoped_ptr<ParseNode> Parser::List(Token node) { | 388 scoped_ptr<ParseNode> Parser::List(Token node) { |
384 scoped_ptr<ParseNode> list(ParseList(node, Token::RIGHT_BRACKET, true)); | 389 scoped_ptr<ParseNode> list(ParseList(node, Token::RIGHT_BRACKET, true)); |
385 if (!has_error() && !at_end()) | 390 if (!has_error() && !at_end()) |
386 Consume(Token::RIGHT_BRACKET, "Expected ']'"); | 391 Consume(Token::RIGHT_BRACKET, "Expected ']'"); |
387 return list.Pass(); | 392 return list.Pass(); |
388 } | 393 } |
389 | 394 |
390 scoped_ptr<ParseNode> Parser::BinaryOperator(scoped_ptr<ParseNode> left, | 395 scoped_ptr<ParseNode> Parser::BinaryOperator(scoped_ptr<ParseNode> left, |
391 Token token) { | 396 Token token) { |
392 scoped_ptr<ParseNode> right = | 397 scoped_ptr<ParseNode> right = |
393 ParseExpression(expressions_[token.type()].precedence + 1); | 398 ParseExpression(expressions_[token.type()].precedence + 1); |
394 if (!right) { | 399 if (!right) { |
395 if (!has_error()) { | 400 if (!has_error()) { |
396 *err_ = Err(token, "Expected right hand side for '" + | 401 *err_ = Err(token, "Expected right-hand side for '" + |
397 token.value().as_string() + "'"); | 402 token.value().as_string() + "'"); |
398 } | 403 } |
399 return scoped_ptr<ParseNode>(); | 404 return scoped_ptr<ParseNode>(); |
400 } | 405 } |
401 scoped_ptr<BinaryOpNode> binary_op(new BinaryOpNode); | 406 scoped_ptr<BinaryOpNode> binary_op(new BinaryOpNode); |
402 binary_op->set_op(token); | 407 binary_op->set_op(token); |
403 binary_op->set_left(left.Pass()); | 408 binary_op->set_left(left.Pass()); |
404 binary_op->set_right(right.Pass()); | 409 binary_op->set_right(right.Pass()); |
405 return binary_op.Pass(); | 410 return binary_op.Pass(); |
406 } | 411 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 return func_call.Pass(); | 449 return func_call.Pass(); |
445 } | 450 } |
446 | 451 |
447 scoped_ptr<ParseNode> Parser::Assignment(scoped_ptr<ParseNode> left, | 452 scoped_ptr<ParseNode> Parser::Assignment(scoped_ptr<ParseNode> left, |
448 Token token) { | 453 Token token) { |
449 if (left->AsIdentifier() == nullptr) { | 454 if (left->AsIdentifier() == nullptr) { |
450 *err_ = Err(left.get(), "Left-hand side of assignment must be identifier."); | 455 *err_ = Err(left.get(), "Left-hand side of assignment must be identifier."); |
451 return scoped_ptr<ParseNode>(); | 456 return scoped_ptr<ParseNode>(); |
452 } | 457 } |
453 scoped_ptr<ParseNode> value = ParseExpression(PRECEDENCE_ASSIGNMENT); | 458 scoped_ptr<ParseNode> value = ParseExpression(PRECEDENCE_ASSIGNMENT); |
| 459 if (!value) { |
| 460 if (!has_error()) |
| 461 *err_ = Err(token, "Expected right-hand side for assignment."); |
| 462 return scoped_ptr<ParseNode>(); |
| 463 } |
454 scoped_ptr<BinaryOpNode> assign(new BinaryOpNode); | 464 scoped_ptr<BinaryOpNode> assign(new BinaryOpNode); |
455 assign->set_op(token); | 465 assign->set_op(token); |
456 assign->set_left(left.Pass()); | 466 assign->set_left(left.Pass()); |
457 assign->set_right(value.Pass()); | 467 assign->set_right(value.Pass()); |
458 return assign.Pass(); | 468 return assign.Pass(); |
459 } | 469 } |
460 | 470 |
461 scoped_ptr<ParseNode> Parser::Subscript(scoped_ptr<ParseNode> left, | 471 scoped_ptr<ParseNode> Parser::Subscript(scoped_ptr<ParseNode> left, |
462 Token token) { | 472 Token token) { |
463 // TODO: Maybe support more complex expressions like a[0][0]. This would | 473 // TODO: Maybe support more complex expressions like a[0][0]. This would |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
741 break; | 751 break; |
742 } | 752 } |
743 } | 753 } |
744 | 754 |
745 // Suffix comments were assigned in reverse, so if there were multiple on | 755 // Suffix comments were assigned in reverse, so if there were multiple on |
746 // the same node, they need to be reversed. | 756 // the same node, they need to be reversed. |
747 if ((*i)->comments() && !(*i)->comments()->suffix().empty()) | 757 if ((*i)->comments() && !(*i)->comments()->suffix().empty()) |
748 const_cast<ParseNode*>(*i)->comments_mutable()->ReverseSuffix(); | 758 const_cast<ParseNode*>(*i)->comments_mutable()->ReverseSuffix(); |
749 } | 759 } |
750 } | 760 } |
OLD | NEW |