@willbooster/tree-sitter-bash 0.25.1
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/LICENSE +21 -0
- package/README.md +44 -0
- package/binding.gyp +30 -0
- package/bindings/node/binding.cc +20 -0
- package/bindings/node/binding_test.js +9 -0
- package/bindings/node/index.d.ts +28 -0
- package/bindings/node/index.js +11 -0
- package/grammar.js +604 -0
- package/package.json +61 -0
- package/queries/highlights.scm +56 -0
- package/src/grammar.json +3967 -0
- package/src/node-types.json +3446 -0
- package/src/parser.c +129111 -0
- package/src/scanner.c +557 -0
- package/src/tree_sitter/alloc.h +54 -0
- package/src/tree_sitter/array.h +336 -0
- package/src/tree_sitter/parser.h +286 -0
- package/tree-sitter.json +46 -0
package/grammar.js
ADDED
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Bash grammar for tree-sitter
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/// <reference types="tree-sitter-cli/dsl" />
|
|
7
|
+
// @ts-check
|
|
8
|
+
|
|
9
|
+
// Characters that end an unquoted word part: metacharacters, quotes, and characters that begin an
|
|
10
|
+
// expansion. `#` only starts a comment at the beginning of a word, so it is handled separately.
|
|
11
|
+
const WORD_BREAKS = ['\\s', '|', '&', ';', '(', ')', '<', '>', '"', '\'', '`', '$', '\\\\'];
|
|
12
|
+
|
|
13
|
+
const EXTGLOB_GROUP = /[?*+@]\((?:[^()\s\\]|\\.)*\)/;
|
|
14
|
+
|
|
15
|
+
const PREC = {
|
|
16
|
+
COMMA: -1,
|
|
17
|
+
ASSIGN: 1,
|
|
18
|
+
TERNARY: 2,
|
|
19
|
+
LOGICAL_OR: 3,
|
|
20
|
+
LOGICAL_AND: 4,
|
|
21
|
+
BITWISE_OR: 5,
|
|
22
|
+
BITWISE_XOR: 6,
|
|
23
|
+
BITWISE_AND: 7,
|
|
24
|
+
EQUALITY: 8,
|
|
25
|
+
COMPARE: 9,
|
|
26
|
+
SHIFT: 10,
|
|
27
|
+
ADD: 11,
|
|
28
|
+
MULTIPLY: 12,
|
|
29
|
+
EXPONENT: 13,
|
|
30
|
+
UNARY: 14,
|
|
31
|
+
PREFIX: 15,
|
|
32
|
+
POSTFIX: 16,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const TEST_UNARY_OPERATORS = [
|
|
36
|
+
'-a', '-b', '-c', '-d', '-e', '-f', '-g', '-h', '-k', '-p', '-r', '-s', '-t', '-u', '-w', '-x',
|
|
37
|
+
'-G', '-L', '-N', '-O', '-S', '-z', '-n', '-o', '-v', '-R',
|
|
38
|
+
];
|
|
39
|
+
const TEST_BINARY_OPERATORS = [
|
|
40
|
+
'-eq', '-ne', '-lt', '-le', '-gt', '-ge', '-nt', '-ot', '-ef', '==', '=', '!=', '<', '>',
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
module.exports = grammar({
|
|
44
|
+
name: 'bash',
|
|
45
|
+
|
|
46
|
+
externals: $ => [
|
|
47
|
+
$.heredoc_start,
|
|
48
|
+
$._heredoc_body_start,
|
|
49
|
+
$.heredoc_content,
|
|
50
|
+
$.heredoc_end,
|
|
51
|
+
$._heredoc_arrow,
|
|
52
|
+
$._heredoc_arrow_dash,
|
|
53
|
+
$._concat,
|
|
54
|
+
$.file_descriptor,
|
|
55
|
+
$.variable_name,
|
|
56
|
+
$._bare_dollar,
|
|
57
|
+
$._empty_value,
|
|
58
|
+
$.regex,
|
|
59
|
+
$.string_content,
|
|
60
|
+
$._backtick_open,
|
|
61
|
+
$._backtick_close,
|
|
62
|
+
$._newline,
|
|
63
|
+
$.__error_recovery,
|
|
64
|
+
],
|
|
65
|
+
|
|
66
|
+
extras: $ => [
|
|
67
|
+
$.comment,
|
|
68
|
+
$.heredoc_body,
|
|
69
|
+
/\s/,
|
|
70
|
+
/\\\r?\n/,
|
|
71
|
+
],
|
|
72
|
+
|
|
73
|
+
supertypes: $ => [
|
|
74
|
+
$._statement,
|
|
75
|
+
$._arithmetic_expression,
|
|
76
|
+
],
|
|
77
|
+
|
|
78
|
+
inline: $ => [
|
|
79
|
+
$._terminator,
|
|
80
|
+
$._command_statement,
|
|
81
|
+
],
|
|
82
|
+
|
|
83
|
+
word: $ => $.word,
|
|
84
|
+
|
|
85
|
+
rules: {
|
|
86
|
+
program: $ => optional($._statements),
|
|
87
|
+
|
|
88
|
+
// Statements
|
|
89
|
+
|
|
90
|
+
_statements: $ => seq(
|
|
91
|
+
repeat(seq($._statement, $._terminator)),
|
|
92
|
+
$._statement,
|
|
93
|
+
optional($._terminator),
|
|
94
|
+
),
|
|
95
|
+
|
|
96
|
+
_terminated_statements: $ => repeat1(seq($._statement, $._terminator)),
|
|
97
|
+
|
|
98
|
+
// A newline is significant only where it can end a statement; elsewhere it is whitespace.
|
|
99
|
+
_terminator: $ => choice(';', '&', $._newline),
|
|
100
|
+
|
|
101
|
+
_statement: $ => choice(
|
|
102
|
+
$._command_statement,
|
|
103
|
+
$.pipeline,
|
|
104
|
+
$.list,
|
|
105
|
+
$.negated_command,
|
|
106
|
+
$.timed_command,
|
|
107
|
+
$.coproc_command,
|
|
108
|
+
),
|
|
109
|
+
|
|
110
|
+
_command_statement: $ => choice(
|
|
111
|
+
$.command,
|
|
112
|
+
$.declaration_command,
|
|
113
|
+
$.redirected_statement,
|
|
114
|
+
$._compound_command,
|
|
115
|
+
$.function_definition,
|
|
116
|
+
),
|
|
117
|
+
|
|
118
|
+
_compound_command: $ => choice(
|
|
119
|
+
$.compound_statement,
|
|
120
|
+
$.subshell,
|
|
121
|
+
$.arithmetic_command,
|
|
122
|
+
$.test_command,
|
|
123
|
+
$.if_statement,
|
|
124
|
+
$.while_statement,
|
|
125
|
+
$.for_statement,
|
|
126
|
+
$.c_style_for_statement,
|
|
127
|
+
$.case_statement,
|
|
128
|
+
),
|
|
129
|
+
|
|
130
|
+
list: $ => prec.left(1, seq(
|
|
131
|
+
field('left', $._statement),
|
|
132
|
+
field('operator', choice('&&', '||')),
|
|
133
|
+
field('right', $._statement),
|
|
134
|
+
)),
|
|
135
|
+
|
|
136
|
+
pipeline: $ => prec.left(3, seq(
|
|
137
|
+
$._pipeline_element,
|
|
138
|
+
repeat1(seq(choice('|', '|&'), $._pipeline_element)),
|
|
139
|
+
)),
|
|
140
|
+
|
|
141
|
+
_pipeline_element: $ => choice($._command_statement, $.coproc_command),
|
|
142
|
+
|
|
143
|
+
negated_command: $ => prec.right(2, seq('!', choice($._command_statement, $.pipeline, $.timed_command))),
|
|
144
|
+
|
|
145
|
+
timed_command: $ => prec.right(2, seq(
|
|
146
|
+
'time',
|
|
147
|
+
optional(field('option', alias('-p', $.word))),
|
|
148
|
+
optional(choice($._command_statement, $.pipeline, $.negated_command)),
|
|
149
|
+
)),
|
|
150
|
+
|
|
151
|
+
coproc_command: $ => prec.right(seq(
|
|
152
|
+
'coproc',
|
|
153
|
+
choice(
|
|
154
|
+
$.command,
|
|
155
|
+
seq(field('name', $.word), $._compound_command),
|
|
156
|
+
$._compound_command,
|
|
157
|
+
),
|
|
158
|
+
)),
|
|
159
|
+
|
|
160
|
+
redirected_statement: $ => prec.left(seq(
|
|
161
|
+
field('body', choice($._compound_command, $.function_definition)),
|
|
162
|
+
repeat1(field('redirect', $._redirect)),
|
|
163
|
+
)),
|
|
164
|
+
|
|
165
|
+
// Simple commands
|
|
166
|
+
|
|
167
|
+
command: $ => prec.left(choice(
|
|
168
|
+
seq(
|
|
169
|
+
repeat($._command_prefix),
|
|
170
|
+
field('name', $.command_name),
|
|
171
|
+
repeat(choice(field('argument', $._word), field('redirect', $._redirect))),
|
|
172
|
+
),
|
|
173
|
+
repeat1($._command_prefix),
|
|
174
|
+
)),
|
|
175
|
+
|
|
176
|
+
_command_prefix: $ => choice(field('assignment', $.variable_assignment), field('redirect', $._redirect)),
|
|
177
|
+
|
|
178
|
+
command_name: $ => $._word,
|
|
179
|
+
|
|
180
|
+
declaration_command: $ => prec.left(seq(
|
|
181
|
+
repeat($._command_prefix),
|
|
182
|
+
field('name', alias(choice('declare', 'typeset', 'export', 'readonly', 'local'), $.command_name)),
|
|
183
|
+
repeat(choice(
|
|
184
|
+
field('argument', choice($.variable_assignment, $._word)),
|
|
185
|
+
field('redirect', $._redirect),
|
|
186
|
+
)),
|
|
187
|
+
)),
|
|
188
|
+
|
|
189
|
+
variable_assignment: $ => seq(
|
|
190
|
+
field('name', choice($.variable_name, $.subscript)),
|
|
191
|
+
field('operator', token.immediate(choice('=', '+='))),
|
|
192
|
+
field('value', choice($._word, $.array, $._empty_value)),
|
|
193
|
+
),
|
|
194
|
+
|
|
195
|
+
subscript: $ => seq(
|
|
196
|
+
field('name', $.variable_name),
|
|
197
|
+
token.immediate('['),
|
|
198
|
+
field('index', $._subscript_index),
|
|
199
|
+
']',
|
|
200
|
+
),
|
|
201
|
+
|
|
202
|
+
_subscript_index: $ => repeat1(choice(
|
|
203
|
+
alias(token.immediate(prec(1, /([^\]$`"'\\]|\\.)+/)), $.word),
|
|
204
|
+
$._quoted_or_expansion,
|
|
205
|
+
)),
|
|
206
|
+
|
|
207
|
+
// Word parts that carry their own delimiters, usable where a bare word would overrun a closing
|
|
208
|
+
// `]` or `}`.
|
|
209
|
+
_quoted_or_expansion: $ => choice(
|
|
210
|
+
$.string,
|
|
211
|
+
$.raw_string,
|
|
212
|
+
$.ansi_c_string,
|
|
213
|
+
$.translated_string,
|
|
214
|
+
$.simple_expansion,
|
|
215
|
+
$.expansion,
|
|
216
|
+
$.command_substitution,
|
|
217
|
+
$.arithmetic_expansion,
|
|
218
|
+
),
|
|
219
|
+
|
|
220
|
+
array: $ => seq(
|
|
221
|
+
token.immediate('('),
|
|
222
|
+
repeat($._word),
|
|
223
|
+
')',
|
|
224
|
+
),
|
|
225
|
+
|
|
226
|
+
// Redirections
|
|
227
|
+
|
|
228
|
+
_redirect: $ => choice($.file_redirect, $.heredoc_redirect, $.herestring_redirect),
|
|
229
|
+
|
|
230
|
+
file_redirect: $ => seq(
|
|
231
|
+
optional(field('descriptor', $.file_descriptor)),
|
|
232
|
+
choice(
|
|
233
|
+
seq(
|
|
234
|
+
field('operator', choice('<', '>', '>>', '>|', '<>', '&>', '&>>', '<&', '>&')),
|
|
235
|
+
field('destination', $._word),
|
|
236
|
+
),
|
|
237
|
+
field('operator', choice('<&-', '>&-')),
|
|
238
|
+
),
|
|
239
|
+
),
|
|
240
|
+
|
|
241
|
+
herestring_redirect: $ => seq(
|
|
242
|
+
optional(field('descriptor', $.file_descriptor)),
|
|
243
|
+
field('operator', '<<<'),
|
|
244
|
+
field('value', $._word),
|
|
245
|
+
),
|
|
246
|
+
|
|
247
|
+
heredoc_redirect: $ => seq(
|
|
248
|
+
optional(field('descriptor', $.file_descriptor)),
|
|
249
|
+
field('operator', choice(alias($._heredoc_arrow, '<<'), alias($._heredoc_arrow_dash, '<<-'))),
|
|
250
|
+
field('delimiter', $.heredoc_start),
|
|
251
|
+
),
|
|
252
|
+
|
|
253
|
+
// The scanner opens a body at the newline that ends the line holding its `<<` operator, so bodies
|
|
254
|
+
// appear, in operator order, wherever that newline falls.
|
|
255
|
+
heredoc_body: $ => seq(
|
|
256
|
+
$._heredoc_body_start,
|
|
257
|
+
repeat(choice(
|
|
258
|
+
$.heredoc_content,
|
|
259
|
+
$.simple_expansion,
|
|
260
|
+
$.expansion,
|
|
261
|
+
$.command_substitution,
|
|
262
|
+
$.arithmetic_expansion,
|
|
263
|
+
)),
|
|
264
|
+
$.heredoc_end,
|
|
265
|
+
),
|
|
266
|
+
|
|
267
|
+
// Compound commands
|
|
268
|
+
|
|
269
|
+
compound_statement: $ => seq('{', optional($._terminated_statements), '}'),
|
|
270
|
+
|
|
271
|
+
subshell: $ => seq('(', optional($._statements), ')'),
|
|
272
|
+
|
|
273
|
+
arithmetic_command: $ => seq(
|
|
274
|
+
'((',
|
|
275
|
+
optional($._arithmetic_expression),
|
|
276
|
+
'))',
|
|
277
|
+
),
|
|
278
|
+
|
|
279
|
+
test_command: $ => seq('[[', $._test_expression, ']]'),
|
|
280
|
+
|
|
281
|
+
_test_expression: $ => choice(
|
|
282
|
+
$._word,
|
|
283
|
+
$.unary_test,
|
|
284
|
+
$.binary_test,
|
|
285
|
+
$.regex_test,
|
|
286
|
+
alias($._test_negation, $.negated_test),
|
|
287
|
+
alias($._test_logical, $.logical_test),
|
|
288
|
+
alias($._test_parenthesized, $.parenthesized_test),
|
|
289
|
+
),
|
|
290
|
+
|
|
291
|
+
unary_test: $ => prec(PREC.UNARY, seq(
|
|
292
|
+
field('operator', alias(choice(...TEST_UNARY_OPERATORS), $.test_operator)),
|
|
293
|
+
field('operand', $._word),
|
|
294
|
+
)),
|
|
295
|
+
|
|
296
|
+
binary_test: $ => prec(PREC.COMPARE, seq(
|
|
297
|
+
field('left', $._word),
|
|
298
|
+
field('operator', alias(choice(...TEST_BINARY_OPERATORS), $.test_operator)),
|
|
299
|
+
field('right', $._word),
|
|
300
|
+
)),
|
|
301
|
+
|
|
302
|
+
regex_test: $ => prec(PREC.COMPARE, seq(
|
|
303
|
+
field('left', $._word),
|
|
304
|
+
field('operator', '=~'),
|
|
305
|
+
field('right', $.regex),
|
|
306
|
+
)),
|
|
307
|
+
|
|
308
|
+
_test_negation: $ => prec(PREC.UNARY, seq('!', $._test_expression)),
|
|
309
|
+
|
|
310
|
+
_test_logical: $ => choice(
|
|
311
|
+
prec.left(PREC.LOGICAL_AND, seq($._test_expression, '&&', $._test_expression)),
|
|
312
|
+
prec.left(PREC.LOGICAL_OR, seq($._test_expression, '||', $._test_expression)),
|
|
313
|
+
),
|
|
314
|
+
|
|
315
|
+
_test_parenthesized: $ => seq('(', $._test_expression, ')'),
|
|
316
|
+
|
|
317
|
+
if_statement: $ => seq(
|
|
318
|
+
'if',
|
|
319
|
+
field('condition', $._terminated_statements),
|
|
320
|
+
'then',
|
|
321
|
+
optional(field('consequence', $._terminated_statements)),
|
|
322
|
+
repeat($.elif_clause),
|
|
323
|
+
optional($.else_clause),
|
|
324
|
+
'fi',
|
|
325
|
+
),
|
|
326
|
+
|
|
327
|
+
elif_clause: $ => seq(
|
|
328
|
+
'elif',
|
|
329
|
+
field('condition', $._terminated_statements),
|
|
330
|
+
'then',
|
|
331
|
+
optional(field('consequence', $._terminated_statements)),
|
|
332
|
+
),
|
|
333
|
+
|
|
334
|
+
else_clause: $ => seq('else', optional($._terminated_statements)),
|
|
335
|
+
|
|
336
|
+
while_statement: $ => seq(
|
|
337
|
+
field('keyword', choice('while', 'until')),
|
|
338
|
+
field('condition', $._terminated_statements),
|
|
339
|
+
field('body', $.do_group),
|
|
340
|
+
),
|
|
341
|
+
|
|
342
|
+
do_group: $ => seq('do', optional($._terminated_statements), 'done'),
|
|
343
|
+
|
|
344
|
+
for_statement: $ => seq(
|
|
345
|
+
field('keyword', choice('for', 'select')),
|
|
346
|
+
field('variable', alias($.word, $.variable_name)),
|
|
347
|
+
optional(seq('in', repeat(field('value', $._word)))),
|
|
348
|
+
$._terminator,
|
|
349
|
+
field('body', choice($.do_group, $.compound_statement)),
|
|
350
|
+
),
|
|
351
|
+
|
|
352
|
+
c_style_for_statement: $ => seq(
|
|
353
|
+
'for',
|
|
354
|
+
'((',
|
|
355
|
+
field('initializer', optional($._arithmetic_expression)),
|
|
356
|
+
';',
|
|
357
|
+
field('condition', optional($._arithmetic_expression)),
|
|
358
|
+
';',
|
|
359
|
+
field('update', optional($._arithmetic_expression)),
|
|
360
|
+
'))',
|
|
361
|
+
optional($._terminator),
|
|
362
|
+
field('body', choice($.do_group, $.compound_statement)),
|
|
363
|
+
),
|
|
364
|
+
|
|
365
|
+
case_statement: $ => seq(
|
|
366
|
+
'case',
|
|
367
|
+
field('value', $._word),
|
|
368
|
+
'in',
|
|
369
|
+
repeat($.case_item),
|
|
370
|
+
optional(alias($._last_case_item, $.case_item)),
|
|
371
|
+
'esac',
|
|
372
|
+
),
|
|
373
|
+
|
|
374
|
+
case_item: $ => seq(
|
|
375
|
+
$._case_patterns,
|
|
376
|
+
optional($._statements),
|
|
377
|
+
field('terminator', choice(';;', ';&', ';;&')),
|
|
378
|
+
),
|
|
379
|
+
|
|
380
|
+
// Only the last item may omit its terminator.
|
|
381
|
+
_last_case_item: $ => seq($._case_patterns, optional($._statements)),
|
|
382
|
+
|
|
383
|
+
_case_patterns: $ => seq(
|
|
384
|
+
optional('('),
|
|
385
|
+
field('pattern', $._word),
|
|
386
|
+
repeat(seq('|', field('pattern', $._word))),
|
|
387
|
+
')',
|
|
388
|
+
),
|
|
389
|
+
|
|
390
|
+
function_definition: $ => seq(
|
|
391
|
+
choice(
|
|
392
|
+
seq('function', field('name', $.word), optional(seq('(', ')'))),
|
|
393
|
+
seq(field('name', $.word), '(', ')'),
|
|
394
|
+
),
|
|
395
|
+
field('body', $._compound_command),
|
|
396
|
+
),
|
|
397
|
+
|
|
398
|
+
// Words
|
|
399
|
+
|
|
400
|
+
_word: $ => choice($._word_part, $.concatenation),
|
|
401
|
+
|
|
402
|
+
concatenation: $ => prec.right(seq(
|
|
403
|
+
$._word_part,
|
|
404
|
+
repeat1(seq($._concat, choice($._word_part, alias($._hash_word, $.word)))),
|
|
405
|
+
)),
|
|
406
|
+
|
|
407
|
+
_word_part: $ => choice(
|
|
408
|
+
$.word,
|
|
409
|
+
$.string,
|
|
410
|
+
$.raw_string,
|
|
411
|
+
$.ansi_c_string,
|
|
412
|
+
$.translated_string,
|
|
413
|
+
$.simple_expansion,
|
|
414
|
+
$.expansion,
|
|
415
|
+
$.command_substitution,
|
|
416
|
+
$.arithmetic_expansion,
|
|
417
|
+
$.process_substitution,
|
|
418
|
+
alias($._bare_dollar, $.word),
|
|
419
|
+
),
|
|
420
|
+
|
|
421
|
+
// An extglob group (`@(a|b)`) is part of the word; `!(` only inside a word, since a leading `!`
|
|
422
|
+
// before `(` negates a subshell.
|
|
423
|
+
word: _ => token(seq(
|
|
424
|
+
choice(noneOf('#', ...WORD_BREAKS), /\\[^\r\n]/, EXTGLOB_GROUP),
|
|
425
|
+
repeat(choice(noneOf(...WORD_BREAKS), /\\[^\r\n]/, EXTGLOB_GROUP, /!\((?:[^()\s\\]|\\.)*\)/)),
|
|
426
|
+
)),
|
|
427
|
+
|
|
428
|
+
// A `#` inside a word (`a$b#c`) is literal; only a `#` that begins a word starts a comment.
|
|
429
|
+
_hash_word: _ => token(prec(1, seq('#', repeat(choice(noneOf(...WORD_BREAKS), /\\[^\r\n]/))))),
|
|
430
|
+
|
|
431
|
+
string: $ => seq(
|
|
432
|
+
'"',
|
|
433
|
+
repeat(choice(
|
|
434
|
+
$.string_content,
|
|
435
|
+
$.simple_expansion,
|
|
436
|
+
$.expansion,
|
|
437
|
+
$.command_substitution,
|
|
438
|
+
$.arithmetic_expansion,
|
|
439
|
+
)),
|
|
440
|
+
'"',
|
|
441
|
+
),
|
|
442
|
+
|
|
443
|
+
raw_string: _ => /'[^']*'/,
|
|
444
|
+
|
|
445
|
+
ansi_c_string: _ => /\$'([^'\\]|\\(.|\r?\n))*'/,
|
|
446
|
+
|
|
447
|
+
translated_string: $ => seq('$"', repeat(choice(
|
|
448
|
+
$.string_content,
|
|
449
|
+
$.simple_expansion,
|
|
450
|
+
$.expansion,
|
|
451
|
+
$.command_substitution,
|
|
452
|
+
$.arithmetic_expansion,
|
|
453
|
+
)), '"'),
|
|
454
|
+
|
|
455
|
+
simple_expansion: $ => seq(
|
|
456
|
+
'$',
|
|
457
|
+
choice(
|
|
458
|
+
alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/), $.variable_name),
|
|
459
|
+
alias(token.immediate(/[0-9*@#?$!_-]/), $.special_variable_name),
|
|
460
|
+
),
|
|
461
|
+
),
|
|
462
|
+
|
|
463
|
+
// `${#}` and `${!}` are the special parameters `#` and `!`; with a following name they are the
|
|
464
|
+
// length and indirection prefixes.
|
|
465
|
+
expansion: $ => seq(
|
|
466
|
+
'${',
|
|
467
|
+
choice(
|
|
468
|
+
seq(
|
|
469
|
+
optional(field('prefix', alias(token.immediate(choice('#', '!')), $.operator))),
|
|
470
|
+
field('parameter', choice(
|
|
471
|
+
alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/), $.variable_name),
|
|
472
|
+
alias(token.immediate(/[0-9]+|[*@?$_-]/), $.special_variable_name),
|
|
473
|
+
alias(token.immediate(choice('#', '!')), $.special_variable_name),
|
|
474
|
+
$._expansion_subscript,
|
|
475
|
+
)),
|
|
476
|
+
optional($._expansion_operation),
|
|
477
|
+
),
|
|
478
|
+
field('parameter', alias(token.immediate(choice('#', '!')), $.special_variable_name)),
|
|
479
|
+
),
|
|
480
|
+
'}',
|
|
481
|
+
),
|
|
482
|
+
|
|
483
|
+
_expansion_subscript: $ => seq(
|
|
484
|
+
field('name', alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/), $.variable_name)),
|
|
485
|
+
token.immediate('['),
|
|
486
|
+
field('index', $._subscript_index),
|
|
487
|
+
']',
|
|
488
|
+
),
|
|
489
|
+
|
|
490
|
+
_expansion_operation: $ => choice(
|
|
491
|
+
seq(
|
|
492
|
+
field('operator', alias(
|
|
493
|
+
token.immediate(choice(
|
|
494
|
+
':-', '-', ':=', '=', ':?', '?', ':+', '+', '#', '##', '%', '%%',
|
|
495
|
+
'^', '^^', ',', ',,', '~', '~~', '/', '//', '/#', '/%', ':', '@',
|
|
496
|
+
)),
|
|
497
|
+
$.operator,
|
|
498
|
+
)),
|
|
499
|
+
repeat(field('argument', choice(alias($._expansion_text, $.word), $._quoted_or_expansion))),
|
|
500
|
+
),
|
|
501
|
+
field('operator', alias(token.immediate(choice('*', '@')), $.operator)),
|
|
502
|
+
),
|
|
503
|
+
|
|
504
|
+
_expansion_text: _ => token(prec(-1, /([^}$`"'\\]|\\(.|\r?\n))+/)),
|
|
505
|
+
|
|
506
|
+
command_substitution: $ => choice(
|
|
507
|
+
seq('$(', optional($._statements), ')'),
|
|
508
|
+
// bash 5.3 runs `${ list; }` and `${| list; }` in the current shell.
|
|
509
|
+
seq(alias(token(/\$\{[\s|]/), '${'), optional($._terminated_statements), '}'),
|
|
510
|
+
seq(alias($._backtick_open, '`'), optional($._statements), alias($._backtick_close, '`')),
|
|
511
|
+
),
|
|
512
|
+
|
|
513
|
+
process_substitution: $ => seq(choice('<(', '>('), optional($._statements), ')'),
|
|
514
|
+
|
|
515
|
+
arithmetic_expansion: $ => choice(
|
|
516
|
+
seq('$((', optional($._arithmetic_expression), '))'),
|
|
517
|
+
seq('$[', optional($._arithmetic_expression), ']'),
|
|
518
|
+
),
|
|
519
|
+
|
|
520
|
+
// Arithmetic
|
|
521
|
+
|
|
522
|
+
_arithmetic_expression: $ => choice(
|
|
523
|
+
$.number,
|
|
524
|
+
alias($._arithmetic_variable, $.variable_name),
|
|
525
|
+
alias($._arithmetic_subscript, $.subscript),
|
|
526
|
+
$.simple_expansion,
|
|
527
|
+
$.expansion,
|
|
528
|
+
$.command_substitution,
|
|
529
|
+
$.arithmetic_expansion,
|
|
530
|
+
$.string,
|
|
531
|
+
alias($._arithmetic_unary, $.unary_expression),
|
|
532
|
+
alias($._arithmetic_postfix, $.postfix_expression),
|
|
533
|
+
alias($._arithmetic_binary, $.binary_expression),
|
|
534
|
+
alias($._arithmetic_ternary, $.ternary_expression),
|
|
535
|
+
alias($._arithmetic_parenthesized, $.parenthesized_expression),
|
|
536
|
+
),
|
|
537
|
+
|
|
538
|
+
number: _ => /(0[xX][0-9a-fA-F]+|[0-9]+#[0-9a-zA-Z@_]+|[0-9]+)/,
|
|
539
|
+
|
|
540
|
+
_arithmetic_variable: _ => /[A-Za-z_][A-Za-z0-9_]*/,
|
|
541
|
+
|
|
542
|
+
_arithmetic_subscript: $ => seq(
|
|
543
|
+
field('name', alias($._arithmetic_variable, $.variable_name)),
|
|
544
|
+
token.immediate('['),
|
|
545
|
+
field('index', $._arithmetic_expression),
|
|
546
|
+
']',
|
|
547
|
+
),
|
|
548
|
+
|
|
549
|
+
_arithmetic_unary: $ => choice(
|
|
550
|
+
prec(PREC.PREFIX, seq(field('operator', choice('++', '--')), $._arithmetic_expression)),
|
|
551
|
+
prec(PREC.UNARY, seq(field('operator', choice('-', '+', '!', '~')), $._arithmetic_expression)),
|
|
552
|
+
),
|
|
553
|
+
|
|
554
|
+
_arithmetic_postfix: $ => prec(PREC.POSTFIX, seq($._arithmetic_expression, field('operator', choice('++', '--')))),
|
|
555
|
+
|
|
556
|
+
_arithmetic_binary: $ => {
|
|
557
|
+
const table = [
|
|
558
|
+
[PREC.COMMA, ','],
|
|
559
|
+
[PREC.ASSIGN, choice('=', '+=', '-=', '*=', '/=', '%=', '**=', '<<=', '>>=', '&=', '^=', '|=')],
|
|
560
|
+
[PREC.LOGICAL_OR, '||'],
|
|
561
|
+
[PREC.LOGICAL_AND, '&&'],
|
|
562
|
+
[PREC.BITWISE_OR, '|'],
|
|
563
|
+
[PREC.BITWISE_XOR, '^'],
|
|
564
|
+
[PREC.BITWISE_AND, '&'],
|
|
565
|
+
[PREC.EQUALITY, choice('==', '!=')],
|
|
566
|
+
[PREC.COMPARE, choice('<', '>', '<=', '>=')],
|
|
567
|
+
[PREC.SHIFT, choice('<<', '>>')],
|
|
568
|
+
[PREC.ADD, choice('+', '-')],
|
|
569
|
+
[PREC.MULTIPLY, choice('*', '/', '%')],
|
|
570
|
+
[PREC.EXPONENT, '**'],
|
|
571
|
+
];
|
|
572
|
+
return choice(...table.map(([precedence, operator]) => {
|
|
573
|
+
const associativity = precedence === PREC.ASSIGN || precedence === PREC.EXPONENT ? prec.right : prec.left;
|
|
574
|
+
return associativity(/** @type {number} */ (precedence), seq(
|
|
575
|
+
field('left', $._arithmetic_expression),
|
|
576
|
+
field('operator', /** @type {RuleOrLiteral} */ (operator)),
|
|
577
|
+
field('right', $._arithmetic_expression),
|
|
578
|
+
));
|
|
579
|
+
}));
|
|
580
|
+
},
|
|
581
|
+
|
|
582
|
+
_arithmetic_ternary: $ => prec.right(PREC.TERNARY, seq(
|
|
583
|
+
field('condition', $._arithmetic_expression),
|
|
584
|
+
'?',
|
|
585
|
+
field('consequence', $._arithmetic_expression),
|
|
586
|
+
':',
|
|
587
|
+
field('alternative', $._arithmetic_expression),
|
|
588
|
+
)),
|
|
589
|
+
|
|
590
|
+
_arithmetic_parenthesized: $ => seq('(', $._arithmetic_expression, ')'),
|
|
591
|
+
|
|
592
|
+
comment: _ => token(prec(-10, /#.*/)),
|
|
593
|
+
},
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* A regular expression matching any single character except the given ones.
|
|
598
|
+
*
|
|
599
|
+
* @param {...string} characters
|
|
600
|
+
* @returns {RegExp}
|
|
601
|
+
*/
|
|
602
|
+
function noneOf(...characters) {
|
|
603
|
+
return new RegExp(`[^${characters.join('')}]`);
|
|
604
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@willbooster/tree-sitter-bash",
|
|
3
|
+
"version": "0.25.1",
|
|
4
|
+
"description": "Bash grammar for tree-sitter",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/WillBooster/tree-sitter-bash.git"
|
|
8
|
+
},
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public",
|
|
11
|
+
"registry": "https://registry.npmjs.org/"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": {
|
|
15
|
+
"name": "Max Brunsfeld",
|
|
16
|
+
"email": "maxbrunsfeld@gmail.com"
|
|
17
|
+
},
|
|
18
|
+
"main": "bindings/node",
|
|
19
|
+
"types": "bindings/node",
|
|
20
|
+
"keywords": [
|
|
21
|
+
"incremental",
|
|
22
|
+
"parsing",
|
|
23
|
+
"tree-sitter",
|
|
24
|
+
"bash"
|
|
25
|
+
],
|
|
26
|
+
"files": [
|
|
27
|
+
"grammar.js",
|
|
28
|
+
"tree-sitter.json",
|
|
29
|
+
"binding.gyp",
|
|
30
|
+
"prebuilds/**",
|
|
31
|
+
"bindings/node/*",
|
|
32
|
+
"queries/*",
|
|
33
|
+
"src/**",
|
|
34
|
+
"*.wasm"
|
|
35
|
+
],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"node-addon-api": "^8.2.1",
|
|
38
|
+
"node-gyp-build": "^4.8.2"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"eslint": "^9.12.0",
|
|
42
|
+
"eslint-config-treesitter": "^1.0.2",
|
|
43
|
+
"prebuildify": "^6.0.1",
|
|
44
|
+
"tree-sitter-cli": "^0.27.0"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"tree-sitter": "^0.25.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"tree-sitter": {
|
|
51
|
+
"optional": true
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"install": "node-gyp-build",
|
|
56
|
+
"lint": "eslint grammar.js",
|
|
57
|
+
"prestart": "tree-sitter build --wasm",
|
|
58
|
+
"start": "tree-sitter playground",
|
|
59
|
+
"test": "node --test bindings/node/*_test.js"
|
|
60
|
+
}
|
|
61
|
+
}
|