antlr-rust-runtime 0.34.0

High performance Rust runtime and target support for ANTLR v4 generated parsers
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026 Konstantin Vyatkin
//! Abstract Transition Network structures used by generated lexers and
//! parsers.
//!
//! Lexers deserialize ANTLR metadata into a graph because lexer simulation
//! still mutates and inspects that shape. Parsers use the packed,
//! index-addressed [`parser_atn::ParserAtn`] representation instead.

pub(crate) mod ascii_range;
mod bypass;
pub mod lexer;
pub mod lexer_dfa;
pub mod parser;
pub mod parser_atn;
pub mod serialized;

#[derive(Clone, Copy)]
struct TailCallSite {
    start: usize,
    stop: usize,
    rule_index: usize,
    state_count: usize,
}

#[derive(Default)]
struct TailCallScratch {
    marks: Vec<u8>,
    work: Vec<(usize, bool)>,
    successors: Vec<usize>,
}

fn plain_epsilon_tail_call<StateKind, StateRule, PushSuccessors>(
    site: TailCallSite,
    scratch: &mut TailCallScratch,
    state_kind: StateKind,
    state_rule_index: StateRule,
    push_successors: PushSuccessors,
) -> bool
where
    StateKind: Fn(usize) -> AtnStateKind,
    StateRule: Fn(usize) -> Option<usize>,
    PushSuccessors: Fn(usize, &mut Vec<usize>) -> bool,
{
    let TailCallSite {
        start,
        stop,
        rule_index,
        state_count,
    } = site;
    if start >= state_count
        || stop >= state_count
        || state_kind(stop) != AtnStateKind::RuleStop
        || state_rule_index(stop) != Some(rule_index)
    {
        return false;
    }

    // Reject cycles as well as semantic, consuming, nested-rule, and dead-end
    // paths. Every continuation must finish the enclosing rule without
    // observable work.
    let TailCallScratch {
        marks,
        work,
        successors,
    } = scratch;
    marks.clear();
    marks.resize(state_count, 0);
    work.clear();
    work.push((start, false));
    successors.clear();
    while let Some((state, exiting)) = work.pop() {
        if state == stop {
            continue;
        }
        if state >= state_count {
            return false;
        }
        if exiting {
            marks[state] = 2;
            continue;
        }
        match marks[state] {
            1 => return false,
            2 => continue,
            _ => {}
        }
        if state_kind(state) == AtnStateKind::RuleStop
            || state_rule_index(state) != Some(rule_index)
        {
            return false;
        }
        successors.clear();
        if !push_successors(state, successors) || successors.is_empty() {
            return false;
        }
        marks[state] = 1;
        work.push((state, true));
        work.extend(successors.iter().copied().map(|target| (target, false)));
    }
    true
}

/// Deserialized lexer Abstract Transition Network.
///
/// The structure keeps the state graph plus ANTLR side tables such as
/// rule-to-start, rule-to-token, mode-to-start, decisions, and actions. Parser
/// ATNs never use this object-graph representation.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LexerAtn {
    max_token_type: i32,
    states: Vec<LexerAtnState>,
    rule_to_start_state: Vec<usize>,
    rule_to_stop_state: Vec<usize>,
    rule_to_token_type: Vec<i32>,
    mode_to_start_state: Vec<usize>,
    decision_to_state: Vec<usize>,
    lexer_actions: Vec<LexerAction>,
}

impl LexerAtn {
    /// Creates an empty lexer ATN with the maximum token type read from the
    /// serialized header.
    pub const fn new(max_token_type: i32) -> Self {
        Self {
            max_token_type,
            states: Vec::new(),
            rule_to_start_state: Vec::new(),
            rule_to_stop_state: Vec::new(),
            rule_to_token_type: Vec::new(),
            mode_to_start_state: Vec::new(),
            decision_to_state: Vec::new(),
            lexer_actions: Vec::new(),
        }
    }

    pub const fn max_token_type(&self) -> i32 {
        self.max_token_type
    }

