| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef TOOLS_GN_PARSE_TREE_H_ | 5 #ifndef TOOLS_GN_PARSE_TREE_H_ |
| 6 #define TOOLS_GN_PARSE_TREE_H_ | 6 #define TOOLS_GN_PARSE_TREE_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 class LiteralNode; | 25 class LiteralNode; |
| 26 class Scope; | 26 class Scope; |
| 27 class UnaryOpNode; | 27 class UnaryOpNode; |
| 28 | 28 |
| 29 class Comments { | 29 class Comments { |
| 30 public: | 30 public: |
| 31 Comments(); | 31 Comments(); |
| 32 virtual ~Comments(); | 32 virtual ~Comments(); |
| 33 | 33 |
| 34 const std::vector<Token>& before() const { return before_; } | 34 const std::vector<Token>& before() const { return before_; } |
| 35 void append_before(Token c) { | 35 void append_before(Token c) { before_.push_back(c); } |
| 36 before_.push_back(c); | 36 void clear_before() { before_.clear(); } |
| 37 } | |
| 38 | 37 |
| 39 const std::vector<Token>& suffix() const { return suffix_; } | 38 const std::vector<Token>& suffix() const { return suffix_; } |
| 40 void append_suffix(Token c) { | 39 void append_suffix(Token c) { suffix_.push_back(c); } |
| 41 suffix_.push_back(c); | |
| 42 } | |
| 43 // Reverse the order of the suffix comments. When walking the tree in | 40 // Reverse the order of the suffix comments. When walking the tree in |
| 44 // post-order we append suffix comments in reverse order, so this fixes them | 41 // post-order we append suffix comments in reverse order, so this fixes them |
| 45 // up. | 42 // up. |
| 46 void ReverseSuffix(); | 43 void ReverseSuffix(); |
| 47 | 44 |
| 48 const std::vector<Token>& after() const { return after_; } | 45 const std::vector<Token>& after() const { return after_; } |
| 49 void append_after(Token c) { | 46 void append_after(Token c) { after_.push_back(c); } |
| 50 after_.push_back(c); | |
| 51 } | |
| 52 | 47 |
| 53 private: | 48 private: |
| 54 // Whole line comments before the expression. | 49 // Whole line comments before the expression. |
| 55 std::vector<Token> before_; | 50 std::vector<Token> before_; |
| 56 | 51 |
| 57 // End-of-line comments after this expression. | 52 // End-of-line comments after this expression. |
| 58 std::vector<Token> suffix_; | 53 std::vector<Token> suffix_; |
| 59 | 54 |
| 60 // For top-level expressions only, after_ lists whole-line comments | 55 // For top-level expressions only, after_ lists whole-line comments |
| 61 // following the expression. | 56 // following the expression. |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 | 148 |
| 154 // Index is the expression inside the []. Will be null if member is set. | 149 // Index is the expression inside the []. Will be null if member is set. |
| 155 const ParseNode* index() const { return index_.get(); } | 150 const ParseNode* index() const { return index_.get(); } |
| 156 void set_index(scoped_ptr<ParseNode> i) { index_ = i.Pass(); } | 151 void set_index(scoped_ptr<ParseNode> i) { index_ = i.Pass(); } |
| 157 | 152 |
| 158 // The member is the identifier on the right hand side of the dot. Will be | 153 // The member is the identifier on the right hand side of the dot. Will be |
| 159 // null if the index is set. | 154 // null if the index is set. |
| 160 const IdentifierNode* member() const { return member_.get(); } | 155 const IdentifierNode* member() const { return member_.get(); } |
| 161 void set_member(scoped_ptr<IdentifierNode> i) { member_ = i.Pass(); } | 156 void set_member(scoped_ptr<IdentifierNode> i) { member_ = i.Pass(); } |
| 162 | 157 |
| 158 void SetNewLocation(int line_number); |
| 159 |
| 163 private: | 160 private: |
| 164 Value ExecuteArrayAccess(Scope* scope, Err* err) const; | 161 Value ExecuteArrayAccess(Scope* scope, Err* err) const; |
| 165 Value ExecuteScopeAccess(Scope* scope, Err* err) const; | 162 Value ExecuteScopeAccess(Scope* scope, Err* err) const; |
| 166 | 163 |
| 167 Token base_; | 164 Token base_; |
| 168 | 165 |
| 169 // Either index or member will be set according to what type of access this | 166 // Either index or member will be set according to what type of access this |
| 170 // is. | 167 // is. |
| 171 scoped_ptr<ParseNode> index_; | 168 scoped_ptr<ParseNode> index_; |
| 172 scoped_ptr<IdentifierNode> member_; | 169 scoped_ptr<IdentifierNode> member_; |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 Value Execute(Scope* scope, Err* err) const override; | 338 Value Execute(Scope* scope, Err* err) const override; |
| 342 LocationRange GetRange() const override; | 339 LocationRange GetRange() const override; |
| 343 Err MakeErrorDescribing( | 340 Err MakeErrorDescribing( |
| 344 const std::string& msg, | 341 const std::string& msg, |
| 345 const std::string& help = std::string()) const override; | 342 const std::string& help = std::string()) const override; |
| 346 void Print(std::ostream& out, int indent) const override; | 343 void Print(std::ostream& out, int indent) const override; |
| 347 | 344 |
| 348 const Token& value() const { return value_; } | 345 const Token& value() const { return value_; } |
| 349 void set_value(const Token& t) { value_ = t; } | 346 void set_value(const Token& t) { value_ = t; } |
| 350 | 347 |
| 348 void SetNewLocation(int line_number); |
| 349 |
| 351 private: | 350 private: |
| 352 Token value_; | 351 Token value_; |
| 353 | 352 |
| 354 DISALLOW_COPY_AND_ASSIGN(IdentifierNode); | 353 DISALLOW_COPY_AND_ASSIGN(IdentifierNode); |
| 355 }; | 354 }; |
| 356 | 355 |
| 357 // ListNode -------------------------------------------------------------------- | 356 // ListNode -------------------------------------------------------------------- |
| 358 | 357 |
| 359 class ListNode : public ParseNode { | 358 class ListNode : public ParseNode { |
| 360 public: | 359 public: |
| (...skipping 10 matching lines...) Expand all Loading... |
| 371 | 370 |
| 372 void set_begin_token(const Token& t) { begin_token_ = t; } | 371 void set_begin_token(const Token& t) { begin_token_ = t; } |
| 373 void set_end(scoped_ptr<EndNode> e) { end_ = e.Pass(); } | 372 void set_end(scoped_ptr<EndNode> e) { end_ = e.Pass(); } |
| 374 const EndNode* End() const { return end_.get(); } | 373 const EndNode* End() const { return end_.get(); } |
| 375 | 374 |
| 376 void append_item(scoped_ptr<ParseNode> s) { | 375 void append_item(scoped_ptr<ParseNode> s) { |
| 377 contents_.push_back(s.release()); | 376 contents_.push_back(s.release()); |
| 378 } | 377 } |
| 379 const std::vector<const ParseNode*>& contents() const { return contents_; } | 378 const std::vector<const ParseNode*>& contents() const { return contents_; } |
| 380 | 379 |
| 380 void SortAsStringsList(); |
| 381 |
| 381 // During formatting, do we want this list to always be multliline? This is | 382 // During formatting, do we want this list to always be multliline? This is |
| 382 // used to make assignments to deps, sources, etc. always be multiline lists, | 383 // used to make assignments to deps, sources, etc. always be multiline lists, |
| 383 // rather than collapsed to a single line when they're one element. | 384 // rather than collapsed to a single line when they're one element. |
| 384 bool prefer_multiline() const { return prefer_multiline_; } | 385 bool prefer_multiline() const { return prefer_multiline_; } |
| 385 void set_prefer_multiline(bool prefer_multiline) { | 386 void set_prefer_multiline(bool prefer_multiline) { |
| 386 prefer_multiline_ = prefer_multiline; | 387 prefer_multiline_ = prefer_multiline; |
| 387 } | 388 } |
| 388 | 389 |
| 390 struct SortRange { |
| 391 size_t begin; |
| 392 size_t end; |
| 393 SortRange(size_t begin, size_t end) : begin(begin), end(end) {} |
| 394 }; |
| 395 // Only public for testing. |
| 396 std::vector<SortRange> GetSortRanges() const; |
| 397 |
| 389 private: | 398 private: |
| 390 // Tokens corresponding to the [ and ]. The end token is stored in inside an | 399 // Tokens corresponding to the [ and ]. The end token is stored in inside an |
| 391 // custom parse node so that it can have comments hung off of it. | 400 // custom parse node so that it can have comments hung off of it. |
| 392 Token begin_token_; | 401 Token begin_token_; |
| 393 scoped_ptr<EndNode> end_; | 402 scoped_ptr<EndNode> end_; |
| 394 bool prefer_multiline_; | 403 bool prefer_multiline_; |
| 395 | 404 |
| 396 // Owning pointers, use unique_ptr when we can use C++11. | 405 // Owning pointers, use unique_ptr when we can use C++11. |
| 397 std::vector<const ParseNode*> contents_; | 406 std::vector<const ParseNode*> contents_; |
| 398 | 407 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 411 Value Execute(Scope* scope, Err* err) const override; | 420 Value Execute(Scope* scope, Err* err) const override; |
| 412 LocationRange GetRange() const override; | 421 LocationRange GetRange() const override; |
| 413 Err MakeErrorDescribing( | 422 Err MakeErrorDescribing( |
| 414 const std::string& msg, | 423 const std::string& msg, |
| 415 const std::string& help = std::string()) const override; | 424 const std::string& help = std::string()) const override; |
| 416 void Print(std::ostream& out, int indent) const override; | 425 void Print(std::ostream& out, int indent) const override; |
| 417 | 426 |
| 418 const Token& value() const { return value_; } | 427 const Token& value() const { return value_; } |
| 419 void set_value(const Token& t) { value_ = t; } | 428 void set_value(const Token& t) { value_ = t; } |
| 420 | 429 |
| 430 void SetNewLocation(int line_number); |
| 431 |
| 421 private: | 432 private: |
| 422 Token value_; | 433 Token value_; |
| 423 | 434 |
| 424 DISALLOW_COPY_AND_ASSIGN(LiteralNode); | 435 DISALLOW_COPY_AND_ASSIGN(LiteralNode); |
| 425 }; | 436 }; |
| 426 | 437 |
| 427 // UnaryOpNode ----------------------------------------------------------------- | 438 // UnaryOpNode ----------------------------------------------------------------- |
| 428 | 439 |
| 429 class UnaryOpNode : public ParseNode { | 440 class UnaryOpNode : public ParseNode { |
| 430 public: | 441 public: |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 const Token& value() const { return value_; } | 516 const Token& value() const { return value_; } |
| 506 void set_value(const Token& t) { value_ = t; } | 517 void set_value(const Token& t) { value_ = t; } |
| 507 | 518 |
| 508 private: | 519 private: |
| 509 Token value_; | 520 Token value_; |
| 510 | 521 |
| 511 DISALLOW_COPY_AND_ASSIGN(EndNode); | 522 DISALLOW_COPY_AND_ASSIGN(EndNode); |
| 512 }; | 523 }; |
| 513 | 524 |
| 514 #endif // TOOLS_GN_PARSE_TREE_H_ | 525 #endif // TOOLS_GN_PARSE_TREE_H_ |
| OLD | NEW |