-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.jison
118 lines (102 loc) · 1.8 KB
/
parser.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
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
/* lexical grammar */
%lex
%%
\s+ /* skip whitespace */
'{' return '{';
'}' return '}';
'(' return '(';
')' return ')';
'[' return '[';
']' return ']';
'<<' return '<<';
'>>' return '>>';
'@@' return '@@';
':>' return ':>';
'|->' return '|->';
',' return ',';
'-' return '-';
'TRUE' return 'TRUE';
'FALSE' return 'FALSE';
'"'[^\"]*'"' return 'STRING';
[a-zA-Z0-9_]*[a-zA-Z][a-zA-Z0-9_]* return 'NameChar';
[0-9]+ return 'Numeral';
/lex
/* operator associations and precedence */
%left ':>' '@@'
%left ',' '|->'
%start Main
%% /* language grammar */
Main : Expression {return $1;};
Expression
: Set {$$ = $1;}
| Sequence {$$ = $1;}
| '(' Function ')' {$$ = $2;}
| '[' Record ']' {$$ = $2;}
| Bool {$$ = $1;}
| Value {$$ = $1;}
| Integer {$$ = $1;}
;
Expressions
: Expressions ',' Expression
{
$1.push($3);
$$ = $1;
}
| Expression
{$$ = [$1];}
;
Set
: '{' Expressions '}'
{
$$ = new Set();
for(v of $2) $$.add(v);
}
| '{''}'
{$$ = new Set();}
;
Sequence
: '<<' Expressions '>>'
{$$ = $2;}
| '<<' '>>'
{$$ = [];}
;
Function
: Function '@@' Function
{
for([x,y] of $3) $1.set(x,y);
$$ = $1;
}
| Value ':>' Expression
{
$$ = new Map();
$$.set($1,$3);
}
;
Record
: Record ',' Record
{
for(x in $3) $1[x] = $3[x];
$$ = $1;
}
| Value '|->' Expression
{
$$ = {}
$$[$1] = $3;
}
;
Bool
: 'TRUE' {$$ = true;}
| 'FALSE' {$$ = false;}
;
Value
: NameChar
{$$ = yytext;}
| STRING
{$$ = yytext}
;
Integer
: '-' Numeral
{$$ = -Number(yytext);}
| Numeral
{$$ = Number(yytext);}
;