use std::collections::HashMap;
use std::sync::Arc;
use tabnas::{Context, ImperativeLexMatcher, Lexer, Rule, Tin, Token, Value};
use crate::state::{with_state, LexMode};
use crate::tokens::{is_reserved, keyword_token_name_unchecked};
#[derive(Debug, Clone, Default)]
pub struct Tins {
map: HashMap<String, Tin>,
}
impl Tins {
pub fn insert(&mut self, name: &str, tin: Tin) {
self.map.insert(name.to_string(), tin);
}
pub fn get(&self, name: &str) -> Tin {
self.map.get(name).copied().unwrap_or(-1)
}
}
fn emit(lexer: &mut Lexer<'_>, tins: &Tins, name: &str, consumed: usize) -> Option<Token> {
let text: String = lexer.remaining().chars().take(consumed).collect();
if text.is_empty() {
return None;
}
let point = lexer.point();
let token = lexer.token(
name,
tins.get(name),
Value::String(text.clone()),
text.as_str(),
point,
);
lexer.advance_chars(text.chars().count());
Some(token)
}
fn bad(lexer: &Lexer<'_>, code: &str, from_chars: usize, to_chars: usize) -> Option<Token> {
let base = lexer.point().site.pos;
Some(lexer.bad_span(code, base + from_chars, base + to_chars))
}
pub(crate) struct Look<'a> {
chars: std::str::Chars<'a>,
read: Vec<char>,
}
impl<'a> Look<'a> {
fn new(text: &'a str) -> Self {
Look {
chars: text.chars(),
read: Vec::new(),
}
}
fn get(&mut self, index: usize) -> Option<char> {
while self.read.len() <= index {
self.read.push(self.chars.next()?);
}
Some(self.read[index])
}
fn at(&mut self, index: usize) -> char {
self.get(index).unwrap_or('\0')
}
fn slice(&mut self, from: usize, to: usize) -> &[char] {
let _ = self.get(to.saturating_sub(1));
let end = to.min(self.read.len());
&self.read[from.min(end)..end]
}
fn count(&mut self) -> usize {
self.read.extend(self.chars.by_ref());
self.read.len()
}
}
pub(crate) enum Cut {
No,
Edge,
Hit(String, usize, Option<LexMode>),
Bad(&'static str, usize, usize),
}
pub(crate) struct Scope<'a> {
last: bool,
mode: &'a LexMode,
before: &'a str,
windowed: bool,
pos: usize,
ci: usize,
}
impl Scope<'_> {
fn short(&self, look: &mut Look<'_>, index: usize, n: usize) -> bool {
!self.last && n > 0 && look.get(index + n - 1).is_none()
}
}
fn hit(name: &str, consumed: usize) -> Cut {
Cut::Hit(name.to_string(), consumed, None)
}
fn mode_for(pos: usize) -> LexMode {
with_state(|state| {
if let Some((at, before)) = &state.mode_at {
if *at == pos {
state.mode = before.clone();
}
}
state.mode.clone()
})
}
fn cut(
lexer: &mut Lexer<'_>,
tins: &Tins,
scan: &dyn Fn(&mut Look<'_>, &Scope<'_>) -> Cut,
) -> Option<Token> {
let point = lexer.point();
let pos = point.site.pos;
let mode = mode_for(pos);
let windowed = lexer.windowed();
let mut last = !windowed;
loop {
let (answer, held) = {
let source = lexer.source();
let window = lexer.window();
let before = &source[..source.len() - window.len()];
let scope = Scope {
last,
mode: &mode,
before,
windowed,
pos,
ci: point.site.ci,
};
let mut look = Look::new(window);
let answer = scan(&mut look, &scope);
(answer, window.len())
};
match answer {
Cut::Edge => {
let chars = lexer.window().chars().count();
lexer.fill((chars + 1).max(2 * chars));
if lexer.window().len() <= held {
last = true;
}
}
Cut::No => return None,
Cut::Bad(code, from, to) => return bad(lexer, code, from, to),
Cut::Hit(name, consumed, after) => {
if let Some(after) = after {
with_state(|state| {
state.mode_at = Some((pos, state.mode.clone()));
state.mode = after;
});
}
return emit(lexer, tins, &name, consumed);
}
}
}
}
fn whitespace(look: &mut Look<'_>, scope: &Scope<'_>) -> Cut {
let Some(first) = look.get(0) else {
return if scope.last { Cut::No } else { Cut::Edge };
};
if !matches!(first, ' ' | '\t' | '\u{0b}' | '\u{0c}' | '\r' | '\n') {
return Cut::No;
}
let mut index = 0;
loop {
match look.get(index) {
Some(' ' | '\t' | '\u{0b}' | '\u{0c}' | '\r') => index += 1,
Some('\n') => {
if scope.mode.in_directive {
break;
}
index += 1;
}
Some(_) => break,
None => {
if !scope.last {
return Cut::Edge;
}
break;
}
}
}
if index == 0 {
return Cut::No;
}
hit("#SP", index)
}
fn line_cont(look: &mut Look<'_>, scope: &Scope<'_>) -> Cut {
if scope.short(look, 0, 1) {
return Cut::Edge;
}
if look.at(0) != '\\' {
return Cut::No;
}
if scope.short(look, 0, 3) {
return Cut::Edge;
}
let consumed = if look.at(1) == '\n' {
2
} else if look.at(1) == '\r' && look.at(2) == '\n' {
3
} else if look.at(1) == '\r' {
2
} else {
return Cut::No;
};
hit("TRIVIA_LINE_CONT", consumed)
}
fn line_comment(look: &mut Look<'_>, scope: &Scope<'_>) -> Cut {
if scope.short(look, 0, 2) {
return Cut::Edge;
}
if look.at(0) != '/' || look.at(1) != '/' {
return Cut::No;
}
let mut index = 2;
loop {
match look.get(index) {
Some('\n' | '\r') => break,
Some(_) => index += 1,
None => {
if !scope.last {
return Cut::Edge;
}
break;
}
}
}
hit("TRIVIA_LINE_COMMENT", index)
}
fn block_comment(look: &mut Look<'_>, scope: &Scope<'_>) -> Cut {
if scope.short(look, 0, 2) {
return Cut::Edge;
}
if look.at(0) != '/' || look.at(1) != '*' {
return Cut::No;
}
let mut index = 2;
while look.get(index + 1).is_some() {
if look.at(index) == '*' && look.at(index + 1) == '/' {
return hit("TRIVIA_BLOCK_COMMENT", index + 2);
}
index += 1;
}
if !scope.last {
return Cut::Edge;
}
Cut::Bad("unterminated_comment", 0, look.count())
}
fn at_line_start(scope: &Scope<'_>) -> bool {
let mut spaces = 0;
for character in scope.before.chars().rev() {
match character {
'\n' | '\r' => return true,
' ' | '\t' => spaces += 1,
_ => return false,
}
}
if !scope.windowed || scope.ci == spaces + 1 {
return true;
}
let low = scope.pos - spaces;
low == 0 || scope.mode.pp_newline_end == Some(low)
}
fn pp_directive_open(look: &mut Look<'_>, scope: &Scope<'_>) -> Cut {
if scope.short(look, 0, 2) {
return Cut::Edge;
}
let consumed = if look.at(0) == '#' {
1
} else if look.at(0) == '%' && look.at(1) == ':' {
2
} else {
return Cut::No;
};
if !at_line_start(scope) {
return Cut::No;
}
let mut mode = scope.mode.clone();
mode.in_directive = true;
mode.directive_name = None;
mode.expect_header_name = false;
Cut::Hit("PP_HASH".to_string(), consumed, Some(mode))
}
fn pp_newline(look: &mut Look<'_>, scope: &Scope<'_>) -> Cut {
if !scope.mode.in_directive {
return Cut::No;
}
if scope.short(look, 0, 2) {
return Cut::Edge;
}
let first = look.at(0);
if first != '\n' && first != '\r' {
return Cut::No;
}
let consumed = if first == '\r' && look.at(1) == '\n' {
2
} else {
1
};
let mut mode = scope.mode.clone();
mode.in_directive = false;
mode.directive_name = None;
mode.expect_header_name = false;
mode.pp_newline_end = Some(scope.pos + consumed);
Cut::Hit("PP_NEWLINE".to_string(), consumed, Some(mode))
}
fn header_name(look: &mut Look<'_>, scope: &Scope<'_>) -> Cut {
if !(scope.mode.in_directive && scope.mode.expect_header_name) {
return Cut::No;
}
if scope.short(look, 0, 1) {
return Cut::Edge;
}
let close = match look.at(0) {
'<' => '>',
'"' => '"',
_ => return Cut::No,
};
let mut index = 1;
while let Some(character) = look.get(index) {
if character == '\n' {
return Cut::Bad("unterminated_header_name", 0, index);
}
if character == close {
let mut mode = scope.mode.clone();
mode.expect_header_name = false;
return Cut::Hit("LIT_HEADER_NAME".to_string(), index + 1, Some(mode));
}
index += 1;
}
if !scope.last {
return Cut::Edge;
}
Cut::Bad("unterminated_header_name", 0, index)
}
fn is_id_start(character: char) -> bool {
character.is_ascii_alphabetic() || character == '_' || character == '$'
}
fn is_id_part(character: char) -> bool {
character.is_ascii_alphanumeric() || character == '_' || character == '$'
}
fn identifier(look: &mut Look<'_>, scope: &Scope<'_>) -> Cut {
if scope.short(look, 0, 1) {
return Cut::Edge;
}
if !is_id_start(look.at(0)) {
return Cut::No;
}
let mut index = 1;
loop {
match look.get(index) {
Some(character) if is_id_part(character) => index += 1,
Some(_) => break,
None => {
if !scope.last {
return Cut::Edge;
}
break;
}
}
}
let word: String = look.slice(0, index).iter().collect();
let mode = scope.mode;
if is_reserved(&word) {
let name = keyword_token_name_unchecked(&word);
if mode.in_directive && mode.directive_name.is_none() {
let mut after = mode.clone();
after.directive_name = Some(word);
return Cut::Hit(name, index, Some(after));
}
return Cut::Hit(name, index, None);
}
if mode.in_directive && mode.directive_name.is_none() {
let mut after = mode.clone();
if word == "include" || word == "embed" || word == "include_next" {
after.expect_header_name = true;
}
after.directive_name = Some(word);
return Cut::Hit("ID".to_string(), index, Some(after));
}
let name = with_state(|state| {
if !mode.in_directive && state.symbols.is_typedef(&word) {
"TYPEDEF_NAME"
} else if !mode.in_directive && state.macros.has(&word) {
"MACRO_NAME"
} else {
"ID"
}
});
hit(name, index)
}
fn number_run(look: &mut Look<'_>) -> Option<usize> {
let mut index = 0;
loop {
let character = look.get(index)?;
if character.is_ascii_alphanumeric() || matches!(character, '_' | '.' | '\'') {
index += 1;
continue;
}
if matches!(character, '+' | '-')
&& index > 0
&& matches!(look.at(index - 1), 'e' | 'E' | 'p' | 'P')
{
index += 1;
continue;
}
return Some(index);
}
}
fn number_text<'l>(look: &'l mut Look<'_>, scope: &Scope<'_>) -> Result<&'l [char], Cut> {
let run = match number_run(look) {
Some(run) => run,
None if scope.last => look.count(),
None => return Err(Cut::Edge),
};
Ok(look.slice(0, run + 1))
}
fn digit_run(text: &[char], from: usize, accept: impl Fn(char) -> bool + Copy) -> usize {
if from >= text.len() || !accept(text[from]) {
return 0;
}
let mut index = from + 1;
while index < text.len() && (text[index] == '\'' || accept(text[index])) {
index += 1;
}
index - from
}
fn is_octal(character: char) -> bool {
('0'..='7').contains(&character)
}
fn is_binary(character: char) -> bool {
character == '0' || character == '1'
}
fn scan_integer(text: &[char]) -> Option<(usize, usize)> {
let number = if at(text, 0) == '0' && matches!(at(text, 1), 'x' | 'X') {
let run = digit_run(text, 2, |character| character.is_ascii_hexdigit());
if run == 0 {
0
} else {
2 + run
}
} else {
0
};
let number = if number > 0 {
number
} else if at(text, 0) == '0' && matches!(at(text, 1), 'b' | 'B') {
let run = digit_run(text, 2, is_binary);
if run == 0 {
0
} else {
2 + run
}
} else {
0
};
let number = if number > 0 {
number
} else if at(text, 0) == '0' {
let mut index = 1;
while index < text.len() && (text[index] == '\'' || is_octal(text[index])) {
index += 1;
}
index
} else if ('1'..='9').contains(&at(text, 0)) {
digit_run(text, 0, |character| character.is_ascii_digit())
} else {
0
};
if number == 0 {
return None;
}
let mut index = number;
while index < text.len() && matches!(text[index], 'u' | 'U' | 'l' | 'L') {
index += 1;
}
while index < text.len() && matches!(text[index], 'w' | 'W' | 'b' | 'B' | 'z' | 'Z') {
index += 1;
}
while index < text.len() && matches!(text[index], 'u' | 'U' | 'l' | 'L') {
index += 1;
}
Some((number, index))
}
fn integer(look: &mut Look<'_>, scope: &Scope<'_>) -> Cut {
if scope.short(look, 0, 1) {
return Cut::Edge;
}
if !look.at(0).is_ascii_digit() {
return Cut::No;
}
let text = match number_text(look, scope) {
Ok(text) => text,
Err(edge) => return edge,
};
let Some((_, whole)) = scan_integer(text) else {
return Cut::No;
};
let hex = at(text, 0) == '0' && matches!(at(text, 1), 'x' | 'X');
let after = at(text, whole);
if after == '.' || after == 'e' || after == 'E' {
if !hex {
return Cut::No;
}
}
if hex && (after == '.' || after == 'p' || after == 'P') {
return Cut::No;
}
hit("LIT_INT", whole)
}
fn scan_float_hex(text: &[char]) -> Option<usize> {
if at(text, 0) != '0' || !matches!(at(text, 1), 'x' | 'X') {
return None;
}
let hexit = |character: char| character.is_ascii_hexdigit();
let mantissa = {
let run = digit_run(text, 2, hexit);
if run > 0 && at(text, 2 + run) == '.' {
let fraction = digit_run(text, 2 + run + 1, hexit);
Some(2 + run + 1 + fraction)
} else if at(text, 2) == '.' {
let fraction = digit_run(text, 3, hexit);
if fraction == 0 {
None
} else {
Some(3 + fraction)
}
} else if run > 0 {
Some(2 + run)
} else {
None
}
}?;
let mut index = mantissa;
if !matches!(at(text, index), 'p' | 'P') {
return None;
}
index += 1;
if matches!(at(text, index), '+' | '-') {
index += 1;
}
let exponent = digit_run(text, index, |character| character.is_ascii_digit());
if exponent == 0 {
return None;
}
index += exponent;
if matches!(at(text, index), 'f' | 'F' | 'l' | 'L') {
index += 1;
}
Some(index)
}
fn decimal_exponent(text: &[char], from: usize) -> usize {
if !matches!(at(text, from), 'e' | 'E') {
return 0;
}
let mut index = from + 1;
if matches!(at(text, index), '+' | '-') {
index += 1;
}
let digits = digit_run(text, index, |character| character.is_ascii_digit());
if digits == 0 {
return 0;
}
index + digits - from
}
fn scan_float_dec(text: &[char]) -> Option<usize> {
let digit = |character: char| character.is_ascii_digit();
let lead = digit_run(text, 0, digit);
for start in [lead, 0] {
if start > 0 && lead == 0 {
continue;
}
if at(text, start) == '.' {
let fraction = digit_run(text, start + 1, digit);
if fraction > 0 {
let mut index = start + 1 + fraction;
index += decimal_exponent(text, index);
return Some(index + float_suffix(text, index));
}
}
if start == 0 {
break;
}
}
if lead > 0 && at(text, lead) == '.' {
let mut index = lead + 1;
index += decimal_exponent(text, index);
return Some(index + float_suffix(text, index));
}
if lead > 0 {
let exponent = decimal_exponent(text, lead);
if exponent > 0 {
let index = lead + exponent;
return Some(index + float_suffix(text, index));
}
}
None
}
fn float_suffix(text: &[char], from: usize) -> usize {
let mut index = from;
if matches!(at(text, index), 'f' | 'F' | 'l' | 'L' | 'd' | 'D') {
index += 1;
}
if matches!(at(text, index), 'f' | 'F' | 'l' | 'L') {
index += 1;
}
index - from
}
fn float(look: &mut Look<'_>, scope: &Scope<'_>) -> Cut {
if scope.short(look, 0, 2) {
return Cut::Edge;
}
let first = look.at(0);
let starts_digit = first.is_ascii_digit();
let starts_dot = first == '.' && look.at(1).is_ascii_digit();
if !starts_digit && !starts_dot {
return Cut::No;
}
let text = match number_text(look, scope) {
Ok(text) => text,
Err(edge) => return edge,
};
let Some(length) = scan_float_hex(text).or_else(|| scan_float_dec(text)) else {
return Cut::No;
};
let matched = &text[..length];
let hex = matched.len() >= 2 && matched[0] == '0' && matches!(matched[1], 'x' | 'X');
let has_float_mark = matched.iter().any(|character| {
matches!(
character,
'.' | 'e' | 'E' | 'p' | 'P' | 'f' | 'F' | 'l' | 'L'
)
});
if !has_float_mark && !hex {
return Cut::No;
}
hit("LIT_FLOAT", length)
}
fn at(text: &[char], index: usize) -> char {
text.get(index).copied().unwrap_or('\0')
}
fn char_prefix(text: &[char]) -> Option<usize> {
for prefix in [&['L'][..], &['u', '8'][..], &['u'][..], &['U'][..], &[][..]] {
if text.len() > prefix.len()
&& text[..prefix.len()] == *prefix
&& text[prefix.len()] == '\''
{
return Some(prefix.len() + 1);
}
}
None
}
fn char_literal(look: &mut Look<'_>, scope: &Scope<'_>) -> Cut {
if scope.short(look, 0, 3) {
return Cut::Edge;
}
let Some(mut index) = char_prefix(look.slice(0, 3)) else {
return Cut::No;
};
while let Some(character) = look.get(index) {
if character == '\n' {
return Cut::Bad("unterminated_char", 0, index);
}
if character == '\\' {
index += 2;
continue;
}
if character == '\'' {
return hit("LIT_CHAR", index + 1);
}
index += 1;
}
if !scope.last {
return Cut::Edge;
}
Cut::Bad("unterminated_char", 0, look.count())
}
fn string_prefix(text: &[char]) -> Option<(usize, bool)> {
for encoding in [&['u', '8'][..], &['u'][..], &['U'][..], &['L'][..], &[][..]] {
if text.len() < encoding.len() || text[..encoding.len()] != *encoding {
continue;
}
let mut index = encoding.len();
let raw = at(text, index) == 'R';
if raw {
index += 1;
}
if at(text, index) == '"' {
return Some((index + 1, raw));
}
}
None
}
fn string_literal(look: &mut Look<'_>, scope: &Scope<'_>) -> Cut {
if scope.short(look, 0, 4) {
return Cut::Edge;
}
let Some((prefix, raw)) = string_prefix(look.slice(0, 4)) else {
return Cut::No;
};
let mut index = prefix;
if raw {
let mut delimiter_end = index;
while look
.get(delimiter_end)
.is_some_and(|character| character != '(')
{
delimiter_end += 1;
}
if look.get(delimiter_end).is_none() {
if !scope.last {
return Cut::Edge;
}
return Cut::Bad("unterminated_string", 0, look.count());
}
let delimiter: String = look.slice(index, delimiter_end).iter().collect();
let closer: Vec<char> = format!("){delimiter}\"").chars().collect();
let mut scan = delimiter_end + 1;
loop {
if look.get(scan + closer.len() - 1).is_none() {
if !scope.last {
return Cut::Edge;
}
return Cut::Bad("unterminated_string", 0, look.count());
}
if look.slice(scan, scan + closer.len()) == &closer[..] {
return hit("LIT_STRING", scan + closer.len());
}
scan += 1;
}
}
while let Some(character) = look.get(index) {
if character == '\n' {
return Cut::Bad("unterminated_string", 0, index);
}
if character == '\\' {
index += 2;
continue;
}
if character == '"' {
return hit("LIT_STRING", index + 1);
}
index += 1;
}
if !scope.last {
return Cut::Edge;
}
Cut::Bad("unterminated_string", 0, look.count())
}
fn sorted_punctuators() -> Vec<(&'static str, &'static str)> {
let mut sorted: Vec<(&str, &str)> = crate::tokens::PUNCTUATORS.to_vec();
sorted.sort_by_key(|entry| std::cmp::Reverse(entry.1.len()));
sorted
}
fn punctuator(
look: &mut Look<'_>,
scope: &Scope<'_>,
sorted: &[(&'static str, &'static str)],
) -> Cut {
let longest = sorted.first().map_or(0, |entry| entry.1.chars().count());
if scope.short(look, 0, longest) {
return Cut::Edge;
}
let ahead: String = look.slice(0, longest).iter().collect();
for (name, source) in sorted {
if ahead.starts_with(source) {
return hit(name, source.chars().count());
}
}
Cut::No
}
pub const MATCHER_ORDER: &[(&str, f64)] = &[
("c_line_cont", 100.0),
("c_block_comment", 110.0),
("c_line_comment", 120.0),
("c_pp_newline", 130.0),
("c_pp_open", 140.0),
("c_header_name", 150.0),
("c_whitespace", 160.0),
("c_string", 200.0),
("c_char", 210.0),
("c_float", 220.0),
("c_int", 230.0),
("c_identifier", 240.0),
("c_punctuator", 900.0),
];
pub fn make(name: &str, tins: Tins) -> ImperativeLexMatcher {
match name {
"c_line_cont" => wrap(tins, line_cont),
"c_block_comment" => wrap(tins, block_comment),
"c_line_comment" => wrap(tins, line_comment),
"c_pp_newline" => wrap(tins, pp_newline),
"c_pp_open" => wrap(tins, pp_directive_open),
"c_header_name" => wrap(tins, header_name),
"c_whitespace" => wrap(tins, whitespace),
"c_string" => wrap(tins, string_literal),
"c_char" => wrap(tins, char_literal),
"c_float" => wrap(tins, float),
"c_int" => wrap(tins, integer),
"c_identifier" => wrap(tins, identifier),
"c_punctuator" => {
let sorted = sorted_punctuators();
wrap(tins, move |look, scope| punctuator(look, scope, &sorted))
}
_ => wrap(tins, |_look, _scope| Cut::No),
}
}
fn wrap(
tins: Tins,
scan: impl Fn(&mut Look<'_>, &Scope<'_>) -> Cut + Send + Sync + 'static,
) -> ImperativeLexMatcher {
Arc::new(
move |lexer: &mut Lexer<'_>, _rule: &mut Rule, _context: &mut Context| {
cut(lexer, &tins, &scan)
},
)
}