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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 | 52 |
53 Comments* ParseNode::comments_mutable() { | 53 Comments* ParseNode::comments_mutable() { |
54 if (!comments_) | 54 if (!comments_) |
55 comments_.reset(new Comments); | 55 comments_.reset(new Comments); |
56 return comments_.get(); | 56 return comments_.get(); |
57 } | 57 } |
58 | 58 |
59 void ParseNode::PrintComments(std::ostream& out, int indent) const { | 59 void ParseNode::PrintComments(std::ostream& out, int indent) const { |
60 if (comments_) { | 60 if (comments_) { |
61 std::string ind = IndentFor(indent + 1); | 61 std::string ind = IndentFor(indent + 1); |
62 for (std::vector<Token>::const_iterator i(comments_->before().begin()); | 62 for (const auto& token : comments_->before()) |
63 i != comments_->before().end(); | 63 out << ind << "+BEFORE_COMMENT(\"" << token.value() << "\")\n"; |
64 ++i) { | 64 for (const auto& token : comments_->suffix()) |
65 out << ind << "+BEFORE_COMMENT(\"" << i->value() << "\")\n"; | 65 out << ind << "+SUFFIX_COMMENT(\"" << token.value() << "\")\n"; |
66 } | 66 for (const auto& token : comments_->after()) |
67 for (std::vector<Token>::const_iterator i(comments_->suffix().begin()); | 67 out << ind << "+AFTER_COMMENT(\"" << token.value() << "\")\n"; |
68 i != comments_->suffix().end(); | |
69 ++i) { | |
70 out << ind << "+SUFFIX_COMMENT(\"" << i->value() << "\")\n"; | |
71 } | |
72 for (std::vector<Token>::const_iterator i(comments_->after().begin()); | |
73 i != comments_->after().end(); | |
74 ++i) { | |
75 out << ind << "+AFTER_COMMENT(\"" << i->value() << "\")\n"; | |
76 } | |
77 } | 68 } |
78 } | 69 } |
79 | 70 |
80 // AccessorNode --------------------------------------------------------------- | 71 // AccessorNode --------------------------------------------------------------- |
81 | 72 |
82 AccessorNode::AccessorNode() { | 73 AccessorNode::AccessorNode() { |
83 } | 74 } |
84 | 75 |
85 AccessorNode::~AccessorNode() { | 76 AccessorNode::~AccessorNode() { |
86 } | 77 } |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 } | 265 } |
275 | 266 |
276 Err BlockNode::MakeErrorDescribing(const std::string& msg, | 267 Err BlockNode::MakeErrorDescribing(const std::string& msg, |
277 const std::string& help) const { | 268 const std::string& help) const { |
278 return Err(GetRange(), msg, help); | 269 return Err(GetRange(), msg, help); |
279 } | 270 } |
280 | 271 |
281 void BlockNode::Print(std::ostream& out, int indent) const { | 272 void BlockNode::Print(std::ostream& out, int indent) const { |
282 out << IndentFor(indent) << "BLOCK\n"; | 273 out << IndentFor(indent) << "BLOCK\n"; |
283 PrintComments(out, indent); | 274 PrintComments(out, indent); |
284 for (size_t i = 0; i < statements_.size(); i++) | 275 for (const auto& statement : statements_) |
285 statements_[i]->Print(out, indent + 1); | 276 statement->Print(out, indent + 1); |
286 if (end_ && end_->comments()) | 277 if (end_ && end_->comments()) |
287 end_->Print(out, indent + 1); | 278 end_->Print(out, indent + 1); |
288 } | 279 } |
289 | 280 |
290 Value BlockNode::ExecuteBlockInScope(Scope* our_scope, Err* err) const { | 281 Value BlockNode::ExecuteBlockInScope(Scope* our_scope, Err* err) const { |
291 for (size_t i = 0; i < statements_.size() && !err->has_error(); i++) { | 282 for (size_t i = 0; i < statements_.size() && !err->has_error(); i++) { |
292 // Check for trying to execute things with no side effects in a block. | 283 // Check for trying to execute things with no side effects in a block. |
293 const ParseNode* cur = statements_[i]; | 284 const ParseNode* cur = statements_[i]; |
294 if (cur->AsList() || cur->AsLiteral() || cur->AsUnaryOp() || | 285 if (cur->AsList() || cur->AsLiteral() || cur->AsUnaryOp() || |
295 cur->AsIdentifier()) { | 286 cur->AsIdentifier()) { |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 | 447 |
457 const ListNode* ListNode::AsList() const { | 448 const ListNode* ListNode::AsList() const { |
458 return this; | 449 return this; |
459 } | 450 } |
460 | 451 |
461 Value ListNode::Execute(Scope* scope, Err* err) const { | 452 Value ListNode::Execute(Scope* scope, Err* err) const { |
462 Value result_value(this, Value::LIST); | 453 Value result_value(this, Value::LIST); |
463 std::vector<Value>& results = result_value.list_value(); | 454 std::vector<Value>& results = result_value.list_value(); |
464 results.reserve(contents_.size()); | 455 results.reserve(contents_.size()); |
465 | 456 |
466 for (size_t i = 0; i < contents_.size(); i++) { | 457 for (const auto& cur : contents_) { |
467 const ParseNode* cur = contents_[i]; | |
468 if (cur->AsBlockComment()) | 458 if (cur->AsBlockComment()) |
469 continue; | 459 continue; |
470 results.push_back(cur->Execute(scope, err)); | 460 results.push_back(cur->Execute(scope, err)); |
471 if (err->has_error()) | 461 if (err->has_error()) |
472 return Value(); | 462 return Value(); |
473 if (results.back().type() == Value::NONE) { | 463 if (results.back().type() == Value::NONE) { |
474 *err = cur->MakeErrorDescribing( | 464 *err = cur->MakeErrorDescribing( |
475 "This does not evaluate to a value.", | 465 "This does not evaluate to a value.", |
476 "I can't do something with nothing."); | 466 "I can't do something with nothing."); |
477 return Value(); | 467 return Value(); |
478 } | 468 } |
479 } | 469 } |
480 return result_value; | 470 return result_value; |
481 } | 471 } |
482 | 472 |
483 LocationRange ListNode::GetRange() const { | 473 LocationRange ListNode::GetRange() const { |
484 return LocationRange(begin_token_.location(), | 474 return LocationRange(begin_token_.location(), |
485 end_->value().location()); | 475 end_->value().location()); |
486 } | 476 } |
487 | 477 |
488 Err ListNode::MakeErrorDescribing(const std::string& msg, | 478 Err ListNode::MakeErrorDescribing(const std::string& msg, |
489 const std::string& help) const { | 479 const std::string& help) const { |
490 return Err(begin_token_, msg, help); | 480 return Err(begin_token_, msg, help); |
491 } | 481 } |
492 | 482 |
493 void ListNode::Print(std::ostream& out, int indent) const { | 483 void ListNode::Print(std::ostream& out, int indent) const { |
494 out << IndentFor(indent) << "LIST\n"; | 484 out << IndentFor(indent) << "LIST\n"; |
495 PrintComments(out, indent); | 485 PrintComments(out, indent); |
496 for (size_t i = 0; i < contents_.size(); i++) | 486 for (const auto& cur : contents_) |
497 contents_[i]->Print(out, indent + 1); | 487 cur->Print(out, indent + 1); |
498 if (end_ && end_->comments()) | 488 if (end_ && end_->comments()) |
499 end_->Print(out, indent + 1); | 489 end_->Print(out, indent + 1); |
500 } | 490 } |
501 | 491 |
502 // LiteralNode ----------------------------------------------------------------- | 492 // LiteralNode ----------------------------------------------------------------- |
503 | 493 |
504 LiteralNode::LiteralNode() { | 494 LiteralNode::LiteralNode() { |
505 } | 495 } |
506 | 496 |
507 LiteralNode::LiteralNode(const Token& token) : value_(token) { | 497 LiteralNode::LiteralNode(const Token& token) : value_(token) { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
640 | 630 |
641 Err EndNode::MakeErrorDescribing(const std::string& msg, | 631 Err EndNode::MakeErrorDescribing(const std::string& msg, |
642 const std::string& help) const { | 632 const std::string& help) const { |
643 return Err(value_, msg, help); | 633 return Err(value_, msg, help); |
644 } | 634 } |
645 | 635 |
646 void EndNode::Print(std::ostream& out, int indent) const { | 636 void EndNode::Print(std::ostream& out, int indent) const { |
647 out << IndentFor(indent) << "END(" << value_.value() << ")\n"; | 637 out << IndentFor(indent) << "END(" << value_.value() << ")\n"; |
648 PrintComments(out, indent); | 638 PrintComments(out, indent); |
649 } | 639 } |
OLD | NEW |