parsuna 0.1.0

Parsuna: recoverable, pull-based parsers with precise errors
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
use std::fmt::Write;
use std::path::PathBuf;

use crate::codegen::EmittedFile;
use crate::lowering::{DispatchLeaf, DispatchTree, Op, StateTable};

pub fn emit(st: &StateTable) -> Vec<EmittedFile> {
    let ns = if st.grammar_name.is_empty() {
        "Parser".to_string()
    } else {
        pascal_case(&st.grammar_name)
    };
    let mut s = String::new();

    writeln!(&mut s, "// Generated by parsuna — do not edit by hand.").unwrap();
    writeln!(&mut s, "//").unwrap();
    writeln!(
        &mut s,
        "// Pull-based, recoverable parser. Call one of the static Grammar.ParseXxx"
    )
    .unwrap();
    writeln!(
        &mut s,
        "// entry points and iterate the returned Parser (or call NextEvent) to"
    )
    .unwrap();
    writeln!(
        &mut s,
        "// walk the parse as a flat Event stream. Requires the Parsuna.Runtime library."
    )
    .unwrap();
    writeln!(&mut s, "#nullable enable").unwrap();
    writeln!(&mut s, "using System;").unwrap();
    writeln!(&mut s, "using System.IO;").unwrap();
    writeln!(&mut s, "using System.Text;").unwrap();
    writeln!(&mut s, "using Parsuna.Runtime;").unwrap();
    writeln!(&mut s).unwrap();
    writeln!(&mut s, "namespace {};", ns).unwrap();
    writeln!(&mut s).unwrap();

    emit_constants(&mut s, st);
    emit_dfa(&mut s, st);
    emit_tables(&mut s, st);
    emit_grammar(&mut s, st);

    let file = PathBuf::from(format!("{}/Grammar.cs", ns));
    vec![EmittedFile {
        path: file,
        contents: s,
    }]
}

fn pascal_case(s: &str) -> String {
    let mut out = String::new();
    let mut upper = true;
    for c in s.chars() {
        if c == '_' || c == '-' {
            upper = true;
            continue;
        }
        if upper {
            out.extend(c.to_uppercase());
            upper = false;
        } else {
            out.push(c);
        }
    }
    out
}

fn emit_constants(s: &mut String, st: &StateTable) {
    writeln!(
        s,
        "/// <summary>Token kinds this grammar can emit. <c>Eof</c>/<c>Error</c> are runtime"
    )
    .unwrap();
    writeln!(
        s,
        "/// sentinels; the rest come from the grammar's <c>token</c> declarations.</summary>"
    )
    .unwrap();
    writeln!(s, "public enum TokenKind : short {{").unwrap();
    writeln!(s, "    Eof = 0,").unwrap();
    writeln!(s, "    Error = -1,").unwrap();
    for t in &st.tokens {
        writeln!(
            s,
            "    {} = {},",
            pascal_case(&t.name.to_lowercase()),
            t.kind
        )
        .unwrap();
    }
    writeln!(s, "}}").unwrap();
    writeln!(s).unwrap();
    writeln!(s, "public static class TokenKindEx {{").unwrap();
    writeln!(
        s,
        "    public static string DisplayName(this TokenKind k) => k switch {{"
    )
    .unwrap();
    writeln!(s, "        TokenKind.Eof => \"EOF\",").unwrap();
    writeln!(s, "        TokenKind.Error => \"ERROR\",").unwrap();
    for t in &st.tokens {
        writeln!(
            s,
            "        TokenKind.{} => \"{}\",",
            pascal_case(&t.name.to_lowercase()),
            t.name
        )
        .unwrap();
    }
    writeln!(s, "        _ => \"?\",").unwrap();
    writeln!(s, "    }};").unwrap();
    writeln!(s, "}}").unwrap();
    writeln!(s).unwrap();

    writeln!(
        s,
        "/// <summary>One variant per non-fragment grammar rule. Attached to enter/exit"
    )
    .unwrap();
    writeln!(
        s,
        "/// events so consumers can identify the subtree each pair delimits.</summary>"
    )
    .unwrap();
    writeln!(s, "public enum RuleKind : ushort {{").unwrap();
    for (i, n) in st.rule_kinds.iter().enumerate() {
        writeln!(s, "    {} = {},", pascal_case(n), i).unwrap();
    }
    writeln!(s, "}}").unwrap();
    writeln!(s).unwrap();
    writeln!(s, "public static class RuleKindEx {{").unwrap();
    writeln!(
        s,
        "    public static string DisplayName(this RuleKind k) => k switch {{"
    )
    .unwrap();
    for n in &st.rule_kinds {
        writeln!(s, "        RuleKind.{} => \"{}\",", pascal_case(n), n).unwrap();
    }
    writeln!(s, "        _ => \"?\",").unwrap();
    writeln!(s, "    }};").unwrap();
    writeln!(s, "}}").unwrap();
    writeln!(s).unwrap();
}

