-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.js
480 lines (439 loc) · 12.7 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
/**
* Creates a rule to match one or more of the rules separated by the separator
* and optionally adds a trailing separator (default is false).
*
* @param {Rule} rule
* @param {string} separator - The separator to use.
* @param {boolean?} trailing - The trailing separator to use.
*
* @returns {SeqRule}
*/
const listSeq = (rule, separator, trailing = false) =>
trailing ?
seq(rule, repeat(seq(separator, rule)), optional(separator)) :
seq(rule, repeat(seq(separator, rule)));
/**
* Creates a rule to match one or more of the rules separated by a comma
* and optionally adds a trailing separator (default is false).
*
* @param {Rule} rule
* @param {boolean?} trailing - The trailing separator to use.
*
* @returns {SeqRule}
*/
function commaSep1(rule, trailing = false) {
// return seq(rule, repeat(seq(',', rule)))
return listSeq(rule, ',', trailing);
}
/**
* Creates a rule to optionally match one or more of the rules separated
* by a comma and optionally adds a trailing separator (default is false).
*
* @param {Rule} rule
* @param {boolean?} trailing - The trailing separator to use.
*
* @returns {ChoiceRule}
*/
function commaSep(rule, trailing = false) {
return optional(commaSep1(rule, trailing));
}
/**
* This callback should take a rule and places it in between a group (e.g. parentheses).
*
* @callback GroupingCallback
* @param {Rule} rule
*
* @returns {SeqRule}
*/
/**
* Creates a rule to match a rule surrounded by parentheses.
*
* @param {Rule} rule
*
* @returns {SeqRule}
*/
function parens(rule) {
return seq('(', rule, ')');
}
/**
* Creates a rule to match a rule surrounded by brackets.
*
* @param {Rule} rule
*
* @returns {SeqRule}
*/
function brackets(rule) {
return seq('[', rule, ']');
}
/**
* Creates a rule that matches a keyword followed by its "effect",
* and then a block.
*
* @param {string} keyword
* @param {Rule} effect
* @param {Rule} block
*
* @returns {SeqRule}
*/
function bodied_block(keyword, effect, block) {
return seq(keyword, effect, field('body', block));
}
/**
* Creates a rule that matches a binary operator in between two rules.
* The two rules are then assigned field names left and right respectively.
*
* @param {string} operator
* @param {Rule} rule
*
* @returns {SeqRule}
*/
function binary_operator(operator, rule) {
return seq(field('left', rule), operator, field('right', rule));
}
module.exports = grammar({
name: 'openscad',
extras: $ => [
$.block_comment,
$.line_comment,
/\s/,
],
supertypes: $ => [
$.literal,
$.expression,
$.number,
$.statement,
],
word: $ => $.identifier,
rules: {
source_file: $ => repeat(choice($.use_statement, $._item)),
// expressions are syntax trees that result in values (not objects)
expression: $ => choice(
$.parenthesized_expression,
$.unary_expression,
$.binary_expression,
$.ternary_expression,
$.let_expression,
$.function_call,
$.index_expression,
$.dot_index_expression,
$.assert_expression,
$.echo_expression,
$.literal,
$._variable_name,
),
// statements are language constructs that can create objects
statement: $ =>
choice(
$.for_block,
$.intersection_for_block,
$.if_block,
$.let_block,
$.assign_block,
$.union_block,
$.transform_chain,
$.include_statement,
$.assert_statement,
';',
),
_item: $ => choice(
$.var_declaration,
$.statement,
$.module_item,
$.function_item,
),
// TODO: segment assignment so that parameters can have the LHS highlighted as @parameter
// and the RHS as either @variable or @constant
parameter: $ => choice(
$._variable_name,
$.assignment,
),
parameters: $ => seq(
'(',
sepBy(',', $.parameter),
optional(','),
')',
),
// variable declaration
var_declaration: $ =>
seq($.assignment, ';'),
// modules
module_item: $ => seq(
'module',
field('name', $.identifier),
field('parameters', $.parameters),
field('body', $.statement),
),
// function_item diffeers from $.function_lit which defines anonymous functions/ function literals:
// https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Function_literals
function_item: $ => seq(
'function',
field('name', $.identifier),
field('parameters', $.parameters),
'=', $.expression,
';',
),
// use/include statements
// These are called statements, but use statements aren't included in
// $.statement because they can't be used in all the same places as other
// statements
include_statement: $ =>
prec.right(
seq('include', $.include_path, optional(';')),
),
use_statement: $ => prec.right(
seq('use', $.include_path, optional(';')),
),
include_path: _ => /<[^>]*>/,
assignment: $ => seq(
field('name', $._variable_name),
'=',
field('value', $.expression),
),
union_block: $ => seq(
'{',
repeat($._item),
'}',
),
// NOTE `for(i) i;` statement is currently possible but will ignore for now
// control-flow blocks
for_block: $ => bodied_block(
'for',
$.assignments,
$.statement,
),
intersection_for_block: $ => bodied_block(
'intersection_for',
$.assignments,
$.statement,
),
let_block: $ => bodied_block(
'let',
$.assignments,
$.statement,
),
assign_block: $ => bodied_block(
'assign',
$.assignments,
$.statement,
),
if_block: $ => prec.right(seq(
'if',
field('condition', $.parenthesized_expression),
field('consequence', $.statement),
optional(field('alternative', seq('else', $.statement))),
)),
modifier: _ => choice('*', '!', '#', '%'),
transform_chain: $ => seq(
repeat($.modifier),
$.module_call,
$.statement,
),
module_call: $ => seq(
field('name', $.identifier),
field('arguments', $.arguments),
),
arguments: $ => parens(commaSep(choice($.expression, $.assignment), true)),
assignments: $ => parens(commaSep($.assignment, true)),
parenthesized_expression: $ => parens($.expression),
// `x = 0; x < 5; x = x + 2`
condition_update_clause: $ => parens(seq(
field('initializer', commaSep($.assignment)), ';',
field('condition', $.expression), ';',
field('update', commaSep($.assignment)),
)),
let_expression: $ => bodied_block('let', $.assignments, $.expression),
// atoms that create immediate values
literal: $ => choice(
$.string,
$.number,
$.boolean,
$.undef, // TODO undef is not really a literal
$.function_lit,
$.range,
$.list,
),
// compound atoms that are still literals
function_lit: $ => seq(
'function',
field('parameters', $.parameters),
field('body', $.expression),
),
range: $ => seq(
'[',
field('start', $.expression),
optional(seq(':', field('increment', $.expression))),
':', field('end', $.expression),
']',
),
list: $ => brackets(seq(commaSep($._list_cell, true))),
_list_cell: $ => choice($.expression, $.each, $.list_comprehension),
_comprehension_cell: $ => choice(
$.expression,
choice(parens($.each), $.each),
choice(parens($.list_comprehension), $.list_comprehension),
),
each: $ => seq(
optional($.let_prefix),
'each',
choice($.expression, $.list_comprehension),
),
list_comprehension: $ => seq(
choice($.for_clause, $.if_clause),
),
// TODO dry up let variants
let_prefix: $ => seq(
'let',
$.assignments,
),
for_clause: $ => seq(
optional($.let_prefix),
'for',
choice($.assignments, $.condition_update_clause),
$._comprehension_cell,
),
if_clause: $ => prec.right(seq(
optional($.let_prefix),
'if',
field('condition', $.parenthesized_expression),
field('consequence', $._comprehension_cell),
optional(
seq('else', field('alternative', $._comprehension_cell)),
),
)),
// operations on expressions
function_call: $ => prec(10,
seq(
field('name', $.expression),
field('arguments', $.arguments),
)),
index_expression: $ => prec(10, seq(
field('value', $.expression),
seq('[', $.expression, ']'),
)),
dot_index_expression: $ => prec(10, seq(
field('value', $.expression), '.',
field('index', $.identifier),
)),
unary_expression: $ => choice(
prec(9, seq('!', $.expression)),
prec.left(6, seq('-', $.expression)),
prec.left(6, seq('+', $.expression)),
),
binary_expression: $ => choice(
prec.left(2, binary_operator('||', $.expression)),
prec.left(3, binary_operator('&&', $.expression)),
prec.left(4, binary_operator('==', $.expression)),
prec.left(4, binary_operator('!=', $.expression)),
prec.left(5, binary_operator('<', $.expression)),
prec.left(5, binary_operator('>', $.expression)),
prec.left(5, binary_operator('<=', $.expression)),
prec.left(5, binary_operator('>=', $.expression)),
prec.left(6, binary_operator('+', $.expression)),
prec.left(6, binary_operator('-', $.expression)),
prec.left(7, binary_operator('*', $.expression)),
prec.left(7, binary_operator('/', $.expression)),
prec.left(7, binary_operator('%', $.expression)),
prec.left(8, binary_operator('^', $.expression)),
),
ternary_expression: ($) => prec.right(1, seq(
field('condition', $.expression), '?',
field('consequence', $.expression), ':',
field('alternative', $.expression),
)),
// Asserts are unusual in that they can be inserted into both statements and
// expressions. We want to treat these two cases differently: assertions in
// the middle of a statement can only be followed by a statement, and the
// same applies to expressions. So, this creates two parsers for the two
// distinct conditions, but uses a shared parser for the text of the assert
// clause itself.
assert_statement: $ => seq(
'assert',
field('arguments', $.arguments),
field('statement', $.statement),
),
// assert tail expressions can be optional:
// check = assert(true);
assert_expression: $ => prec.right(seq(
'assert',
field('arguments', $.arguments),
optional(field('expression', $.expression)),
)),
// echo_expression behaves the same way as assert_expression
echo_expression: $ => prec.right(seq(
'echo',
field('arguments', $.arguments),
optional(field('expression', $.expression)),
)),
// valid names for variables, functions, and modules
identifier: _ => /[a-zA-Z_]\w*/,
special_variable: $ => seq('$', $.identifier),
_variable_name: $ => choice($.identifier, $.special_variable),
// https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Strings
// const ESCAPE_SEQUENCE = token(/\\([nrt"\\]|(\r?\n))/);
escape_sequence: _ => token.immediate(
seq('\\',
choice(
/[^xu]/,
/[nrt"\\]/,
/u[0-9a-fA-F]{4}/,
/u\{[0-9a-fA-F]+\}/,
/x[0-9a-fA-F]{2}/,
),
)),
string: $ => seq(
'"',
repeat(choice(
token.immediate(prec(1, /[^"\\]+/)),
$.escape_sequence,
)),
'"',
),
number: $ => choice($.integer, $.float),
integer: _ =>
token(
seq(optional(token.immediate('-')),
/\d+/,
)),
float: _ => token(
seq(
optional(token.immediate('-')),
/(\d+(\.\d+)?|\.\d+)/,
optional(/[eE][+-]?\d+?/),
)),
boolean: _ => choice('true', 'false'),
undef: _ => 'undef',
// http://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment/36328890#36328890
block_comment: _ =>
token(
seq(
'/*',
/[^*]*\*+([^/*][^*]*\*+)*/,
'/',
)),
line_comment: $ => seq('//', /.*/),
},
});
/**
* Creates a rule to match one or more of the rules separated by the separator.
*
* @param {RuleOrLiteral} sep - The separator to use.
* @param {RuleOrLiteral} rule
*
* @returns {SeqRule}
*/
function sepBy1(sep, rule) {
return seq(rule, repeat(seq(sep, rule)));
}
/**
* Creates a rule to optionally match one or more of the rules separated by the separator.
*
* @param {RuleOrLiteral} sep - The separator to use.
* @param {RuleOrLiteral} rule
*
* @returns {ChoiceRule}
*/
function sepBy(sep, rule) {
return optional(sepBy1(sep, rule));
}