Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / tools / gn / parser.h
blob3348e2d225505033af347634c9befbd73d8f416e
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_
8 #include <map>
9 #include <vector>
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"
17 class Parser;
18 typedef scoped_ptr<ParseNode> (Parser::*PrefixFunc)(Token token);
19 typedef scoped_ptr<ParseNode> (Parser::*InfixFunc)(scoped_ptr<ParseNode> left,
20 Token token);
22 extern const char kGrammar_Help[];
24 struct ParserHelper {
25 PrefixFunc prefix;
26 InfixFunc infix;
27 int precedence;
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.
33 class Parser {
34 public:
35 // Will return a null pointer and set the err on error.
36 static scoped_ptr<ParseNode> Parse(const std::vector<Token>& tokens,
37 Err* err);
39 // Alternative to parsing that assumes the input is an expression.
40 static scoped_ptr<ParseNode> ParseExpression(const std::vector<Token>& tokens,
41 Err* err);
43 // Alternative to parsing that assumes the input is a literal value.
44 static scoped_ptr<ParseNode> ParseValue(const std::vector<Token>& tokens,
45 Err* err);
47 private:
48 // Vector must be valid for lifetime of call.
49 Parser(const std::vector<Token>& tokens, Err* err);
50 ~Parser();
52 scoped_ptr<ParseNode> ParseExpression();
54 // Parses an expression with the given precedence or higher.
55 scoped_ptr<ParseNode> ParseExpression(int precedence);
57 // |PrefixFunc|s used in parsing expressions.
58 scoped_ptr<ParseNode> Literal(Token token);
59 scoped_ptr<ParseNode> Name(Token token);
60 scoped_ptr<ParseNode> Group(Token token);
61 scoped_ptr<ParseNode> Not(Token token);
62 scoped_ptr<ParseNode> List(Token token);
63 scoped_ptr<ParseNode> BlockComment(Token token);
65 // |InfixFunc|s used in parsing expressions.
66 scoped_ptr<ParseNode> BinaryOperator(scoped_ptr<ParseNode> left, Token token);
67 scoped_ptr<ParseNode> IdentifierOrCall(scoped_ptr<ParseNode> left,
68 Token token);
69 scoped_ptr<ParseNode> Assignment(scoped_ptr<ParseNode> left, Token token);
70 scoped_ptr<ParseNode> Subscript(scoped_ptr<ParseNode> left, Token token);
71 scoped_ptr<ParseNode> DotOperator(scoped_ptr<ParseNode> left, Token token);
73 // Helper to parse a comma separated list, optionally allowing trailing
74 // commas (allowed in [] lists, not in function calls).
75 scoped_ptr<ListNode> ParseList(Token start_token,
76 Token::Type stop_before,
77 bool allow_trailing_comma);
79 scoped_ptr<ParseNode> ParseFile();
80 scoped_ptr<ParseNode> ParseStatement();
81 scoped_ptr<BlockNode> ParseBlock();
82 scoped_ptr<ParseNode> ParseCondition();
84 // Generates a pre- and post-order traversal of the tree.
85 void TraverseOrder(const ParseNode* root,
86 std::vector<const ParseNode*>* pre,
87 std::vector<const ParseNode*>* post);
89 // Attach comments to nearby syntax.
90 void AssignComments(ParseNode* file);
92 bool IsAssignment(const ParseNode* node) const;
93 bool IsStatementBreak(Token::Type token_type) const;
95 bool LookAhead(Token::Type type);
96 bool Match(Token::Type type);
97 Token Consume(Token::Type type, const char* error_message);
98 Token Consume(Token::Type* types,
99 size_t num_types,
100 const char* error_message);
101 Token Consume();
103 const Token& cur_token() const { return tokens_[cur_]; }
105 bool done() const { return at_end() || has_error(); }
106 bool at_end() const { return cur_ >= tokens_.size(); }
107 bool has_error() const { return err_->has_error(); }
109 std::vector<Token> tokens_;
110 std::vector<Token> line_comment_tokens_;
111 std::vector<Token> suffix_comment_tokens_;
113 static ParserHelper expressions_[Token::NUM_TYPES];
115 Err* err_;
117 // Current index into the tokens.
118 size_t cur_;
120 FRIEND_TEST_ALL_PREFIXES(Parser, BinaryOp);
121 FRIEND_TEST_ALL_PREFIXES(Parser, Block);
122 FRIEND_TEST_ALL_PREFIXES(Parser, Condition);
123 FRIEND_TEST_ALL_PREFIXES(Parser, Expression);
124 FRIEND_TEST_ALL_PREFIXES(Parser, FunctionCall);
125 FRIEND_TEST_ALL_PREFIXES(Parser, List);
126 FRIEND_TEST_ALL_PREFIXES(Parser, ParenExpression);
127 FRIEND_TEST_ALL_PREFIXES(Parser, UnaryOp);
129 DISALLOW_COPY_AND_ASSIGN(Parser);
132 #endif // TOOLS_GN_PARSER_H_