move errors to dedicated file

This commit is contained in:
Josh Thomas 2024-10-13 18:01:10 -05:00
parent aa7913b988
commit 9c816917cb
3 changed files with 22 additions and 20 deletions

20
src/error.rs Normal file
View file

@ -0,0 +1,20 @@
use std::fmt;
#[derive(Debug)]
pub enum LexerError {
EmptyToken(usize),
UnexpectedCharacter(char, usize),
}
impl fmt::Display for LexerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LexerError::EmptyToken(line) => write!(f, "Empty token at line {}", line),
LexerError::UnexpectedCharacter(c, line) => {
write!(f, "Unexpected character '{}' at line {}", c, line)
}
}
}
}
impl std::error::Error for LexerError {}

View file

@ -1,6 +1,6 @@
use std::fmt;
use std::fmt::Debug;
use crate::error::LexerError;
use crate::scanner::{Scanner, ScannerState};
#[derive(Debug, Clone, PartialEq)]
@ -70,25 +70,6 @@ pub trait Tokenizer: Scanner {
fn add_token(&mut self, token_type: Self::TokenType);
}
#[derive(Debug)]
pub enum LexerError {
EmptyToken(usize),
UnexpectedCharacter(char, usize),
}
impl fmt::Display for LexerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LexerError::EmptyToken(line) => write!(f, "Empty token at line {}", line),
LexerError::UnexpectedCharacter(c, line) => {
write!(f, "Unexpected character '{}' at line {}", c, line)
}
}
}
}
impl std::error::Error for LexerError {}
pub struct Lexer {
source: String,
tokens: Vec<Token>,

View file

@ -1,2 +1,3 @@
mod error;
mod lexer;
mod scanner;