This repository has been archived by the owner on Jun 15, 2019. It is now read-only.
forked from zaach/ebnf-parser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bnf.l
349 lines (283 loc) · 17 KB
/
bnf.l
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
%code imports %{
import helpers from 'jison-helpers-lib';
%}
ASCII_LETTER [a-zA-z]
// \p{Alphabetic} already includes [a-zA-z], hence we don't need to merge
// with {UNICODE_LETTER} (though jison has code to optimize if you *did*
// include the `[a-zA-Z]` anyway):
UNICODE_LETTER [\p{Alphabetic}]
ALPHA [{UNICODE_LETTER}_]
DIGIT [\p{Number}]
WHITESPACE [\s\r\n\p{Separator}]
ALNUM [{ALPHA}{DIGIT}]
NAME [{ALPHA}](?:[{ALNUM}-]*{ALNUM})?
ID [{ALPHA}]{ALNUM}*
DECIMAL_NUMBER [1-9][0-9]*
HEX_NUMBER "0"[xX][0-9a-fA-F]+
BR \r\n|\n|\r
// WhiteSpace MUST NOT match CR/LF and the regex `\s` DOES, so we cannot use
// that one directly. Instead we define the {WS} macro here:
WS [^\S\r\n]
// Quoted string content: support *escaped* quotes inside strings:
QUOTED_STRING_CONTENT (?:\\\'|\\[^\']|[^\\\'\r\n])*
DOUBLEQUOTED_STRING_CONTENT (?:\\\"|\\[^\"]|[^\\\"\r\n])*
// backquoted ES6/ES2017 string templates MAY span multiple lines:
ES2017_STRING_CONTENT (?:\\\`|\\[^\`]|[^\\\`])*
// Regex for matching all the possible stuff which can be placed between those `%lex.../lex` markers:
// multiple lines of arbitrary material. Use a non-gready `*?` in there to ensure that the regex
// doesn't also consume the terminating `/lex` token!
LEX_CONTENT {WS}*(?:{BR}[^]*?)?{BR}{WS}*
%x action code path options option_values
%s token
%s bnf ebnf
%options easy_keyword_rules
%options ranges
%options xregexp
%%
<action>"/*"[^]*?"*/" return 'ACTION_BODY';
<action>"//"[^\r\n]* return 'ACTION_BODY';
<action>"/"[^ /]*?['"{}][^ ]*?"/" return 'ACTION_BODY'; // regexp with braces or quotes (and no spaces)
<action>\"{DOUBLEQUOTED_STRING_CONTENT}\"
return 'ACTION_BODY';
<action>\'{QUOTED_STRING_CONTENT}\'
return 'ACTION_BODY';
<action>[/"'][^{}/"']+ return 'ACTION_BODY';
<action>[^{}/"']+ return 'ACTION_BODY';
<action>"{" yy.depth++; return '{';
<action>"}" if (yy.depth === 0) {
this.popState();
} else {
yy.depth--;
}
return '}';
<token>{BR} this.popState();
<token>"%%" this.popState();
<token>";" this.popState();
<bnf,ebnf>"%%" this.pushState('code');
return '%%';
// Support bison's `%empty` (and our own alias `%epsilon`) to identify an empty rule alt:
<bnf,ebnf>"%empty" return 'EPSILON';
<bnf,ebnf>"%epsilon" return 'EPSILON';
// See also https://en.wikipedia.org/wiki/Epsilon#Glyph_variants
<bnf,ebnf>"\u0190" return 'EPSILON';
<bnf,ebnf>"\u025B" return 'EPSILON';
<bnf,ebnf>"\u03B5" return 'EPSILON';
<bnf,ebnf>"\u03F5" return 'EPSILON';
<ebnf>"(" return '(';
<ebnf>")" return ')';
<ebnf>"*" return '*';
<ebnf>"?" return '?';
<ebnf>"+" return '+';
<options>{NAME} return 'NAME';
<options>"=" this.pushState('option_values');
return '=';
<option_values>{
\"{DOUBLEQUOTED_STRING_CONTENT}\"
yytext = unescQuote(this.matches[1], /\\"/g);
this.popState();
return 'OPTION_STRING_VALUE'; // value is always a string type
\'{QUOTED_STRING_CONTENT}\'
yytext = unescQuote(this.matches[1], /\\'/g);
this.popState();
return 'OPTION_STRING_VALUE'; // value is always a string type
\`{ES2017_STRING_CONTENT}\`
yytext = unescQuote(this.matches[1], /\\`/g);
this.popState();
return 'OPTION_STRING_VALUE'; // value is always a string type
}
// Comments should be gobbled and discarded anywhere *except* the code/action blocks:
<INITIAL,ebnf,bnf,token,path,options,option_values>"//"[^\r\n]*
/* skip single-line comment */
<INITIAL,ebnf,bnf,token,path,options,option_values>"/*"[^]*?"*/"
/* skip multi-line comment */
<option_values>[^\s\r\n]+ this.popState();
return 'OPTION_VALUE';
<options>{BR}{WS}+(?=\S) /* skip leading whitespace on the next line of input, when followed by more options */
<options>{BR} this.popState(); return 'OPTIONS_END';
<options,option_values>{WS}+ /* skip whitespace */
{WS}+ /* skip whitespace */
{BR}+ /* skip newlines */
"["{ID}"]" yytext = this.matches[1]; return 'ALIAS';
{ID} return 'ID';
{NAME} return 'NAME';
"$end" return 'EOF_ID';
// `$eof` and `EOF` are synonyms of `$end` ('$eof' is for bison compatibility);
// this is the only place where two symbol names may map to a single symbol ID number
// and we do not want `$eof`/`EOF` to show up in the symbol tables of generated parsers
// as we use `$end` for that one!
"$eof" return 'EOF_ID';
\"{DOUBLEQUOTED_STRING_CONTENT}\" %{
yytext = unescQuote(this.matches[1], /\\"/g);
return 'STRING';
%}
\'{QUOTED_STRING_CONTENT}\' %{
yytext = unescQuote(this.matches[1], /\\'/g);
return 'STRING';
%}
<token>[^\s\r\n]+ return 'TOKEN_WORD';
":" return ':';
";" return ';';
"|" return '|';
"%%" this.pushState(yy.ebnf ? 'ebnf' : 'bnf'); return '%%';
"%ebnf" yy.ebnf = true; return 'EBNF';
"%debug" return 'DEBUG';
"%parser-type" return 'PARSER_TYPE';
"%prec" return 'PREC';
"%start" return 'START';
"%left" return 'LEFT';
"%right" return 'RIGHT';
"%nonassoc" return 'NONASSOC';
"%token" this.pushState('token'); return 'TOKEN';
"%parse-param" return 'PARSE_PARAM';
"%options" this.pushState('options'); return 'OPTIONS';
"%lex"{LEX_CONTENT}"/lex" %{
// remove the %lex../lex wrapper and return the pure lex section:
yytext = this.matches[1];
return 'LEX_BLOCK';
%}
"%code" return 'INIT_CODE';
"%import" return 'IMPORT';
<INITIAL,ebnf,bnf,code>"%include" this.pushState('path');
return 'INCLUDE';
"%"{NAME}([^\r\n]*) %{
/* ignore unrecognized decl */
this.warn(rmCommonWS`
EBNF: ignoring unsupported parser option ${dquote(yytext)}
while lexing in ${dquote(this.topState())} state.
Erroneous area:
` + this.prettyPrintRange(yylloc));
yytext = [
this.matches[1], // {NAME}
this.matches[2].trim() // optional value/parameters
];
return 'UNKNOWN_DECL';
%}
"<"{ID}">" yytext = this.matches[1];
return 'TOKEN_TYPE';
"{{"([^]*?)"}}" yytext = this.matches[1].replace(/\}\\\}/g, '}}'); // unescape any literal '}\}' that exists within the action code block
return 'ACTION';
"%{"([^]*?)"%}" yytext = this.matches[1].replace(/%\\\}/g, '%}'); // unescape any literal '%\}' that exists within the action code block
return 'ACTION';
"{" yy.depth = 0; this.pushState('action');
return '{';
"->".* yytext = yytext.substr(2, yyleng - 2).trim();
return 'ARROW_ACTION';
"→".* yytext = yytext.substr(1, yyleng - 1).trim();
return 'ARROW_ACTION';
"=>".* yytext = yytext.substr(2, yyleng - 2).trim();
return 'ARROW_ACTION';
{HEX_NUMBER} yytext = parseInt(yytext, 16); return 'INTEGER';
{DECIMAL_NUMBER}(?![xX0-9a-fA-F]) yytext = parseInt(yytext, 10); return 'INTEGER';
// in the trailing CODE block, only accept these `%include` macros when
// they appear at the start of a line and make sure the rest of lexer
// regexes account for this one so it'll match that way only:
<code>[^\r\n]*(\r|\n)+ return 'CODE';
<code>[^\r\n]+ return 'CODE'; // the bit of CODE just before EOF...
<path>{BR} this.popState(); this.unput(yytext);
<path>\"{DOUBLEQUOTED_STRING_CONTENT}\"
yytext = unescQuote(this.matches[1]);
this.popState();
return 'PATH';
<path>\'{QUOTED_STRING_CONTENT}\'
yytext = unescQuote(this.matches[1]);
this.popState();
return 'PATH';
<path>{WS}+ // skip whitespace in the line
<path>[^\s\r\n]+ this.popState();
return 'PATH';
// detect and report unterminated string constants ASAP
// for 'action', 'options', but also for other lexer conditions:
//
// these error catching rules fix https://github.com/GerHobbelt/jison/issues/13
<action>\" yyerror(rmCommonWS`
unterminated string constant in lexer rule action block.
Erroneous area:
` + this.prettyPrintRange(yylloc));
return 'error';
<action>\' yyerror(rmCommonWS`
unterminated string constant in lexer rule action block.
Erroneous area:
` + this.prettyPrintRange(yylloc));
return 'error';
<action>\` yyerror(rmCommonWS`
unterminated string constant in lexer rule action block.
Erroneous area:
` + this.prettyPrintRange(yylloc));
return 'error';
<option_values>\" yyerror(rmCommonWS`
unterminated string constant in %options entry.
Erroneous area:
` + this.prettyPrintRange(yylloc));
return 'error';
<option_values>\' yyerror(rmCommonWS`
unterminated string constant in %options entry.
Erroneous area:
` + this.prettyPrintRange(yylloc));
return 'error';
<option_values>\` yyerror(rmCommonWS`
unterminated string constant in %options entry.
Erroneous area:
` + this.prettyPrintRange(yylloc));
return 'error';
<*>\" var rules = (this.topState() === 'macro' ? 'macro\'s' : this.topState());
yyerror(rmCommonWS`
unterminated string constant encountered while lexing
${rules}.
Erroneous area:
` + this.prettyPrintRange(yylloc));
return 'error';
<*>\' var rules = (this.topState() === 'macro' ? 'macro\'s' : this.topState());
yyerror(rmCommonWS`
unterminated string constant encountered while lexing
${rules}.
Erroneous area:
` + this.prettyPrintRange(yylloc));
return 'error';
<*>\` var rules = (this.topState() === 'macro' ? 'macro\'s' : this.topState());
yyerror(rmCommonWS`
unterminated string constant encountered while lexing
${rules}.
Erroneous area:
` + this.prettyPrintRange(yylloc));
return 'error';
<*>. %{
/* b0rk on bad characters */
yyerror(rmCommonWS`
unsupported parser input: ${dquote(yytext)}
while lexing in ${dquote(this.topState())} state.
Erroneous area:
` + this.prettyPrintRange(yylloc));
%}
<*><<EOF>> return 'EOF';
%%
var rmCommonWS = helpers.rmCommonWS;
var dquote = helpers.dquote;
function indent(s, i) {
var a = s.split('\n');
var pf = (new Array(i + 1)).join(' ');
return pf + a.join('\n' + pf);
}
// unescape a string value which is wrapped in quotes/doublequotes
function unescQuote(str) {
str = '' + str;
var a = str.split('\\\\');
a = a.map(function (s) {
return s.replace(/\\'/g, "'").replace(/\\"/g, '"');
});
str = a.join('\\\\');
return str;
}
lexer.warn = function l_warn() {
if (this.yy && this.yy.parser && typeof this.yy.parser.warn === 'function') {
return this.yy.parser.warn.apply(this, arguments);
} else {
console.warn.apply(console, arguments);
}
};
lexer.log = function l_log() {
if (this.yy && this.yy.parser && typeof this.yy.parser.log === 'function') {
return this.yy.parser.log.apply(this, arguments);
} else {
console.log.apply(console, arguments);
}
};