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::recognizer::{Actions, Recognizer};
19use crate::rule_context::{states_stack, RuleContext as _};
20use crate::token::{Token, TOKEN_EOF};
21use crate::token_factory::TokenFactory;
22use crate::token_stream::TokenStream;
23use crate::tree::{NodeKindType, ParseTreeListener, Tree as _, TreeNode};
24use crate::utils::cell_update;
25use crate::vocabulary::Vocabulary;
26
27#[cfg(feature = "recursion-limit")]
28const DEFAULT_RECURSION_LIMIT: u32 = 2000;
29
30/// parser functionality required for `ParserATNSimulator` to work
31#[allow(missing_docs)]
32pub trait Parser<'input, 'arena, TF>: Recognizer<'input, 'arena, TF::Tok>
33where
34    'input: 'arena,
35    TF: TokenFactory<'input, 'arena> + 'arena,
36{
37    fn get_arena(&self) -> &'arena Arena;
38
39    fn get_interpreter(&self) -> &'arena ParserATNSimulator<'arena>;
40
41    fn get_token_factory(&self) -> &TF;
42
43    fn get_current_context(&self) -> &'arena TreeNode<'input, 'arena, Self::Node, TF::Tok>;
44
45    /// Current rule context, or `None` when the parser is not inside a rule.
46    ///
47    /// Java has no equivalent accessor: `ParserATNSimulator.adaptivePredict`
48    /// takes the outer context as an explicit parameter, and the tool's
49    /// interpreter tests pass `ParserRuleContext.EMPTY`. This accessor lets
50    /// [crate::parser_atn_simulator::ParserATNSimulator::adaptive_predict]
51    /// treat a missing context as Java's `ParserRuleContext.EMPTY`.
52    fn get_current_context_opt(
53        &self,
54    ) -> Option<&'arena TreeNode<'input, 'arena, Self::Node, TF::Tok>> {
55        Some(self.get_current_context())
56    }
57
58    fn consume(
59        &mut self,
60        err_handler: &mut impl ErrorStrategy<'input, 'arena, TF, Self>,
61    ) -> Result<(), ANTLRError>
62    where
63        Self: Sized;
64
65    fn precpred(
66        &self,
67        localctx: Option<&TreeNode<'input, 'arena, Self::Node, TF::Tok>>,
68        precedence: i32,
69    ) -> bool;
70
71    fn get_input_stream_mut(&mut self) -> &mut dyn TokenStream<'input, 'arena, TF>;
72    fn get_input_stream(&self) -> &dyn TokenStream<'input, 'arena, TF>;
73    fn get_current_token(&self) -> &'arena TF::Tok;
74    fn get_expected_tokens<'a>(&'a self) -> Cow<'a, IntervalSet>;
75
76    fn add_error_listener(
77        &mut self,
78        listener: Box<dyn ErrorListener<'input, 'arena, Self, TF::Tok> + 'input>,
79    ) where
80        Self: Sized;
81
82    fn remove_error_listeners(&mut self);
83
84    fn notify_error_listeners(
85        &self,
86        msg: String,
87        offending_token: Option<isize>,
88        err: Option<&ANTLRError>,
89    );
90    fn get_error_lister_dispatch<'a>(
91        &'a self,
92    ) -> Box<dyn ErrorListener<'input, 'arena, Self, TF::Tok> + 'a>
93    where
94        Self: Sized;
95
96    fn is_expected_token(&self, symbol: i32) -> bool;
97    fn get_precedence(&self) -> i32;
98
99    fn get_state(&self) -> i32;
100    fn set_state(&mut self, v: i32);
101    fn get_rule_invocation_stack(&self) -> Vec<String>;
102
103    #[cfg(feature = "recursion-limit")]
104    fn get_recursion_limit(&self) -> u32;
105    #[cfg(feature = "recursion-limit")]
106    fn set_recursion_limit(&mut self, v: u32);
107}
108
109/// Abstract base parser implementation
110///
111/// Only meant to be instantiated by generated parsers
112pub struct BaseParser<'input, 'arena, Ext, Node, Input, TF>
113where
114    'input: 'arena,
115    // Grammar-specific implementation of parser functionality required for
116    // `ParserATNSimulator` to work
117    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
118    // Token factory used by the input stream
119    TF: TokenFactory<'input, 'arena> + 'arena,
120    // Token stream (lexer)
121    Input: TokenStream<'input, 'arena, TF>,
122    Node: NodeKindType<'arena, TF::Tok>,
123{
124    interp: Option<ParserATNSimulator<'arena>>,
125    atn_manager: &'static ParserATNSimulatorManager,
126    global_cache_threshold: usize,
127
128    /// Rule context parser is currently processing
129    ctx: *mut (),
130
131    /// Track the {@link ParserRuleContext} objects during the parse and hook
132    /// them up using the {@link ParserRuleContext#children} list so that it
133    /// forms a parse tree. The {@link ParserRuleContext} returned from the start
134    /// rule represents the root of the parse tree.
135    ///
136    /// <p>Note that if we are not building parse trees, rule contexts only point
137    /// upwards. When a rule exits, it returns the context bute that gets garbage
138    /// collected if nobody holds a reference. It points upwards but nobody
139    /// points at it. </p>
140    ///
141    /// <p>When we build parse trees, we are adding all of these contexts to
142    /// {@link ParserRuleContext#children} list. Contexts are then not candidates
143    /// for garbage collection.</p>
144    ///
145    /// Returns {@code true} if a complete parse tree will be constructed while
146    /// parsing, otherwise {@code false}
147    pub build_parse_trees: bool,
148
149    /// true if parser reached EOF
150    pub matched_eof: bool,
151
152    state: i32,
153    /// Token stream that is currently used by this parser
154    pub input: Input,
155    precedence_stack: Vec<i32>,
156    #[cfg(feature = "recursion-limit")]
157    pub recursion_limit: u32,
158    #[cfg(feature = "recursion-limit")]
159    current_recursion_depth: u32,
160
161    parse_listeners: Vec<Box<Node::Listener>>,
162    _syntax_errors: Cell<i32>,
163    error_listeners: Vec<ErrorListenerDelegate<'input, 'arena, Self, TF::Tok>>,
164
165    pub arena: &'arena Arena,
166    ext: Ext,
167    pd: PhantomData<(
168        &'input (),
169        &'arena TF,
170        //        &'arena TreeNode<'input, 'arena, Node, TF::Tok>,
171    )>,
172}
173
174impl<'input, 'arena, Ext, Node, Input, TF> Debug
175    for BaseParser<'input, 'arena, Ext, Node, Input, TF>
176where
177    'input: 'arena,
178    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
179    TF: TokenFactory<'input, 'arena> + 'arena,
180    Input: TokenStream<'input, 'arena, TF>,
181    Node: NodeKindType<'arena, TF::Tok>,
182{
183    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
184        f.debug_struct("BaseParser")
185            .field("state", &self.state)
186            .field("matched_eof", &self.matched_eof)
187            .field("build_parse_trees", &self.build_parse_trees)
188            .finish_non_exhaustive()
189    }
190}
191
192impl<'input, 'arena, Ext, Node, Input, TF> Deref
193    for BaseParser<'input, 'arena, Ext, Node, Input, TF>
194where
195    'input: 'arena,
196    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
197    TF: TokenFactory<'input, 'arena> + 'arena,
198    Input: TokenStream<'input, 'arena, TF>,
199    Node: NodeKindType<'arena, TF::Tok>,
200{
201    type Target = Ext;
202
203    fn deref(&self) -> &Self::Target {
204        &self.ext
205    }
206}
207
208impl<'input, 'arena, Ext, Node, Input, TF> DerefMut
209    for BaseParser<'input, 'arena, Ext, Node, Input, TF>
210where
211    'input: 'arena,
212    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
213    TF: TokenFactory<'input, 'arena> + 'arena,
214    Input: TokenStream<'input, 'arena, TF>,
215    Node: NodeKindType<'arena, TF::Tok>,
216{
217    fn deref_mut(&mut self) -> &mut Self::Target {
218        &mut self.ext
219    }
220}
221
222pub trait ParserRecog<'input, 'arena, R, Tok>: Actions<'input, 'arena, R, Tok>
223where
224    'input: 'arena,
225    R: Recognizer<'input, 'arena, Tok>,
226    Tok: Token + 'input,
227{
228    fn get_atn_simulator_man(&self) -> &'static ParserATNSimulatorManager;
229}
230
231impl<'input, 'arena, Ext, Node, Input, TF> Recognizer<'input, 'arena, TF::Tok>
232    for BaseParser<'input, 'arena, Ext, Node, Input, TF>
233where
234    'input: 'arena,
235    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
236    TF: TokenFactory<'input, 'arena> + 'arena,
237    Input: TokenStream<'input, 'arena, TF>,
238    Node: NodeKindType<'arena, TF::Tok>,
239{
240    type Node = Node;
241
242    fn sempred(
243        &mut self,
244        localctx: Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>>,
245        rule_index: i32,
246        action_index: i32,
247    ) -> bool {
248        Ext::sempred(localctx, rule_index, action_index, self)
249    }
250
251    fn get_rule_names(&self) -> &[&str] {
252        self.ext.get_rule_names()
253    }
254
255    fn get_vocabulary(&self) -> &dyn Vocabulary {
256        self.ext.get_vocabulary()
257    }
258
259    fn get_grammar_file_name(&self) -> &str {
260        self.ext.get_grammar_file_name()
261    }
262
263    fn get_atn(&self) -> &ATN {
264        self.interp.as_ref().unwrap().atn()
265    }
266}
267
268impl<'input, 'arena, Ext, Node, Input, TF> Parser<'input, 'arena, TF>
269    for BaseParser<'input, 'arena, Ext, Node, Input, TF>
270where
271    'input: 'arena,
272    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
273    TF: TokenFactory<'input, 'arena> + 'arena,
274    Input: TokenStream<'input, 'arena, TF>,
275    Node: NodeKindType<'arena, TF::Tok>,
276{
277    fn get_arena(&self) -> &'arena Arena {
278        self.arena
279    }
280
281    #[inline(always)]
282    fn get_interpreter(&self) -> &'arena ParserATNSimulator<'arena> {
283        unsafe {
284            std::mem::transmute::<&ParserATNSimulator<'arena>, &'arena ParserATNSimulator<'arena>>(
285                self.interp.as_ref().unwrap(),
286            )
287        }
288    }
289
290    fn get_token_factory(&self) -> &TF {
291        self.input.get_token_source().get_token_factory()
292    }
293
294    #[inline(always)]
295    fn get_current_context(&self) -> &'arena TreeNode<'input, 'arena, Node, TF::Tok> {
296        self.ctx().unwrap()
297    }
298
299    #[inline(always)]
300    fn get_current_context_opt(&self) -> Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>> {
301        self.ctx()
302    }
303
304    fn consume(
305        &mut self,
306        err_handler: &mut impl ErrorStrategy<'input, 'arena, TF, Self>,
307    ) -> Result<(), ANTLRError> {
308        let o = self.get_current_token();
309        if o.borrow().get_token_type() != TOKEN_EOF {
310            self.input.consume();
311        }
312        if self.build_parse_trees || !self.parse_listeners.is_empty() {
313            if err_handler.in_error_recovery_mode(self) {
314                // todo report ructc inference issue
315                let node = self.create_error_node(o)?;
316                self.add_child_to_ctx(node);
317                for listener in &mut self.parse_listeners {
318                    listener.visit_error_node(
319                        node.as_error_node()
320                            .expect("node was created as error node"),
321                    )?
322                }
323            } else {
324                let node = self.create_token_node(o)?;
325                self.add_child_to_ctx(node);
326                for listener in &mut self.parse_listeners {
327                    listener.visit_terminal(
328                        node.as_terminal_node()
329                            .expect("node was created as terminal node"),
330                    )?
331                }
332            }
333        }
334        Ok(())
335    }
336
337    fn precpred(
338        &self,
339        _localctx: Option<&TreeNode<'input, 'arena, Node, TF::Tok>>,
340        precedence: i32,
341    ) -> bool {
342        //        localctx.map(|it|println!("check at{}",it.to_string_tree(self)));
343        //        println!("{}",self.get_precedence());
344        precedence >= self.get_precedence()
345    }
346
347    fn get_input_stream_mut(&mut self) -> &mut dyn TokenStream<'input, 'arena, TF> {
348        &mut self.input //.as_mut()
349    }
350
351    fn get_input_stream(&self) -> &dyn TokenStream<'input, 'arena, TF> {
352        &self.input
353    }
354
355    #[inline]
356    fn get_current_token(&self) -> &'arena TF::Tok {
357        self.input.get(self.input.index())
358    }
359
360    fn get_expected_tokens<'a>(&'a self) -> Cow<'a, IntervalSet> {
361        let states_stack = states_stack(self.ctx().unwrap());
362        self.interp
363            .as_ref()
364            .unwrap()
365            .atn()
366            .get_expected_tokens::<TF::Tok>(self.state, states_stack)
367    }
368
369    fn add_error_listener(
370        &mut self,
371        listener: Box<dyn ErrorListener<'input, 'arena, Self, TF::Tok> + 'input>,
372    ) {
373        self.error_listeners
374            .push(ErrorListenerDelegate::new(listener))
375    }
376
377    fn remove_error_listeners(&mut self) {
378        self.error_listeners.clear();
379    }
380
381    fn notify_error_listeners(
382        &self,
383        msg: String,
384        offending_token: Option<isize>,
385        err: Option<&ANTLRError>,
386    ) {
387        cell_update(&self._syntax_errors, |it| it + 1);
388        let offending_token: Option<&_> = match offending_token {
389            None => Some(self.get_current_token().borrow()),
390            Some(x) => Some(self.input.get(x).borrow()),
391        };
392        let line = offending_token.map_or(0, |x| x.get_line());
393        let column = offending_token.map_or(-1, |x| x.get_char_position_in_line());
394
395        for listener in self.error_listeners.iter() {
396            listener.syntax_error(
397                self,
398                offending_token.map(|x| x as _),
399                line,
400                column,
401                &msg,
402                err,
403            )
404        }
405    }
406
407    fn get_error_lister_dispatch<'a>(
408        &'a self,
409    ) -> Box<dyn ErrorListener<'input, 'arena, Self, TF::Tok> + 'a> {
410        Box::new(ProxyErrorListener {
411            delegates: self.error_listeners.borrow(),
412        })
413    }
414
415    fn is_expected_token(&self, _symbol: i32) -> bool {
416        unimplemented!()
417    }
418
419    fn get_precedence(&self) -> i32 {
420        *self.precedence_stack.last().unwrap_or(&-1)
421    }
422
423    #[inline(always)]
424    fn get_state(&self) -> i32 {
425        self.state
426    }
427
428    #[inline(always)]
429    fn set_state(&mut self, v: i32) {
430        self.state = v;
431    }
432
433    fn get_rule_invocation_stack(&self) -> Vec<String> {
434        let mut vec = Vec::new();
435        let rule_names = self.get_rule_names();
436        let mut ctx = self.get_current_context();
437        loop {
438            let rule_index = ctx.get_rule_index();
439            vec.push(rule_names.get(rule_index).unwrap_or(&"n/a").to_string());
440            ctx = if let Some(parent) = ctx.get_parent() {
441                parent
442            } else {
443                break;
444            }
445        }
446        vec
447    }
448
449    #[cfg(feature = "recursion-limit")]
450    fn get_recursion_limit(&self) -> u32 {
451        self.recursion_limit
452    }
453
454    #[cfg(feature = "recursion-limit")]
455    fn set_recursion_limit(&mut self, v: u32) {
456        self.recursion_limit = v;
457    }
458
459    //    fn get_rule_invocation_stack(&self, c: _) -> Vec<String> {
460    //        unimplemented!()
461    //    }
462}
463
464#[allow(missing_docs)] // todo docs
465impl<'input, 'arena, Ext, Node, Input, TF> BaseParser<'input, 'arena, Ext, Node, Input, TF>
466where
467    'input: 'arena,
468    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
469    TF: TokenFactory<'input, 'arena> + 'arena,
470    Input: TokenStream<'input, 'arena, TF>,
471    Node: NodeKindType<'arena, TF::Tok>,
472{
473    /// Replaces the input token stream, mirroring Java's
474    /// `Parser.setTokenStream`.
475    ///
476    /// Unlike Java, this does not seek the old stream back to 0 or reset
477    /// parser state; it is intended for swapping in a fresh stream before any
478    /// rule invocation, as the ANTLR tool's interpreter tests do when they
479    /// reuse one parser (and its decision DFAs) across several inputs.
480    pub fn set_input_stream(&mut self, input: Input) {
481        self.input = input;
482    }
483
484    pub fn new_base_parser(arena: &'arena Arena, input: Input, ext: Ext) -> Self {
485        let atn_manager = ext.get_atn_simulator_man();
486        let interp = ParserATNSimulator::new(atn_manager.get_simulator(arena));
487        Self {
488            atn_manager,
489            interp: Some(interp),
490            global_cache_threshold: 0,
491            ctx: std::ptr::null_mut(),
492            build_parse_trees: true,
493            matched_eof: false,
494            state: -1,
495            input,
496            precedence_stack: vec![0],
497            #[cfg(feature = "recursion-limit")]
498            recursion_limit: DEFAULT_RECURSION_LIMIT,
499            #[cfg(feature = "recursion-limit")]
500            current_recursion_depth: 0,
501            parse_listeners: vec![],
502            _syntax_errors: Cell::new(0),
503            error_listeners: vec![ErrorListenerDelegate::new(
504                Box::new(ConsoleErrorListener {})
505                    as Box<dyn ErrorListener<'input, 'arena, Self, TF::Tok> + 'input>,
506            )],
507            arena,
508            ext,
509            pd: PhantomData,
510        }
511    }
512
513    pub fn set_global_cache_threshold(&mut self, threshold: usize) {
514        self.global_cache_threshold = threshold;
515    }
516
517    pub fn get_interpreter_mut(&mut self) -> &mut ParserATNSimulator<'arena> {
518        self.interp.as_mut().unwrap()
519    }
520
521    /// If current context is same as the given one by pointer comparison
522    #[inline]
523    pub fn ctx_is(&self, other: Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>>) -> bool {
524        if self.ctx.is_null() && other.is_none() {
525            true
526        } else if self.ctx.is_null() || other.is_none() {
527            false
528        } else {
529            std::ptr::eq(self.ctx, other.unwrap() as *const _ as *const ())
530        }
531    }
532
533    /// Gets a reference to current context.
534    #[inline]
535    pub fn ctx(&self) -> Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>> {
536        if self.ctx.is_null() {
537            None
538        } else {
539            unsafe { Some(&*(self.ctx as *const TreeNode<'input, 'arena, Node, TF::Tok>)) }
540        }
541    }
542
543    /// Gets a mutable reference to current context.
544    ///
545    /// # Safety
546    /// Follows the same safety rules as dereferencing *mut to &mut
547    #[inline]
548    pub unsafe fn ctx_mut(
549        &mut self,
550    ) -> Option<&'arena mut TreeNode<'input, 'arena, Node, TF::Tok>> {
551        if self.ctx.is_null() {
552            None
553        } else {
554            Some(&mut *(self.ctx as *mut TreeNode<'input, 'arena, Node, TF::Tok>))
555        }
556    }
557
558    #[inline]
559    fn parent_ctx(&self) -> Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>> {
560        self.ctx().and_then(|it| it.get_parent())
561    }
562
563    #[inline]
564    fn set_current_ctx(&mut self, ctx: Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>>) {
565        if let Some(ctx) = ctx {
566            self.ctx = ctx as *const TreeNode<'input, 'arena, Node, TF::Tok>
567                as *mut TreeNode<'input, 'arena, Node, TF::Tok> as *mut ();
568        } else {
569            self.ctx = std::ptr::null_mut();
570        }
571    }
572
573    pub fn take_ctx(&mut self) -> Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>> {
574        if self.ctx.is_null() {
575            None
576        } else {
577            let ret = unsafe { &*(self.ctx as *const TreeNode<'input, 'arena, Node, TF::Tok>) };
578            self.ctx = std::ptr::null_mut();
579            Some(ret)
580        }
581    }
582
583    #[inline]
584    fn add_child_to_ctx(&mut self, child: &'arena TreeNode<'input, 'arena, Node, TF::Tok>) {
585        if !self.ctx.is_null() {
586            unsafe {
587                (*(self.ctx as *mut TreeNode<'input, 'arena, Node, TF::Tok>)).add_child(child);
588            }
589        }
590    }
591
592    #[inline]
593    pub fn with_mut_ctx<F, R>(&mut self, f: F) -> R
594    where
595        F: FnOnce(&mut TreeNode<'input, 'arena, Node, TF::Tok>) -> R,
596    {
597        assert!(!self.ctx.is_null());
598        unsafe { f(&mut *(self.ctx as *mut TreeNode<'input, 'arena, Node, TF::Tok>)) }
599    }
600
601    #[inline]
602    pub fn match_token(
603        &mut self,
604        ttype: i32,
605        err_handler: &mut impl ErrorStrategy<'input, 'arena, TF, Self>,
606    ) -> Result<&'arena TF::Tok, ANTLRError> {
607        let mut token = self.get_current_token();
608        if token.get_token_type() == ttype {
609            if ttype == TOKEN_EOF {
610                self.matched_eof = true;
611            }
612
613            err_handler.report_match(self);
614            self.consume(err_handler)?;
615        } else {
616            token = err_handler.recover_inline(self)?;
617            if self.build_parse_trees && token.get_token_index() == -1 {
618                self.add_child_to_ctx(self.create_error_node(token)?);
619            }
620        }
621        Ok(token)
622    }
623
624    #[inline]
625    pub fn match_wildcard(
626        &mut self,
627        err_handler: &mut impl ErrorStrategy<'input, 'arena, TF, Self>,
628    ) -> Result<&'arena TF::Tok, ANTLRError> {
629        let mut token = self.get_current_token();
630        if token.get_token_type() > 0 {
631            err_handler.report_match(self);
632            self.consume(err_handler)?;
633        } else {
634            token = err_handler.recover_inline(self)?;
635            if self.build_parse_trees && token.get_token_index() == -1 {
636                self.add_child_to_ctx(self.create_error_node(token)?);
637            }
638        }
639        Ok(token)
640    }
641
642    /// Adds parse listener for this parser
643    /// returns `listener_id` that can be used later to get listener back
644    ///
645    /// ### Example for listener usage:
646    /// todo
647    pub fn add_dyn_parse_listener(&mut self, listener: Box<Node::Listener>) {
648        self.parse_listeners.push(listener);
649    }
650
651    /// Removes parse listener with corresponding `listener_id`, casts it back to user type and returns it to the caller.
652    /// `listener_id` is returned when listener is added via `add_parse_listener`
653    pub fn remove_parse_listener<L>(&mut self, listener_id: ListenerId<L>) -> Box<L>
654    where
655        L: ParseTreeListener<'arena, Node, TF::Tok>,
656    {
657        let index = self
658            .parse_listeners
659            .iter()
660            .position(|it| ListenerId::new(it).actual_id == listener_id.actual_id)
661            .expect("listener not found");
662        unsafe { listener_id.into_listener(self.parse_listeners.remove(index)) }
663    }
664
665    /// Removes all added parse listeners without returning them
666    pub fn remove_parse_listeners(&mut self) {
667        self.parse_listeners.clear()
668    }
669
670    pub fn trigger_enter_rule_event(&mut self) -> Result<(), ANTLRError> {
671        let ctx = self.ctx().unwrap();
672        for listener in self.parse_listeners.iter_mut() {
673            listener.enter_every_rule(ctx)?;
674            ctx.enter_rule(listener)?;
675        }
676        Ok(())
677    }
678
679    pub fn trigger_exit_rule_event(&mut self) -> Result<(), ANTLRError> {
680        let ctx = self.ctx().unwrap();
681        for listener in self.parse_listeners.iter_mut().rev() {
682            ctx.exit_rule(listener)?;
683            listener.exit_every_rule(ctx)?;
684        }
685        Ok(())
686    }
687
688    //    fn get_atn_with_bypass_alts(&self) { unimplemented!() }
689    //
690    //    fn compile_parse_tree_pattern(&self, pattern, patternRuleIndex: Lexer, lexer: Lexer) { unimplemented!() }
691
692    #[inline]
693    pub fn enter_rule(
694        &mut self,
695        localctx: &'arena mut TreeNode<'input, 'arena, Node, TF::Tok>,
696        state: i32,
697        _rule_index: usize,
698    ) -> Result<(), ANTLRError> {
699        let child = localctx;
700        if self.build_parse_trees {
701            self.set_current_ctx(child.get_parent());
702            self.add_child_to_ctx(child);
703        }
704
705        self.set_state(state);
706        self.set_current_ctx(Some(child));
707        let start = self.input.lt(1);
708        self.with_mut_ctx(|ctx| {
709            ctx.set_start(start);
710        });
711
712        if !self.parse_listeners.is_empty() {
713            self.trigger_enter_rule_event()?;
714        }
715
716        #[cfg(feature = "recursion-limit")]
717        {
718            self.current_recursion_depth += 1;
719            if self.current_recursion_depth > self.recursion_limit {
720                return Err(ANTLRError::recursion_limit_exceeded(self.recursion_limit));
721            }
722        }
723        Ok(())
724    }
725
726    #[inline]
727    pub fn exit_rule(
728        &mut self,
729    ) -> Result<&'arena TreeNode<'input, 'arena, Node, TF::Tok>, ANTLRError> {
730        assert!(self.ctx().is_some());
731
732        #[cfg(feature = "recursion-limit")]
733        {
734            self.current_recursion_depth -= 1;
735        }
736        if self.matched_eof {
737            // if we have matched EOF, it cannot consume past EOF so we use LT(1) here
738            let stop = self.input.lt(1);
739            self.with_mut_ctx(|ctx| {
740                ctx.set_stop(stop);
741            });
742        } else {
743            // stop node is what we just matched
744            let stop = self.input.lt(-1);
745            self.with_mut_ctx(|ctx| {
746                ctx.set_stop(stop);
747            });
748        }
749        if !self.parse_listeners.is_empty() {
750            self.trigger_exit_rule_event()?;
751        }
752
753        self.set_state(self.ctx().unwrap().get_invoking_state());
754        let child = self.ctx().unwrap();
755        self.set_current_ctx(child.get_parent());
756
757        Ok(child)
758    }
759
760    pub fn enter_recursion_rule(
761        &mut self,
762        localctx: &'arena mut TreeNode<'input, 'arena, Node, TF::Tok>,
763        state: i32,
764        _rule_index: usize,
765        precedence: i32,
766    ) -> Result<(), ANTLRError> {
767        self.set_state(state);
768        self.precedence_stack.push(precedence);
769        self.set_current_ctx(Some(localctx));
770        let start = self.input.lt(1);
771        self.with_mut_ctx(|ctx| {
772            ctx.set_start(start);
773        });
774        if !self.parse_listeners.is_empty() {
775            self.trigger_enter_rule_event()?;
776        }
777        //println!("{}",self.input.lt(1).map(Token::to_owned).unwrap());
778
779        #[cfg(feature = "recursion-limit")]
780        {
781            self.current_recursion_depth += 1;
782            if self.current_recursion_depth > self.recursion_limit {
783                return Err(ANTLRError::recursion_limit_exceeded(self.recursion_limit));
784            }
785        }
786        Ok(())
787    }
788
789    pub fn push_new_recursion_context(
790        &mut self,
791        localctx: &'arena mut TreeNode<'input, 'arena, Node, TF::Tok>,
792        state: i32,
793        _rule_index: usize,
794    ) -> Result<&'arena TreeNode<'input, 'arena, Node, TF::Tok>, ANTLRError> {
795        let stop = self.input.lt(-1);
796        self.with_mut_ctx(|ctx| {
797            ctx.set_parent(Some(localctx));
798            ctx.set_invoking_state(state);
799            ctx.set_stop(stop);
800        });
801
802        let prev = self.take_ctx().unwrap();
803        self.set_current_ctx(Some(localctx));
804        let start = prev.get_start_token();
805        self.with_mut_ctx(|ctx| {
806            ctx.set_start(start);
807        });
808        if self.build_parse_trees {
809            self.add_child_to_ctx(prev);
810        }
811        if !self.parse_listeners.is_empty() {
812            self.trigger_enter_rule_event()?;
813        }
814        Ok(prev)
815    }
816
817    pub fn unroll_recursion_context(
818        &mut self,
819        parent_ctx: Option<&'arena TreeNode<'input, 'arena, Node, TF::Tok>>,
820    ) -> Result<&'arena TreeNode<'input, 'arena, Node, TF::Tok>, ANTLRError> {
821        assert!(self.ctx().is_some());
822
823        #[cfg(feature = "recursion-limit")]
824        {
825            self.current_recursion_depth -= 1;
826        }
827        self.precedence_stack.pop();
828        let stop = self.input.lt(-1);
829        self.with_mut_ctx(|ctx| {
830            ctx.set_stop(stop);
831        });
832        let retctx = self.ctx;
833
834        // unroll so _ctx is as it was before call to recursive method
835        if !self.parse_listeners.is_empty() {
836            while !self.ctx_is(parent_ctx) {
837                self.trigger_exit_rule_event()?;
838                self.set_current_ctx(self.parent_ctx());
839            }
840        } else {
841            self.set_current_ctx(parent_ctx);
842        }
843
844        // hook into tree
845        unsafe {
846            (*(retctx as *mut TreeNode<'input, 'arena, Node, TF::Tok>)).set_parent(parent_ctx);
847        }
848
849        //        println!("{:?}",self.ctx.as_ref().map(|it|it.to_string_tree(self)));
850        if self.build_parse_trees && parent_ctx.is_some() {
851            self.add_child_to_ctx(unsafe {
852                &*(retctx as *const TreeNode<'input, 'arena, Node, TF::Tok>)
853            });
854        }
855        Ok(unsafe { &*(retctx as *const TreeNode<'input, 'arena, Node, TF::Tok>) })
856    }
857
858    #[allow(clippy::mut_from_ref)] // &mut is from the arena allocation
859    fn create_token_node(
860        &self,
861        token: &'arena TF::Tok,
862    ) -> Result<&'arena mut TreeNode<'input, 'arena, Node, TF::Tok>, ANTLRError> {
863        let ptr = TreeNode::create_token_node(self.arena, token);
864        if ptr.is_null() {
865            #[cfg(feature = "arena-allocation-limit")]
866            {
867                Err(ANTLRError::arena_allocation_limit_exceeded(
868                    self.arena.allocation_limit_bytes(),
869                    self.arena.total_allocated_bytes(),
870                ))
871            }
872            #[cfg(not(feature = "arena-allocation-limit"))]
873            {
874                std::alloc::handle_alloc_error(std::alloc::Layout::new::<
875                    TreeNode<'input, 'arena, Node, TF::Tok>,
876                >())
877            }
878        } else {
879            Ok(unsafe { &mut *ptr })
880        }
881    }
882
883    #[allow(clippy::mut_from_ref)] // &mut is from the arena allocation
884    fn create_error_node(
885        &self,
886        token: &'arena TF::Tok,
887    ) -> Result<&'arena mut TreeNode<'input, 'arena, Node, TF::Tok>, ANTLRError> {
888        let ptr = TreeNode::create_error_node(self.arena, token);
889        if ptr.is_null() {
890            Err(ANTLRError::dfa_cache_limit_exceeded(0, 0, 0))
891        } else {
892            Ok(unsafe { &mut *ptr })
893        }
894    }
895
896    /// Text representation of generated DFA for debugging purposes
897    pub fn dump_dfa(&self) {
898        let mut seen_one = false;
899        for i in 0..self.get_atn().decision_to_state.len() {
900            let dfa = self
901                .get_interpreter()
902                .decision_to_dfa(i)
903                .expect("dfa should exist for each decision");
904            if !dfa.is_empty() {
905                if seen_one {
906                    println!()
907                }
908                println!("Decision {}:", dfa.decision);
909                print!("{}", dfa.to_string(self.get_vocabulary()));
910                seen_one = true;
911            }
912        }
913    }
914}
915
916impl<'input, 'arena, Ext, Node, Input, TF> Drop for BaseParser<'input, 'arena, Ext, Node, Input, TF>
917where
918    'input: 'arena,
919    Ext: ParserRecog<'input, 'arena, Self, TF::Tok>,
920    TF: TokenFactory<'input, 'arena> + 'arena,
921    Input: TokenStream<'input, 'arena, TF>,
922    Node: NodeKindType<'arena, TF::Tok>,
923{
924    fn drop(&mut self) {
925        if self.global_cache_threshold == 0 {
926            return;
927        }
928
929        let Some(interp) = self.interp.take() else {
930            return;
931        };
932        let cache_bytes = interp.total_allocated_bytes();
933        drop(interp);
934
935        if cache_bytes > self.global_cache_threshold {
936            self.atn_manager.reset_all();
937        }
938    }
939}
940
941/// Allows to safely cast listener back to user type
942#[derive(Debug)]
943pub struct ListenerId<T: ?Sized> {
944    pub(crate) actual_id: usize,
945    phantom: PhantomData<fn() -> T>,
946}
947
948impl<T: ?Sized> ListenerId<T> {
949    #[allow(clippy::borrowed_box)]
950    pub fn new(listener: &Box<T>) -> ListenerId<T> {
951        ListenerId {
952            actual_id: listener.as_ref() as *const T as *const () as usize,
953            phantom: Default::default(),
954        }
955    }
956}
957
958impl<T> ListenerId<T> {
959    unsafe fn into_listener<U: ?Sized>(self, boxed: Box<U>) -> Box<T> {
960        Box::from_raw(Box::into_raw(boxed) as *mut T)
961    }
962}