feat: 更新错误处理机制,增强词法分析器和解析器的错误报告功能
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user