    pub fn states(&self) -> &[LexerAtnState] {
        &self.states
    }

    pub fn state(&self, state_number: usize) -> Option<&LexerAtnState> {
        self.states.get(state_number)
    }

    pub fn state_mut(&mut self, state_number: usize) -> Option<&mut LexerAtnState> {
        self.states.get_mut(state_number)
    }

    /// Appends a state and returns the state number assigned by insertion
    /// order.
    pub fn add_state(&mut self, state: LexerAtnState) -> usize {
        let index = self.states.len();
        self.states.push(state);
        index
    }

    pub fn decision_to_state(&self) -> &[usize] {
        &self.decision_to_state
    }

    pub fn add_decision_state(&mut self, state_number: usize) {
        self.decision_to_state.push(state_number);
    }

    pub fn rule_to_start_state(&self) -> &[usize] {
        &self.rule_to_start_state
    }

    pub fn set_rule_to_start_state(&mut self, rule_to_start_state: Vec<usize>) {
        self.rule_to_start_state = rule_to_start_state;
    }

    pub fn rule_to_stop_state(&self) -> &[usize] {
        &self.rule_to_stop_state
    }

    pub fn set_rule_to_stop_state(&mut self, rule_to_stop_state: Vec<usize>) {
        self.rule_to_stop_state = rule_to_stop_state;
    }

    pub fn rule_to_token_type(&self) -> &[i32] {
        &self.rule_to_token_type
    }

    pub fn set_rule_to_token_type(&mut self, rule_to_token_type: Vec<i32>) {
        self.rule_to_token_type = rule_to_token_type;
    }

    pub fn mode_to_start_state(&self) -> &[usize] {
        &self.mode_to_start_state
    }

    pub fn add_mode_start_state(&mut self, state_number: usize) {
        self.mode_to_start_state.push(state_number);
    }

    pub fn lexer_actions(&self) -> &[LexerAction] {
        &self.lexer_actions
    }

    pub fn set_lexer_actions(&mut self, lexer_actions: Vec<LexerAction>) {
        self.lexer_actions = lexer_actions;
    }

    /// Recomputes conservative tail-call markers after the graph is complete.
    ///
    /// Call this after every transition and derived rule-return edge has been
    /// added. Any later mutation through [`Self::add_state`],
    /// [`Self::state_mut`], or [`LexerAtnState::add_transition`] invalidates the
    /// stored markers, so callers must run this analysis again before prediction.
    #[doc(hidden)]
    pub fn identify_tail_calls(&mut self) {
        let mut tail_calls = Vec::new();
        let mut scratch = TailCallScratch::default();
        for source in 0..self.states.len() {
            for index in 0..self.states[source].transitions.len() {
                let follow_state = match &self.states[source].transitions[index] {
                    LexerTransition::Rule { follow_state, .. } => *follow_state,
                    _ => continue,
                };
                tail_calls.push((
                    source,
                    index,
                    self.tail_call_follow_is_safe(source, follow_state, &mut scratch),
                ));
            }
        }
        for (source, index, tail_call) in tail_calls {
            if let Some(LexerTransition::Rule {
                tail_call: marker, ..
            }) = self
                .states
                .get_mut(source)
                .and_then(|state| state.transitions.get_mut(index))
            {
                *marker = tail_call;
            }
        }
    }

    fn tail_call_follow_is_safe(
        &self,
        source: usize,
        start: usize,
        scratch: &mut TailCallScratch,
    ) -> bool {
        let Some(rule_index) = self.states.get(source).and_then(|state| state.rule_index) else {
            return false;
        };
        let Some(&stop) = self.rule_to_stop_state.get(rule_index) else {
            return false;
        };
        plain_epsilon_tail_call(
            TailCallSite {
                start,
                stop,
                rule_index,
                state_count: self.states.len(),
            },
            scratch,
            |state| self.states[state].kind,
            |state| self.states[state].rule_index,
            |state, successors| {
                for transition in &self.states[state].transitions {
                    let LexerTransition::Epsilon { target } = transition else {
                        return false;
                    };
                    successors.push(*target);
                }
                true
            },
        )
    }
}

