-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.jison
62 lines (51 loc) · 1.35 KB
/
grammar.jison
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
%lex
%s CONF TAIL
%%
<TAIL>(.|\n)*<<EOF>> return "CODE";
\s+ /* skip whitespace */
\{[^}]+\} return "CODE";
[A-Za-z][A-Za-z0-9]* return "NAME";
<CONF>"$$" %{ this.begin("TAIL"); return "MARK"; %}
"$$" %{ this.begin("CONF"); return "MARK"; %}
";" return ";";
<INITIAL>"[" return "[";
<INITIAL>"]" return "]";
"(" return "(";
")" return ")";
"," return ",";
"!" return "!";
"\\" return "\\";
"_" return "_";
<CONF>"=" return "=";
/lex
%token MARK NAME CODE
%%
prog : rset MARK init tail {return {rules: $1, conf: $3, code: $4};}
;
rset : /* empty */ {$$ = [];}
| rset side CODE side ';' {$1.push({left: $2, right: $4, code: $3}); $$ = $1;}
;
side : cell {$$ = {node: $1, pax: []};}
| cell '[' list ']' {$$ = {node: $1, pax: $3};}
;
tree : leaf {$$ = {node: $1, pax: []};}
| cell '(' list ')' {$$ = {node: $1, pax: $3};}
;
list : tree {$$ = [$1];}
| list ',' tree {$1.push($3); $$ = $1;}
;
leaf : cell
| NAME {$$ = {agent: "wire", name: $1};}
;
cell : need NAME {$$ = {agent: $2, need: $1, code: ""};}
| need NAME '_' CODE {$$ = {agent: $2, need: $1, code: $4.slice(1, -1)};}
;
need : '!' {$$ = true;}
| '\' {$$ = false;}
;
init : /* empty */ {$$ = [];}
| init tree '=' tree ';' {$1.push({left: $2, right: $4}); $$ = $1;}
;
tail : /* empty */ {$$ = "";}
| MARK CODE {$$ = $2;}
;