From eb280a903f8f20d0b0c0ef5acae955a20929d100 Mon Sep 17 00:00:00 2001 From: Greg Brown Date: Wed, 6 Jan 2021 16:36:46 +0000 Subject: Create Chewed, the consumer crate. --- chewed/Cargo.toml | 9 ++++++ chewed/src/error.rs | 58 +++++++++++++++++++++++++++++++++++++ chewed/src/lib.rs | 5 ++++ chewed/src/parse.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 chewed/Cargo.toml create mode 100644 chewed/src/error.rs create mode 100644 chewed/src/lib.rs create mode 100644 chewed/src/parse.rs (limited to 'chewed') diff --git a/chewed/Cargo.toml b/chewed/Cargo.toml new file mode 100644 index 0000000..87053bc --- /dev/null +++ b/chewed/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "chewed" +version = "0.1.0" +authors = ["Greg Brown "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/chewed/src/error.rs b/chewed/src/error.rs new file mode 100644 index 0000000..cb2cc4b --- /dev/null +++ b/chewed/src/error.rs @@ -0,0 +1,58 @@ +use std::{error::Error, fmt}; + +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub enum TakeError { + BadBranch(char, &'static [char]), + BadString(String, &'static str), + EndOfStream, +} + +impl fmt::Display for TakeError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::BadBranch(got, expected) => { + write!(f, "Unexpected character {:?}.", got)?; + + if expected.is_empty() { + write!(f, "Expected end of input.") + } else if expected.len() == 1 { + write!(f, "Expected character {:?}.", expected[0]) + } else { + let mut iter = expected.iter(); + write!(f, "Expected one of {:?}", iter.next().unwrap())?; + + for c in iter { + write!(f, ", {:?}", c)?; + } + + Ok(()) + } + } + Self::BadString(got, expected) => write!(f, "Unexpected string {:?}. Expected {:?}", got, expected), + Self::EndOfStream => write!(f, "Unexpected end of input"), + } + } +} + +impl Error for TakeError {} + +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub enum ParseError { + TakeError(TakeError), + InputContinues, +} + +impl fmt::Display for ParseError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::TakeError(e) => e.fmt(f), + Self::InputContinues => write!(f, "Expected end of input"), + } + } +} + +impl From for ParseError { + fn from(e: TakeError) -> Self { + Self::TakeError(e) + } +} diff --git a/chewed/src/lib.rs b/chewed/src/lib.rs new file mode 100644 index 0000000..341af9d --- /dev/null +++ b/chewed/src/lib.rs @@ -0,0 +1,5 @@ +mod error; +mod parse; + +pub use error::*; +pub use parse::*; diff --git a/chewed/src/parse.rs b/chewed/src/parse.rs new file mode 100644 index 0000000..0687ea5 --- /dev/null +++ b/chewed/src/parse.rs @@ -0,0 +1,82 @@ +use super::error::{ParseError, TakeError}; + +pub trait Parser: Iterator { + fn peek(&mut self) -> Option; + + fn take(&mut self) -> Result { + P::take(self) + } + + fn parse(self) -> Result + where + Self: Sized, + { + P::parse(self) + } + + fn take_str(&mut self, s: &'static str) -> Result<(), TakeError> { + let mut count = 0; + + for exp in s.chars() { + if let Some(got) = self.peek() { + if got == exp { + self.next(); + count += 1 + } else { + let mut out = String::from(&s[..count]); + out.push(got); + + return Err(TakeError::BadString(out, s)); + } + } else { + return Err(TakeError::EndOfStream); + } + } + + Ok(()) + } +} + +pub trait Parse: Sized { + fn take(input: &mut P) -> Result; + + fn parse(mut input: P) -> Result { + let res = Self::take(&mut input)?; + + if input.peek().is_some() { + Err(ParseError::InputContinues) + } else { + Ok(res) + } + } +} + +impl Parse for () { + fn take(_: &mut P) -> Result { + Ok(()) + } +} + +impl Parse for (A, B) { + fn take(input: &mut P) -> Result { + let a = input.take()?; + let b = input.take()?; + Ok((a, b)) + } + + fn parse(mut input: P) -> Result { + let a = A::take(&mut input)?; + let b = B::parse(input)?; + Ok((a, b)) + } +} + +impl Parse for Box { + fn take(input: &mut P) -> Result { + Ok(Box::new(input.take()?)) + } + + fn parse(input: P) -> Result { + Ok(Box::new(input.parse()?)) + } +} -- cgit v1.2.3