rustyfi-syntax 0.1.4

Lexer, token stream, and syan2-based surface grammar for SATySFi
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
use crate::span::Span;

/// One SATySFi token. Variant names and payloads mirror the token declarations
/// of the v0.0.6 `parser.mly` (`LETNONREC` = `Let`, `LAMBDA` = `Fun`, ...).
/// Command payloads keep their sigil, exactly like the OCaml lexer
/// (`HorzCmd("\\emph")`, `VertCmd("+p")`).
///
/// The variants listed in the `token_leaves!` block at the bottom of this file
/// get a matching leaf struct (`KwLet`, `VarTok`, ...) with a peek → match →
/// push-back-on-mismatch `Parse`/`Unparse`/`Spanned`. Multi-field and
/// multi-variant-matching leaves (`LengthTok`, `LiteralTok`, `BinOpTok`,
/// `VarInHorzTok`, ...) stay hand-written in [`crate::leaf`].
#[derive(Clone, Debug, PartialEq)]
pub enum Token {
    // ---- headers ----
    HeaderRequire(String),
    HeaderImport(String),
    HeaderStage0,
    HeaderStage1,
    HeaderPersistent0,

    // ---- identifiers & constants (program mode) ----
    Var(String),
    VarWithMod(Vec<String>, String),
    Constructor(String),
    TypeVar(String),
    OpenModule(String), // `Mod.(`
    /// `M.N.P` — a dotted module path ending in an UPPER segment
    /// (upstream `LONG_UPPER`, `lexer_v1.mll:357-363`). SATySFi 0.1-only:
    /// under 0.0.6 this spelling is a lex error ("module path must end
    /// with a variable name"), unchanged. Payload mirrors
    /// [`Token::VarWithMod`]: `mods = ["M","N"]`, final segment `"P"` —
    /// upstream's `(modidents, modident0)` pair carries the same split
    /// (`parser_v1.mly:407-413` reassembles the chain from it).
    LongUpper(Vec<String>, String),
    IntConst(i64),
    FloatConst(f64),
    LengthConst(f64, String),
    /// Backtick string literal. `omit_pre`/`omit_post` mirror the OCaml
    /// `LITERAL(_, s, pre, post)` flags (whether an adjacent space is trimmed).
    Literal {
        body: String,
        omit_pre: bool,
        omit_post: bool,
    },

    // ---- keywords ----
    //
    // NOTE: `not` deliberately has NO reserved keyword token here. It lexes
    // as an ordinary `Var`/`VarTok`, exactly like any other primitive name
    // (`arabic`, `floor`, ...), so `not expr` works through the existing
    // `AppExpr` application machinery with no grammar rule of its own. A
    // reserved `Not` token would have none: nothing in the grammar could
    // consume it, which is what made the registered `"not"` primitive
    // (`primitives.rs`) unreachable from real syntax until `picture.satyh`'s
    // `not (x1r >' x2r)`/`not reversed` surfaced it.
    Mod,
    If,
    Then,
    Else,
    Let, // `let` (LETNONREC)
    LetRec, // `let-rec`
    LetAnd, // `and`
    In,
    Fun, // `fun` (LAMBDA)
    True,
    False,
    Before,
    While,
    Do,
    LetMutable, // `let-mutable`
    Match,
    With,
    When,
    As,
    Type,
    Of,
    Module,
    Struct,
    Sig,
    Val,
    End,
    Direct,
    Constraint,
    LetHorz, // `let-inline`
    LetVert, // `let-block`
    LetMath, // `let-math`
    Controls,
    Cycle,
    HorzCmdType, // `inline-cmd`
    VertCmdType, // `block-cmd`
    MathCmdType, // `math-cmd`
    Command,
    Open,
    /// `rec` — SATySFi 0.1-only keyword (`val rec`/`let rec`); under 0.0.6
    /// this word stays a plain identifier (see `lexer.rs`'s version-gated
    /// keyword table).
    Rec,
    /// `inline` — SATySFi 0.1-only keyword (`val inline \cmd = ...`).
    Inline,
    /// `block` — SATySFi 0.1-only keyword (`val block +cmd = ...`).
    Block,
    /// `mutable` — SATySFi 0.1-only keyword (`val mutable x <- e`/`let
    /// mutable x <- e in ..`); under 0.0.6 this word stays a plain
    /// identifier (see `lexer.rs`'s version-gated keyword table).
    Mutable,
    /// `signature` — SATySFi 0.1-only keyword (`signature S = sig … end`,
    /// upstream `lexer_v1.mll:348`); a plain identifier under 0.0.6.
    Signature,
    /// `include` — SATySFi 0.1-only keyword (`include M` binds /
    /// `include S` decls, `lexer_v1.mll:335`); a plain identifier under
    /// 0.0.6.
    Include,
    /// `use` — SATySFi 0.1-only keyword (Envelopes packaging headers `use
    /// package …` / `use … of <path>`, `saphe-split:parser.mly:371-380 @
    /// b836d512`); a plain identifier under 0.0.6 (no 0.0.6 grammar reserves
    /// it — see `lexer.rs`'s version-gated keyword table).
    Use,
    /// `package` — SATySFi 0.1-only keyword (`use package …`); a plain
    /// identifier under 0.0.6.
    Package,
    /// `math` — SATySFi 0.1-only keyword (`val math <ctx> \cmd … = …`,
    /// `parser_v1.mly:452-453` dispatch, MATH reserved at :240); a plain
    /// identifier under 0.0.6 (0.0.6 has no `math` keyword — the word
    /// survives only as the surface name of `BaseType::MathText`).
    Math,
    /// `persistent` — SATySFi 0.1-only keyword, the stage qualifier of a
    /// `val persistent ~x = e` binding (`parser_v1.mly:420-421`, decl form
    /// `:602-603`; reserved at `:241`, lexed at `lexer_v1.mll:345`). A plain
    /// identifier under 0.0.6, which spells the same idea per FILE with a
    /// `@stage:` header rather than per binding.
    Persistent,

