This repository has been archived by the owner on May 1, 2024. It is now read-only.
forked from GerHobbelt/ebnf-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbnf.y
418 lines (368 loc) · 8.93 KB
/
bnf.y
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
%start spec
%parse-param options
/* grammar for parsing jison grammar files */
%{
var fs = require('fs');
var transform = require('./ebnf-transform').transform;
var ebnf = false;
%}
%%
spec
: declaration_list '%%' grammar optional_end_block EOF
{
$$ = $declaration_list;
if ($optional_end_block && $optional_end_block.trim() !== '') {
yy.addDeclaration($$, { include: $optional_end_block });
}
return extend($$, $grammar);
}
;
optional_end_block
:
| '%%' extra_parser_module_code
{ $$ = $extra_parser_module_code; }
;
optional_action_header_block
:
{ $$ = {}; }
| optional_action_header_block ACTION
{
$$ = $optional_action_header_block;
yy.addDeclaration($$, { actionInclude: $ACTION });
}
| optional_action_header_block include_macro_code
{
$$ = $optional_action_header_block;
yy.addDeclaration($$, { actionInclude: $include_macro_code });
}
;
declaration_list
: declaration_list declaration
{ $$ = $declaration_list; yy.addDeclaration($$, $declaration); }
|
{ $$ = {}; }
;
declaration
: START id
{ $$ = {start: $id}; }
| LEX_BLOCK
{ $$ = {lex: $LEX_BLOCK}; }
| operator
{ $$ = {operator: $operator}; }
| TOKEN full_token_definitions
{ $$ = {token_list: $full_token_definitions}; }
| ACTION
{ $$ = {include: $ACTION}; }
| include_macro_code
{ $$ = {include: $include_macro_code}; }
| parse_param
{ $$ = {parseParam: $parse_param}; }
| parser_type
{ $$ = {parserType: $parser_type}; }
| options
{ $$ = {options: $options}; }
| UNKNOWN_DECL
{ $$ = {unknownDecl: $UNKNOWN_DECL}; }
| IMPORT import_name import_path
{ $$ = {imports: {name: $import_name, path: $import_path}}; }
;
import_name
: ID
| STRING
;
import_path
: ID
| STRING
;
options
: OPTIONS option_list OPTIONS_END
{ $$ = $option_list; }
;
option_list
: option_list option
{ $$ = $option_list; $$.push($option); }
| option
{ $$ = [$option]; }
;
option
: NAME[option]
{ $$ = [$option, true]; }
| NAME[option] '=' OPTION_VALUE[value]
{ $$ = [$option, $value]; }
| NAME[option] '=' NAME[value]
{ $$ = [$option, $value]; }
;
parse_param
: PARSE_PARAM token_list
{ $$ = $token_list; }
;
parser_type
: PARSER_TYPE symbol
{ $$ = $symbol; }
;
operator
: associativity token_list
{ $$ = [$associativity]; $$.push.apply($$, $token_list); }
;
associativity
: LEFT
{ $$ = 'left'; }
| RIGHT
{ $$ = 'right'; }
| NONASSOC
{ $$ = 'nonassoc'; }
;
token_list
: token_list symbol
{ $$ = $token_list; $$.push($symbol); }
| symbol
{ $$ = [$symbol]; }
;
full_token_definitions
: full_token_definitions full_token_definition
{ $$ = $full_token_definitions; $$.push($full_token_definition); }
| full_token_definition
{ $$ = [$full_token_definition]; }
;
// As per http://www.gnu.org/software/bison/manual/html_node/Token-Decl.html
full_token_definition
: optional_token_type id optional_token_value optional_token_description
{
$$ = {id: $id};
if ($optional_token_type) {
$$.type = $optional_token_type;
}
if ($optional_token_value) {
$$.value = $optional_token_value;
}
if ($optional_token_description) {
$$.description = $optional_token_description;
}
}
;
optional_token_type
: /* epsilon */
{ $$ = false; }
| TOKEN_TYPE
;
optional_token_value
: /* epsilon */
{ $$ = false; }
| INTEGER
;
optional_token_description
: /* epsilon */
{ $$ = false; }
| STRING
;
id_list
: id_list id
{ $$ = $id_list; $$.push($id); }
| id
{ $$ = [$id]; }
;
token_id
: TOKEN_TYPE id
{ $$ = $id; }
| id
{ $$ = $id; }
;
grammar
: optional_action_header_block production_list
{
$$ = $optional_action_header_block;
$$.grammar = $production_list;
}
;
production_list
: production_list production
{
$$ = $production_list;
if ($production[0] in $$) {
$$[$production[0]] = $$[$production[0]].concat($production[1]);
} else {
$$[$production[0]] = $production[1];
}
}
| production
{ $$ = {}; $$[$production[0]] = $production[1]; }
;
production
: id ':' handle_list ';'
{$$ = [$id, $handle_list];}
;
handle_list
: handle_list '|' handle_action
{
$$ = $handle_list;
$$.push($handle_action);
}
| handle_action
{
$$ = [$handle_action];
}
;
handle_action
: handle prec action
{
$$ = [($handle.length ? $handle.join(' ') : '')];
if ($action) {
$$.push($action);
}
if ($prec) {
$$.push($prec);
}
if ($$.length === 1) {
$$ = $$[0];
}
}
;
handle
: handle expression_suffix
{
$$ = $handle;
$$.push($expression_suffix);
}
|
{
$$ = [];
}
;
handle_sublist
: handle_sublist '|' handle
{
$$ = $handle_sublist;
$$.push($handle.join(' '));
}
| handle
{
$$ = [$handle.join(' ')];
}
;
expression_suffix
: expression suffix ALIAS
{
$$ = $expression + $suffix + "[" + $ALIAS + "]";
}
| expression suffix
{
$$ = $expression + $suffix;
}
;
expression
: ID
{
$$ = $ID;
}
| STRING
{
// Re-encode the string *anyway* as it will
// be made part of the rule rhs a.k.a. production (type: *string*) again and we want
// to be able to handle all tokens, including *significant space*
// encoded as literal tokens in a grammar such as this: `rule: A ' ' B`.
if ($STRING.indexOf("'") >= 0) {
$$ = '"' + $STRING + '"';
} else {
$$ = "'" + $STRING + "'";
}
}
| '(' handle_sublist ')'
{
$$ = '(' + $handle_sublist.join(' | ') + ')';
}
;
suffix
: /* epsilon */
{ $$ = ''; }
| '*'
| '?'
| '+'
;
prec
: PREC symbol
{
$$ = { prec: $symbol };
}
|
{
$$ = null;
}
;
symbol
: id
{ $$ = $id; }
| STRING
{ $$ = $STRING; }
;
id
: ID
{ $$ = $ID; }
;
action
: '{' action_body '}'
{ $$ = $action_body; }
| ACTION
{ $$ = $ACTION; }
| include_macro_code
{ $$ = $include_macro_code; }
| ARROW_ACTION
{ $$ = '$$ =' + $ARROW_ACTION + ';'; }
|
{ $$ = ''; }
;
action_body
:
{ $$ = ''; }
| action_comments_body
{ $$ = $action_comments_body; }
| action_body '{' action_body '}' action_comments_body
{ $$ = $1 + $2 + $3 + $4 + $5; }
| action_body '{' action_body '}'
{ $$ = $1 + $2 + $3 + $4; }
;
action_comments_body
: ACTION_BODY
{ $$ = $ACTION_BODY; }
| action_comments_body ACTION_BODY
{ $$ = $action_comments_body + $ACTION_BODY; }
;
extra_parser_module_code
: optional_module_code_chunk
{ $$ = $optional_module_code_chunk; }
| optional_module_code_chunk include_macro_code extra_parser_module_code
{ $$ = $optional_module_code_chunk + $include_macro_code + $extra_parser_module_code; }
;
include_macro_code
: INCLUDE PATH
{
console.log('options: ', options);
var fileContent = fs.readFileSync($PATH, { encoding: 'utf-8' });
// And no, we don't support nested '%include':
$$ = '\n// Included by Jison: ' + $PATH + ':\n\n' + fileContent + '\n\n// End Of Include by Jison: ' + $PATH + '\n\n';
}
| INCLUDE error
{
console.error("%include MUST be followed by a valid file path");
}
;
module_code_chunk
: CODE
{ $$ = $CODE; }
| module_code_chunk CODE
{ $$ = $module_code_chunk + $CODE; }
;
optional_module_code_chunk
: module_code_chunk
{ $$ = $module_code_chunk; }
| /* nil */
{ $$ = ''; }
;
%%
// transform ebnf to bnf if necessary
function extend(json, grammar) {
json.bnf = ebnf ? transform(grammar.grammar) : grammar.grammar;
if (grammar.actionInclude) {
json.actionInclude = grammar.actionInclude;
}
return json;
}