fn token_short(st: &StateTable, kind: i16) -> String {
    if kind == 0 {
        return "(short)TokenKind.Eof".to_string();
    }
    if kind == -1 {
        return "(short)TokenKind.Error".to_string();
    }
    match st.tokens.iter().find(|t| t.kind == kind) {
        Some(t) => format!(
            "(short)TokenKind.{}",
            pascal_case(&t.name.to_lowercase())
        ),
        None => panic!("unknown token id {} while emitting C# backend", kind),
    }
}

fn rule_id(st: &StateTable, kind: u16) -> String {
    let name = st
        .rule_kinds
        .get(kind as usize)
        .unwrap_or_else(|| panic!("unknown rule kind id {} while emitting C# backend", kind));
    format!("(int)RuleKind.{}", pascal_case(name))
}

fn emit_dfa(s: &mut String, st: &StateTable) {
    let dfa = &st.lexer_dfa;
    writeln!(s, "internal static class DfaTables {{").unwrap();
    writeln!(s, "    public static readonly uint[] Trans = new uint[] {{").unwrap();
    for state in &dfa.states {
        write!(s, "        ").unwrap();
        for (j, t) in state.trans.iter().enumerate() {
            if j == 255 {
                write!(s, "{}u,", t).unwrap();
            } else {
                write!(s, "{}u, ", t).unwrap();
            }
        }
        writeln!(s).unwrap();
    }
    writeln!(s, "    }};").unwrap();
    writeln!(
        s,
        "    public static readonly ushort[] Accept = new ushort[] {{"
    )
    .unwrap();
    write!(s, "        ").unwrap();
    for (i, state) in dfa.states.iter().enumerate() {
        let v = state.accept.unwrap_or(0);
        if i == dfa.states.len() - 1 {
            write!(s, "{},", v).unwrap();
        } else {
            write!(s, "{}, ", v).unwrap();
        }
    }
    writeln!(s).unwrap();
    writeln!(s, "    }};").unwrap();
    writeln!(
        s,
        "    public static readonly DfaConfig Lexer = new DfaConfig({}u, Trans, Accept);",
        dfa.start
    )
    .unwrap();
    writeln!(s, "}}").unwrap();
    writeln!(s).unwrap();
}

