feat: 更新错误处理机制,增强词法分析器和解析器的错误报告功能

This commit is contained in:
0264408
2026-06-16 14:24:49 +08:00
parent e052226cdb
commit c7e4e0dce0
4 changed files with 240 additions and 155 deletions
+22
View File
@@ -2,13 +2,35 @@ use crate::lexer::Token;
#[derive(Debug)]
pub enum RuntimeError {
LexError { message: String, line: usize, column: usize },
ParseError { message: String, token: Token },
RuntimeError { message: String, token: Option<Token> },
}
impl RuntimeError {
pub fn lex(message: impl Into<String>, line: usize, column: usize) -> Self {
RuntimeError::LexError {
message: message.into(),
line,
column,
}
}
pub fn parse(message: impl Into<String>, token: Token) -> Self {
RuntimeError::ParseError {
message: message.into(),
token,
}
}
}
impl std::fmt::Display for RuntimeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RuntimeError::LexError { message, line, column } => {
write!(f, "[Lex Error] {} at line {}, column {}", message, line, column)
}
RuntimeError::ParseError { message, token } => {
write!(f, "[Parse Error] {} at line {}, column {}", message, token.line, token.column)
}