do_not_use_antlr_rust/
lexer_atn_simulator.rs

1//! Implementation of lexer automata(DFA)
2use std::cell::Cell;
3
4use std::ops::Deref;
5use std::rc::Rc;
6use std::sync::Arc;
7use std::usize;
8
9use crate::atn::ATN;
10use crate::atn_config::{ATNConfig, ATNConfigType};
11use crate::atn_config_set::ATNConfigSet;
12use crate::atn_simulator::{BaseATNSimulator, IATNSimulator};
13use crate::atn_state::ATNStateType::RuleStopState;
14use crate::atn_state::{ATNState, ATNStateType};
15
16use crate::dfa::DFA;
17use crate::dfa_state::{DFAState, DFAStateRef};
18use crate::errors::ANTLRError;
19use crate::errors::ANTLRError::LexerNoAltError;
20use crate::int_stream::{IntStream, EOF};
21use crate::lexer::{Lexer, LexerPosition, LEXER_MAX_CHAR_VALUE, LEXER_MIN_CHAR_VALUE};
22use crate::lexer_action_executor::LexerActionExecutor;
23use crate::prediction_context::EMPTY_PREDICTION_CONTEXT;
24use crate::prediction_context::{
25    PredictionContext, PredictionContextCache, PREDICTION_CONTEXT_EMPTY_RETURN_STATE,
26};
27use crate::token::TOKEN_EOF;
28
29use crate::transition::{
30    ActionTransition, PredicateTransition, RuleTransition, Transition, TransitionType,
31};
32use crate::utils::cell_update;
33use parking_lot::{RwLock, RwLockUpgradableReadGuard, RwLockWriteGuard};
34
35#[allow(missing_docs)]
36pub const ERROR_DFA_STATE_REF: DFAStateRef = usize::MAX;
37
38// todo rewrite this to be actually usable
39#[doc(hidden)]
40pub trait ILexerATNSimulator: IATNSimulator {
41    fn reset(&mut self);
42    fn match_token<'input>(
43        &mut self,
44        mode: usize,
45        lexer: &mut impl Lexer<'input>,
46    ) -> Result<isize, ANTLRError>;
47    fn get_char_position_in_line(&self) -> isize;
48    fn set_char_position_in_line(&mut self, column: isize);
49    fn get_line(&self) -> isize;
50    fn set_line(&mut self, line: isize);
51    fn consume<T: IntStream + ?Sized>(&self, input: &mut T);
52    #[cold]
53    fn recover(&mut self, _re: ANTLRError, input: &mut impl IntStream) {
54        if input.la(1) != EOF {
55            self.consume(input)
56        }
57    }
58}
59
60/// Simple DFA implementation enough for lexer.
61#[derive(Debug)]
62pub struct LexerATNSimulator {
63    base: BaseATNSimulator,
64
65    //    merge_cache: DoubleDict,
66    start_index: isize,
67    pub(crate) current_pos: Rc<LexerPosition>,
68    mode: usize,
69    prev_accept: SimState,
70    // lexer_action_executor: Option<Box<LexerActionExecutor>>,
71}
72
73impl ILexerATNSimulator for LexerATNSimulator {
74    fn reset(&mut self) { self.prev_accept.reset() }
75
76    fn match_token<'input>(
77        &mut self,
78        mode: usize,
79        //        input:&mut dyn CharStream,
80        lexer: &mut impl Lexer<'input>,
81    ) -> Result<isize, ANTLRError> {
82        self.mode = mode;
83        let mark = lexer.input().mark();
84        //        println!("start matching on mode {}",mode);
85        let result = (|| {
86            self.start_index = lexer.input().index();
87            self.prev_accept.reset();
88            let temp = self.base.decision_to_dfa.clone();
89            let dfa = temp
90                .get(mode)
91                .ok_or_else(|| ANTLRError::IllegalStateError("invalid mode".into()))?;
92            let dfa = dfa.upgradable_read();
93
94            let s0 = dfa.s0;
95            match s0 {
96                None => self.match_atn(lexer, dfa),
97                Some(s0) => self.exec_atn(s0, lexer, dfa),
98                //                Err(_) => panic!("dfa rwlock error")
99            }
100        })();
101        lexer.input().release(mark);
102        result
103    }
104
105    fn get_char_position_in_line(&self) -> isize { self.current_pos.char_position_in_line.get() }
106
107    fn set_char_position_in_line(&mut self, column: isize) {
108        self.current_pos.char_position_in_line.set(column)
109    }
110
111    fn get_line(&self) -> isize { self.current_pos.line.get() }
112
113    fn set_line(&mut self, line: isize) { self.current_pos.char_position_in_line.set(line) }
114
115    fn consume<T: IntStream + ?Sized>(&self, _input: &mut T) {
116        let ch = _input.la(1);
117        if ch == '\n' as isize {
118            cell_update(&self.current_pos.line, |x| x + 1);
119            self.current_pos.char_position_in_line.set(0);
120        } else {
121            cell_update(&self.current_pos.char_position_in_line, |x| x + 1);
122        }
123        _input.consume();
124    }
125
126    //    fn get_recog(&self) -> Rc<RefCell<Box<Recognizer>>>{
127    //        Rc::clone(&self.recog)
128    //    }
129}
130
131impl IATNSimulator for LexerATNSimulator {
132    fn shared_context_cache(&self) -> &PredictionContextCache { self.base.shared_context_cache() }
133
134    fn atn(&self) -> &ATN { self.base.atn() }
135
136    fn decision_to_dfa(&self) -> &Vec<RwLock<DFA>> { self.base.decision_to_dfa() }
137}
138
139#[allow(missing_docs)]
140pub const MIN_DFA_EDGE: isize = 0;
141#[allow(missing_docs)]
142pub const MAX_DFA_EDGE: isize = 127;
143
144impl LexerATNSimulator {
145    /// Creates `LexerATNSimulator` instance which creates DFA over `atn`
146    ///
147    /// Called from generated parser.
148    pub fn new_lexer_atnsimulator(
149        atn: Arc<ATN>,
150        decision_to_dfa: Arc<Vec<RwLock<DFA>>>,
151        shared_context_cache: Arc<PredictionContextCache>,
152    ) -> LexerATNSimulator {
153        LexerATNSimulator {
154            base: BaseATNSimulator::new_base_atnsimulator(
155                atn,
156                decision_to_dfa,
157                shared_context_cache,
158            ),
159            start_index: 0,
160            current_pos: Rc::new(LexerPosition {
161                line: Cell::new(0),
162                char_position_in_line: Cell::new(0),
163            }),
164            mode: 0,
165            prev_accept: SimState::new(),
166            // lexer_action_executor: None,
167        }
168    }
169
170    //    fn copy_state(&self, _simulator: &mut LexerATNSimulator) {
171    //        unimplemented!()
172    //    }
173
174    #[cold]
175    fn match_atn<'input>(
176        &mut self,
177        lexer: &mut impl Lexer<'input>,
178        dfa: RwLockUpgradableReadGuard<'_, DFA>,
179    ) -> Result<isize, ANTLRError> {
180        //        let start_state = self.atn().mode_to_start_state.get(self.mode as usize).ok_or(ANTLRError::IllegalStateError("invalid mode".into()))?;
181        let atn = self.atn();
182        let start_state = *atn
183            .mode_to_start_state
184            .get(self.mode)
185            .ok_or_else(|| ANTLRError::IllegalStateError("invalid mode".into()))?;
186
187        let _old_mode = self.mode;
188        let mut s0_closure = self.compute_start_state(atn.states[start_state].as_ref(), lexer);
189        let _supress_edge = s0_closure.has_semantic_context();
190        s0_closure.set_has_semantic_context(false);
191
192        let mut dfa_mut = RwLockUpgradableReadGuard::upgrade(dfa);
193
194        let next_state = self.add_dfastate(&mut dfa_mut, s0_closure);
195        if !_supress_edge {
196            dfa_mut.s0 = Some(next_state);
197        }
198
199        self.exec_atn(
200            next_state,
201            lexer,
202            RwLockWriteGuard::downgrade_to_upgradable(dfa_mut),
203        )
204    }
205
206    fn exec_atn<'input>(
207        &mut self,
208        //        input: &'a mut dyn CharStream,
209        ds0: DFAStateRef,
210        lexer: &mut impl Lexer<'input>,
211        dfa: RwLockUpgradableReadGuard<'_, DFA>,
212    ) -> Result<isize, ANTLRError> {
213        //        if self.get_dfa().states.read().unwrap().get(ds0).unwrap().is_accept_state{
214        self.capture_sim_state(&dfa, lexer.input(), ds0);
215        //        }
216        let mut dfa = Some(dfa);
217        let mut symbol = lexer.input().la(1);
218        let mut s = ds0;
219        loop {
220            let target = Self::get_existing_target_state(dfa.as_ref().unwrap(), s, symbol);
221            let target =
222                target.unwrap_or_else(|| self.compute_target_state(&mut dfa, s, symbol, lexer));
223            //              let target = dfastates.deref().get(s).unwrap() ;x
224
225            if target == ERROR_DFA_STATE_REF {
226                break;
227            }
228            //            println!(" --- target computed {:?}", self.get_dfa().states.read().unwrap()[target].configs.configs.iter().map(|it|it.get_state()).collect::<Vec<_>>());
229
230            if symbol != EOF {
231                self.consume(lexer.input());
232            }
233
234            if self.capture_sim_state(dfa.as_ref().unwrap(), lexer.input(), target) {
235                if symbol == EOF {
236                    break;
237                }
238            }
239
240            symbol = lexer.input().la(1);
241
242            s = target;
243        }
244        // let _last = self.get_dfa().states.read().get(s).unwrap();
245
246        self.fail_or_accept(symbol, lexer, dfa.unwrap())
247    }
248
249    #[inline(always)]
250    fn get_existing_target_state(dfa: &DFA, _s: DFAStateRef, t: isize) -> Option<DFAStateRef> {
251        // if t < MIN_DFA_EDGE || t > MAX_DFA_EDGE {
252        //     return None;
253        // }
254
255        dfa.states[_s]
256            .edges
257            .get((t - MIN_DFA_EDGE) as usize)
258            .and_then(|x| match x {
259                0 => None,
260                x => Some(x),
261            })
262            .copied()
263    }
264
265    #[cold]
266    fn compute_target_state<'input>(
267        &self,
268        dfa: &mut Option<RwLockUpgradableReadGuard<'_, DFA>>,
269        s: DFAStateRef,
270        _t: isize,
271        lexer: &mut impl Lexer<'input>,
272    ) -> DFAStateRef {
273        let mut reach = ATNConfigSet::new_ordered();
274        self.get_reachable_config_set(
275            &dfa.as_ref().unwrap().states[s].configs,
276            &mut reach,
277            _t,
278            lexer,
279        );
280        //        println!(" --- target computed {:?}", reach.configs.iter().map(|it|it.get_state()).collect::<Vec<_>>());
281
282        let mut dfa_mut = RwLockUpgradableReadGuard::upgrade(dfa.take().unwrap());
283        // let mut states = dfa_mut.states;
284        if reach.is_empty() {
285            if !reach.has_semantic_context() {
286                self.add_dfaedge(&mut dfa_mut.states[s], _t, ERROR_DFA_STATE_REF);
287            }
288            *dfa = Some(RwLockWriteGuard::downgrade_to_upgradable(dfa_mut));
289            return ERROR_DFA_STATE_REF;
290        }
291
292        let supress_edge = reach.has_semantic_context();
293        reach.set_has_semantic_context(false);
294        let to = self.add_dfastate(&mut dfa_mut, Box::new(reach));
295        if !supress_edge {
296            let from = &mut dfa_mut.states[s];
297            self.add_dfaedge(from, _t, to);
298        }
299        //        println!("target state computed from {:?} to {:?} on symbol {}", _s, to, char::try_from(_t as u32).unwrap());
300        *dfa = Some(RwLockWriteGuard::downgrade_to_upgradable(dfa_mut));
301        to
302        //        states.get(to).unwrap()
303    }
304
305    fn get_reachable_config_set<'input>(
306        &self,
307        // _states: &V,
308        //        _input: &mut dyn CharStream,
309        _closure: &ATNConfigSet,
310        _reach: &mut ATNConfigSet,
311        _t: isize,
312        lexer: &mut impl Lexer<'input>,
313    ) {
314        let mut skip_alt = 0;
315        //        println!(" --- source {:?}", _closure.configs.iter().map(|it|it.get_state()).collect::<Vec<_>>());
316        for config in _closure.get_items() {
317            let current_alt_reached_accept_state = config.get_alt() == skip_alt;
318            if current_alt_reached_accept_state {
319                if let ATNConfigType::LexerATNConfig {
320                    passed_through_non_greedy_decision: true,
321                    ..
322                } = config.get_type()
323                {
324                    continue;
325                }
326            }
327            let atn_state = self.atn().states[config.get_state()].as_ref();
328            for tr in atn_state.get_transitions() {
329                if let Some(target) = tr.get_reachable_target(_t) {
330                    let exec = config.get_lexer_executor().map(|x| {
331                        x.clone()
332                            .fix_offset_before_match(lexer.input().index() - self.start_index)
333                    });
334
335                    let new = config.cloned_with_new_exec(self.atn().states[target].as_ref(), exec);
336                    if self.closure(
337                        new,
338                        _reach,
339                        current_alt_reached_accept_state,
340                        true,
341                        _t == EOF,
342                        lexer,
343                    ) {
344                        skip_alt = config.get_alt();
345                        break;
346                    }
347                }
348            }
349        }
350    }
351
352    //    fn get_reachable_target<T>(&self, states: &T, _trans: &Transition, _t: isize) -> &ATNState
353    //    where
354    //        T: Deref<Target = Vec<DFAState>>,
355    //    {
356    //        unimplemented!()
357    //    }
358
359    fn fail_or_accept<'input>(
360        &mut self,
361        _t: isize,
362        lexer: &mut impl Lexer<'input>,
363        dfa: RwLockUpgradableReadGuard<'_, DFA>,
364    ) -> Result<isize, ANTLRError> {
365        //        println!("fail_or_accept");
366        if let Some(state) = self.prev_accept.dfa_state {
367            //            let lexer_action_executor;
368            self.accept(lexer.input());
369
370            let prediction = {
371                let dfa_state_prediction = &dfa.states[state];
372                //                println!("accepted, prediction = {}, on dfastate {}", dfa_state_prediction.prediction, dfa_state_prediction.state_number);
373                //                lexer_action_executor = dfa_state_prediction.lexer_action_executor.clone();
374                //                let recog = self.recog.clone();
375                if let Some(x) = dfa_state_prediction.lexer_action_executor.as_ref() {
376                    x.execute(lexer, self.start_index)
377                }
378
379                dfa_state_prediction.prediction
380            };
381
382            //            self.lexer_action_executor = lexer_action_executor;
383            Ok(prediction)
384        } else {
385            if _t == EOF && lexer.input().index() == self.start_index {
386                return Ok(TOKEN_EOF);
387            }
388            Err(LexerNoAltError {
389                start_index: self.start_index,
390            })
391        }
392    }
393
394    fn accept<'input>(&mut self, input: &mut impl IntStream) {
395        input.seek(self.prev_accept.index);
396        self.current_pos.line.set(self.prev_accept.line);
397        self.current_pos
398            .char_position_in_line
399            .set(self.prev_accept.column);
400    }
401
402    fn compute_start_state<'input>(
403        &self,
404        _p: &dyn ATNState,
405        lexer: &mut impl Lexer<'input>,
406    ) -> Box<ATNConfigSet> {
407        //        let initial_context = &EMPTY_PREDICTION_CONTEXT;
408        let mut config_set = ATNConfigSet::new_ordered();
409
410        for (i, tr) in _p.get_transitions().iter().enumerate() {
411            let target = tr.get_target();
412            let atn_config = ATNConfig::new_lexer_atnconfig6(
413                target,
414                (i + 1) as isize,
415                EMPTY_PREDICTION_CONTEXT.clone(),
416            );
417            self.closure(atn_config, &mut config_set, false, false, false, lexer);
418        }
419
420        Box::new(config_set)
421    }
422
423    fn closure<'input>(
424        &self,
425        //        _input: &mut dyn CharStream,
426        mut config: ATNConfig,
427        _configs: &mut ATNConfigSet,
428        mut _current_alt_reached_accept_state: bool,
429        _speculative: bool,
430        _treat_eofas_epsilon: bool,
431        lexer: &mut impl Lexer<'input>,
432    ) -> bool {
433        //        let config = &config;
434        let atn = self.atn();
435        let state = atn.states[config.get_state()].as_ref();
436        //        println!("closure called on state {} {:?}", state.get_state_number(), state.get_state_type());
437
438        if let ATNStateType::RuleStopState {} = state.get_state_type() {
439            //            println!("reached rulestopstate {}",state.get_state_number());
440            if config.get_context().map(|x| x.has_empty_path()) != Some(false) {
441                if config.get_context().map(|x| x.is_empty()) != Some(false) {
442                    _configs.add(Box::new(config));
443                    return true;
444                } else {
445                    _configs.add(Box::new(
446                        config.cloned_with_new_ctx(state, Some(EMPTY_PREDICTION_CONTEXT.clone())),
447                    ));
448                    _current_alt_reached_accept_state = true
449                }
450            }
451
452            if config.get_context().map(|x| x.is_empty()) == Some(false) {
453                let ctx = config.take_context();
454                for i in 0..ctx.length() {
455                    if ctx.get_return_state(i) != PREDICTION_CONTEXT_EMPTY_RETURN_STATE {
456                        let new_ctx = ctx.get_parent(i).cloned();
457                        let return_state =
458                            self.atn().states[ctx.get_return_state(i) as usize].as_ref();
459                        let next_config = config.cloned_with_new_ctx(return_state, new_ctx);
460                        _current_alt_reached_accept_state = self.closure(
461                            next_config,
462                            _configs,
463                            _current_alt_reached_accept_state,
464                            _speculative,
465                            _treat_eofas_epsilon,
466                            lexer,
467                        )
468                    }
469                }
470            }
471
472            return _current_alt_reached_accept_state;
473        }
474
475        if !state.has_epsilon_only_transitions() {
476            if let ATNConfigType::LexerATNConfig {
477                passed_through_non_greedy_decision,
478                ..
479            } = config.config_type
480            {
481                if !_current_alt_reached_accept_state || !passed_through_non_greedy_decision {
482                    _configs.add(Box::new(config.clone()));
483                }
484            }
485        }
486
487        let state = atn.states[config.get_state()].as_ref();
488
489        for tr in state.get_transitions() {
490            let c = self.get_epsilon_target(
491                &mut config,
492                tr.as_ref(),
493                _configs,
494                _speculative,
495                _treat_eofas_epsilon,
496                lexer,
497            );
498
499            if let Some(c) = c {
500                _current_alt_reached_accept_state = self.closure(
501                    c,
502                    _configs,
503                    _current_alt_reached_accept_state,
504                    _speculative,
505                    _treat_eofas_epsilon,
506                    lexer,
507                );
508            }
509        }
510
511        _current_alt_reached_accept_state
512    }
513
514    fn get_epsilon_target<'input>(
515        &self,
516        //        _input: &mut dyn CharStream,
517        _config: &mut ATNConfig,
518        _trans: &dyn Transition,
519        _configs: &mut ATNConfigSet,
520        _speculative: bool,
521        _treat_eofas_epsilon: bool,
522        lexer: &mut impl Lexer<'input>,
523    ) -> Option<ATNConfig> {
524        let mut result = None;
525        let target = self.atn().states.get(_trans.get_target()).unwrap().as_ref();
526        //        println!("epsilon target for {:?} is {:?}", _trans, target.get_state_type());
527        match _trans.get_serialization_type() {
528            TransitionType::TRANSITION_EPSILON => {
529                result = Some(_config.cloned(target));
530            }
531            TransitionType::TRANSITION_RULE => {
532                let rt = _trans.cast::<RuleTransition>();
533                //println!("rule transition follow state{}", rt.follow_state);
534                let pred_ctx = PredictionContext::new_singleton(
535                    Some(_config.get_context().unwrap().clone()),
536                    rt.follow_state as isize,
537                );
538                result = Some(_config.cloned_with_new_ctx(target, Some(pred_ctx.into())));
539            }
540            TransitionType::TRANSITION_PREDICATE => {
541                let tr = _trans.cast::<PredicateTransition>();
542                _configs.set_has_semantic_context(true);
543                if self.evaluate_predicate(tr.rule_index, tr.pred_index, _speculative, lexer) {
544                    result = Some(_config.cloned(target));
545                }
546            }
547            TransitionType::TRANSITION_ACTION => {
548                //println!("action transition");
549                if _config.get_context().map(|x| x.has_empty_path()) != Some(false) {
550                    if let ATNConfigType::LexerATNConfig {
551                        lexer_action_executor,
552                        ..
553                    } = _config.get_type()
554                    {
555                        let tr = _trans.cast::<ActionTransition>();
556                        let lexer_action =
557                            self.atn().lexer_actions[tr.action_index as usize].clone();
558                        //dbg!(&lexer_action);
559                        let lexer_action_executor = LexerActionExecutor::new_copy_append(
560                            lexer_action_executor.as_deref(),
561                            lexer_action,
562                        );
563                        result =
564                            Some(_config.cloned_with_new_exec(target, Some(lexer_action_executor)))
565                    }
566                } else {
567                    result = Some(_config.cloned(target));
568                }
569            }
570            TransitionType::TRANSITION_RANGE
571            | TransitionType::TRANSITION_SET
572            | TransitionType::TRANSITION_ATOM => {
573                if _treat_eofas_epsilon {
574                    if _trans.matches(EOF, LEXER_MIN_CHAR_VALUE, LEXER_MAX_CHAR_VALUE) {
575                        let target = self.atn().states[_trans.get_target()].as_ref();
576                        result = Some(_config.cloned(target));
577                    }
578                }
579            }
580            TransitionType::TRANSITION_WILDCARD => {}
581            TransitionType::TRANSITION_NOTSET => {}
582            TransitionType::TRANSITION_PRECEDENCE => {
583                panic!("precedence predicates are not supposed to be in lexer");
584            }
585        }
586
587        result
588    }
589
590    fn evaluate_predicate<'input, T: Lexer<'input>>(
591        &self,
592        //        input: &mut dyn CharStream,
593        rule_index: isize,
594        pred_index: isize,
595        speculative: bool,
596        lexer: &mut T,
597    ) -> bool {
598        if !speculative {
599            return lexer.sempred(None, rule_index, pred_index);
600        }
601
602        let saved_column = self.current_pos.char_position_in_line.get();
603        let saved_line = self.current_pos.line.get();
604        let index = lexer.input().index();
605        let marker = lexer.input().mark();
606        self.consume(lexer.input());
607
608        let result = lexer.sempred(None, rule_index, pred_index);
609
610        self.current_pos.char_position_in_line.set(saved_column);
611        self.current_pos.line.set(saved_line);
612        lexer.input().seek(index);
613        lexer.input().release(marker);
614        return result;
615    }
616
617    fn capture_sim_state(
618        &mut self,
619        dfa: &DFA,
620        input: &impl IntStream,
621        dfa_state: DFAStateRef,
622    ) -> bool {
623        if dfa.states[dfa_state].is_accept_state {
624            self.prev_accept = SimState {
625                index: input.index(),
626                line: self.current_pos.line.get(),
627                column: self.current_pos.char_position_in_line.get(),
628                dfa_state: Some(dfa_state),
629            };
630            // self.prev_accept.index = input.index();
631            // self.prev_accept.dfa_state = Some(dfa_state);
632            return true;
633        }
634        false
635    }
636
637    fn add_dfaedge(&self, _from: &mut DFAState, t: isize, _to: DFAStateRef) {
638        if t < MIN_DFA_EDGE || t > MAX_DFA_EDGE {
639            return;
640        }
641
642        if _from.edges.len() < (MAX_DFA_EDGE - MIN_DFA_EDGE + 1) as usize {
643            _from
644                .edges
645                .resize((MAX_DFA_EDGE - MIN_DFA_EDGE + 1) as usize, 0);
646        }
647        _from.edges[(t - MIN_DFA_EDGE) as usize] = _to;
648    }
649
650    fn add_dfastate(&self, dfa: &mut DFA, _configs: Box<ATNConfigSet>) -> DFAStateRef
651// where
652    //     V: DerefMut<Target = Vec<DFAState>>,
653    {
654        assert!(!_configs.has_semantic_context());
655        let mut dfastate = DFAState::new_dfastate(usize::MAX, _configs);
656        let rule_index = dfastate
657            .configs //_configs
658            .get_items()
659            .find(|c| RuleStopState == *self.atn().states[c.get_state()].get_state_type())
660            .map(|c| {
661                let rule_index = self.atn().states[c.get_state()].get_rule_index();
662
663                //println!("accepted rule {} on state {}",rule_index,c.get_state());
664                (
665                    self.atn().rule_to_token_type[rule_index],
666                    c.get_lexer_executor()
667                        .map(LexerActionExecutor::clone)
668                        .map(Box::new),
669                )
670            });
671
672        if let Some((prediction, exec)) = rule_index {
673            dfastate.prediction = prediction;
674            dfastate.lexer_action_executor = exec;
675            dfastate.is_accept_state = true;
676        }
677
678        let states = &mut dfa.states;
679        let key = dfastate.default_hash();
680        let dfastate_index = *dfa
681            .states_map
682            .entry(key)
683            .or_insert_with(|| {
684                dfastate.state_number = states.deref().len();
685                dfastate.configs.set_read_only(true);
686                let i = dfastate.state_number;
687                //println!("inserting new DFA state {} with size {}", i, dfastate.configs.length());
688                states.push(dfastate);
689                vec![i]
690            })
691            .first()
692            .unwrap();
693
694        //println!("new DFA state {}", dfastate_index);
695
696        //        dfa.states.write().unwrap().get_mut(*dfastate_index).unwrap()
697        dfastate_index
698    }
699
700    /// Returns current DFA that is currently used.
701    pub fn get_dfa(&self) -> &RwLock<DFA> { &self.decision_to_dfa()[self.mode] }
702
703    /// Returns current DFA for particular lexer mode
704    pub fn get_dfa_for_mode(&self, mode: usize) -> &RwLock<DFA> { &self.decision_to_dfa()[mode] }
705
706    // fn get_token_name(&self, _tt: isize) -> String { unimplemented!() }
707
708    // fn reset_sim_state(_sim: &mut SimState) { unimplemented!() }
709}
710
711#[derive(Debug)]
712pub(crate) struct SimState {
713    index: isize,
714    line: isize,
715    column: isize,
716    dfa_state: Option<usize>,
717}
718
719impl SimState {
720    pub(crate) fn new() -> SimState {
721        SimState {
722            index: -1,
723            line: 0,
724            column: -1,
725            dfa_state: None,
726        }
727    }
728
729    pub(crate) fn reset(&mut self) {
730        // self.index = -1;
731        // self.line = 0;
732        // self.column = -1;
733        self.dfa_state = None;
734    }
735}