use std::cell::RefCell;
use std::collections::{BTreeSet, HashMap};
use std::hash::BuildHasherDefault;
use std::rc::Rc;
use crate::atn::Atn;
use crate::char_stream::{CharStream, TextInterval};
use crate::int_stream::EOF;
use crate::prediction::PredictionFxHasher;
use crate::recognizer::{Recognizer, RecognizerData};
use crate::token::{CommonToken, CommonTokenFactory, TokenFactory, TokenSourceError, TokenSpec};
#[allow(clippy::disallowed_types)]
type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<PredictionFxHasher>>;
pub const SKIP: i32 = -3;
pub const MORE: i32 = -2;
pub const DEFAULT_MODE: i32 = 0;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct LexerMode(pub i32);
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct LexerCustomAction {
rule_index: i32,
action_index: i32,
position: usize,
}
impl LexerCustomAction {
pub const fn new(rule_index: i32, action_index: i32, position: usize) -> Self {
Self {
rule_index,
action_index,
position,
}
}
pub const fn rule_index(self) -> i32 {
self.rule_index
}
pub const fn action_index(self) -> i32 {
self.action_index
}
pub const fn position(self) -> usize {
self.position
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct LexerPredicate {
rule_index: usize,
pred_index: usize,
position: usize,
}
impl LexerPredicate {
pub const fn new(rule_index: usize, pred_index: usize, position: usize) -> Self {
Self {
rule_index,
pred_index,
position,
}
}
pub const fn rule_index(self) -> usize {
self.rule_index
}
pub const fn pred_index(self) -> usize {
self.pred_index
}
pub const fn position(self) -> usize {
self.position
}
}
#[derive(Debug)]
enum LexerRef<'a, I, F>
where
I: CharStream,
F: TokenFactory,
{
Shared(&'a BaseLexer<I, F>),
Mut(&'a mut BaseLexer<I, F>),
}
impl<I, F> LexerRef<'_, I, F>
where
I: CharStream,
F: TokenFactory,
{
const fn get(&self) -> &BaseLexer<I, F> {
match self {
LexerRef::Shared(lexer) => lexer,
LexerRef::Mut(lexer) => lexer,
}
}
}
#[derive(Debug)]
pub struct LexerSemCtx<'a, I, F = CommonTokenFactory>
where
I: CharStream,
F: TokenFactory,
{
lexer: LexerRef<'a, I, F>,
rule_index: usize,
coordinate_index: usize,
position: usize,
}
impl<'a, I, F> LexerSemCtx<'a, I, F>
where
I: CharStream,
F: TokenFactory,
{
pub(crate) const fn new(
lexer: &'a BaseLexer<I, F>,
rule_index: usize,
coordinate_index: usize,
position: usize,
) -> Self {
Self {
lexer: LexerRef::Shared(lexer),
rule_index,
coordinate_index,
position,
}
}
pub(crate) const fn new_mut(
lexer: &'a mut BaseLexer<I, F>,
rule_index: usize,
coordinate_index: usize,
position: usize,
) -> Self {
Self {
lexer: LexerRef::Mut(lexer),
rule_index,
coordinate_index,
position,
}
}
#[must_use]
pub const fn rule_index(&self) -> usize {
self.rule_index
}
#[must_use]
pub const fn coordinate_index(&self) -> usize {
self.coordinate_index
}
#[must_use]
pub const fn position(&self) -> usize {
self.position
}
#[must_use]
pub fn mode(&self) -> i32 {
self.lexer.get().mode()
}
#[must_use]
pub const fn column(&self) -> usize {
self.lexer.get().column()
}
#[must_use]
pub fn position_column(&self) -> usize {
self.lexer.get().column_at(self.position)
}
#[must_use]
pub const fn token_start_column(&self) -> usize {
self.lexer.get().token_start_column()
}
#[must_use]
pub fn text_so_far(&self) -> String {
self.lexer.get().token_text_until(self.position)
}
pub fn set_mode(&mut self, mode: i32) -> bool {
match &mut self.lexer {
LexerRef::Mut(lexer) => {
lexer.set_mode(mode);
true
}
LexerRef::Shared(_) => false,
}
}
pub fn push_mode(&mut self, mode: i32) -> bool {
match &mut self.lexer {
LexerRef::Mut(lexer) => {
lexer.push_mode(mode);
true
}
LexerRef::Shared(_) => false,
}
}
pub fn pop_mode(&mut self) -> Option<i32> {
match &mut self.lexer {
LexerRef::Mut(lexer) => lexer.pop_mode(),
LexerRef::Shared(_) => None,
}
}
}
pub trait Lexer: Recognizer {
fn mode(&self) -> i32;
fn set_mode(&mut self, mode: i32);
fn push_mode(&mut self, mode: i32);
fn pop_mode(&mut self) -> Option<i32>;
}
#[derive(Clone, Debug)]
pub struct BaseLexer<I, F = CommonTokenFactory> {
input: I,
data: RecognizerData,
factory: F,
mode: i32,
mode_stack: Vec<i32>,
token_start: usize,
token_start_line: usize,
token_start_column: usize,
line: usize,
column: usize,
hit_eof: bool,
force_interpreted: bool,
errors: Vec<TokenSourceError>,
dfa_cache: Rc<RefCell<LexerDfaCache>>,
}
#[derive(Clone, Debug, Default)]
struct LexerDfaCache {
state_numbers: FxHashMap<LexerDfaKey, usize>,
accept_predictions: FxHashMap<usize, i32>,
edges: BTreeSet<LexerDfaEdge>,
cached_states: Vec<Option<Rc<LexerDfaCachedState>>>,
dense_edges: Vec<Option<Box<DenseEdgeRow>>>,
sparse_edges: FxHashMap<(usize, i32), LexerDfaCachedTransition>,
mode_starts: FxHashMap<i32, usize>,
}
const DENSE_EDGE_SYMBOLS: usize = 128;
type DenseEdgeRow = [LexerDfaCachedTransition; DENSE_EDGE_SYMBOLS];
const EMPTY_DENSE_EDGE: LexerDfaCachedTransition = LexerDfaCachedTransition {
target_state: usize::MAX,
position_delta: 0,
};
thread_local! {
static SHARED_LEXER_DFA_CACHES: RefCell<HashMap<usize, Rc<RefCell<LexerDfaCache>>>> =
RefCell::new(HashMap::new());
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) struct LexerDfaKey {
configs: Vec<LexerDfaConfigKey>,
}
impl LexerDfaKey {
pub(crate) fn new(mut configs: Vec<LexerDfaConfigKey>) -> Self {
configs.sort_unstable();
Self { configs }
}
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) struct LexerDfaConfigKey {
pub(crate) state: usize,
pub(crate) alt_rule_index: Option<usize>,
pub(crate) consumed_eof: bool,
pub(crate) passed_non_greedy: bool,
pub(crate) stack: Vec<usize>,
pub(crate) actions: Vec<LexerDfaActionKey>,
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) struct LexerDfaActionKey {
pub(crate) action_index: usize,
pub(crate) position_delta: usize,
pub(crate) rule_index: usize,
}
impl LexerDfaConfigKey {
pub(crate) const fn new(
state: usize,
alt_rule_index: Option<usize>,
consumed_eof: bool,
passed_non_greedy: bool,
stack: Vec<usize>,
actions: Vec<LexerDfaActionKey>,
) -> Self {
Self {
state,
alt_rule_index,
consumed_eof,
passed_non_greedy,
stack,
actions,
}
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct LexerDfaCachedTransition {
pub(crate) target_state: usize,
pub(crate) position_delta: usize,
}
#[derive(Clone, Debug)]
pub(crate) struct LexerDfaCachedAccept {
pub(crate) position_delta: usize,
pub(crate) rule_index: usize,
pub(crate) consumed_eof: bool,
pub(crate) actions: Vec<LexerDfaActionKey>,
}
#[derive(Clone, Debug)]
pub(crate) struct LexerDfaCachedState {
pub(crate) has_semantic_context: bool,
pub(crate) configs: Vec<LexerDfaConfigKey>,
pub(crate) accept: Option<LexerDfaCachedAccept>,
}
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
struct LexerDfaEdge {
from: usize,
symbol: i32,
to: usize,
}
impl<I> BaseLexer<I>
where
I: CharStream,
{
pub fn new(input: I, data: RecognizerData) -> Self {
Self::with_factory(input, data, CommonTokenFactory)
}
}
impl<I, F> BaseLexer<I, F>
where
I: CharStream,
F: TokenFactory,
{
pub fn with_factory(input: I, data: RecognizerData, factory: F) -> Self {
Self {
input,
data,
factory,
mode: DEFAULT_MODE,
mode_stack: Vec::new(),
token_start: 0,
token_start_line: 1,
token_start_column: 0,
line: 1,
column: 0,
hit_eof: false,
force_interpreted: false,
errors: Vec::new(),
dfa_cache: Rc::new(RefCell::new(LexerDfaCache::default())),
}
}
#[must_use]
pub fn with_shared_dfa(mut self, atn: &'static Atn) -> Self {
let ptr: *const Atn = atn;
let key = ptr as usize;
self.dfa_cache = SHARED_LEXER_DFA_CACHES
.with(|caches| Rc::clone(caches.borrow_mut().entry(key).or_insert_with(Rc::default)));
self
}
pub const fn input(&self) -> &I {
&self.input
}
pub const fn input_mut(&mut self) -> &mut I {
&mut self.input
}
pub fn begin_token(&mut self) {
self.token_start = self.input.index();
self.token_start_line = self.line;
self.token_start_column = self.column;
}
pub const fn token_start(&self) -> usize {
self.token_start
}
pub const fn token_start_line(&self) -> usize {
self.token_start_line
}
pub const fn token_start_column(&self) -> usize {
self.token_start_column
}
pub fn consume_char(&mut self) {
let la = self.input.la(1);
if la == EOF {
return;
}
self.input.consume();
if char::from_u32(la.cast_unsigned()) == Some('\n') {
self.line += 1;
self.column = 0;
} else {
self.column += 1;
}
}
pub fn reset_accept_position(&mut self, index: usize) {
let target = index.max(self.token_start);
self.input.seek(self.token_start);
self.line = self.token_start_line;
self.column = self.token_start_column;
while self.input.index() < target && self.input.la(1) != EOF {
self.consume_char();
}
}
pub fn emit(&self, token_type: i32, channel: i32, text: Option<String>) -> CommonToken {
let stop = self.input.index().checked_sub(1).unwrap_or(usize::MAX);
self.emit_with_stop(token_type, channel, stop, text)
}
pub fn emit_with_stop(
&self,
token_type: i32,
channel: i32,
stop: usize,
text: Option<String>,
) -> CommonToken {
let text = text.or_else(|| {
if stop == usize::MAX {
Some("<EOF>".to_owned())
} else {
None
}
});
let source_interval = if text.is_none() && stop != usize::MAX && self.token_start <= stop {
self.input
.text_source_interval(TextInterval::new(self.token_start, stop))
} else {
None
};
let source_text = source_interval
.as_ref()
.and_then(|(input, start_byte, stop_byte)| {
Some(crate::token::TokenSourceText {
input: Rc::clone(input),
start_byte: u32::try_from(*start_byte).ok()?,
stop_byte: u32::try_from(*stop_byte).ok()?,
})
});
let source_byte_span = source_text
.as_ref()
.map(|source_text| (source_text.start_byte, source_text.stop_byte));
let text = text.or_else(|| {
source_text
.is_none()
.then(|| self.input.text(TextInterval::new(self.token_start, stop)))
});
let mut token = self.factory.create(TokenSpec {
token_type,
channel,
start: self.token_start,
stop,
line: self.token_start_line,
column: self.token_start_column,
text,
source_text,
source_name: self.input.source_name(),
});
if let Some((start_byte, stop_byte)) =
source_byte_span.or_else(|| self.token_byte_span(stop))
{
token = token.with_byte_span(start_byte, stop_byte);
}
token
}
pub fn token_text(&self) -> String {
self.token_text_until(self.input.index())
}
pub fn token_text_until(&self, stop_exclusive: usize) -> String {
if stop_exclusive <= self.token_start {
return String::new();
}
self.input
.text(TextInterval::new(self.token_start, stop_exclusive - 1))
}
pub fn column_at(&self, position: usize) -> usize {
let mut column = self.token_start_column;
if position <= self.token_start {
return column;
}
for ch in self
.input
.text(TextInterval::new(self.token_start, position - 1))
.chars()
{
if ch == '\n' {
column = 0;
} else {
column += 1;
}
}
column
}
pub fn eof_token(&self) -> CommonToken {
let token = CommonToken::eof(
self.input.source_name(),
self.input.index(),
self.line,
self.column,
);
match self.eof_byte_offset() {
Some(byte_offset) => token.with_byte_span(byte_offset, byte_offset),
None => token,
}
}
fn eof_byte_offset(&self) -> Option<u32> {
self.byte_offset_at(self.input.index())
}
fn token_byte_span(&self, stop: usize) -> Option<(u32, u32)> {
if stop != usize::MAX && self.token_start <= stop {
let (_, start_byte, stop_byte) = self
.input
.text_source_interval(TextInterval::new(self.token_start, stop))?;
return Some((
u32::try_from(start_byte).ok()?,
u32::try_from(stop_byte).ok()?,
));
}
let byte_offset = self.byte_offset_at(self.token_start)?;
Some((byte_offset, byte_offset))
}
fn byte_offset_at(&self, index: usize) -> Option<u32> {
let byte_offset = if index == 0 {
0
} else {
let previous = TextInterval::new(index - 1, index - 1);
self.input.text_source_interval(previous)?.2
};
u32::try_from(byte_offset).ok()
}
}
impl<I, F> Recognizer for BaseLexer<I, F>
where
I: CharStream,
F: TokenFactory,
{
fn data(&self) -> &RecognizerData {
&self.data
}
fn data_mut(&mut self) -> &mut RecognizerData {
&mut self.data
}
}
impl<I, F> Lexer for BaseLexer<I, F>
where
I: CharStream,
F: TokenFactory,
{
fn mode(&self) -> i32 {
self.mode
}
fn set_mode(&mut self, mode: i32) {
self.mode = mode;
}
fn push_mode(&mut self, mode: i32) {
self.mode_stack.push(self.mode);
self.mode = mode;
}
fn pop_mode(&mut self) -> Option<i32> {
let mode = self.mode_stack.pop()?;
self.mode = mode;
Some(mode)
}
}
impl<I, F> BaseLexer<I, F>
where
I: CharStream,
F: TokenFactory,
{
pub const fn line(&self) -> usize {
self.line
}
pub const fn column(&self) -> usize {
self.column
}
pub fn source_name(&self) -> &str {
self.input.source_name()
}
pub const fn hit_eof(&self) -> bool {
self.hit_eof
}
pub const fn set_hit_eof(&mut self, hit_eof: bool) {
self.hit_eof = hit_eof;
}
pub const fn set_force_interpreted(&mut self, force_interpreted: bool) {
self.force_interpreted = force_interpreted;
}
pub const fn force_interpreted(&self) -> bool {
self.force_interpreted
}
pub fn record_error(&mut self, line: usize, column: usize, message: impl Into<String>) {
self.errors
.push(TokenSourceError::new(line, column, message));
}
pub fn drain_errors(&mut self) -> Vec<TokenSourceError> {
std::mem::take(&mut self.errors)
}
pub(crate) fn lexer_dfa_state(
&self,
key: LexerDfaKey,
accept_prediction: Option<i32>,
) -> usize {
let mut cache = self.dfa_cache.borrow_mut();
let next = cache.state_numbers.len();
let state = *cache.state_numbers.entry(key).or_insert(next);
if let Some(prediction) = accept_prediction {
cache.accept_predictions.insert(state, prediction);
}
state
}
pub fn record_lexer_dfa_edge(&self, from: usize, symbol: i32, to: usize) {
self.dfa_cache
.borrow_mut()
.edges
.insert(LexerDfaEdge { from, symbol, to });
}
pub(crate) fn cached_lexer_dfa_transition(
&self,
state: usize,
symbol: i32,
) -> Option<LexerDfaCachedTransition> {
let cache = self.dfa_cache.borrow();
if let Ok(sym) = usize::try_from(symbol)
&& sym < DENSE_EDGE_SYMBOLS
{
let transition = cache.dense_edges.get(state)?.as_ref()?[sym];
return (transition.target_state != usize::MAX).then_some(transition);
}
cache.sparse_edges.get(&(state, symbol)).copied()
}
pub(crate) fn cache_lexer_dfa_transition(
&self,
state: usize,
symbol: i32,
transition: LexerDfaCachedTransition,
) {
let mut cache = self.dfa_cache.borrow_mut();
if let Ok(sym) = usize::try_from(symbol)
&& sym < DENSE_EDGE_SYMBOLS
{
if cache.dense_edges.len() <= state {
cache.dense_edges.resize_with(state + 1, || None);
}
let row = cache.dense_edges[state]
.get_or_insert_with(|| Box::new([EMPTY_DENSE_EDGE; DENSE_EDGE_SYMBOLS]));
if row[sym].target_state == usize::MAX {
row[sym] = transition;
}
return;
}
cache
.sparse_edges
.entry((state, symbol))
.or_insert(transition);
}
pub(crate) fn cached_lexer_dfa_state(&self, state: usize) -> Option<Rc<LexerDfaCachedState>> {
self.dfa_cache
.borrow()
.cached_states
.get(state)
.cloned()
.flatten()
}
pub(crate) fn cache_lexer_dfa_state(&self, state: usize, cached_state: LexerDfaCachedState) {
let mut cache = self.dfa_cache.borrow_mut();
if cache.cached_states.len() <= state {
cache.cached_states.resize_with(state + 1, || None);
}
cache.cached_states[state].get_or_insert_with(|| Rc::new(cached_state));
}
pub(crate) fn cached_lexer_mode_start(&self, mode: i32) -> Option<usize> {
self.dfa_cache.borrow().mode_starts.get(&mode).copied()
}
pub(crate) fn cache_lexer_mode_start(&self, mode: i32, state: usize) {
self.dfa_cache
.borrow_mut()
.mode_starts
.entry(mode)
.or_insert(state);
}
pub fn lexer_dfa_string(&self) -> String {
let mut out = String::new();
let cache = self.dfa_cache.borrow();
for edge in &cache.edges {
let Some(label) = lexer_dfa_edge_label(edge.symbol) else {
continue;
};
out.push_str(&self.lexer_dfa_state_string(edge.from));
out.push('-');
out.push_str(&label);
out.push_str("->");
out.push_str(&self.lexer_dfa_state_string(edge.to));
out.push('\n');
}
out
}
fn lexer_dfa_state_string(&self, state: usize) -> String {
self.dfa_cache
.borrow()
.accept_predictions
.get(&state)
.map_or_else(
|| format!("s{state}"),
|prediction| format!(":s{state}=>{prediction}"),
)
}
}
fn lexer_dfa_edge_label(symbol: i32) -> Option<String> {
char::from_u32(symbol.cast_unsigned()).map(|ch| format!("'{ch}'"))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::char_stream::InputStream;
use crate::recognizer::RecognizerData;
use crate::token::{DEFAULT_CHANNEL, Token};
use crate::vocabulary::Vocabulary;
#[test]
fn eof_token_uses_utf8_byte_offset_after_non_ascii_input() {
let data = RecognizerData::new(
"T",
Vocabulary::new(
std::iter::empty::<Option<&str>>(),
std::iter::empty::<Option<&str>>(),
std::iter::empty::<Option<&str>>(),
),
);
let mut lexer = BaseLexer::new(InputStream::new("β"), data);
lexer.consume_char();
let token = lexer.eof_token();
assert_eq!(token.start(), 1);
assert_eq!(token.stop(), 0);
assert_eq!(token.text(), "<EOF>");
assert_eq!(token.byte_span(), 2..2);
}
#[test]
fn eof_rule_token_uses_utf8_byte_offset_after_non_ascii_input() {
let data = RecognizerData::new(
"T",
Vocabulary::new(
std::iter::empty::<Option<&str>>(),
std::iter::empty::<Option<&str>>(),
std::iter::empty::<Option<&str>>(),
),
);
let mut lexer = BaseLexer::new(InputStream::new("β"), data);
lexer.consume_char();
lexer.begin_token();
let token = lexer.emit_with_stop(1, DEFAULT_CHANNEL, 0, Some("<EOF>".to_owned()));
assert_eq!(token.start(), 1);
assert_eq!(token.stop(), 0);
assert_eq!(token.text(), "<EOF>");
assert_eq!(token.byte_span(), 2..2);
}
#[test]
fn emit_implicit_text_uses_utf8_byte_span_for_non_ascii_input() {
let data = RecognizerData::new(
"T",
Vocabulary::new(
std::iter::empty::<Option<&str>>(),
std::iter::empty::<Option<&str>>(),
std::iter::empty::<Option<&str>>(),
),
);
let mut lexer = BaseLexer::new(InputStream::new("β"), data);
lexer.begin_token();
lexer.consume_char();
let token = lexer.emit(1, DEFAULT_CHANNEL, None);
assert_eq!(token.start(), 0);
assert_eq!(token.stop(), 0);
assert_eq!(token.text(), "β");
assert_eq!(token.byte_span(), 0..2);
}
}