diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..e1dbc61 --- /dev/null +++ b/src/error.rs @@ -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 {} diff --git a/src/lexer.rs b/src/lexer.rs index eba0458..935aade 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -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, diff --git a/src/lib.rs b/src/lib.rs index 605a466..4652f07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ +mod error; mod lexer; mod scanner;