/// A node in the ANTLR ATN graph.
///
/// Some ANTLR state subclasses carry references to paired states, such as a
/// block-start state's end state or a loop-end state's loop-back state. This
/// representation stores those links as state numbers so the graph remains easy
/// to clone and serialize in tests.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LexerAtnState {
    pub state_number: usize,
    pub rule_index: Option<usize>,
    pub kind: AtnStateKind,
    pub end_state: Option<usize>,
    pub loop_back_state: Option<usize>,
    pub non_greedy: bool,
    pub precedence_rule_decision: bool,
    pub left_recursive_rule: bool,
    pub transitions: Vec<LexerTransition>,
}

impl LexerAtnState {
    /// Creates an ATN state with no rule index and no outgoing transitions.
    pub const fn new(state_number: usize, kind: AtnStateKind) -> Self {
        Self {
            state_number,
            rule_index: None,
            kind,
            end_state: None,
            loop_back_state: None,
            non_greedy: false,
            precedence_rule_decision: false,
            left_recursive_rule: false,
            transitions: Vec::new(),
        }
    }

    #[must_use]
    pub const fn with_rule_index(mut self, rule_index: usize) -> Self {
        self.rule_index = Some(rule_index);
        self
    }

    /// Adds an outgoing transition in serialized order.
    ///
    /// Transition order matters for alternatives and lexer priority, so the
    /// runtime preserves the order emitted by ANTLR.
    pub fn add_transition(&mut self, transition: LexerTransition) {
        self.transitions.push(transition);
    }

    pub fn is_rule_stop(&self) -> bool {
        self.kind == AtnStateKind::RuleStop
    }
}

/// Serialized ANTLR state kind.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AtnStateKind {
    Invalid,
    Basic,
    RuleStart,
    BlockStart,
    PlusBlockStart,
    StarBlockStart,
    TokenStart,
    RuleStop,
    BlockEnd,
    StarLoopBack,
    StarLoopEntry,
    PlusLoopBack,
    LoopEnd,
}

/// Edge between two ATN states.
///
/// Epsilon-like transitions do not consume input. Matching transitions compare
/// the current input symbol against an atom, range, set, negated set, or
/// wildcard.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum LexerTransition {
    Epsilon {
        target: usize,
    },
    Atom {
        target: usize,
        label: i32,
    },
    Range {
        target: usize,
        start: i32,
        stop: i32,
    },
    Set {
        target: usize,
        set: IntervalSet,
    },
    NotSet {
        target: usize,
        set: IntervalSet,
    },
    Wildcard {
        target: usize,
    },
    Rule {
        target: usize,
        rule_index: usize,
        follow_state: usize,
        precedence: i32,
        tail_call: bool,
    },
    Predicate {
        target: usize,
        rule_index: usize,
        pred_index: usize,
        context_dependent: bool,
    },
    Action {
        target: usize,
        rule_index: usize,
        action_index: Option<usize>,
        context_dependent: bool,
    },
    Precedence {
        target: usize,
        precedence: i32,
    },
}

impl LexerTransition {
    /// Returns the target state number for this transition.
    pub const fn target(&self) -> usize {
        match self {
            Self::Epsilon { target }
            | Self::Atom { target, .. }
            | Self::Range { target, .. }
            | Self::Set { target, .. }
            | Self::NotSet { target, .. }
            | Self::Wildcard { target }
            | Self::Rule { target, .. }
            | Self::Predicate { target, .. }
            | Self::Action { target, .. }
            | Self::Precedence { target, .. } => *target,
        }
    }

    /// Returns whether traversing this transition consumes no input.
    pub const fn is_epsilon(&self) -> bool {
        matches!(
            self,
            Self::Epsilon { .. }
                | Self::Rule { .. }
                | Self::Predicate { .. }
                | Self::Action { .. }
                | Self::Precedence { .. }
        )
    }

