Chromium Code Reviews| 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" |
| 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 (const auto& i : comments_->before()) | |
|
brettw
2014/09/23 21:33:15
This is technically on the list of stuff "to be di
scottmg
2014/09/23 22:15:37
Done.
| |
| 56 out << ind << "+BEFORE_COMMENT(\"" << i.value() << "\")\n"; | |
| 57 for (const auto& i : comments_->suffix()) | |
| 58 out << ind << "+SUFFIX_COMMENT(\"" << i.value() << "\")\n"; | |
| 59 for (const auto& i : comments_->after()) | |
| 60 out << ind << "+AFTER_COMMENT(\"" << i.value() << "\")\n"; | |
| 61 } | |
| 62 } | |
| 63 | |
| 43 // AccessorNode --------------------------------------------------------------- | 64 // AccessorNode --------------------------------------------------------------- |
| 44 | 65 |
| 45 AccessorNode::AccessorNode() { | 66 AccessorNode::AccessorNode() { |
| 46 } | 67 } |
| 47 | 68 |
| 48 AccessorNode::~AccessorNode() { | 69 AccessorNode::~AccessorNode() { |
| 49 } | 70 } |
| 50 | 71 |
| 51 const AccessorNode* AccessorNode::AsAccessor() const { | 72 const AccessorNode* AccessorNode::AsAccessor() const { |
| 52 return this; | 73 return this; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 70 return LocationRange(); | 91 return LocationRange(); |
| 71 } | 92 } |
| 72 | 93 |
| 73 Err AccessorNode::MakeErrorDescribing(const std::string& msg, | 94 Err AccessorNode::MakeErrorDescribing(const std::string& msg, |
| 74 const std::string& help) const { | 95 const std::string& help) const { |
| 75 return Err(GetRange(), msg, help); | 96 return Err(GetRange(), msg, help); |
| 76 } | 97 } |
| 77 | 98 |
| 78 void AccessorNode::Print(std::ostream& out, int indent) const { | 99 void AccessorNode::Print(std::ostream& out, int indent) const { |
| 79 out << IndentFor(indent) << "ACCESSOR\n"; | 100 out << IndentFor(indent) << "ACCESSOR\n"; |
| 101 PrintComments(out, indent); | |
| 80 out << IndentFor(indent + 1) << base_.value() << "\n"; | 102 out << IndentFor(indent + 1) << base_.value() << "\n"; |
| 81 if (index_) | 103 if (index_) |
| 82 index_->Print(out, indent + 1); | 104 index_->Print(out, indent + 1); |
| 83 else if (member_) | 105 else if (member_) |
| 84 member_->Print(out, indent + 1); | 106 member_->Print(out, indent + 1); |
| 85 } | 107 } |
| 86 | 108 |
| 87 Value AccessorNode::ExecuteArrayAccess(Scope* scope, Err* err) const { | 109 Value AccessorNode::ExecuteArrayAccess(Scope* scope, Err* err) const { |
| 88 Value index_value = index_->Execute(scope, err); | 110 Value index_value = index_->Execute(scope, err); |
| 89 if (err->has_error()) | 111 if (err->has_error()) |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 185 return left_->GetRange().Union(right_->GetRange()); | 207 return left_->GetRange().Union(right_->GetRange()); |
| 186 } | 208 } |
| 187 | 209 |
| 188 Err BinaryOpNode::MakeErrorDescribing(const std::string& msg, | 210 Err BinaryOpNode::MakeErrorDescribing(const std::string& msg, |
| 189 const std::string& help) const { | 211 const std::string& help) const { |
| 190 return Err(op_, msg, help); | 212 return Err(op_, msg, help); |
| 191 } | 213 } |
| 192 | 214 |
| 193 void BinaryOpNode::Print(std::ostream& out, int indent) const { | 215 void BinaryOpNode::Print(std::ostream& out, int indent) const { |
| 194 out << IndentFor(indent) << "BINARY(" << op_.value() << ")\n"; | 216 out << IndentFor(indent) << "BINARY(" << op_.value() << ")\n"; |
| 217 PrintComments(out, indent); | |
| 195 left_->Print(out, indent + 1); | 218 left_->Print(out, indent + 1); |
| 196 right_->Print(out, indent + 1); | 219 right_->Print(out, indent + 1); |
| 197 } | 220 } |
| 198 | 221 |
| 199 // BlockNode ------------------------------------------------------------------ | 222 // BlockNode ------------------------------------------------------------------ |
| 200 | 223 |
| 201 BlockNode::BlockNode(bool has_scope) : has_scope_(has_scope) { | 224 BlockNode::BlockNode(bool has_scope) : has_scope_(has_scope) { |
| 202 } | 225 } |
| 203 | 226 |
| 204 BlockNode::~BlockNode() { | 227 BlockNode::~BlockNode() { |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 234 return LocationRange(); | 257 return LocationRange(); |
| 235 } | 258 } |
| 236 | 259 |
| 237 Err BlockNode::MakeErrorDescribing(const std::string& msg, | 260 Err BlockNode::MakeErrorDescribing(const std::string& msg, |
| 238 const std::string& help) const { | 261 const std::string& help) const { |
| 239 return Err(GetRange(), msg, help); | 262 return Err(GetRange(), msg, help); |
| 240 } | 263 } |
| 241 | 264 |
| 242 void BlockNode::Print(std::ostream& out, int indent) const { | 265 void BlockNode::Print(std::ostream& out, int indent) const { |
| 243 out << IndentFor(indent) << "BLOCK\n"; | 266 out << IndentFor(indent) << "BLOCK\n"; |
| 267 PrintComments(out, indent); | |
| 244 for (size_t i = 0; i < statements_.size(); i++) | 268 for (size_t i = 0; i < statements_.size(); i++) |
| 245 statements_[i]->Print(out, indent + 1); | 269 statements_[i]->Print(out, indent + 1); |
| 246 } | 270 } |
| 247 | 271 |
| 248 Value BlockNode::ExecuteBlockInScope(Scope* our_scope, Err* err) const { | 272 Value BlockNode::ExecuteBlockInScope(Scope* our_scope, Err* err) const { |
| 249 for (size_t i = 0; i < statements_.size() && !err->has_error(); i++) { | 273 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. | 274 // Check for trying to execute things with no side effects in a block. |
| 251 const ParseNode* cur = statements_[i]; | 275 const ParseNode* cur = statements_[i]; |
| 252 if (cur->AsList() || cur->AsLiteral() || cur->AsUnaryOp() || | 276 if (cur->AsList() || cur->AsLiteral() || cur->AsUnaryOp() || |
| 253 cur->AsIdentifier()) { | 277 cur->AsIdentifier()) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 return if_token_.range().Union(if_true_->GetRange()); | 334 return if_token_.range().Union(if_true_->GetRange()); |
| 311 } | 335 } |
| 312 | 336 |
| 313 Err ConditionNode::MakeErrorDescribing(const std::string& msg, | 337 Err ConditionNode::MakeErrorDescribing(const std::string& msg, |
| 314 const std::string& help) const { | 338 const std::string& help) const { |
| 315 return Err(if_token_, msg, help); | 339 return Err(if_token_, msg, help); |
| 316 } | 340 } |
| 317 | 341 |
| 318 void ConditionNode::Print(std::ostream& out, int indent) const { | 342 void ConditionNode::Print(std::ostream& out, int indent) const { |
| 319 out << IndentFor(indent) << "CONDITION\n"; | 343 out << IndentFor(indent) << "CONDITION\n"; |
| 344 PrintComments(out, indent); | |
| 320 condition_->Print(out, indent + 1); | 345 condition_->Print(out, indent + 1); |
| 321 if_true_->Print(out, indent + 1); | 346 if_true_->Print(out, indent + 1); |
| 322 if (if_false_) | 347 if (if_false_) |
| 323 if_false_->Print(out, indent + 1); | 348 if_false_->Print(out, indent + 1); |
| 324 } | 349 } |
| 325 | 350 |
| 326 // FunctionCallNode ----------------------------------------------------------- | 351 // FunctionCallNode ----------------------------------------------------------- |
| 327 | 352 |
| 328 FunctionCallNode::FunctionCallNode() { | 353 FunctionCallNode::FunctionCallNode() { |
| 329 } | 354 } |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 347 return function_.range().Union(args_->GetRange()); | 372 return function_.range().Union(args_->GetRange()); |
| 348 } | 373 } |
| 349 | 374 |
| 350 Err FunctionCallNode::MakeErrorDescribing(const std::string& msg, | 375 Err FunctionCallNode::MakeErrorDescribing(const std::string& msg, |
| 351 const std::string& help) const { | 376 const std::string& help) const { |
| 352 return Err(function_, msg, help); | 377 return Err(function_, msg, help); |
| 353 } | 378 } |
| 354 | 379 |
| 355 void FunctionCallNode::Print(std::ostream& out, int indent) const { | 380 void FunctionCallNode::Print(std::ostream& out, int indent) const { |
| 356 out << IndentFor(indent) << "FUNCTION(" << function_.value() << ")\n"; | 381 out << IndentFor(indent) << "FUNCTION(" << function_.value() << ")\n"; |
| 382 PrintComments(out, indent); | |
| 357 args_->Print(out, indent + 1); | 383 args_->Print(out, indent + 1); |
| 358 if (block_) | 384 if (block_) |
| 359 block_->Print(out, indent + 1); | 385 block_->Print(out, indent + 1); |
| 360 } | 386 } |
| 361 | 387 |
| 362 // IdentifierNode -------------------------------------------------------------- | 388 // IdentifierNode -------------------------------------------------------------- |
| 363 | 389 |
| 364 IdentifierNode::IdentifierNode() { | 390 IdentifierNode::IdentifierNode() { |
| 365 } | 391 } |
| 366 | 392 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 391 return value_.range(); | 417 return value_.range(); |
| 392 } | 418 } |
| 393 | 419 |
| 394 Err IdentifierNode::MakeErrorDescribing(const std::string& msg, | 420 Err IdentifierNode::MakeErrorDescribing(const std::string& msg, |
| 395 const std::string& help) const { | 421 const std::string& help) const { |
| 396 return Err(value_, msg, help); | 422 return Err(value_, msg, help); |
| 397 } | 423 } |
| 398 | 424 |
| 399 void IdentifierNode::Print(std::ostream& out, int indent) const { | 425 void IdentifierNode::Print(std::ostream& out, int indent) const { |
| 400 out << IndentFor(indent) << "IDENTIFIER(" << value_.value() << ")\n"; | 426 out << IndentFor(indent) << "IDENTIFIER(" << value_.value() << ")\n"; |
| 427 PrintComments(out, indent); | |
| 401 } | 428 } |
| 402 | 429 |
| 403 // ListNode ------------------------------------------------------------------- | 430 // ListNode ------------------------------------------------------------------- |
| 404 | 431 |
| 405 ListNode::ListNode() { | 432 ListNode::ListNode() { |
| 406 } | 433 } |
| 407 | 434 |
| 408 ListNode::~ListNode() { | 435 ListNode::~ListNode() { |
| 409 STLDeleteContainerPointers(contents_.begin(), contents_.end()); | 436 STLDeleteContainerPointers(contents_.begin(), contents_.end()); |
| 410 } | 437 } |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 437 return LocationRange(begin_token_.location(), end_token_.location()); | 464 return LocationRange(begin_token_.location(), end_token_.location()); |
| 438 } | 465 } |
| 439 | 466 |
| 440 Err ListNode::MakeErrorDescribing(const std::string& msg, | 467 Err ListNode::MakeErrorDescribing(const std::string& msg, |
| 441 const std::string& help) const { | 468 const std::string& help) const { |
| 442 return Err(begin_token_, msg, help); | 469 return Err(begin_token_, msg, help); |
| 443 } | 470 } |
| 444 | 471 |
| 445 void ListNode::Print(std::ostream& out, int indent) const { | 472 void ListNode::Print(std::ostream& out, int indent) const { |
| 446 out << IndentFor(indent) << "LIST\n"; | 473 out << IndentFor(indent) << "LIST\n"; |
| 474 PrintComments(out, indent); | |
| 447 for (size_t i = 0; i < contents_.size(); i++) | 475 for (size_t i = 0; i < contents_.size(); i++) |
| 448 contents_[i]->Print(out, indent + 1); | 476 contents_[i]->Print(out, indent + 1); |
| 449 } | 477 } |
| 450 | 478 |
| 451 // LiteralNode ----------------------------------------------------------------- | 479 // LiteralNode ----------------------------------------------------------------- |
| 452 | 480 |
| 453 LiteralNode::LiteralNode() { | 481 LiteralNode::LiteralNode() { |
| 454 } | 482 } |
| 455 | 483 |
| 456 LiteralNode::LiteralNode(const Token& token) : value_(token) { | 484 LiteralNode::LiteralNode(const Token& token) : value_(token) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 492 return value_.range(); | 520 return value_.range(); |
| 493 } | 521 } |
| 494 | 522 |
| 495 Err LiteralNode::MakeErrorDescribing(const std::string& msg, | 523 Err LiteralNode::MakeErrorDescribing(const std::string& msg, |
| 496 const std::string& help) const { | 524 const std::string& help) const { |
| 497 return Err(value_, msg, help); | 525 return Err(value_, msg, help); |
| 498 } | 526 } |
| 499 | 527 |
| 500 void LiteralNode::Print(std::ostream& out, int indent) const { | 528 void LiteralNode::Print(std::ostream& out, int indent) const { |
| 501 out << IndentFor(indent) << "LITERAL(" << value_.value() << ")\n"; | 529 out << IndentFor(indent) << "LITERAL(" << value_.value() << ")\n"; |
| 530 PrintComments(out, indent); | |
| 502 } | 531 } |
| 503 | 532 |
| 504 // UnaryOpNode ---------------------------------------------------------------- | 533 // UnaryOpNode ---------------------------------------------------------------- |
| 505 | 534 |
| 506 UnaryOpNode::UnaryOpNode() { | 535 UnaryOpNode::UnaryOpNode() { |
| 507 } | 536 } |
| 508 | 537 |
| 509 UnaryOpNode::~UnaryOpNode() { | 538 UnaryOpNode::~UnaryOpNode() { |
| 510 } | 539 } |
| 511 | 540 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 524 return op_.range().Union(operand_->GetRange()); | 553 return op_.range().Union(operand_->GetRange()); |
| 525 } | 554 } |
| 526 | 555 |
| 527 Err UnaryOpNode::MakeErrorDescribing(const std::string& msg, | 556 Err UnaryOpNode::MakeErrorDescribing(const std::string& msg, |
| 528 const std::string& help) const { | 557 const std::string& help) const { |
| 529 return Err(op_, msg, help); | 558 return Err(op_, msg, help); |
| 530 } | 559 } |
| 531 | 560 |
| 532 void UnaryOpNode::Print(std::ostream& out, int indent) const { | 561 void UnaryOpNode::Print(std::ostream& out, int indent) const { |
| 533 out << IndentFor(indent) << "UNARY(" << op_.value() << ")\n"; | 562 out << IndentFor(indent) << "UNARY(" << op_.value() << ")\n"; |
| 563 PrintComments(out, indent); | |
| 534 operand_->Print(out, indent + 1); | 564 operand_->Print(out, indent + 1); |
| 535 } | 565 } |
| OLD | NEW |