nff: LexError should include position for InvalidNumber; update diagnostics

This commit is contained in:
raf 2025-06-02 11:26:32 +03:00
commit c0002f5806
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
2 changed files with 49 additions and 24 deletions

View file

@ -453,7 +453,7 @@ impl AnalyzerModule for LexicalAnalyzer {
}
impl LexicalAnalyzer {
fn lex_error_to_diagnostic(error: &LexError, source: &str) -> Diagnostic {
pub fn lex_error_to_diagnostic(error: &LexError, source: &str) -> Diagnostic {
match error {
LexError::InvalidToken { position, text } => {
let pos = Position::from_text_size(TextSize::from(*position as u32), source);
@ -478,27 +478,17 @@ impl LexicalAnalyzer {
"Unterminated string literal".to_string(),
)
}
LexError::InvalidNumber { text } => {
if let Some(pos) = source.find(text) {
let start_pos = Position::from_text_size(TextSize::from(pos as u32), source);
let end_pos =
Position::new(start_pos.line, start_pos.character + text.len() as u32);
let range = Range::new(start_pos, end_pos);
Diagnostic::new(
range,
DiagnosticSeverity::Error,
DiagnosticCode::InvalidNumber,
format!("Invalid number: '{}'", text),
)
} else {
let range = Range::single_position(Position::new(0, 0));
Diagnostic::new(
range,
DiagnosticSeverity::Error,
DiagnosticCode::InvalidNumber,
format!("Invalid number: '{}'", text),
)
}
LexError::InvalidNumber { position, text } => {
let start_pos = Position::from_text_size(TextSize::from(*position as u32), source);
let end_pos =
Position::new(start_pos.line, start_pos.character + text.len() as u32);
let range = Range::new(start_pos, end_pos);
Diagnostic::new(
range,
DiagnosticSeverity::Error,
DiagnosticCode::InvalidNumber,
format!("Invalid number: '{}'", text),
)
}
}
}