    /// Returns whether this rule call's follow state is a provably redundant
    /// prediction-context frame.
    pub const fn is_tail_call(&self) -> bool {
        matches!(
            self,
            Self::Rule {
                tail_call: true,
                ..
            }
        )
    }

    /// Tests whether this transition consumes `symbol`.
    ///
    /// `min_vocabulary` and `max_vocabulary` define the accepted symbol range
    /// for wildcard and negated-set transitions.
    pub fn matches(&self, symbol: i32, min_vocabulary: i32, max_vocabulary: i32) -> bool {
        match self {
            Self::Atom { label, .. } => *label == symbol,
            Self::Range { start, stop, .. } => (*start..=*stop).contains(&symbol),
            Self::Set { set, .. } => set.contains(symbol),
            Self::NotSet { set, .. } => {
                (min_vocabulary..=max_vocabulary).contains(&symbol) && !set.contains(symbol)
            }
            Self::Wildcard { .. } => (min_vocabulary..=max_vocabulary).contains(&symbol),
            Self::Epsilon { .. }
            | Self::Rule { .. }
            | Self::Predicate { .. }
            | Self::Action { .. }
            | Self::Precedence { .. } => false,
        }
    }
}

/// Ordered set of integer intervals used by set and negated-set transitions.
///
/// Unicode grammars can contain very large ranges, so this stores normalized
/// intervals rather than expanding every code point into a flat set.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct IntervalSet {
    ranges: Vec<(i32, i32)>,
}

impl IntervalSet {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn from_range(start: i32, stop: i32) -> Self {
        let mut set = Self::new();
        set.add_range(start, stop);
        set
    }

    pub fn add(&mut self, value: i32) {
        self.add_range(value, value);
    }

    /// Adds an inclusive interval and merges it with adjacent or overlapping
    /// intervals.
    pub fn add_range(&mut self, start: i32, stop: i32) {
        let (start, stop) = if start <= stop {
            (start, stop)
        } else {
            (stop, start)
        };
        self.ranges.push((start, stop));
        self.normalize();
    }

    /// Re-sorts and coalesces interval storage after insertion.
    fn normalize(&mut self) {
        self.ranges.sort_unstable();
        let mut merged: Vec<(i32, i32)> = Vec::with_capacity(self.ranges.len());
        for (start, stop) in self.ranges.drain(..) {
            if let Some((_, last_stop)) = merged.last_mut() {
                if start <= last_stop.saturating_add(1) {
                    *last_stop = (*last_stop).max(stop);
                    continue;
                }
            }
            merged.push((start, stop));
        }
        self.ranges = merged;
    }

    /// Returns true when `value` falls inside any stored interval.
    pub fn contains(&self, value: i32) -> bool {
        // Ranges are kept sorted and coalesced by `normalize`, so the first
        // range whose `start > value` cannot contain `value` and neither can
        // any range after it. Binary searching for that boundary turns
        // membership lookup from O(n) to O(log n), which matters because
        // parser/lexer hot paths call this once per `Set`/`NotSet`/`Wildcard`
        // transition probe.
        match self.ranges.binary_search_by(|(start, _)| start.cmp(&value)) {
            Ok(_) => true,
            Err(pos) => pos > 0 && self.ranges[pos - 1].1 >= value,
        }
    }

    pub fn ranges(&self) -> &[(i32, i32)] {
        &self.ranges
    }

    pub const fn is_empty(&self) -> bool {
        self.ranges.is_empty()
    }
}

/// Serialized lexer action attached to an action transition.
///
/// These actions are grammar-independent operations generated by ANTLR's lexer
/// commands (`skip`, `more`, `type`, `channel`, `pushMode`, `popMode`, and
/// `mode`). Custom embedded actions are represented but intentionally inert
/// until a generated semantic-action hook exists.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum LexerAction {
    Channel(i32),
    Custom { rule_index: i32, action_index: i32 },
    Mode(i32),
    More,
    PopMode,
    PushMode(i32),
    Skip,
    Type(i32),
}

