summaryrefslogtreecommitdiffstats
path: root/third_party/rust/jsparagus/js_parser/esgrammar.pgen
blob: 7a0b4cb7b91069fcea3c7699360269466ff690f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Grammar for ECMArkup grammar descriptions

var token CHR;        # <NL>
var token EQ;         # ::
var token MATCH_REF;  # $0
var token NL;         # (actual newline character)
var token NT;         # IdentifierName
var token NTALT;      # |LineTerminator|
var token NTCALL;     # Expression (when followed by `[` or `<`)
var token PRODID;     # #comment
var token PROSE;      # > any text following a greater than sign
var token T;          # `var`
var token WPROSE;     # [> any text after greater than sign, wrapped in brackets]
var token RUSTCOMMENT;# //comment

token But = "but";
token Empty = "empty";
token Here = "here";
token Lookahead = "lookahead";
token No = "no";
token Not = "not";
token Of = "of";
token One = "one";
token Or = "or";
token Through = "through";
token Returns = "returns";
token Some = "Some";
token None = "None";
token Arrow = "=>";
token Comma = ",";
token OpenBracket = "[";
token CloseBracket = "]";
token QuestionMark = "?";
token Tilde = "~";
token PlusSign = "+";
token Equal = "=";
token Equals = "==";
token IsNotEqualTo = "!=";
token IsNotIn = "<!";
token OpenBrace = "{";
token CloseBrace = "}";
token OpenParen = "(";
token CloseParen = ")";
token AtSign = "@";
token OpenAngle = "<";
token CloseAngle = ">";
token Impl = "impl";
token For = "for";
token Let = "let";
token SemiColon = ";";
token Lifetime = "'";

// Entry point for grammar_extension! macro's content.
goal nt rust_edsl {
    rust_impl rust_nt_def_list => rust_edsl($0, $1);
}

nt rust_impl {
    "impl" nt_type "for" nt_type "{" "}" ";" => rust_impl($1, $3);
    "impl" "<" nt_type_params ">" nt_type "for" nt_type "{" "}" ";" => rust_param_impl($4, $6, $2);
}

nt rust_nt_def_list {
    rust_nt_def_or_blank_line;
    rust_nt_def_list rust_nt_def_or_blank_line => concat($0, $1);
}

nt rust_nt_def_or_blank_line {
    NL => blank_line();
    RUSTCOMMENT => blank_line();
    rust_nt_def => nt_def_to_list($0);
}

// grammar extension syntax for adding/removing/extending the productions.
nt rust_nt_def {
    "let" nt_lhs "=" "{" rust_rhs_line "}" ";" => rust_nt_def($1, $4);
}

nt rust_rhs_line {
    rust_symbols => rust_rhs_line($0);
}

nt rust_symbols {
    rust_symbol;
    rust_symbols rust_symbol => concat($0, $1);
}

nt rust_symbol {
    "{" rust_expr "}" => single($1);
    symbol => single($0);
    NL => empty();
}

nt rust_expr {
    expr => rust_expr($0);
}

// Entry point for esgrammar files.
goal nt grammar {
    nt_def_list;
}

nt nt_def_list {
    nt_def_or_blank_line;
    nt_def_list nt_def_or_blank_line => concat($0, $1);
}

nt nt_def_or_blank_line {
    NL => blank_line();
    nt_def => nt_def_to_list($0);
}

nt nt_def {
    nt_type_line? nt_lhs EQ NL rhs_lines NL => nt_def($0, $1, $2, $4);
    nt_type_line? nt_lhs EQ "one" "of" NL t_list_lines NL => nt_def_one_of($0, $1, $2, $6);
}

nt nt_lhs {
    NT => nt_lhs_no_params($0);
    NTCALL "[" params "]" => nt_lhs_with_params($0, $2);
}

nt params {
    param => single($0);
    params "," param => append($0, $2);
}

nt param {
    NT;
}

nt nt_type_line {
    "@" "returns" nt_type NL => $2;
}

// Define a type as understood by Rust type-system.
nt nt_type {
   NT => simple_type($0);
   NTCALL "<" nt_type_params ">" => parameterized_type($0, $2);
}

nt nt_type_params {
    nt_type_param => single($0);
    nt_type_params "," nt_type_param => append($0, $2);
}

nt nt_type_param {
   nt_type;
   "'" NT => lifetime_type($0);
}

nt t_list_lines {
    t_list_line;
    t_list_lines t_list_line => concat($0, $1);
}

nt t_list_line {
    terminal_seq NL => t_list_line($0);
}

nt terminal_seq {
    terminal => single($0);
    terminal_seq terminal => append($0, $1);
}

nt terminal {
    T => terminal($0);
    CHR => chr($0);
}

nt rhs_lines {
    rhs_line => single($0);
    rhs_lines rhs_line => append($0, $1);
}

nt rhs_line {
    ifdef? rhs reducer? PRODID? NL => rhs_line($0, $1, $2, $3);
    PROSE NL => rhs_line_prose($0);
}

nt rhs {
    symbols;
    "[" "empty" "]" => empty_rhs();
}

nt reducer {
   NL? "=>" expr => $2;
}

nt expr {
    MATCH_REF => expr_match_ref($0);
    NT "(" expr_args? ")" expr_try? => expr_call($0, $2, $4);
    "Some" "(" expr ")" => expr_some($2);
    "None" => expr_none();
}

nt expr_try {
    "?";
}

nt expr_args {
    expr => single($0);
    expr_args "," expr => append($0, $2);
}

nt ifdef {
   "[" definite_sigil NT "]" => ifdef($1, $2);
}

nt symbols {
    symbol => single($0);
    symbols symbol => append($0, $1);
}

nt symbol {
    terminal;
    nonterminal;
    nonterminal "?" => optional($0);
    nonterminal "but" "not" exclusion => but_not($0, $3);
    nonterminal "but" "not" "one" "of" exclusion_list => but_not_one_of($0, $5);
    "[" "lookahead" lookahead_assertion "]" => $2;
    no_line_terminator_here;
    WPROSE => $0;
}

nt no_line_terminator_here {
    "[" "no" line_terminator "here" "]" => no_line_terminator_here($2);
}

nt nonterminal {
    NT => nonterminal($0);
    NTCALL "[" args "]" => nonterminal_apply($0, $2);
}

nt args {
    arg => single($0);
    args "," arg => append($0, $2);
}

nt arg {
    sigil NT => arg_expr($0, $1);
}

nt sigil {
    definite_sigil;
    "?";
}

nt definite_sigil {
    "~" => sigil_false();
    "+" => sigil_true();
}

nt exclusion_list {
    exclusion => single($0);
    exclusion_list "or" exclusion => append($0, $2);
}

nt exclusion {
    terminal => exclusion_terminal($0);
    nonterminal => exclusion_nonterminal($0);
    CHR "through" CHR => exclusion_chr_range($0, $2);
}

nt lookahead_assertion {
    "==" terminal => la_eq($1);
    "!=" terminal => la_ne($1);
    "<!" NT => la_not_in_nonterminal($1);
    "<!" "{" lookahead_exclusions "}" => la_not_in_set($2);
}

nt lookahead_exclusions {
    lookahead_exclusion => single($0);
    lookahead_exclusions "," lookahead_exclusion => append($0, $2);
}

nt lookahead_exclusion {
    lookahead_exclusion_element => single($0);
    lookahead_exclusion lookahead_exclusion_element => append($0, $1);
}

nt lookahead_exclusion_element {
    terminal;
    no_line_terminator_here;
}

nt line_terminator {
    NT;
    NTALT;
}