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
//! FIRST(k)/FOLLOW computation and the helpers the grammar analysis builds
//! on (length-bounded concatenation, bounded star closure, etc.).

use std::collections::{BTreeMap, BTreeSet};

use super::EOF_MARKER;
use crate::grammar::ir::*;

/// A sequence of token names, bounded by the analysis' `k`. Tokens are
/// referred to by name here rather than numeric id because analysis runs
/// before lowering assigns ids.
pub type Seq = Vec<String>;
/// FIRST(k): the set of token sequences of length ≤ `k` that can start a
/// rule or expression. An empty sequence means "ε" (the expression is
/// nullable for this prefix).
pub type FirstSet = BTreeSet<Seq>;
/// Classic FOLLOW set: single tokens that may legally follow a rule.
/// `EOF_MARKER` stands in for end-of-input.
pub type FollowSet = BTreeSet<String>;

/// Length-bounded concatenation: `{ truncate(a ++ b, k) | a ∈ A, b ∈ B }`.
///
/// Any sequence in `A` already of length `k` is passed through unchanged —
/// `b` can add nothing to it since we only keep the first `k` tokens.
pub fn concat_k(a: &FirstSet, b: &FirstSet, k: usize) -> FirstSet {
    let mut out = FirstSet::new();
    for x in a {
        if x.len() >= k {
            out.insert(x.clone());
            continue;
        }
        for y in b {
            let mut s = x.clone();
            for t in y {
                if s.len() >= k {
                    break;
                }
                s.push(t.clone());
            }
            out.insert(s);
        }
    }
    out
}

/// Length-bounded Kleene star over FIRST sets. Seeded with ε and saturated
/// by repeatedly concatenating `inner`; the fixed point is finite because
/// sequences are capped at `k` tokens.
pub fn star_k(inner: &FirstSet, k: usize) -> FirstSet {
    let mut out: FirstSet = FirstSet::new();
    out.insert(Vec::new());
    loop {
        let added = concat_k(&out, inner, k);
        let prev = out.len();
        for s in added {
            out.insert(s);
        }
        if out.len() == prev {
            break;
        }
    }
    out
}

/// Compute FIRST(k) and nullability for every rule by fixed-point iteration.
///
/// Classic worklist-free saturation: on each pass we recompute every rule's
/// FIRST from the current approximation and stop once nothing changed. The
/// iteration always terminates because FIRST(k) is a finite lattice.
pub fn compute_first(
    g: &Grammar,
    k: usize,
) -> (BTreeMap<String, bool>, BTreeMap<String, FirstSet>) {
    let mut nullable: BTreeMap<String, bool> =
        g.rules.iter().map(|r| (r.name.clone(), false)).collect();
    let mut first: BTreeMap<String, FirstSet> = g
        .rules
        .iter()
        .map(|r| (r.name.clone(), FirstSet::new()))
        .collect();

    loop {
        let mut changed = false;
        for r in &g.rules {
            let e_first = first_of(&r.body, &nullable, &first, k);
            let mut cur = first.get(&r.name).cloned().unwrap_or_default();
            let cur_null = *nullable.get(&r.name).unwrap_or(&false);
            let mut new_null = cur_null || e_first.iter().any(|s| s.is_empty());
            for seq in &e_first {
                if cur.insert(seq.clone()) {
                    changed = true;
                }
            }
            if new_null != cur_null {
                changed = true;
                new_null = true;
                nullable.insert(r.name.clone(), new_null);
            }
            first.insert(r.name.clone(), cur);
        }
        if !changed {
            break;
        }
    }

    (nullable, first)
}

/// FIRST(k) of a single expression, given the rule-level tables already
/// computed by [`compute_first`].
pub fn first_of(
    e: &Expr,
    nullable: &BTreeMap<String, bool>,
    rule_first: &BTreeMap<String, FirstSet>,
    k: usize,
) -> FirstSet {
    let mut out = FirstSet::new();
    match e {
        Expr::Empty => {
            out.insert(Vec::new());
        }
        Expr::Token(n) => {
            if k == 0 {
                out.insert(Vec::new());
            } else {
                out.insert(vec![n.clone()]);
            }
        }
        Expr::Rule(n) => {
            if let Some(s) = rule_first.get(n) {
                out.extend(s.iter().cloned());
            }
            if *nullable.get(n).unwrap_or(&false) {
                out.insert(Vec::new());
            }
        }
        Expr::Seq(xs) => {
            // Prefix-accumulate FIRST across the sequence. Short-circuit
            // once every accumulated sequence is already `k` long —
            // subsequent elements cannot alter the result.
            let mut acc: FirstSet = FirstSet::new();
            acc.insert(Vec::new());
            for x in xs {
                let fx = first_of(x, nullable, rule_first, k);
                acc = concat_k(&acc, &fx, k);

                if acc.iter().all(|s| s.len() >= k) {
                    break;
                }
            }
            out = acc;
        }
        Expr::Alt(xs) => {
            for x in xs {
                out.extend(first_of(x, nullable, rule_first, k).into_iter());
            }
        }
        Expr::Opt(x) => {
            out.extend(first_of(x, nullable, rule_first, k).into_iter());
            out.insert(Vec::new());
        }
        Expr::Star(x) => {
            let inner = first_of(x, nullable, rule_first, k);
            out = star_k(&inner, k);
        }
        Expr::Plus(x) => {
            let inner = first_of(x, nullable, rule_first, k);
            let star = star_k(&inner, k);
            out = concat_k(&inner, &star, k);
        }
    }
    out
}

