Skip to main content

dbt_antlr_runtime/
parser.rs

1//! Base parser implementation
2use std::borrow::{Borrow, Cow};
3use std::cell::Cell;
4use std::fmt::{Debug, Formatter};
5use std::marker::PhantomData;
6use std::ops::{Deref, DerefMut};
7
8use crate::arena::Arena;
9use crate::atn::ATN;
10use crate::atn_simulator::{IATNSimulator, ParserATNSimulatorManager};
11use crate::error_listener::{
12    ConsoleErrorListener, ErrorListener, ErrorListenerDelegate, ProxyErrorListener,
13};
14use crate::error_strategy::ErrorStrategy;
15use crate::errors::ANTLRError;
16use crate::interval_set::IntervalSet;
17use crate::parser_atn_simulator::ParserATNSimulator;
18use crate::prediction_mode::PredictionMode;
19use crate::recognizer::{Actions, Recognizer};
20use crate::rule_context::{states_stack, RuleContext as _};
21use crate::token::{OwningToken, Token, TOKEN_EOF};
22use crate::token_factory::TokenFactory;
23use crate::token_stream::TokenStream;
24use crate::tree::{NodeKindType, ParseTreeListener, Tree as _, TreeNode};
25use crate::utils::cell_update;
26use crate::vocabulary::Vocabulary;
27
28#[cfg(feature = "recursion-limit")]
29const DEFAULT_RECURSION_LIMIT: u32 = 2000;
30
31/// parser functionality required for `ParserATNSimulator` to work
32#[allow(missing_docs)]
33pub trait Parser<'input, 'arena, TF>: Recognizer<'input, 'arena, TF::Tok>
34where
35    'input: 'arena,
36    TF: TokenFactory<'input, 'arena> + 'arena,
37{
38    fn get_arena(&self) -> &'arena Arena;
39
40    fn get_interpreter(&self) -> &'arena ParserATNSimulator<'arena>;
41
42    fn get_token_factory(&self) -> &TF;
43
44    fn get_current_context(&self) -> &'arena TreeNode<'input, 'arena, Self::Node, TF::Tok>;
45
46    /// Current rule context, or `None` when the parser is not inside a rule.
47    ///
48    /// Java has no equivalent accessor: `ParserATNSimulator.adaptivePredict`
49    /// takes the outer context as an explicit parameter, and the tool's
50    /// interpreter tests pass `ParserRuleContext.EMPTY`. This accessor lets
51    /// [crate::parser_atn_simulator::ParserATNSimulator::adaptive_predict]
52    /// treat a missing context as Java's `ParserRuleContext.EMPTY`.
53    fn get_current_context_opt(
54        &self,
55    ) -> Option<&'arena TreeNode<'input, 'arena, Self::Node, TF::Tok>> {
56        Some(self.get_current_context())
57    }
58
59    fn consume(
60        &mut self,
61        err_handler: &mut impl ErrorStrategy<'input, 'arena, TF, Self>,
62    ) -> Result<(), ANTLRError>
63    where
64        Self: Sized;
65
66    fn precpred(
67        &self,
68        localctx: Option<&TreeNode<'input, 'arena, Self::Node, TF::Tok>>,
69        precedence: i32,
70    ) -> bool;
71
72    fn get_input_stream_mut(&mut self) -> &mut dyn TokenStream<'input, 'arena, TF>;
73    fn get_input_stream(&self) -> &dyn TokenStream<'input, 'arena, TF>;
74    fn get_current_token(&self) -> &'arena TF::Tok;
75    fn get_expected_tokens<'a>(&'a self) -> Cow<'a, IntervalSet>;
76
77    fn add_error_listener(
78        &mut self,
79        listener: Box<dyn ErrorListener<'input, 'arena, Self, TF::Tok> + 'input>,
80    ) where
81        Self: Sized;
82
83    fn remove_error_listeners(&mut self);
84
85    fn notify_error_listeners(
86        &self,
87        msg: String,
88        offending_token: Option<isize>,
89        err: Option<&ANTLRError>,
90    );
91    fn get_error_lister_dispatch<'a>(
92        &'a self,
93    ) -> Box<dyn ErrorListener<'input, 'arena, Self, TF::Tok> + 'a>
94    where
95        Self: Sized;
96
97    fn is_expected_token(&self, symbol: i32) -> bool;
98
99    /// The `(parent_ctx_ptr, call_sites)` pairs of the active neutral parses,
100    /// outermost first.
101    #[doc(hidden)]
102    fn neutral_call_sites(&self) -> &[(*const (), &'static [i32])] {
103        &[]
104    }
105    fn get_precedence(&self) -> i32;
106
107    fn get_state(&self) -> i32;
108    fn set_state(&mut self, v: i32);
109    fn get_rule_invocation_stack(&self) -> Vec<String>;
110
111    #[cfg(feature = "recursion-limit")]
112    fn get_recursion_limit(&self) -> u32;
113    #[cfg(feature = "recursion-limit")]
114    fn set_recursion_limit(&mut self, v: u32);
115}
116
117/// The parser state that the neutral parse of a shared-descent group saves and
118/// restores (see [`BaseParser::begin_neutral_parse`]).
119#[doc(hidden)]
120#[derive(Debug)]
121pub struct NeutralParse {
122    start_index: isize,
123    mark: isize,
124    /// The stream index where the shared rule starts, after the prefix.
125    shared_index: isize,
126    state: i32,
127    children: usize,
128    matched_eof: bool,
129}
130
131/// A shared rule parsed by a neutral parse, waiting for the re-descent to call
132/// it (see [`BaseParser::resume_take`]).
133struct Resume<'input, 'arena, Node, Tok>
134where
135    'input: 'arena,
136    Node: NodeKindType<'arena, Tok>,
137    Tok: Token + 'input,
138{
139    node: &'arena TreeNode<'input, 'arena, Node, Tok>,
140    rule: usize,
141    shared_index: isize,
142    tail_index: isize,
143    /// `matched_eof` after the shared rule.
144    matched_eof: bool,
145    mark: isize,
146}
147
148/// Abstract base parser implementation
149///
150/// Only meant to be instantiated by generated parsers
151pub struct BaseParser<'input, 'arena, Ext, Node, Input, TF>
152where
153    'input: 'arena,
154    // Grammar-specific implementation of parser functionality required for
155    // `ParserATNSimulator` to work
156    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
157    // Token factory used by the input stream
158    TF: TokenFactory<'input, 'arena> + 'arena,
159    // Token stream (lexer)
160    Input: TokenStream<'input, 'arena, TF>,
161    Node: NodeKindType<'arena, TF::Tok>,
162{
163    interp: Option<ParserATNSimulator<'arena>>,
164    atn_manager: &'static ParserATNSimulatorManager,
165    global_cache_threshold: usize,
166
167    /// Rule context parser is currently processing
168    ctx: *mut (),
169
170    /// Track the {@link ParserRuleContext} objects during the parse and hook
171    /// them up using the {@link ParserRuleContext#children} list so that it
172    /// forms a parse tree. The {@link ParserRuleContext} returned from the start
173    /// rule represents the root of the parse tree.
174    ///
175    /// <p>Note that if we are not building parse trees, rule contexts only point
176    /// upwards. When a rule exits, it returns the context bute that gets garbage
177    /// collected if nobody holds a reference. It points upwards but nobody
178    /// points at it. </p>
179    ///
180    /// <p>When we build parse trees, we are adding all of these contexts to
181    /// {@link ParserRuleContext#children} list. Contexts are then not candidates
182    /// for garbage collection.</p>
183    ///
184    /// Returns {@code true} if a complete parse tree will be constructed while
185    /// parsing, otherwise {@code false}
186    pub build_parse_trees: bool,
187
188    /// true if parser reached EOF
189    pub matched_eof: bool,
190
191    state: i32,
192    /// Token stream that is currently used by this parser
193    pub input: Input,
194    precedence_stack: Vec<i32>,
195    #[cfg(feature = "recursion-limit")]
196    pub recursion_limit: u32,
197    #[cfg(feature = "recursion-limit")]
198    current_recursion_depth: u32,
199
200    parse_listeners: Vec<Box<Node::Listener>>,
201    _syntax_errors: Cell<i32>,
202    error_listeners: Vec<ErrorListenerDelegate<'input, 'arena, Self, TF::Tok>>,
203    /// Mutes the parse listeners while it is above zero. A counter, because
204    /// the neutral parses of nested shared-descent groups nest (see
205    /// [`Self::begin_neutral_parse`]).
206    muted: u32,
207    /// The pending re-descent of a shared-descent group (see
208    /// [`Self::resume_neutral_parse`]).
209    resume: Option<Resume<'input, 'arena, Node, TF::Tok>>,
210    /// The `(parent_ctx_ptr, call_sites)` pairs of the active neutral parses,
211    /// outermost first.
212    neutral_call_sites: Vec<(*const (), &'static [i32])>,
213
214    pub arena: &'arena Arena,
215    ext: Ext,
216    pd: PhantomData<(
217        &'input (),
218        &'arena TF,
219        //        &'arena TreeNode<'input, 'arena, Node, TF::Tok>,
220    )>,
221}
222
223impl<'input, 'arena, Ext, Node, Input, TF> Debug
224    for BaseParser<'input, 'arena, Ext, Node, Input, TF>
225where
226    'input: 'arena,
227    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
228    TF: TokenFactory<'input, 'arena> + 'arena,
229    Input: TokenStream<'input, 'arena, TF>,
230    Node: NodeKindType<'arena, TF::Tok>,
231{
232    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
233        f.debug_struct("BaseParser")
234            .field("state", &self.state)
235            .field("matched_eof", &self.matched_eof)
236            .field("build_parse_trees", &self.build_parse_trees)
237            .finish_non_exhaustive()
238    }
239}
240
241impl<'input, 'arena, Ext, Node, Input, TF> Deref
242    for BaseParser<'input, 'arena, Ext, Node, Input, TF>
243where
244    'input: 'arena,
245    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
246    TF: TokenFactory<'input, 'arena> + 'arena,
247    Input: TokenStream<'input, 'arena, TF>,
248    Node: NodeKindType<'arena, TF::Tok>,
249{
250    type Target = Ext;
251
252    fn deref(&self) -> &Self::Target {
253        &self.ext
254    }
255}
256
257impl<'input, 'arena, Ext, Node, Input, TF> DerefMut
258    for BaseParser<'input, 'arena, Ext, Node, Input, TF>
259where
260    'input: 'arena,
261    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
262    TF: TokenFactory<'input, 'arena> + 'arena,
263    Input: TokenStream<'input, 'arena, TF>,
264    Node: NodeKindType<'arena, TF::Tok>,
265{
266    fn deref_mut(&mut self) -> &mut Self::Target {
267        &mut self.ext
268    }
269}
270
271pub trait ParserRecog<'input, 'arena, R, Tok>: Actions<'input, 'arena, R, Tok>
272where
273    'input: 'arena,
274    R: Recognizer<'input, 'arena, Tok>,
275    Tok: Token + 'input,
276{
277    fn get_atn_simulator_man(&self) -> &'static ParserATNSimulatorManager;
278}
279
280impl<'input, 'arena, Ext, Node, Input, TF> Recognizer<'input, 'arena, TF::Tok>
281    for BaseParser<'input, 'arena, Ext, Node, Input, TF>
282where
283    'input: 'arena,
284    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
285    TF: TokenFactory<'input, 'arena> + 'arena,
286    Input: TokenStream<'input, 'arena, TF>,
287    Node: NodeKindType<'arena, TF::Tok>,
288{
289    type Node = Node;
290
291    fn sempred(
292        &mut self,
293        localctx: Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>>,
294        rule_index: i32,
295        action_index: i32,
296    ) -> bool {
297        Ext::sempred(localctx, rule_index, action_index, self)
298    }
299
300    fn get_rule_names(&self) -> &[&str] {
301        self.ext.get_rule_names()
302    }
303
304    fn get_vocabulary(&self) -> &dyn Vocabulary {
305        self.ext.get_vocabulary()
306    }
307
308    fn get_grammar_file_name(&self) -> &str {
309        self.ext.get_grammar_file_name()
310    }
311
312    fn get_atn(&self) -> &ATN {
313        self.interp.as_ref().unwrap().atn()
314    }
315}
316
317impl<'input, 'arena, Ext, Node, Input, TF> Parser<'input, 'arena, TF>
318    for BaseParser<'input, 'arena, Ext, Node, Input, TF>
319where
320    'input: 'arena,
321    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
322    TF: TokenFactory<'input, 'arena> + 'arena,
323    Input: TokenStream<'input, 'arena, TF>,
324    Node: NodeKindType<'arena, TF::Tok>,
325{
326    fn get_arena(&self) -> &'arena Arena {
327        self.arena
328    }
329
330    #[inline(always)]
331    fn get_interpreter(&self) -> &'arena ParserATNSimulator<'arena> {
332        unsafe {
333            std::mem::transmute::<&ParserATNSimulator<'arena>, &'arena ParserATNSimulator<'arena>>(
334                self.interp.as_ref().unwrap(),
335            )
336        }
337    }
338
339    fn get_token_factory(&self) -> &TF {
340        self.input.get_token_source().get_token_factory()
341    }
342
343    #[inline(always)]
344    fn get_current_context(&self) -> &'arena TreeNode<'input, 'arena, Node, TF::Tok> {
345        self.ctx().unwrap()
346    }
347
348    #[inline(always)]
349    fn get_current_context_opt(&self) -> Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>> {
350        self.ctx()
351    }
352
353    fn consume(
354        &mut self,
355        err_handler: &mut impl ErrorStrategy<'input, 'arena, TF, Self>,
356    ) -> Result<(), ANTLRError> {
357        let o = self.get_current_token();
358        if o.borrow().get_token_type() != TOKEN_EOF {
359            self.input.consume();
360        }
361        if self.build_parse_trees || !self.parse_listeners.is_empty() {
362            if err_handler.in_error_recovery_mode(self) {
363                // todo report ructc inference issue
364                let node = self.create_error_node(o)?;
365                self.add_child_to_ctx(node);
366                if self.muted == 0 {
367                    for listener in &mut self.parse_listeners {
368                        listener.visit_error_node(
369                            node.as_error_node()
370                                .expect("node was created as error node"),
371                        )?
372                    }
373                }
374            } else {
375                let node = self.create_token_node(o)?;
376                self.add_child_to_ctx(node);
377                if self.muted == 0 {
378                    for listener in &mut self.parse_listeners {
379                        listener.visit_terminal(
380                            node.as_terminal_node()
381                                .expect("node was created as terminal node"),
382                        )?
383                    }
384                }
385            }
386        }
387        Ok(())
388    }
389
390    fn precpred(
391        &self,
392        _localctx: Option<&TreeNode<'input, 'arena, Node, TF::Tok>>,
393        precedence: i32,
394    ) -> bool {
395        //        localctx.map(|it|println!("check at{}",it.to_string_tree(self)));
396        //        println!("{}",self.get_precedence());
397        precedence >= self.get_precedence()
398    }
399
400    fn get_input_stream_mut(&mut self) -> &mut dyn TokenStream<'input, 'arena, TF> {
401        &mut self.input //.as_mut()
402    }
403
404    fn get_input_stream(&self) -> &dyn TokenStream<'input, 'arena, TF> {
405        &self.input
406    }
407
408    #[inline]
409    fn get_current_token(&self) -> &'arena TF::Tok {
410        self.input.get(self.input.index())
411    }
412
413    fn get_expected_tokens<'a>(&'a self) -> Cow<'a, IntervalSet> {
414        let states_stack = states_stack(self.ctx().unwrap());
415        self.interp
416            .as_ref()
417            .unwrap()
418            .atn()
419            .get_expected_tokens::<TF::Tok>(self.state, states_stack)
420    }
421
422    fn add_error_listener(
423        &mut self,
424        listener: Box<dyn ErrorListener<'input, 'arena, Self, TF::Tok> + 'input>,
425    ) {
426        self.error_listeners
427            .push(ErrorListenerDelegate::new(listener))
428    }
429
430    fn remove_error_listeners(&mut self) {
431        self.error_listeners.clear();
432    }
433
434    fn notify_error_listeners(
435        &self,
436        msg: String,
437        offending_token: Option<isize>,
438        err: Option<&ANTLRError>,
439    ) {
440        cell_update(&self._syntax_errors, |it| it + 1);
441        let offending_token: Option<&_> = match offending_token {
442            None => Some(self.get_current_token().borrow()),
443            Some(x) => Some(self.input.get(x).borrow()),
444        };
445        let line = offending_token.map_or(0, |x| x.get_line());
446        let column = offending_token.map_or(-1, |x| x.get_char_position_in_line());
447
448        for listener in self.error_listeners.iter() {
449            listener.syntax_error(
450                self,
451                offending_token.map(|x| x as _),
452                line,
453                column,
454                &msg,
455                err,
456            )
457        }
458    }
459
460    fn get_error_lister_dispatch<'a>(
461        &'a self,
462    ) -> Box<dyn ErrorListener<'input, 'arena, Self, TF::Tok> + 'a> {
463        Box::new(ProxyErrorListener {
464            delegates: self.error_listeners.borrow(),
465        })
466    }
467
468    fn is_expected_token(&self, _symbol: i32) -> bool {
469        unimplemented!()
470    }
471
472    fn get_precedence(&self) -> i32 {
473        *self.precedence_stack.last().unwrap_or(&-1)
474    }
475
476    #[inline(always)]
477    fn neutral_call_sites(&self) -> &[(*const (), &'static [i32])] {
478        &self.neutral_call_sites
479    }
480
481    #[inline(always)]
482    fn get_state(&self) -> i32 {
483        self.state
484    }
485
486    #[inline(always)]
487    fn set_state(&mut self, v: i32) {
488        self.state = v;
489    }
490
491    fn get_rule_invocation_stack(&self) -> Vec<String> {
492        let mut vec = Vec::new();
493        let rule_names = self.get_rule_names();
494        let mut ctx = self.get_current_context();
495        loop {
496            let rule_index = ctx.get_rule_index();
497            vec.push(rule_names.get(rule_index).unwrap_or(&"n/a").to_string());
498            ctx = if let Some(parent) = ctx.get_parent() {
499                parent
500            } else {
501                break;
502            }
503        }
504        vec
505    }
506
507    #[cfg(feature = "recursion-limit")]
508    fn get_recursion_limit(&self) -> u32 {
509        self.recursion_limit
510    }
511
512    #[cfg(feature = "recursion-limit")]
513    fn set_recursion_limit(&mut self, v: u32) {
514        self.recursion_limit = v;
515    }
516
517    //    fn get_rule_invocation_stack(&self, c: _) -> Vec<String> {
518    //        unimplemented!()
519    //    }
520}
521
522#[allow(missing_docs)] // todo docs
523impl<'input, 'arena, Ext, Node, Input, TF> BaseParser<'input, 'arena, Ext, Node, Input, TF>
524where
525    'input: 'arena,
526    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
527    TF: TokenFactory<'input, 'arena> + 'arena,
528    Input: TokenStream<'input, 'arena, TF>,
529    Node: NodeKindType<'arena, TF::Tok>,
530{
531    /// Replaces the input token stream, mirroring Java's
532    /// `Parser.setTokenStream`.
533    ///
534    /// Unlike Java, this does not seek the old stream back to 0 or reset
535    /// parser state; it is intended for swapping in a fresh stream before any
536    /// rule invocation, as the ANTLR tool's interpreter tests do when they
537    /// reuse one parser (and its decision DFAs) across several inputs.
538    pub fn set_input_stream(&mut self, input: Input) {
539        self.input = input;
540    }
541
542    pub fn new_base_parser(arena: &'arena Arena, input: Input, ext: Ext) -> Self {
543        let atn_manager = ext.get_atn_simulator_man();
544        let interp = ParserATNSimulator::new(atn_manager.get_simulator(arena));
545        Self {
546            atn_manager,
547            interp: Some(interp),
548            global_cache_threshold: 0,
549            ctx: std::ptr::null_mut(),
550            build_parse_trees: true,
551            matched_eof: false,
552            state: -1,
553            input,
554            precedence_stack: vec![0],
555            #[cfg(feature = "recursion-limit")]
556            recursion_limit: DEFAULT_RECURSION_LIMIT,
557            #[cfg(feature = "recursion-limit")]
558            current_recursion_depth: 0,
559            parse_listeners: vec![],
560            _syntax_errors: Cell::new(0),
561            error_listeners: vec![ErrorListenerDelegate::new(
562                Box::new(ConsoleErrorListener {})
563                    as Box<dyn ErrorListener<'input, 'arena, Self, TF::Tok> + 'input>,
564            )],
565            muted: 0,
566            resume: None,
567            neutral_call_sites: Vec::new(),
568            arena,
569            ext,
570            pd: PhantomData,
571        }
572    }
573
574    pub fn set_global_cache_threshold(&mut self, threshold: usize) {
575        self.global_cache_threshold = threshold;
576    }
577
578    pub fn get_interpreter_mut(&mut self) -> &mut ParserATNSimulator<'arena> {
579        self.interp.as_mut().unwrap()
580    }
581
582    /// Predicts the alternative of a decision with its static SLL DFA table.
583    ///
584    /// Returns [`INVALID_ALT`] when the decision has no table and when the walk
585    /// leaves the table at an escape state the generated code must handle with
586    /// [`ParserATNSimulator::adaptive_predict`]. The virtual alternative of a
587    /// shared-descent group is an escape too: this block has no arm for it (see
588    /// [`Self::dfa_predict_virtual`]). A lookahead that has no table edge and no
589    /// fallback alternative fails like the adaptive simulation does.
590    ///
591    /// The walk consumes tokens while it reads them and rewinds afterwards, so
592    /// every step costs one `la(1)`, plus one `consume` unless the step stops
593    /// the walk. It must not read
594    /// `la(k)`: a channel-filtering token stream resolves `la(k)` by scanning
595    /// from the current token, which would make a k-token lookahead O(k^2).
596    ///
597    /// [`INVALID_ALT`]: crate::atn::INVALID_ALT
598    pub fn dfa_predict(&mut self, decision: i32) -> Result<i32, ANTLRError> {
599        let alt = self.dfa_walk(decision)?;
600        if alt >= crate::dfa::static_parser_dfa::VIRTUAL_ALT_BASE {
601            return Ok(crate::atn::INVALID_ALT);
602        }
603        Ok(alt)
604    }
605
606    /// Predicts like [`Self::dfa_predict`], but returns the virtual alternative
607    /// of a shared-descent group (at or above
608    /// [`VIRTUAL_ALT_BASE`](crate::dfa::static_parser_dfa::VIRTUAL_ALT_BASE))
609    /// instead of deferring. A generated `DescentAltBlock` resolves the group to
610    /// one of its member alternatives.
611    pub fn dfa_predict_virtual(&mut self, decision: i32) -> Result<i32, ANTLRError> {
612        self.dfa_walk(decision)
613    }
614    /// The table walk of [`Self::dfa_predict`] and
615    /// [`Self::dfa_predict_virtual`].
616    ///
617    /// An edge entry flags a target state where the walk stops, so the walk
618    /// resolves an accept without consuming the token that leads into it. A
619    /// one-token decision that accepts returns before it marks the input.
620    #[inline]
621    fn dfa_walk(&mut self, decision: i32) -> Result<i32, ANTLRError> {
622        use crate::dfa::static_parser_dfa::{NO_EDGE, STATE_MASK, TERMINAL_BIT};
623
624        let Some(tables) = self.get_interpreter().static_parser_tables() else {
625            return Ok(crate::atn::INVALID_ALT);
626        };
627        let Some(table) = tables.table(decision, self.get_precedence()) else {
628            return Ok(crate::atn::INVALID_ALT);
629        };
630        let token = self.input.la(1);
631        let entry = table.start_entry(token);
632        if entry >= TERMINAL_BIT && entry != NO_EDGE {
633            // SAFETY: the loader checked that edge targets are states.
634            let outcome = unsafe { table.outcome_unchecked(usize::from(entry & STATE_MASK)) };
635            if outcome > 0 {
636                return Ok(outcome);
637            }
638        }
639        self.dfa_walk_steps(table, decision, token, entry)
640    }
641
642    /// Continues [`Self::dfa_walk`] from the start state, whose entry for
643    /// `token` is `entry`.
644    fn dfa_walk_steps(
645        &mut self,
646        table: &crate::dfa::static_parser_dfa::StaticParserTable,
647        decision: i32,
648        mut token: i32,
649        mut entry: u16,
650    ) -> Result<i32, ANTLRError> {
651        use crate::dfa::static_parser_dfa::{Outcome, NO_EDGE, STATE_MASK, TERMINAL_BIT};
652
653        let start_index = self.input.index();
654        let mark = self.input.mark();
655        let mut state = 0_usize;
656        let result = loop {
657            if entry == NO_EDGE {
658                break self.dfa_no_edge(table, decision, state, start_index);
659            }
660            let target = usize::from(entry & STATE_MASK);
661            if entry & TERMINAL_BIT != 0 {
662                // SAFETY: the loader checked that edge targets are states.
663                let outcome = unsafe { table.outcome_unchecked(target) };
664                if outcome > 0 {
665                    self.input.seek(start_index);
666                    break Ok(outcome);
667                }
668                let outcome = Outcome::decode(outcome);
669                if matches!(outcome, Some(Outcome::Escape(Some(_)))) && token != TOKEN_EOF {
670                    // A frontier continuation resumes after the token that
671                    // led into the escape state.
672                    self.input.consume();
673                }
674                break self.dfa_terminal(table, decision, outcome, start_index);
675            }
676            if token != TOKEN_EOF {
677                self.input.consume();
678            }
679            state = target;
680            token = self.input.la(1);
681            // SAFETY: `state` is an edge target.
682            entry = unsafe { table.edge_entry_unchecked(state, token) };
683        };
684        self.input.release(mark);
685        result
686    }
687
688    /// Resolves a table walk without an edge for the current token: the start
689    /// state may stop the walk itself, and otherwise the state falls back to
690    /// an alternative that already left the decision or fails.
691    #[cold]
692    fn dfa_no_edge(
693        &mut self,
694        table: &crate::dfa::static_parser_dfa::StaticParserTable,
695        decision: i32,
696        state: usize,
697        start_index: isize,
698    ) -> Result<i32, ANTLRError> {
699        use crate::dfa::static_parser_dfa::Outcome;
700
701        // SAFETY: `state` is 0 or an edge target.
702        let outcome = unsafe { table.outcome_unchecked(state) };
703        if outcome > 0 {
704            self.input.seek(start_index);
705            return Ok(outcome);
706        }
707        if outcome != 0 {
708            return self.dfa_terminal(table, decision, Outcome::decode(outcome), start_index);
709        }
710        if let Some(fallback) = table.fallback(state as i32) {
711            self.input.seek(start_index);
712            return Ok(fallback);
713        }
714        let start = OwningToken::from(self.input.get(start_index) as &dyn Token);
715        let error = match self.input.lt(1) {
716            Some(offending) => {
717                ANTLRError::no_alt_full(self, start, OwningToken::from(offending as &dyn Token))
718            }
719            None => ANTLRError::no_alt(self),
720        };
721        self.input.seek(start_index);
722        Err(error)
723    }
724
725    /// Resolves a table walk that stopped at an escape, conflict escape, or
726    /// guarded state. A frontier continuation resumes from the current
727    /// position; every other outcome rewinds to `start_index`.
728    #[cold]
729    fn dfa_terminal(
730        &mut self,
731        table: &crate::dfa::static_parser_dfa::StaticParserTable,
732        decision: i32,
733        outcome: Option<crate::dfa::static_parser_dfa::Outcome>,
734        start_index: isize,
735    ) -> Result<i32, ANTLRError> {
736        use crate::dfa::static_parser_dfa::Outcome;
737
738        match outcome {
739            Some(Outcome::Escape(Some(slot))) => self.get_interpreter().resume_from_continuation(
740                decision,
741                slot as usize,
742                table.num_continuations(),
743                table.continuation_at(slot),
744                start_index,
745                self,
746            ),
747            Some(Outcome::Conflict(min_alt)) => {
748                self.input.seek(start_index);
749                self.get_interpreter()
750                    .adaptive_predict_conflict(decision, min_alt, self)
751            }
752            Some(Outcome::Guarded(guard)) => {
753                let defers = self.guard_defers(table, guard.group);
754                self.input.seek(start_index);
755                Ok(if defers { guard.defer_alt } else { guard.alt })
756            }
757            _ => {
758                self.input.seek(start_index);
759                Ok(crate::atn::INVALID_ALT)
760            }
761        }
762    }
763
764    /// Whether the stack guard of a guarded state defers.
765    ///
766    /// A guarded conflict (an optional-postfix take or a precedence loop's
767    /// iterate) resolves to the guarded alternative unless the real parse
768    /// stack's epsilon-pop chase can reach a guard root of the state's guard
769    /// group. The walk stops at the first invoking state whose follow region
770    /// cannot reach its own rule's stop.
771    fn guard_defers(
772        &self,
773        table: &crate::dfa::static_parser_dfa::StaticParserTable,
774        group: u32,
775    ) -> bool {
776        let mut context = self.ctx();
777        while let Some(node) = context {
778            let invoking_state = node.get_invoking_state();
779            if invoking_state < 0 {
780                return false;
781            }
782            let parent = node.get_parent();
783            let parent_ptr = parent.map_or(std::ptr::null(), |p| p as *const _ as *const ());
784            if let Some((_, call_sites)) = self
785                .neutral_call_sites
786                .iter()
787                .find(|&&(ptr, sites)| ptr == parent_ptr && sites.first() == Some(&invoking_state))
788            {
789                if call_sites
790                    .iter()
791                    .any(|&site| table.guard_step(group, site) == Some(true))
792                {
793                    return true;
794                }
795                if call_sites
796                    .iter()
797                    .all(|&site| table.guard_step(group, site) == Some(false))
798                {
799                    return false;
800                }
801                context = parent;
802                continue;
803            }
804            match table.guard_step(group, invoking_state) {
805                Some(true) => return true,
806                Some(false) => return false,
807                None => context = parent,
808            }
809        }
810        false
811    }
812    /// Whether a shared-descent group can start a neutral parse here.
813    ///
814    /// A group cannot start while the re-descent of another group is pending,
815    /// and not while parse listeners are attached without a parse tree: the
816    /// muted listener events of the shared rule are replayed from its subtree.
817    ///
818    /// A group also cannot start in the SLL prediction mode. For an ambiguous
819    /// input, SLL resolves the conflict of the decision to its minimum
820    /// alternative, but the tail dispatch after the shared rule can select
821    /// another member.
822    #[doc(hidden)]
823    pub fn descent_ready(&mut self) -> bool {
824        self.drop_stale_resume();
825        self.resume.is_none()
826            && (self.build_parse_trees || self.parse_listeners.is_empty())
827            && self.get_interpreter().get_prediction_mode() != PredictionMode::SLL
828    }
829
830    /// Ends a re-descent that moved past the start of its shared rule without
831    /// calling it. An error recovery in the prefix can do that; the kept
832    /// subtree is then useless.
833    fn drop_stale_resume(&mut self) {
834        let index = self.input.index();
835        if let Some(resume) = self.resume.take_if(|resume| index > resume.shared_index) {
836            self.input.release(resume.mark);
837        }
838    }
839
840    /// Starts the neutral parse of a shared-descent group: the generated block
841    /// calls the shared rule once, with parse listeners muted, to read the
842    /// token after it.
843    ///
844    /// Skips the `prefix_len` leading tokens of the group (the table walk read
845    /// them) and records the group's member `call_sites` so the shared rule's
846    /// context has a real invoking state and full-context prediction inside it
847    /// sees every member's follow state. Finish with
848    /// [`Self::resume_neutral_parse`] to keep its subtree for the re-descent,
849    /// or [`Self::abort_neutral_parse`] when the shared rule exits with an
850    /// unrecoverable error.
851    #[doc(hidden)]
852    pub fn begin_neutral_parse(
853        &mut self,
854        call_sites: &'static [i32],
855        prefix_len: usize,
856    ) -> NeutralParse {
857        let ctx = self.ctx();
858        let start_index = self.input.index();
859        let mark = self.input.mark();
860        // Skip by consuming, not by index arithmetic: a channel-filtering
861        // stream has off-channel tokens between the prefix tokens.
862        for _ in 0..prefix_len {
863            if self.input.la(1) != TOKEN_EOF {
864                self.input.consume();
865            }
866        }
867        let neutral = NeutralParse {
868            start_index,
869            mark,
870            shared_index: self.input.index(),
871            state: self.state,
872            children: ctx.map_or(0, |ctx| ctx.get_child_count()),
873            matched_eof: self.matched_eof,
874        };
875        self.neutral_call_sites.push((self.ctx, call_sites));
876        self.muted += 1;
877        self.state = call_sites.first().copied().unwrap_or(-1);
878        neutral
879    }
880
881    /// Releases a neutral parse when the shared rule returned an
882    /// unrecoverable error.
883    #[doc(hidden)]
884    pub fn abort_neutral_parse(&mut self, neutral: NeutralParse) {
885        debug_assert!(self.muted > 0, "no neutral parse is active");
886        self.muted -= 1;
887        self.neutral_call_sites.pop();
888        self.input.release(neutral.mark);
889    }
890
891    /// Finishes a neutral parse and keeps the subtree `node` of the shared rule
892    /// `rule` for the re-descent.
893    ///
894    /// Rewinds the stream to the decision start and detaches `node` from the
895    /// current context so the member alternative can re-descend through its
896    /// prefix to its call of the shared rule, where [`Self::resume_take`]
897    /// grafts `node` back and seeks to the tail.
898    #[doc(hidden)]
899    pub fn resume_neutral_parse(
900        &mut self,
901        neutral: NeutralParse,
902        node: &'arena TreeNode<'input, 'arena, Node, TF::Tok>,
903        rule: usize,
904    ) {
905        debug_assert!(self.muted > 0, "no neutral parse is active");
906        self.muted -= 1;
907        self.neutral_call_sites.pop();
908        // The mark stays until the re-descent seeks forward to the tail.
909        self.resume = Some(Resume {
910            node,
911            rule,
912            shared_index: neutral.shared_index,
913            tail_index: self.input.index(),
914            matched_eof: self.matched_eof,
915            mark: neutral.mark,
916        });
917        self.input.seek(neutral.start_index);
918        if let Some(ctx) = unsafe { self.ctx_mut() } {
919            ctx.truncate_children(neutral.children);
920        }
921        self.state = neutral.state;
922        self.matched_eof = neutral.matched_eof;
923    }
924
925    /// Returns the subtree that a neutral parse kept for the shared rule
926    /// `rule`, when the re-descent calls it at the position where the neutral
927    /// parse started it. The subtree is grafted under the current context, the
928    /// muted listener events are replayed, and the stream moves to the token
929    /// after the subtree.
930    ///
931    /// Returns `None` when no re-descent is pending or `rule` is another rule.
932    /// A call of `rule` at another position ends the re-descent: the kept
933    /// subtree does not belong there, so the rule parses normally.
934    #[doc(hidden)]
935    pub fn resume_take(
936        &mut self,
937        rule: usize,
938    ) -> Result<Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>>, ANTLRError> {
939        let Some(resume) = self.resume.take_if(|resume| resume.rule == rule) else {
940            return Ok(None);
941        };
942        if self.input.index() != resume.shared_index {
943            self.input.release(resume.mark);
944            return Ok(None);
945        }
946        let node = resume.node;
947        let state = self.state;
948        let parent = self.ctx();
949        if self.build_parse_trees {
950            self.add_child_to_ctx(node);
951        }
952        // The neutral parse created the context under the decision's context
953        // at the time; the re-descent's context and call site may differ.
954        self.set_current_ctx(Some(node));
955        self.with_mut_ctx(|ctx| {
956            ctx.set_parent(parent);
957            ctx.set_invoking_state(state);
958        });
959        self.set_current_ctx(parent);
960        if self.muted == 0 && !self.parse_listeners.is_empty() {
961            self.replay_listener_events(node)?;
962        }
963        self.matched_eof = resume.matched_eof;
964        self.input.seek(resume.tail_index);
965        self.input.release(resume.mark);
966        Ok(Some(node))
967    }
968
969    /// Fires the parse listener events of the subtree `root` in the order of
970    /// a live parse: enter events in listener order, exit events in reverse
971    /// listener order.
972    ///
973    /// A live parse of a left-recursive rule exits the previous context of
974    /// the operator loop before it enters the new recursion context, and that
975    /// previous context is the first child of the new one. So the replay
976    /// visits such a first child before it enters its parent.
977    fn replay_listener_events(
978        &mut self,
979        root: &'arena TreeNode<'input, 'arena, Node, TF::Tok>,
980    ) -> Result<(), ANTLRError> {
981        struct Frame<'a, T> {
982            node: &'a T,
983            next_child: usize,
984            entered: bool,
985        }
986        // An explicit stack: the subtree can be as deep as the input.
987        let mut stack = vec![Frame {
988            node: root,
989            next_child: 0,
990            entered: false,
991        }];
992        while let Some(Frame {
993            node,
994            mut next_child,
995            mut entered,
996        }) = stack.pop()
997        {
998            if let Some(terminal) = node.as_terminal_node() {
999                for listener in &mut self.parse_listeners {
1000                    listener.visit_terminal(terminal)?;
1001                }
1002                continue;
1003            }
1004            if let Some(error) = node.as_error_node() {
1005                for listener in &mut self.parse_listeners {
1006                    listener.visit_error_node(error)?;
1007                }
1008                continue;
1009            }
1010            if !entered {
1011                let previous = if next_child == 0 {
1012                    self.recursion_predecessor(node)
1013                } else {
1014                    None
1015                };
1016                if let Some(previous) = previous {
1017                    stack.push(Frame {
1018                        node,
1019                        next_child: 1,
1020                        entered: false,
1021                    });
1022                    stack.push(Frame {
1023                        node: previous,
1024                        next_child: 0,
1025                        entered: false,
1026                    });
1027                    continue;
1028                }
1029                for listener in self.parse_listeners.iter_mut() {
1030                    listener.enter_every_rule(node)?;
1031                    node.enter_rule(listener)?;
1032                }
1033                entered = true;
1034            }
1035            if let Some(child) = node.get_child(next_child) {
1036                next_child += 1;
1037                stack.push(Frame {
1038                    node,
1039                    next_child,
1040                    entered,
1041                });
1042                stack.push(Frame {
1043                    node: child,
1044                    next_child: 0,
1045                    entered: false,
1046                });
1047            } else {
1048                for listener in self.parse_listeners.iter_mut().rev() {
1049                    node.exit_rule(listener)?;
1050                    listener.exit_every_rule(node)?;
1051                }
1052            }
1053        }
1054        Ok(())
1055    }
1056
1057    /// The previous context of the left-recursive operator loop that created
1058    /// the recursion context `node`: its first child, a context of the same
1059    /// rule that `push_new_recursion_context` re-parented with the rule start
1060    /// state as its invoking state.
1061    fn recursion_predecessor(
1062        &self,
1063        node: &'arena TreeNode<'input, 'arena, Node, TF::Tok>,
1064    ) -> Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>> {
1065        let first = node.get_child(0)?;
1066        if first.as_terminal_node().is_some() || first.as_error_node().is_some() {
1067            return None;
1068        }
1069        let rule = node.get_rule_index();
1070        let start = self.get_interpreter().atn().rule_to_start_state.get(rule)?;
1071        (first.get_rule_index() == rule && first.get_invoking_state() == start.get_state_number())
1072            .then_some(first)
1073    }
1074
1075    /// If current context is same as the given one by pointer comparison
1076    #[inline]
1077    pub fn ctx_is(&self, other: Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>>) -> bool {
1078        if self.ctx.is_null() && other.is_none() {
1079            true
1080        } else if self.ctx.is_null() || other.is_none() {
1081            false
1082        } else {
1083            std::ptr::eq(self.ctx, other.unwrap() as *const _ as *const ())
1084        }
1085    }
1086
1087    /// Gets a reference to current context.
1088    #[inline]
1089    pub fn ctx(&self) -> Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>> {
1090        if self.ctx.is_null() {
1091            None
1092        } else {
1093            unsafe { Some(&*(self.ctx as *const TreeNode<'input, 'arena, Node, TF::Tok>)) }
1094        }
1095    }
1096
1097    /// Gets a mutable reference to current context.
1098    ///
1099    /// # Safety
1100    /// Follows the same safety rules as dereferencing *mut to &mut
1101    #[inline]
1102    pub unsafe fn ctx_mut(
1103        &mut self,
1104    ) -> Option<&'arena mut TreeNode<'input, 'arena, Node, TF::Tok>> {
1105        if self.ctx.is_null() {
1106            None
1107        } else {
1108            Some(&mut *(self.ctx as *mut TreeNode<'input, 'arena, Node, TF::Tok>))
1109        }
1110    }
1111
1112    #[inline]
1113    fn parent_ctx(&self) -> Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>> {
1114        self.ctx().and_then(|it| it.get_parent())
1115    }
1116
1117    #[inline]
1118    fn set_current_ctx(&mut self, ctx: Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>>) {
1119        if let Some(ctx) = ctx {
1120            self.ctx = ctx as *const TreeNode<'input, 'arena, Node, TF::Tok>
1121                as *mut TreeNode<'input, 'arena, Node, TF::Tok> as *mut ();
1122        } else {
1123            self.ctx = std::ptr::null_mut();
1124        }
1125    }
1126
1127    pub fn take_ctx(&mut self) -> Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>> {
1128        if self.ctx.is_null() {
1129            None
1130        } else {
1131            let ret = unsafe { &*(self.ctx as *const TreeNode<'input, 'arena, Node, TF::Tok>) };
1132            self.ctx = std::ptr::null_mut();
1133            Some(ret)
1134        }
1135    }
1136
1137    #[inline]
1138    fn add_child_to_ctx(&mut self, child: &'arena TreeNode<'input, 'arena, Node, TF::Tok>) {
1139        if !self.ctx.is_null() {
1140            unsafe {
1141                (*(self.ctx as *mut TreeNode<'input, 'arena, Node, TF::Tok>)).add_child(child);
1142            }
1143        }
1144    }
1145
1146    #[inline]
1147    pub fn with_mut_ctx<F, R>(&mut self, f: F) -> R
1148    where
1149        F: FnOnce(&mut TreeNode<'input, 'arena, Node, TF::Tok>) -> R,
1150    {
1151        assert!(!self.ctx.is_null());
1152        unsafe { f(&mut *(self.ctx as *mut TreeNode<'input, 'arena, Node, TF::Tok>)) }
1153    }
1154
1155    #[inline]
1156    pub fn match_token(
1157        &mut self,
1158        ttype: i32,
1159        err_handler: &mut impl ErrorStrategy<'input, 'arena, TF, Self>,
1160    ) -> Result<&'arena TF::Tok, ANTLRError> {
1161        let mut token = self.get_current_token();
1162        if token.get_token_type() == ttype {
1163            if ttype == TOKEN_EOF {
1164                self.matched_eof = true;
1165            }
1166
1167            err_handler.report_match(self);
1168            self.consume(err_handler)?;
1169        } else {
1170            token = err_handler.recover_inline(self)?;
1171            if self.build_parse_trees && token.get_token_index() == -1 {
1172                self.add_child_to_ctx(self.create_error_node(token)?);
1173            }
1174        }
1175        Ok(token)
1176    }
1177
1178    #[inline]
1179    pub fn match_wildcard(
1180        &mut self,
1181        err_handler: &mut impl ErrorStrategy<'input, 'arena, TF, Self>,
1182    ) -> Result<&'arena TF::Tok, ANTLRError> {
1183        let mut token = self.get_current_token();
1184        if token.get_token_type() > 0 {
1185            err_handler.report_match(self);
1186            self.consume(err_handler)?;
1187        } else {
1188            token = err_handler.recover_inline(self)?;
1189            if self.build_parse_trees && token.get_token_index() == -1 {
1190                self.add_child_to_ctx(self.create_error_node(token)?);
1191            }
1192        }
1193        Ok(token)
1194    }
1195
1196    /// Adds parse listener for this parser
1197    /// returns `listener_id` that can be used later to get listener back
1198    ///
1199    /// ### Example for listener usage:
1200    /// todo
1201    pub fn add_dyn_parse_listener(&mut self, listener: Box<Node::Listener>) {
1202        self.parse_listeners.push(listener);
1203    }
1204
1205    /// Removes parse listener with corresponding `listener_id`, casts it back to user type and returns it to the caller.
1206    /// `listener_id` is returned when listener is added via `add_parse_listener`
1207    pub fn remove_parse_listener<L>(&mut self, listener_id: ListenerId<L>) -> Box<L>
1208    where
1209        L: ParseTreeListener<'arena, Node, TF::Tok>,
1210    {
1211        let index = self
1212            .parse_listeners
1213            .iter()
1214            .position(|it| ListenerId::new(it).actual_id == listener_id.actual_id)
1215            .expect("listener not found");
1216        unsafe { listener_id.into_listener(self.parse_listeners.remove(index)) }
1217    }
1218
1219    /// Removes all added parse listeners without returning them
1220    pub fn remove_parse_listeners(&mut self) {
1221        self.parse_listeners.clear()
1222    }
1223
1224    pub fn trigger_enter_rule_event(&mut self) -> Result<(), ANTLRError> {
1225        if self.muted > 0 {
1226            return Ok(());
1227        }
1228        let ctx = self.ctx().unwrap();
1229        for listener in self.parse_listeners.iter_mut() {
1230            listener.enter_every_rule(ctx)?;
1231            ctx.enter_rule(listener)?;
1232        }
1233        Ok(())
1234    }
1235
1236    pub fn trigger_exit_rule_event(&mut self) -> Result<(), ANTLRError> {
1237        if self.muted > 0 {
1238            return Ok(());
1239        }
1240        let ctx = self.ctx().unwrap();
1241        for listener in self.parse_listeners.iter_mut().rev() {
1242            ctx.exit_rule(listener)?;
1243            listener.exit_every_rule(ctx)?;
1244        }
1245        Ok(())
1246    }
1247
1248    //    fn get_atn_with_bypass_alts(&self) { unimplemented!() }
1249    //
1250    //    fn compile_parse_tree_pattern(&self, pattern, patternRuleIndex: Lexer, lexer: Lexer) { unimplemented!() }
1251
1252    #[inline]
1253    pub fn enter_rule(
1254        &mut self,
1255        localctx: &'arena mut TreeNode<'input, 'arena, Node, TF::Tok>,
1256        state: i32,
1257        _rule_index: usize,
1258    ) -> Result<(), ANTLRError> {
1259        let child = localctx;
1260        if self.build_parse_trees {
1261            self.set_current_ctx(child.get_parent());
1262            self.add_child_to_ctx(child);
1263        }
1264
1265        self.set_state(state);
1266        self.set_current_ctx(Some(child));
1267        let start = self.input.lt(1);
1268        self.with_mut_ctx(|ctx| {
1269            ctx.set_start(start);
1270        });
1271
1272        if !self.parse_listeners.is_empty() {
1273            self.trigger_enter_rule_event()?;
1274        }
1275
1276        #[cfg(feature = "recursion-limit")]
1277        {
1278            self.current_recursion_depth += 1;
1279            if self.current_recursion_depth > self.recursion_limit {
1280                return Err(ANTLRError::recursion_limit_exceeded(self.recursion_limit));
1281            }
1282        }
1283        Ok(())
1284    }
1285
1286    #[inline]
1287    pub fn exit_rule(
1288        &mut self,
1289    ) -> Result<&'arena TreeNode<'input, 'arena, Node, TF::Tok>, ANTLRError> {
1290        assert!(self.ctx().is_some());
1291
1292        #[cfg(feature = "recursion-limit")]
1293        {
1294            self.current_recursion_depth -= 1;
1295        }
1296        if self.matched_eof {
1297            // if we have matched EOF, it cannot consume past EOF so we use LT(1) here
1298            let stop = self.input.lt(1);
1299            self.with_mut_ctx(|ctx| {
1300                ctx.set_stop(stop);
1301            });
1302        } else {
1303            // stop node is what we just matched
1304            let stop = self.input.lt(-1);
1305            self.with_mut_ctx(|ctx| {
1306                ctx.set_stop(stop);
1307            });
1308        }
1309        if !self.parse_listeners.is_empty() {
1310            self.trigger_exit_rule_event()?;
1311        }
1312
1313        self.set_state(self.ctx().unwrap().get_invoking_state());
1314        let child = self.ctx().unwrap();
1315        self.set_current_ctx(child.get_parent());
1316
1317        Ok(child)
1318    }
1319
1320    pub fn enter_recursion_rule(
1321        &mut self,
1322        localctx: &'arena mut TreeNode<'input, 'arena, Node, TF::Tok>,
1323        state: i32,
1324        _rule_index: usize,
1325        precedence: i32,
1326    ) -> Result<(), ANTLRError> {
1327        self.set_state(state);
1328        self.precedence_stack.push(precedence);
1329        self.set_current_ctx(Some(localctx));
1330        let start = self.input.lt(1);
1331        self.with_mut_ctx(|ctx| {
1332            ctx.set_start(start);
1333        });
1334        if !self.parse_listeners.is_empty() {
1335            self.trigger_enter_rule_event()?;
1336        }
1337        //println!("{}",self.input.lt(1).map(Token::to_owned).unwrap());
1338
1339        #[cfg(feature = "recursion-limit")]
1340        {
1341            self.current_recursion_depth += 1;
1342            if self.current_recursion_depth > self.recursion_limit {
1343                return Err(ANTLRError::recursion_limit_exceeded(self.recursion_limit));
1344            }
1345        }
1346        Ok(())
1347    }
1348
1349    pub fn push_new_recursion_context(
1350        &mut self,
1351        localctx: &'arena mut TreeNode<'input, 'arena, Node, TF::Tok>,
1352        state: i32,
1353        _rule_index: usize,
1354    ) -> Result<&'arena TreeNode<'input, 'arena, Node, TF::Tok>, ANTLRError> {
1355        let stop = self.input.lt(-1);
1356        self.with_mut_ctx(|ctx| {
1357            ctx.set_parent(Some(localctx));
1358            ctx.set_invoking_state(state);
1359            ctx.set_stop(stop);
1360        });
1361
1362        let prev = self.take_ctx().unwrap();
1363        self.set_current_ctx(Some(localctx));
1364        let start = prev.get_start_token();
1365        self.with_mut_ctx(|ctx| {
1366            ctx.set_start(start);
1367        });
1368        if self.build_parse_trees {
1369            self.add_child_to_ctx(prev);
1370        }
1371        if !self.parse_listeners.is_empty() {
1372            self.trigger_enter_rule_event()?;
1373        }
1374        Ok(prev)
1375    }
1376
1377    pub fn unroll_recursion_context(
1378        &mut self,
1379        parent_ctx: Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>>,
1380    ) -> Result<&'arena TreeNode<'input, 'arena, Node, TF::Tok>, ANTLRError> {
1381        assert!(self.ctx().is_some());
1382
1383        #[cfg(feature = "recursion-limit")]
1384        {
1385            self.current_recursion_depth -= 1;
1386        }
1387        self.precedence_stack.pop();
1388        let stop = self.input.lt(-1);
1389        self.with_mut_ctx(|ctx| {
1390            ctx.set_stop(stop);
1391        });
1392        let retctx = self.ctx;
1393
1394        // unroll so _ctx is as it was before call to recursive method
1395        if !self.parse_listeners.is_empty() {
1396            while !self.ctx_is(parent_ctx) {
1397                self.trigger_exit_rule_event()?;
1398                self.set_current_ctx(self.parent_ctx());
1399            }
1400        } else {
1401            self.set_current_ctx(parent_ctx);
1402        }
1403
1404        // hook into tree
1405        unsafe {
1406            (*(retctx as *mut TreeNode<'input, 'arena, Node, TF::Tok>)).set_parent(parent_ctx);
1407        }
1408
1409        //        println!("{:?}",self.ctx.as_ref().map(|it|it.to_string_tree(self)));
1410        if self.build_parse_trees && parent_ctx.is_some() {
1411            self.add_child_to_ctx(unsafe {
1412                &*(retctx as *const TreeNode<'input, 'arena, Node, TF::Tok>)
1413            });
1414        }
1415        Ok(unsafe { &*(retctx as *const TreeNode<'input, 'arena, Node, TF::Tok>) })
1416    }
1417
1418    #[allow(clippy::mut_from_ref)] // &mut is from the arena allocation
1419    fn create_token_node(
1420        &self,
1421        token: &'arena TF::Tok,
1422    ) -> Result<&'arena mut TreeNode<'input, 'arena, Node, TF::Tok>, ANTLRError> {
1423        let ptr = TreeNode::create_token_node(self.arena, token);
1424        if ptr.is_null() {
1425            #[cfg(feature = "arena-allocation-limit")]
1426            {
1427                Err(ANTLRError::arena_allocation_limit_exceeded(
1428                    self.arena.allocation_limit_bytes(),
1429                    self.arena.total_allocated_bytes(),
1430                ))
1431            }
1432            #[cfg(not(feature = "arena-allocation-limit"))]
1433            {
1434                std::alloc::handle_alloc_error(std::alloc::Layout::new::<
1435                    TreeNode<'input, 'arena, Node, TF::Tok>,
1436                >())
1437            }
1438        } else {
1439            Ok(unsafe { &mut *ptr })
1440        }
1441    }
1442
1443    #[allow(clippy::mut_from_ref)] // &mut is from the arena allocation
1444    fn create_error_node(
1445        &self,
1446        token: &'arena TF::Tok,
1447    ) -> Result<&'arena mut TreeNode<'input, 'arena, Node, TF::Tok>, ANTLRError> {
1448        let ptr = TreeNode::create_error_node(self.arena, token);
1449        if ptr.is_null() {
1450            Err(ANTLRError::dfa_cache_limit_exceeded(0, 0, 0))
1451        } else {
1452            Ok(unsafe { &mut *ptr })
1453        }
1454    }
1455
1456    /// Text representation of generated DFA for debugging purposes
1457    pub fn dump_dfa(&self) {
1458        let mut seen_one = false;
1459        for i in 0..self.get_atn().decision_to_state.len() {
1460            let dfa = self
1461                .get_interpreter()
1462                .decision_to_dfa(i)
1463                .expect("dfa should exist for each decision");
1464            if !dfa.is_empty() {
1465                if seen_one {
1466                    println!()
1467                }
1468                println!("Decision {}:", dfa.decision);
1469                print!("{}", dfa.to_string(self.get_vocabulary()));
1470                seen_one = true;
1471            }
1472        }
1473    }
1474}
1475
1476impl<'input, 'arena, Ext, Node, Input, TF> Drop for BaseParser<'input, 'arena, Ext, Node, Input, TF>
1477where
1478    'input: 'arena,
1479    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
1480    TF: TokenFactory<'input, 'arena> + 'arena,
1481    Input: TokenStream<'input, 'arena, TF>,
1482    Node: NodeKindType<'arena, TF::Tok>,
1483{
1484    fn drop(&mut self) {
1485        if self.global_cache_threshold == 0 {
1486            return;
1487        }
1488
1489        let Some(interp) = self.interp.take() else {
1490            return;
1491        };
1492        let cache_bytes = interp.total_allocated_bytes();
1493        drop(interp);
1494
1495        if cache_bytes > self.global_cache_threshold {
1496            self.atn_manager.reset_all();
1497        }
1498    }
1499}
1500
1501/// Allows to safely cast listener back to user type
1502#[derive(Debug)]
1503pub struct ListenerId<T: ?Sized> {
1504    pub(crate) actual_id: usize,
1505    phantom: PhantomData<fn() -> T>,
1506}
1507
1508impl<T: ?Sized> ListenerId<T> {
1509    #[allow(clippy::borrowed_box)]
1510    pub fn new(listener: &Box<T>) -> ListenerId<T> {
1511        ListenerId {
1512            actual_id: listener.as_ref() as *const T as *const () as usize,
1513            phantom: Default::default(),
1514        }
1515    }
1516}
1517
1518impl<T> ListenerId<T> {
1519    unsafe fn into_listener<U: ?Sized>(self, boxed: Box<U>) -> Box<T> {
1520        Box::from_raw(Box::into_raw(boxed) as *mut T)
1521    }
1522}