tree-sitter-beancount 2.1.0 → 2.1.3

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/grammar.js CHANGED
@@ -6,7 +6,7 @@ module.exports = grammar({
6
6
  name: "beancount",
7
7
 
8
8
  // Ensure we don't extract keywords from tokens
9
- //word: ($) => $.identifier,
9
+ word: ($) => $.identifier,
10
10
 
11
11
  inline: ($) => [
12
12
  ],
@@ -24,6 +24,11 @@ module.exports = grammar({
24
24
  /( |\r|\t)+/,
25
25
  ],
26
26
 
27
+ supertypes: $ => [
28
+ $._entry,
29
+ $._directive,
30
+ ],
31
+
27
32
  rules: {
28
33
  file: $ => repeat(
29
34
  choice(
@@ -63,57 +68,27 @@ module.exports = grammar({
63
68
  /* Types for terminal symbols */
64
69
  _indent: $ => token(/[ \r\t]+/),
65
70
  _eol: $ => token(/\n/),
66
- _pipe: $ => token('|'),
67
71
  atat: $ => token('@@'),
68
72
  at: $ => token('@'),
69
- lcurllcurl: $ => token('{{'),
70
- rcurlrcurl: $ => token('}}'),
71
- lcurl: $ => token('{'),
72
- rcurl: $ => token('}'),
73
- _equal: $ => token('='),
74
- _comma: $ => token(','),
75
- _tilde: $ => token('~'),
76
- _hash: $ => token('#'),
77
73
  asterisk: $ => token('*'),
78
74
  slash: $ => token('/'),
79
- _colon: $ => token(':'),
80
75
  plus: $ => token('+'),
81
76
  minus: $ => token('-'),
82
- _lparen: $ => token('('),
83
- _rparen: $ => token(')'),
84
77
  flag: $ => token(/[!&?%PSTCURM*#]/),
85
- TXN: $ => token('txn'),
86
- BALANCE: $ => token('balance'),
87
- OPEN: $ => token('open'),
88
- CLOSE: $ => token('close'),
89
- COMMODITY: $ => token('commodity'),
90
- PAD: $ => token('pad'),
91
- EVENT: $ => token('event'),
92
- PRICE: $ => token('price'),
93
- NOTE: $ => token('note'),
94
- DOCUMENT: $ => token('document'),
95
- QUERY: $ => token('query'),
96
- CUSTOM: $ => token('custom'),
97
- PUSHTAG: $ => token('pushtag'),
98
- POPTAG: $ => token('poptag'),
99
- PUSHMETA: $ => token('pushmeta'),
100
- POPMETA: $ => token('popmeta'),
101
- OPTION: $ => token('option'),
102
- INCLUDE: $ => token('include'),
103
- PLUGIN: $ => token('plugin'),
104
78
  _none: $ => token('NULL'),
105
79
  bool: $ => token(/TRUE|FALSE/),
106
80
  date: $ => token(/([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/),
81
+ //An account name is a colon-separated list of capitalized words which begin with a letter, and whose first word must be one of five account types:
82
+ //Each component of the account names begin with a capital letter or a number and are followed by letters, numbers or dash (-) characters.
83
+ //All other characters are disallowed.
107
84
  account: $ =>
108
85
  token(
109
86
  seq(
110
- /[A-Z]|[^\x00-\x7F]/,
111
- repeat(/[A-Za-z0-9\-]|[^\x00-\x7F]/),
87
+ /Assets|Liabilities|Equity|Income|Expenses/,
112
88
  repeat1(
113
89
  seq(
114
90
  ":",
115
- /[A-Z0-9]|[^\x00-\x7F]/,
116
- repeat(/[A-Za-z0-9\-]|[^\x00-\x7F]/),
91
+ /[\p{Lu}\p{N}][\p{L}\p{N}\-]*/,
117
92
  ),
118
93
  ),
119
94
  ),
@@ -123,7 +98,6 @@ module.exports = grammar({
123
98
  number: $ => token(/([0-9]+|[0-9][0-9,]+[0-9])(\.[0-9]*)?/),
124
99
  tag: $ => token(/#[A-Za-z0-9\-_/.]+/),
125
100
  link: $ => token(/\^[A-Za-z0-9\-_/.]+/),
126
- key: $ => token(/[a-z][a-zA-Z0-9\-_]+/),
127
101
 
128
102
  /* Operator precedence.
129
103
  * This is pulled straight out of the textbook example:
@@ -141,10 +115,10 @@ module.exports = grammar({
141
115
  /* Grammar Rules */
142
116
 
143
117
  txn: $ => choice(
144
- $.TXN,
118
+ "txn",
145
119
  $.flag,
146
- $.asterisk,
147
- $._hash
120
+ "*",
121
+ "#"
148
122
  ),
149
123
 
150
124
  _number_expr: $ =>
@@ -156,9 +130,9 @@ module.exports = grammar({
156
130
  ),
157
131
  _paren__number_expr: $ =>
158
132
  seq(
159
- $._lparen,
133
+ "(",
160
134
  $._number_expr,
161
- $._rparen,
135
+ ")",
162
136
  ),
163
137
  unary_number_expr: $ =>
164
138
  prec(3,
@@ -192,10 +166,13 @@ module.exports = grammar({
192
166
  ),
193
167
 
194
168
  // OPTIONAL
195
- txn_strings: $ =>
196
- seq(
197
- $.string,
198
- optional($.string),
169
+ _txn_strings: $ =>
170
+ choice(
171
+ seq(
172
+ alias($.string, $.payee),
173
+ alias($.string, $.narration),
174
+ ),
175
+ alias($.string, $.narration),
199
176
  ),
200
177
 
201
178
  // OPTIONAL
@@ -214,18 +191,40 @@ module.exports = grammar({
214
191
  seq(
215
192
  field("date", $.date),
216
193
  field("txn", $.txn),
217
- field("txn_strings", optional($.txn_strings)),
194
+ optional($._txn_strings),
218
195
  field("tags_links", optional($.tags_links)),
219
196
  field("comment", optional($.comment)),
220
197
  $._eol,
221
- field("posting_or_kv_list", optional($.posting_or_kv_list)),
198
+ optional(
199
+ repeat1(
200
+ choice(
201
+ seq(
202
+ $._indent,
203
+ $._eol
204
+ ),
205
+ seq(
206
+ $._indent,
207
+ $.tags_links,
208
+ $._eol
209
+ ),
210
+ $._key_value_line,
211
+ $.posting,
212
+ seq(
213
+ $._indent,
214
+ $.comment,
215
+ $._eol
216
+ )
217
+ )
218
+ ),
219
+ ),
220
+
222
221
  ),
223
222
 
224
223
  // OPTIONAL
225
224
  optflag: $ =>
226
225
  choice(
227
- $.asterisk,
228
- $._hash,
226
+ "*",
227
+ "#",
229
228
  $.flag,
230
229
  ),
231
230
 
@@ -241,8 +240,6 @@ module.exports = grammar({
241
240
  // )
242
241
  //),
243
242
 
244
- //account: $ => $.ACCOUNT,
245
-
246
243
  posting: $ =>
247
244
  choice(
248
245
  seq(
@@ -286,21 +283,9 @@ module.exports = grammar({
286
283
  )
287
284
  ),
288
285
 
289
- key_value: $ =>
290
- prec.left(seq(
291
- $.key,
292
- $._colon,
293
- optional($._key_value_value),
294
- )),
295
-
296
- key_value_line: $ => seq(
297
- $._indent,
298
- $.key_value,
299
- $._eol
300
- ),
286
+ key: $ => token(/[a-z][a-zA-Z0-9\-_]+/),
301
287
 
302
- // OPTIONAL
303
- _key_value_value: $ =>
288
+ value: $ =>
304
289
  choice(
305
290
  $.string,
306
291
  $.account,
@@ -313,31 +298,26 @@ module.exports = grammar({
313
298
  $.amount
314
299
  ),
315
300
 
316
- // OPTIONAL
317
- posting_or_kv_list: $ =>
318
- repeat1(
319
- choice(
320
- seq(
321
- $._indent,
322
- $._eol
323
- ),
324
- seq(
325
- $._indent,
326
- $.tags_links,
327
- $._eol
328
- ),
329
- $.key_value_line,
330
- $.posting,
331
- seq(
332
- $._indent,
333
- $.comment,
334
- $._eol
335
- )
336
- )
337
- ),
301
+ key_value: $ =>
302
+ prec.left(seq(
303
+ $.key,
304
+ ":",
305
+ $.value,
306
+ )),
307
+
308
+ _key_value_line: $ => seq(
309
+ $._indent,
310
+ /*prec.left(seq(
311
+ $.key,
312
+ ":",
313
+ $.value,
314
+ )),*/
315
+ $.key_value,
316
+ $._eol
317
+ ),
338
318
 
339
319
  // OPTIONAL
340
- key_value_list: $ =>
320
+ _key_value_list: $ =>
341
321
  repeat1(
342
322
  choice(
343
323
  seq(
@@ -345,58 +325,58 @@ module.exports = grammar({
345
325
  $._eol
346
326
  ),
347
327
  seq(
348
- $.key_value_line
328
+ $._key_value_line
349
329
  )
350
330
  )
351
331
  ),
352
332
 
353
333
  // OPTIONAL
354
- currency_list: $ =>
355
- seq(
356
- $.currency,
357
- repeat(
358
- seq(
359
- $._comma,
360
- $.currency
361
- )
362
- )
363
- ),
364
334
 
365
335
  pushtag: $ => seq(
366
- alias($.PUSHTAG, "pushtag"),
336
+ "pushtag",
367
337
  $.tag,
368
338
  $._eol
369
339
  ),
370
340
 
371
341
  poptag: $ => seq(
372
- alias($.POPTAG, "poptag"),
342
+ "poptag",
373
343
  $.tag,
374
344
  $._eol
375
345
  ),
376
346
 
377
347
  pushmeta: $ => seq(
378
- alias($.PUSHMETA, "pushmeta"),
348
+ "pushmeta",
379
349
  $.key_value,
380
350
  $._eol
381
351
  ),
382
352
 
383
353
  popmeta: $ => seq(
384
- alias($.POPMETA, "popmeta"),
354
+ "popmeta",
385
355
  $.key,
386
- $._colon,
356
+ ":",
387
357
  $._eol
388
358
  ),
389
359
 
390
360
  open: $ =>
391
361
  seq(
392
362
  field("date", $.date),
393
- alias($.OPEN, "open"),
363
+ "open",
394
364
  field("account", $.account),
395
- field("currencies", repeat($.currency_list)),
365
+ field("currencies", repeat(
366
+ seq(
367
+ $.currency,
368
+ repeat(
369
+ seq(
370
+ ",",
371
+ $.currency
372
+ )
373
+ )
374
+ ),
375
+ )),
396
376
  field("opt_booking", optional($.opt_booking)),
397
377
  field("comment", optional($.comment)),
398
378
  $._eol,
399
- optional($.key_value_list)
379
+ optional($._key_value_list)
400
380
  ),
401
381
 
402
382
  // OPTIONAL
@@ -405,38 +385,38 @@ module.exports = grammar({
405
385
  close: $ =>
406
386
  seq(
407
387
  field("date", $.date),
408
- alias($.CLOSE, "close"),
388
+ "close",
409
389
  field("account", $.account),
410
390
  field("comment", optional($.comment)),
411
391
  $._eol,
412
- optional($.key_value_list)
392
+ optional($._key_value_list)
413
393
  ),
414
394
 
415
395
  commodity: $ =>
416
396
  seq(
417
397
  field("date", $.date),
418
- alias($.COMMODITY, "commodity"),
398
+ "commodity",
419
399
  field("currency", $.currency),
420
400
  field("comment", optional($.comment)),
421
401
  $._eol,
422
- optional($.key_value_list)
402
+ optional($._key_value_list)
423
403
  ),
424
404
 
425
405
  pad: $ =>
426
406
  seq(
427
407
  field("date", $.date),
428
- alias($.PAD, "pad"),
408
+ "pad",
429
409
  field("account", $.account),
430
410
  field("from_account", $.account),
431
411
  field("comment", optional($.comment)),
432
412
  $._eol,
433
- optional($.key_value_list)
413
+ optional($._key_value_list)
434
414
  ),
435
415
 
436
416
  balance: $ =>
437
417
  seq(
438
418
  field("date", $.date),
439
- alias($.BALANCE, "balance"),
419
+ "balance",
440
420
  field("account", $.account),
441
421
  field("amount",
442
422
  //choice(
@@ -446,7 +426,7 @@ module.exports = grammar({
446
426
  ),
447
427
  field("comment", optional($.comment)),
448
428
  $._eol,
449
- optional($.key_value_list)
429
+ optional($._key_value_list)
450
430
  ),
451
431
 
452
432
  amount: $ =>
@@ -463,7 +443,7 @@ module.exports = grammar({
463
443
  ),
464
444
  seq(
465
445
  $._number_expr,
466
- $._tilde,
446
+ "~",
467
447
  $._number_expr,
468
448
  $.currency
469
449
  )
@@ -487,7 +467,7 @@ module.exports = grammar({
487
467
  ),
488
468
  seq(
489
469
  field("per", optional($._number_expr)),
490
- $._hash,
470
+ "#",
491
471
  field("total", optional($._number_expr)),
492
472
  field("currency", $.currency)
493
473
  ),
@@ -505,24 +485,24 @@ module.exports = grammar({
505
485
  cost_spec: $ =>
506
486
  choice(
507
487
  seq(
508
- $.lcurl,
509
- field("cost_comp_list", optional($.cost_comp_list)),
510
- $.rcurl
488
+ "{",
489
+ field("cost_comp_list", optional($._cost_comp_list)),
490
+ "}"
511
491
  ),
512
492
  seq(
513
- $.lcurllcurl,
514
- field("cost_comp_list", optional($.cost_comp_list)),
515
- $.rcurlrcurl
493
+ "{{",
494
+ field("cost_comp_list", optional($._cost_comp_list)),
495
+ "}}"
516
496
  ),
517
497
  ),
518
498
 
519
499
  // OPTIONAL
520
- cost_comp_list: $ =>
500
+ _cost_comp_list: $ =>
521
501
  seq(
522
502
  $.cost_comp,
523
503
  repeat(
524
504
  seq(
525
- $._comma,
505
+ ",",
526
506
  $.cost_comp
527
507
  )
528
508
  )
@@ -533,47 +513,47 @@ module.exports = grammar({
533
513
  $.compound_amount,
534
514
  $.date,
535
515
  $.string,
536
- $.asterisk
516
+ "*"
537
517
  ),
538
518
 
539
519
  price: $ =>
540
520
  seq(
541
521
  field("date", $.date),
542
- alias($.PRICE, "price"),
522
+ "price",
543
523
  field("currency", $.currency),
544
524
  field("amount", $.amount),
545
525
  $._eol,
546
- optional($.key_value_list)
526
+ optional($._key_value_list)
547
527
  ),
548
528
 
549
529
  event: $ =>
550
530
  seq(
551
531
  field("date", $.date),
552
- alias($.EVENT, "event"),
532
+ "event",
553
533
  field("type", $.string),
554
534
  field("desc", $.string),
555
535
  $._eol,
556
- optional($.key_value_list)
536
+ optional($._key_value_list)
557
537
  ),
558
538
 
559
539
  query: $ =>
560
540
  seq(
561
541
  field("date", $.date),
562
- alias($.QUERY, "query"),
542
+ "query",
563
543
  field("name", $.string),
564
544
  field("query", $.string),
565
545
  $._eol,
566
- optional($.key_value_list)
546
+ optional($._key_value_list)
567
547
  ),
568
548
 
569
549
  note: $ =>
570
550
  seq(
571
551
  field("date", $.date),
572
- alias($.NOTE, "note"),
552
+ "note",
573
553
  field("account", $.account),
574
554
  field("note", $.string),
575
555
  $._eol,
576
- optional($.key_value_list)
556
+ optional($._key_value_list)
577
557
  ),
578
558
 
579
559
  filename: $ => $.string,
@@ -581,12 +561,12 @@ module.exports = grammar({
581
561
  document: $ =>
582
562
  seq(
583
563
  field("date", $.date),
584
- alias($.DOCUMENT, "document"),
564
+ "document",
585
565
  field("account", $.account),
586
566
  field("filename", $.filename),
587
567
  field("tags_links", optional($.tags_links)),
588
568
  $._eol,
589
- optional($.key_value_list)
569
+ optional($._key_value_list)
590
570
  ),
591
571
 
592
572
  custom_value: $ =>
@@ -599,19 +579,19 @@ module.exports = grammar({
599
579
  $.account
600
580
  ),
601
581
 
602
- custom_value_list: $ =>
603
- repeat1(
604
- $.custom_value
605
- ),
606
582
 
607
583
  custom: $ =>
608
584
  seq(
609
585
  field("date", $.date),
610
- alias($.CUSTOM, "custom"),
586
+ "custom",
611
587
  field("name", $.string),
612
- field("custom_value_list", optional($.custom_value_list)),
588
+ field("custom_value_list", optional(
589
+ repeat1(
590
+ $.custom_value
591
+ ),
592
+ )),
613
593
  $._eol,
614
- optional($.key_value_list)
594
+ optional($._key_value_list)
615
595
  ),
616
596
 
617
597
  _entry: $ =>
@@ -631,14 +611,14 @@ module.exports = grammar({
631
611
  ),
632
612
 
633
613
  option: $ => seq(
634
- alias($.OPTION, "option"),
614
+ "option",
635
615
  field("key", $.string),
636
616
  field("value", $.string),
637
617
  $._eol,
638
618
  ),
639
619
 
640
620
  include: $ => seq(
641
- alias($.INCLUDE, "include"),
621
+ "include",
642
622
  $.string,
643
623
  $._eol,
644
624
  ),
@@ -646,12 +626,12 @@ module.exports = grammar({
646
626
  plugin: $ =>
647
627
  choice(
648
628
  seq(
649
- alias($.PLUGIN, "plugin"),
629
+ "plugin",
650
630
  $.string,
651
631
  $._eol
652
632
  ),
653
633
  seq(
654
- alias($.PLUGIN, "plugin"),
634
+ "plugin",
655
635
  $.string,
656
636
  $.string,
657
637
  $._eol
@@ -681,7 +661,7 @@ module.exports = grammar({
681
661
  comment: $ => seq(';', /.*/),
682
662
 
683
663
  // NOTE: includes reserved identifiers
684
- identifier: $ => /[_a-zA-Z0-9]+/,
664
+ identifier: $ => /[a-z]+/,
685
665
 
686
666
  _skipped_lines: $ =>
687
667
  choice(
@@ -691,7 +671,7 @@ module.exports = grammar({
691
671
  $._eol
692
672
  ),
693
673
  seq(
694
- $._colon,
674
+ ":",
695
675
  /.*/,
696
676
  $._eol
697
677
  ),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tree-sitter-beancount",
3
- "version": "2.1.0",
3
+ "version": "2.1.3",
4
4
  "description": "a tree-sitter parser for the beancount syntax",
5
5
  "main": "bindings/node",
6
6
  "scripts": {
@@ -25,6 +25,6 @@
25
25
  "nan": "^2.15.0"
26
26
  },
27
27
  "devDependencies": {
28
- "tree-sitter-cli": "0.20.0"
28
+ "tree-sitter-cli": "^0.20.0"
29
29
  }
30
30
  }