summaryrefslogtreecommitdiff
path: root/chewed/src/parse.rs
diff options
context:
space:
mode:
authorGreg Brown <gmb60@cam.ac.uk>2021-01-14 11:42:55 +0000
committerGreg Brown <gmb60@cam.ac.uk>2021-01-14 11:42:55 +0000
commitaac3549a72663c523a456b2f5d7c3b77f509cdd6 (patch)
tree562824f3cfa5feca791715c733f7749197bb7e7a /chewed/src/parse.rs
parent0d01692c97ea8ca6fc4b229e5b9678cb252bceda (diff)
Add labelled expressions.
Restructure project (again). Convert `Cat` and `Alt` from binary to n+2-ary.
Diffstat (limited to 'chewed/src/parse.rs')
-rw-r--r--chewed/src/parse.rs27
1 files changed, 12 insertions, 15 deletions
diff --git a/chewed/src/parse.rs b/chewed/src/parse.rs
index 65e9272..2d01757 100644
--- a/chewed/src/parse.rs
+++ b/chewed/src/parse.rs
@@ -1,3 +1,5 @@
+use std::fmt::Display;
+
use super::{
error::{ParseError, TakeError},
position::LineCol,
@@ -102,7 +104,7 @@ impl<I: ?Sized + Iterator<Item = char>> Parser for IterWrapper<I> {
}
}
-pub trait Parse: Sized {
+pub trait Parse: Display + Sized {
fn take<P: Parser + ?Sized>(input: &mut P) -> Result<Self, TakeError>;
fn parse<P: Parser>(mut input: P) -> Result<Self, ParseError> {
@@ -124,23 +126,18 @@ pub trait Parse: Sized {
}
}
-impl Parse for () {
- fn take<P: Parser + ?Sized>(_: &mut P) -> Result<Self, TakeError> {
- Ok(())
- }
-}
+#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
+pub struct Epsilon;
-impl<A: Parse, B: Parse> Parse for (A, B) {
- fn take<P: Parser + ?Sized>(input: &mut P) -> Result<Self, TakeError> {
- let a = input.take()?;
- let b = input.take()?;
- Ok((a, b))
+impl Display for Epsilon {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "")
}
+}
- fn parse<P: Parser>(mut input: P) -> Result<Self, ParseError> {
- let a = A::take(&mut input)?;
- let b = B::parse(input)?;
- Ok((a, b))
+impl Parse for Epsilon {
+ fn take<P: Parser + ?Sized>(_: &mut P) -> Result<Self, TakeError> {
+ Ok(Epsilon)
}
}