-
Notifications
You must be signed in to change notification settings - Fork 12
/
typedjs_parser.jison
104 lines (88 loc) · 1.92 KB
/
typedjs_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
/* lexical grammar */
%lex
%%
\s+ /* skip whitespace */
"String" return 'string'
"Number" return 'number'
"Char" return 'char'
"Boolean" return 'boolean'
([a-z]|[A-Z]|\_|\.)+ return 'VAR'
"//+" return 'BEGIN'
"," return ','
"(" return '('
")" return ')'
"[" return '['
"]" return ']'
"{" return '{'
"}" return '}'
"::" return '::'
"->" return '->'
":" return ':'
"|" return '|'
<<EOF>> return 'EOF'
. return 'INVALID'
/lex
/* operator associations and precedence */
%left ','
%left ':'
%left '|'
%start expressions
%% /* language grammar */
expressions
: BEGIN VAR "::" arg_list EOF
{return '{"func":"' + $2 + '","args":[' +$4 + ']}';}
;
arg_list
: e
{$$ = $1;}
| arg_list "->" e
{$$ = $1 +"," + $3;}
;
primitive
: string
{$$ = '"string"';}
| char
{$$ = '"char"';}
| number
{$$ = '"number"';}
| boolean
{$$ = '"boolean"';}
;
tuple
: '(' e ')'
{$$ = "["+$2+"]";}
;
object
: '{' e '}'
{$$ = "{"+$2+"}";}
;
list
: '[' primitive ']'
{$$ = '{"array":' + $2 + '}';}
| '[' object ']'
{$$ = '{"array":' + $2 + '}';}
| '[' tuple ']'
{$$ = '{"array":' + $2 + '}';}
| '[' list ']'
{$$ = '{"array":' + $2 + '}';}
| '[' or ']'
{$$ = '{"array":{"or":[' + $2 + ']}}'; }
;
or
: e '|' e
{$$ = $1 + ',' + $3;}
;
e
: e ',' e
{$$ = $1+','+$3;}
| or
{$$ = '{"or":[' + $1 + ']}';}
| object
| tuple
| list
| primitive
| e ':' e
{$$ = $1 +':'+ $3;}
| VAR
{$$ = '"' + String(yytext) + '"';}
;