#[cfg(test)]
mod tests {
    use super::*;

    fn classified_lexer_rule(continuations: Vec<(usize, LexerTransition)>) -> LexerAtn {
        let mut atn = LexerAtn::new(4);
        for (kind, rule_index) in [
            (AtnStateKind::RuleStart, 0),
            (AtnStateKind::Basic, 0),
            (AtnStateKind::Basic, 0),
            (AtnStateKind::RuleStop, 0),
            (AtnStateKind::RuleStart, 1),
            (AtnStateKind::RuleStop, 1),
            (AtnStateKind::RuleStart, 2),
            (AtnStateKind::RuleStop, 2),
            (AtnStateKind::Basic, 0),
            (AtnStateKind::Basic, 0),
        ] {
            let state_number = atn.states.len();
            atn.add_state(LexerAtnState::new(state_number, kind).with_rule_index(rule_index));
        }
        atn.set_rule_to_start_state(vec![0, 4, 6]);
        atn.set_rule_to_stop_state(vec![3, 5, 7]);
        atn.state_mut(0)
            .expect("caller start")
            .add_transition(LexerTransition::Epsilon { target: 1 });
        atn.state_mut(1)
            .expect("call source")
            .add_transition(LexerTransition::Rule {
                target: 4,
                rule_index: 1,
                follow_state: 2,
                precedence: 0,
                tail_call: false,
            });
        atn.state_mut(4)
            .expect("callee start")
            .add_transition(LexerTransition::Epsilon { target: 5 });
        atn.state_mut(6)
            .expect("other rule start")
            .add_transition(LexerTransition::Epsilon { target: 7 });
        for (source, transition) in continuations {
            atn.state_mut(source)
                .expect("continuation source")
                .add_transition(transition);
        }
        atn.identify_tail_calls();
        atn
    }

    fn classified_lexer_call(atn: &LexerAtn) -> &LexerTransition {
        &atn.state(1).expect("call source").transitions[0]
    }

    #[test]
    fn lexer_tail_call_classifier_is_conservative() {
        let positive = classified_lexer_rule(vec![
            (2, LexerTransition::Epsilon { target: 8 }),
            (8, LexerTransition::Epsilon { target: 3 }),
        ]);
        assert!(classified_lexer_call(&positive).is_tail_call());

        let rejected = [
            ("dead end", Vec::new()),
            (
                "consuming edge",
                vec![(
                    2,
                    LexerTransition::Atom {
                        target: 3,
                        label: 1,
                    },
                )],
            ),
            (
                "predicate",
                vec![(
                    2,
                    LexerTransition::Predicate {
                        target: 3,
                        rule_index: 0,
                        pred_index: 0,
                        context_dependent: false,
                    },
                )],
            ),
            (
                "action",
                vec![(
                    2,
                    LexerTransition::Action {
                        target: 3,
                        rule_index: 0,
                        action_index: Some(0),
                        context_dependent: false,
                    },
                )],
            ),
            (
                "nested rule",
                vec![(
                    2,
                    LexerTransition::Rule {
                        target: 4,
                        rule_index: 1,
                        follow_state: 3,
                        precedence: 0,
                        tail_call: false,
                    },
                )],
            ),
            (
                "epsilon cycle",
                vec![(2, LexerTransition::Epsilon { target: 2 })],
            ),
            (
                "other rule stop",
                vec![(2, LexerTransition::Epsilon { target: 7 })],
            ),
        ];
        for (label, continuations) in rejected {
            let atn = classified_lexer_rule(continuations);
            assert!(
                !classified_lexer_call(&atn).is_tail_call(),
                "{label} must not be classified as a lexer tail call"
            );
        }
    }

    #[test]
    fn interval_set_handles_ranges() {
        let set = IntervalSet::from_range(2, 4);
        assert!(set.contains(2));
        assert!(set.contains(3));
        assert!(set.contains(4));
        assert!(!set.contains(5));
        assert_eq!(set.ranges(), &[(2, 4)]);
    }
}