tree-sitter-batch 0.3.2 → 0.4.2

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
@@ -1,5 +1,6 @@
1
1
  const ci = (word) => new RegExp(word.split('').map((c) => /[a-zA-Z]/.test(c) ? `[${c.toLowerCase()}${c.toUpperCase()}]` : c).join(''));
2
2
  const kw = (word) => token(prec(10, ci(word)));
3
+ const operand = ($) => [$.cond_exec, $.pipe_stmt, $.cmd, $.parenthesized];
3
4
 
4
5
  export default grammar({
5
6
  name: 'batch',
@@ -11,7 +12,7 @@ export default grammar({
11
12
  $.echo_off, $.comment, $.label, $.variable_assignment,
12
13
  $.if_stmt, $.goto_stmt, $.call_stmt, $.exit_stmt,
13
14
  $.setlocal_stmt, $.endlocal_stmt, $.for_stmt,
14
- $.redirect_stmt, $.pipe_stmt, $.cond_exec,
15
+ $.redirect_stmt, $.pipe_stmt, $.cond_exec, $.command_sep,
15
16
  $.parenthesized, $.cmd,
16
17
  ),
17
18
  echo_off: () => prec(10, seq('@', kw('echo'), choice(kw('off'), kw('on')))),
@@ -20,12 +21,13 @@ export default grammar({
20
21
  seq('::', /[^\r\n]*/),
21
22
  ))),
22
23
  label: () => token(seq(':', /[a-zA-Z_][a-zA-Z0-9_-]*/)),
23
- variable_assignment: () => prec(8, seq(
24
- optional('@'), kw('set'),
25
- optional(seq(/[ \t]+/, /\/[aApP]/)), /[ \t]+/,
24
+ variable_assignment: ($) => prec(8, seq(
25
+ optional('@'), alias(kw('set'), $.set_keyword),
26
+ optional(seq(/[ \t]+/, alias(/\/[aApP]/, $.set_option))),
27
+ /[ \t]+/,
26
28
  choice(
27
- seq('"', /[a-zA-Z_][a-zA-Z0-9_]*/, '=', optional(/[^"\r\n]*/), '"'),
28
- seq(/[a-zA-Z_][a-zA-Z0-9_]*/, '=', optional(/[^\r\n]*/)),
29
+ seq('"', alias(/[a-zA-Z_][a-zA-Z0-9_]*/, $.variable_name), '=', optional(alias(/[^"\r\n]+/, $.assignment_value)), '"'),
30
+ seq(alias(/[a-zA-Z_][a-zA-Z0-9_]*/, $.variable_name), '=', optional(alias(/[^\r\n]+/, $.assignment_value))),
29
31
  ),
30
32
  )),
31
33
  if_stmt: ($) => prec.right(8, seq(
@@ -84,7 +86,7 @@ export default grammar({
84
86
  for_options: () => token(prec(10, choice(ci('/d'), seq(ci('/r'), optional(seq(/[ \t]+/, /[^\s]+/))), ci('/l'), seq(ci('/f'), optional(seq(/[ \t]+/, '"', /[^"]*/, '"')))))),
85
87
  for_variable: () => token(seq('%%', optional('~'), /[a-zA-Z]/)),
86
88
  for_set: () => /[^)\r\n]+/,
87
- parenthesized: ($) => seq('(', repeat(choice(seq($._stmt, /\r?\n/), /\r?\n/)), ')'),
89
+ parenthesized: ($) => seq('(', repeat(choice(seq($._stmt, /\r?\n/), /\r?\n/)), optional($._stmt), ')'),
88
90
  redirect_stmt: ($) => prec.right(4, seq(choice($.cmd, $.parenthesized), $.redirection)),
89
91
  redirection: ($) => prec.right(seq(
90
92
  optional(/[0-2]/), $.redirect_op, $.redirect_target,
@@ -94,9 +96,14 @@ export default grammar({
94
96
  redirect_target: () => token(choice(ci('nul'), ci('con'), /[^\s|&><\r\n]+/)),
95
97
  pipe_stmt: ($) => prec.left(3, seq(choice($.cmd, $.parenthesized), '|', choice($.cmd, $.parenthesized))),
96
98
  cond_exec: ($) => choice(
97
- prec.left(2, seq(choice($.cmd, $.parenthesized), '&&', choice($.cmd, $.parenthesized))),
98
- prec.left(1, seq(choice($.cmd, $.parenthesized), '||', choice($.cmd, $.parenthesized))),
99
+ prec.left(1, seq(choice(...operand($)), '&&', choice($.cmd, $.parenthesized))),
100
+ prec.left(1, seq(choice(...operand($)), '||', choice($.cmd, $.parenthesized))),
99
101
  ),
102
+ command_sep: ($) => prec.left(0, seq(
103
+ choice($.command_sep, ...operand($)),
104
+ '&',
105
+ choice(...operand($)),
106
+ )),
100
107
  variable_reference: () => token(choice(
101
108
  seq('%', /[a-zA-Z_][a-zA-Z0-9_]*/, '%'),
102
109
  seq('%~', /[a-zA-Z]*/, /[0-9]/),
@@ -109,9 +116,10 @@ export default grammar({
109
116
  cmd: ($) => prec.right(5, seq(optional('@'), $.command_name, optional($.argument_list))),
110
117
  command_name: () => /[a-zA-Z_][a-zA-Z0-9_.-]*/,
111
118
  argument_list: ($) => prec.right(repeat1($._arg)),
112
- _arg: ($) => choice($.string, $.variable_reference, $.command_option, $.argument_value),
119
+ _arg: ($) => choice($.string, $.variable_reference, $.command_option, $.paren_expression, $.argument_value),
113
120
  command_option: () => token(seq('/', /[a-zA-Z_?][a-zA-Z0-9_:]*/)),
114
- argument_value: () => /[^\s|&><"\r\n%!][^\s|&><"\r\n]*/,
121
+ paren_expression: ($) => seq('(', repeat($._arg), ')'),
122
+ argument_value: () => /(?:\^[&|<>^()]|[^\s|&><"\r\n%!()])(?:\^[&|<>^()]|[^\s|&><"\r\n()])*/,
115
123
  integer: () => /[0-9]+/,
116
124
  },
117
125
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tree-sitter-batch",
3
- "version": "0.3.2",
3
+ "version": "0.4.2",
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
@@ -87,6 +87,10 @@
87
87
  "type": "SYMBOL",
88
88
  "name": "cond_exec"
89
89
  },
90
+ {
91
+ "type": "SYMBOL",
92
+ "name": "command_sep"
93
+ },
90
94
  {
91
95
  "type": "SYMBOL",
92
96
  "name": "parenthesized"
@@ -250,15 +254,20 @@
250
254
  ]
251
255
  },
252
256
  {
253
- "type": "TOKEN",
257
+ "type": "ALIAS",
254
258
  "content": {
255
- "type": "PREC",
256
- "value": 10,
259
+ "type": "TOKEN",
257
260
  "content": {
258
- "type": "PATTERN",
259
- "value": "[sS][eE][tT]"
261
+ "type": "PREC",
262
+ "value": 10,
263
+ "content": {
264
+ "type": "PATTERN",
265
+ "value": "[sS][eE][tT]"
266
+ }
260
267
  }
261
- }
268
+ },
269
+ "named": true,
270
+ "value": "set_keyword"
262
271
  },
263
272
  {
264
273
  "type": "CHOICE",
@@ -271,8 +280,13 @@
271
280
  "value": "[ \\t]+"
272
281
  },
273
282
  {
274
- "type": "PATTERN",
275
- "value": "\\/[aApP]"
283
+ "type": "ALIAS",
284
+ "content": {
285
+ "type": "PATTERN",
286
+ "value": "\\/[aApP]"
287
+ },
288
+ "named": true,
289
+ "value": "set_option"
276
290
  }
277
291
  ]
278
292
  },
@@ -296,8 +310,13 @@
296
310
  "value": "\""
297
311
  },
298
312
  {
299
- "type": "PATTERN",
300
- "value": "[a-zA-Z_][a-zA-Z0-9_]*"
313
+ "type": "ALIAS",
314
+ "content": {
315
+ "type": "PATTERN",
316
+ "value": "[a-zA-Z_][a-zA-Z0-9_]*"
317
+ },
318
+ "named": true,
319
+ "value": "variable_name"
301
320
  },
302
321
  {
303
322
  "type": "STRING",
@@ -307,8 +326,13 @@
307
326
  "type": "CHOICE",
308
327
  "members": [
309
328
  {
310
- "type": "PATTERN",
311
- "value": "[^\"\\r\\n]*"
329
+ "type": "ALIAS",
330
+ "content": {
331
+ "type": "PATTERN",
332
+ "value": "[^\"\\r\\n]+"
333
+ },
334
+ "named": true,
335
+ "value": "assignment_value"
312
336
  },
313
337
  {
314
338
  "type": "BLANK"
@@ -325,8 +349,13 @@
325
349
  "type": "SEQ",
326
350
  "members": [
327
351
  {
328
- "type": "PATTERN",
329
- "value": "[a-zA-Z_][a-zA-Z0-9_]*"
352
+ "type": "ALIAS",
353
+ "content": {
354
+ "type": "PATTERN",
355
+ "value": "[a-zA-Z_][a-zA-Z0-9_]*"
356
+ },
357
+ "named": true,
358
+ "value": "variable_name"
330
359
  },
331
360
  {
332
361
  "type": "STRING",
@@ -336,8 +365,13 @@
336
365
  "type": "CHOICE",
337
366
  "members": [
338
367
  {
339
- "type": "PATTERN",
340
- "value": "[^\\r\\n]*"
368
+ "type": "ALIAS",
369
+ "content": {
370
+ "type": "PATTERN",
371
+ "value": "[^\\r\\n]+"
372
+ },
373
+ "named": true,
374
+ "value": "assignment_value"
341
375
  },
342
376
  {
343
377
  "type": "BLANK"
@@ -1228,6 +1262,18 @@
1228
1262
  ]
1229
1263
  }
1230
1264
  },
1265
+ {
1266
+ "type": "CHOICE",
1267
+ "members": [
1268
+ {
1269
+ "type": "SYMBOL",
1270
+ "name": "_stmt"
1271
+ },
1272
+ {
1273
+ "type": "BLANK"
1274
+ }
1275
+ ]
1276
+ },
1231
1277
  {
1232
1278
  "type": "STRING",
1233
1279
  "value": ")"
@@ -1422,13 +1468,21 @@
1422
1468
  "members": [
1423
1469
  {
1424
1470
  "type": "PREC_LEFT",
1425
- "value": 2,
1471
+ "value": 1,
1426
1472
  "content": {
1427
1473
  "type": "SEQ",
1428
1474
  "members": [
1429
1475
  {
1430
1476
  "type": "CHOICE",
1431
1477
  "members": [
1478
+ {
1479
+ "type": "SYMBOL",
1480
+ "name": "cond_exec"
1481
+ },
1482
+ {
1483
+ "type": "SYMBOL",
1484
+ "name": "pipe_stmt"
1485
+ },
1432
1486
  {
1433
1487
  "type": "SYMBOL",
1434
1488
  "name": "cmd"
@@ -1468,6 +1522,14 @@
1468
1522
  {
1469
1523
  "type": "CHOICE",
1470
1524
  "members": [
1525
+ {
1526
+ "type": "SYMBOL",
1527
+ "name": "cond_exec"
1528
+ },
1529
+ {
1530
+ "type": "SYMBOL",
1531
+ "name": "pipe_stmt"
1532
+ },
1471
1533
  {
1472
1534
  "type": "SYMBOL",
1473
1535
  "name": "cmd"
@@ -1500,6 +1562,65 @@
1500
1562
  }
1501
1563
  ]
1502
1564
  },
1565
+ "command_sep": {
1566
+ "type": "PREC_LEFT",
1567
+ "value": 0,
1568
+ "content": {
1569
+ "type": "SEQ",
1570
+ "members": [
1571
+ {
1572
+ "type": "CHOICE",
1573
+ "members": [
1574
+ {
1575
+ "type": "SYMBOL",
1576
+ "name": "command_sep"
1577
+ },
1578
+ {
1579
+ "type": "SYMBOL",
1580
+ "name": "cond_exec"
1581
+ },
1582
+ {
1583
+ "type": "SYMBOL",
1584
+ "name": "pipe_stmt"
1585
+ },
1586
+ {
1587
+ "type": "SYMBOL",
1588
+ "name": "cmd"
1589
+ },
1590
+ {
1591
+ "type": "SYMBOL",
1592
+ "name": "parenthesized"
1593
+ }
1594
+ ]
1595
+ },
1596
+ {
1597
+ "type": "STRING",
1598
+ "value": "&"
1599
+ },
1600
+ {
1601
+ "type": "CHOICE",
1602
+ "members": [
1603
+ {
1604
+ "type": "SYMBOL",
1605
+ "name": "cond_exec"
1606
+ },
1607
+ {
1608
+ "type": "SYMBOL",
1609
+ "name": "pipe_stmt"
1610
+ },
1611
+ {
1612
+ "type": "SYMBOL",
1613
+ "name": "cmd"
1614
+ },
1615
+ {
1616
+ "type": "SYMBOL",
1617
+ "name": "parenthesized"
1618
+ }
1619
+ ]
1620
+ }
1621
+ ]
1622
+ }
1623
+ },
1503
1624
  "variable_reference": {
1504
1625
  "type": "TOKEN",
1505
1626
  "content": {
@@ -1709,6 +1830,10 @@
1709
1830
  "type": "SYMBOL",
1710
1831
  "name": "command_option"
1711
1832
  },
1833
+ {
1834
+ "type": "SYMBOL",
1835
+ "name": "paren_expression"
1836
+ },
1712
1837
  {
1713
1838
  "type": "SYMBOL",
1714
1839
  "name": "argument_value"
@@ -1731,9 +1856,29 @@
1731
1856
  ]
1732
1857
  }
1733
1858
  },
1859
+ "paren_expression": {
1860
+ "type": "SEQ",
1861
+ "members": [
1862
+ {
1863
+ "type": "STRING",
1864
+ "value": "("
1865
+ },
1866
+ {
1867
+ "type": "REPEAT",
1868
+ "content": {
1869
+ "type": "SYMBOL",
1870
+ "name": "_arg"
1871
+ }
1872
+ },
1873
+ {
1874
+ "type": "STRING",
1875
+ "value": ")"
1876
+ }
1877
+ ]
1878
+ },
1734
1879
  "argument_value": {
1735
1880
  "type": "PATTERN",
1736
- "value": "[^\\s|&><\"\\r\\n%!][^\\s|&><\"\\r\\n]*"
1881
+ "value": "(?:\\^[&|<>^()]|[^\\s|&><\"\\r\\n%!()])(?:\\^[&|<>^()]|[^\\s|&><\"\\r\\n()])*"
1737
1882
  },
1738
1883
  "integer": {
1739
1884
  "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
@@ -64,6 +68,37 @@
64
68
  ]
65
69
  }
66
70
  },
71
+ {
72
+ "type": "command_sep",
73
+ "named": true,
74
+ "fields": {},
75
+ "children": {
76
+ "multiple": true,
77
+ "required": true,
78
+ "types": [
79
+ {
80
+ "type": "cmd",
81
+ "named": true
82
+ },
83
+ {
84
+ "type": "command_sep",
85
+ "named": true
86
+ },
87
+ {
88
+ "type": "cond_exec",
89
+ "named": true
90
+ },
91
+ {
92
+ "type": "parenthesized",
93
+ "named": true
94
+ },
95
+ {
96
+ "type": "pipe_stmt",
97
+ "named": true
98
+ }
99
+ ]
100
+ }
101
+ },
67
102
  {
68
103
  "type": "cond_exec",
69
104
  "named": true,
@@ -76,9 +111,17 @@
76
111
  "type": "cmd",
77
112
  "named": true
78
113
  },
114
+ {
115
+ "type": "cond_exec",
116
+ "named": true
117
+ },
79
118
  {
80
119
  "type": "parenthesized",
81
120
  "named": true
121
+ },
122
+ {
123
+ "type": "pipe_stmt",
124
+ "named": true
82
125
  }
83
126
  ]
84
127
  }
@@ -211,6 +254,37 @@
211
254
  "named": true,
212
255
  "fields": {}
213
256
  },
257
+ {
258
+ "type": "paren_expression",
259
+ "named": true,
260
+ "fields": {},
261
+ "children": {
262
+ "multiple": true,
263
+ "required": false,
264
+ "types": [
265
+ {
266
+ "type": "argument_value",
267
+ "named": true
268
+ },
269
+ {
270
+ "type": "command_option",
271
+ "named": true
272
+ },
273
+ {
274
+ "type": "paren_expression",
275
+ "named": true
276
+ },
277
+ {
278
+ "type": "string",
279
+ "named": true
280
+ },
281
+ {
282
+ "type": "variable_reference",
283
+ "named": true
284
+ }
285
+ ]
286
+ }
287
+ },
214
288
  {
215
289
  "type": "parenthesized",
216
290
  "named": true,
@@ -227,6 +301,10 @@
227
301
  "type": "cmd",
228
302
  "named": true
229
303
  },
304
+ {
305
+ "type": "command_sep",
306
+ "named": true
307
+ },
230
308
  {
231
309
  "type": "comment",
232
310
  "named": true
@@ -322,6 +400,10 @@
322
400
  "type": "cmd",
323
401
  "named": true
324
402
  },
403
+ {
404
+ "type": "command_sep",
405
+ "named": true
406
+ },
325
407
  {
326
408
  "type": "comment",
327
409
  "named": true
@@ -431,12 +513,38 @@
431
513
  {
432
514
  "type": "variable_assignment",
433
515
  "named": true,
434
- "fields": {}
516
+ "fields": {},
517
+ "children": {
518
+ "multiple": true,
519
+ "required": true,
520
+ "types": [
521
+ {
522
+ "type": "assignment_value",
523
+ "named": true
524
+ },
525
+ {
526
+ "type": "set_keyword",
527
+ "named": true
528
+ },
529
+ {
530
+ "type": "set_option",
531
+ "named": true
532
+ },
533
+ {
534
+ "type": "variable_name",
535
+ "named": true
536
+ }
537
+ ]
538
+ }
435
539
  },
436
540
  {
437
541
  "type": "\"",
438
542
  "named": false
439
543
  },
544
+ {
545
+ "type": "&",
546
+ "named": false
547
+ },
440
548
  {
441
549
  "type": "&&",
442
550
  "named": false
@@ -461,6 +569,10 @@
461
569
  "type": "argument_value",
462
570
  "named": true
463
571
  },
572
+ {
573
+ "type": "assignment_value",
574
+ "named": true
575
+ },
464
576
  {
465
577
  "type": "command_name",
466
578
  "named": true
@@ -501,10 +613,22 @@
501
613
  "type": "redirect_target",
502
614
  "named": true
503
615
  },
616
+ {
617
+ "type": "set_keyword",
618
+ "named": true
619
+ },
620
+ {
621
+ "type": "set_option",
622
+ "named": true
623
+ },
504
624
  {
505
625
  "type": "string",
506
626
  "named": true
507
627
  },
628
+ {
629
+ "type": "variable_name",
630
+ "named": true
631
+ },
508
632
  {
509
633
  "type": "variable_reference",
510
634
  "named": true