fn emit_tables(s: &mut String, st: &StateTable) {
    writeln!(s, "internal static class Tables {{").unwrap();
    writeln!(s, "    public const int K = {};", st.k).unwrap();

    for (i, f) in st.first_sets.iter().enumerate() {
        let seqs: Vec<String> = f
            .iter()
            .map(|seq| {
                format!(
                    "new short[] {{ {} }}",
                    seq.iter()
                        .map(|t| token_short(st, *t))
                        .collect::<Vec<_>>()
                        .join(", ")
                )
            })
            .collect();
        writeln!(
            s,
            "    public static readonly short[][] First{} = new short[][] {{ {} }};",
            i,
            seqs.join(", ")
        )
        .unwrap();
    }
    for (i, f) in st.sync_sets.iter().enumerate() {
        writeln!(
            s,
            "    public static readonly short[] Sync{} = new short[] {{ {} }};",
            i,
            f.iter()
                .map(|t| token_short(st, *t))
                .collect::<Vec<_>>()
                .join(", ")
        )
        .unwrap();
    }

    let skips: Vec<String> = st
        .tokens
        .iter()
        .filter(|t| t.skip)
        .map(|t| format!("k == {}", token_short(st, t.kind)))
        .collect();
    let skip_body = if skips.is_empty() {
        "false".to_string()
    } else {
        skips.join(" || ")
    };
    writeln!(
        s,
        "    public static bool IsSkip(short k) => {};",
        skip_body
    )
    .unwrap();
    writeln!(s, "}}").unwrap();
    writeln!(s).unwrap();
}

fn emit_grammar(s: &mut String, st: &StateTable) {
    writeln!(s, "/// <summary>Grammar-specific entry points. Thin wrapper around Parsuna.Runtime.Parser.</summary>").unwrap();
    writeln!(s, "public static class Grammar {{").unwrap();

    for (name, id) in &st.entry_states {
        writeln!(
            s,
            "    private const int Entry{} = {};",
            pascal_case(name),
            id
        )
        .unwrap();
    }
    writeln!(s).unwrap();
    writeln!(
        s,
        "    private static readonly ParserConfig Config = new ParserConfig("
    )
    .unwrap();
    writeln!(s, "        Tables.K,").unwrap();
    writeln!(s, "        (short)TokenKind.Eof,").unwrap();
    writeln!(s, "        Tables.IsSkip,").unwrap();
    writeln!(s, "        Drive);").unwrap();
    writeln!(s).unwrap();

    writeln!(s, "    private static Parser FromStream(Stream stream, int entry) =>").unwrap();
    writeln!(
        s,
        "        new Parser(new Lexer(stream, DfaTables.Lexer, (short)TokenKind.Eof, (short)TokenKind.Error), entry, Config);"
    )
    .unwrap();
    writeln!(s).unwrap();

    for (name, _) in &st.entry_states {
        writeln!(
            s,
            "    /// <summary>Parse the <c>{name}</c> rule from a byte <c>Stream</c>.</summary>",
        )
        .unwrap();
        writeln!(
            s,
            "    public static Parser Parse{cap}(Stream stream) => FromStream(stream, Entry{cap});",
            cap = pascal_case(name)
        )
        .unwrap();
        writeln!(
            s,
            "    /// <summary>Parse the <c>{name}</c> rule from a UTF-8 string.</summary>",
        )
        .unwrap();
        writeln!(
            s,
            "    public static Parser Parse{cap}(string src) => Parse{cap}(new MemoryStream(Encoding.UTF8.GetBytes(src)));",
            cap = pascal_case(name)
        ).unwrap();
        writeln!(s).unwrap();
    }

    emit_drive(s, st);

    writeln!(s, "}}").unwrap();
}

fn emit_drive(s: &mut String, st: &StateTable) {
    writeln!(s, "    private static void Drive(Parser p) {{").unwrap();
    writeln!(s, "        int cur = p.State();").unwrap();
    writeln!(
        s,
        "        while (p.QueueIsEmpty() && cur != Parser.Terminated) {{"
    )
    .unwrap();
    writeln!(s, "            switch (cur) {{").unwrap();
    for state in st.states.values() {
        writeln!(
            s,
            "                case {}: {{ // {}",
            state.id, state.label
        )
        .unwrap();
        for op in &state.ops {
            emit_op(s, st, op, state.id);
        }
        writeln!(s, "                    break;").unwrap();
        writeln!(s, "                }}").unwrap();
    }
    writeln!(
        s,
        "                default: throw new InvalidOperationException($\"unknown state {{cur}}\");"
    )
    .unwrap();
    writeln!(s, "            }}").unwrap();
    writeln!(s, "        }}").unwrap();
    writeln!(s, "        p.SetState(cur);").unwrap();
    writeln!(s, "    }}").unwrap();
    writeln!(s).unwrap();
}

