Chromium Code Reviews| 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 #include "tools/gn/parser.h" | 5 #include "tools/gn/parser.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "tools/gn/functions.h" | 8 #include "tools/gn/functions.h" |
| 9 #include "tools/gn/operators.h" | 9 #include "tools/gn/operators.h" |
| 10 #include "tools/gn/token.h" | 10 #include "tools/gn/token.h" |
| 11 | 11 |
| 12 // grammar: | 12 const char kGrammar_Help[] = |
| 13 // | 13 "GN build language grammar\n" |
| 14 // file := (statement)* | 14 "\n" |
| 15 // statement := block | if | assignment | 15 " This help topic defines the grammar for the GN build language.\n" |
|
brettw
2015/03/26 03:10:16
This line seems unnecessary.
| |
| 16 // block := '{' statement* '}' | 16 "\n" |
| 17 // if := 'if' '(' expr ')' statement [ else ] | 17 "Tokens\n" |
| 18 // else := 'else' (if | statement)* | 18 "\n" |
| 19 // assignment := ident {'=' | '+=' | '-='} expr | 19 " GN build files are read as sequences of tokens. While splitting the\n" |
| 20 " file into tokens, the next token is the longest sequence of characters\n" | |
| 21 " that form a valid token.\n" | |
| 22 "\n" | |
| 23 "White space and comments\n" | |
| 24 "\n" | |
| 25 " White space is comprised of spaces (U+0020), horizontal tabs (U+0009),\n" | |
| 26 " carriage returns (U+000D), and newlines (U+000A).\n" | |
| 27 "\n" | |
| 28 " Comments start at the character \"#\" and stop at the next newline.\n" | |
| 29 "\n" | |
| 30 " White space and comments are ignored except that they may separate\n" | |
| 31 " tokens that would otherwise combine into a single token.\n" | |
| 32 "\n" | |
| 33 "Identifiers\n" | |
| 34 "\n" | |
| 35 " Identifiers name variables and functions.\n" | |
| 36 "\n" | |
| 37 " identifier = letter { letter | digit } .\n" | |
| 38 " letter = \"A\" ... \"Z\" | \"a\" ... \"z\" | \"_\" .\n" | |
| 39 " digit = \"0\" ... \"9\" .\n" | |
| 40 "\n" | |
| 41 "Keywords\n" | |
| 42 "\n" | |
| 43 " The following keywords are reserved and may not be used as\n" | |
| 44 " identifiers:\n" | |
| 45 "\n" | |
| 46 " else false if true\n" | |
| 47 "\n" | |
| 48 "Integer literals\n" | |
| 49 "\n" | |
| 50 " An integer literal represents a decimal integer value.\n" | |
| 51 "\n" | |
| 52 " integer = [ \"-\" ] digit { digit } .\n" | |
| 53 "\n" | |
| 54 "String literals\n" | |
| 55 "\n" | |
| 56 " A string literal represents a string value consisting of the quoted\n" | |
| 57 " characters with possible escape sequences and variable expansions.\n" | |
| 58 "\n" | |
| 59 " string = `\"` { char | escape | expansion } `\"` .\n" | |
| 60 " escape = `\\` ( \"$\" | `\"` | char ) .\n" | |
| 61 " expansion = \"$\" ( identifier | \"{\" identifier \"}\" ) .\n" | |
| 62 " char = /* any character except \"$\", `\"`, or newline */ .\n" | |
| 63 "\n" | |
| 64 " After a backslash, certain sequences represent special characters:\n" | |
| 65 "\n" | |
| 66 " \\\" U+0022 quotation mark\n" | |
| 67 " \\$ U+0024 dollar sign\n" | |
| 68 " \\\\ U+005C backslash\n" | |
| 69 "\n" | |
| 70 " All other backslashes represent themselves.\n" | |
| 71 "\n" | |
| 72 "Punctuation\n" | |
| 73 "\n" | |
| 74 " The following character sequences represent punctuation:\n" | |
| 75 "\n" | |
| 76 " + += == != ( )\n" | |
| 77 " - -= < <= [ ]\n" | |
| 78 " ! = > >= { }\n" | |
| 79 " && || . ,\n" | |
| 80 "\n" | |
| 81 "Grammar\n" | |
| 82 "\n" | |
| 83 " The input tokens form a syntax tree following a context-free grammar:\n" | |
| 84 "\n" | |
| 85 " File = StatementList .\n" | |
| 86 "\n" | |
| 87 " Statement = Assignment | Call | Condition .\n" | |
| 88 " Assignment = identifier AssignOp Expr .\n" | |
| 89 " Call = identifier \"(\" [ ExprList ] \")\" [ Block ] .\n" | |
| 90 " Condition = \"if\" \"(\" Expr \")\" Block\n" | |
| 91 " [ \"else\" ( Condition | Block ) ] .\n" | |
| 92 " Block = \"{\" StatementList \"}\" .\n" | |
| 93 " StatementList = { Statement } .\n" | |
| 94 "\n" | |
| 95 " Expr = UnaryExpr | Expr BinaryOp Expr .\n" | |
| 96 " UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr .\n" | |
| 97 " PrimaryExpr = identifier | integer | string | Call\n" | |
| 98 " | identifier \"[\" Expr \"]\"\n" | |
| 99 " | identifier \".\" identifier\n" | |
| 100 " | \"(\" Expr \")\"\n" | |
| 101 " | \"[\" [ ExprList [ \",\" ] ] \"]\" .\n" | |
| 102 " ExprList = Expr { \",\" Expr } .\n" | |
| 103 "\n" | |
| 104 " AssignOp = \"=\" | \"+=\" | \"-=\" .\n" | |
| 105 " UnaryOp = \"!\" .\n" | |
| 106 " BinaryOp = \"+\" | \"-\" // highest priority\n" | |
| 107 " | \"<\" | \"<=\" | \">\" | \">=\"\n" | |
| 108 " | \"==\" | \"!=\"\n" | |
| 109 " | \"&&\"\n" | |
| 110 " | \"||\" . // lowest priority\n" | |
| 111 "\n" | |
| 112 " All binary operators are left-associative.\n"; | |
| 20 | 113 |
| 21 enum Precedence { | 114 enum Precedence { |
| 22 PRECEDENCE_ASSIGNMENT = 1, // Lowest precedence. | 115 PRECEDENCE_ASSIGNMENT = 1, // Lowest precedence. |
| 23 PRECEDENCE_OR = 2, | 116 PRECEDENCE_OR = 2, |
| 24 PRECEDENCE_AND = 3, | 117 PRECEDENCE_AND = 3, |
| 25 PRECEDENCE_EQUALITY = 4, | 118 PRECEDENCE_EQUALITY = 4, |
| 26 PRECEDENCE_RELATION = 5, | 119 PRECEDENCE_RELATION = 5, |
| 27 PRECEDENCE_SUM = 6, | 120 PRECEDENCE_SUM = 6, |
| 28 PRECEDENCE_PREFIX = 7, | 121 PRECEDENCE_PREFIX = 7, |
| 29 PRECEDENCE_CALL = 8, | 122 PRECEDENCE_CALL = 8, |
| (...skipping 586 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 616 break; | 709 break; |
| 617 } | 710 } |
| 618 } | 711 } |
| 619 | 712 |
| 620 // Suffix comments were assigned in reverse, so if there were multiple on | 713 // Suffix comments were assigned in reverse, so if there were multiple on |
| 621 // the same node, they need to be reversed. | 714 // the same node, they need to be reversed. |
| 622 if ((*i)->comments() && !(*i)->comments()->suffix().empty()) | 715 if ((*i)->comments() && !(*i)->comments()->suffix().empty()) |
| 623 const_cast<ParseNode*>(*i)->comments_mutable()->ReverseSuffix(); | 716 const_cast<ParseNode*>(*i)->comments_mutable()->ReverseSuffix(); |
| 624 } | 717 } |
| 625 } | 718 } |
| OLD | NEW |