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_PARSER_H_
6 #define TOOLS_GN_PARSER_H_
11 #include "base/basictypes.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "tools/gn/err.h"
15 #include "tools/gn/parse_tree.h"
18 typedef scoped_ptr
<ParseNode
> (Parser::*PrefixFunc
)(Token token
);
19 typedef scoped_ptr
<ParseNode
> (Parser::*InfixFunc
)(scoped_ptr
<ParseNode
> left
,
22 extern const char kGrammar_Help
[];
30 // Parses a series of tokens. The resulting AST will refer to the tokens passed
31 // to the input, so the tokens an the file data they refer to must outlive your
32 // use of the ParseNode.
35 // Will return a null pointer and set the err on error.
36 static scoped_ptr
<ParseNode
> Parse(const std::vector
<Token
>& tokens
,
39 // Alternative to parsing that assumes the input is an expression.
40 static scoped_ptr
<ParseNode
> ParseExpression(const std::vector
<Token
>& tokens
,
43 scoped_ptr
<ParseNode
> ParseExpression();
46 // Vector must be valid for lifetime of call.
47 Parser(const std::vector
<Token
>& tokens
, Err
* err
);
50 // Parses an expression with the given precedence or higher.
51 scoped_ptr
<ParseNode
> ParseExpression(int precedence
);
53 // |PrefixFunc|s used in parsing expressions.
54 scoped_ptr
<ParseNode
> Literal(Token token
);
55 scoped_ptr
<ParseNode
> Name(Token token
);
56 scoped_ptr
<ParseNode
> Group(Token token
);
57 scoped_ptr
<ParseNode
> Not(Token token
);
58 scoped_ptr
<ParseNode
> List(Token token
);
59 scoped_ptr
<ParseNode
> BlockComment(Token token
);
61 // |InfixFunc|s used in parsing expressions.
62 scoped_ptr
<ParseNode
> BinaryOperator(scoped_ptr
<ParseNode
> left
, Token token
);
63 scoped_ptr
<ParseNode
> IdentifierOrCall(scoped_ptr
<ParseNode
> left
,
65 scoped_ptr
<ParseNode
> Assignment(scoped_ptr
<ParseNode
> left
, Token token
);
66 scoped_ptr
<ParseNode
> Subscript(scoped_ptr
<ParseNode
> left
, Token token
);
67 scoped_ptr
<ParseNode
> DotOperator(scoped_ptr
<ParseNode
> left
, Token token
);
69 // Helper to parse a comma separated list, optionally allowing trailing
70 // commas (allowed in [] lists, not in function calls).
71 scoped_ptr
<ListNode
> ParseList(Token start_token
,
72 Token::Type stop_before
,
73 bool allow_trailing_comma
);
75 scoped_ptr
<ParseNode
> ParseFile();
76 scoped_ptr
<ParseNode
> ParseStatement();
77 scoped_ptr
<BlockNode
> ParseBlock();
78 scoped_ptr
<ParseNode
> ParseCondition();
80 // Generates a pre- and post-order traversal of the tree.
81 void TraverseOrder(const ParseNode
* root
,
82 std::vector
<const ParseNode
*>* pre
,
83 std::vector
<const ParseNode
*>* post
);
85 // Attach comments to nearby syntax.
86 void AssignComments(ParseNode
* file
);
88 bool IsAssignment(const ParseNode
* node
) const;
89 bool IsStatementBreak(Token::Type token_type
) const;
91 bool LookAhead(Token::Type type
);
92 bool Match(Token::Type type
);
93 Token
Consume(Token::Type type
, const char* error_message
);
94 Token
Consume(Token::Type
* types
,
96 const char* error_message
);
99 const Token
& cur_token() const { return tokens_
[cur_
]; }
101 bool done() const { return at_end() || has_error(); }
102 bool at_end() const { return cur_
>= tokens_
.size(); }
103 bool has_error() const { return err_
->has_error(); }
105 std::vector
<Token
> tokens_
;
106 std::vector
<Token
> line_comment_tokens_
;
107 std::vector
<Token
> suffix_comment_tokens_
;
109 static ParserHelper expressions_
[Token::NUM_TYPES
];
113 // Current index into the tokens.
116 FRIEND_TEST_ALL_PREFIXES(Parser
, BinaryOp
);
117 FRIEND_TEST_ALL_PREFIXES(Parser
, Block
);
118 FRIEND_TEST_ALL_PREFIXES(Parser
, Condition
);
119 FRIEND_TEST_ALL_PREFIXES(Parser
, Expression
);
120 FRIEND_TEST_ALL_PREFIXES(Parser
, FunctionCall
);
121 FRIEND_TEST_ALL_PREFIXES(Parser
, List
);
122 FRIEND_TEST_ALL_PREFIXES(Parser
, ParenExpression
);
123 FRIEND_TEST_ALL_PREFIXES(Parser
, UnaryOp
);
125 DISALLOW_COPY_AND_ASSIGN(Parser
);
128 #endif // TOOLS_GN_PARSER_H_