fn emit_op(s: &mut String, st: &StateTable, op: &Op, self_id: u32) {
    match op {
        Op::Enter(k) => {
            writeln!(s, "                    p.Enter({});", rule_id(st, *k)).unwrap();
        }
        Op::Exit(k) => {
            writeln!(s, "                    p.Exit({});", rule_id(st, *k)).unwrap();
        }
        Op::Expect {
            kind,
            token_name,
            sync,
        } => {
            writeln!(
                s,
                "                    p.TryConsume({}, Tables.Sync{}, \"{}\");",
                token_short(st, *kind),
                sync,
                token_name
            )
            .unwrap();
        }
        Op::PushRet(r) => {
            writeln!(s, "                    p.PushRet({});", r).unwrap();
        }
        Op::Jump(n) => {
            writeln!(s, "                    cur = {};", n).unwrap();
        }
        Op::Ret => {
            writeln!(s, "                    cur = p.PopRet();").unwrap();
        }
        Op::Star { first, body, next } => {
            writeln!(s, "                    if (p.MatchesFirst(Tables.First{})) {{ p.PushRet({}); cur = {}; }}", first, self_id, body).unwrap();
            writeln!(s, "                    else cur = {};", next).unwrap();
        }
        Op::Opt { first, body, next } => {
            writeln!(s, "                    if (p.MatchesFirst(Tables.First{})) {{ p.PushRet({}); cur = {}; }}", first, next, body).unwrap();
            writeln!(s, "                    else cur = {};", next).unwrap();
        }
        Op::Dispatch { tree, sync, next } => {
            emit_dispatch_tree(s, st, tree, *sync, *next, "                    ");
        }
    }
}

fn emit_dispatch_tree(
    s: &mut String,
    st: &StateTable,
    tree: &DispatchTree,
    sync: u32,
    next: u32,
    ind: &str,
) {
    match tree {
        DispatchTree::Leaf(leaf) => {
            write!(s, "{}{{ ", ind).unwrap();
            emit_leaf_inline(s, leaf, sync, next);
            writeln!(s, "}}").unwrap();
        }
        DispatchTree::Switch {
            depth,
            arms,
            default,
        } => {
            writeln!(s, "{}switch (p.Look({}).Kind) {{", ind, depth).unwrap();
            let inner = format!("{}  ", ind);
            for (kind, sub) in arms {
                let lit = format!("(short){}", *kind);
                match sub {
                    DispatchTree::Leaf(leaf) => {
                        write!(s, "{}case {}: {{ ", inner, lit).unwrap();
                        emit_leaf_inline(s, leaf, sync, next);
                        writeln!(s, "break; }}").unwrap();
                    }
                    _ => {
                        writeln!(s, "{}case {}: {{", inner, lit).unwrap();
                        emit_dispatch_tree(s, st, sub, sync, next, &format!("{}  ", inner));
                        writeln!(s, "{}  break;", inner).unwrap();
                        writeln!(s, "{}}}", inner).unwrap();
                    }
                }
            }
            write!(s, "{}default: {{ ", inner).unwrap();
            emit_leaf_inline(s, default, sync, next);
            writeln!(s, "break; }}").unwrap();
            writeln!(s, "{}}}", ind).unwrap();
        }
    }
}

fn emit_leaf_inline(s: &mut String, leaf: &DispatchLeaf, sync: u32, next: u32) {
    match leaf {
        DispatchLeaf::Arm(t) => write!(s, "p.PushRet({}); cur = {}; ", next, t).unwrap(),
        DispatchLeaf::Fallthrough => write!(s, "cur = {}; ", next).unwrap(),
        DispatchLeaf::Error => write!(
            s,
            "cur = {}; p.ErrorHere(\"unexpected token\"); p.RecoverTo(Tables.Sync{}); ",
            next, sync
        )
        .unwrap(),
    }
}