aboutsummaryrefslogtreecommitdiffstats
path: root/lib/nix/parser.mly
blob: dc1638d2ba816894b5035276e3fe11cb87aa1a0b (about) (plain) (blame)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* Tokens with data */
%token <string> INT
%token <string> FLOAT
/* A path */
%token <string> PATH
/* Search path, enclosed in <> */
%token <string> SPATH
/* Home path, starts with ~ */
%token <string> HPATH
%token <string> URI
%token <string> STR_START
%token <string> STR_MID
%token STR_END
%token <string> ISTR_START
%token <string> ISTR_MID
%token <int> ISTR_END
%token <string> ID
/* Tokens that stand for themselves */
%token SELECT "."
%token QMARK "?"
%token CONCAT "++"
%token NOT "!"
%token MERGE "//"
%token ASSIGN "="
%token LT "<"
%token LTE "<="
%token GT ">"
%token GTE ">="
%token EQ "=="
%token NEQ "!="
%token AND "&&"
%token OR "||"
%token IMPL "->"
%token AQUOTE_OPEN "${"
%token AQUOTE_CLOSE "}$"
%token LBRACE "{"
%token RBRACE "}"
%token LBRACK "["
%token RBRACK "]"
%token PLUS "+"
%token MINUS "-"
%token TIMES "*"
%token SLASH "/"
%token LPAREN "("
%token RPAREN ")"
%token COLON ":"
%token SEMICOLON ";"
%token COMMA ","
%token ELLIPSIS "..."
%token AS "@"
/* Keywords */
%token WITH "with"
%token REC "rec"
%token LET "let"
%token IN "in"
%token INHERIT "inherit"
%token IF "if"
%token THEN "then"
%token ELSE "else"
%token ASSERT "assert"
%token ORDEF "or"

/* End of input */
%token EOF

%{
  open Types
%}

%start <Types.expr> main

%%

main:
| e = expr0 EOF
    { e }

expr0:
| "if"; e1 = expr0; "then"; e2 = expr0; "else"; e3 = expr0
    { Cond (e1, e2, e3) }
| "with"; e1 = expr0; ";"; e2 = expr0
    { With (e1, e2) }
| "assert"; e1 = expr0; ";"; e2 = expr0
    { Assert (e1, e2) }
| "let"; xs = delimited("{", list(binding), "}")
    { SetLet xs }
| "let"; xs = list(binding); "in"; e = expr0
    { Let (xs, e) }
| l = lambda
    { Val l }
| e = expr1
    { e }

/* Rules expr1-expr14 are almost direct translation of the operator
   precedence table:
   https://nixos.org/nix/manual/#sec-language-operators */

%inline binary_expr(Lhs, Op, Rhs):
| lhs = Lhs; op = Op; rhs = Rhs
    { BinaryOp (op, lhs, rhs) }

expr1:
| e = binary_expr(expr2, "->" {Impl}, expr1)
| e = expr2
    { e }

expr2:
| e = binary_expr(expr2, "||" {Or}, expr3)
| e = expr3
    { e }

expr3:
| e = binary_expr(expr3, "&&" {And}, expr4)
| e = expr4
    { e }

%inline expr4_ops:
| "==" { Eq }
| "!=" { Neq }

expr4:
| e = binary_expr(expr5, expr4_ops, expr5)
| e = expr5
    { e }

%inline expr5_ops:
| "<" { Lt }
| ">" { Gt }
| "<=" { Lte }
| ">=" { Gte }

expr5:
| e = binary_expr(expr6, expr5_ops, expr6)
| e = expr6
    { e }

expr6:
| e = binary_expr(expr7, "//" {Merge}, expr6)
| e = expr7
    { e }

expr7:
| e = preceded("!", expr7)
    { UnaryOp (Not, e) }
| e = expr8
    { e }

%inline expr8_ops:
| "+" { Plus }
| "-" { Minus }

