use crate::parsing::c::lex::tokens::{
TOK_ERR_INVALID_ESCAPE, TOK_ERR_UNTERMINATED_CHAR, TOK_ERR_UNTERMINATED_COMMENT,
TOK_ERR_UNTERMINATED_STRING,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum C11LexerDiagnosticKind {
UnterminatedString,
UnterminatedChar,
UnterminatedBlockComment,
InvalidEscape,
}
impl C11LexerDiagnosticKind {
#[must_use]
pub fn from_token(token: u32) -> Option<Self> {
match token {
TOK_ERR_UNTERMINATED_STRING => Some(Self::UnterminatedString),
TOK_ERR_UNTERMINATED_CHAR => Some(Self::UnterminatedChar),
TOK_ERR_UNTERMINATED_COMMENT => Some(Self::UnterminatedBlockComment),
TOK_ERR_INVALID_ESCAPE => Some(Self::InvalidEscape),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct C11LexerDiagnostic {
pub kind: C11LexerDiagnosticKind,
pub token_index: u32,
pub byte_start: u32,
pub byte_len: u32,
}
#[must_use]
pub fn first_c11_lexer_diagnostic(
tok_types: &[u32],
tok_starts: &[u32],
tok_lens: &[u32],
) -> Option<C11LexerDiagnostic> {
let limit = tok_types.len().min(tok_starts.len()).min(tok_lens.len());
tok_types
.iter()
.take(limit)
.enumerate()
.find_map(|(idx, token)| {
C11LexerDiagnosticKind::from_token(*token).map(|kind| C11LexerDiagnostic {
kind,
token_index: idx as u32,
byte_start: tok_starts[idx],
byte_len: tok_lens[idx],
})
})
}