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_PARSER_H_ | 5 #ifndef TOOLS_GN_PARSER_H_ |
6 #define TOOLS_GN_PARSER_H_ | 6 #define TOOLS_GN_PARSER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <memory> | 11 #include <memory> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "tools/gn/err.h" | 16 #include "tools/gn/err.h" |
17 #include "tools/gn/parse_tree.h" | 17 #include "tools/gn/parse_tree.h" |
18 | 18 |
19 class Parser; | 19 class Parser; |
20 typedef std::unique_ptr<ParseNode> (Parser::*PrefixFunc)(Token token); | 20 typedef std::unique_ptr<ParseNode> (Parser::*PrefixFunc)(const Token& token); |
21 typedef std::unique_ptr<ParseNode> ( | 21 typedef std::unique_ptr<ParseNode> ( |
22 Parser::*InfixFunc)(std::unique_ptr<ParseNode> left, Token token); | 22 Parser::*InfixFunc)(std::unique_ptr<ParseNode> left, const Token& token); |
23 | 23 |
24 extern const char kGrammar_Help[]; | 24 extern const char kGrammar_Help[]; |
25 | 25 |
26 struct ParserHelper { | 26 struct ParserHelper { |
27 PrefixFunc prefix; | 27 PrefixFunc prefix; |
28 InfixFunc infix; | 28 InfixFunc infix; |
29 int precedence; | 29 int precedence; |
30 }; | 30 }; |
31 | 31 |
32 // Parses a series of tokens. The resulting AST will refer to the tokens passed | 32 // Parses a series of tokens. The resulting AST will refer to the tokens passed |
(...skipping 18 matching lines...) Expand all Loading... |
51 // Vector must be valid for lifetime of call. | 51 // Vector must be valid for lifetime of call. |
52 Parser(const std::vector<Token>& tokens, Err* err); | 52 Parser(const std::vector<Token>& tokens, Err* err); |
53 ~Parser(); | 53 ~Parser(); |
54 | 54 |
55 std::unique_ptr<ParseNode> ParseExpression(); | 55 std::unique_ptr<ParseNode> ParseExpression(); |
56 | 56 |
57 // Parses an expression with the given precedence or higher. | 57 // Parses an expression with the given precedence or higher. |
58 std::unique_ptr<ParseNode> ParseExpression(int precedence); | 58 std::unique_ptr<ParseNode> ParseExpression(int precedence); |
59 | 59 |
60 // |PrefixFunc|s used in parsing expressions. | 60 // |PrefixFunc|s used in parsing expressions. |
61 std::unique_ptr<ParseNode> Block(Token token); | 61 std::unique_ptr<ParseNode> Block(const Token& token); |
62 std::unique_ptr<ParseNode> Literal(Token token); | 62 std::unique_ptr<ParseNode> Literal(const Token& token); |
63 std::unique_ptr<ParseNode> Name(Token token); | 63 std::unique_ptr<ParseNode> Name(const Token& token); |
64 std::unique_ptr<ParseNode> Group(Token token); | 64 std::unique_ptr<ParseNode> Group(const Token& token); |
65 std::unique_ptr<ParseNode> Not(Token token); | 65 std::unique_ptr<ParseNode> Not(const Token& token); |
66 std::unique_ptr<ParseNode> List(Token token); | 66 std::unique_ptr<ParseNode> List(const Token& token); |
67 std::unique_ptr<ParseNode> BlockComment(Token token); | 67 std::unique_ptr<ParseNode> BlockComment(const Token& token); |
68 | 68 |
69 // |InfixFunc|s used in parsing expressions. | 69 // |InfixFunc|s used in parsing expressions. |
70 std::unique_ptr<ParseNode> BinaryOperator(std::unique_ptr<ParseNode> left, | 70 std::unique_ptr<ParseNode> BinaryOperator(std::unique_ptr<ParseNode> left, |
71 Token token); | 71 const Token& token); |
72 std::unique_ptr<ParseNode> IdentifierOrCall(std::unique_ptr<ParseNode> left, | 72 std::unique_ptr<ParseNode> IdentifierOrCall(std::unique_ptr<ParseNode> left, |
73 Token token); | 73 const Token& token); |
74 std::unique_ptr<ParseNode> Assignment(std::unique_ptr<ParseNode> left, | 74 std::unique_ptr<ParseNode> Assignment(std::unique_ptr<ParseNode> left, |
75 Token token); | 75 const Token& token); |
76 std::unique_ptr<ParseNode> Subscript(std::unique_ptr<ParseNode> left, | 76 std::unique_ptr<ParseNode> Subscript(std::unique_ptr<ParseNode> left, |
77 Token token); | 77 const Token& token); |
78 std::unique_ptr<ParseNode> DotOperator(std::unique_ptr<ParseNode> left, | 78 std::unique_ptr<ParseNode> DotOperator(std::unique_ptr<ParseNode> left, |
79 Token token); | 79 const Token& token); |
80 | 80 |
81 // Helper to parse a comma separated list, optionally allowing trailing | 81 // Helper to parse a comma separated list, optionally allowing trailing |
82 // commas (allowed in [] lists, not in function calls). | 82 // commas (allowed in [] lists, not in function calls). |
83 std::unique_ptr<ListNode> ParseList(Token start_token, | 83 std::unique_ptr<ListNode> ParseList(const Token& start_token, |
84 Token::Type stop_before, | 84 Token::Type stop_before, |
85 bool allow_trailing_comma); | 85 bool allow_trailing_comma); |
86 | 86 |
87 std::unique_ptr<ParseNode> ParseFile(); | 87 std::unique_ptr<ParseNode> ParseFile(); |
88 std::unique_ptr<ParseNode> ParseStatement(); | 88 std::unique_ptr<ParseNode> ParseStatement(); |
89 // Expects to be passed the token corresponding to the '{' and that the | 89 // Expects to be passed the token corresponding to the '{' and that the |
90 // current token is the one following the '{'. | 90 // current token is the one following the '{'. |
91 std::unique_ptr<BlockNode> ParseBlock(Token being_brace, | 91 std::unique_ptr<BlockNode> ParseBlock(const Token& begin_brace, |
92 BlockNode::ResultMode result_mode); | 92 BlockNode::ResultMode result_mode); |
93 std::unique_ptr<ParseNode> ParseCondition(); | 93 std::unique_ptr<ParseNode> ParseCondition(); |
94 | 94 |
95 // Generates a pre- and post-order traversal of the tree. | 95 // Generates a pre- and post-order traversal of the tree. |
96 void TraverseOrder(const ParseNode* root, | 96 void TraverseOrder(const ParseNode* root, |
97 std::vector<const ParseNode*>* pre, | 97 std::vector<const ParseNode*>* pre, |
98 std::vector<const ParseNode*>* post); | 98 std::vector<const ParseNode*>* post); |
99 | 99 |
100 // Attach comments to nearby syntax. | 100 // Attach comments to nearby syntax. |
101 void AssignComments(ParseNode* file); | 101 void AssignComments(ParseNode* file); |
102 | 102 |
103 bool IsAssignment(const ParseNode* node) const; | 103 bool IsAssignment(const ParseNode* node) const; |
104 bool IsStatementBreak(Token::Type token_type) const; | 104 bool IsStatementBreak(Token::Type token_type) const; |
105 | 105 |
106 bool LookAhead(Token::Type type); | 106 bool LookAhead(Token::Type type); |
107 bool Match(Token::Type type); | 107 bool Match(Token::Type type); |
108 Token Consume(Token::Type type, const char* error_message); | 108 const Token& Consume(Token::Type type, const char* error_message); |
109 Token Consume(Token::Type* types, | 109 const Token& Consume(Token::Type* types, |
110 size_t num_types, | 110 size_t num_types, |
111 const char* error_message); | 111 const char* error_message); |
112 Token Consume(); | 112 const Token& Consume(); |
113 | 113 |
114 // Call this only if !at_end(). | 114 // Call this only if !at_end(). |
115 const Token& cur_token() const { return tokens_[cur_]; } | 115 const Token& cur_token() const { return tokens_[cur_]; } |
116 | 116 |
117 const Token& cur_or_last_token() const { | 117 const Token& cur_or_last_token() const { |
118 return at_end() ? tokens_[tokens_.size() - 1] : cur_token(); | 118 return at_end() ? tokens_[tokens_.size() - 1] : cur_token(); |
119 } | 119 } |
120 | 120 |
121 bool done() const { return at_end() || has_error(); } | 121 bool done() const { return at_end() || has_error(); } |
122 bool at_end() const { return cur_ >= tokens_.size(); } | 122 bool at_end() const { return cur_ >= tokens_.size(); } |
123 bool has_error() const { return err_->has_error(); } | 123 bool has_error() const { return err_->has_error(); } |
124 | 124 |
125 std::vector<Token> tokens_; | 125 std::vector<Token> tokens_; |
126 std::vector<Token> line_comment_tokens_; | 126 std::vector<Token> line_comment_tokens_; |
127 std::vector<Token> suffix_comment_tokens_; | 127 std::vector<Token> suffix_comment_tokens_; |
128 | 128 |
129 static ParserHelper expressions_[Token::NUM_TYPES]; | 129 static ParserHelper expressions_[Token::NUM_TYPES]; |
130 | 130 |
| 131 Token invalid_token_; |
131 Err* err_; | 132 Err* err_; |
132 | 133 |
133 // Current index into the tokens. | 134 // Current index into the tokens. |
134 size_t cur_; | 135 size_t cur_; |
135 | 136 |
136 FRIEND_TEST_ALL_PREFIXES(Parser, BinaryOp); | 137 FRIEND_TEST_ALL_PREFIXES(Parser, BinaryOp); |
137 FRIEND_TEST_ALL_PREFIXES(Parser, Block); | 138 FRIEND_TEST_ALL_PREFIXES(Parser, Block); |
138 FRIEND_TEST_ALL_PREFIXES(Parser, Condition); | 139 FRIEND_TEST_ALL_PREFIXES(Parser, Condition); |
139 FRIEND_TEST_ALL_PREFIXES(Parser, Expression); | 140 FRIEND_TEST_ALL_PREFIXES(Parser, Expression); |
140 FRIEND_TEST_ALL_PREFIXES(Parser, FunctionCall); | 141 FRIEND_TEST_ALL_PREFIXES(Parser, FunctionCall); |
141 FRIEND_TEST_ALL_PREFIXES(Parser, List); | 142 FRIEND_TEST_ALL_PREFIXES(Parser, List); |
142 FRIEND_TEST_ALL_PREFIXES(Parser, ParenExpression); | 143 FRIEND_TEST_ALL_PREFIXES(Parser, ParenExpression); |
143 FRIEND_TEST_ALL_PREFIXES(Parser, UnaryOp); | 144 FRIEND_TEST_ALL_PREFIXES(Parser, UnaryOp); |
144 | 145 |
145 DISALLOW_COPY_AND_ASSIGN(Parser); | 146 DISALLOW_COPY_AND_ASSIGN(Parser); |
146 }; | 147 }; |
147 | 148 |
148 #endif // TOOLS_GN_PARSER_H_ | 149 #endif // TOOLS_GN_PARSER_H_ |
OLD | NEW |