From 1dfdc001b34a6555f161f0526f8ba21ac8e9c3c7 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 13 May 2017 15:26:41 -0700 Subject: [PATCH] Add an error class --- lexer/src/error.rs | 16 ++++++++++++++++ lexer/src/lib.rs | 12 ++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 lexer/src/error.rs diff --git a/lexer/src/error.rs b/lexer/src/error.rs new file mode 100644 index 0000000..93aa3d2 --- /dev/null +++ b/lexer/src/error.rs @@ -0,0 +1,16 @@ +/* lexer/src/error.rs + * Eryn Wells + */ + +#[derive(Debug, Eq, PartialEq)] +pub struct Error { + message: String +} + +impl Error { + pub fn new(msg: String) -> Error { + Error { + message: msg + } + } +} diff --git a/lexer/src/lib.rs b/lexer/src/lib.rs index f7ad002..e01c242 100644 --- a/lexer/src/lib.rs +++ b/lexer/src/lib.rs @@ -4,6 +4,10 @@ use std::iter::Peekable; +mod error; + +pub use error::Error; + #[derive(Debug, Eq, PartialEq)] pub enum Token { LeftParen, RightParen, Id(String), } @@ -15,7 +19,7 @@ enum IterationResult { Finish, Continue, Emit(Token, Resume), - Error(String), + Error(Error), } pub struct Lexer where T: Iterator { @@ -32,12 +36,12 @@ impl Lexer where T: Iterator { } fn fail(&self, msg: String) -> IterationResult { - IterationResult::Error(msg) + IterationResult::Error(Error::new(msg)) } } impl Iterator for Lexer where T: Iterator { - type Item = Result; + type Item = Result; fn next(&mut self) -> Option { let mut buffer = String::new(); @@ -81,7 +85,7 @@ impl Iterator for Lexer where T: Iterator { } return Some(Ok(token)) }, - IterationResult::Error(msg) => return Some(Err(msg)), + IterationResult::Error(err) => return Some(Err(err)), }; } None