    // ---- grouping delimiters ----
    LParen,
    RParen,
    BRecord, // `(|`
    ERecord, // `|)`
    BList, // `[`
    EList, // `]`
    BHorzGrp, // `{` opening inline text
    EHorzGrp, // `}` closing inline text
    BVertGrp, // `'<` / `<` opening block text
    EVertGrp, // `>` closing block text
    BMathGrp, // `${` / `{` opening math
    EMathGrp, // `}` closing math
    BPath, // `<[`
    EPath, // `]>`

    // ---- punctuation & operators (program mode) ----
    ListPunct, // `;`
    Access, // `#`
    Arrow, // `->`
    OverwriteEq, // `<-`
    Bar, // `|`
    Wildcard, // `_`
    Colon,
    Comma,
    Cons, // `::`
    /// `:>` — signature coercion/ascription (COERCE, `lexer_v1.mll:280`).
    /// SATySFi 0.1-only: under 0.0.6 the same two characters lex as
    /// `Colon` + `BinopGt(">")`, unchanged.
    Coerce,
    ExactMinus,
    DefEq, // `=`
    ExactTimes, // `*`
    ExactAmp,   // `&`
    ExactTilde, // `~`
    PathCurve,  // `..`
    PathLine,   // `--`
    BinopPlus(String),
    BinopMinus(String),
    BinopTimes(String),
    BinopDivides(String),
    BinopEq(String),
    BinopLt(String),
    BinopGt(String),
    BinopAmp(String),
    BinopBar(String),
    BinopHat(String),
    UnopExclam(String),
    OptionalType, // `?`
    OptionalArrow, // `?->`
    Optional, // `?:`
    Omission, // `?*`
    /// `?'r` — a SATySFi 0.1 row variable (upstream `ROWVAR`,
    /// `lexer_v1.mll:310-311`). `RowVarTok`
    /// carries the name sans sigil, exactly like [`Token::TypeVar`]/
    /// `TypeVarTok`. V0_1-only: the lexer (`lexer.rs`'s `'?'` arm) only ever
    /// mints this under `RustyfiVersion::V0_1`; under `V0_0`, `?'r` stays
    /// two tokens (`OptionalType` then `TypeVar`), byte-identical to before
    /// this addition.
    RowVar(String), // `?'r`

    // ---- commands (payload includes the `\`/`+` sigil) ----
    HorzCmd(String),
    HorzCmdWithMod(Vec<String>, String),
    HorzMacro(String),
    VertCmd(String),
    VertCmdWithMod(Vec<String>, String),
    VertMacro(String),
    MathCmd(String),
    MathCmdWithMod(Vec<String>, String),

    // ---- text-mode content ----
    VarInHorz(Vec<String>, String),
    VarInVert(Vec<String>, String),
    VarInMath(Vec<String>, String),
    Char(String),
    /// A backtick literal written INSIDE inline text (`` `…` ``). Distinct from
    /// `Char` because upstream keeps it distinct: it reaches the evaluator as
    /// `ImInputHorzEmbeddedCodeText` and is dispatched to the context's
    /// `code_text_command` (`evaluator.cppo.ml:768-779`), which is how a doc
    /// class sets code spans in a monospace face. Do NOT lex it to a plain
    /// `Char` run: that erases the distinction irrecoverably, and the literal
    /// then inherits whatever face surrounds it (italic, inside `\emph`).
    CodeText(String),
    Space,
    Break,
    Item(usize), // `*`+ with depth
    Sep, // `|`
    EndActive, // `;` closing an active area

    // ---- math-mode content ----
    MathChar(String),
    Superscript, // `^`
    Subscript, // `_`
    Primes(usize),

    Eoi,
}

impl std::fmt::Display for Token {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use Token::*;
        match self {
            HeaderRequire(s) => write!(f, "@require: {s}"),
            HeaderImport(s) => write!(f, "@import: {s}"),
            HeaderStage0 => write!(f, "@stage: 0"),
            HeaderStage1 => write!(f, "@stage: 1"),
            HeaderPersistent0 => write!(f, "@stage: persistent"),
            Var(s) | Constructor(s) => write!(f, "{s}"),
            VarWithMod(m, s) | LongUpper(m, s) => write!(f, "{}.{s}", m.join(".")),
            TypeVar(s) => write!(f, "'{s}"),
            OpenModule(s) => write!(f, "{s}.("),
            IntConst(n) => write!(f, "{n}"),
            FloatConst(x) => write!(f, "{x}"),
            LengthConst(x, u) => write!(f, "{x}{u}"),
            Literal { body, .. } => write!(f, "`{body}`"),
            Mod => write!(f, "mod"),
            If => write!(f, "if"),
            Then => write!(f, "then"),
            Else => write!(f, "else"),
            Let => write!(f, "let"),
            LetRec => write!(f, "let-rec"),
            LetAnd => write!(f, "and"),
            In => write!(f, "in"),
            Fun => write!(f, "fun"),
            True => write!(f, "true"),
            False => write!(f, "false"),
            Before => write!(f, "before"),
            While => write!(f, "while"),
            Do => write!(f, "do"),
            LetMutable => write!(f, "let-mutable"),
            Match => write!(f, "match"),
            With => write!(f, "with"),
            When => write!(f, "when"),
            As => write!(f, "as"),
            Type => write!(f, "type"),
            Of => write!(f, "of"),
            Module => write!(f, "module"),
            Struct => write!(f, "struct"),
            Sig => write!(f, "sig"),
            Val => write!(f, "val"),
            End => write!(f, "end"),
            Direct => write!(f, "direct"),
            Constraint => write!(f, "constraint"),
            LetHorz => write!(f, "let-inline"),
            LetVert => write!(f, "let-block"),
            LetMath => write!(f, "let-math"),
            Controls => write!(f, "controls"),
            Cycle => write!(f, "cycle"),
            HorzCmdType => write!(f, "inline-cmd"),
            VertCmdType => write!(f, "block-cmd"),
            MathCmdType => write!(f, "math-cmd"),
            Command => write!(f, "command"),
            Open => write!(f, "open"),
            Rec => write!(f, "rec"),
            Inline => write!(f, "inline"),
            Block => write!(f, "block"),
            Mutable => write!(f, "mutable"),
            Signature => write!(f, "signature"),
            Include => write!(f, "include"),
            Use => write!(f, "use"),
            Package => write!(f, "package"),
            Math => write!(f, "math"),
            Persistent => write!(f, "persistent"),
            LParen => write!(f, "("),
            RParen => write!(f, ")"),
            BRecord => write!(f, "(|"),
            ERecord => write!(f, "|)"),
            BList => write!(f, "["),
            EList => write!(f, "]"),
            BHorzGrp => write!(f, "{{"),
            EHorzGrp => write!(f, "}}"),
            BVertGrp => write!(f, "'<"),
            EVertGrp => write!(f, ">"),
            BMathGrp => write!(f, "${{"),
            EMathGrp => write!(f, "}}"),
            BPath => write!(f, "<["),
            EPath => write!(f, "]>"),
            ListPunct => write!(f, ";"),
            Access => write!(f, "#"),
            Arrow => write!(f, "->"),
            OverwriteEq => write!(f, "<-"),
            Bar => write!(f, "|"),
            Wildcard => write!(f, "_"),
            Colon => write!(f, ":"),
            Comma => write!(f, ","),
            Cons => write!(f, "::"),
            Coerce => write!(f, ":>"),
            ExactMinus => write!(f, "-"),
            DefEq => write!(f, "="),
            ExactTimes => write!(f, "*"),
            ExactAmp => write!(f, "&"),
            ExactTilde => write!(f, "~"),
            PathCurve => write!(f, ".."),
            PathLine => write!(f, "--"),
            BinopPlus(s) | BinopMinus(s) | BinopTimes(s) | BinopDivides(s) | BinopEq(s)
            | BinopLt(s) | BinopGt(s) | BinopAmp(s) | BinopBar(s) | BinopHat(s)
            | UnopExclam(s) => write!(f, "{s}"),
            OptionalType => write!(f, "?"),
            OptionalArrow => write!(f, "?->"),
            Optional => write!(f, "?:"),
            Omission => write!(f, "?*"),
            RowVar(s) => write!(f, "?'{s}"),
            HorzCmd(s) | VertCmd(s) | MathCmd(s) | HorzMacro(s) | VertMacro(s) => {
                write!(f, "{s}")
            }
            HorzCmdWithMod(m, s) | VertCmdWithMod(m, s) | MathCmdWithMod(m, s) => {
                let (sigil, name) = s.split_at(1);
                write!(f, "{sigil}{}.{name}", m.join("."))
            }
            VarInHorz(m, s) | VarInVert(m, s) | VarInMath(m, s) => {
                if m.is_empty() {
                    write!(f, "#{s}")
                } else {
                    write!(f, "#{}.{s}", m.join("."))
                }
            }
            Char(s) => write!(f, "{s}"),
            CodeText(s) => write!(f, "`{s}`"),
            Space => write!(f, " "),
            Break => writeln!(f),
            Item(n) => write!(f, "{}", "*".repeat(*n)),
            Sep => write!(f, "|"),
            EndActive => write!(f, ";"),
            MathChar(s) => write!(f, "{s}"),
            Superscript => write!(f, "^"),
            Subscript => write!(f, "_"),
            Primes(n) => write!(f, "{}", "'".repeat(*n)),
            Eoi => Ok(()),
        }
    }
}

