m2-syn 0.6.1

Parser, typed CST, traversal and procedural macros for Macaulay2
Documentation
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
crate::syntax! {
// P = precedence, B = binary binding strength, U = unary binding strength.
// `L` and `R` derive B from P. Columns absent for a role are omitted, and
// spellings with identical metadata share one row.
tokens {
    // Operators                          roles                 (P, B, U)
    [->] [<-] [=] [=>]                   { infix }              (14, R)
    [>>]                                 { bin, aug }           (14, R)
    [<<]                                 { bin, pref, aug }     (18, L)
    [|-]                                 { bin, pref, aug }     (20, L)
    [===>]                               { bin, aug }           (22, R)
    [<===]                               { bin, pref }          (22, R)
    [<==>]                               { bin, aug }           (24, R)
    [==>]                                { bin, aug }           (26, R)
    [<==]                                { bin, pref }          (26, R)
    [or]                                 { bin }                (28, R)
    [??]                                 { bin, pref, aug }     (28, R)
    [xor]                                { bin }                (30, R)
    [and]                                { bin }                (32, R)
    [not]                                { pref }               (34)
    [!=] [=!=] [==] [===]                { bin }                (36, R)
    [<] [<=] [>] [>=] [?]                { bin, pref }          (36, R)
    [~]                                  { bin, pref, aug }     (36, R)
    [||]                                 { bin, aug }           (38, L)
    [:]                                  { bin, aug }           (40, R)
    [|]                                  { bin, aug }           (42, L)
    [^^]                                 { bin, aug }           (44, L)
    [&]                                  { bin, aug }           (46, L)
    [..] [..<]                           { bin, aug }           (48, L)
    [++]                                 { bin, aug }           (50, L)
    [+] [-]                              { bin, pref, aug }     (50, L)
    ["·"]                                { bin, aug }           (52, L)
    [**] ["⊠"] ["⧢"]                     { bin, aug }           (54, L)
    [%] [/] [/ /]                        { bin, aug }           (58, L)
    [*]                                  { bin, pref, aug }     (58, L)
    ["\\"] ["\\\\"]                      { bin, aug }           (58, R)
    [@]                                  { bin, aug }           (60, R)
    [(*)]                                { post }               (64)
    [@@] [@@?]                           { bin, aug }           (66, L)
    [^*] [^~] [_*] [_~]                  { post }               (68)
    [#?] [.] [.?]                        { bin }                (70, L)
    [^<] [^<=] [^>] [^>=]                { bin }                (70, L)
    [_<] [_<=] [_>] [_>=]                { bin }                (70, L)
    [^] [^**] [_] [|_]                   { bin, aug }           (70, L)
    [#]                                  { bin, pref }          (70, L, 61)
    [!] [^!] [_!]                        { post }               (72)

    // Keywords
    [else] [then] [do] [list] [except]                          (12)
    [when] [of] [in] [from] [to]                                (16)
    [break] [continue] [return]
    [catch] [throw] [trap]
    [if] [while] [try]
    [TEST] [time] [timing]
    [elapsedTime] [elapsedTiming]
    [breakpoint] [profile] [shield]
    [step] [finish]                                             (62, 12)
    [for] [new]                                                 (62, 16)
    [symbol] [global] [local] [threadLocal] [threadVariable]    (62, 74)

    // Separators
    [;]                                                         (8)
    [,]                                                         (10)
}

// Leaves retain their source text. Canonical names select the specialized
// storage and parsing behavior supplied by the schema macro.
strutuct! { Symbol }
strutuct! { Empty }
strutuct! { Adj }
strutuct! { EOC }
strutuct! { BOF }
strutuct! { EOF }
strutuct! { EscapeSequence }
strutuct! { FloatLiteral }
strutuct! { IntegerLiteral }
strutuct! { RawStringContent }
strutuct! { StringContent }

// `Lines<T>` is stored as `Vec<T>` and printed one element per line.
#[comments]
#[cell_stream]
strutuct!{ SourceFile
    bof: BOF,
    cells: Lines<Cell>,
    eof: EOF,
}

#[comments]
strutuct!{ Cell
    content: PackedContent,
    semicolon: Token![;]?,
    eoc: EOC,
}

strutuct!{ Component
    Expr(Expr*)
    Empty(Empty)
}

strutuct!{ ExprPack
    pack: Punctuated<Component>,
}

strutuct!{ PackedContent
    Expr(Expr*)
    Pack(ExprPack)
}

strutuct!{ Statement
    content: PackedContent,
    semicolon: Token![;],
}

#[delimiter(bracket)]
strutuct!{ Array
    statements: Vec<Statement>,
    elements: PackedContent?,
}

#[delimiter(brace)]
strutuct!{ List
    statements: Vec<Statement>,
    elements: PackedContent?,
}

#[delimiter(angle_bar)]
strutuct!{ AngleBarList
    statements: Vec<Statement>,
    elements: PackedContent?,
}

#[delimiter(parenthesis)]
strutuct!{ Sequence
    statements: Vec<Statement>,
    elements: PackedContent?,
}

#[delimiter(parenthesis)]
strutuct!{ Holder
    muted: Vec<Statement>,
    value: Expr*?,
}

strutuct!{ StringElement
    Escape(EscapeSequence)
    Content(StringContent)
}

#[literal(string)]
strutuct!{ StringLiteral
    elements: Vec<StringElement>,
}

strutuct!{ RawStringElement
    Escape(EscapeSequence)
    Content(RawStringContent)
}

#[literal(raw_string)]
strutuct!{ RawStringLiteral
    elements: Vec<RawStringElement>,
}

strutuct!{ AssignmentArgument
    List(AssignmentList*)
    Empty(Empty)
    Symbol(Symbol)
    Op(ExprOp)
    // `x <- y` as a target: M2 reads it as an installation target for `<-`.
    Evaluated(ExprAssignEvaluated)
}

strutuct!{ AssignmentListContent
    Argument(AssignmentArgument)
    Pack {
        arguments: Punctuated<AssignmentArgument>,
    }
}

strutuct!{ AssignmentList
    contents: Delimited<Option<AssignmentListContent>>,
}

strutuct!{ ArgumentListContent
    Symbol(Symbol)
    Pack {
        arguments: Punctuated<Symbol>,
    }
}

// These products are shared by otherwise independent expression families.
strutuct!{ TypeAnnotation
    typical_value: Expr*,
    operator: Token![=>],
}

strutuct!{ ThenClause
    then_token: Token![then],
    expr: Expr*,
}

strutuct!{ ElseClause
    else_token: Token![else],
    expr: Expr*,
}

strutuct!{ RangeBoundFrom
    from_token: Token![from],
    value: Expr*,
}

strutuct!{ RangeBoundTo
    to_token: Token![to],
    value: Expr*,
}

strutuct!{ WhenCondition
    when_token: Token![when],
    condition: Expr*,
}

strutuct!{ ListStep
    list_token: Token![list],
    expr: Expr*,
}

strutuct!{ DoStep
    do_token: Token![do],
    statement: Expr*,
}

strutuct!{ LoopBody
    list_step: ListStep?,
    do_step: DoStep?,
}

strutuct!{ OfClass
    of_token: Token![of],
    parent: Expr*,
}

strutuct!{ FromInstance
    from_token: Token![from],
    instance: Expr*,
}

strutuct!{ TryFallback
    Else(ElseClause)
    Except {
        except_token: Token![except],
        exception: Symbol,
        do_token: Token![do],
        value: Expr*,
    }
}

// Semantic alternatives are nested so every layer becomes a dispatch point.
// The short layer names keep the generated cumulative type names readable.
strutuct!{ Expr
    Literal {
        Float(FloatLiteral)
        Integer(IntegerLiteral)
        RawString(RawStringLiteral)
        String(StringLiteral)
    }

    Collection {
        Sequence(Sequence)
        List(List)
        Array(Array)
        AngleBar(AngleBarList)
    }
    Holder(Holder)
    Op {
        Bin {
            Adj {
                left_operand: Expr*,
                operator: Adj,
                right_operand: Expr*,
            }
            Regular {
                left_operand: Expr*,
                operator: BinaryOperator,
                right_operand: Expr*,
            }
            Member {
                Dot {
                    receiver: Expr*,
                    operator: Token![.],
                    member: Expr*,
                }
                Optional {
                    receiver: Expr*,
                    operator: Token![.?],
                    member: Expr*,
                }
            }
            Index {
                receiver: Expr*,
                operator: Token![#],
                index: Expr*,
            }
        }
        Prefix {
            operator: PrefixOperator,
            operand: Expr*,
        }
        Postfix {
            operand: Expr*,
            operator: PostfixOperator,
        }
    }
    Assign {
        Simple {
            Global {
                variable: Symbol,
                operator: Token![=],
                value: Expr*,
            }
            Local {
                variable: Symbol,
                operator: Token![:=],
                value: Expr*,
            }
        }
        Parallel {
            Global {
                argument_list: AssignmentList*,
                operator: Token![=],
                value: Expr*,
            }
            Local {
                argument_list: AssignmentList*,
                operator: Token![:=],
                value: Expr*,
            }
        }
        Evaluated {
            evaluated_domain: Expr*,
            operator: Token![<-],
            value: Expr*,
        }
        Augmented {
            target: Expr*,
            operator: AugmentedOperator,
            value: Expr*,
        }
        Install {
            Method {
                method: Expr*,
                operator: Adj,
                signature: Expr*,
                assign_op: Token![:=],
                typical_value: TypeAnnotation?,
                function: Expr*,
            }
            Op {
                domain_expr: ExprOp,
                assign_op: Token![:=],
                typical_value: TypeAnnotation?,
                function: Expr*,
            }
            New {
                new_statement: ExprNew,
                operator: Token![:=],
                function: Expr*,
            }
        }
        Op {
            argument_expr: ExprOp,
            operator: Token![=],
            value_expr: Expr*,
        }
    }
    Option {
        key: Expr*,
        operator: Token![=>],
        value: Expr*,
    }
    Lambda {
        Variadic {
            argument: Symbol,
            operator: Token![->],
            body: Expr*,
        }
        List {
            arguments: Delimited<Option<ArgumentListContent>>,
            operator: Token![->],
            body: Expr*,
        }
    }
    If {
        if_token: Token![if],
        condition: Expr*,
        then_clause: ThenClause,
        else_clause: ElseClause?,
    }
    For {
        Bare {
            for_token: Token![for],
            variable: Symbol,
            condition: WhenCondition?,
            body: LoopBody,
        }
        Range {
            for_token: Token![for],
            variable: Symbol,
            lower_bound: RangeBoundFrom?,
            upper_bound: RangeBoundTo?,
            condition: WhenCondition?,
            body: LoopBody,
        }
        In {
            for_token: Token![for],
            variable: Symbol,
            in_token: Token![in],
            collection: Expr*,
            condition: WhenCondition?,
            body: LoopBody,
        }
    }
    While {
        while_token: Token![while],
        condition: Expr*,
        body: LoopBody,
    }
    Try {
        try_token: Token![try],
        value: Expr*,
        on_success: ThenClause?,
        fallback: TryFallback?,
    }
    Quote {
        Symbol {
            specifier: Token![symbol],
            symbol: Symbol,
        }
        Local {
            specifier: Token![local],
            symbol: Symbol,
        }
        Global {
            specifier: Token![global],
            symbol: Symbol,
        }
        ThreadVariable {
            specifier: Token![threadVariable],
            symbol: Symbol,
        }
        ThreadLocal {
            specifier: Token![threadLocal],
            symbol: Symbol,
        }
    }
    New {
        new_token: Token![new],
        class: Expr*,
        parent_class: OfClass?,
        instance: FromInstance?,
    }
    Control {
        Break {
            break_token: Token![break],
            value: Expr*?,
        }
        Continue {
            continue_token: Token![continue],
            value: Expr*?,
        }
        Return {
            return_token: Token![return],
            value: Expr*?,
        }
    }
    Catch {
        catch_token: Token![catch],
        value: Expr*,
    }
    Throw {
        throw_token: Token![throw],
        value: Expr*,
    }
    Trap {
        trap_token: Token![trap],
        value: Expr*,
    }
    Debug {
        Test {
            keyword: Token![TEST],
            value: Expr*?,
        }
        Step {
            keyword: Token![step],
            value: Expr*?,
        }
        Finish {
            keyword: Token![finish],
            value: Expr*?,
        }
        Shield {
            keyword: Token![shield],
            value: Expr*?,
        }
        Breakpoint {
            keyword: Token![breakpoint],
            value: Expr*?,
        }
    }
    Profile {
        ElapsedTime {
            modifier: Token![elapsedTime],
            value: Expr*,
        }
        ElapsedTiming {
            modifier: Token![elapsedTiming],
            value: Expr*,
        }
        Profile {
            modifier: Token![profile],
            value: Expr*,
        }
        Time {
            modifier: Token![time],
            value: Expr*,
        }
        Timing {
            modifier: Token![timing],
            value: Expr*,
        }
    }
    Symbol(Symbol)
}
}