use codespan_reporting::diagnostic::Label;
use crate::file_manager::{Diagnostic, Loc};
pub enum ErrorCode {
CannotAssign(Loc),
NameNotDefined(Loc, String),
InvalidAssignment(Loc),
BreakOutsideLoop(Loc),
ContinueOutsideLoop(Loc),
ReturnOutsideFunction(Loc),
InvalidMember(Loc),
MetaNotAllowed(Loc),
}
impl From<ErrorCode> for Diagnostic {
fn from(value: ErrorCode) -> Self {
match value {
ErrorCode::CannotAssign(loc) => Diagnostic::error()
.with_code("E2000")
.with_message("Can not assign to this expression")
.with_labels(vec![Label::primary(loc.fid, loc)]),
ErrorCode::NameNotDefined(loc, name) => Diagnostic::error()
.with_code("E2001")
.with_message(format!("Name `{name}` is not defined in current scope"))
.with_labels(vec![Label::primary(loc.fid, loc)]),
ErrorCode::InvalidAssignment(loc) => Diagnostic::error()
.with_code("E2002")
.with_message("Assignment can not be used as expression")
.with_labels(vec![Label::primary(loc.fid, loc)]),
ErrorCode::BreakOutsideLoop(loc) => Diagnostic::error()
.with_code("E2003")
.with_message("Can not break outside a loop")
.with_labels(vec![Label::primary(loc.fid, loc)]),
ErrorCode::ContinueOutsideLoop(loc) => Diagnostic::error()
.with_code("E2004")
.with_message("Can not continue outside a loop")
.with_labels(vec![Label::primary(loc.fid, loc)]),
ErrorCode::ReturnOutsideFunction(loc) => Diagnostic::error()
.with_code("E2005")
.with_message("Can not return outside a function")
.with_labels(vec![Label::primary(loc.fid, loc)]),
ErrorCode::InvalidMember(loc) => Diagnostic::error()
.with_code("E2006")
.with_message("Member assessment must be an identifier")
.with_labels(vec![Label::primary(loc.fid, loc)]),
ErrorCode::MetaNotAllowed(loc) => Diagnostic::error()
.with_code("E2007")
.with_message("Set meta table is not allowed here")
.with_labels(vec![Label::primary(loc.fid, loc)])
.with_notes(vec![
"Meta table can only be set on newly created table".to_string(),
"For example: `table = {...} <- Meta`".to_string(),
]),
}
}
}