-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparser.mly
193 lines (165 loc) · 4.82 KB
/
parser.mly
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
%{
open Types
%}
(* keywords *)
%token ALIAS
%token BIND
%token DEFINE
%token VAR
%token SWITCH
%token WHEN
%token INCREMENT
(* cmds *)
%token <string> IDENTIFIER
%token <string> UNQUOTED
%token <string> QUOTED
(* symbols *)
%token LBRACE RBRACE
%token LPAREN RPAREN
%token COMMA COLON SEMICOLON
%token RARROW
%token ASSIGN
%token WILDCARD
(* soft statement terminator *)
%token NEWLINE
(* EOF *)
%token EOF
%start prog
%type <Types.parse_tree> prog
%%
(* Making newlines act as command terminators is useful
* for backwards-compatibility and bugs: forgetting a semicolon
* would cause the command in the second line to be treated as
* parameters to the command in the previous line.
*
* However, the downside of having newlines produce a token instead of being
* eaten by the lexer is that now we need to find a way to ignore those newline
* tokens everywhere thats not the end of a statement. Unfortunatelly, the
* inly way I could think to do this involves putting a call to "s" after almost
* every non terminal in the grammar, which is very tricky to get right and prone
* to forgetting about some cases :/
*)
s:
| (*empty*) {()}
| s NEWLINE {()}
terminator:
| EOF {()}
| NEWLINE s {()}
| SEMICOLON s {()}
prog:
| s b=block EOF { b }
block:
| stmts_or_decls=list(stmt_or_decl) {
(* We wrap extra "Block" stmts around declarations that are mixed
* with other statements instead of being at the top of the block *)
let open Core_kernel.Std in
let to_block pos xs =
match xs with
| [x] -> x (* no need for a Block node when there is a single stmt *)
| _ -> ParseTree(pos, Block xs)
in
let nested_decls =
List.fold_right stmts_or_decls
~init: []
~f:(fun x rest ->
match x with
| Core_kernel.Std.Either.First (pos, decl) -> [ ParseTree(pos, decl (to_block pos rest)) ]
| Core_kernel.Std.Either.Second stmt -> stmt :: rest
)
in
to_block $startpos nested_decls }
stmt_or_decl:
| decl { Core_kernel.Std.Either.First $1 }
| stmt { Core_kernel.Std.Either.Second $1 }
decl:
| raw_decl { ($startpos, $1) }
raw_decl:
| DEFINE s name=name s value=expr terminator
{ fun scope -> Let(name, Decl_Const(value), scope) }
| DEFINE s
name=name s
LPAREN s
args=separated_list(terminated(COMMA, s), terminated(name, s))
RPAREN s
body=stmt
{ fun scope -> Let(name, Decl_Macro(args, body), scope) }
| ALIAS s name=alias_name s body=stmt
{ fun scope -> Let(name, Decl_Alias(body), scope) }
| VAR s
name=name s
COLON s
LBRACE s
domain=separated_list(terminated(COMMA, s), terminated(str, s))
RBRACE (*no s*)
m_init=ioption(varinit) terminator
{
let open Core_kernel.Std in
match domain with
| [] -> raise (CompilationError([
($startpos(domain), "Empty variable domain")
]))
| (first_value ::_) ->
let init =
match m_init with
| Some s -> s
| None -> first_value
in
fun scope -> Let(name, Decl_Var(domain, init), scope)
}
stmt:
| SEMICOLON s
{ ParseTree($startpos, Nop) }
| LBRACE s block=block RBRACE s
{ block }
| cmd=cmd (**) args=expr* (**) terminator
{ ParseTree($startpos, Cmd(cmd, args)) }
| name=name (**)
LPAREN s
args=separated_list(terminated(COMMA, s), terminated(expr, s))
RPAREN terminator
{ ParseTree($startpos, CallMacro(name, args)) }
| BIND s keys=expr s body=stmt
{ ParseTree($startpos, Bind(keys, body)) }
| name=name (**) value=varinit terminator
{ ParseTree($startpos, Assign(name, value)) }
| INCREMENT (**) name=name terminator
{ ParseTree($startpos, Increment(name)) }
| c=condtype s name=name s LBRACE s cases=list(case) RBRACE s
{ ParseTree($startpos, Cond(c, name, cases)) }
varinit:
| ASSIGN s value=str { value }
case:
| value=pattern s RARROW s body=stmt { (value, body) }
(* These final productions appear inside other commands and don't consume any newlines *)
condtype:
| WHEN { Cond_When }
| SWITCH { Cond_If }
pattern:
| str { Pat_Const $1 }
| WILDCARD { Pat_Wildcard }
name:
| name=IDENTIFIER
{
if List.mem name.[0] ['+';'-'] then (
raise (CompilationError([
($startpos, "Non-alias identifier starting with + or -")
]))
);
($startpos, name)
}
alias_name:
| name=IDENTIFIER
{ ($startpos, name) }
cmd: raw_cmd { ($startpos, $1) }
expr: raw_expr { ($startpos, $1) }
str: raw_str { ($startpos, $1) }
raw_cmd:
| raw_str { Core_kernel.Std.Either.First $1 }
| QUOTED { Core_kernel.Std.Either.Second $1 }
raw_str:
| IDENTIFIER { $1 }
| UNQUOTED { $1 }
raw_expr:
| IDENTIFIER { $1 }
| UNQUOTED { $1 }
| QUOTED { $1 }