Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: tools/gn/parser.cc

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

Powered by Google App Engine
This is Rietveld 408576698