Index: tools/gn/parse_tree.cc |
diff --git a/tools/gn/parse_tree.cc b/tools/gn/parse_tree.cc |
index c4f78b7ce0768f9f7e7d025d112e1e25c7c31930..a1d969a1089465827fe3061a40369dfb1b7929d0 100644 |
--- a/tools/gn/parse_tree.cc |
+++ b/tools/gn/parse_tree.cc |
@@ -16,14 +16,17 @@ |
namespace { |
std::string IndentFor(int value) { |
- std::string ret; |
- for (int i = 0; i < value; i++) |
- ret.append(" "); |
- return ret; |
+ return std::string(value, ' '); |
} |
} // namespace |
+Comments::Comments() { |
+} |
+ |
+Comments::~Comments() { |
+} |
+ |
ParseNode::ParseNode() { |
} |
@@ -40,6 +43,33 @@ const ListNode* ParseNode::AsList() const { return NULL; } |
const LiteralNode* ParseNode::AsLiteral() const { return NULL; } |
const UnaryOpNode* ParseNode::AsUnaryOp() const { return NULL; } |
+Comments* ParseNode::comments_mutable() { |
+ if (!comments_) |
+ comments_.reset(new Comments); |
+ return comments_.get(); |
+} |
+ |
+void ParseNode::PrintComments(std::ostream& out, int indent) const { |
+ if (comments_) { |
+ std::string ind = IndentFor(indent + 1); |
+ for (std::vector<Token>::const_iterator i(comments_->before().begin()); |
+ i != comments_->before().end(); |
+ ++i) { |
+ out << ind << "+BEFORE_COMMENT(\"" << i->value() << "\")\n"; |
+ } |
+ for (std::vector<Token>::const_iterator i(comments_->suffix().begin()); |
+ i != comments_->suffix().end(); |
+ ++i) { |
+ out << ind << "+SUFFIX_COMMENT(\"" << i->value() << "\")\n"; |
+ } |
+ for (std::vector<Token>::const_iterator i(comments_->after().begin()); |
+ i != comments_->after().end(); |
+ ++i) { |
+ out << ind << "+AFTER_COMMENT(\"" << i->value() << "\")\n"; |
+ } |
+ } |
+} |
+ |
// AccessorNode --------------------------------------------------------------- |
AccessorNode::AccessorNode() { |
@@ -77,6 +107,7 @@ Err AccessorNode::MakeErrorDescribing(const std::string& msg, |
void AccessorNode::Print(std::ostream& out, int indent) const { |
out << IndentFor(indent) << "ACCESSOR\n"; |
+ PrintComments(out, indent); |
out << IndentFor(indent + 1) << base_.value() << "\n"; |
if (index_) |
index_->Print(out, indent + 1); |
@@ -192,6 +223,7 @@ Err BinaryOpNode::MakeErrorDescribing(const std::string& msg, |
void BinaryOpNode::Print(std::ostream& out, int indent) const { |
out << IndentFor(indent) << "BINARY(" << op_.value() << ")\n"; |
+ PrintComments(out, indent); |
left_->Print(out, indent + 1); |
right_->Print(out, indent + 1); |
} |
@@ -241,6 +273,7 @@ Err BlockNode::MakeErrorDescribing(const std::string& msg, |
void BlockNode::Print(std::ostream& out, int indent) const { |
out << IndentFor(indent) << "BLOCK\n"; |
+ PrintComments(out, indent); |
for (size_t i = 0; i < statements_.size(); i++) |
statements_[i]->Print(out, indent + 1); |
} |
@@ -317,6 +350,7 @@ Err ConditionNode::MakeErrorDescribing(const std::string& msg, |
void ConditionNode::Print(std::ostream& out, int indent) const { |
out << IndentFor(indent) << "CONDITION\n"; |
+ PrintComments(out, indent); |
condition_->Print(out, indent + 1); |
if_true_->Print(out, indent + 1); |
if (if_false_) |
@@ -354,6 +388,7 @@ Err FunctionCallNode::MakeErrorDescribing(const std::string& msg, |
void FunctionCallNode::Print(std::ostream& out, int indent) const { |
out << IndentFor(indent) << "FUNCTION(" << function_.value() << ")\n"; |
+ PrintComments(out, indent); |
args_->Print(out, indent + 1); |
if (block_) |
block_->Print(out, indent + 1); |
@@ -398,6 +433,7 @@ Err IdentifierNode::MakeErrorDescribing(const std::string& msg, |
void IdentifierNode::Print(std::ostream& out, int indent) const { |
out << IndentFor(indent) << "IDENTIFIER(" << value_.value() << ")\n"; |
+ PrintComments(out, indent); |
} |
// ListNode ------------------------------------------------------------------- |
@@ -444,6 +480,7 @@ Err ListNode::MakeErrorDescribing(const std::string& msg, |
void ListNode::Print(std::ostream& out, int indent) const { |
out << IndentFor(indent) << "LIST\n"; |
+ PrintComments(out, indent); |
for (size_t i = 0; i < contents_.size(); i++) |
contents_[i]->Print(out, indent + 1); |
} |
@@ -499,6 +536,7 @@ Err LiteralNode::MakeErrorDescribing(const std::string& msg, |
void LiteralNode::Print(std::ostream& out, int indent) const { |
out << IndentFor(indent) << "LITERAL(" << value_.value() << ")\n"; |
+ PrintComments(out, indent); |
} |
// UnaryOpNode ---------------------------------------------------------------- |
@@ -531,5 +569,6 @@ Err UnaryOpNode::MakeErrorDescribing(const std::string& msg, |
void UnaryOpNode::Print(std::ostream& out, int indent) const { |
out << IndentFor(indent) << "UNARY(" << op_.value() << ")\n"; |
+ PrintComments(out, indent); |
operand_->Print(out, indent + 1); |
} |