Diffstat (limited to 'src/parser/util.rs')
| -rw-r--r-- | src/parser/util.rs | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/parser/util.rs b/src/parser/util.rs new file mode 100644 index 0000000..a4f1c71 --- /dev/null +++ b/src/parser/util.rs @@ -0,0 +1,107 @@ +use super::types::*; + +macro_rules! t { + (ident) => { + select! { Token::Ident(ident) => ident }.labelled("ident") + }; + (if) => { + just(Token::If) + }; + (else) => { + just(Token::Else) + }; + (=) => { + just(Token::Equal) + }; + (λ) => { + just(Token::Lambda) + }; + (<-) => { + just(Token::Place) + }; + (,) => { + just(Token::Comma) + }; + (:) => { + just(Token::Colon) + }; + (->) => { + just(Token::ThinArrow) + }; + (()) => { + just(Token::Unit) + }; + ('(') => { + just(Token::OpeningBracket('(')) + }; + (')') => { + just(Token::ClosingBracket(')')) + }; + ('[') => { + just(Token::OpeningBracket('[')) + }; + (']') => { + just(Token::ClosingBracket(']')) + }; + ('{') => { + just(Token::OpeningBracket('{')) + }; + ('}') => { + just(Token::ClosingBracket('}')) + }; +} +macro_rules! parser { + ($t:ty) => { + impl Parser<'s, crate::parser::types::Input<'s>, $t, extra::Err<Error<'s>>> + Clone + } +} + +pub trait TakeSpan { + fn tspn<T>(&mut self, x: T) -> Spanned<T>; +} + +impl<'a, 'b> TakeSpan for MapExtra<'a, 'b, Input<'a>, chumsky::extra::Err<Error<'a>>> { + fn tspn<T>(&mut self, x: T) -> Spanned<T> { + Spanned::from((x, self.span())) + } +} +macro_rules! spanned { + () => { + |a, extra| Spanned::from((a, extra.span())) + }; +} + +use chumsky::input::MapExtra; +pub(crate) use parser; +pub(crate) use spanned; +pub(crate) use t; + +pub trait Unit<T> { + fn empty(&self) -> T; +} + +impl<T> Unit<Option<()>> for Option<T> { + fn empty(&self) -> Option<()> { + self.as_ref().map(|_| ()) + } +} + +pub trait Spanner { + fn spun(self, s: Span) -> Spanned<Self> + where + Self: Sized, + { + (self, s).into() + } +} +impl<T> Spanner for T {} + +pub trait MapLeft<T, V> { + fn ml<U>(self, f: impl Fn(T) -> U) -> (U, V); +} + +impl<T, V> MapLeft<T, V> for (T, V) { + fn ml<U>(self, f: impl Fn(T) -> U) -> (U, V) { + (f(self.0), self.1) + } +} |