/// Split a FIRST set into its non-empty sequences and a boolean indicating
/// whether ε was a member. Handy when a caller wants to treat nullability
/// as a separate bit from the prediction set.
pub fn split_nullable(s: &FirstSet) -> (FirstSet, bool) {
    let mut out = FirstSet::new();
    let mut null = false;
    for seq in s {
        if seq.is_empty() {
            null = true;
        } else {
            out.insert(seq.clone());
        }
    }
    (out, null)
}

/// Compute FOLLOW (single-token) for every rule. The result is seeded with
/// `EOF_MARKER` for every rule (any rule could be the outermost one we are
/// about to emit) and then saturated by walking each rule's body.
pub fn compute_follow(
    g: &Grammar,
    first: &BTreeMap<String, FirstSet>,
    nullable: &BTreeMap<String, bool>,
) -> BTreeMap<String, FollowSet> {
    let mut follow: BTreeMap<String, FollowSet> = g
        .rules
        .iter()
        .map(|r| {
            let mut s = BTreeSet::new();
            s.insert(EOF_MARKER.to_string());
            (r.name.clone(), s)
        })
        .collect();

    loop {
        let mut changed = false;
        for r in &g.rules {
            walk_follow(&r.body, &r.name, nullable, first, &mut follow, &mut changed);
        }
        if !changed {
            break;
        }
    }
    follow
}

fn walk_follow(
    e: &Expr,
    host: &str,
    nullable: &BTreeMap<String, bool>,
    first: &BTreeMap<String, FirstSet>,
    follow: &mut BTreeMap<String, FollowSet>,
    changed: &mut bool,
) {
    match e {
        Expr::Empty | Expr::Token(_) => {}
        Expr::Rule(name) => {
            if let Some(hf) = follow.get(host).cloned() {
                let target = follow.entry(name.clone()).or_default();
                for t in &hf {
                    if target.insert(t.clone()) {
                        *changed = true;
                    }
                }
            }
        }
        Expr::Seq(xs) => {
            for (i, x) in xs.iter().enumerate() {
                if let Expr::Rule(name) = x {
                    let tail = &xs[i + 1..];
                    let tf = first_of_seq_1(tail, nullable, first);
                    let tail_nullable = tf.contains("");
                    {
                        let target = follow.entry(name.clone()).or_default();
                        for t in tf.iter().filter(|t| !t.is_empty()) {
                            if target.insert(t.clone()) {
                                *changed = true;
                            }
                        }
                    }
                    if tail_nullable {
                        let host_follow = follow.get(host).cloned().unwrap_or_default();
                        let target = follow.entry(name.clone()).or_default();
                        for t in &host_follow {
                            if target.insert(t.clone()) {
                                *changed = true;
                            }
                        }
                    }
                }
                walk_follow(x, host, nullable, first, follow, changed);
            }
        }
        Expr::Alt(xs) => {
            for x in xs {
                walk_follow(x, host, nullable, first, follow, changed);
            }
        }
        Expr::Opt(x) => walk_follow(x, host, nullable, first, follow, changed),
        Expr::Star(x) | Expr::Plus(x) => {
            walk_follow(x, host, nullable, first, follow, changed);
            let fx = first_of(x, nullable, first, 1);
            let trailing = collect_trailing_rules(x, nullable);
            let host_follow = follow.get(host).cloned().unwrap_or_default();
            for r in trailing {
                let target = follow.entry(r).or_default();
                for seq in fx.iter().filter(|s| !s.is_empty()) {
                    if target.insert(seq[0].clone()) {
                        *changed = true;
                    }
                }
                for t in &host_follow {
                    if target.insert(t.clone()) {
                        *changed = true;
                    }
                }
            }
        }
    }
}