/// A token together with its source span: the atom type fed to the syan parser.
pub type Atom = syan::span::WithSpan<Token, Span>;

// Leaf structs + `Parse`/`Unparse`/`Spanned`, one per listed variant. Stands
// in for syan's `#[derive(TokenLeaves)]`, which exists only on that crate's
// api-ergonomics line and is absent from main; see `leaf_macro.rs`.
token_leaves! {
    atom = Atom, span = Span, read_span = |a| a.span;
    (HeaderRequire(String) => HeaderRequireTok, "'@require:'", field = content);
    (HeaderImport(String) => HeaderImportTok, "'@import:'", field = content);
    (Var(String) => VarTok, "a variable name", field = name);
    (Constructor(String) => CtorTok, "a constructor name", field = name);
    (TypeVar(String) => TypeVarTok, "a type variable", field = name);
    (OpenModule(String) => OpenModuleTok, "'Mod.('", field = name);
    (IntConst(i64) => IntTok, "an integer constant", field = value);
    (FloatConst(f64) => FloatTok, "a float constant", field = value);
    (If => KwIf, "'if'");
    (Then => KwThen, "'then'");
    (Else => KwElse, "'else'");
    (Let => KwLet, "'let'");
    (LetRec => KwLetRec, "'let-rec'");
    (LetAnd => KwAnd, "'and'");
    (In => KwIn, "'in'");
    (Fun => KwFun, "'fun'");
    (True => KwTrue, "'true'");
    (False => KwFalse, "'false'");
    (Before => KwBefore, "'before'");
    (While => KwWhile, "'while'");
    (Do => KwDo, "'do'");
    (LetMutable => KwLetMutable, "'let-mutable'");
    (Match => KwMatch, "'match'");
    (With => KwWith, "'with'");
    (When => KwWhen, "'when'");
    (As => KwAs, "'as'");
    (Type => KwType, "'type'");
    (Of => KwOf, "'of'");
    (Module => KwModule, "'module'");
    (Struct => KwStruct, "'struct'");
    (Sig => KwSig, "'sig'");
    (Val => KwVal, "'val'");
    (End => KwEnd, "'end'");
    (Direct => KwDirect, "'direct'");
    (Constraint => ConstraintTok, "'constraint'");
    (LetHorz => KwLetHorz, "'let-inline'");
    (LetVert => KwLetVert, "'let-block'");
    (LetMath => KwLetMath, "'let-math'");
    (HorzCmdType => HorzCmdTypeTok, "'inline-cmd'");
    (VertCmdType => VertCmdTypeTok, "'block-cmd'");
    (MathCmdType => MathCmdTypeTok, "'math-cmd'");
    (Command => CommandTok, "'command'");
    (Open => KwOpen, "'open'");
    (Rec => KwRec, "'rec'");
    (Inline => KwInline, "'inline'");
    (Block => KwBlock, "'block'");
    (Mutable => KwMutable, "'mutable'");
    (Signature => KwSignature, "'signature'");
    (Include => KwInclude, "'include'");
    (Use => KwUse, "'use'");
    (Package => KwPackage, "'package'");
    (Math => KwMath, "'math'");
    (Persistent => KwPersistent, "'persistent'");
    (LParen => LParenTok, "'('");
    (RParen => RParenTok, "')'");
    (BRecord => BRecordTok, "'(|'");
    (ERecord => ERecordTok, "'|)'");
    (BList => BListTok, "'['");
    (EList => EListTok, "']'");
    (BHorzGrp => BHorzGrpTok, "'{'");
    (EHorzGrp => EHorzGrpTok, "'}'");
    (BVertGrp => BVertGrpTok, "'<'");
    (EVertGrp => EVertGrpTok, "'>'");
    (BMathGrp => BMathGrpTok, "'${'");
    (EMathGrp => EMathGrpTok, "'}'");
    (ListPunct => ListPunctTok, "';'");
    (Access => AccessTok, "'#'");
    (Arrow => ArrowTok, "'->'");
    (OverwriteEq => OverwriteEqTok, "'<-'");
    (Bar => BarTok, "'|'");
    (Wildcard => WildcardTok, "'_'");
    (Colon => ColonTok, "':'");
    (Comma => CommaTok, "','");
    (Cons => ConsTok, "'::'");
    (Coerce => CoerceTok, "':>'");
    (ExactMinus => ExactMinusTok, "'-'");
    (DefEq => DefEqTok, "'='");
    (ExactTimes => ExactTimesTok, "'*'");
    (ExactAmp => ExactAmpTok, "'&'");
    (ExactTilde => ExactTildeTok, "'~'");
    (UnopExclam(String) => UnopExclamTok, "a '!' operator", field = text);
    (OptionalType => OptionalTypeTok, "'?'");
    (OptionalArrow => OptionalArrowTok, "'?->'");
    (Optional => OptionalTok, "'?:'");
    (Omission => OmissionTok, "'?*'");
    (RowVar(String) => RowVarTok, "a row variable (\"?'r\")", field = name);
    (HorzCmd(String) => HorzCmdTok, "an inline command", field = name);
    (VertCmd(String) => VertCmdTok, "a block command", field = name);
    (MathCmd(String) => MathCmdTok, "a math command", field = name);
    (Char(String) => CharTok, "inline text", field = text);
    (CodeText(String) => CodeTextTok, "an inline code literal", field = text);
    (Space => SpaceTok, "a space");
    (Break => BreakTok, "a line break");
    (Item(usize) => ItemTok, "an itemize bullet", field = depth);
    (Sep => SepTok, "'|' separator");
    (EndActive => EndActiveTok, "';'");
    (MathChar(String) => MathCharTok, "a math character", field = text);
    (Superscript => SuperscriptTok, "'^'");
    (Subscript => SubscriptTok, "'_'");
    (Primes(usize) => PrimesTok, "a primes mark", field = count);
    (Eoi => EoiTok, "end of input");
}