From deebe6d2f8db5c9961f2bc0956b29cd91f508184 Mon Sep 17 00:00:00 2001 From: Josh Thomas Date: Sun, 13 Oct 2024 18:03:17 -0500 Subject: [PATCH] move errors to dedicated file (#7) --- src/error.rs | 20 ++++++++++++++++++++ src/lexer.rs | 21 +-------------------- src/lib.rs | 1 + 3 files changed, 22 insertions(+), 20 deletions(-) create mode 100644 src/error.rs 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;