Skip to main content

m2_syn/schema/
nodes.rs

1m2_syn_macros::syntax! {
2    // Structural precedences with no single owning operator token; consumed
3    // directly by the native parser. Operator tokens below carry their own
4    // precedence numbers inline instead of naming a shared table entry.
5    precedence: {
6        PREC_CLOSER = 6,
7        PREC_SEMICOLON = 8,
8        PREC_COMMA = 10,
9        PREC_CONTROL = 12,
10        PREC_LOOP_CLAUSE = 16,
11        PREC_COLLECTION = 56,
12        PREC_APPLICATION_RIGHT = 61,
13        PREC_APPLICATION = 62,
14        PREC_QUOTE = 74,
15    }
16
17    // Each row carries its parsing precedence, then only what its role needs:
18    // `L`/`R` for a binary operator's associativity, and a unary strength
19    // only where it differs from the precedence.
20    tokens {
21        [->] [<-] [=] [=>]              { infix }               (14, R)
22        [>>]                            { bin, aug }            (14, R)
23        [<<]                            { bin, pref, aug }      (18, L)
24        [|-]                            { bin, pref, aug }      (20, L)
25        [===>]                          { bin, aug }            (22, R)
26        [<===]                          { bin, pref }           (22, R)
27        [<==>]                          { bin, aug }            (24, R)
28        [==>]                           { bin, aug }            (26, R)
29        [<==]                           { bin, pref }           (26, R)
30        [or]                            { bin }                 (28, R)
31        [??]                            { bin, pref, aug }      (28, R)
32        [xor]                           { bin }                 (30, R)
33        [and]                           { bin }                 (32, R)
34        [not]                           { pref }                (34)
35        [!=] [=!=] [==] [===]           { bin }                 (36, R)
36        [<] [<=] [>] [>=] [?]           { bin, pref }           (36, R)
37        [~]                             { bin, pref, aug }      (36, R)
38        [||]                            { bin, aug }            (38, L)
39        [:]                             { bin, aug }            (40, R)
40        [|]                             { bin, aug }            (42, L)
41        [^^]                            { bin, aug }            (44, L)
42        [&]                             { bin, aug }            (46, L)
43        [..] [..<]                      { bin, aug }            (48, L)
44        [++]                            { bin, aug }            (50, L)
45        [+] [-]                         { bin, pref, aug }      (50, L)
46        ["·"]                           { bin, aug }            (52, L)
47        [**] ["⊠"] ["⧢"]                { bin, aug }            (54, L)
48        [%] [/] [/ /]                   { bin, aug }            (58, L)
49        [*]                             { bin, pref, aug }      (58, L)
50        ["\\"] ["\\\\"]                 { bin, aug }            (58, R)
51        [@]                             { bin, aug }            (60, R)
52        [(*)]                           { post }                (64)
53        [@@] [@@?]                      { bin, aug }            (66, L)
54        [^*] [^~] [_*] [_~]             { post }                (68)
55        [#?] [.] [.?]                   { bin }                 (70, L)
56        [^<] [^<=] [^>] [^>=]           { bin }                 (70, L)
57        [_<] [_<=] [_>] [_>=]           { bin }                 (70, L)
58        [^] [^**] [_] [|_]              { bin, aug }            (70, L)
59        [#]                             { bin, pref }           (70, L, 61)
60        [!] [^!] [_!]                   { post }                (72)
61
62
63        [else] [then] [do] [list] [except]                      (12)
64        [when] [of] [in] [from] [to]                            (16)
65        [break] [continue] [return] [catch] [throw] [trap]
66        [if] [while] [try]
67        [TEST] [time] [timing] [elapsedTime] [elapsedTiming]
68        [breakpoint] [profile] [shield] [step] [finish]
69            (PREC_APPLICATION, PREC_CONTROL)
70        [for] [new]
71            (PREC_APPLICATION, PREC_LOOP_CLAUSE)
72        [symbol] [global] [local] [threadLocal] [threadVariable]
73            (PREC_APPLICATION, PREC_QUOTE)
74        [;] (PREC_SEMICOLON)
75        [,] (PREC_COMMA)
76    }
77
78    #[operator(prefix)]
79    enum PrefixOperator {}
80
81    #[operator(binary)]
82    enum BinaryOperator {}
83
84    #[operator(postfix)]
85    enum PostfixOperator {}
86
87    // Leaf nodes retain their source text. Their concrete names are obtained by
88    // converting the Rust name to snake_case, matching grammar.js.
89    #[leaf(qualified)]
90    struct Symbol;
91    #[leaf(empty)]
92    struct Empty;
93    #[leaf(empty)]
94    struct Adj;
95    #[leaf(eoc)]
96    struct Eoc;
97    #[leaf(empty)]
98    struct Eof;
99    struct EscapeSequence;
100    #[leaf(float)]
101    struct FloatLiteral;
102    #[leaf(integer)]
103    struct IntegerLiteral;
104    struct RawStringContent;
105    struct StringContent;
106
107    // `(_)` marks a field addressed positionally rather than by a CST field
108    // name. The macro may still give it an artificial Rust name, but that
109    // name is not a CST field. `Lines<T>` is `Vec<T>` printed one per line.
110    struct SourceFile {
111        cells: Lines<Cell>,
112        eof: Eof
113    }
114
115    struct Cell {
116        content: ExprPack,
117        semicolon: Token![;]?,
118        eoc: Eoc,
119    }
120
121    enum Component {
122        Expr,
123        Empty
124    }
125
126    struct ExprPack {
127        pack: Punctuated<Component>
128    }
129
130    struct Statement {
131        content: ExprPack,
132        semicolon: Token![;]
133    }
134
135
136    #[delimiter(bracket)]
137    struct Array {
138        statements: Vec<Statement>,
139        elements: ExprPack?
140    }
141
142    #[delimiter(brace)]
143    struct List {
144        statements: Vec<Statement>,
145        elements: ExprPack?
146    }
147
148    #[delimiter(angle_bar)]
149    struct AngleBarList {
150        statements: Vec<Statement>,
151        elements: ExprPack?
152    }
153
154    #[delimiter(parenthesis)]
155    struct Sequence {
156        statements: Vec<Statement>,
157        elements: ExprPack?
158    }
159
160    #[delimiter(parenthesis)]
161    struct Holder {
162        muted: Vec<Statement>,
163        value: Expr?,
164    }
165
166    #[literal(string)]
167    struct StringLiteral {
168        elements: Vec<StringElement>,
169    }
170
171    #[literal(raw_string)]
172    struct RawStringLiteral {
173        elements: Vec<RawStringElement>,
174    }
175
176    struct BinaryExpr {
177        left_operand: Expr,
178        operator: BinaryOperator,
179        right_operand: Expr,
180    }
181
182    struct AdjacentExpr {
183        left_operand: Expr,
184        operator: Adj,
185        right_operand: Expr,
186    }
187
188    struct PrefixExpr {
189        operator: PrefixOperator,
190        operand: Expr,
191    }
192
193    struct PostfixExpr {
194        operand: Expr,
195        operator: PostfixOperator,
196    }
197
198
199
200    enum LambdaParameters {
201        Variadic(Symbol),
202        FixedArity(Delimited<Punctuated<Symbol>?>)
203    }
204
205    struct LambdaExpression {
206        parameters: LambdaParameters,
207        operator: Token![->],
208        body: Expr,
209    }
210
211    struct GlobalBinding {
212        variable: Symbol,
213        operator: Token![=],
214        value: Expr,
215    }
216
217    struct LocalBinding {
218        variable: Symbol,
219        operator: Token![:=],
220        value: Expr,
221    }
222
223
224    struct TypeAnnotation {
225        typical_value: Expr,
226        operator: Token![=>]
227    }
228
229
230    struct OperatorAssignment {
231        argument_expr: OperatorExpr,
232        operator: Token![=],
233        value_expr: Expr,
234    }
235
236
237    struct OperatorInstallation {
238        domain_expr: OperatorExpr,
239        assign_op: Token![:=],
240        typical_value: TypeAnnotation?,
241        function: Expr,
242    }
243
244    struct MethodInstallation {
245        method: Expr,
246        signature: Expr,
247        assign_op: Token![:=],
248        typical_value: TypeAnnotation?,
249        function: Expr,
250    }
251
252    struct NewInstallation {
253        new_statement: NewStatement,
254        operator: Token![:=],
255        function: Expr,
256    }
257
258
259    enum Installation {
260        MethodInstallation,
261        OperatorInstallation,
262        NewInstallation,
263    }
264
265    enum AssignmentPackComponent {
266        AssignmentPack,
267        Empty,
268        Symbol,
269        OperatorExpr,
270    }
271
272    struct AssignmentPack*(Delimited<Punctuated<AssignmentPackComponent>?>)
273
274
275    struct GlobalParallelAssignment {
276        argument_pack: AssignmentPack,
277        operator: Token![=],
278        value: Expr,
279    }
280
281    struct LocalParallelAssignment {
282        argument_pack: AssignmentPack,
283        operator: Token![:=],
284        value: Expr,
285    }
286
287
288
289    enum SimpleBinding {
290        GlobalBinding,
291        LocalBinding,
292    }
293
294    enum ParallelAssignment {
295        GlobalParallelAssignment,
296        LocalParallelAssignment,
297    }
298
299    struct EvaluatedAssignment {
300        evaluated_domain: Expr,
301        operator: Token![<-],
302        value: Expr,
303    }
304
305    struct OptionExpression {
306        key: Expr,
307        operator: Token![=>],
308        value: Expr,
309    }
310
311    enum Assignment {
312        SimpleBinding,
313        ParallelAssignment,
314        EvaluatedAssignment,
315        Installation,
316        OperatorAssignment,
317    }
318
319
320
321    struct ThenClause {
322        then_token: Token![then],
323        expr: Expr,
324    }
325
326    struct ElseClause {
327        else_token: Token![else],
328        expr: Expr
329    }
330
331    struct IfStatement {
332        if_token: Token![if],
333        condition: Expr,
334        then_clause: ThenClause,
335        else_clause: ElseClause?,
336    }
337
338    struct RangeBoundFrom {
339        from_token: Token![from],
340        value: Expr,
341    }
342
343    struct RangeBoundTo {
344        to_token: Token![to],
345        value: Expr,
346    }
347
348    struct IterRange {
349        lower_bound: RangeBoundFrom?,
350        upper_bound: RangeBoundTo?,
351    }
352
353    struct IterIn {
354        in_token: Token![in],
355        collection: Expr,
356    }
357
358    struct WhenCondition {
359        when_token: Token![when],
360        condition: Expr,
361    }
362
363    struct ListStep {
364        list_token: Token![list],
365        expr: Expr,
366    }
367
368    struct DoStep {
369        do_token: Token![do],
370        statement: Expr,
371    }
372
373    struct LoopBody {
374        list_step: ListStep?,
375        do_step: DoStep?,
376    }
377
378    enum IterationDomain {
379        IterRange,
380        IterIn,
381    }
382
383    struct ForLoop {
384        for_token: Token![for],
385        variable: Symbol,
386        iteration_domain: IterationDomain?,
387        when_condition: WhenCondition?,
388        body: LoopBody,
389    }
390
391    struct WhileLoop {
392        while_token: Token![while],
393        condition: Expr,
394        body: LoopBody,
395    }
396
397    struct OfClass {
398        of_token: Token![of],
399        parent: Expr,
400    }
401
402    struct FromInstance {
403        from_token: Token![from],
404        instance: Expr,
405    }
406
407    struct NewStatement {
408        new_token: Token![new],
409        class: Expr,
410        parent_class: OfClass?,
411        instance: FromInstance?,
412    }
413
414
415    enum ProfilingModifier {
416        Token![elapsedTime],
417        Token![elapsedTiming],
418        Token![profile],
419        Token![time],
420        Token![timing],
421    }
422
423
424    struct ProfilingExpr {
425        profiling_modifier: ProfilingModifier,
426        value: Expr,
427    }
428
429    enum DebugKeyword {
430        Token![TEST],
431        Token![step],
432        Token![finish],
433        Token![shield],
434        Token![breakpoint],
435    }
436
437    struct DebugExpr {
438        debug_token: DebugKeyword,
439        value: Expr?,
440    }
441
442
443    struct BreakExpr {
444        break_token: Token![break],
445        value: Expr?,
446    }
447
448    struct ContinueExpr {
449        continue_token: Token![continue],
450        value: Expr?,
451    }
452
453    struct ReturnExpr {
454        return_token: Token![return],
455        value: Expr?,
456    }
457
458    enum ControlFlow {
459        BreakExpr,
460        ContinueExpr,
461        ReturnExpr,
462    }
463
464    struct Catch {
465        catch_token: Token![catch],
466        value: Expr,
467    }
468
469    struct Throw {
470        throw_token: Token![throw],
471        value: Expr,
472    }
473
474    struct Trap {
475        trap_token: Token![trap],
476        value: Expr,
477    }
478
479    struct ExceptDo {
480        except_token: Token![except],
481        exception: Symbol,
482        do_token: Token![do],
483        value: Expr,
484    }
485
486    enum  TryFallback {
487        ElseClause,
488        ExceptDo,
489    }
490
491    struct TryStatement {
492        try_token: Token![try],
493        value: Expr,
494        on_success: ThenClause?,
495        fallback: TryFallback?
496    }
497
498    enum QuoteSpecifier {
499        Token![symbol],
500        Token![local],
501        Token![global],
502        Token![threadVariable],
503        Token![threadLocal],
504    }
505
506    struct QuoteExpression {
507        specifier: QuoteSpecifier,
508        symbol: Symbol,
509    }
510
511    enum StringElement {
512        EscapeSequence,
513        StringContent,
514    }
515
516    enum RawStringElement {
517        EscapeSequence,
518        RawStringContent,
519    }
520
521    enum Collection {
522        Sequence,
523        List,
524        Array,
525        AngleBarList,
526    }
527
528    enum OperatorExpr {
529        AdjacentExpr,
530        BinaryExpr,
531        PrefixExpr,
532        PostfixExpr,
533    }
534
535    enum Literal {
536        FloatLiteral,
537        IntegerLiteral,
538        RawStringLiteral,
539        StringLiteral,
540    }
541
542    enum Expr* {
543        Literal,
544        Collection,
545        Holder,
546        OperatorExpr,
547        Assignment,
548        OptionExpression,
549        LambdaExpression,
550        IfStatement,
551        ForLoop,
552        WhileLoop,
553        TryStatement,
554        QuoteExpression,
555        NewStatement,
556        ControlFlow,
557        Catch,
558        Throw,
559        Trap,
560        DebugExpr,
561        ProfilingExpr,
562        Symbol,
563    }
564}