| 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 <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 14 #include "base/macros.h" | 15 #include "base/macros.h" |
| 15 #include "base/memory/scoped_ptr.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 scoped_ptr<ParseNode> (Parser::*PrefixFunc)(Token token); | 20 typedef std::unique_ptr<ParseNode> (Parser::*PrefixFunc)(Token token); |
| 21 typedef scoped_ptr<ParseNode> (Parser::*InfixFunc)(scoped_ptr<ParseNode> left, | 21 typedef std::unique_ptr<ParseNode> ( |
| 22 Token token); | 22 Parser::*InfixFunc)(std::unique_ptr<ParseNode> left, 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 |
| 33 // to the input, so the tokens an the file data they refer to must outlive your | 33 // to the input, so the tokens an the file data they refer to must outlive your |
| 34 // use of the ParseNode. | 34 // use of the ParseNode. |
| 35 class Parser { | 35 class Parser { |
| 36 public: | 36 public: |
| 37 // Will return a null pointer and set the err on error. | 37 // Will return a null pointer and set the err on error. |
| 38 static scoped_ptr<ParseNode> Parse(const std::vector<Token>& tokens, | 38 static std::unique_ptr<ParseNode> Parse(const std::vector<Token>& tokens, |
| 39 Err* err); | 39 Err* err); |
| 40 | 40 |
| 41 // Alternative to parsing that assumes the input is an expression. | 41 // Alternative to parsing that assumes the input is an expression. |
| 42 static scoped_ptr<ParseNode> ParseExpression(const std::vector<Token>& tokens, | 42 static std::unique_ptr<ParseNode> ParseExpression( |
| 43 Err* err); | 43 const std::vector<Token>& tokens, |
| 44 Err* err); |
| 44 | 45 |
| 45 // Alternative to parsing that assumes the input is a literal value. | 46 // Alternative to parsing that assumes the input is a literal value. |
| 46 static scoped_ptr<ParseNode> ParseValue(const std::vector<Token>& tokens, | 47 static std::unique_ptr<ParseNode> ParseValue(const std::vector<Token>& tokens, |
| 47 Err* err); | 48 Err* err); |
| 48 | 49 |
| 49 private: | 50 private: |
| 50 // Vector must be valid for lifetime of call. | 51 // Vector must be valid for lifetime of call. |
| 51 Parser(const std::vector<Token>& tokens, Err* err); | 52 Parser(const std::vector<Token>& tokens, Err* err); |
| 52 ~Parser(); | 53 ~Parser(); |
| 53 | 54 |
| 54 scoped_ptr<ParseNode> ParseExpression(); | 55 std::unique_ptr<ParseNode> ParseExpression(); |
| 55 | 56 |
| 56 // Parses an expression with the given precedence or higher. | 57 // Parses an expression with the given precedence or higher. |
| 57 scoped_ptr<ParseNode> ParseExpression(int precedence); | 58 std::unique_ptr<ParseNode> ParseExpression(int precedence); |
| 58 | 59 |
| 59 // |PrefixFunc|s used in parsing expressions. | 60 // |PrefixFunc|s used in parsing expressions. |
| 60 scoped_ptr<ParseNode> Literal(Token token); | 61 std::unique_ptr<ParseNode> Literal(Token token); |
| 61 scoped_ptr<ParseNode> Name(Token token); | 62 std::unique_ptr<ParseNode> Name(Token token); |
| 62 scoped_ptr<ParseNode> Group(Token token); | 63 std::unique_ptr<ParseNode> Group(Token token); |
| 63 scoped_ptr<ParseNode> Not(Token token); | 64 std::unique_ptr<ParseNode> Not(Token token); |
| 64 scoped_ptr<ParseNode> List(Token token); | 65 std::unique_ptr<ParseNode> List(Token token); |
| 65 scoped_ptr<ParseNode> BlockComment(Token token); | 66 std::unique_ptr<ParseNode> BlockComment(Token token); |
| 66 | 67 |
| 67 // |InfixFunc|s used in parsing expressions. | 68 // |InfixFunc|s used in parsing expressions. |
| 68 scoped_ptr<ParseNode> BinaryOperator(scoped_ptr<ParseNode> left, Token token); | 69 std::unique_ptr<ParseNode> BinaryOperator(std::unique_ptr<ParseNode> left, |
| 69 scoped_ptr<ParseNode> IdentifierOrCall(scoped_ptr<ParseNode> left, | 70 Token token); |
| 71 std::unique_ptr<ParseNode> IdentifierOrCall(std::unique_ptr<ParseNode> left, |
| 72 Token token); |
| 73 std::unique_ptr<ParseNode> Assignment(std::unique_ptr<ParseNode> left, |
| 74 Token token); |
| 75 std::unique_ptr<ParseNode> Subscript(std::unique_ptr<ParseNode> left, |
| 76 Token token); |
| 77 std::unique_ptr<ParseNode> DotOperator(std::unique_ptr<ParseNode> left, |
| 70 Token token); | 78 Token token); |
| 71 scoped_ptr<ParseNode> Assignment(scoped_ptr<ParseNode> left, Token token); | |
| 72 scoped_ptr<ParseNode> Subscript(scoped_ptr<ParseNode> left, Token token); | |
| 73 scoped_ptr<ParseNode> DotOperator(scoped_ptr<ParseNode> left, Token token); | |
| 74 | 79 |
| 75 // Helper to parse a comma separated list, optionally allowing trailing | 80 // Helper to parse a comma separated list, optionally allowing trailing |
| 76 // commas (allowed in [] lists, not in function calls). | 81 // commas (allowed in [] lists, not in function calls). |
| 77 scoped_ptr<ListNode> ParseList(Token start_token, | 82 std::unique_ptr<ListNode> ParseList(Token start_token, |
| 78 Token::Type stop_before, | 83 Token::Type stop_before, |
| 79 bool allow_trailing_comma); | 84 bool allow_trailing_comma); |
| 80 | 85 |
| 81 scoped_ptr<ParseNode> ParseFile(); | 86 std::unique_ptr<ParseNode> ParseFile(); |
| 82 scoped_ptr<ParseNode> ParseStatement(); | 87 std::unique_ptr<ParseNode> ParseStatement(); |
| 83 scoped_ptr<BlockNode> ParseBlock(); | 88 std::unique_ptr<BlockNode> ParseBlock(); |
| 84 scoped_ptr<ParseNode> ParseCondition(); | 89 std::unique_ptr<ParseNode> ParseCondition(); |
| 85 | 90 |
| 86 // Generates a pre- and post-order traversal of the tree. | 91 // Generates a pre- and post-order traversal of the tree. |
| 87 void TraverseOrder(const ParseNode* root, | 92 void TraverseOrder(const ParseNode* root, |
| 88 std::vector<const ParseNode*>* pre, | 93 std::vector<const ParseNode*>* pre, |
| 89 std::vector<const ParseNode*>* post); | 94 std::vector<const ParseNode*>* post); |
| 90 | 95 |
| 91 // Attach comments to nearby syntax. | 96 // Attach comments to nearby syntax. |
| 92 void AssignComments(ParseNode* file); | 97 void AssignComments(ParseNode* file); |
| 93 | 98 |
| 94 bool IsAssignment(const ParseNode* node) const; | 99 bool IsAssignment(const ParseNode* node) const; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 125 FRIEND_TEST_ALL_PREFIXES(Parser, Expression); | 130 FRIEND_TEST_ALL_PREFIXES(Parser, Expression); |
| 126 FRIEND_TEST_ALL_PREFIXES(Parser, FunctionCall); | 131 FRIEND_TEST_ALL_PREFIXES(Parser, FunctionCall); |
| 127 FRIEND_TEST_ALL_PREFIXES(Parser, List); | 132 FRIEND_TEST_ALL_PREFIXES(Parser, List); |
| 128 FRIEND_TEST_ALL_PREFIXES(Parser, ParenExpression); | 133 FRIEND_TEST_ALL_PREFIXES(Parser, ParenExpression); |
| 129 FRIEND_TEST_ALL_PREFIXES(Parser, UnaryOp); | 134 FRIEND_TEST_ALL_PREFIXES(Parser, UnaryOp); |
| 130 | 135 |
| 131 DISALLOW_COPY_AND_ASSIGN(Parser); | 136 DISALLOW_COPY_AND_ASSIGN(Parser); |
| 132 }; | 137 }; |
| 133 | 138 |
| 134 #endif // TOOLS_GN_PARSER_H_ | 139 #endif // TOOLS_GN_PARSER_H_ |
| OLD | NEW |