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/parse_tree.h" | 5 #include "tools/gn/parse_tree.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 } | 33 } |
34 | 34 |
35 ParseNode::ParseNode() { | 35 ParseNode::ParseNode() { |
36 } | 36 } |
37 | 37 |
38 ParseNode::~ParseNode() { | 38 ParseNode::~ParseNode() { |
39 } | 39 } |
40 | 40 |
41 const AccessorNode* ParseNode::AsAccessor() const { return NULL; } | 41 const AccessorNode* ParseNode::AsAccessor() const { return NULL; } |
42 const BinaryOpNode* ParseNode::AsBinaryOp() const { return NULL; } | 42 const BinaryOpNode* ParseNode::AsBinaryOp() const { return NULL; } |
| 43 const BlockCommentNode* ParseNode::AsBlockComment() const { return NULL; } |
43 const BlockNode* ParseNode::AsBlock() const { return NULL; } | 44 const BlockNode* ParseNode::AsBlock() const { return NULL; } |
44 const ConditionNode* ParseNode::AsConditionNode() const { return NULL; } | 45 const ConditionNode* ParseNode::AsConditionNode() const { return NULL; } |
| 46 const EndNode* ParseNode::AsEnd() const { return NULL; } |
45 const FunctionCallNode* ParseNode::AsFunctionCall() const { return NULL; } | 47 const FunctionCallNode* ParseNode::AsFunctionCall() const { return NULL; } |
46 const IdentifierNode* ParseNode::AsIdentifier() const { return NULL; } | 48 const IdentifierNode* ParseNode::AsIdentifier() const { return NULL; } |
47 const ListNode* ParseNode::AsList() const { return NULL; } | 49 const ListNode* ParseNode::AsList() const { return NULL; } |
48 const LiteralNode* ParseNode::AsLiteral() const { return NULL; } | 50 const LiteralNode* ParseNode::AsLiteral() const { return NULL; } |
49 const UnaryOpNode* ParseNode::AsUnaryOp() const { return NULL; } | 51 const UnaryOpNode* ParseNode::AsUnaryOp() const { return NULL; } |
50 const BlockCommentNode* ParseNode::AsBlockComment() const { return NULL; } | |
51 | 52 |
52 Comments* ParseNode::comments_mutable() { | 53 Comments* ParseNode::comments_mutable() { |
53 if (!comments_) | 54 if (!comments_) |
54 comments_.reset(new Comments); | 55 comments_.reset(new Comments); |
55 return comments_.get(); | 56 return comments_.get(); |
56 } | 57 } |
57 | 58 |
58 void ParseNode::PrintComments(std::ostream& out, int indent) const { | 59 void ParseNode::PrintComments(std::ostream& out, int indent) const { |
59 if (comments_) { | 60 if (comments_) { |
60 std::string ind = IndentFor(indent + 1); | 61 std::string ind = IndentFor(indent + 1); |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 | 257 |
257 // Check for unused vars in the scope. | 258 // Check for unused vars in the scope. |
258 our_scope.CheckForUnusedVars(err); | 259 our_scope.CheckForUnusedVars(err); |
259 return ret; | 260 return ret; |
260 } | 261 } |
261 return ExecuteBlockInScope(containing_scope, err); | 262 return ExecuteBlockInScope(containing_scope, err); |
262 } | 263 } |
263 | 264 |
264 LocationRange BlockNode::GetRange() const { | 265 LocationRange BlockNode::GetRange() const { |
265 if (begin_token_.type() != Token::INVALID && | 266 if (begin_token_.type() != Token::INVALID && |
266 end_token_.type() != Token::INVALID) { | 267 end_->value().type() != Token::INVALID) { |
267 return begin_token_.range().Union(end_token_.range()); | 268 return begin_token_.range().Union(end_->value().range()); |
268 } else if (!statements_.empty()) { | 269 } else if (!statements_.empty()) { |
269 return statements_[0]->GetRange().Union( | 270 return statements_[0]->GetRange().Union( |
270 statements_[statements_.size() - 1]->GetRange()); | 271 statements_[statements_.size() - 1]->GetRange()); |
271 } | 272 } |
272 return LocationRange(); | 273 return LocationRange(); |
273 } | 274 } |
274 | 275 |
275 Err BlockNode::MakeErrorDescribing(const std::string& msg, | 276 Err BlockNode::MakeErrorDescribing(const std::string& msg, |
276 const std::string& help) const { | 277 const std::string& help) const { |
277 return Err(GetRange(), msg, help); | 278 return Err(GetRange(), msg, help); |
278 } | 279 } |
279 | 280 |
280 void BlockNode::Print(std::ostream& out, int indent) const { | 281 void BlockNode::Print(std::ostream& out, int indent) const { |
281 out << IndentFor(indent) << "BLOCK\n"; | 282 out << IndentFor(indent) << "BLOCK\n"; |
282 PrintComments(out, indent); | 283 PrintComments(out, indent); |
283 for (size_t i = 0; i < statements_.size(); i++) | 284 for (size_t i = 0; i < statements_.size(); i++) |
284 statements_[i]->Print(out, indent + 1); | 285 statements_[i]->Print(out, indent + 1); |
| 286 if (end_ && end_->comments()) |
| 287 end_->Print(out, indent + 1); |
285 } | 288 } |
286 | 289 |
287 Value BlockNode::ExecuteBlockInScope(Scope* our_scope, Err* err) const { | 290 Value BlockNode::ExecuteBlockInScope(Scope* our_scope, Err* err) const { |
288 for (size_t i = 0; i < statements_.size() && !err->has_error(); i++) { | 291 for (size_t i = 0; i < statements_.size() && !err->has_error(); i++) { |
289 // Check for trying to execute things with no side effects in a block. | 292 // Check for trying to execute things with no side effects in a block. |
290 const ParseNode* cur = statements_[i]; | 293 const ParseNode* cur = statements_[i]; |
291 if (cur->AsList() || cur->AsLiteral() || cur->AsUnaryOp() || | 294 if (cur->AsList() || cur->AsLiteral() || cur->AsUnaryOp() || |
292 cur->AsIdentifier()) { | 295 cur->AsIdentifier()) { |
293 *err = cur->MakeErrorDescribing( | 296 *err = cur->MakeErrorDescribing( |
294 "This statement has no effect.", | 297 "This statement has no effect.", |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 *err = cur->MakeErrorDescribing( | 474 *err = cur->MakeErrorDescribing( |
472 "This does not evaluate to a value.", | 475 "This does not evaluate to a value.", |
473 "I can't do something with nothing."); | 476 "I can't do something with nothing."); |
474 return Value(); | 477 return Value(); |
475 } | 478 } |
476 } | 479 } |
477 return result_value; | 480 return result_value; |
478 } | 481 } |
479 | 482 |
480 LocationRange ListNode::GetRange() const { | 483 LocationRange ListNode::GetRange() const { |
481 return LocationRange(begin_token_.location(), end_token_.location()); | 484 return LocationRange(begin_token_.location(), |
| 485 end_->value().location()); |
482 } | 486 } |
483 | 487 |
484 Err ListNode::MakeErrorDescribing(const std::string& msg, | 488 Err ListNode::MakeErrorDescribing(const std::string& msg, |
485 const std::string& help) const { | 489 const std::string& help) const { |
486 return Err(begin_token_, msg, help); | 490 return Err(begin_token_, msg, help); |
487 } | 491 } |
488 | 492 |
489 void ListNode::Print(std::ostream& out, int indent) const { | 493 void ListNode::Print(std::ostream& out, int indent) const { |
490 out << IndentFor(indent) << "LIST\n"; | 494 out << IndentFor(indent) << "LIST\n"; |
491 PrintComments(out, indent); | 495 PrintComments(out, indent); |
492 for (size_t i = 0; i < contents_.size(); i++) | 496 for (size_t i = 0; i < contents_.size(); i++) |
493 contents_[i]->Print(out, indent + 1); | 497 contents_[i]->Print(out, indent + 1); |
| 498 if (end_ && end_->comments()) |
| 499 end_->Print(out, indent + 1); |
494 } | 500 } |
495 | 501 |
496 // LiteralNode ----------------------------------------------------------------- | 502 // LiteralNode ----------------------------------------------------------------- |
497 | 503 |
498 LiteralNode::LiteralNode() { | 504 LiteralNode::LiteralNode() { |
499 } | 505 } |
500 | 506 |
501 LiteralNode::LiteralNode(const Token& token) : value_(token) { | 507 LiteralNode::LiteralNode(const Token& token) : value_(token) { |
502 } | 508 } |
503 | 509 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
603 | 609 |
604 Err BlockCommentNode::MakeErrorDescribing(const std::string& msg, | 610 Err BlockCommentNode::MakeErrorDescribing(const std::string& msg, |
605 const std::string& help) const { | 611 const std::string& help) const { |
606 return Err(comment_, msg, help); | 612 return Err(comment_, msg, help); |
607 } | 613 } |
608 | 614 |
609 void BlockCommentNode::Print(std::ostream& out, int indent) const { | 615 void BlockCommentNode::Print(std::ostream& out, int indent) const { |
610 out << IndentFor(indent) << "BLOCK_COMMENT(" << comment_.value() << ")\n"; | 616 out << IndentFor(indent) << "BLOCK_COMMENT(" << comment_.value() << ")\n"; |
611 PrintComments(out, indent); | 617 PrintComments(out, indent); |
612 } | 618 } |
| 619 |
| 620 |
| 621 // EndNode --------------------------------------------------------------------- |
| 622 |
| 623 EndNode::EndNode(const Token& token) : value_(token) { |
| 624 } |
| 625 |
| 626 EndNode::~EndNode() { |
| 627 } |
| 628 |
| 629 const EndNode* EndNode::AsEnd() const { |
| 630 return this; |
| 631 } |
| 632 |
| 633 Value EndNode::Execute(Scope* scope, Err* err) const { |
| 634 return Value(); |
| 635 } |
| 636 |
| 637 LocationRange EndNode::GetRange() const { |
| 638 return value_.range(); |
| 639 } |
| 640 |
| 641 Err EndNode::MakeErrorDescribing(const std::string& msg, |
| 642 const std::string& help) const { |
| 643 return Err(value_, msg, help); |
| 644 } |
| 645 |
| 646 void EndNode::Print(std::ostream& out, int indent) const { |
| 647 out << IndentFor(indent) << "END(" << value_.value() << ")\n"; |
| 648 PrintComments(out, indent); |
| 649 } |
OLD | NEW |