Repository navigation
Expand file tree
/
Copy pathgrammar.js
More file actions
1378 lines (1226 loc) · 46.1 KB
/
Copy pathgrammar.js
File metadata and controls
1378 lines (1226 loc) · 46.1 KB
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file Grammar for the LDMud dialect of LPC
* @author Daniel McCarney <daniel@binaryparadox.net>
* @license MIT
*/
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
// Precedence table — ordered low-to-high to match prolang.y's
// %left/%right/%nonassoc declarations (prolang.y line 9577).
const PREC = {
COMMA: -1,
ASSIGNMENT: 1,
CONDITIONAL: 2,
LOGICAL_OR: 3,
LOGICAL_AND: 4,
INCLUSIVE_OR: 5,
EXCLUSIVE_OR: 6,
BITWISE_AND: 7,
EQUAL: 8,
RELATIONAL: 9,
SHIFT: 10,
ADD: 11,
MULTIPLY: 12,
UNARY: 14,
CAST: 15,
POSTFIX: 16, // postfix ++ / --, member access, indexing, call
CALL: 17,
};
export default grammar({
name: "lpc",
extras: $ => [
/\s|\\\r?\n/,
$.comment,
],
word: $ => $.identifier,
conflicts: $ => [
// Modifier prefixes are shared by function def, global var, struct def,
// inherit, and the inheritance scope qualifier. The parser can't tell
// which one we're in until it sees the first non-modifier token.
[$.function_definition, $.global_var, $.struct_definition, $.inheritance],
// `int*` may be an array type or `int` followed by `*` from the next
// production. Prefer to consume `*` into the type.
[$.non_void_type, $._single_non_void_type],
// `IDENT::` and `STRING::` collide with `_expr_primary` (an
// identifier or string used as a value) until the parser sees `::`.
[$._expr_primary, $.scoped_function_name],
// `for(int x` — could be the start of the single-type
// `for(type d1, d2, ...)` shape or the mixed-type
// `for(for_init_decl, ...)` shape. Decided once we see the next
// type or expression after the first comma. `prec.dynamic` on the
// single-type alternative tilts the resolution toward it when both
// paths are valid.
[$._for_init, $.for_init_decl],
],
rules: {
source_file: $ => repeat($._definition),
// ---------------------------------------------------------------------
// Top-level definitions (prolang.y `def`, line ~9790)
// ---------------------------------------------------------------------
_definition: $ => choice(
$.function_definition,
$.global_var,
$.struct_definition,
$.inheritance,
$.default_visibility,
$.preproc_include,
$.preproc_def,
$.preproc_function_def,
$.preproc_undef,
$.preproc_pragma,
$.preproc_if,
$.preproc_ifdef,
$.preproc_call,
),
// ---------------------------------------------------------------------
// Preprocessor (handled in lex.c, but we expose a tree-sitter-c-style
// approximation so editors can navigate). Adapted from tree-sitter-c.
// ---------------------------------------------------------------------
preproc_include: $ => seq(
preprocessor('include'),
field('path', choice(
$.string_literal,
$.system_lib_string,
$.identifier,
)),
token.immediate(/\r?\n/),
),
system_lib_string: $ => token(seq(
'<',
repeat(choice(/[^>\n]/, '\\>')),
'>',
)),
preproc_def: $ => seq(
preprocessor('define'),
field('name', $.identifier),
field('value', optional($.preproc_arg)),
token.immediate(/\r?\n/),
),
preproc_function_def: $ => seq(
preprocessor('define'),
field('name', $.identifier),
field('parameters', $.preproc_params),
field('value', optional($.preproc_arg)),
token.immediate(/\r?\n/),
),
preproc_params: $ => seq(
token.immediate('('),
commaSep(choice($.identifier, '...')),
')',
),
preproc_undef: $ => seq(
preprocessor('undef'),
field('name', $.identifier),
token.immediate(/\r?\n/),
),
preproc_pragma: $ => seq(
preprocessor('pragma'),
field('value', commaSep1($.identifier)),
token.immediate(/\r?\n/),
),
preproc_call: $ => seq(
field('directive', $.preproc_directive),
field('argument', optional($.preproc_arg)),
token.immediate(/\r?\n/),
),
preproc_directive: $ => /#[ \t]*[a-zA-Z]\w*/,
preproc_arg: $ => token(prec(-1, repeat1(/.|\\\r?\n/))),
...preprocIf('', $ => $._definition),
...preprocIf('_in_statement', $ => $._statement),
// For aggregate literals (`({ ... })`, `'({ ... })`, struct
// literals) and call argument lists. The slot is an expression with
// an optional trailing comma, e.g. same shape as a regular slot so the
// branch contents are interchangeable with inline elements.
...preprocIf('_in_aggregate', $ => $._aggregate_slot),
_aggregate_slot: $ => choice(
seq($._expression, ','),
$._expression,
),
// A separate flavor of preproc for mapping literals: same `#if/#elif/
// #else/#endif` shape, but the wrapped slot is a `mapping_entry`
// (`"k": v[,]`) rather than a bare expression. The dunemud corpus
// frequently `#if`-wraps whole mapping entries (obj/soul2.c:756,
// obj/cmds/player/calc.c:434, etc.). Surfacing this as its own
// family — instead of widening `_aggregate_slot` — avoids the
// `_expression` vs `mapping_entry` ambiguity on a leading identifier
// that the comment above used to warn about: `mapping_entry`'s `:`
// half is optional, so adding it to `_aggregate_slot` would make
// every bare expression also match as a key-only `mapping_entry`.
...preprocIf('_in_mapping', $ => $._mapping_slot),
_mapping_slot: $ => choice(
seq($.mapping_entry, ','),
$.mapping_entry,
),
_preproc_expression: $ => choice(
$.identifier,
$.number_literal,
$.string_literal,
$.preproc_defined,
alias($.preproc_unary_expression, $.unary_expression),
alias($.preproc_binary_expression, $.binary_expression),
alias($.preproc_parenthesized_expression, $.parenthesized_expression),
alias($.preproc_call_expression, $.function_call),
),
preproc_parenthesized_expression: $ => seq(
'(', $._preproc_expression, ')',
),
preproc_defined: $ => choice(
seq('defined', '(', $.identifier, ')'),
seq('defined', $.identifier),
),
preproc_unary_expression: $ => prec.left(PREC.UNARY, seq(
field('operator', choice('!', '~', '-', '+')),
field('argument', $._preproc_expression),
)),
preproc_call_expression: $ => prec(PREC.CALL, seq(
field('function', $.identifier),
field('arguments', alias($.preproc_argument_list, $.argument_list)),
)),
preproc_argument_list: $ => seq(
'(', commaSep($._preproc_expression), ')',
),
preproc_binary_expression: $ => {
const table = [
['+', PREC.ADD],
['-', PREC.ADD],
['*', PREC.MULTIPLY],
['/', PREC.MULTIPLY],
['%', PREC.MULTIPLY],
['||', PREC.LOGICAL_OR],
['&&', PREC.LOGICAL_AND],
['|', PREC.INCLUSIVE_OR],
['^', PREC.EXCLUSIVE_OR],
['&', PREC.BITWISE_AND],
['==', PREC.EQUAL],
['!=', PREC.EQUAL],
['>', PREC.RELATIONAL],
['>=', PREC.RELATIONAL],
['<=', PREC.RELATIONAL],
['<', PREC.RELATIONAL],
['<<', PREC.SHIFT],
['>>', PREC.SHIFT],
['>>>', PREC.SHIFT],
];
return choice(...table.map(([op, p]) =>
prec.left(p, seq(
field('left', $._preproc_expression),
field('operator', op),
field('right', $._preproc_expression),
))));
},
// ---------------------------------------------------------------------
// Functions
// ---------------------------------------------------------------------
// prolang.y `function_def` (line 9804): `type L_IDENTIFIER ( argument )
// body`. `type` is `type_modifier_list opt_basic_type`, the basic
// type is optional (a typeless function defaults to mixed).
function_definition: $ => seq(
optional($.async_modifier),
repeat($.type_modifier),
field('type', optional($._basic_type)),
field('name', $.identifier),
field('parameters', $.parameter_list),
field('body', choice($.block, ';')),
),
parameter_list: $ => seq(
'(',
choice(
commaSep($.parameter),
'void',
),
')',
),
// prolang.y `new_arg_name` (line 11176): `non_void_type L_IDENTIFIER
// opt_default_value`. The basic-type half of `non_void_type` is
// optional (defaults to mixed). See `t-language/ti-types.c` for
// typeless params. `varargs` is technically part of
// `type_modifier_list` (the only modifier the driver lets through);
// we surface it as its own slot since real usage is always a single
// `varargs` prefix.
parameter: $ => seq(
optional('varargs'),
field('type', optional($.non_void_type)),
field('name', $.identifier),
optional(seq('=', field('default', $._expression))),
),
// ---------------------------------------------------------------------
// Globals: `int x, *y, z = 1;`: type then comma-separated names, each
// with optional `*`-prefix (legacy pointer-array decl) and initializer.
// ---------------------------------------------------------------------
global_var: $ => seq(
repeat($.type_modifier),
field('type', $._basic_type),
commaSep1($._global_declarator),
';',
),
_global_declarator: $ => seq(
repeat('*'),
field('name', $.identifier),
optional(seq('=', field('value', $._expression))),
),
// ---------------------------------------------------------------------
// Structs: prolang.y `struct_decl` (line 10434) covers forward-decl
// (`struct foo;`), full def (`struct foo { ... }`), and base-struct
// extension (`struct foo (bar) { ... }`).
// ---------------------------------------------------------------------
struct_definition: $ => seq(
repeat($.type_modifier),
'struct',
field('name', $.identifier),
optional($.struct_member_list),
';',
),
struct_member_list: $ => seq(
optional(seq('(', field('base', $.identifier), ')')),
'{',
repeat($.struct_member),
'}',
),
struct_member: $ => seq(
field('type', $.non_void_type),
commaSep1(seq(
repeat('*'),
field('name', $.identifier),
)),
';',
),
// ---------------------------------------------------------------------
// Inheritance & default visibility
// ---------------------------------------------------------------------
// prolang.y line 10560: `type_modifier_list inheritance_qualifier
// inheritance_modifier_list 'inherit' string_constant ';'`. We collapse
// the three pre-`inherit` lists into a single repeat(choice(...)).
//
// The source is a `string_constant` (prolang.y:13207). `identifier` is
// accepted as a macro stand-in: `inherit FOO+"x";` after preprocessor
// expansion is `inherit "...path..."+"x"`, which the bison grammar
// handles via `string_constant '+' L_STRING`. We accept the unexpanded
// form by admitting an identifier wherever a string literal would
// appear, since macro expansion happens in lex.c before bison sees it.
inheritance: $ => seq(
repeat(choice(
$.type_modifier,
$.inheritance_modifier,
$.inheritance_scope,
)),
'inherit',
field('source', $._string_constant),
';',
),
// prolang.y line 13207. Adjacent string literals (`"a" "b"`, no `+`)
// are also valid, but lex.c handles them by concatenating tokens
// before bison sees the result (see prolang.y:13217 fatal). Our
// `concatenated_string` covers the adjacent form; this rule covers
// the explicit `+` and parenthesised forms. `+` is left-associative
// per the bison rule `string_constant '+' L_STRING` (left-recursive).
_string_constant: $ => choice(
$.string_literal,
$.concatenated_string,
$.identifier, // macro stand-in
prec.left(seq($._string_constant, '+', $._string_constant)),
seq('(', $._string_constant, ')'),
),
inheritance_modifier: $ => 'virtual',
// prolang.y inheritance_qualifier (line 10814): the scope name
// (`functions` / `variables` / `structs`) isn't a lexer keyword.
// prolang.y string-compares the L_IDENTIFIER. Literal-string tokens
// here only match in inherit/default position, so these stay usable
// as ordinary identifiers elsewhere.
inheritance_scope: $ => choice('variables', 'functions', 'structs'),
default_visibility: $ => seq(
'default',
repeat(choice(
$.type_modifier,
$.inheritance_scope,
)),
';',
),
// ---------------------------------------------------------------------
// Types — prolang.y line 10948.
//
// non_void_type = type_modifier_list opt_basic_non_void_type
// basic_non_void_type = single_basic_non_void_type ('|' single...)*
// single_basic_non_void_type = atom | single...'*' | '<' non_void_type '>'
//
// `void` is only allowed at the top of `basic_type` (return type /
// parameter list "void"); arrays (`*`) and unions (`|`) are not.
// ---------------------------------------------------------------------
type_modifier: $ => choice(
'nomask',
'static',
'private',
'public',
'varargs',
'protected',
'nosave',
'deprecated',
'visible',
),
async_modifier: $ => 'async',
_basic_type: $ => choice(
$.non_void_type,
'void',
),
non_void_type: $ => sep1('|', $._single_non_void_type),
_single_non_void_type: $ => choice(
$._atomic_type,
prec.left(seq($._single_non_void_type, '*')),
),
_atomic_type: $ => choice(
'status',
'int',
'bytes',
'string',
'closure',
'coroutine',
'symbol',
'float',
'mapping',
'mixed',
'lpctype',
// `object "/blueprint"` / `lwobject "/blueprint"`. Blueprint slot
// takes a `simple_string_constant` (prolang.y:13190). We don't
// accept a bare identifier there, it would make `object obj;`
// ambiguous. We *do* accept the LDMud predefined `__FOO__` macros
// since they expand to strings before bison sees them and using
// them in this slot is idiomatic (`object __FILE__ var`).
prec.right(seq('object', optional(field('blueprint',
choice($.string_literal, $.concatenated_string, $.predefined_macro))))),
prec.right(seq('lwobject', optional(field('blueprint',
choice($.string_literal, $.concatenated_string, $.predefined_macro))))),
// `struct mixed` is the type-erased struct (prolang.y:11024);
// otherwise the struct_name shape from line 17264.
seq('struct', choice($.struct_name, 'mixed')),
seq('<', $.non_void_type, '>'),
),
// ---------------------------------------------------------------------
// Statements
// ---------------------------------------------------------------------
block: $ => seq('{', repeat($._statement), '}'),
_statement: $ => choice(
$.expression_statement,
$.local_var_decl,
$.return_statement,
$.if_statement,
$.while_statement,
$.do_statement,
$.for_statement,
$.foreach_statement,
$.switch_statement,
$.break_statement,
$.continue_statement,
$.block,
';',
$.preproc_if_in_statement,
$.preproc_ifdef_in_statement,
// Leaf preprocessor directives mid-function. `#pragma` flips a
// parser-state flag in the LDMud driver; we don't honor the
// semantic effect, but it has to *parse*. Real-world LPC also
// has stray `#define` / `#undef` / `#include` mid-function.
$.preproc_def,
$.preproc_function_def,
$.preproc_undef,
$.preproc_pragma,
$.preproc_include,
),
expression_statement: $ => seq($._comma_expression, ';'),
local_var_decl: $ => seq(
field('type', $._basic_type),
commaSep1($._local_declarator),
';',
),
_local_declarator: $ => seq(
repeat('*'),
field('name', $.identifier),
optional(seq('=', field('value', $._expression))),
),
return_statement: $ => seq(
'return',
optional($._comma_expression),
';',
),
break_statement: $ => seq('break', ';'),
continue_statement: $ => seq('continue', ';'),
if_statement: $ => prec.right(seq(
'if',
'(',
field('condition', $._comma_expression),
')',
field('consequence', $._statement),
optional(seq('else', field('alternative', $._statement))),
)),
while_statement: $ => seq(
'while',
'(', field('condition', $._comma_expression), ')',
field('body', $._statement),
),
do_statement: $ => seq(
'do',
field('body', $._statement),
'while',
'(', field('condition', $._comma_expression), ')',
';',
),
for_statement: $ => seq(
'for', '(',
field('init', optional($._for_init)),
';',
field('condition', optional($._comma_expression)),
';',
field('update', optional($._comma_expression)),
')',
field('body', $._statement),
),
// prolang.y `for_init_expr` (line 12182) → `comma_expr_decl` (line
// 12194), where each comma-separated element is either an `expr0`
// or a `local_name_lvalue ('=' expr0)?` typed declaration. Each
// element carries its own type, so `for(int i = 0, int j; ...; ...)`
// is valid LPC (surfaced by the dunemud lib).
//
// The single-type form `for(type d1, d2; ...)` is a dedicated shape
// — when every declarator shares a type, the tree mirrors a
// `local_var_decl` for parity. The mixed form admits each item as
// either an expression or a typed `for_init_decl`.
_for_init: $ => choice(
// Dynamic prec: when the for-init can be parsed *both* as a
// single-type list of declarators AND as the mixed-type list, we
// prefer the single-type shape (cleaner tree, matches the
// existing corpus). `prec.dynamic` resolves at parse time so the
// mixed-type form still wins when it's the only valid parse
// (e.g. the second item has its own type or is a bare expression).
prec.dynamic(1, seq(
field('type', $._basic_type),
commaSep1($._local_declarator),
)),
commaSep1($._for_init_item),
),
_for_init_item: $ => choice(
$._expression,
$.for_init_decl,
),
for_init_decl: $ => seq(
field('type', $._basic_type),
$._local_declarator,
),
foreach_statement: $ => seq(
'foreach', '(',
field('vars', sep1(',', $._foreach_var)),
choice(':', 'in'),
field('source', choice($._expression, $.foreach_range)),
')',
field('body', $._statement),
),
_foreach_var: $ => choice(
$.identifier,
seq(
field('type', $._basic_type),
repeat('*'),
field('name', $.identifier),
),
),
// `lower .. upper` form of foreach source. Distinct from the range
// syntax used in subscripts (`x[a..b]`) and in switch case ranges.
foreach_range: $ => seq(
field('lower', $._expression),
'..',
field('upper', $._expression),
),
switch_statement: $ => seq(
'switch',
'(', field('value', $._comma_expression), ')',
'{',
repeat(choice($.switch_case, $.preproc_if_in_statement, $.preproc_ifdef_in_statement)),
'}',
),
switch_case: $ => prec.right(seq(
choice(
'default',
seq('case', field('label', $.case_label),
optional(seq('..', field('upper', $.case_label)))),
),
':',
repeat($._statement),
)),
// prolang.y `case_label` (line 12948) accepts a folded numeric
// constant or a string/bytes constant. We additionally accept bare
// identifiers. They cover #defined constants like `case FOO:` that
// we can't expand without a real preprocessor.
case_label: $ => prec(1, choice(
$.number_literal,
$.string_literal,
$.concatenated_string,
$.bytes_literal,
$.char_literal,
$.identifier,
seq('-', $.case_label),
seq('+', $.case_label),
seq('!', $.case_label),
seq('~', $.case_label),
seq('(', $.case_label, ')'),
)),
// ---------------------------------------------------------------------
// Expressions
//
// prolang.y's precedence (lowest → highest):
// = += ... (right) ASSIGNMENT
// ?: (right) CONDITIONAL
// || LOGICAL_OR
// && LOGICAL_AND
// | INCLUSIVE_OR
// ^ EXCLUSIVE_OR
// & BITWISE_AND
// == != in EQUAL
// < <= > >= RELATIONAL
// << >> >>> SHIFT
// + - ADD
// * / % MULTIPLY
// prefix ! ~ - (cast) UNARY
// ++ -- . -> [ ( POSTFIX
// ---------------------------------------------------------------------
_comma_expression: $ => prec.left(PREC.COMMA, commaSep1($._expression)),
_expression: $ => choice(
$.assignment_expression,
$.conditional_expression,
$.binary_expression,
$.unary_expression,
$.update_expression,
$.cast_expression,
$.decl_cast_expression,
$.lvalue_ref_expression,
$._expr_primary,
),
assignment_expression: $ => prec.right(PREC.ASSIGNMENT, seq(
field('left', $._expression),
field('operator', $.assignment_operator),
field('right', $._expression),
)),
assignment_operator: $ => choice(
'=', '*=', '/=', '%=', '+=', '-=',
'<<=', '>>=', '>>>=',
'&=', '&&=', '^=', '|=', '||=',
),
conditional_expression: $ => prec.right(PREC.CONDITIONAL, seq(
field('condition', $._expression),
'?',
field('consequence', $._expression),
':',
field('alternative', $._expression),
)),
// All LPC binary operators are left-associative (prolang.y line 9577,
// every relevant token under `%left`).
binary_expression: $ => {
const table = [
['||', PREC.LOGICAL_OR],
['&&', PREC.LOGICAL_AND],
['|', PREC.INCLUSIVE_OR],
['^', PREC.EXCLUSIVE_OR],
['&', PREC.BITWISE_AND],
['==', PREC.EQUAL],
['!=', PREC.EQUAL],
['in', PREC.EQUAL],
['<', PREC.RELATIONAL],
['<=', PREC.RELATIONAL],
['>', PREC.RELATIONAL],
['>=', PREC.RELATIONAL],
['<<', PREC.SHIFT],
['>>', PREC.SHIFT],
['>>>', PREC.SHIFT],
['+', PREC.ADD],
['-', PREC.ADD],
['*', PREC.MULTIPLY],
['/', PREC.MULTIPLY],
['%', PREC.MULTIPLY],
];
return choice(...table.map(([op, p]) =>
prec.left(p, seq(
field('left', $._expression),
field('operator', op),
field('right', $._expression),
))));
},
unary_expression: $ => prec.right(PREC.UNARY, seq(
field('operator', choice('!', '~', '-', '+')),
field('argument', $._expression),
)),
update_expression: $ => {
const arg = field('argument', $._expression);
const op = field('operator', choice('--', '++'));
return choice(
prec.right(PREC.UNARY, seq(op, arg)),
prec.left(PREC.POSTFIX, seq(arg, op)),
);
},
// prolang.y `cast` (line 11074): `'(' single_basic_non_void_type ')'`.
// `void` is not accepted; a bare union must wrap in `<...>`, e.g.
// `(<int|string>)x`. We type the field as `non_void_type` (which is a
// single type or a `|`-union), slightly looser than prolang.y, but
// the union-without-`<...>` case is a semantic concern, not syntax.
cast_expression: $ => prec.right(PREC.CAST, seq(
'(', field('type', $.non_void_type), ')',
field('value', $._expression),
)),
// Decl-cast: `({ type })` value. prolang.y comments mark this as
// candidate for removal but it's still parsed (line 11081).
decl_cast_expression: $ => prec.right(PREC.CAST, seq(
'(', '{', field('type', $._basic_type), '}', ')',
field('value', $._expression),
)),
lvalue_ref_expression: $ => prec.right(PREC.UNARY, choice(
seq('&', $.identifier),
seq('&', '(', $._expression, ')'),
)),
// Primary expressions / postfix chain
_expr_primary: $ => choice(
$.identifier,
// LDMud predefined `__FOO__` macros. See `predefined_macro`. They
// expand to a string or integer at preprocessor time, so they
// appear in expression position; we don't expand, so the node
// stands in as an opaque primary.
$.predefined_macro,
// Function-like predefined macros: `__PATH__(n)` is the only one
// that takes an argument in practice (lex.c:858). The others
// (`__FILE__`, `__DIR__`, `__VERSION__`, …) are registered as
// nullary in lex.c (`-1` argcount) but the bare-token form is
// already covered by `predefined_macro` above.
$.predefined_macro_call,
$.number_literal,
$.string_literal,
$.concatenated_string,
$.bytes_literal,
$.concatenated_bytes_string,
$.char_literal,
$.symbol,
$.closure,
$.lpctype_literal,
$.array_literal,
$.quoted_aggregate,
$.mapping_literal,
$.empty_mapping_literal,
$.struct_literal,
$.parenthesized_expression,
$.function_call,
// `::ident` and `anchestor::ident` aren't valid as a bare value
// (the bison grammar wants the call-args), but admitting them as
// a primary lets `function_call`'s `(args)` postfix complete the
// form even when the parser entered through `parenthesized_expression`
// and the FIRST set otherwise blocked `::`. Surfaced by
// `return(::lfun(args))` in the dunemud lib (see
// `inherited_function_calls.txt`).
$.scoped_function_name,
$.call_other,
$.member_access,
$.subscript_expression,
$.range_expression,
$.inline_func,
$.inline_closure,
$.catch_expression,
$.await_expression,
$.yield_expression,
),
// `[basic_type]`. first-class lpctype value (prolang.y line 15369).
// E.g. `[int]`, `[int|string]`, `[mixed]`, `[void]`. Distinct from the
// subscript expression `x[i]` because it appears in primary position
// (no preceding object).
lpctype_literal: $ => seq('[', $._basic_type, ']'),
parenthesized_expression: $ => seq(
'(', $._comma_expression, ')',
),
// ----- Function call & call-other -----
// prolang.y `function_name` (line 18471): `ident`, `::ident`, or
// `anchestor::ident` where `anchestor` is a single string or
// identifier (line 18558 — concatenation isn't allowed there).
// Inlined into `function_call` (rather than a hidden helper) so
// that the `::ident(...)` form is reachable without precedence
// games — when the parser sees `(` followed by `::`, the only
// valid expansion is `parenthesized_expression → ... →
// function_call(scoped_function_name)`. Keeping `function_call` as
// a single rule with a top-level choice on the function slot lets
// tree-sitter compute that path without losing the `::` token to a
// ternary/mapping/struct-init context.
function_call: $ => prec(PREC.CALL, seq(
field('function', choice(
$.identifier,
$.scoped_function_name,
)),
field('arguments', $.argument_list),
)),
scoped_function_name: $ => choice(
seq('::', field('name', $.identifier)),
seq(field('anchestor', choice($.identifier, $.string_literal)),
'::', field('name', $.identifier)),
),
argument_list: $ => seq(
'(',
aggregateBody(seq($._expression, optional('...')), $),
')',
),
// Higher precedence than `member_access` so that `obj->fun(...)`
// shifts `(` into the call rather than reducing to a bare member
// access. (Surfaced once `_aggregate_slot` allowed bare-expression
// slots without a trailing comma to disambiguate.)
call_other: $ => prec(PREC.CALL, seq(
field('object', $._expression),
field('operator', choice('->', '.')),
field('function', $._member_or_lookup),
field('arguments', $.argument_list),
)),
member_access: $ => prec(PREC.POSTFIX, seq(
field('object', $._expression),
field('operator', choice('->', '.')),
field('member', $._member_or_lookup),
)),
// The name half of `obj->fun` / `obj->fun()` / `obj.field`. prolang.y
// accepts an identifier, a string constant, or a parenthesized
// expression that resolves to a string at runtime.
_member_or_lookup: $ => choice(
$.identifier,
$.string_literal,
$.concatenated_string,
seq('(', $._expression, ')'),
),
// ----- Indexing & ranges -----
subscript_expression: $ => prec(PREC.POSTFIX, seq(
field('object', $._expression),
'[',
optional($._index_anchor),
field('index', $._expression),
optional(seq(',', optional($._index_anchor), field('index2', $._expression))),
']',
)),
// `x[a..b]`: single-key range subscript.
// `x[k, a..b]` : two-key map range subscript: takes key `k` then a
// range over the value column. prolang.y has the full suite of
// anchor combos at lines 16760+; we model it permissively as an
// optional `key,` prefix.
range_expression: $ => prec(PREC.POSTFIX, seq(
field('object', $._expression),
'[',
optional(seq(field('key', $._expression), ',')),
optional(seq(optional($._index_anchor), field('lower', $._expression))),
'..',
optional(seq(optional($._index_anchor), field('upper', $._expression))),
']',
)),
// `<` = count from end, `>` = explicit from start (rare).
_index_anchor: $ => choice('<', '>'),
// ----- Catch / await / yield -----
// prolang.y `catch` (line 18655) + `opt_catch_mods` (line 18755).
// The modifier list is introduced by `;` and modifiers are
// comma-separated. `reserve`/`limit` take an expression, `nolog`/
// `publish` don't.
catch_expression: $ => seq(
'catch',
'(',
$._comma_expression,
optional(seq(';', commaSep1($.catch_modifier))),
')',
),
// prolang.y `opt_catch_modifier` (line 18803). LDMud parses every
// modifier as a bare identifier and validates names in the action;
// we bake the four documented names in directly. `reserve <expr>`
// and `limit <expr>` take an int expression; `nolog`/`publish` are
// bare.
catch_modifier: $ => choice(
'nolog',
'publish',
seq('reserve', $._expression),
seq('limit', $._expression),
),
// prolang.y coroutine_call (line 18578).
// await(coro) await(coro, expr)
// yield() yield(value) yield(value, coro)
await_expression: $ => seq(
'await', '(', $._expression, optional(seq(',', $._expression)), ')',
),
yield_expression: $ => seq(
'yield', '(',
optional(seq($._expression, optional(seq(',', $._expression)))),
')',
),
// ----- Closures and inline functions -----
// prolang.y `inline_func` (line 10063):
// inline_opt_async L_FUNC inline_opt_type inline_opt_args
// inline_opt_context inline_block
// `inline_opt_args` is either empty (synthesizing $1..$9 implicits)
// or `'(' argument ')'`, so the parameter list is *optional*. Body is
// always a block — no prototype form.
inline_func: $ => prec.right(seq(
optional($.async_modifier),
'function',
field('type', optional($._basic_type)),
field('parameters', optional($.parameter_list)),
field('context', optional($.inline_context)),
field('body', $.block),
)),
inline_context: $ => prec.right(seq(
':',
sep(';', $.local_var_decl_no_semi),
optional(';'),
)),
// Variant of local_var_decl without a trailing `;`, used inside the
// `: ... ;` context list of an inline function.
local_var_decl_no_semi: $ => seq(
field('type', $._basic_type),
commaSep1($._local_declarator),
),
// `(: ... :)` short-form closure. lex.c emits L_BEGIN_INLINE /
// L_END_INLINE as atomic tokens (lex.c:6119,6137) — no whitespace
// allowed between `(` and `:` or between `:` and `)`. Body is
// `statements inline_comma_expr` per prolang.y line 10260.
//
// We can't tokenize `(:` atomically because the corpus also
// contains `return(::lfun())` (no space between `(` and `::`); a
// greedy `(:` token swallows the first `:` of `::` and breaks the
// parse. tree-sitter's regex tokens don't support negative
// lookahead, so we instead spell the open as `(` immediately
// followed by `:`, where `token.immediate` enforces "no whitespace
// between" without committing the `:` early. The same applies to
// `:)` for symmetry with `:)`-vs-`:` (e.g., `x?:y` Elvis-of-if-else).
inline_closure: $ => seq(
'(', token.immediate(':'),
repeat($._statement),
commaSep($._expression),
':', token.immediate(')'),
),
// `#'<thing>` closure. lex.c's closure() emits a single L_CLOSURE
// token covering the whole form, so we tokenize as one atom too —
// avoids ambiguity with `#` preproc and `'` symbols. The full
// operator-closure list is lex.c symbol_operator (lines 1148–1677);