tree-sitter-batch 0.3.1 → 0.4.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/README.md CHANGED
@@ -48,8 +48,10 @@ Parsed tree:
48
48
  (echo_off)
49
49
  (comment)
50
50
  (setlocal_stmt)
51
- (variable_assignment)
52
- (variable_assignment)
51
+ (variable_assignment
52
+ (set_keyword) (variable_name) (assignment_value))
53
+ (variable_assignment
54
+ (set_keyword) (set_option) (variable_name) (assignment_value))
53
55
  (if_stmt
54
56
  (string)
55
57
  (parenthesized
package/grammar.js CHANGED
@@ -4,6 +4,7 @@ const kw = (word) => token(prec(10, ci(word)));
4
4
  export default grammar({
5
5
  name: 'batch',
6
6
  extras: () => [/[ \t]/],
7
+ word: ($) => $.command_name,
7
8
  rules: {
8
9
  program: ($) => repeat(choice(seq($._stmt, /\r?\n/), /\r?\n/)),
9
10
  _stmt: ($) => choice(
@@ -19,12 +20,13 @@ export default grammar({
19
20
  seq('::', /[^\r\n]*/),
20
21
  ))),
21
22
  label: () => token(seq(':', /[a-zA-Z_][a-zA-Z0-9_-]*/)),
22
- variable_assignment: () => prec(8, seq(
23
- optional('@'), kw('set'),
24
- optional(seq(/[ \t]+/, /\/[aApP]/)), /[ \t]+/,
23
+ variable_assignment: ($) => prec(8, seq(
24
+ optional('@'), alias(kw('set'), $.set_keyword),
25
+ optional(seq(/[ \t]+/, alias(/\/[aApP]/, $.set_option))),
26
+ /[ \t]+/,
25
27
  choice(
26
- seq('"', /[a-zA-Z_][a-zA-Z0-9_]*/, '=', optional(/[^"\r\n]*/), '"'),
27
- seq(/[a-zA-Z_][a-zA-Z0-9_]*/, '=', optional(/[^\r\n]*/)),
28
+ seq('"', alias(/[a-zA-Z_][a-zA-Z0-9_]*/, $.variable_name), '=', optional(alias(/[^"\r\n]+/, $.assignment_value)), '"'),
29
+ seq(alias(/[a-zA-Z_][a-zA-Z0-9_]*/, $.variable_name), '=', optional(alias(/[^\r\n]+/, $.assignment_value))),
28
30
  ),
29
31
  )),
30
32
  if_stmt: ($) => prec.right(8, seq(
@@ -83,7 +85,7 @@ export default grammar({
83
85
  for_options: () => token(prec(10, choice(ci('/d'), seq(ci('/r'), optional(seq(/[ \t]+/, /[^\s]+/))), ci('/l'), seq(ci('/f'), optional(seq(/[ \t]+/, '"', /[^"]*/, '"')))))),
84
86
  for_variable: () => token(seq('%%', optional('~'), /[a-zA-Z]/)),
85
87
  for_set: () => /[^)\r\n]+/,
86
- parenthesized: ($) => seq('(', repeat(choice(seq($._stmt, /\r?\n/), /\r?\n/)), ')'),
88
+ parenthesized: ($) => seq('(', repeat(choice(seq($._stmt, /\r?\n/), /\r?\n/)), optional($._stmt), ')'),
87
89
  redirect_stmt: ($) => prec.right(4, seq(choice($.cmd, $.parenthesized), $.redirection)),
88
90
  redirection: ($) => prec.right(seq(
89
91
  optional(/[0-2]/), $.redirect_op, $.redirect_target,
@@ -93,8 +95,8 @@ export default grammar({
93
95
  redirect_target: () => token(choice(ci('nul'), ci('con'), /[^\s|&><\r\n]+/)),
94
96
  pipe_stmt: ($) => prec.left(3, seq(choice($.cmd, $.parenthesized), '|', choice($.cmd, $.parenthesized))),
95
97
  cond_exec: ($) => choice(
96
- prec.left(2, seq(choice($.cmd, $.parenthesized), '&&', choice($.cmd, $.parenthesized))),
97
- prec.left(1, seq(choice($.cmd, $.parenthesized), '||', choice($.cmd, $.parenthesized))),
98
+ prec.left(1, seq(choice($.cond_exec, $.cmd, $.parenthesized), '&&', choice($.cmd, $.parenthesized))),
99
+ prec.left(1, seq(choice($.cond_exec, $.cmd, $.parenthesized), '||', choice($.cmd, $.parenthesized))),
98
100
  ),
99
101
  variable_reference: () => token(choice(
100
102
  seq('%', /[a-zA-Z_][a-zA-Z0-9_]*/, '%'),
@@ -108,9 +110,10 @@ export default grammar({
108
110
  cmd: ($) => prec.right(5, seq(optional('@'), $.command_name, optional($.argument_list))),
109
111
  command_name: () => /[a-zA-Z_][a-zA-Z0-9_.-]*/,
110
112
  argument_list: ($) => prec.right(repeat1($._arg)),
111
- _arg: ($) => choice($.string, $.variable_reference, $.command_option, $.argument_value),
113
+ _arg: ($) => choice($.string, $.variable_reference, $.command_option, $.paren_expression, $.argument_value),
112
114
  command_option: () => token(seq('/', /[a-zA-Z_?][a-zA-Z0-9_:]*/)),
113
- argument_value: () => /[^\s|&><"\r\n%!][^\s|&><"\r\n]*/,
115
+ paren_expression: ($) => seq('(', repeat($._arg), ')'),
116
+ argument_value: () => /[^\s|&><"\r\n%!()][^\s|&><"\r\n()]*/,
114
117
  integer: () => /[0-9]+/,
115
118
  },
116
119
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tree-sitter-batch",
3
- "version": "0.3.1",
3
+ "version": "0.4.1",
4
4
  "description": "A Windows Batch/CMD grammar for tree-sitter",
5
5
  "type": "module",
6
6
  "repository": {
@@ -8,7 +8,10 @@
8
8
  (label) @label
9
9
 
10
10
  ; Variable assignment
11
- (variable_assignment) @variable
11
+ (set_keyword) @keyword
12
+ (variable_name) @variable
13
+ (set_option) @constant
14
+ (assignment_value) @string
12
15
 
13
16
  ; IF/FOR/GOTO/CALL statements
14
17
  (if_stmt) @keyword
package/src/grammar.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "$schema": "https://tree-sitter.github.io/tree-sitter/assets/schemas/grammar.schema.json",
3
3
  "name": "batch",
4
+ "word": "command_name",
4
5
  "rules": {
5
6
  "program": {
6
7
  "type": "REPEAT",
@@ -249,15 +250,20 @@
249
250
  ]
250
251
  },
251
252
  {
252
- "type": "TOKEN",
253
+ "type": "ALIAS",
253
254
  "content": {
254
- "type": "PREC",
255
- "value": 10,
255
+ "type": "TOKEN",
256
256
  "content": {
257
- "type": "PATTERN",
258
- "value": "[sS][eE][tT]"
257
+ "type": "PREC",
258
+ "value": 10,
259
+ "content": {
260
+ "type": "PATTERN",
261
+ "value": "[sS][eE][tT]"
262
+ }
259
263
  }
260
- }
264
+ },
265
+ "named": true,
266
+ "value": "set_keyword"
261
267
  },
262
268
  {
263
269
  "type": "CHOICE",
@@ -270,8 +276,13 @@
270
276
  "value": "[ \\t]+"
271
277
  },
272
278
  {
273
- "type": "PATTERN",
274
- "value": "\\/[aApP]"
279
+ "type": "ALIAS",
280
+ "content": {
281
+ "type": "PATTERN",
282
+ "value": "\\/[aApP]"
283
+ },
284
+ "named": true,
285
+ "value": "set_option"
275
286
  }
276
287
  ]
277
288
  },
@@ -295,8 +306,13 @@
295
306
  "value": "\""
296
307
  },
297
308
  {
298
- "type": "PATTERN",
299
- "value": "[a-zA-Z_][a-zA-Z0-9_]*"
309
+ "type": "ALIAS",
310
+ "content": {
311
+ "type": "PATTERN",
312
+ "value": "[a-zA-Z_][a-zA-Z0-9_]*"
313
+ },
314
+ "named": true,
315
+ "value": "variable_name"
300
316
  },
301
317
  {
302
318
  "type": "STRING",
@@ -306,8 +322,13 @@
306
322
  "type": "CHOICE",
307
323
  "members": [
308
324
  {
309
- "type": "PATTERN",
310
- "value": "[^\"\\r\\n]*"
325
+ "type": "ALIAS",
326
+ "content": {
327
+ "type": "PATTERN",
328
+ "value": "[^\"\\r\\n]+"
329
+ },
330
+ "named": true,
331
+ "value": "assignment_value"
311
332
  },
312
333
  {
313
334
  "type": "BLANK"
@@ -324,8 +345,13 @@
324
345
  "type": "SEQ",
325
346
  "members": [
326
347
  {
327
- "type": "PATTERN",
328
- "value": "[a-zA-Z_][a-zA-Z0-9_]*"
348
+ "type": "ALIAS",
349
+ "content": {
350
+ "type": "PATTERN",
351
+ "value": "[a-zA-Z_][a-zA-Z0-9_]*"
352
+ },
353
+ "named": true,
354
+ "value": "variable_name"
329
355
  },
330
356
  {
331
357
  "type": "STRING",
@@ -335,8 +361,13 @@
335
361
  "type": "CHOICE",
336
362
  "members": [
337
363
  {
338
- "type": "PATTERN",
339
- "value": "[^\\r\\n]*"
364
+ "type": "ALIAS",
365
+ "content": {
366
+ "type": "PATTERN",
367
+ "value": "[^\\r\\n]+"
368
+ },
369
+ "named": true,
370
+ "value": "assignment_value"
340
371
  },
341
372
  {
342
373
  "type": "BLANK"
@@ -1227,6 +1258,18 @@
1227
1258
  ]
1228
1259
  }
1229
1260
  },
1261
+ {
1262
+ "type": "CHOICE",
1263
+ "members": [
1264
+ {
1265
+ "type": "SYMBOL",
1266
+ "name": "_stmt"
1267
+ },
1268
+ {
1269
+ "type": "BLANK"
1270
+ }
1271
+ ]
1272
+ },
1230
1273
  {
1231
1274
  "type": "STRING",
1232
1275
  "value": ")"
@@ -1421,13 +1464,17 @@
1421
1464
  "members": [
1422
1465
  {
1423
1466
  "type": "PREC_LEFT",
1424
- "value": 2,
1467
+ "value": 1,
1425
1468
  "content": {
1426
1469
  "type": "SEQ",
1427
1470
  "members": [
1428
1471
  {
1429
1472
  "type": "CHOICE",
1430
1473
  "members": [
1474
+ {
1475
+ "type": "SYMBOL",
1476
+ "name": "cond_exec"
1477
+ },
1431
1478
  {
1432
1479
  "type": "SYMBOL",
1433
1480
  "name": "cmd"
@@ -1467,6 +1514,10 @@
1467
1514
  {
1468
1515
  "type": "CHOICE",
1469
1516
  "members": [
1517
+ {
1518
+ "type": "SYMBOL",
1519
+ "name": "cond_exec"
1520
+ },
1470
1521
  {
1471
1522
  "type": "SYMBOL",
1472
1523
  "name": "cmd"
@@ -1708,6 +1759,10 @@
1708
1759
  "type": "SYMBOL",
1709
1760
  "name": "command_option"
1710
1761
  },
1762
+ {
1763
+ "type": "SYMBOL",
1764
+ "name": "paren_expression"
1765
+ },
1711
1766
  {
1712
1767
  "type": "SYMBOL",
1713
1768
  "name": "argument_value"
@@ -1730,9 +1785,29 @@
1730
1785
  ]
1731
1786
  }
1732
1787
  },
1788
+ "paren_expression": {
1789
+ "type": "SEQ",
1790
+ "members": [
1791
+ {
1792
+ "type": "STRING",
1793
+ "value": "("
1794
+ },
1795
+ {
1796
+ "type": "REPEAT",
1797
+ "content": {
1798
+ "type": "SYMBOL",
1799
+ "name": "_arg"
1800
+ }
1801
+ },
1802
+ {
1803
+ "type": "STRING",
1804
+ "value": ")"
1805
+ }
1806
+ ]
1807
+ },
1733
1808
  "argument_value": {
1734
1809
  "type": "PATTERN",
1735
- "value": "[^\\s|&><\"\\r\\n%!][^\\s|&><\"\\r\\n]*"
1810
+ "value": "[^\\s|&><\"\\r\\n%!()][^\\s|&><\"\\r\\n()]*"
1736
1811
  },
1737
1812
  "integer": {
1738
1813
  "type": "PATTERN",
@@ -15,6 +15,10 @@
15
15
  "type": "command_option",
16
16
  "named": true
17
17
  },
18
+ {
19
+ "type": "paren_expression",
20
+ "named": true
21
+ },
18
22
  {
19
23
  "type": "string",
20
24
  "named": true
@@ -76,6 +80,10 @@
76
80
  "type": "cmd",
77
81
  "named": true
78
82
  },
83
+ {
84
+ "type": "cond_exec",
85
+ "named": true
86
+ },
79
87
  {
80
88
  "type": "parenthesized",
81
89
  "named": true
@@ -211,6 +219,37 @@
211
219
  "named": true,
212
220
  "fields": {}
213
221
  },
222
+ {
223
+ "type": "paren_expression",
224
+ "named": true,
225
+ "fields": {},
226
+ "children": {
227
+ "multiple": true,
228
+ "required": false,
229
+ "types": [
230
+ {
231
+ "type": "argument_value",
232
+ "named": true
233
+ },
234
+ {
235
+ "type": "command_option",
236
+ "named": true
237
+ },
238
+ {
239
+ "type": "paren_expression",
240
+ "named": true
241
+ },
242
+ {
243
+ "type": "string",
244
+ "named": true
245
+ },
246
+ {
247
+ "type": "variable_reference",
248
+ "named": true
249
+ }
250
+ ]
251
+ }
252
+ },
214
253
  {
215
254
  "type": "parenthesized",
216
255
  "named": true,
@@ -431,7 +470,29 @@
431
470
  {
432
471
  "type": "variable_assignment",
433
472
  "named": true,
434
- "fields": {}
473
+ "fields": {},
474
+ "children": {
475
+ "multiple": true,
476
+ "required": true,
477
+ "types": [
478
+ {
479
+ "type": "assignment_value",
480
+ "named": true
481
+ },
482
+ {
483
+ "type": "set_keyword",
484
+ "named": true
485
+ },
486
+ {
487
+ "type": "set_option",
488
+ "named": true
489
+ },
490
+ {
491
+ "type": "variable_name",
492
+ "named": true
493
+ }
494
+ ]
495
+ }
435
496
  },
436
497
  {
437
498
  "type": "\"",
@@ -461,6 +522,10 @@
461
522
  "type": "argument_value",
462
523
  "named": true
463
524
  },
525
+ {
526
+ "type": "assignment_value",
527
+ "named": true
528
+ },
464
529
  {
465
530
  "type": "command_name",
466
531
  "named": true
@@ -501,10 +566,22 @@
501
566
  "type": "redirect_target",
502
567
  "named": true
503
568
  },
569
+ {
570
+ "type": "set_keyword",
571
+ "named": true
572
+ },
573
+ {
574
+ "type": "set_option",
575
+ "named": true
576
+ },
504
577
  {
505
578
  "type": "string",
506
579
  "named": true
507
580
  },
581
+ {
582
+ "type": "variable_name",
583
+ "named": true
584
+ },
508
585
  {
509
586
  "type": "variable_reference",
510
587
  "named": true