-
Notifications
You must be signed in to change notification settings - Fork 18
/
parser.mly
214 lines (149 loc) · 3.42 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
%{
open Util
%}
(* Infix operations a la OCaml *)
%token <string Util.Location.t> PREFIXOP INFIXOP0 INFIXOP1 INFIXOP2 INFIXOP3 INFIXOP4
(* Names *)
%token <string> NAME
%token UNDERSCORE
(* Parentheses & punctuations *)
%token LPAREN RPAREN
%token COLONEQ
%token COMMA COLON DARROW ARROW
(* Expressions *)
%token LET IN
%token TYPE
%token PROD
%token LAMBDA
(* Toplevel commands *)
%token <string> QUOTED_STRING
%token LOAD
%token DEF
%token CHECK
%token EVAL
%token AXIOM
(* End of input token *)
%token EOF
(* Precedence and fixity of infix operators *)
%left INFIXOP0
%right INFIXOP1
%left INFIXOP2
%left INFIXOP3
%right INFIXOP4
%start <Syntax.toplevel list> file
%start <Syntax.toplevel> commandline
%%
file:
| f=filecontents EOF
{ f }
filecontents:
|
{ [] }
| d=topcomp ds=filecontents
{ d :: ds }
commandline:
| topcomp EOF
{ $1 }
(* Things that can be defined on toplevel. *)
topcomp: mark_location(topcomp_) { $1 }
topcomp_:
| LOAD fn=QUOTED_STRING
{ Syntax.TopLoad fn }
| DEF x=var_name COLONEQ e=term
{ Syntax.TopDefinition (x, e) }
| CHECK e=term
{ Syntax.TopCheck e }
| EVAL e=term
{ Syntax.TopEval e }
| AXIOM x=var_name COLON e=term
{ Syntax.TopAxiom (x, e) }
term : mark_location(term_) { $1 }
term_:
| e=infix_term_
{ e }
| PROD a=prod_abstraction COMMA e=term
{ Syntax.prod a e }
| e1=infix_term ARROW e2=term
{ Syntax.arrow e1 e2 }
| LAMBDA a=lambda_abstraction DARROW e=term
{ Syntax.lambda a e }
| LET x=var_name COLONEQ e1=term IN e2=term
{ Syntax.Let (x, e1, e2) }
| e=infix_term COLON t=term
{ Syntax.Ascribe (e, t) }
infix_term: mark_location(infix_term_) { $1 }
infix_term_:
| e=app_term_
{ e }
| e2=infix_term oploc=infix e3=infix_term
{ let {Location.data=op; loc} = oploc in
let op = Location.locate ~loc (Syntax.Var op) in
let e1 = Location.locate ~loc (Syntax.Apply (op, e2)) in
Syntax.Apply (e1, e3)
}
app_term: mark_location(app_term_) { $1 }
app_term_:
| e=prefix_term_
{ e }
| e1=app_term e2=prefix_term
{ Syntax.Apply (e1, e2) }
prefix_term: mark_location(prefix_term_) { $1 }
prefix_term_:
| e=simple_term_
{ e }
| oploc=prefix e2=prefix_term
{ let {Location.data=op; loc} = oploc in
let op = Location.locate ~loc (Syntax.Var op) in
Syntax.Apply (op, e2)
}
(* simple_term : mark_location(simple_term_) { $1 } *)
simple_term_:
| LPAREN e=term_ RPAREN
{ e }
| TYPE
{ Syntax.Type }
| x=var_name
{ Syntax.Var x }
var_name:
| NAME
{ $1 }
| LPAREN op=infix RPAREN
{ op.Location.data }
| LPAREN op=prefix RPAREN
{ op.Location.data }
| UNDERSCORE
{ Name.anonymous () }
%inline infix:
| op=INFIXOP0
{ op }
| op=INFIXOP1
{ op }
| op=INFIXOP2
{ op }
| op=INFIXOP3
{ op }
| op=INFIXOP4
{ op }
%inline prefix:
| op=PREFIXOP
{ op }
lambda_abstraction:
| lst=nonempty_list(binder)
{ lst }
prod_abstraction:
| lst=nonempty_list(typed_binder)
{ lst }
typed_binder: mark_location(typed_binder_) { $1 }
typed_binder_:
| LPAREN xs=nonempty_list(var_name) COLON t=term RPAREN
{ (xs, t) }
binder: mark_location(binder_) { $1 }
binder_:
| LPAREN xs=nonempty_list(var_name) COLON t=term RPAREN
{ (xs, Some t) }
| x=var_name
{ ([x], None) }
mark_location(X):
| x=X
{ Location.locate ~loc:(Location.make $startpos $endpos) x }
%%