forked from zaach/ebnf-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bnf.l
58 lines (49 loc) · 2.47 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
id [a-zA-Z][a-zA-Z0-9_-]*
%x action code
%s bnf ebnf
%%
<bnf,ebnf>"%%" this.pushState('code');return '%%';
<ebnf>"(" return '(';
<ebnf>")" return ')';
<ebnf>"*" return '*';
<ebnf>"?" return '?';
<ebnf>"+" return '+';
\s+ /* skip whitespace */
"//".* /* skip comment */
"/*"(.|\n|\r)*?"*/" /* skip comment */
"["{id}"]" yytext = yytext.substr(1, yyleng-2); return 'ALIAS';
{id} return 'ID';
'"'[^"]+'"' yytext = yytext.substr(1, yyleng-2); return 'STRING';
"'"[^']+"'" yytext = yytext.substr(1, yyleng-2); return 'STRING';
":" return ':';
";" return ';';
"|" return '|';
"%%" this.pushState(ebnf ? 'ebnf' : 'bnf'); return '%%';
"%ebnf" if (!yy.options) yy.options = {}; ebnf = yy.options.ebnf = true;
"%prec" return 'PREC';
"%start" return 'START';
"%left" return 'LEFT';
"%right" return 'RIGHT';
"%nonassoc" return 'NONASSOC';
"%parse-param" return 'PARSE_PARAM';
"%options" return 'OPTIONS';
"%lex"[\w\W]*?"/lex" return 'LEX_BLOCK';
"%"[a-zA-Z]+[^\r\n]* /* ignore unrecognized decl */
"<"[a-zA-Z]*">" /* ignore type */
"{{"[\w\W]*?"}}" yytext = yytext.substr(2, yyleng-4); return 'ACTION';
"%{"(.|\r|\n)*?"%}" yytext = yytext.substr(2, yytext.length-4); return 'ACTION';
"{" yy.depth = 0; this.pushState('action'); return '{';
"->".* yytext = yytext.substr(2, yyleng-2); return 'ARROW_ACTION';
. /* ignore bad characters */
<*><<EOF>> return 'EOF';
<action>"/*"(.|\n|\r)*?"*/" return 'ACTION_BODY';
<action>"//".* return 'ACTION_BODY';
<action>"/"[^ /]*?['"{}'][^ ]*?"/" return 'ACTION_BODY'; // regexp with braces or quotes (and no spaces)
<action>\"("\\\\"|'\"'|[^"])*\" return 'ACTION_BODY';
<action>"'"("\\\\"|"\'"|[^'])*"'" return 'ACTION_BODY';
<action>[/"'][^{}/"']+ return 'ACTION_BODY';
<action>[^{}/"']+ return 'ACTION_BODY';
<action>"{" yy.depth++; return '{';
<action>"}" if (yy.depth==0) this.begin(ebnf ? 'ebnf' : 'bnf'); else yy.depth--; return '}';
<code>(.|\n|\r)+ return 'CODE';
%%