use codespan_reporting::diagnostic::Label;
use crate::file_manager::{Diagnostic, Loc};
#[cfg_attr(test, derive(Debug))]
pub enum ErrorCode {
InvalidNum(String),
IntegerOverflow,
ParseFloatError(String),
InvalidEscapeSequence,
OpenQuote,
InvalidOp(char),
}
pub fn to_diagnostic(error: ErrorCode, loc: Loc) -> (Diagnostic, bool) {
let eof = matches!(error, ErrorCode::OpenQuote);
let diag = match error {
ErrorCode::InvalidNum(s) => Diagnostic::error()
.with_code("E0001")
.with_message(format!("Invalid number literal `{s}`"))
.with_labels(vec![Label::primary(loc.fid, loc)]),
ErrorCode::IntegerOverflow => Diagnostic::error()
.with_code("E0002")
.with_message("Integer literal overflow 64-bits integer".to_string())
.with_labels(vec![Label::primary(loc.fid, loc)]),
ErrorCode::ParseFloatError(s) => Diagnostic::error()
.with_code("E0003")
.with_message(s)
.with_labels(vec![Label::primary(loc.fid, loc)]),
ErrorCode::InvalidEscapeSequence => Diagnostic::error()
.with_code("E0004")
.with_message("Invalid escape sequence in string literal")
.with_labels(vec![Label::primary(loc.fid, loc)]),
ErrorCode::OpenQuote => Diagnostic::error()
.with_code("E0005")
.with_message("String literal is not terminated")
.with_labels(vec![Label::primary(loc.fid, loc)]),
ErrorCode::InvalidOp(c) => Diagnostic::error()
.with_code("E0006")
.with_message(format!("Invalid operator `{c}`"))
.with_labels(vec![Label::primary(loc.fid, loc)]),
};
(diag, eof)
}