expr8:
| e = binary_expr(expr8, expr8_ops, expr9)
| e = expr9
    { e }

%inline expr9_ops:
| "*" { Mult }
| "/" { Div }

expr9:
| e = binary_expr(expr9, expr9_ops, expr10)
| e = expr10
    { e }

expr10:
| e = binary_expr(expr11, "++" {Concat}, expr10)
| e = expr11
    { e }

expr11:
| e = expr12 "?" p = attr_path
    { Test (e, p) }
| e = expr12
    { e }

expr12:
| e = preceded("-", expr13)
    { UnaryOp (Negate, e) }
| e = expr13
    { e }

expr13:
| f = expr13; arg = expr14
    { Apply (f, arg) }
| e = expr14
    { e }

%inline selectable:
| s = set
    { Val s }
| id = ID
    { Id id }
| e = delimited("(", expr0, ")")
    { e }

expr14:
| e = selectable; "."; p = attr_path; o = option(preceded("or", expr14))
    { Select (e, p, o) }
| e = atomic_expr; "or"
    { Apply (e, Id "or") }
| e = atomic_expr
    { e }

atomic_expr:
| id = ID
    { Id id }
| v = value
    { Val v }
| e = delimited("(", expr0, ")")
    { e }

attr_path:
| p = separated_nonempty_list(".", attr_path_component)
    { p }

attr_path_component:
| "or"
    { Id "or" }
| id = ID
    { Id id }
| e = delimited("${", expr0, "}$")
    { Aquote e }
| s = str
    { Val s }

value:
| s = str
    { s }
| s = istr
    { s }
| i = INT
    {Int i}
| f = FLOAT
    { Float f }
| p = PATH
    { Path p }
| sp = SPATH
    { SPath sp }
| hp = HPATH
    { HPath hp }
| uri = URI
    { Uri uri }
| l = nixlist
    { l }
| s = set
    { s }

%inline str_mid(X):
| xs = list(pair(delimited("${", expr0, "}$"), X)) { xs }

/* Double-quoted string */
str:
| start = STR_START; mids = str_mid(STR_MID); STR_END
    { Str (start, mids) }

/* Indented string */
istr:
| start = ISTR_START; mids = str_mid(ISTR_MID); i = ISTR_END
    { IStr (i, start, mids) }

/* Lists and sets */
nixlist:
| xs = delimited("[", list(expr14), "]")
    { List xs }

empty_set:
| "{"; "}" {}

set:
| empty_set
    { AttSet (Nonrec, []) }
| xs = delimited("{", nonempty_list(binding), "}")
    { AttSet (Nonrec, xs) }
| xs = preceded("rec", delimited("{", list(binding), "}"))
    { AttSet (Rec, xs) }

binding:
| kv = terminated(separated_pair(attr_path, "=", expr0), ";")
    { let (k, v) = kv in AttrPath (k, v) }
| xs = delimited("inherit", pair(option(delimited("(", expr0, ")")), list(attr_path_component)), ";")
    { let (prefix, ids) = xs in Inherit (prefix, ids) }

lambda:
| id = ID; "@"; p = param_set; ":"; e = expr0
    { Lambda (ParamSet (Some id, p), e) }
| p = param_set; "@"; id = ID; ":"; e = expr0
    { Lambda (ParamSet (Some id, p), e) }
| p = param_set; ":"; e = expr0
    { Lambda (ParamSet (None, p), e) }
| id = ID; ":"; e = expr0
    { Lambda (Alias id, e) }

%inline param_set:
| empty_set
    { ([], Exact) }
| "{"; "..."; "}"
    { ([], Loose) }
| ps = delimited("{", pair(pair(params, ","?), boption("...")), "}")
    { let ((ps, _), ellipsis) = ps in (ps, if ellipsis then Loose else Exact) }

params:
| p = param
    { [p] }
| ps = params; ","; p = param
    { ps @ [p] }

%inline param:
p = pair(ID, option(preceded("?", expr0)))
    { p }