fn first_of_seq_1(
    xs: &[Expr],
    nullable: &BTreeMap<String, bool>,
    first: &BTreeMap<String, FirstSet>,
) -> BTreeSet<String> {
    let mut out = BTreeSet::new();
    let mut all_nullable = true;
    for x in xs {
        let f = first_of(x, nullable, first, 1);
        let has_eps = f.iter().any(|s| s.is_empty());
        for seq in &f {
            if !seq.is_empty() {
                out.insert(seq[0].clone());
            }
        }
        if !has_eps {
            all_nullable = false;
            break;
        }
    }
    if all_nullable {
        out.insert(String::new());
    }
    out
}

fn collect_trailing_rules(e: &Expr, nullable: &BTreeMap<String, bool>) -> Vec<String> {
    let mut out = Vec::new();
    match e {
        Expr::Empty | Expr::Token(_) => {}
        Expr::Rule(n) => out.push(n.clone()),
        Expr::Seq(xs) => {
            for x in xs.iter().rev() {
                let mut sub = collect_trailing_rules(x, nullable);
                let is_null = expr_nullable(x, nullable);
                out.append(&mut sub);
                if !is_null {
                    break;
                }
            }
        }
        Expr::Alt(xs) => {
            for x in xs {
                out.append(&mut collect_trailing_rules(x, nullable));
            }
        }
        Expr::Opt(x) | Expr::Star(x) | Expr::Plus(x) => {
            out.append(&mut collect_trailing_rules(x, nullable))
        }
    }
    out
}

/// FOLLOW(k): like [`compute_follow`] but producing sequences of up to `k`
/// tokens, suitable for the LL(k) dispatch trees.
pub fn compute_follow_k(
    g: &Grammar,
    first: &BTreeMap<String, FirstSet>,
    nullable: &BTreeMap<String, bool>,
    k: usize,
) -> BTreeMap<String, FirstSet> {
    let mut follow: BTreeMap<String, FirstSet> = g
        .rules
        .iter()
        .map(|r| {
            let mut s = FirstSet::new();
            s.insert(vec![EOF_MARKER.to_string()]);
            (r.name.clone(), s)
        })
        .collect();

    loop {
        let mut changed = false;
        for r in &g.rules {
            let host_follow = follow.get(&r.name).cloned().unwrap_or_default();
            walk_follow_k(
                &r.body,
                &host_follow,
                nullable,
                first,
                &mut follow,
                &mut changed,
                k,
            );
        }
        if !changed {
            break;
        }
    }
    follow
}

fn walk_follow_k(
    e: &Expr,
    tail: &FirstSet,
    nullable: &BTreeMap<String, bool>,
    first: &BTreeMap<String, FirstSet>,
    follow: &mut BTreeMap<String, FirstSet>,
    changed: &mut bool,
    k: usize,
) {
    match e {
        Expr::Empty | Expr::Token(_) => {}
        Expr::Rule(name) => {
            let target = follow.entry(name.clone()).or_default();
            for seq in tail {
                if target.insert(seq.clone()) {
                    *changed = true;
                }
            }
        }
        Expr::Seq(xs) => {
            let n = xs.len();
            let mut succ: Vec<FirstSet> = vec![tail.clone(); n + 1];
            for i in (0..n).rev() {
                let fx = first_of(&xs[i], nullable, first, k);
                succ[i] = concat_k(&fx, &succ[i + 1], k);
            }
            for i in 0..n {
                walk_follow_k(&xs[i], &succ[i + 1], nullable, first, follow, changed, k);
            }
        }
        Expr::Alt(xs) => {
            for x in xs {
                walk_follow_k(x, tail, nullable, first, follow, changed, k);
            }
        }
        Expr::Opt(x) => {
            walk_follow_k(x, tail, nullable, first, follow, changed, k);
        }
        Expr::Star(x) | Expr::Plus(x) => {
            // Inside a repetition the body can be followed by more
            // iterations or by the outer tail — hence star_k then tail.
            let fx = first_of(x, nullable, first, k);
            let star = star_k(&fx, k);
            let body_tail = concat_k(&star, tail, k);
            walk_follow_k(x, &body_tail, nullable, first, follow, changed, k);
        }
    }
}

fn expr_nullable(e: &Expr, nullable: &BTreeMap<String, bool>) -> bool {
    match e {
        Expr::Empty => true,
        Expr::Token(_) => false,
        Expr::Rule(n) => *nullable.get(n).unwrap_or(&false),
        Expr::Seq(xs) => xs.iter().all(|x| expr_nullable(x, nullable)),
        Expr::Alt(xs) => xs.iter().any(|x| expr_nullable(x, nullable)),
        Expr::Opt(_) | Expr::Star(_) => true,
        Expr::Plus(x) => expr_nullable(x, nullable),
    }
}