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
3 // found in the LICENSE file.
5 #ifndef TOOLS_GN_PARSE_TREE_H_
6 #define TOOLS_GN_PARSE_TREE_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "tools/gn/err.h"
14 #include "tools/gn/token.h"
15 #include "tools/gn/value.h"
21 class FunctionCallNode;
28 // ParseNode -------------------------------------------------------------------
36 virtual const AccessorNode* AsAccessor() const;
37 virtual const BinaryOpNode* AsBinaryOp() const;
38 virtual const BlockNode* AsBlock() const;
39 virtual const ConditionNode* AsConditionNode() const;
40 virtual const FunctionCallNode* AsFunctionCall() const;
41 virtual const IdentifierNode* AsIdentifier() const;
42 virtual const ListNode* AsList() const;
43 virtual const LiteralNode* AsLiteral() const;
44 virtual const UnaryOpNode* AsUnaryOp() const;
46 virtual Value Execute(Scope* scope, Err* err) const = 0;
48 virtual LocationRange GetRange() const = 0;
50 // Returns an error with the given messages and the range set to something
51 // that indicates this node.
52 virtual Err MakeErrorDescribing(
53 const std::string& msg,
54 const std::string& help = std::string()) const = 0;
56 // Prints a representation of this node to the given string, indenting
57 // by the given number of spaces.
58 virtual void Print(std::ostream& out, int indent) const = 0;
61 DISALLOW_COPY_AND_ASSIGN(ParseNode);
64 // AccessorNode ----------------------------------------------------------------
66 // Access an array element.
68 // If we need to add support for member variables like "variable.len" I was
69 // thinking this would also handle that case.
70 class AccessorNode : public ParseNode {
73 virtual ~AccessorNode();
75 virtual const AccessorNode* AsAccessor() const OVERRIDE;
76 virtual Value Execute(Scope* scope, Err* err) const OVERRIDE;
77 virtual LocationRange GetRange() const OVERRIDE;
78 virtual Err MakeErrorDescribing(
79 const std::string& msg,
80 const std::string& help = std::string()) const OVERRIDE;
81 virtual void Print(std::ostream& out, int indent) const OVERRIDE;
83 // Base is the thing on the left of the [], currently always required to be
84 // an identifier token.
85 const Token& base() const { return base_; }
86 void set_base(const Token& b) { base_ = b; }
88 // Index is the expression inside the [].
89 const ParseNode* index() const { return index_.get(); }
90 void set_index(scoped_ptr<ParseNode> i) { index_ = i.Pass(); }
94 scoped_ptr<ParseNode> index_;
96 DISALLOW_COPY_AND_ASSIGN(AccessorNode);
99 // BinaryOpNode ----------------------------------------------------------------
101 class BinaryOpNode : public ParseNode {
104 virtual ~BinaryOpNode();
106 virtual const BinaryOpNode* AsBinaryOp() const OVERRIDE;
107 virtual Value Execute(Scope* scope, Err* err) const OVERRIDE;
108 virtual LocationRange GetRange() const OVERRIDE;
109 virtual Err MakeErrorDescribing(
110 const std::string& msg,
111 const std::string& help = std::string()) const OVERRIDE;
112 virtual void Print(std::ostream& out, int indent) const OVERRIDE;
114 const Token& op() const { return op_; }
115 void set_op(const Token& t) { op_ = t; }
117 const ParseNode* left() const { return left_.get(); }
118 void set_left(scoped_ptr<ParseNode> left) {
122 const ParseNode* right() const { return right_.get(); }
123 void set_right(scoped_ptr<ParseNode> right) {
124 right_ = right.Pass();
128 scoped_ptr<ParseNode> left_;
130 scoped_ptr<ParseNode> right_;
132 DISALLOW_COPY_AND_ASSIGN(BinaryOpNode);
135 // BlockNode -------------------------------------------------------------------
137 class BlockNode : public ParseNode {
139 // Set has_scope if this block introduces a nested scope.
140 explicit BlockNode(bool has_scope);
141 virtual ~BlockNode();
143 virtual const BlockNode* AsBlock() const OVERRIDE;
144 virtual Value Execute(Scope* scope, Err* err) const OVERRIDE;
145 virtual LocationRange GetRange() const OVERRIDE;
146 virtual Err MakeErrorDescribing(
147 const std::string& msg,
148 const std::string& help = std::string()) const OVERRIDE;
149 virtual void Print(std::ostream& out, int indent) const OVERRIDE;
151 void set_begin_token(const Token& t) { begin_token_ = t; }
152 void set_end_token(const Token& t) { end_token_ = t; }
154 const std::vector<ParseNode*>& statements() const { return statements_; }
155 void append_statement(scoped_ptr<ParseNode> s) {
156 statements_.push_back(s.release());
159 // Doesn't create a nested scope.
160 Value ExecuteBlockInScope(Scope* our_scope, Err* err) const;
165 // Tokens corresponding to { and }, if any (may be NULL).
169 // Owning pointers, use unique_ptr when we can use C++11.
170 std::vector<ParseNode*> statements_;
172 DISALLOW_COPY_AND_ASSIGN(BlockNode);
175 // ConditionNode ---------------------------------------------------------------
177 class ConditionNode : public ParseNode {
180 virtual ~ConditionNode();
182 virtual const ConditionNode* AsConditionNode() const OVERRIDE;
183 virtual Value Execute(Scope* scope, Err* err) const OVERRIDE;
184 virtual LocationRange GetRange() const OVERRIDE;
185 virtual Err MakeErrorDescribing(
186 const std::string& msg,
187 const std::string& help = std::string()) const OVERRIDE;
188 virtual void Print(std::ostream& out, int indent) const OVERRIDE;
190 void set_if_token(const Token& token) { if_token_ = token; }
192 const ParseNode* condition() const { return condition_.get(); }
193 void set_condition(scoped_ptr<ParseNode> c) {
194 condition_ = c.Pass();
197 const BlockNode* if_true() const { return if_true_.get(); }
198 void set_if_true(scoped_ptr<BlockNode> t) {
202 // This is either empty, a block (for the else clause), or another
204 const ParseNode* if_false() const { return if_false_.get(); }
205 void set_if_false(scoped_ptr<ParseNode> f) {
206 if_false_ = f.Pass();
210 // Token corresponding to the "if" string.
213 scoped_ptr<ParseNode> condition_; // Always non-null.
214 scoped_ptr<BlockNode> if_true_; // Always non-null.
215 scoped_ptr<ParseNode> if_false_; // May be null.
217 DISALLOW_COPY_AND_ASSIGN(ConditionNode);
220 // FunctionCallNode ------------------------------------------------------------
222 class FunctionCallNode : public ParseNode {
225 virtual ~FunctionCallNode();
227 virtual const FunctionCallNode* AsFunctionCall() const OVERRIDE;
228 virtual Value Execute(Scope* scope, Err* err) const OVERRIDE;
229 virtual LocationRange GetRange() const OVERRIDE;
230 virtual Err MakeErrorDescribing(
231 const std::string& msg,
232 const std::string& help = std::string()) const OVERRIDE;
233 virtual void Print(std::ostream& out, int indent) const OVERRIDE;
235 const Token& function() const { return function_; }
236 void set_function(Token t) { function_ = t; }
238 const ListNode* args() const { return args_.get(); }
239 void set_args(scoped_ptr<ListNode> a) { args_ = a.Pass(); }
241 const BlockNode* block() const { return block_.get(); }
242 void set_block(scoped_ptr<BlockNode> b) { block_ = b.Pass(); }
246 scoped_ptr<ListNode> args_;
247 scoped_ptr<BlockNode> block_; // May be null.
249 DISALLOW_COPY_AND_ASSIGN(FunctionCallNode);
252 // IdentifierNode --------------------------------------------------------------
254 class IdentifierNode : public ParseNode {
257 IdentifierNode(const Token& token);
258 virtual ~IdentifierNode();
260 virtual const IdentifierNode* AsIdentifier() const OVERRIDE;
261 virtual Value Execute(Scope* scope, Err* err) const OVERRIDE;
262 virtual LocationRange GetRange() const OVERRIDE;
263 virtual Err MakeErrorDescribing(
264 const std::string& msg,
265 const std::string& help = std::string()) const OVERRIDE;
266 virtual void Print(std::ostream& out, int indent) const OVERRIDE;
268 const Token& value() const { return value_; }
269 void set_value(const Token& t) { value_ = t; }
274 DISALLOW_COPY_AND_ASSIGN(IdentifierNode);
277 // ListNode --------------------------------------------------------------------
279 class ListNode : public ParseNode {
284 virtual const ListNode* AsList() const OVERRIDE;
285 virtual Value Execute(Scope* scope, Err* err) const OVERRIDE;
286 virtual LocationRange GetRange() const OVERRIDE;
287 virtual Err MakeErrorDescribing(
288 const std::string& msg,
289 const std::string& help = std::string()) const OVERRIDE;
290 virtual void Print(std::ostream& out, int indent) const OVERRIDE;
292 void set_begin_token(const Token& t) { begin_token_ = t; }
293 void set_end_token(const Token& t) { end_token_ = t; }
295 void append_item(scoped_ptr<ParseNode> s) {
296 contents_.push_back(s.release());
298 const std::vector<const ParseNode*>& contents() const { return contents_; }
301 // Tokens corresponding to the [ and ].
305 // Owning pointers, use unique_ptr when we can use C++11.
306 std::vector<const ParseNode*> contents_;
308 DISALLOW_COPY_AND_ASSIGN(ListNode);
311 // LiteralNode -----------------------------------------------------------------
313 class LiteralNode : public ParseNode {
316 LiteralNode(const Token& token);
317 virtual ~LiteralNode();
319 virtual const LiteralNode* AsLiteral() const OVERRIDE;
320 virtual Value Execute(Scope* scope, Err* err) const OVERRIDE;
321 virtual LocationRange GetRange() const OVERRIDE;
322 virtual Err MakeErrorDescribing(
323 const std::string& msg,
324 const std::string& help = std::string()) const OVERRIDE;
325 virtual void Print(std::ostream& out, int indent) const OVERRIDE;
327 const Token& value() const { return value_; }
328 void set_value(const Token& t) { value_ = t; }
333 DISALLOW_COPY_AND_ASSIGN(LiteralNode);
336 // UnaryOpNode -----------------------------------------------------------------
338 class UnaryOpNode : public ParseNode {
341 virtual ~UnaryOpNode();
343 virtual const UnaryOpNode* AsUnaryOp() const OVERRIDE;
344 virtual Value Execute(Scope* scope, Err* err) const OVERRIDE;
345 virtual LocationRange GetRange() const OVERRIDE;
346 virtual Err MakeErrorDescribing(
347 const std::string& msg,
348 const std::string& help = std::string()) const OVERRIDE;
349 virtual void Print(std::ostream& out, int indent) const OVERRIDE;
351 const Token& op() const { return op_; }
352 void set_op(const Token& t) { op_ = t; }
354 const ParseNode* operand() const { return operand_.get(); }
355 void set_operand(scoped_ptr<ParseNode> operand) {
356 operand_ = operand.Pass();
361 scoped_ptr<ParseNode> operand_;
363 DISALLOW_COPY_AND_ASSIGN(UnaryOpNode);
366 #endif // TOOLS_GN_PARSE_TREE_H_