Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: tools/gn/parse_tree.cc

Issue 588893006: gn: attach comments to parse tree (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: x64 Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/gn/parse_tree.h ('k') | tools/gn/parse_tree_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "tools/gn/functions.h" 11 #include "tools/gn/functions.h"
12 #include "tools/gn/operators.h" 12 #include "tools/gn/operators.h"
13 #include "tools/gn/scope.h" 13 #include "tools/gn/scope.h"
14 #include "tools/gn/string_utils.h" 14 #include "tools/gn/string_utils.h"
15 15
16 namespace { 16 namespace {
17 17
18 std::string IndentFor(int value) { 18 std::string IndentFor(int value) {
19 std::string ret; 19 return std::string(value, ' ');
20 for (int i = 0; i < value; i++)
21 ret.append(" ");
22 return ret;
23 } 20 }
24 21
25 } // namespace 22 } // namespace
26 23
24 Comments::Comments() {
25 }
26
27 Comments::~Comments() {
28 }
29
27 ParseNode::ParseNode() { 30 ParseNode::ParseNode() {
28 } 31 }
29 32
30 ParseNode::~ParseNode() { 33 ParseNode::~ParseNode() {
31 } 34 }
32 35
33 const AccessorNode* ParseNode::AsAccessor() const { return NULL; } 36 const AccessorNode* ParseNode::AsAccessor() const { return NULL; }
34 const BinaryOpNode* ParseNode::AsBinaryOp() const { return NULL; } 37 const BinaryOpNode* ParseNode::AsBinaryOp() const { return NULL; }
35 const BlockNode* ParseNode::AsBlock() const { return NULL; } 38 const BlockNode* ParseNode::AsBlock() const { return NULL; }
36 const ConditionNode* ParseNode::AsConditionNode() const { return NULL; } 39 const ConditionNode* ParseNode::AsConditionNode() const { return NULL; }
37 const FunctionCallNode* ParseNode::AsFunctionCall() const { return NULL; } 40 const FunctionCallNode* ParseNode::AsFunctionCall() const { return NULL; }
38 const IdentifierNode* ParseNode::AsIdentifier() const { return NULL; } 41 const IdentifierNode* ParseNode::AsIdentifier() const { return NULL; }
39 const ListNode* ParseNode::AsList() const { return NULL; } 42 const ListNode* ParseNode::AsList() const { return NULL; }
40 const LiteralNode* ParseNode::AsLiteral() const { return NULL; } 43 const LiteralNode* ParseNode::AsLiteral() const { return NULL; }
41 const UnaryOpNode* ParseNode::AsUnaryOp() const { return NULL; } 44 const UnaryOpNode* ParseNode::AsUnaryOp() const { return NULL; }
42 45
46 Comments* ParseNode::comments_mutable() {
47 if (!comments_)
48 comments_.reset(new Comments);
49 return comments_.get();
50 }
51
52 void ParseNode::PrintComments(std::ostream& out, int indent) const {
53 if (comments_) {
54 std::string ind = IndentFor(indent + 1);
55 for (std::vector<Token>::const_iterator i(comments_->before().begin());
56 i != comments_->before().end();
57 ++i) {
58 out << ind << "+BEFORE_COMMENT(\"" << i->value() << "\")\n";
59 }
60 for (std::vector<Token>::const_iterator i(comments_->suffix().begin());
61 i != comments_->suffix().end();
62 ++i) {
63 out << ind << "+SUFFIX_COMMENT(\"" << i->value() << "\")\n";
64 }
65 for (std::vector<Token>::const_iterator i(comments_->after().begin());
66 i != comments_->after().end();
67 ++i) {
68 out << ind << "+AFTER_COMMENT(\"" << i->value() << "\")\n";
69 }
70 }
71 }
72
43 // AccessorNode --------------------------------------------------------------- 73 // AccessorNode ---------------------------------------------------------------
44 74
45 AccessorNode::AccessorNode() { 75 AccessorNode::AccessorNode() {
46 } 76 }
47 77
48 AccessorNode::~AccessorNode() { 78 AccessorNode::~AccessorNode() {
49 } 79 }
50 80
51 const AccessorNode* AccessorNode::AsAccessor() const { 81 const AccessorNode* AccessorNode::AsAccessor() const {
52 return this; 82 return this;
(...skipping 17 matching lines...) Expand all
70 return LocationRange(); 100 return LocationRange();
71 } 101 }
72 102
73 Err AccessorNode::MakeErrorDescribing(const std::string& msg, 103 Err AccessorNode::MakeErrorDescribing(const std::string& msg,
74 const std::string& help) const { 104 const std::string& help) const {
75 return Err(GetRange(), msg, help); 105 return Err(GetRange(), msg, help);
76 } 106 }
77 107
78 void AccessorNode::Print(std::ostream& out, int indent) const { 108 void AccessorNode::Print(std::ostream& out, int indent) const {
79 out << IndentFor(indent) << "ACCESSOR\n"; 109 out << IndentFor(indent) << "ACCESSOR\n";
110 PrintComments(out, indent);
80 out << IndentFor(indent + 1) << base_.value() << "\n"; 111 out << IndentFor(indent + 1) << base_.value() << "\n";
81 if (index_) 112 if (index_)
82 index_->Print(out, indent + 1); 113 index_->Print(out, indent + 1);
83 else if (member_) 114 else if (member_)
84 member_->Print(out, indent + 1); 115 member_->Print(out, indent + 1);
85 } 116 }
86 117
87 Value AccessorNode::ExecuteArrayAccess(Scope* scope, Err* err) const { 118 Value AccessorNode::ExecuteArrayAccess(Scope* scope, Err* err) const {
88 Value index_value = index_->Execute(scope, err); 119 Value index_value = index_->Execute(scope, err);
89 if (err->has_error()) 120 if (err->has_error())
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 return left_->GetRange().Union(right_->GetRange()); 216 return left_->GetRange().Union(right_->GetRange());
186 } 217 }
187 218
188 Err BinaryOpNode::MakeErrorDescribing(const std::string& msg, 219 Err BinaryOpNode::MakeErrorDescribing(const std::string& msg,
189 const std::string& help) const { 220 const std::string& help) const {
190 return Err(op_, msg, help); 221 return Err(op_, msg, help);
191 } 222 }
192 223
193 void BinaryOpNode::Print(std::ostream& out, int indent) const { 224 void BinaryOpNode::Print(std::ostream& out, int indent) const {
194 out << IndentFor(indent) << "BINARY(" << op_.value() << ")\n"; 225 out << IndentFor(indent) << "BINARY(" << op_.value() << ")\n";
226 PrintComments(out, indent);
195 left_->Print(out, indent + 1); 227 left_->Print(out, indent + 1);
196 right_->Print(out, indent + 1); 228 right_->Print(out, indent + 1);
197 } 229 }
198 230
199 // BlockNode ------------------------------------------------------------------ 231 // BlockNode ------------------------------------------------------------------
200 232
201 BlockNode::BlockNode(bool has_scope) : has_scope_(has_scope) { 233 BlockNode::BlockNode(bool has_scope) : has_scope_(has_scope) {
202 } 234 }
203 235
204 BlockNode::~BlockNode() { 236 BlockNode::~BlockNode() {
(...skipping 29 matching lines...) Expand all
234 return LocationRange(); 266 return LocationRange();
235 } 267 }
236 268
237 Err BlockNode::MakeErrorDescribing(const std::string& msg, 269 Err BlockNode::MakeErrorDescribing(const std::string& msg,
238 const std::string& help) const { 270 const std::string& help) const {
239 return Err(GetRange(), msg, help); 271 return Err(GetRange(), msg, help);
240 } 272 }
241 273
242 void BlockNode::Print(std::ostream& out, int indent) const { 274 void BlockNode::Print(std::ostream& out, int indent) const {
243 out << IndentFor(indent) << "BLOCK\n"; 275 out << IndentFor(indent) << "BLOCK\n";
276 PrintComments(out, indent);
244 for (size_t i = 0; i < statements_.size(); i++) 277 for (size_t i = 0; i < statements_.size(); i++)
245 statements_[i]->Print(out, indent + 1); 278 statements_[i]->Print(out, indent + 1);
246 } 279 }
247 280
248 Value BlockNode::ExecuteBlockInScope(Scope* our_scope, Err* err) const { 281 Value BlockNode::ExecuteBlockInScope(Scope* our_scope, Err* err) const {
249 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++) {
250 // 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.
251 const ParseNode* cur = statements_[i]; 284 const ParseNode* cur = statements_[i];
252 if (cur->AsList() || cur->AsLiteral() || cur->AsUnaryOp() || 285 if (cur->AsList() || cur->AsLiteral() || cur->AsUnaryOp() ||
253 cur->AsIdentifier()) { 286 cur->AsIdentifier()) {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 return if_token_.range().Union(if_true_->GetRange()); 343 return if_token_.range().Union(if_true_->GetRange());
311 } 344 }
312 345
313 Err ConditionNode::MakeErrorDescribing(const std::string& msg, 346 Err ConditionNode::MakeErrorDescribing(const std::string& msg,
314 const std::string& help) const { 347 const std::string& help) const {
315 return Err(if_token_, msg, help); 348 return Err(if_token_, msg, help);
316 } 349 }
317 350
318 void ConditionNode::Print(std::ostream& out, int indent) const { 351 void ConditionNode::Print(std::ostream& out, int indent) const {
319 out << IndentFor(indent) << "CONDITION\n"; 352 out << IndentFor(indent) << "CONDITION\n";
353 PrintComments(out, indent);
320 condition_->Print(out, indent + 1); 354 condition_->Print(out, indent + 1);
321 if_true_->Print(out, indent + 1); 355 if_true_->Print(out, indent + 1);
322 if (if_false_) 356 if (if_false_)
323 if_false_->Print(out, indent + 1); 357 if_false_->Print(out, indent + 1);
324 } 358 }
325 359
326 // FunctionCallNode ----------------------------------------------------------- 360 // FunctionCallNode -----------------------------------------------------------
327 361
328 FunctionCallNode::FunctionCallNode() { 362 FunctionCallNode::FunctionCallNode() {
329 } 363 }
(...skipping 17 matching lines...) Expand all
347 return function_.range().Union(args_->GetRange()); 381 return function_.range().Union(args_->GetRange());
348 } 382 }
349 383
350 Err FunctionCallNode::MakeErrorDescribing(const std::string& msg, 384 Err FunctionCallNode::MakeErrorDescribing(const std::string& msg,
351 const std::string& help) const { 385 const std::string& help) const {
352 return Err(function_, msg, help); 386 return Err(function_, msg, help);
353 } 387 }
354 388
355 void FunctionCallNode::Print(std::ostream& out, int indent) const { 389 void FunctionCallNode::Print(std::ostream& out, int indent) const {
356 out << IndentFor(indent) << "FUNCTION(" << function_.value() << ")\n"; 390 out << IndentFor(indent) << "FUNCTION(" << function_.value() << ")\n";
391 PrintComments(out, indent);
357 args_->Print(out, indent + 1); 392 args_->Print(out, indent + 1);
358 if (block_) 393 if (block_)
359 block_->Print(out, indent + 1); 394 block_->Print(out, indent + 1);
360 } 395 }
361 396
362 // IdentifierNode -------------------------------------------------------------- 397 // IdentifierNode --------------------------------------------------------------
363 398
364 IdentifierNode::IdentifierNode() { 399 IdentifierNode::IdentifierNode() {
365 } 400 }
366 401
(...skipping 24 matching lines...) Expand all
391 return value_.range(); 426 return value_.range();
392 } 427 }
393 428
394 Err IdentifierNode::MakeErrorDescribing(const std::string& msg, 429 Err IdentifierNode::MakeErrorDescribing(const std::string& msg,
395 const std::string& help) const { 430 const std::string& help) const {
396 return Err(value_, msg, help); 431 return Err(value_, msg, help);
397 } 432 }
398 433
399 void IdentifierNode::Print(std::ostream& out, int indent) const { 434 void IdentifierNode::Print(std::ostream& out, int indent) const {
400 out << IndentFor(indent) << "IDENTIFIER(" << value_.value() << ")\n"; 435 out << IndentFor(indent) << "IDENTIFIER(" << value_.value() << ")\n";
436 PrintComments(out, indent);
401 } 437 }
402 438
403 // ListNode ------------------------------------------------------------------- 439 // ListNode -------------------------------------------------------------------
404 440
405 ListNode::ListNode() { 441 ListNode::ListNode() {
406 } 442 }
407 443
408 ListNode::~ListNode() { 444 ListNode::~ListNode() {
409 STLDeleteContainerPointers(contents_.begin(), contents_.end()); 445 STLDeleteContainerPointers(contents_.begin(), contents_.end());
410 } 446 }
(...skipping 26 matching lines...) Expand all
437 return LocationRange(begin_token_.location(), end_token_.location()); 473 return LocationRange(begin_token_.location(), end_token_.location());
438 } 474 }
439 475
440 Err ListNode::MakeErrorDescribing(const std::string& msg, 476 Err ListNode::MakeErrorDescribing(const std::string& msg,
441 const std::string& help) const { 477 const std::string& help) const {
442 return Err(begin_token_, msg, help); 478 return Err(begin_token_, msg, help);
443 } 479 }
444 480
445 void ListNode::Print(std::ostream& out, int indent) const { 481 void ListNode::Print(std::ostream& out, int indent) const {
446 out << IndentFor(indent) << "LIST\n"; 482 out << IndentFor(indent) << "LIST\n";
483 PrintComments(out, indent);
447 for (size_t i = 0; i < contents_.size(); i++) 484 for (size_t i = 0; i < contents_.size(); i++)
448 contents_[i]->Print(out, indent + 1); 485 contents_[i]->Print(out, indent + 1);
449 } 486 }
450 487
451 // LiteralNode ----------------------------------------------------------------- 488 // LiteralNode -----------------------------------------------------------------
452 489
453 LiteralNode::LiteralNode() { 490 LiteralNode::LiteralNode() {
454 } 491 }
455 492
456 LiteralNode::LiteralNode(const Token& token) : value_(token) { 493 LiteralNode::LiteralNode(const Token& token) : value_(token) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 return value_.range(); 529 return value_.range();
493 } 530 }
494 531
495 Err LiteralNode::MakeErrorDescribing(const std::string& msg, 532 Err LiteralNode::MakeErrorDescribing(const std::string& msg,
496 const std::string& help) const { 533 const std::string& help) const {
497 return Err(value_, msg, help); 534 return Err(value_, msg, help);
498 } 535 }
499 536
500 void LiteralNode::Print(std::ostream& out, int indent) const { 537 void LiteralNode::Print(std::ostream& out, int indent) const {
501 out << IndentFor(indent) << "LITERAL(" << value_.value() << ")\n"; 538 out << IndentFor(indent) << "LITERAL(" << value_.value() << ")\n";
539 PrintComments(out, indent);
502 } 540 }
503 541
504 // UnaryOpNode ---------------------------------------------------------------- 542 // UnaryOpNode ----------------------------------------------------------------
505 543
506 UnaryOpNode::UnaryOpNode() { 544 UnaryOpNode::UnaryOpNode() {
507 } 545 }
508 546
509 UnaryOpNode::~UnaryOpNode() { 547 UnaryOpNode::~UnaryOpNode() {
510 } 548 }
511 549
(...skipping 12 matching lines...) Expand all
524 return op_.range().Union(operand_->GetRange()); 562 return op_.range().Union(operand_->GetRange());
525 } 563 }
526 564
527 Err UnaryOpNode::MakeErrorDescribing(const std::string& msg, 565 Err UnaryOpNode::MakeErrorDescribing(const std::string& msg,
528 const std::string& help) const { 566 const std::string& help) const {
529 return Err(op_, msg, help); 567 return Err(op_, msg, help);
530 } 568 }
531 569
532 void UnaryOpNode::Print(std::ostream& out, int indent) const { 570 void UnaryOpNode::Print(std::ostream& out, int indent) const {
533 out << IndentFor(indent) << "UNARY(" << op_.value() << ")\n"; 571 out << IndentFor(indent) << "UNARY(" << op_.value() << ")\n";
572 PrintComments(out, indent);
534 operand_->Print(out, indent + 1); 573 operand_->Print(out, indent + 1);
535 } 574 }
OLDNEW
« no previous file with comments | « tools/gn/parse_tree.h ('k') | tools/gn/parse_tree_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698