1use std::cell::{RefCell, RefMut};
2use std::collections::{BTreeSet, HashMap, VecDeque};
3use std::hash::BuildHasherDefault;
4use std::rc::Rc;
5
6use crate::atn::LexerAtn;
7use crate::char_stream::{CharStream, TextInterval};
8use crate::int_stream::EOF;
9use crate::prediction::{
10 ContextArena, ContextId, EMPTY_CONTEXT, PredictionFxHasher, PredictionWorkspace,
11};
12use crate::recognizer::{Recognizer, RecognizerData};
13use crate::token::{
14 DEFAULT_CHANNEL, INVALID_TOKEN_TYPE, TokenId, TokenSink, TokenSourceError, TokenSpec,
15 TokenStoreError,
16};
17
18#[allow(clippy::disallowed_types)]
19type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<PredictionFxHasher>>;
20
21pub const SKIP: i32 = -3;
22pub const MORE: i32 = -2;
23pub const DEFAULT_MODE: i32 = 0;
24
25#[derive(Clone, Copy, Debug, Eq, PartialEq)]
26pub struct LexerMode(pub i32);
27
28#[derive(Clone, Copy, Debug, Eq, PartialEq)]
35pub struct LexerCustomAction {
36 rule_index: i32,
37 action_index: i32,
38 position: usize,
39}
40
41impl LexerCustomAction {
42 pub const fn new(rule_index: i32, action_index: i32, position: usize) -> Self {
44 Self {
45 rule_index,
46 action_index,
47 position,
48 }
49 }
50
51 pub const fn rule_index(self) -> i32 {
53 self.rule_index
54 }
55
56 pub const fn action_index(self) -> i32 {
58 self.action_index
59 }
60
61 pub const fn position(self) -> usize {
63 self.position
64 }
65}
66
67#[derive(Clone, Copy, Debug, Eq, PartialEq)]
69pub struct LexerPredicate {
70 rule_index: usize,
71 pred_index: usize,
72 position: usize,
73}
74
75impl LexerPredicate {
76 pub const fn new(rule_index: usize, pred_index: usize, position: usize) -> Self {
78 Self {
79 rule_index,
80 pred_index,
81 position,
82 }
83 }
84
85 pub const fn rule_index(self) -> usize {
87 self.rule_index
88 }
89
90 pub const fn pred_index(self) -> usize {
92 self.pred_index
93 }
94
95 pub const fn position(self) -> usize {
97 self.position
98 }
99}
100
101#[derive(Debug)]
106enum LexerRef<'a, I>
107where
108 I: CharStream,
109{
110 Shared(&'a BaseLexer<I>),
111 Mut(&'a mut BaseLexer<I>),
112}
113
114impl<I> LexerRef<'_, I>
115where
116 I: CharStream,
117{
118 const fn get(&self) -> &BaseLexer<I> {
119 match self {
120 LexerRef::Shared(lexer) => lexer,
121 LexerRef::Mut(lexer) => lexer,
122 }
123 }
124}
125
126#[derive(Debug)]
128pub struct LexerSemCtx<'a, I>
129where
130 I: CharStream,
131{
132 lexer: LexerRef<'a, I>,
133 rule_index: usize,
134 coordinate_index: usize,
135 position: usize,
136}
137
138impl<'a, I> LexerSemCtx<'a, I>
139where
140 I: CharStream,
141{
142 pub(crate) const fn new(
143 lexer: &'a BaseLexer<I>,
144 rule_index: usize,
145 coordinate_index: usize,
146 position: usize,
147 ) -> Self {
148 Self {
149 lexer: LexerRef::Shared(lexer),
150 rule_index,
151 coordinate_index,
152 position,
153 }
154 }
155
156 pub(crate) const fn new_mut(
159 lexer: &'a mut BaseLexer<I>,
160 rule_index: usize,
161 coordinate_index: usize,
162 position: usize,
163 ) -> Self {
164 Self {
165 lexer: LexerRef::Mut(lexer),
166 rule_index,
167 coordinate_index,
168 position,
169 }
170 }
171
172 #[must_use]
174 pub const fn rule_index(&self) -> usize {
175 self.rule_index
176 }
177
178 #[must_use]
180 pub const fn coordinate_index(&self) -> usize {
181 self.coordinate_index
182 }
183
184 #[must_use]
186 pub const fn position(&self) -> usize {
187 self.position
188 }
189
190 #[must_use]
192 pub fn mode(&self) -> i32 {
193 self.lexer.get().mode()
194 }
195
196 #[must_use]
198 pub const fn column(&self) -> usize {
199 self.lexer.get().column()
200 }
201
202 #[must_use]
204 pub fn position_column(&self) -> usize {
205 self.lexer.get().column_at(self.position)
206 }
207
208 #[must_use]
210 pub const fn token_start_column(&self) -> usize {
211 self.lexer.get().token_start_column()
212 }
213
214 #[must_use]
216 pub fn text_so_far(&self) -> String {
217 self.lexer.get().token_text_until(self.position)
218 }
219
220 pub fn la(&mut self, offset: isize) -> i32 {
226 match &mut self.lexer {
227 LexerRef::Shared(lexer) => lexer.lookahead_at(self.position, offset),
228 LexerRef::Mut(lexer) => lexer.input_mut().la(offset),
229 }
230 }
231
232 #[must_use]
234 pub const fn token_start(&self) -> usize {
235 self.lexer.get().token_start()
236 }
237
238 #[must_use]
240 pub const fn token_type(&self) -> i32 {
241 self.lexer.get().token_type()
242 }
243
244 #[must_use]
246 pub const fn channel(&self) -> i32 {
247 self.lexer.get().channel()
248 }
249
250 pub const fn set_type(&mut self, token_type: i32) -> bool {
253 match &mut self.lexer {
254 LexerRef::Mut(lexer) => {
255 lexer.set_type(token_type);
256 true
257 }
258 LexerRef::Shared(_) => false,
259 }
260 }
261
262 pub const fn set_channel(&mut self, channel: i32) -> bool {
265 match &mut self.lexer {
266 LexerRef::Mut(lexer) => {
267 lexer.set_channel(channel);
268 true
269 }
270 LexerRef::Shared(_) => false,
271 }
272 }
273
274 pub fn consume(&mut self) -> bool {
277 match &mut self.lexer {
278 LexerRef::Mut(lexer) => {
279 lexer.consume_char();
280 true
281 }
282 LexerRef::Shared(_) => false,
283 }
284 }
285
286 pub const fn skip(&mut self) -> bool {
288 self.set_type(SKIP)
289 }
290
291 pub const fn more(&mut self) -> bool {
294 self.set_type(MORE)
295 }
296
297 pub fn reset_accept_position(&mut self, index: usize) -> bool {
299 match &mut self.lexer {
300 LexerRef::Mut(lexer) => {
301 lexer.reset_accept_position(index);
302 true
303 }
304 LexerRef::Shared(_) => false,
305 }
306 }
307
308 pub fn set_token_start(&mut self, index: usize) -> bool {
314 match &mut self.lexer {
315 LexerRef::Mut(lexer) => lexer.set_token_start(index),
316 LexerRef::Shared(_) => false,
317 }
318 }
319
320 pub fn enqueue_token(&mut self, token_type: i32, stop: usize) -> bool {
326 let channel = self.channel();
327 self.enqueue_token_with_channel(token_type, channel, stop)
328 }
329
330 pub fn enqueue_token_with_channel(
333 &mut self,
334 token_type: i32,
335 channel: i32,
336 stop: usize,
337 ) -> bool {
338 match &mut self.lexer {
339 LexerRef::Mut(lexer) => {
340 lexer.enqueue_token(token_type, channel, stop, None);
341 true
342 }
343 LexerRef::Shared(_) => false,
344 }
345 }
346
347 pub fn set_mode(&mut self, mode: i32) -> bool {
354 match &mut self.lexer {
355 LexerRef::Mut(lexer) => {
356 lexer.set_mode(mode);
357 true
358 }
359 LexerRef::Shared(_) => false,
360 }
361 }
362
363 pub fn push_mode(&mut self, mode: i32) -> bool {
366 match &mut self.lexer {
367 LexerRef::Mut(lexer) => {
368 lexer.push_mode(mode);
369 true
370 }
371 LexerRef::Shared(_) => false,
372 }
373 }
374
375 pub fn pop_mode(&mut self) -> Option<i32> {
379 match &mut self.lexer {
380 LexerRef::Mut(lexer) => lexer.pop_mode(),
381 LexerRef::Shared(_) => None,
382 }
383 }
384}
385
386#[derive(Debug)]
394pub struct LexerLifecycleCtx<'a, I>
395where
396 I: CharStream,
397{
398 lexer: &'a mut BaseLexer<I>,
399 accept_position: Option<usize>,
400}
401
402impl<'a, I> LexerLifecycleCtx<'a, I>
403where
404 I: CharStream,
405{
406 pub(crate) const fn new(lexer: &'a mut BaseLexer<I>, accept_position: Option<usize>) -> Self {
407 Self {
408 lexer,
409 accept_position,
410 }
411 }
412
413 #[must_use]
418 pub const fn accept_position(&self) -> Option<usize> {
419 self.accept_position
420 }
421
422 #[must_use]
424 pub fn input_position(&self) -> usize {
425 self.lexer.input().index()
426 }
427
428 #[must_use]
430 pub const fn mode(&self) -> i32 {
431 self.lexer.mode
432 }
433
434 #[must_use]
436 pub const fn line(&self) -> usize {
437 self.lexer.line()
438 }
439
440 #[must_use]
442 pub const fn column(&self) -> usize {
443 self.lexer.column()
444 }
445
446 #[must_use]
448 pub const fn token_start(&self) -> usize {
449 self.lexer.token_start()
450 }
451
452 #[must_use]
454 pub const fn token_start_line(&self) -> usize {
455 self.lexer.token_start_line()
456 }
457
458 #[must_use]
460 pub const fn token_start_column(&self) -> usize {
461 self.lexer.token_start_column()
462 }
463
464 #[must_use]
466 pub const fn token_type(&self) -> i32 {
467 self.lexer.token_type()
468 }
469
470 #[must_use]
472 pub const fn channel(&self) -> i32 {
473 self.lexer.channel()
474 }
475
476 #[must_use]
478 pub fn pending_token_count(&self) -> usize {
479 self.lexer.pending_tokens.len()
480 }
481
482 #[must_use]
484 pub fn token_text(&self) -> String {
485 self.lexer.token_text()
486 }
487
488 #[must_use]
492 pub fn accepted_text(&self) -> Option<String> {
493 self.accept_position
494 .map(|position| self.lexer.token_text_until(position))
495 }
496
497 pub fn la(&mut self, offset: isize) -> i32 {
500 self.lexer.la(offset)
501 }
502
503 pub fn consume(&mut self) {
505 self.lexer.consume_char();
506 }
507
508 pub const fn set_type(&mut self, token_type: i32) {
510 self.lexer.set_type(token_type);
511 }
512
513 pub const fn set_channel(&mut self, channel: i32) {
515 self.lexer.set_channel(channel);
516 }
517
518 pub const fn skip(&mut self) {
520 self.lexer.skip();
521 }
522
523 pub const fn more(&mut self) {
525 self.lexer.more();
526 }
527
528 pub fn reset_accept_position(&mut self, index: usize) {
530 self.lexer.reset_accept_position(index);
531 }
532
533 pub fn set_token_start(&mut self, index: usize) -> bool {
535 self.lexer.set_token_start(index)
536 }
537
538 pub fn enqueue_token(&mut self, token_type: i32, stop: usize) {
540 self.enqueue_token_with_channel(token_type, self.channel(), stop);
541 }
542
543 pub fn enqueue_token_with_channel(&mut self, token_type: i32, channel: i32, stop: usize) {
545 self.lexer.enqueue_token(token_type, channel, stop, None);
546 }
547
548 pub fn set_mode(&mut self, mode: i32) {
550 self.lexer.set_mode(mode);
551 }
552
553 pub fn push_mode(&mut self, mode: i32) {
555 self.lexer.push_mode(mode);
556 }
557
558 pub fn pop_mode(&mut self) -> Option<i32> {
560 self.lexer.pop_mode()
561 }
562}
563
564pub trait Lexer: Recognizer {
565 fn mode(&self) -> i32;
566 fn set_mode(&mut self, mode: i32);
567 fn push_mode(&mut self, mode: i32);
568 fn pop_mode(&mut self) -> Option<i32>;
569}
570
571#[derive(Clone, Debug)]
572pub struct BaseLexer<I> {
573 input: I,
574 data: RecognizerData,
575 has_source_text: bool,
576 mode: i32,
577 mode_stack: Vec<i32>,
578 token_type: i32,
579 channel: i32,
580 token_start: usize,
581 token_start_line: usize,
582 token_start_column: usize,
583 line: usize,
584 column: usize,
585 hit_eof: bool,
586 force_interpreted: bool,
587 errors: RefCell<Vec<TokenSourceError>>,
588 semantic_error_coordinates: RefCell<BTreeSet<(u8, usize, usize, usize)>>,
589 pending_tokens: VecDeque<TokenSpec>,
590 dfa_cache: Rc<RefCell<LexerDfaCache>>,
591}
592
593#[derive(Debug, Default)]
601struct LexerDfaCache {
602 prediction: LexerPredictionStore,
603 state_numbers: FxHashMap<LexerDfaKey, usize>,
604 accept_predictions: FxHashMap<usize, i32>,
605 edges: BTreeSet<LexerDfaEdge>,
609 cached_states: Vec<Option<Rc<LexerDfaCachedState>>>,
611 dense_edges: Vec<Option<Box<DenseEdgeRow>>>,
616 sparse_edges: FxHashMap<(usize, i32), LexerDfaCachedTransition>,
618 mode_starts: FxHashMap<i32, usize>,
619}
620
621#[derive(Debug, Default)]
624pub(crate) struct LexerPredictionStore {
625 pub(crate) contexts: LexerContextArena,
626 pub(crate) workspace: PredictionWorkspace,
627}
628
629#[repr(transparent)]
631#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
632pub(crate) struct LexerContextId(u32);
633
634pub(crate) const EMPTY_LEXER_CONTEXT: LexerContextId = LexerContextId(0);
635
636#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
642pub(crate) enum LexerContextNode {
643 Empty,
644 Singleton {
645 parent: LexerContextId,
646 return_state: usize,
647 },
648 Union {
649 left: LexerContextId,
650 right: LexerContextId,
651 },
652}
653
654#[derive(Clone, Copy, Debug)]
655struct LexerContextRecord {
656 node: LexerContextNode,
657 path_set: ContextId,
658}
659
660#[derive(Debug)]
662pub(crate) struct LexerContextArena {
663 records: Vec<LexerContextRecord>,
664 ids: FxHashMap<LexerContextNode, LexerContextId>,
665 path_sets: ContextArena,
666}
667
668impl LexerContextArena {
669 pub(crate) fn new() -> Self {
670 let mut ids = FxHashMap::default();
671 ids.insert(LexerContextNode::Empty, EMPTY_LEXER_CONTEXT);
672 Self {
673 records: vec![LexerContextRecord {
674 node: LexerContextNode::Empty,
675 path_set: EMPTY_CONTEXT,
676 }],
677 ids,
678 path_sets: ContextArena::new(),
679 }
680 }
681
682 pub(crate) fn singleton(
683 &mut self,
684 parent: LexerContextId,
685 return_state: usize,
686 ) -> LexerContextId {
687 self.assert_valid(parent);
688 let node = LexerContextNode::Singleton {
689 parent,
690 return_state,
691 };
692 if let Some(&context) = self.ids.get(&node) {
693 return context;
694 }
695 let path_set = self
696 .path_sets
697 .singleton(self.record(parent).path_set, return_state);
698 self.intern(node, path_set)
699 }
700
701 pub(crate) fn merge(
702 &mut self,
703 left: LexerContextId,
704 right: LexerContextId,
705 workspace: &mut PredictionWorkspace,
706 ) -> LexerContextId {
707 self.assert_valid(left);
708 self.assert_valid(right);
709 if left == right {
710 return left;
711 }
712 let left_set = self.record(left).path_set;
713 let right_set = self.record(right).path_set;
714 let path_set = self.path_sets.merge(left_set, right_set, false, workspace);
715 if path_set == left_set {
716 return left;
717 }
718 let node = LexerContextNode::Union { left, right };
719 if let Some(&context) = self.ids.get(&node) {
720 return context;
721 }
722 self.intern(node, path_set)
723 }
724
725 pub(crate) fn node(&self, context: LexerContextId) -> LexerContextNode {
726 self.record(context).node
727 }
728
729 #[cfg(test)]
730 pub(crate) const fn len(&self) -> usize {
731 self.records.len()
732 }
733
734 fn intern(&mut self, node: LexerContextNode, path_set: ContextId) -> LexerContextId {
735 let context = LexerContextId(
736 u32::try_from(self.records.len()).expect("lexer context arena must fit in u32"),
737 );
738 self.records.push(LexerContextRecord { node, path_set });
739 self.ids.insert(node, context);
740 context
741 }
742
743 fn record(&self, context: LexerContextId) -> &LexerContextRecord {
744 self.assert_valid(context);
745 &self.records[usize::try_from(context.0).expect("u32 lexer context ID fits in usize")]
746 }
747
748 fn assert_valid(&self, context: LexerContextId) {
749 assert!(
750 usize::try_from(context.0).is_ok_and(|index| index < self.records.len()),
751 "lexer context ID does not belong to this store"
752 );
753 }
754}
755
756impl Default for LexerContextArena {
757 fn default() -> Self {
758 Self::new()
759 }
760}
761
762const DENSE_EDGE_SYMBOLS: usize = 128;
764
765type DenseEdgeRow = [LexerDfaCachedTransition; DENSE_EDGE_SYMBOLS];
766
767const EMPTY_DENSE_EDGE: LexerDfaCachedTransition = LexerDfaCachedTransition {
770 target_state: usize::MAX,
771 position_delta: 0,
772};
773
774thread_local! {
775 static SHARED_LEXER_DFA_CACHES: RefCell<HashMap<usize, Rc<RefCell<LexerDfaCache>>>> =
778 RefCell::new(HashMap::new());
779}
780
781#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
786pub(crate) struct LexerDfaKey {
787 configs: Vec<LexerDfaConfigKey>,
788}
789
790impl LexerDfaKey {
791 pub(crate) const fn new(configs: Vec<LexerDfaConfigKey>) -> Self {
792 Self { configs }
793 }
794}
795
796#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
798pub(crate) struct LexerDfaConfigKey {
799 pub(crate) state: usize,
800 pub(crate) alt_rule_index: Option<usize>,
801 pub(crate) consumed_eof: bool,
802 pub(crate) passed_non_greedy: bool,
803 pub(crate) context: LexerContextId,
804 pub(crate) actions: Vec<LexerDfaActionKey>,
805}
806
807#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
808pub(crate) struct LexerDfaActionKey {
809 pub(crate) action_index: usize,
810 pub(crate) position_delta: usize,
811 pub(crate) rule_index: usize,
812}
813
814impl LexerDfaConfigKey {
815 pub(crate) const fn new(
816 state: usize,
817 alt_rule_index: Option<usize>,
818 consumed_eof: bool,
819 passed_non_greedy: bool,
820 context: LexerContextId,
821 actions: Vec<LexerDfaActionKey>,
822 ) -> Self {
823 Self {
824 state,
825 alt_rule_index,
826 consumed_eof,
827 passed_non_greedy,
828 context,
829 actions,
830 }
831 }
832}
833
834#[derive(Clone, Copy, Debug)]
835pub(crate) struct LexerDfaCachedTransition {
836 pub(crate) target_state: usize,
837 pub(crate) position_delta: usize,
838}
839
840#[derive(Clone, Debug)]
841pub(crate) struct LexerDfaCachedAccept {
842 pub(crate) position_delta: usize,
843 pub(crate) rule_index: usize,
844 pub(crate) consumed_eof: bool,
845 pub(crate) actions: Vec<LexerDfaActionKey>,
846}
847
848#[derive(Clone, Debug)]
849pub(crate) struct LexerDfaCachedState {
850 pub(crate) has_semantic_context: bool,
851 pub(crate) configs: Vec<LexerDfaConfigKey>,
852 pub(crate) accept: Option<LexerDfaCachedAccept>,
853}
854
855#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
858struct LexerDfaEdge {
859 from: usize,
860 symbol: i32,
861 to: usize,
862}
863
864impl<I> BaseLexer<I>
865where
866 I: CharStream,
867{
868 pub fn new(input: I, data: RecognizerData) -> Self {
869 let has_source_text = input.source_text().is_some();
870 Self {
871 input,
872 data,
873 has_source_text,
874 mode: DEFAULT_MODE,
875 mode_stack: Vec::new(),
876 token_type: INVALID_TOKEN_TYPE,
877 channel: DEFAULT_CHANNEL,
878 token_start: 0,
879 token_start_line: 1,
880 token_start_column: 0,
881 line: 1,
882 column: 0,
883 hit_eof: false,
884 force_interpreted: false,
885 errors: RefCell::new(Vec::new()),
886 semantic_error_coordinates: RefCell::new(BTreeSet::new()),
887 pending_tokens: VecDeque::new(),
888 dfa_cache: Rc::new(RefCell::new(LexerDfaCache::default())),
889 }
890 }
891
892 pub fn reset(&mut self) {
899 self.input.seek(0);
900 self.mode = DEFAULT_MODE;
901 self.mode_stack.clear();
902 self.token_type = INVALID_TOKEN_TYPE;
903 self.channel = DEFAULT_CHANNEL;
904 self.token_start = 0;
905 self.token_start_line = 1;
906 self.token_start_column = 0;
907 self.line = 1;
908 self.column = 0;
909 self.hit_eof = false;
910 self.errors.get_mut().clear();
911 self.semantic_error_coordinates.get_mut().clear();
912 self.pending_tokens.clear();
913 }
914
915 pub fn set_input_stream(&mut self, input: I) {
920 self.input = input;
921 self.has_source_text = self.input.source_text().is_some();
922 self.reset();
923 }
924
925 #[must_use]
935 pub fn with_shared_dfa(mut self, atn: &'static LexerAtn) -> Self {
936 let ptr: *const LexerAtn = atn;
937 let key = ptr as usize;
938 self.dfa_cache = SHARED_LEXER_DFA_CACHES
939 .with(|caches| Rc::clone(caches.borrow_mut().entry(key).or_insert_with(Rc::default)));
940 self
941 }
942
943 pub fn clear_dfa(&self) {
949 let mut cache = self.dfa_cache.borrow_mut();
950 let prediction = std::mem::take(&mut cache.prediction);
953 *cache = LexerDfaCache {
954 prediction,
955 ..LexerDfaCache::default()
956 };
957 }
958
959 pub const fn input(&self) -> &I {
960 &self.input
961 }
962
963 pub const fn input_mut(&mut self) -> &mut I {
964 &mut self.input
965 }
966
967 pub fn begin_token(&mut self) {
970 self.semantic_error_coordinates.get_mut().clear();
971 self.token_type = INVALID_TOKEN_TYPE;
972 self.channel = DEFAULT_CHANNEL;
973 self.token_start = self.input.index();
974 self.token_start_line = self.line;
975 self.token_start_column = self.column;
976 }
977
978 pub const fn token_start(&self) -> usize {
980 self.token_start
981 }
982
983 pub const fn token_start_line(&self) -> usize {
985 self.token_start_line
986 }
987
988 pub const fn token_start_column(&self) -> usize {
990 self.token_start_column
991 }
992
993 pub const fn token_type(&self) -> i32 {
995 self.token_type
996 }
997
998 pub const fn set_type(&mut self, token_type: i32) {
1000 self.token_type = token_type;
1001 }
1002
1003 pub const fn channel(&self) -> i32 {
1005 self.channel
1006 }
1007
1008 pub const fn set_channel(&mut self, channel: i32) {
1010 self.channel = channel;
1011 }
1012
1013 pub const fn skip(&mut self) {
1015 self.set_type(SKIP);
1016 }
1017
1018 pub const fn more(&mut self) {
1020 self.set_type(MORE);
1021 }
1022
1023 pub fn la(&mut self, offset: isize) -> i32 {
1026 self.input.la(offset)
1027 }
1028
1029 fn lookahead_at(&self, position: usize, offset: isize) -> i32 {
1030 if offset == 0 {
1031 return 0;
1032 }
1033 let absolute = if offset > 0 {
1034 position.checked_add((offset - 1).cast_unsigned())
1035 } else {
1036 offset
1037 .checked_neg()
1038 .and_then(|distance| usize::try_from(distance).ok())
1039 .and_then(|distance| position.checked_sub(distance))
1040 };
1041 let Some(index) = absolute.filter(|index| *index < self.input.size()) else {
1042 return EOF;
1043 };
1044 if let Some(symbol) = self.input.symbol_at(index) {
1045 return symbol;
1046 }
1047 self.input
1048 .text(TextInterval::new(index, index))
1049 .chars()
1050 .next()
1051 .map_or(EOF, |ch| u32::from(ch).cast_signed())
1052 }
1053
1054 pub fn consume_char(&mut self) {
1061 let la = self.input.la(1);
1062 if la == EOF {
1063 return;
1064 }
1065 self.input.consume();
1066 if char::from_u32(la.cast_unsigned()) == Some('\n') {
1067 self.line += 1;
1068 self.column = 0;
1069 } else {
1070 self.column += 1;
1071 }
1072 }
1073
1074 pub(crate) fn commit_position(&mut self, start: usize, target: usize) {
1077 self.reposition_from(start, self.line, self.column, target);
1078 }
1079
1080 fn reposition_from(&mut self, start: usize, line: usize, column: usize, target: usize) {
1081 let start = start.min(self.input.size());
1082 let target = target.max(start).min(self.input.size());
1083 if let Some(summary) = self.input.position_summary(start, target) {
1084 self.input.seek(target);
1085 (self.line, self.column) = summary.apply(line, column);
1086 #[cfg(feature = "perf-counters")]
1087 crate::perf::record_lexer_bulk_commit(target - start);
1088 return;
1089 }
1090
1091 self.input.seek(start);
1092 self.line = line;
1093 self.column = column;
1094 #[cfg(feature = "perf-counters")]
1095 let before = self.input.index();
1096 while self.input.index() < target && self.input.la(1) != EOF {
1097 self.consume_char();
1098 }
1099 #[cfg(feature = "perf-counters")]
1100 crate::perf::record_lexer_scalar_replay(self.input.index().saturating_sub(before));
1101 }
1102
1103 pub fn reset_accept_position(&mut self, index: usize) {
1110 let target = index.max(self.token_start);
1111 self.reposition_from(
1112 self.token_start,
1113 self.token_start_line,
1114 self.token_start_column,
1115 target,
1116 );
1117 }
1118
1119 pub fn set_token_start(&mut self, index: usize) -> bool {
1125 if index < self.token_start || index > self.input.index() {
1126 return false;
1127 }
1128 let (line, column) = self.position_at(index);
1129 self.token_start = index;
1130 self.token_start_line = line;
1131 self.token_start_column = column;
1132 true
1133 }
1134
1135 pub fn emit(
1143 &self,
1144 sink: &mut TokenSink<'_>,
1145 token_type: i32,
1146 channel: i32,
1147 text: Option<String>,
1148 ) -> Result<TokenId, TokenStoreError> {
1149 let stop = self.input.index().checked_sub(1).unwrap_or(usize::MAX);
1150 self.emit_with_stop(sink, token_type, channel, stop, text)
1151 }
1152
1153 pub fn emit_with_stop(
1159 &self,
1160 sink: &mut TokenSink<'_>,
1161 token_type: i32,
1162 channel: i32,
1163 stop: usize,
1164 text: Option<String>,
1165 ) -> Result<TokenId, TokenStoreError> {
1166 sink.push(self.token_spec_with_stop(token_type, channel, stop, text))
1167 }
1168
1169 fn token_spec_with_stop(
1170 &self,
1171 token_type: i32,
1172 channel: i32,
1173 stop: usize,
1174 text: Option<String>,
1175 ) -> TokenSpec {
1176 let text = text.or_else(|| {
1177 if stop == usize::MAX {
1178 Some("<EOF>".to_owned())
1179 } else {
1180 None
1181 }
1182 });
1183 let source_interval = if self.has_source_text
1184 && text.is_none()
1185 && stop != usize::MAX
1186 && self.token_start <= stop
1187 {
1188 self.input
1189 .byte_interval(TextInterval::new(self.token_start, stop))
1190 } else {
1191 None
1192 };
1193 let text = text.or_else(|| {
1194 source_interval
1195 .is_none()
1196 .then(|| self.input.text(TextInterval::new(self.token_start, stop)))
1197 });
1198 let (start_byte, stop_byte) = source_interval
1199 .or_else(|| self.token_byte_span(stop))
1200 .unwrap_or((self.token_start, self.token_start));
1201 TokenSpec {
1202 token_type,
1203 channel,
1204 start: self.token_start,
1205 stop,
1206 start_byte,
1207 stop_byte,
1208 line: self.token_start_line,
1209 column: self.token_start_column,
1210 text,
1211 source_backed: source_interval.is_some(),
1212 }
1213 }
1214
1215 pub fn enqueue_token(
1221 &mut self,
1222 token_type: i32,
1223 channel: i32,
1224 stop: usize,
1225 text: Option<String>,
1226 ) {
1227 let token = self.token_spec_with_stop(token_type, channel, stop, text);
1228 self.pending_tokens.push_back(token);
1229 }
1230
1231 pub(crate) fn emit_pending_token(
1232 &mut self,
1233 sink: &mut TokenSink<'_>,
1234 ) -> Result<Option<TokenId>, TokenStoreError> {
1235 self.pending_tokens
1236 .pop_front()
1237 .map(|token| sink.push(token))
1238 .transpose()
1239 }
1240
1241 pub(crate) fn emit_or_enqueue_with_stop(
1242 &mut self,
1243 sink: &mut TokenSink<'_>,
1244 stop: usize,
1245 text: Option<String>,
1246 ) -> Result<TokenId, TokenStoreError> {
1247 let token = self.token_spec_with_stop(self.token_type, self.channel, stop, text);
1248 self.emit_or_enqueue(sink, token)
1249 }
1250
1251 fn emit_or_enqueue(
1252 &mut self,
1253 sink: &mut TokenSink<'_>,
1254 token: TokenSpec,
1255 ) -> Result<TokenId, TokenStoreError> {
1256 if self.pending_tokens.is_empty() {
1257 return sink.push(token);
1258 }
1259 self.pending_tokens.push_back(token);
1260 self.emit_pending_token(sink)?
1261 .ok_or_else(|| unreachable!("the pending-token queue was just populated"))
1262 }
1263
1264 pub fn token_text(&self) -> String {
1267 self.token_text_until(self.input.index())
1268 }
1269
1270 pub fn token_text_until(&self, stop_exclusive: usize) -> String {
1278 if stop_exclusive <= self.token_start {
1279 return String::new();
1280 }
1281 self.input
1282 .text(TextInterval::new(self.token_start, stop_exclusive - 1))
1283 }
1284
1285 pub fn column_at(&self, position: usize) -> usize {
1288 self.position_at(position).1
1289 }
1290
1291 fn position_at(&self, position: usize) -> (usize, usize) {
1292 let mut line = self.token_start_line;
1293 let mut column = self.token_start_column;
1294 if position <= self.token_start {
1295 return (line, column);
1296 }
1297 if let Some(summary) = self.input.position_summary(self.token_start, position) {
1298 return summary.apply(line, column);
1299 }
1300 for ch in self
1301 .input
1302 .text(TextInterval::new(self.token_start, position - 1))
1303 .chars()
1304 {
1305 if ch == '\n' {
1306 line += 1;
1307 column = 0;
1308 } else {
1309 column += 1;
1310 }
1311 }
1312 (line, column)
1313 }
1314
1315 pub fn eof_token(&self, sink: &mut TokenSink<'_>) -> Result<TokenId, TokenStoreError> {
1317 sink.push(self.eof_token_spec())
1318 }
1319
1320 pub(crate) fn emit_eof_or_pending(
1321 &mut self,
1322 sink: &mut TokenSink<'_>,
1323 ) -> Result<TokenId, TokenStoreError> {
1324 let token = self.eof_token_spec();
1325 self.emit_or_enqueue(sink, token)
1326 }
1327
1328 fn eof_token_spec(&self) -> TokenSpec {
1329 let byte_offset = self.eof_byte_offset().unwrap_or_else(|| self.input.index());
1330 TokenSpec::eof(self.input.index(), byte_offset, self.line, self.column)
1331 }
1332
1333 fn eof_byte_offset(&self) -> Option<usize> {
1334 self.byte_offset_at(self.input.index())
1335 }
1336
1337 fn token_byte_span(&self, stop: usize) -> Option<(usize, usize)> {
1338 if stop != usize::MAX && self.token_start <= stop {
1339 let (start_byte, stop_byte) = self
1340 .input
1341 .byte_interval(TextInterval::new(self.token_start, stop))?;
1342 return Some((start_byte, stop_byte));
1343 }
1344 let byte_offset = self.byte_offset_at(self.token_start)?;
1345 Some((byte_offset, byte_offset))
1346 }
1347
1348 fn byte_offset_at(&self, index: usize) -> Option<usize> {
1349 let byte_offset = if index == 0 {
1350 0
1351 } else {
1352 let previous = TextInterval::new(index - 1, index - 1);
1353 self.input.byte_interval(previous)?.1
1354 };
1355 Some(byte_offset)
1356 }
1357}
1358
1359impl<I> Recognizer for BaseLexer<I>
1360where
1361 I: CharStream,
1362{
1363 fn data(&self) -> &RecognizerData {
1364 &self.data
1365 }
1366
1367 fn data_mut(&mut self) -> &mut RecognizerData {
1368 &mut self.data
1369 }
1370}
1371
1372impl<I> Lexer for BaseLexer<I>
1373where
1374 I: CharStream,
1375{
1376 fn mode(&self) -> i32 {
1377 self.mode
1378 }
1379
1380 fn set_mode(&mut self, mode: i32) {
1381 self.mode = mode;
1382 }
1383
1384 fn push_mode(&mut self, mode: i32) {
1385 self.mode_stack.push(self.mode);
1386 self.mode = mode;
1387 }
1388
1389 fn pop_mode(&mut self) -> Option<i32> {
1390 let mode = self.mode_stack.pop()?;
1391 self.mode = mode;
1392 Some(mode)
1393 }
1394}
1395
1396impl<I> BaseLexer<I>
1397where
1398 I: CharStream,
1399{
1400 pub const fn line(&self) -> usize {
1401 self.line
1402 }
1403
1404 pub const fn column(&self) -> usize {
1405 self.column
1406 }
1407
1408 pub fn source_name(&self) -> &str {
1409 self.input.source_name()
1410 }
1411
1412 pub fn source_text(&self) -> Option<Rc<str>> {
1413 self.input.source_text()
1414 }
1415
1416 pub const fn hit_eof(&self) -> bool {
1417 self.hit_eof
1418 }
1419
1420 pub const fn set_hit_eof(&mut self, hit_eof: bool) {
1421 self.hit_eof = hit_eof;
1422 }
1423
1424 pub const fn set_force_interpreted(&mut self, force_interpreted: bool) {
1432 self.force_interpreted = force_interpreted;
1433 }
1434
1435 pub const fn force_interpreted(&self) -> bool {
1437 self.force_interpreted
1438 }
1439
1440 pub fn record_error(&self, line: usize, column: usize, message: impl Into<String>) {
1443 self.errors
1444 .borrow_mut()
1445 .push(TokenSourceError::new(line, column, message));
1446 }
1447
1448 pub fn record_semantic_error(&self, action: bool, rule_index: usize, coordinate_index: usize) {
1450 let kind = u8::from(action);
1451 if !self.semantic_error_coordinates.borrow_mut().insert((
1452 kind,
1453 rule_index,
1454 coordinate_index,
1455 self.token_start,
1456 )) {
1457 return;
1458 }
1459 let label = if action { "action" } else { "predicate" };
1460 self.record_error(
1461 self.token_start_line,
1462 self.token_start_column,
1463 format!("unhandled lexer semantic {label}: rule={rule_index} index={coordinate_index}"),
1464 );
1465 }
1466
1467 pub fn drain_errors(&mut self) -> Vec<TokenSourceError> {
1469 std::mem::take(self.errors.get_mut())
1470 }
1471
1472 pub(crate) fn lexer_prediction_store(&self) -> RefMut<'_, LexerPredictionStore> {
1475 RefMut::map(self.dfa_cache.borrow_mut(), |cache| &mut cache.prediction)
1476 }
1477
1478 pub(crate) fn reset_lexer_prediction_workspace(&self) {
1481 self.dfa_cache.borrow_mut().prediction.workspace.reset();
1482 }
1483
1484 #[cfg(test)]
1485 pub(crate) fn lexer_dfa_cache_shape(&self) -> (usize, usize, usize, usize) {
1486 let cache = self.dfa_cache.borrow();
1487 let cached_states = cache.cached_states.iter().flatten().count();
1488 let cached_transitions = cache
1489 .dense_edges
1490 .iter()
1491 .flatten()
1492 .map(|row| {
1493 row.iter()
1494 .filter(|transition| transition.target_state != usize::MAX)
1495 .count()
1496 })
1497 .sum::<usize>()
1498 + cache.sparse_edges.len();
1499 let max_configs = cache
1500 .cached_states
1501 .iter()
1502 .flatten()
1503 .map(|state| state.configs.len())
1504 .max()
1505 .unwrap_or(0);
1506 let contexts = cache.prediction.contexts.len();
1507 (cached_states, cached_transitions, max_configs, contexts)
1508 }
1509
1510 pub(crate) fn lexer_dfa_state(
1513 &self,
1514 key: LexerDfaKey,
1515 accept_prediction: Option<i32>,
1516 ) -> usize {
1517 let mut cache = self.dfa_cache.borrow_mut();
1518 let next = cache.state_numbers.len();
1519 let state = *cache.state_numbers.entry(key).or_insert(next);
1520 if let Some(prediction) = accept_prediction {
1521 cache.accept_predictions.insert(state, prediction);
1522 }
1523 state
1524 }
1525
1526 pub fn record_lexer_dfa_edge(&self, from: usize, symbol: i32, to: usize) {
1528 self.dfa_cache
1529 .borrow_mut()
1530 .edges
1531 .insert(LexerDfaEdge { from, symbol, to });
1532 }
1533
1534 pub(crate) fn cached_lexer_dfa_transition(
1535 &self,
1536 state: usize,
1537 symbol: i32,
1538 ) -> Option<LexerDfaCachedTransition> {
1539 let cache = self.dfa_cache.borrow();
1540 if let Ok(sym) = usize::try_from(symbol)
1541 && sym < DENSE_EDGE_SYMBOLS
1542 {
1543 let transition = cache.dense_edges.get(state)?.as_ref()?[sym];
1544 return (transition.target_state != usize::MAX).then_some(transition);
1545 }
1546 cache.sparse_edges.get(&(state, symbol)).copied()
1547 }
1548
1549 pub(crate) fn cache_lexer_dfa_transition(
1550 &self,
1551 state: usize,
1552 symbol: i32,
1553 transition: LexerDfaCachedTransition,
1554 ) {
1555 let mut cache = self.dfa_cache.borrow_mut();
1556 if let Ok(sym) = usize::try_from(symbol)
1557 && sym < DENSE_EDGE_SYMBOLS
1558 {
1559 if cache.dense_edges.len() <= state {
1560 cache.dense_edges.resize_with(state + 1, || None);
1561 }
1562 let row = cache.dense_edges[state]
1563 .get_or_insert_with(|| Box::new([EMPTY_DENSE_EDGE; DENSE_EDGE_SYMBOLS]));
1564 if row[sym].target_state == usize::MAX {
1566 row[sym] = transition;
1567 }
1568 return;
1569 }
1570 cache
1571 .sparse_edges
1572 .entry((state, symbol))
1573 .or_insert(transition);
1574 }
1575
1576 pub(crate) fn cached_lexer_dfa_state(&self, state: usize) -> Option<Rc<LexerDfaCachedState>> {
1577 self.dfa_cache
1578 .borrow()
1579 .cached_states
1580 .get(state)
1581 .cloned()
1582 .flatten()
1583 }
1584
1585 pub(crate) fn cache_lexer_dfa_state(&self, state: usize, cached_state: LexerDfaCachedState) {
1586 let mut cache = self.dfa_cache.borrow_mut();
1587 if cache.cached_states.len() <= state {
1588 cache.cached_states.resize_with(state + 1, || None);
1589 }
1590 cache.cached_states[state].get_or_insert_with(|| Rc::new(cached_state));
1591 }
1592
1593 pub(crate) fn cached_lexer_mode_start(&self, mode: i32) -> Option<usize> {
1594 self.dfa_cache.borrow().mode_starts.get(&mode).copied()
1595 }
1596
1597 pub(crate) fn cache_lexer_mode_start(&self, mode: i32, state: usize) {
1598 self.dfa_cache
1599 .borrow_mut()
1600 .mode_starts
1601 .entry(mode)
1602 .or_insert(state);
1603 }
1604
1605 pub fn lexer_dfa_string(&self) -> String {
1607 let mut out = String::new();
1608 let cache = self.dfa_cache.borrow();
1609 for edge in &cache.edges {
1610 let Some(label) = lexer_dfa_edge_label(edge.symbol) else {
1611 continue;
1612 };
1613 out.push_str(&self.lexer_dfa_state_string(edge.from));
1614 out.push('-');
1615 out.push_str(&label);
1616 out.push_str("->");
1617 out.push_str(&self.lexer_dfa_state_string(edge.to));
1618 out.push('\n');
1619 }
1620 out
1621 }
1622
1623 fn lexer_dfa_state_string(&self, state: usize) -> String {
1624 self.dfa_cache
1625 .borrow()
1626 .accept_predictions
1627 .get(&state)
1628 .map_or_else(
1629 || format!("s{state}"),
1630 |prediction| format!(":s{state}=>{prediction}"),
1631 )
1632 }
1633}
1634
1635fn lexer_dfa_edge_label(symbol: i32) -> Option<String> {
1636 char::from_u32(symbol.cast_unsigned()).map(|ch| format!("'{ch}'"))
1637}
1638
1639#[cfg(test)]
1640mod tests {
1641 use super::*;
1642 use crate::char_stream::InputStream;
1643 use crate::int_stream::IntStream;
1644 use crate::recognizer::RecognizerData;
1645 use crate::token::{DEFAULT_CHANNEL, Token, TokenStore};
1646 use crate::vocabulary::Vocabulary;
1647
1648 #[derive(Clone, Debug)]
1649 struct UnsharedInput(InputStream);
1650
1651 impl IntStream for UnsharedInput {
1652 fn consume(&mut self) {
1653 self.0.consume();
1654 }
1655
1656 fn la(&mut self, offset: isize) -> i32 {
1657 self.0.la(offset)
1658 }
1659
1660 fn index(&self) -> usize {
1661 self.0.index()
1662 }
1663
1664 fn seek(&mut self, index: usize) {
1665 self.0.seek(index);
1666 }
1667
1668 fn size(&self) -> usize {
1669 self.0.size()
1670 }
1671
1672 fn source_name(&self) -> &str {
1673 self.0.source_name()
1674 }
1675 }
1676
1677 impl CharStream for UnsharedInput {
1678 fn text(&self, interval: TextInterval) -> String {
1679 self.0.text(interval)
1680 }
1681
1682 fn byte_interval(&self, interval: TextInterval) -> Option<(usize, usize)> {
1683 self.0.byte_interval(interval)
1684 }
1685 }
1686
1687 #[test]
1688 fn eof_token_uses_utf8_byte_offset_after_non_ascii_input() {
1689 let data = RecognizerData::new(
1690 "T",
1691 Vocabulary::new(
1692 std::iter::empty::<Option<&str>>(),
1693 std::iter::empty::<Option<&str>>(),
1694 std::iter::empty::<Option<&str>>(),
1695 ),
1696 );
1697 let mut lexer = BaseLexer::new(InputStream::new("β"), data);
1698 lexer.consume_char();
1699
1700 let mut store = TokenStore::new(lexer.source_text(), lexer.source_name());
1701 let mut sink = TokenSink::new(&mut store);
1702 let id = lexer.eof_token(&mut sink).expect("test token should fit");
1703 let token = sink.view(id).expect("emitted token should exist");
1704
1705 assert_eq!(token.start(), 1);
1706 assert_eq!(token.stop(), 0);
1707 assert_eq!(token.text(), Some("<EOF>"));
1708 assert_eq!(token.byte_span(), 2..2);
1709 }
1710
1711 #[test]
1712 fn eof_rule_token_uses_utf8_byte_offset_after_non_ascii_input() {
1713 let data = RecognizerData::new(
1714 "T",
1715 Vocabulary::new(
1716 std::iter::empty::<Option<&str>>(),
1717 std::iter::empty::<Option<&str>>(),
1718 std::iter::empty::<Option<&str>>(),
1719 ),
1720 );
1721 let mut lexer = BaseLexer::new(InputStream::new("β"), data);
1722 lexer.consume_char();
1723 lexer.begin_token();
1724
1725 let mut store = TokenStore::new(lexer.source_text(), lexer.source_name());
1726 let mut sink = TokenSink::new(&mut store);
1727 let id = lexer
1728 .emit_with_stop(&mut sink, 1, DEFAULT_CHANNEL, 0, Some("<EOF>".to_owned()))
1729 .expect("test token should fit");
1730 let token = sink.view(id).expect("emitted token should exist");
1731
1732 assert_eq!(token.start(), 1);
1733 assert_eq!(token.stop(), 0);
1734 assert_eq!(token.text(), Some("<EOF>"));
1735 assert_eq!(token.byte_span(), 2..2);
1736 }
1737
1738 #[test]
1739 fn emit_implicit_text_uses_utf8_byte_span_for_non_ascii_input() {
1740 let data = RecognizerData::new(
1741 "T",
1742 Vocabulary::new(
1743 std::iter::empty::<Option<&str>>(),
1744 std::iter::empty::<Option<&str>>(),
1745 std::iter::empty::<Option<&str>>(),
1746 ),
1747 );
1748 let mut lexer = BaseLexer::new(InputStream::new("β"), data);
1749 lexer.begin_token();
1750 lexer.consume_char();
1751
1752 let mut store = TokenStore::new(lexer.source_text(), lexer.source_name());
1753 let mut sink = TokenSink::new(&mut store);
1754 let id = lexer
1755 .emit(&mut sink, 1, DEFAULT_CHANNEL, None)
1756 .expect("test token should fit");
1757 let token = sink.view(id).expect("emitted token should exist");
1758
1759 assert_eq!(token.start(), 0);
1760 assert_eq!(token.stop(), 0);
1761 assert_eq!(token.text(), Some("β"));
1762 assert_eq!(token.byte_span(), 0..2);
1763 }
1764
1765 #[test]
1766 fn emit_falls_back_to_explicit_text_without_shareable_source() {
1767 let data = RecognizerData::new(
1768 "T",
1769 Vocabulary::new(
1770 std::iter::empty::<Option<&str>>(),
1771 std::iter::empty::<Option<&str>>(),
1772 std::iter::empty::<Option<&str>>(),
1773 ),
1774 );
1775 let mut lexer = BaseLexer::new(UnsharedInput(InputStream::new("β")), data);
1776 lexer.begin_token();
1777 lexer.consume_char();
1778
1779 let mut store = TokenStore::new(lexer.source_text(), lexer.source_name());
1780 let mut sink = TokenSink::new(&mut store);
1781 let id = lexer
1782 .emit(&mut sink, 1, DEFAULT_CHANNEL, None)
1783 .expect("unshared input should emit explicit token text");
1784 let token = sink.view(id).expect("emitted token should exist");
1785
1786 assert_eq!(token.text(), Some("β"));
1787 assert_eq!(token.byte_span(), 0..2);
1788 }
1789
1790 #[test]
1791 fn position_commits_and_rewinds_preserve_line_and_column() {
1792 let data = RecognizerData::new(
1793 "T",
1794 Vocabulary::new(
1795 std::iter::empty::<Option<&str>>(),
1796 std::iter::empty::<Option<&str>>(),
1797 std::iter::empty::<Option<&str>>(),
1798 ),
1799 );
1800 let mut lexer = BaseLexer::new(InputStream::new("ab\nγd"), data);
1801 lexer.begin_token();
1802
1803 lexer.commit_position(0, 5);
1804 assert_eq!(lexer.input().index(), 5);
1805 assert_eq!((lexer.line(), lexer.column()), (2, 2));
1806 assert_eq!(lexer.column_at(2), 2);
1807 assert_eq!(lexer.column_at(4), 1);
1808
1809 lexer.reset_accept_position(3);
1810 assert_eq!(lexer.input().index(), 3);
1811 assert_eq!((lexer.line(), lexer.column()), (2, 0));
1812 }
1813
1814 #[test]
1815 fn custom_stream_position_commit_replays_without_fast_path_methods() {
1816 let data = RecognizerData::new(
1817 "T",
1818 Vocabulary::new(
1819 std::iter::empty::<Option<&str>>(),
1820 std::iter::empty::<Option<&str>>(),
1821 std::iter::empty::<Option<&str>>(),
1822 ),
1823 );
1824 let mut lexer = BaseLexer::new(UnsharedInput(InputStream::new("a\nb")), data);
1825 lexer.begin_token();
1826
1827 lexer.commit_position(0, 3);
1828 assert_eq!(lexer.input().index(), 3);
1829 assert_eq!((lexer.line(), lexer.column()), (2, 1));
1830 }
1831
1832 #[test]
1833 fn semantic_hook_errors_are_deduplicated_per_token_coordinate() {
1834 let data = RecognizerData::new(
1835 "T",
1836 Vocabulary::new(
1837 std::iter::empty::<Option<&str>>(),
1838 std::iter::empty::<Option<&str>>(),
1839 std::iter::empty::<Option<&str>>(),
1840 ),
1841 );
1842 let mut lexer = BaseLexer::new(InputStream::new("a"), data);
1843 lexer.begin_token();
1844 lexer.record_semantic_error(false, 3, 7);
1845 lexer.record_semantic_error(false, 3, 7);
1846
1847 let errors = lexer.drain_errors();
1848 assert_eq!(errors.len(), 1);
1849 assert_eq!(
1850 errors[0].message,
1851 "unhandled lexer semantic predicate: rule=3 index=7"
1852 );
1853
1854 lexer.begin_token();
1855 lexer.record_semantic_error(false, 3, 7);
1856 assert_eq!(
1857 lexer.drain_errors().len(),
1858 1,
1859 "deduplication resets at every token boundary, even after rewinding"
1860 );
1861 }
1862
1863 #[test]
1864 fn set_input_stream_replaces_input_and_resets_transient_state() {
1865 let data = RecognizerData::new(
1866 "T",
1867 Vocabulary::new(
1868 std::iter::empty::<Option<&str>>(),
1869 std::iter::empty::<Option<&str>>(),
1870 std::iter::empty::<Option<&str>>(),
1871 ),
1872 );
1873 let mut lexer = BaseLexer::new(InputStream::new("old"), data);
1874 lexer.consume_char();
1875 lexer.set_mode(7);
1876 lexer.push_mode(9);
1877 lexer.set_type(3);
1878 lexer.record_error(1, 0, "stale");
1879
1880 lexer.set_input_stream(InputStream::with_source_name("new", "replacement"));
1881
1882 assert_eq!(lexer.input().index(), 0);
1883 assert_eq!(lexer.input().size(), 3);
1884 assert_eq!(lexer.source_name(), "replacement");
1885 assert_eq!(lexer.source_text().as_deref(), Some("new"));
1886 assert_eq!(lexer.mode(), DEFAULT_MODE);
1887 assert_eq!(lexer.token_type(), INVALID_TOKEN_TYPE);
1888 assert_eq!((lexer.line(), lexer.column()), (1, 0));
1889 assert!(!lexer.hit_eof());
1890 assert!(lexer.drain_errors().is_empty());
1891 assert!(lexer.pop_mode().is_none());
1892 }
1893
1894 #[test]
1895 fn clear_dfa_invalidates_all_lexers_sharing_the_cache() {
1896 let atn = Box::leak(Box::new(LexerAtn::new(1)));
1897 let data = || {
1898 RecognizerData::new(
1899 "T",
1900 Vocabulary::new(
1901 std::iter::empty::<Option<&str>>(),
1902 std::iter::empty::<Option<&str>>(),
1903 std::iter::empty::<Option<&str>>(),
1904 ),
1905 )
1906 };
1907 let first = BaseLexer::new(InputStream::new("a"), data()).with_shared_dfa(atn);
1908 let second = BaseLexer::new(InputStream::new("a"), data()).with_shared_dfa(atn);
1909 let state = first.lexer_dfa_state(LexerDfaKey::new(Vec::new()), Some(1));
1910 first.record_lexer_dfa_edge(state, i32::from(b'a'), state);
1911
1912 assert!(!second.lexer_dfa_string().is_empty());
1913 first.clear_dfa();
1914 assert!(first.lexer_dfa_string().is_empty());
1915 assert!(second.lexer_dfa_string().is_empty());
1916 }
1917}