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 #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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 // Prints a representation of this node to the given string, indenting | 56 // Prints a representation of this node to the given string, indenting |
57 // by the given number of spaces. | 57 // by the given number of spaces. |
58 virtual void Print(std::ostream& out, int indent) const = 0; | 58 virtual void Print(std::ostream& out, int indent) const = 0; |
59 | 59 |
60 private: | 60 private: |
61 DISALLOW_COPY_AND_ASSIGN(ParseNode); | 61 DISALLOW_COPY_AND_ASSIGN(ParseNode); |
62 }; | 62 }; |
63 | 63 |
64 // AccessorNode ---------------------------------------------------------------- | 64 // AccessorNode ---------------------------------------------------------------- |
65 | 65 |
66 // Access an array element. | 66 // Access an array or scope element. |
67 // | 67 // |
68 // If we need to add support for member variables like "variable.len" I was | 68 // Currently, such values are only read-only. In that you can do: |
69 // thinking this would also handle that case. | 69 // a = obj1.a |
| 70 // b = obj2[0] |
| 71 // But not |
| 72 // obj1.a = 5 |
| 73 // obj2[0] = 6 |
| 74 // |
| 75 // In the current design where the dot operator is used only for templates, we |
| 76 // explicitly don't want to allow you to do "invoker.foo = 5", so if we added |
| 77 // support for accessors to be lvalues, we would also need to add some concept |
| 78 // of a constant scope. Supporting this would also add a lot of complications |
| 79 // to the operator= implementation, since some accessors might return values |
| 80 // in the const root scope that shouldn't be modified. Without a strong |
| 81 // use-case for this, it seems simpler to just disallow it. |
| 82 // |
| 83 // Additionally, the left-hand-side of the accessor must currently be an |
| 84 // identifier. So you can't do things like: |
| 85 // function_call()[1] |
| 86 // a = b.c.d |
| 87 // These are easier to implement if we needed them but given the very limited |
| 88 // use cases for this, it hasn't seemed worth the bother. |
70 class AccessorNode : public ParseNode { | 89 class AccessorNode : public ParseNode { |
71 public: | 90 public: |
72 AccessorNode(); | 91 AccessorNode(); |
73 virtual ~AccessorNode(); | 92 virtual ~AccessorNode(); |
74 | 93 |
75 virtual const AccessorNode* AsAccessor() const OVERRIDE; | 94 virtual const AccessorNode* AsAccessor() const OVERRIDE; |
76 virtual Value Execute(Scope* scope, Err* err) const OVERRIDE; | 95 virtual Value Execute(Scope* scope, Err* err) const OVERRIDE; |
77 virtual LocationRange GetRange() const OVERRIDE; | 96 virtual LocationRange GetRange() const OVERRIDE; |
78 virtual Err MakeErrorDescribing( | 97 virtual Err MakeErrorDescribing( |
79 const std::string& msg, | 98 const std::string& msg, |
80 const std::string& help = std::string()) const OVERRIDE; | 99 const std::string& help = std::string()) const OVERRIDE; |
81 virtual void Print(std::ostream& out, int indent) const OVERRIDE; | 100 virtual void Print(std::ostream& out, int indent) const OVERRIDE; |
82 | 101 |
83 // Base is the thing on the left of the [], currently always required to be | 102 // Base is the thing on the left of the [] or dot, currently always required |
84 // an identifier token. | 103 // to be an identifier token. |
85 const Token& base() const { return base_; } | 104 const Token& base() const { return base_; } |
86 void set_base(const Token& b) { base_ = b; } | 105 void set_base(const Token& b) { base_ = b; } |
87 | 106 |
88 // Index is the expression inside the []. | 107 // Index is the expression inside the []. Will be null if member is set. |
89 const ParseNode* index() const { return index_.get(); } | 108 const ParseNode* index() const { return index_.get(); } |
90 void set_index(scoped_ptr<ParseNode> i) { index_ = i.Pass(); } | 109 void set_index(scoped_ptr<ParseNode> i) { index_ = i.Pass(); } |
91 | 110 |
| 111 // The member is the identifier on the right hand side of the dot. Will be |
| 112 // null if the index is set. |
| 113 const IdentifierNode* member() const { return member_.get(); } |
| 114 void set_member(scoped_ptr<IdentifierNode> i) { member_ = i.Pass(); } |
| 115 |
92 private: | 116 private: |
| 117 Value ExecuteArrayAccess(Scope* scope, Err* err) const; |
| 118 Value ExecuteScopeAccess(Scope* scope, Err* err) const; |
| 119 |
93 Token base_; | 120 Token base_; |
| 121 |
| 122 // Either index or member will be set according to what type of access this |
| 123 // is. |
94 scoped_ptr<ParseNode> index_; | 124 scoped_ptr<ParseNode> index_; |
| 125 scoped_ptr<IdentifierNode> member_; |
95 | 126 |
96 DISALLOW_COPY_AND_ASSIGN(AccessorNode); | 127 DISALLOW_COPY_AND_ASSIGN(AccessorNode); |
97 }; | 128 }; |
98 | 129 |
99 // BinaryOpNode ---------------------------------------------------------------- | 130 // BinaryOpNode ---------------------------------------------------------------- |
100 | 131 |
101 class BinaryOpNode : public ParseNode { | 132 class BinaryOpNode : public ParseNode { |
102 public: | 133 public: |
103 BinaryOpNode(); | 134 BinaryOpNode(); |
104 virtual ~BinaryOpNode(); | 135 virtual ~BinaryOpNode(); |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 } | 388 } |
358 | 389 |
359 private: | 390 private: |
360 Token op_; | 391 Token op_; |
361 scoped_ptr<ParseNode> operand_; | 392 scoped_ptr<ParseNode> operand_; |
362 | 393 |
363 DISALLOW_COPY_AND_ASSIGN(UnaryOpNode); | 394 DISALLOW_COPY_AND_ASSIGN(UnaryOpNode); |
364 }; | 395 }; |
365 | 396 |
366 #endif // TOOLS_GN_PARSE_TREE_H_ | 397 #endif // TOOLS_GN_PARSE_TREE_H_ |
OLD | NEW |