mirror of
https://github.com/MorizzG/rlox.git
synced 2025-12-06 04:12:42 +00:00
Chapter 7
This commit is contained in:
commit
42dbe531ad
15 changed files with 1112 additions and 0 deletions
57
src/error.rs
Normal file
57
src/error.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
use thiserror::Error;
|
||||
|
||||
use crate::lexer::Token;
|
||||
use crate::misc::CodePos;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum LexerError {
|
||||
#[error("Unexpected character '{c}' at {code_pos}.")]
|
||||
UnexpectedCharacter { c: char, code_pos: CodePos },
|
||||
#[error("Unterminated string literal starting at {code_pos}.")]
|
||||
UnterminatedStringLiteral { code_pos: CodePos },
|
||||
#[error("Unterminated block comment starting at {code_pos}.")]
|
||||
UnterminatedBlockComment { code_pos: CodePos },
|
||||
#[error("Invalid number literal {lexeme} at {code_pos}: {msg}")]
|
||||
InvalidNumberLiteral {
|
||||
lexeme: String,
|
||||
msg: String,
|
||||
code_pos: CodePos,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ParserError {
|
||||
#[error("Token stream ended unexpectedly.")]
|
||||
TokenStreamEnded,
|
||||
#[error("Unexpected token {token} at {0}.", token.code_pos())]
|
||||
UnexpectedToken { token: Token },
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum LoxError {
|
||||
#[error("{msg}")]
|
||||
LexerError { msg: String },
|
||||
#[error("{msg}")]
|
||||
ParserError { msg: String },
|
||||
}
|
||||
|
||||
impl From<Vec<LexerError>> for LoxError {
|
||||
fn from(lexer_errs: Vec<LexerError>) -> Self {
|
||||
let msg = if lexer_errs.len() == 1 {
|
||||
format!("{}", lexer_errs[0])
|
||||
} else {
|
||||
let msgs: Vec<String> = lexer_errs.iter().map(|err| format!("{}", err)).collect();
|
||||
msgs.join("\n")
|
||||
};
|
||||
|
||||
LoxError::LexerError { msg }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ParserError> for LoxError {
|
||||
fn from(parser_error: ParserError) -> Self {
|
||||
LoxError::ParserError {
|
||||
msg: format!("{parser_error}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue