-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrammar.js
398 lines (336 loc) · 9.45 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
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
// FIXME: This will be refactored after fixing grammar's edge cases.
const PREC = {
terminate: 1000,
comment: 900,
visibility_mark: 850,
escape: 800,
string: 500,
variable: 100,
colon_start: 80,
ident_fix: 51,
connection: 10,
glob: 8,
block: 5,
identifier: 3,
import: 1,
label: 1,
};
/**
* Creates a special rule for labels and identifiers, because can be pretty
* much every character _including_ spaces, which makes it difficult to parse
* with a simple set of rules or regexes.
*
* @param {RuleOrLiteral} rule
*/
const spaced_str = (rule) => choice(
rule,
seq(repeat1(seq(rule, /[ ]+/)), rule),
);
/**
* Creates a rule to match one or more occurrences of `rule` separated by
* `separator`.
*
* @param {RuleOrLiteral} rule
* @param {RuleOrLiteral} separator
* @returns {SeqRule}
*/
const repeat_sep = (rule, separator) => seq(rule, repeat(seq(separator, rule)));
/**
* Creates a rule to match at least 2 occurrences of `rule` separated by
* `separator`.
*
* @param {RuleOrLiteral} rule
* @param {RuleOrLiteral} separator
* @returns {SeqRule}
*/
const repeat_sep1 = (rule, separator) => seq(rule, repeat1(seq(separator, rule)));
module.exports = grammar({
name: 'd2',
// I don't know why it works, but it works...
// Keeping it /\s*/, /\s/ or /\s+/ (single value) doesn't work somehow.
extras: _ => [/\s+/, /\s/],
conflicts: $ => [
[$._single_top_level_identifier, $.identifier],
],
rules: {
source_file: $ => seq(
repeat(
choice(
seq($._declaration, $._eol),
$._eol,
),
),
optional($._declaration),
),
// Comments can be parsed as declaration, but since we're a bit strict on
// our _eof rule for correct parsing, we should also consider cases like
// "x -> y # commend\n", where comment appears after declaration but before
// _eol.
// I'm not if that's something quality grammars do, but it works for now.
_declaration: $ => seq(
choice(
$.declaration,
$.import,
$._variable,
$.method_declaration,
$.comment,
$.block_comment,
),
optional(choice(
$.comment,
$.block_comment,
)),
),
comment: _ => token(prec(PREC.comment, /#[^\n]+/)),
block_comment: _ => seq(
token(prec(PREC.comment, '"""')),
repeat(choice(/[^"]/, /"[^"]/, /""[^"]/)),
token(prec(PREC.comment, '"""')),
),
declaration: $ => seq(
$._expr,
optional($._colon_block),
),
_expr: $ => repeat_sep($._identifier, $.connection),
_colon_block: $ => choice(
seq(token(prec(PREC.label, ':')), $.label),
seq(token(prec(PREC.label, ':')), $.block),
seq(token(prec(PREC.label, ':')), $.import),
seq(token(prec(PREC.label, ':')), $.label, $.block),
),
method_declaration: $ => prec.right(99, seq(
$.identifier,
'(', optional($.arguments), ')',
optional(seq(
token.immediate(prec(PREC.colon_start, ':')),
'(', optional($.returns), ')',
)),
)),
// x int
// x, y int
// x int, y int
// x, y int, z int32
arguments: $ => repeat_sep(
seq(
repeat_sep($.argument_name, ','),
$.argument_type,
),
',',
),
returns: $ => alias($.arguments, 'returns'),
argument_name: _ => token(/[a-zA-Z0-9_]+/),
argument_type: _ => token(/[a-zA-Z0-9_\[\]]+/),
connection: _ => choice(
token(prec(PREC.connection, /<-+>/)),
token(prec(PREC.connection, /<-+/)),
token(prec(PREC.connection, /-+>/)),
token(prec(PREC.connection, /--+/)),
),
import: $ => seq(
choice(
token(prec(PREC.import, '@')),
token(prec(PREC.import, '...@')),
),
repeat1(choice(
$._double_quoted,
$._single_quoted,
$.escape,
/[^\n;}\\]+/,
)),
),
block: $ => prec(PREC.block, seq(
token(prec(PREC.block, '{')),
seq(
repeat(seq($._declaration, $._eol)),
optional($._declaration),
),
token(prec(PREC.block, '}')),
)),
label: $ => choice(
$.label_codeblock,
$._label_constraints,
$._label_literal,
),
// tree-sitter is a context-free parser, so it's impossible to catch cases
// with custom start & end tokens (see #64)
// We've hardcoded all the basic options and added fallback that could
// catch some cases, but not all of them.
label_codeblock: $ => choice(
$._label_codeblock_single,
$._label_codeblock_double,
$._label_codeblock_triple,
$._label_codeblock_ticks,
$._label_codeblock_fallback,
),
_label_codeblock_single: $ => seq(
token(prec(PREC.label, '|')),
optional($.codeblock_language), /\s/,
optional(alias(/[^\|]*/, $.codeblock_content)),
token(prec(PREC.label, '|')),
),
_label_codeblock_double: $ => seq(
token(prec(PREC.label, '||')),
optional($.codeblock_language), /\s/,
optional($.codeblock_content),
token(prec(PREC.label, '||')),
),
_label_codeblock_triple: $ => seq(
token(prec(PREC.label, '|||')),
optional($.codeblock_language), /\s/,
optional($.codeblock_content),
token(prec(PREC.label, '|||')),
),
_label_codeblock_ticks: $ => seq(
token(prec(PREC.label, '|`')),
optional($.codeblock_language), /\s/,
optional(alias(/[^`]*/, $.codeblock_content)),
token(prec(PREC.label, '`|')),
),
// Fallback label rule, when others failed.
// It tries to catch cases with custom start & end labels,
// but still could fail
_label_codeblock_fallback: $ => seq(
token(prec(PREC.label, /\|[^a-zA-Z0-9_\s]+/)),
optional($.codeblock_language), /\s/,
optional($.codeblock_content),
token(prec(PREC.label, /[^a-zA-Z0-9_\s]+\|/)),
),
codeblock_language: _ => /[a-zA-Z0-9_]+/,
codeblock_content: _ => prec.right(
repeat1(choice(/./, /\s/)),
),
_label_constraints: $ => seq(
token(prec(PREC.label, '[')),
repeat_sep($.label_constraint, token(';')),
token(prec(PREC.label, ']')),
),
label_constraint: $ => repeat1(choice(
spaced_str(/[^\s;\]]+/),
$._variable,
)),
_label_literal: $ => prec.right(choice(
$.integer,
$.float,
$.boolean,
$._label_double_quoted,
$._single_quoted,
spaced_str($._label_token),
)),
integer: _ => /[\-+]?\d+/,
float: _ => /[\-+]?\d+\.\d+/,
boolean: _ => choice('true', 'false'),
_label_token: $ => repeat1(choice(
/[^\s;|{}\\]+/,
$._variable,
$.escape,
)),
_label_double_quoted: $ => seq(
token(prec(PREC.label, '"')),
repeat(choice(
token.immediate(prec(PREC.label, /[^"\n\\$]+/)),
$.escape,
$._variable,
)),
token(prec(PREC.label, '"')),
),
_identifier: $ => prec.right(choice(
$.identifier_chain,
$._single_top_level_identifier,
)),
identifier_chain: $ => prec.right(repeat_sep1(
$._single_top_level_identifier,
token('.'),
)),
_single_top_level_identifier: $ => choice(
$.identifier,
$.global_glob,
$.glob,
$.recursive_glob,
$.connection_reference,
),
connection_reference: $ => seq(
token('('), $._expr, token(')'),
$.connection_identifier,
),
connection_identifier: $ => seq(
token('['), choice(/\d+/, $.glob), token(']'),
),
identifier: $ => prec.right(seq(
optional(choice(
$._filters,
$.visibility_mark,
)),
choice(
spaced_str($._ident_base),
$._single_quoted,
$._double_quoted,
),
)),
_ident_base: $ => repeat1(choice(
token(prec(PREC.identifier, /[^\s:.;&{}()!\\]/)), // All the special stuff
$.glob,
$.escape,
)),
glob: _ => token(prec(PREC.glob, '*')),
recursive_glob: _ => token(prec(PREC.glob, '**')),
global_glob: _ => token(prec(PREC.glob, '***')),
_filters: $ => choice(
$.glob_filter,
$.inverse_glob_filter,
),
glob_filter: _ => token('&'),
inverse_glob_filter: _ => token('!&'),
visibility_mark: _ => choice(
token(prec(PREC.visibility_mark, '-')),
token(prec(PREC.visibility_mark, '+')),
token(prec(PREC.visibility_mark, '\\#')),
),
_variable: $ => choice(
$.variable,
$.spread_variable,
),
variable: $ => seq(
token(prec(PREC.variable, '$')), token('{'),
$._identifier,
token('}'),
),
spread_variable: $ => seq(
token(prec(PREC.variable, '...$')), token('{'),
$._identifier,
token('}'),
),
_single_quoted: $ => seq(
token(prec(PREC.string, '\'')),
repeat(choice(
token.immediate(prec(PREC.string, /[^'\n\\]+/)),
$.escape,
)),
token(prec(PREC.string, '\'')),
),
_double_quoted: $ => seq(
token(prec(PREC.string, '"')),
repeat(choice(
token.immediate(prec(PREC.string, /[^"\n\\]+/)),
$.escape,
)),
token(prec(PREC.string, '"')),
),
escape: _ => seq(
token(prec(PREC.escape, '\\')),
choice(
/[^xuU]/,
/\d{2,3}/,
/x[0-9a-fA-F]{2,}/,
/u[0-9a-fA-F]{4}/,
/U[0-9a-fA-F]{8}/,
),
),
_eol: _ => choice(
token(prec(PREC.terminate, /\n/)),
token(prec(PREC.terminate, ';')),
token(prec(PREC.terminate, '\0')),
),
},
});