@apple/tree-sitter-pkl 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CODE_OF_CONDUCT.adoc +78 -0
- package/CONTRIBUTING.adoc +52 -0
- package/Cargo.lock +80 -0
- package/Cargo.toml +29 -0
- package/LICENSE.txt +202 -0
- package/MAINTAINERS.adoc +11 -0
- package/NOTICE.txt +4 -0
- package/README.adoc +61 -0
- package/SECURITY.adoc +13 -0
- package/binding.gyp +19 -0
- package/bindings/node/binding.cc +28 -0
- package/bindings/node/index.js +19 -0
- package/grammar.js +779 -0
- package/package.json +51 -0
- package/queries/highlights.scm +138 -0
- package/queries/injections.scm +9 -0
- package/queries/locals.scm +38 -0
- package/src/grammar.json +3962 -0
- package/src/node-types.json +5668 -0
- package/src/parser.c +59696 -0
- package/src/scanner.c +285 -0
- package/src/synctests.ts +44 -0
- package/src/tree_sitter/parser.h +224 -0
- package/tsconfig.json +101 -0
package/grammar.js
ADDED
|
@@ -0,0 +1,779 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/// <reference path="node_modules/tree-sitter-cli/dsl.d.ts" />
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Operator precedences
|
|
20
|
+
*/
|
|
21
|
+
const PREC = {
|
|
22
|
+
NON_NULL: 21,
|
|
23
|
+
NEG: 20,
|
|
24
|
+
NOT: 19,
|
|
25
|
+
EXP: 18,
|
|
26
|
+
MUL: 17,
|
|
27
|
+
ADD: 16,
|
|
28
|
+
REL: 15,
|
|
29
|
+
IS: 14,
|
|
30
|
+
AS: 13,
|
|
31
|
+
EQ: 12,
|
|
32
|
+
AND: 11,
|
|
33
|
+
OR: 10,
|
|
34
|
+
PIPE: 9,
|
|
35
|
+
COALESCE: 8,
|
|
36
|
+
|
|
37
|
+
IF: -4,
|
|
38
|
+
LET: -5,
|
|
39
|
+
THROW: -6,
|
|
40
|
+
TRACE: -7,
|
|
41
|
+
READ: -8,
|
|
42
|
+
READ_OR_NULL: -9,
|
|
43
|
+
READ_GLOB: -10,
|
|
44
|
+
FUN: -11,
|
|
45
|
+
|
|
46
|
+
NULLABLE_TYPE: 5,
|
|
47
|
+
FUN_TYPE: -5,
|
|
48
|
+
UNION_TYPE: -6,
|
|
49
|
+
DEFAULT_TYPE: -7,
|
|
50
|
+
|
|
51
|
+
VAR_OBJ_LITERAL: 2,
|
|
52
|
+
OBJ_LITERAL: 1,
|
|
53
|
+
OBJ_MEMBER: -1
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const decimalLiteral = seq(/\d/, /[\d_]*/);
|
|
57
|
+
|
|
58
|
+
module.exports = grammar({
|
|
59
|
+
name: 'pkl',
|
|
60
|
+
|
|
61
|
+
externals: $ => [
|
|
62
|
+
$._sl1_string_chars,
|
|
63
|
+
$._sl2_string_chars,
|
|
64
|
+
$._sl3_string_chars,
|
|
65
|
+
$._sl4_string_chars,
|
|
66
|
+
$._sl5_string_chars,
|
|
67
|
+
$._sl6_string_chars,
|
|
68
|
+
$._ml_string_chars,
|
|
69
|
+
$._ml1_string_chars,
|
|
70
|
+
$._ml2_string_chars,
|
|
71
|
+
$._ml3_string_chars,
|
|
72
|
+
$._ml4_string_chars,
|
|
73
|
+
$._ml5_string_chars,
|
|
74
|
+
$._ml6_string_chars,
|
|
75
|
+
$._open_square_bracket,
|
|
76
|
+
$._open_entry_bracket,
|
|
77
|
+
],
|
|
78
|
+
|
|
79
|
+
extras: $ => [
|
|
80
|
+
$.lineComment,
|
|
81
|
+
$.docComment,
|
|
82
|
+
$.blockComment,
|
|
83
|
+
/[ \t\f\r\n;]/
|
|
84
|
+
],
|
|
85
|
+
|
|
86
|
+
word: $ => $.identifier,
|
|
87
|
+
|
|
88
|
+
conflicts: $ => [
|
|
89
|
+
// in ANTLR we deal with these by not allowing a newline or semicolon before subscript
|
|
90
|
+
[$.objectProperty, $.subscriptExpr],
|
|
91
|
+
[$.objectMethod, $.subscriptExpr],
|
|
92
|
+
[$.objectEntry, $.subscriptExpr],
|
|
93
|
+
[$.objectPredicate, $.subscriptExpr],
|
|
94
|
+
|
|
95
|
+
// these should be fixable in some other way (perhaps with prec)
|
|
96
|
+
[$.propertyCallExpr, $.methodCallExpr],
|
|
97
|
+
[$.variableExpr, $.methodCallExpr],
|
|
98
|
+
[$.variableExpr, $.typedIdentifier],
|
|
99
|
+
|
|
100
|
+
// not sure what this one is about
|
|
101
|
+
[$.objectElement, $._expr],
|
|
102
|
+
|
|
103
|
+
[$.qualifiedIdentifier],
|
|
104
|
+
[$.type]
|
|
105
|
+
],
|
|
106
|
+
|
|
107
|
+
rules: {
|
|
108
|
+
module: $ => seq(
|
|
109
|
+
optional($.moduleHeader),
|
|
110
|
+
repeat(choice($.importClause, $.importGlobClause)),
|
|
111
|
+
repeat($._moduleMember)
|
|
112
|
+
),
|
|
113
|
+
|
|
114
|
+
moduleHeader: $ => seq(
|
|
115
|
+
repeat($.annotation),
|
|
116
|
+
choice(
|
|
117
|
+
seq($.moduleClause, optional($.extendsOrAmendsClause)),
|
|
118
|
+
$.extendsOrAmendsClause
|
|
119
|
+
),
|
|
120
|
+
),
|
|
121
|
+
|
|
122
|
+
moduleClause: $ => seq(
|
|
123
|
+
repeat($.modifier),
|
|
124
|
+
"module",
|
|
125
|
+
$.qualifiedIdentifier
|
|
126
|
+
),
|
|
127
|
+
|
|
128
|
+
extendsOrAmendsClause: $ => seq(
|
|
129
|
+
choice("extends", "amends"),
|
|
130
|
+
$.stringConstant
|
|
131
|
+
),
|
|
132
|
+
|
|
133
|
+
importClause: $ => seq(
|
|
134
|
+
"import",
|
|
135
|
+
$.stringConstant,
|
|
136
|
+
optional(seq("as", $.identifier))
|
|
137
|
+
),
|
|
138
|
+
|
|
139
|
+
importGlobClause: $ => seq(
|
|
140
|
+
"import*",
|
|
141
|
+
$.stringConstant,
|
|
142
|
+
optional(seq("as", $.identifier))
|
|
143
|
+
),
|
|
144
|
+
|
|
145
|
+
_moduleMember: $ => choice(
|
|
146
|
+
$.clazz,
|
|
147
|
+
$.typeAlias,
|
|
148
|
+
$.classProperty,
|
|
149
|
+
$.classMethod
|
|
150
|
+
),
|
|
151
|
+
|
|
152
|
+
clazz: $ => seq(
|
|
153
|
+
repeat($.annotation),
|
|
154
|
+
repeat($.modifier),
|
|
155
|
+
"class",
|
|
156
|
+
$.identifier,
|
|
157
|
+
optional($.typeParameterList),
|
|
158
|
+
optional($.classExtendsClause),
|
|
159
|
+
optional($.classBody)
|
|
160
|
+
),
|
|
161
|
+
|
|
162
|
+
classExtendsClause: $ => seq(
|
|
163
|
+
"extends",
|
|
164
|
+
$.qualifiedIdentifier,
|
|
165
|
+
optional($.typeArgumentList)
|
|
166
|
+
),
|
|
167
|
+
|
|
168
|
+
classBody: $ => seq(
|
|
169
|
+
"{",
|
|
170
|
+
repeat(choice($.classProperty, $.classMethod)),
|
|
171
|
+
"}"
|
|
172
|
+
),
|
|
173
|
+
|
|
174
|
+
typeAlias: $ => seq(
|
|
175
|
+
repeat($.annotation),
|
|
176
|
+
repeat($.modifier),
|
|
177
|
+
"typealias",
|
|
178
|
+
$.identifier,
|
|
179
|
+
optional($.typeParameterList),
|
|
180
|
+
"=",
|
|
181
|
+
$.type
|
|
182
|
+
),
|
|
183
|
+
|
|
184
|
+
classProperty: $ => seq(
|
|
185
|
+
repeat($.annotation),
|
|
186
|
+
repeat($.modifier),
|
|
187
|
+
$.identifier,
|
|
188
|
+
choice(
|
|
189
|
+
$.typeAnnotation,
|
|
190
|
+
seq(
|
|
191
|
+
optional($.typeAnnotation),
|
|
192
|
+
choice(
|
|
193
|
+
seq("=", $._expr),
|
|
194
|
+
repeat1($.objectBody)
|
|
195
|
+
)
|
|
196
|
+
)
|
|
197
|
+
)
|
|
198
|
+
),
|
|
199
|
+
|
|
200
|
+
classMethod: $ => seq(
|
|
201
|
+
repeat($.annotation),
|
|
202
|
+
$.methodHeader,
|
|
203
|
+
optional(seq("=", $._expr))
|
|
204
|
+
),
|
|
205
|
+
|
|
206
|
+
methodHeader: $ => seq(
|
|
207
|
+
repeat($.modifier),
|
|
208
|
+
"function",
|
|
209
|
+
$.identifier,
|
|
210
|
+
optional($.typeParameterList),
|
|
211
|
+
$.parameterList,
|
|
212
|
+
optional($.typeAnnotation)
|
|
213
|
+
),
|
|
214
|
+
|
|
215
|
+
annotation: $ => seq(
|
|
216
|
+
"@",
|
|
217
|
+
$.qualifiedIdentifier,
|
|
218
|
+
optional($.objectBody)
|
|
219
|
+
),
|
|
220
|
+
|
|
221
|
+
objectBody: $ => seq(
|
|
222
|
+
"{",
|
|
223
|
+
optional($.objectBodyParameters),
|
|
224
|
+
repeat($._objectMember),
|
|
225
|
+
"}"
|
|
226
|
+
),
|
|
227
|
+
|
|
228
|
+
_objectMember: $ => prec(PREC.OBJ_MEMBER, choice(
|
|
229
|
+
$.objectProperty,
|
|
230
|
+
$.objectMethod,
|
|
231
|
+
$.objectEntry,
|
|
232
|
+
$.objectElement,
|
|
233
|
+
$.objectPredicate,
|
|
234
|
+
$.forGenerator,
|
|
235
|
+
$.whenGenerator,
|
|
236
|
+
$.objectSpread,
|
|
237
|
+
)),
|
|
238
|
+
|
|
239
|
+
objectProperty: $ => seq(
|
|
240
|
+
repeat($.modifier),
|
|
241
|
+
$.identifier,
|
|
242
|
+
choice(
|
|
243
|
+
seq(
|
|
244
|
+
optional($.typeAnnotation),
|
|
245
|
+
"=",
|
|
246
|
+
$._expr
|
|
247
|
+
),
|
|
248
|
+
repeat1($.objectBody)
|
|
249
|
+
)
|
|
250
|
+
),
|
|
251
|
+
|
|
252
|
+
objectMethod: $ => seq($.methodHeader, "=", $._expr),
|
|
253
|
+
|
|
254
|
+
objectEntry: $ => seq(
|
|
255
|
+
$._open_entry_bracket,
|
|
256
|
+
$._expr,
|
|
257
|
+
"]",
|
|
258
|
+
choice(
|
|
259
|
+
seq("=", $._expr),
|
|
260
|
+
repeat1($.objectBody)
|
|
261
|
+
)
|
|
262
|
+
),
|
|
263
|
+
|
|
264
|
+
objectElement: $ => choice($.variableExpr, $._expr2),
|
|
265
|
+
|
|
266
|
+
objectPredicate: $ => seq(
|
|
267
|
+
"[[",
|
|
268
|
+
$._expr,
|
|
269
|
+
"]]",
|
|
270
|
+
choice(
|
|
271
|
+
seq("=", $._expr),
|
|
272
|
+
repeat1($.objectBody)
|
|
273
|
+
)
|
|
274
|
+
),
|
|
275
|
+
|
|
276
|
+
forGenerator: $ => seq(
|
|
277
|
+
"for",
|
|
278
|
+
"(",
|
|
279
|
+
$.typedIdentifier,
|
|
280
|
+
optional(seq(
|
|
281
|
+
",",
|
|
282
|
+
$.typedIdentifier
|
|
283
|
+
)),
|
|
284
|
+
"in",
|
|
285
|
+
$._expr,
|
|
286
|
+
")",
|
|
287
|
+
choice(
|
|
288
|
+
$.objectBody,
|
|
289
|
+
$._objectMember // deprecated in 0.15
|
|
290
|
+
)
|
|
291
|
+
),
|
|
292
|
+
|
|
293
|
+
whenGenerator: $ => seq(
|
|
294
|
+
"when",
|
|
295
|
+
"(",
|
|
296
|
+
$._expr,
|
|
297
|
+
")",
|
|
298
|
+
choice(
|
|
299
|
+
$.objectBody,
|
|
300
|
+
$._objectMember // deprecated in 0.15
|
|
301
|
+
)
|
|
302
|
+
),
|
|
303
|
+
|
|
304
|
+
objectSpread: $ => seq(
|
|
305
|
+
choice(
|
|
306
|
+
"...",
|
|
307
|
+
"...?"
|
|
308
|
+
),
|
|
309
|
+
$._expr
|
|
310
|
+
),
|
|
311
|
+
|
|
312
|
+
objectBodyParameters: $ => seq(
|
|
313
|
+
commaSep1($.typedIdentifier),
|
|
314
|
+
"->"
|
|
315
|
+
),
|
|
316
|
+
|
|
317
|
+
typeAnnotation: $ => seq(":", $.type),
|
|
318
|
+
|
|
319
|
+
type: $ => choice(
|
|
320
|
+
"unknown",
|
|
321
|
+
"nothing",
|
|
322
|
+
$.stringConstant,
|
|
323
|
+
seq($.qualifiedIdentifier, optional($.typeArgumentList)),
|
|
324
|
+
seq("(", $.type, ")"),
|
|
325
|
+
prec(PREC.NULLABLE_TYPE, seq($.type, "?")),
|
|
326
|
+
seq($.type, "(", commaSep1($._expr), ")"),
|
|
327
|
+
prec.left(PREC.UNION_TYPE, seq($.type, "|", $.type)),
|
|
328
|
+
prec(PREC.DEFAULT_TYPE, seq("*", $.type)),
|
|
329
|
+
prec(PREC.FUN_TYPE, seq("(", commaSep($.type), ")", "->", $.type))
|
|
330
|
+
),
|
|
331
|
+
|
|
332
|
+
typeArgumentList: $ => seq(
|
|
333
|
+
"<",
|
|
334
|
+
commaSep1($.type),
|
|
335
|
+
">"
|
|
336
|
+
),
|
|
337
|
+
|
|
338
|
+
typeParameterList: $ => seq(
|
|
339
|
+
"<",
|
|
340
|
+
commaSep1($.typeParameter),
|
|
341
|
+
">"
|
|
342
|
+
),
|
|
343
|
+
|
|
344
|
+
typeParameter: $ => seq(
|
|
345
|
+
optional(choice("in", "out")),
|
|
346
|
+
$.identifier
|
|
347
|
+
),
|
|
348
|
+
|
|
349
|
+
parameterList: $ => seq(
|
|
350
|
+
'(',
|
|
351
|
+
commaSep($.typedIdentifier),
|
|
352
|
+
')'
|
|
353
|
+
),
|
|
354
|
+
|
|
355
|
+
argumentList: $ => seq(
|
|
356
|
+
'(',
|
|
357
|
+
commaSep($._expr),
|
|
358
|
+
')'
|
|
359
|
+
),
|
|
360
|
+
|
|
361
|
+
modifier: $ => choice(
|
|
362
|
+
"external",
|
|
363
|
+
"abstract",
|
|
364
|
+
"open",
|
|
365
|
+
"local",
|
|
366
|
+
"hidden",
|
|
367
|
+
"fixed",
|
|
368
|
+
"const"
|
|
369
|
+
),
|
|
370
|
+
|
|
371
|
+
_expr: $ => choice(
|
|
372
|
+
$.variableExpr,
|
|
373
|
+
$.variableObjectLiteral,
|
|
374
|
+
$._expr2
|
|
375
|
+
),
|
|
376
|
+
|
|
377
|
+
variableObjectLiteral: $ => prec(PREC.VAR_OBJ_LITERAL, seq(
|
|
378
|
+
$.identifier,
|
|
379
|
+
$.objectBody
|
|
380
|
+
)),
|
|
381
|
+
|
|
382
|
+
_expr2: $ => choice(
|
|
383
|
+
$.parenthesizedExpr,
|
|
384
|
+
$.thisExpr,
|
|
385
|
+
$.outerExpr,
|
|
386
|
+
$.moduleExpr,
|
|
387
|
+
$.nullLiteral,
|
|
388
|
+
$.trueLiteral,
|
|
389
|
+
$.falseLiteral,
|
|
390
|
+
$.intLiteral,
|
|
391
|
+
$.floatLiteral,
|
|
392
|
+
$.slStringLiteral,
|
|
393
|
+
$.mlStringLiteral,
|
|
394
|
+
$.newExpr,
|
|
395
|
+
$.objectLiteral,
|
|
396
|
+
$.methodCallExpr,
|
|
397
|
+
$.propertyCallExpr,
|
|
398
|
+
$.subscriptExpr,
|
|
399
|
+
$.unaryExpr,
|
|
400
|
+
$.binaryExpr,
|
|
401
|
+
$.binaryExprRightAssoc,
|
|
402
|
+
$.isExpr,
|
|
403
|
+
$.asExpr,
|
|
404
|
+
$.ifExpr,
|
|
405
|
+
$.letExpr,
|
|
406
|
+
$.throwExpr,
|
|
407
|
+
$.traceExpr,
|
|
408
|
+
$.readExpr,
|
|
409
|
+
$.readOrNullExpr,
|
|
410
|
+
$.readGlobExpr,
|
|
411
|
+
$.importExpr,
|
|
412
|
+
$.importGlobExpr,
|
|
413
|
+
$.functionLiteral
|
|
414
|
+
),
|
|
415
|
+
|
|
416
|
+
parenthesizedExpr: $ => seq("(", $._expr, ")"),
|
|
417
|
+
|
|
418
|
+
thisExpr: $ => "this",
|
|
419
|
+
|
|
420
|
+
outerExpr: $ => "outer",
|
|
421
|
+
|
|
422
|
+
moduleExpr: $ => "module",
|
|
423
|
+
|
|
424
|
+
variableExpr: $ => $.identifier,
|
|
425
|
+
|
|
426
|
+
nullLiteral: $ => "null",
|
|
427
|
+
|
|
428
|
+
trueLiteral: $ => "true",
|
|
429
|
+
|
|
430
|
+
falseLiteral: $ => "false",
|
|
431
|
+
|
|
432
|
+
intLiteral: $ => {
|
|
433
|
+
return token(choice(
|
|
434
|
+
decimalLiteral,
|
|
435
|
+
seq('0x', /[\da-fA-F]/, /[\da-fA-F_]*/),
|
|
436
|
+
seq('0b', /[0-1]/, /[0-1_]*/),
|
|
437
|
+
seq('0o', /[0-7]/, /[0-7_]*/)
|
|
438
|
+
))
|
|
439
|
+
},
|
|
440
|
+
|
|
441
|
+
floatLiteral: $ => {
|
|
442
|
+
const exponent = seq(choice('e', 'E'), optional(choice('+', '-')), decimalLiteral)
|
|
443
|
+
return token(choice(
|
|
444
|
+
seq(optional(decimalLiteral), '.', decimalLiteral, optional(exponent)),
|
|
445
|
+
seq(decimalLiteral, exponent)
|
|
446
|
+
))
|
|
447
|
+
},
|
|
448
|
+
|
|
449
|
+
stringConstant: $ => choice(
|
|
450
|
+
seq(
|
|
451
|
+
'"',
|
|
452
|
+
repeat(choice(
|
|
453
|
+
token.immediate(/[^"\\\n\r]+/),
|
|
454
|
+
$.escapeSequence
|
|
455
|
+
)),
|
|
456
|
+
'"'
|
|
457
|
+
),
|
|
458
|
+
seq(
|
|
459
|
+
'#"',
|
|
460
|
+
repeat(choice(
|
|
461
|
+
$._sl1_string_chars,
|
|
462
|
+
alias($.escapeSequence1, $.escapeSequence),
|
|
463
|
+
)),
|
|
464
|
+
'"#'
|
|
465
|
+
)
|
|
466
|
+
),
|
|
467
|
+
|
|
468
|
+
slStringLiteral: $ => choice(
|
|
469
|
+
seq(
|
|
470
|
+
'"',
|
|
471
|
+
repeat(choice(
|
|
472
|
+
token.immediate(/[^"\\\n\r]+/),
|
|
473
|
+
$.escapeSequence,
|
|
474
|
+
$.interpolationExpr,
|
|
475
|
+
)),
|
|
476
|
+
'"'
|
|
477
|
+
),
|
|
478
|
+
seq(
|
|
479
|
+
'#"',
|
|
480
|
+
repeat(choice(
|
|
481
|
+
$._sl1_string_chars,
|
|
482
|
+
alias($.escapeSequence1, $.escapeSequence),
|
|
483
|
+
alias($.interpolationExpr1, $.interpolationExpr)
|
|
484
|
+
)),
|
|
485
|
+
'"#'
|
|
486
|
+
),
|
|
487
|
+
seq(
|
|
488
|
+
'##"',
|
|
489
|
+
repeat(choice(
|
|
490
|
+
$._sl2_string_chars,
|
|
491
|
+
alias($.escapeSequence2, $.escapeSequence),
|
|
492
|
+
alias($.interpolationExpr2, $.interpolationExpr)
|
|
493
|
+
)),
|
|
494
|
+
'"##'
|
|
495
|
+
),
|
|
496
|
+
seq(
|
|
497
|
+
'###"',
|
|
498
|
+
repeat(choice(
|
|
499
|
+
$._sl3_string_chars,
|
|
500
|
+
alias($.escapeSequence3, $.escapeSequence),
|
|
501
|
+
alias($.interpolationExpr3, $.interpolationExpr)
|
|
502
|
+
)),
|
|
503
|
+
'"###'
|
|
504
|
+
),
|
|
505
|
+
seq(
|
|
506
|
+
'####"',
|
|
507
|
+
repeat(choice(
|
|
508
|
+
$._sl4_string_chars,
|
|
509
|
+
alias($.escapeSequence4, $.escapeSequence),
|
|
510
|
+
alias($.interpolationExpr4, $.interpolationExpr)
|
|
511
|
+
)),
|
|
512
|
+
'"####'
|
|
513
|
+
),
|
|
514
|
+
seq(
|
|
515
|
+
'#####"',
|
|
516
|
+
repeat(choice(
|
|
517
|
+
$._sl5_string_chars,
|
|
518
|
+
alias($.escapeSequence5, $.escapeSequence),
|
|
519
|
+
alias($.interpolationExpr5, $.interpolationExpr)
|
|
520
|
+
)),
|
|
521
|
+
'"#####'
|
|
522
|
+
),
|
|
523
|
+
seq(
|
|
524
|
+
'######"',
|
|
525
|
+
repeat(choice(
|
|
526
|
+
$._sl6_string_chars,
|
|
527
|
+
alias($.escapeSequence6, $.escapeSequence),
|
|
528
|
+
alias($.interpolationExpr6, $.interpolationExpr)
|
|
529
|
+
)),
|
|
530
|
+
'"######'
|
|
531
|
+
),
|
|
532
|
+
),
|
|
533
|
+
|
|
534
|
+
mlStringLiteral: $ => choice(
|
|
535
|
+
seq(
|
|
536
|
+
'"""',
|
|
537
|
+
repeat(choice(
|
|
538
|
+
$._ml_string_chars,
|
|
539
|
+
$.escapeSequence,
|
|
540
|
+
$.interpolationExpr
|
|
541
|
+
)),
|
|
542
|
+
'"""'
|
|
543
|
+
),
|
|
544
|
+
seq(
|
|
545
|
+
'#"""',
|
|
546
|
+
repeat(choice(
|
|
547
|
+
$._ml1_string_chars,
|
|
548
|
+
alias($.escapeSequence1, $.escapeSequence),
|
|
549
|
+
alias($.interpolationExpr1, $.interpolationExpr)
|
|
550
|
+
)),
|
|
551
|
+
'"""#'
|
|
552
|
+
),
|
|
553
|
+
seq(
|
|
554
|
+
'##"""',
|
|
555
|
+
repeat(choice(
|
|
556
|
+
$._ml2_string_chars,
|
|
557
|
+
alias($.escapeSequence2, $.escapeSequence),
|
|
558
|
+
alias($.interpolationExpr2, $.interpolationExpr)
|
|
559
|
+
)),
|
|
560
|
+
'"""##'
|
|
561
|
+
),
|
|
562
|
+
seq(
|
|
563
|
+
'###"""',
|
|
564
|
+
repeat(choice(
|
|
565
|
+
$._ml3_string_chars,
|
|
566
|
+
alias($.escapeSequence3, $.escapeSequence),
|
|
567
|
+
alias($.interpolationExpr3, $.interpolationExpr)
|
|
568
|
+
)),
|
|
569
|
+
'"""###'
|
|
570
|
+
),
|
|
571
|
+
seq(
|
|
572
|
+
'####"""',
|
|
573
|
+
repeat(choice(
|
|
574
|
+
$._ml4_string_chars,
|
|
575
|
+
alias($.escapeSequence4, $.escapeSequence),
|
|
576
|
+
alias($.interpolationExpr4, $.interpolationExpr)
|
|
577
|
+
)),
|
|
578
|
+
'"""####'
|
|
579
|
+
),
|
|
580
|
+
seq(
|
|
581
|
+
'#####"""',
|
|
582
|
+
repeat(choice(
|
|
583
|
+
$._ml5_string_chars,
|
|
584
|
+
alias($.escapeSequence5, $.escapeSequence),
|
|
585
|
+
alias($.interpolationExpr5, $.interpolationExpr)
|
|
586
|
+
)),
|
|
587
|
+
'"""#####'
|
|
588
|
+
),
|
|
589
|
+
seq(
|
|
590
|
+
'######"""',
|
|
591
|
+
repeat(choice(
|
|
592
|
+
$._ml6_string_chars,
|
|
593
|
+
alias($.escapeSequence6, $.escapeSequence),
|
|
594
|
+
alias($.interpolationExpr6, $.interpolationExpr)
|
|
595
|
+
)),
|
|
596
|
+
'"""######'
|
|
597
|
+
),
|
|
598
|
+
),
|
|
599
|
+
|
|
600
|
+
escapeSequence: $ => token.immediate(seq(
|
|
601
|
+
'\\',
|
|
602
|
+
choice(
|
|
603
|
+
/[tnr\\"]/,
|
|
604
|
+
/u\{[0-9a-fA-F]+\}/
|
|
605
|
+
)
|
|
606
|
+
)),
|
|
607
|
+
|
|
608
|
+
escapeSequence1: $ => token.immediate(seq(
|
|
609
|
+
'\\#',
|
|
610
|
+
choice(
|
|
611
|
+
/[tnr\\"]/,
|
|
612
|
+
/u\{[0-9a-fA-F]+\}/
|
|
613
|
+
)
|
|
614
|
+
)),
|
|
615
|
+
|
|
616
|
+
escapeSequence2: $ => token.immediate(seq(
|
|
617
|
+
'\\##',
|
|
618
|
+
choice(
|
|
619
|
+
/[tnr\\"]/,
|
|
620
|
+
/u\{[0-9a-fA-F]+\}/
|
|
621
|
+
)
|
|
622
|
+
)),
|
|
623
|
+
|
|
624
|
+
escapeSequence3: $ => token.immediate(seq(
|
|
625
|
+
'\\###',
|
|
626
|
+
choice(
|
|
627
|
+
/[tnr\\"]/,
|
|
628
|
+
/u\{[0-9a-fA-F]+\}/
|
|
629
|
+
)
|
|
630
|
+
)),
|
|
631
|
+
|
|
632
|
+
escapeSequence4: $ => token.immediate(seq(
|
|
633
|
+
'\\####',
|
|
634
|
+
choice(
|
|
635
|
+
/[tnr\\"]/,
|
|
636
|
+
/u\{[0-9a-fA-F]+\}/
|
|
637
|
+
)
|
|
638
|
+
)),
|
|
639
|
+
|
|
640
|
+
escapeSequence5: $ => token.immediate(seq(
|
|
641
|
+
'\\#####',
|
|
642
|
+
choice(
|
|
643
|
+
/[tnr\\"]/,
|
|
644
|
+
/u\{[0-9a-fA-F]+\}/
|
|
645
|
+
)
|
|
646
|
+
)),
|
|
647
|
+
|
|
648
|
+
escapeSequence6: $ => token.immediate(seq(
|
|
649
|
+
'\\######',
|
|
650
|
+
choice(
|
|
651
|
+
/[tnr\\"]/,
|
|
652
|
+
/u\{[0-9a-fA-F]+\}/
|
|
653
|
+
)
|
|
654
|
+
)),
|
|
655
|
+
|
|
656
|
+
interpolationExpr: $ => seq(token.immediate("\\("), $._expr, ")"),
|
|
657
|
+
|
|
658
|
+
interpolationExpr1: $ => seq(token.immediate("\\#("), $._expr, ")"),
|
|
659
|
+
|
|
660
|
+
interpolationExpr2: $ => seq(token.immediate("\\##("), $._expr, ")"),
|
|
661
|
+
|
|
662
|
+
interpolationExpr3: $ => seq(token.immediate("\\###("), $._expr, ")"),
|
|
663
|
+
|
|
664
|
+
interpolationExpr4: $ => seq(token.immediate("\\####("), $._expr, ")"),
|
|
665
|
+
|
|
666
|
+
interpolationExpr5: $ => seq(token.immediate("\\#####("), $._expr, ")"),
|
|
667
|
+
|
|
668
|
+
interpolationExpr6: $ => seq(token.immediate("\\######("), $._expr, ")"),
|
|
669
|
+
|
|
670
|
+
newExpr: $ => seq("new", optional($.type), $.objectBody),
|
|
671
|
+
|
|
672
|
+
objectLiteral: $ => prec(PREC.OBJ_LITERAL, seq($._expr2, $.objectBody)),
|
|
673
|
+
|
|
674
|
+
methodCallExpr: $ => seq(optional(seq(choice("super", $._expr), choice(".", "?."))), $.identifier, $.argumentList),
|
|
675
|
+
|
|
676
|
+
propertyCallExpr: $ => seq(choice("super", $._expr), choice(".", "?."), $.identifier),
|
|
677
|
+
|
|
678
|
+
subscriptExpr: $ => seq(choice("super", $._expr), $._open_square_bracket, $._expr, "]"),
|
|
679
|
+
|
|
680
|
+
unaryExpr: $ => choice(
|
|
681
|
+
prec.left(PREC.NEG, seq($._expr, '!!')),
|
|
682
|
+
prec.left(PREC.NEG, seq('-', $._expr)),
|
|
683
|
+
prec.left(PREC.NOT, seq('!', $._expr)),
|
|
684
|
+
),
|
|
685
|
+
|
|
686
|
+
binaryExprRightAssoc: $ => choice(...[
|
|
687
|
+
['**', PREC.EXP],
|
|
688
|
+
['??', PREC.COALESCE]
|
|
689
|
+
].map(([operator, precedence]) =>
|
|
690
|
+
prec.right(precedence, seq($._expr, operator, $._expr))
|
|
691
|
+
)),
|
|
692
|
+
|
|
693
|
+
binaryExpr: $ => choice(...[
|
|
694
|
+
['*', PREC.MUL],
|
|
695
|
+
['/', PREC.MUL],
|
|
696
|
+
['~/', PREC.MUL],
|
|
697
|
+
['%', PREC.MUL],
|
|
698
|
+
['+', PREC.ADD],
|
|
699
|
+
['-', PREC.ADD],
|
|
700
|
+
['<', PREC.REL],
|
|
701
|
+
['<=', PREC.REL],
|
|
702
|
+
['>=', PREC.REL],
|
|
703
|
+
['>', PREC.REL],
|
|
704
|
+
['==', PREC.EQ],
|
|
705
|
+
['!=', PREC.EQ],
|
|
706
|
+
['&&', PREC.AND],
|
|
707
|
+
['||', PREC.OR],
|
|
708
|
+
['|>', PREC.PIPE]
|
|
709
|
+
].map(([operator, precedence]) =>
|
|
710
|
+
prec.left(precedence, seq($._expr, operator, $._expr))
|
|
711
|
+
)),
|
|
712
|
+
|
|
713
|
+
isExpr: $ => prec(PREC.IS, seq($._expr, "is", $.type)),
|
|
714
|
+
|
|
715
|
+
asExpr: $ => prec(PREC.IS, seq($._expr, "as", $.type)),
|
|
716
|
+
|
|
717
|
+
ifExpr: $ => prec(PREC.IF, seq("if", "(", $._expr, ")", $._expr, "else", $._expr)),
|
|
718
|
+
|
|
719
|
+
letExpr: $ => prec(PREC.LET, seq("let", "(", $.typedIdentifier, "=", $._expr, ")", $._expr)),
|
|
720
|
+
|
|
721
|
+
throwExpr: $ => prec(PREC.THROW, seq("throw", $._expr)),
|
|
722
|
+
|
|
723
|
+
traceExpr: $ => prec(PREC.TRACE, seq("trace", $._expr)),
|
|
724
|
+
|
|
725
|
+
readExpr: $ => prec(PREC.READ, seq("read", $._expr)),
|
|
726
|
+
|
|
727
|
+
readOrNullExpr: $ => prec(PREC.READ_OR_NULL, seq("read?", $._expr)),
|
|
728
|
+
|
|
729
|
+
readGlobExpr: $ => prec(PREC.READ_GLOB, seq("read*", $._expr)),
|
|
730
|
+
|
|
731
|
+
importExpr: $ => seq("import", seq('(', $.stringConstant, ')')),
|
|
732
|
+
|
|
733
|
+
importGlobExpr: $ => seq("import*", seq('(', $.stringConstant, ')')),
|
|
734
|
+
|
|
735
|
+
functionLiteral: $ => prec(PREC.FUN, seq($.parameterList, "->", $._expr)),
|
|
736
|
+
|
|
737
|
+
qualifiedIdentifier: $ => seq(
|
|
738
|
+
$.identifier,
|
|
739
|
+
repeat(seq(".", $.identifier)),
|
|
740
|
+
),
|
|
741
|
+
|
|
742
|
+
typedIdentifier: $ => seq($.identifier, optional($.typeAnnotation)),
|
|
743
|
+
|
|
744
|
+
// TODO: adapt to pkl
|
|
745
|
+
identifier: $ => {
|
|
746
|
+
const alpha = /[^\s0-9:;`"'@#.,|^&<=>+\-*/\\%?!~()\[\]{}\uFEFF\u2060\u200B\u00A0]/
|
|
747
|
+
const alpha_numeric = /[^\s:;`"'@#.,|^&<=>+\-*/\\%?!~()\[\]{}\uFEFF\u2060\u200B\u00A0]/
|
|
748
|
+
|
|
749
|
+
return token(choice(seq(alpha, repeat(alpha_numeric)), /`[^`]*`/))
|
|
750
|
+
},
|
|
751
|
+
|
|
752
|
+
lineComment: $ => token(seq('//', /.*/)),
|
|
753
|
+
|
|
754
|
+
// TODO
|
|
755
|
+
docComment: $ => token(seq('///', /.*/)),
|
|
756
|
+
|
|
757
|
+
blockComment: $ => token(seq(
|
|
758
|
+
'/*',
|
|
759
|
+
/[^*]*\*+([^/*][^*]*\*+)*/,
|
|
760
|
+
'/'
|
|
761
|
+
))
|
|
762
|
+
}
|
|
763
|
+
});
|
|
764
|
+
|
|
765
|
+
function commaSep1 (rule) {
|
|
766
|
+
return seq(rule, repeat(seq(',', rule)));
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
function commaSep (rule) {
|
|
770
|
+
return optional(commaSep1(rule));
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
function sepBy (sep, rule) {
|
|
774
|
+
return optional(sepBy1(sep, rule))
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
function sepBy1 (sep, rule) {
|
|
778
|
+
return seq(rule, repeat(seq(sep, rule)));
|
|
779
|
+
}
|