-
Notifications
You must be signed in to change notification settings - Fork 6
/
grammar.js
432 lines (379 loc) · 12.6 KB
/
grammar.js
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
const PREC = {
// The priorities below are taken from the spec here:
// https://jsonnet.org/ref/spec.html#associativity_precedence
// The order is reversed though: In the spec, '1' means high priority
// but with tree-sitter it means low priority.
application_indexing: 13,
unary: 12,
multiplicative: 11,
additive: 10,
bitshift: 9,
comparison: 8,
equality: 7,
bitand: 6,
bitxor: 5,
bitor: 4,
and: 3,
or: 2,
// This is not in the spec, which is why we go from 2 to 13 above
// while in the spec it is going from 1 to 12.
member: 1,
};
module.exports = grammar({
name: "jsonnet",
extras: ($) => [/\s/, $.comment],
externals: ($) => [$._string_start, $._string_content, $._string_end],
word: ($) => $._ident,
inline: ($) => [$.h, $.objinside],
conflicts: () => [],
rules: {
document: ($) => $._expr,
comment: () =>
token(
choice(
seq("//", /.*/),
seq("#", /.*/),
seq("/*", /[^*]*\*+([^/*][^*]*\*+)*/, "/"),
),
),
_expr: ($) =>
choice(
$.null,
$.true,
$.false,
$.self,
$.dollar,
$.string,
$.number,
$.object,
$.array,
$.forloop,
$.fieldaccess,
$.indexing,
$.fieldaccess_super,
$.indexing_super,
$.functioncall,
$.id,
$.local_bind,
$.conditional,
$.binary,
$.unary,
$.implicit_plus,
$.anonymous_function,
$._assert_expr,
$.import,
$.importstr,
$.error,
$.in_super,
$.parenthesis,
),
// Literals
null: () => "null",
true: () => "true",
false: () => "false",
// Keywords
self: () => "self",
dollar: () => "$",
super: () => "super",
local: () => "local",
tailstrict: () => "tailstrict",
// Types
number: ($) => $._number,
string: ($) => $._string,
object: ($) => seq("{", optional($.objinside), "}"),
array: ($) => seq("[", commaSep($._expr, true), "]"),
forloop: ($) =>
seq(
"[",
$._expr,
optional(","),
$.forspec,
optional($.compspec),
"]",
),
fieldaccess: ($) =>
prec(
PREC.application_indexing,
seq($._expr, ".", field("last", $.id)),
),
indexing: ($) =>
prec(
PREC.application_indexing,
seq(
$._expr,
"[",
optional($._expr),
optional(
seq(
":",
optional($._expr),
optional(seq(":", optional($._expr))),
),
),
"]",
),
),
fieldaccess_super: ($) => seq($.super, ".", $.id),
indexing_super: ($) => seq($.super, "[", $._expr, "]"),
functioncall: ($) =>
prec(
PREC.application_indexing,
seq(
$._expr,
"(",
optional($.args),
")",
optional($.tailstrict),
),
),
id: ($) => $._ident,
// This use of an intermediate rule for identifiers is to
// overcome some limitations in ocaml-tree-sitter-semgrep.
// Indeed, ocaml-tree-sitter-semgrep can't override terminals (here was id)
// that are also mentioned in the 'word:' directive.
_ident: () => /[_a-zA-Z][_a-zA-Z0-9]*/,
local_bind: ($) =>
prec.right(seq($.local, commaSep1($.bind, false), ";", $._expr)),
conditional: ($) =>
prec.right(
seq(
"if",
field("condition", $._expr),
"then",
field("consequence", $._expr),
optional(seq("else", field("alternative", $._expr))),
),
),
multiplicative: () => choice("*", "/", "%"),
additive: () => choice("+", "-"),
bitshift: () => choice("<<", ">>"),
comparison: () => choice("<", "<=", ">", ">=", "in"),
equality: () => choice("==", "!="),
bitand: () => "&",
bitxor: () => "^",
bitor: () => "|",
and: () => "&&",
or: () => "||",
binary: ($) => {
const table = [
[PREC.multiplicative, $.multiplicative],
[PREC.additive, $.additive],
[PREC.bitshift, $.bitshift],
[PREC.comparison, $.comparison],
[PREC.equality, $.equality],
[PREC.bitand, $.bitand],
[PREC.bitxor, $.bitxor],
[PREC.bitor, $.bitor],
[PREC.and, $.and],
[PREC.or, $.or],
];
return choice(
...table.map(([precedence, operator]) =>
prec.left(
precedence,
seq(
field("left", $._expr),
field("operator", operator),
field("right", $._expr),
),
),
),
);
},
unary: ($) =>
prec(
PREC.unary,
seq(field("operator", $.unaryop), field("argument", $._expr)),
),
unaryop: () => choice("-", "+", "!", "~"),
implicit_plus: ($) => seq($._expr, $.object),
anonymous_function: ($) =>
prec.right(
seq(
"function",
"(",
optional(field("params", $.params)),
")",
field("body", $._expr),
),
),
_assert_expr: ($) => prec.right(seq($.assert, ";", $._expr)),
// import string
import: ($) => seq("import", $.string),
// importstr string
importstr: ($) => seq("importstr", $.string),
// error expr
error: ($) => prec.right(seq("error", $._expr)),
in_super: ($) => prec(PREC.comparison, seq($._expr, "in", $.super)),
parenthesis: ($) => seq("(", $._expr, ")"),
objinside: ($) =>
choice(
// seq($.member, repeat(seq(",", $.member)), optional(",")),
commaSep1($.member, true),
$.objforloop,
),
objforloop: ($) =>
seq(
repeat(seq($.objlocal, ",")),
$.field,
repeat(seq(",", $.objlocal)),
optional(","),
$.forspec,
optional($.compspec),
),
member: ($) =>
prec.right(PREC.member, choice($.objlocal, $.assert, $.field)),
field: ($) =>
choice(
seq($.fieldname, optional("+"), $.h, $._expr),
seq(
field("function", $.fieldname),
"(",
optional($.params),
")",
$.h,
$._expr,
),
),
h: () => choice(":", "::", ":::"),
objlocal: ($) => seq($.local, $.bind),
compspec: ($) => repeat1(choice($.forspec, $.ifspec)),
forspec: ($) => seq("for", $.id, "in", $._expr),
ifspec: ($) => seq("if", $._expr),
fieldname: ($) =>
prec.right(choice($.id, $.string, seq("[", $._expr, "]"))),
// assert in objects
assert: ($) => seq("assert", $._expr, optional(seq(":", $._expr))),
bind: ($) =>
choice(
seq($.id, "=", $._expr),
prec.right(
seq(
field("function", $.id),
"(",
optional(field("params", $.params)),
")",
"=",
field("body", $._expr),
),
),
),
args: ($) =>
choice(
seq(
$._expr,
repeat(seq(",", $._expr)),
repeat(seq(",", $.named_argument)),
optional(","),
),
seq(
$.named_argument,
repeat(seq(",", $.named_argument)),
optional(","),
),
),
named_argument: ($) => seq($.id, "=", $._expr),
params: ($) => commaSep1($.param, true),
param: ($) =>
seq(
field("identifier", $.id),
optional(seq("=", field("value", $._expr))),
),
// COPIED FROM: tree-sitter-json
_number: () => {
const hex_literal = seq(choice("0x", "0X"), /[\da-fA-F]+/);
const decimal_digits = /\d+/;
const signed_integer = seq(
optional(choice("-", "+")),
decimal_digits,
);
const exponent_part = seq(choice("e", "E"), signed_integer);
const binary_literal = seq(choice("0b", "0B"), /[0-1]+/);
const octal_literal = seq(choice("0o", "0O"), /[0-7]+/);
const decimal_integer_literal = seq(
optional(choice("-", "+")),
choice("0", seq(/[1-9]/, optional(decimal_digits))),
);
const decimal_literal = choice(
seq(
decimal_integer_literal,
".",
optional(decimal_digits),
optional(exponent_part),
),
seq(".", decimal_digits, optional(exponent_part)),
seq(decimal_integer_literal, optional(exponent_part)),
);
return token(
choice(
hex_literal,
decimal_literal,
binary_literal,
octal_literal,
),
);
},
_string: ($) =>
choice(
// Single Quotes
seq(
optional("@"),
alias($._single, $.string_start),
alias($._single, $.string_end),
),
seq(
optional("@"),
alias($._single, $.string_start),
alias($._str_single, $.string_content),
alias($._single, $.string_end),
),
// Double Quotes
seq(
optional("@"),
alias($._double, $.string_start),
alias($._double, $.string_end),
),
seq(
optional("@"),
alias($._double, $.string_start),
alias($._str_double, $.string_content),
alias($._double, $.string_end),
),
// ||| Quotes
seq(
optional("@"),
alias($._string_start, $.string_start),
alias($._string_content, $.string_content),
alias($._string_end, $.string_end),
),
),
_single: () => "'",
_double: () => '"',
_str_double: ($) =>
repeat1(
choice(
token.immediate(prec(1, /[^\\"\n]+/)),
$.escape_sequence,
),
),
_str_single: ($) =>
repeat1(
choice(
token.immediate(prec(1, /[^\\'\n]+/)),
$.escape_sequence,
),
),
escape_sequence: () =>
token.immediate(seq("\\", /(\"|\\|\/|b|f|n|r|t|u)/)),
},
});
function commaSep1(rule, trailing) {
if (trailing) {
return seq(rule, repeat(seq(",", rule)), optional(","));
} else {
return seq(rule, repeat(seq(",", rule)));
}
}
function commaSep(rule, trailing) {
return optional(commaSep1(rule, trailing));
}