use crate::Parser;
pub trait Combinator<I, S>: Parser<I, S>
where
Self: Sized,
{
fn then<P: Parser<I, S>>(self, other: P) -> All<(Self, P)> {
All((self, other))
}
fn or<P: Parser<I, S>>(self, other: P) -> Any<(Self, P)>
where
I: Clone,
{
Any((self, other))
}
fn map<O, F: FnOnce(Self::O) -> O>(self, f: F) -> Map<Self, F> {
Map(self, f)
}
fn map_with<O, F: FnOnce(Self::O, &mut S) -> O>(self, f: F) -> MapWith<Self, F> {
MapWith(self, f)
}
fn filter<F: FnOnce(&Self::O) -> bool>(self, f: F) -> Filter<Self, F> {
Filter(self, f)
}
fn filter_map<O, F: FnOnce(Self::O) -> Option<O>>(self, f: F) -> FilterMap<Self, F> {
FilterMap(self, f)
}
fn then_ignore<P: Parser<I, S>>(self, other: P) -> ThenMap<I, S, Self, P, Self::O> {
self.then(other).map(|(l, _r): (Self::O, P::O)| l)
}
fn ignore_then<P: Parser<I, S>>(self, other: P) -> ThenMap<I, S, Self, P, P::O> {
self.then(other).map(|(_l, r): (Self::O, P::O)| r)
}
fn delimited_by<L, R>(self, l: L, r: R) -> DelimitedBy<L, Self, R, L::O, Self::O, R::O>
where
L: Parser<I, S>,
R: Parser<I, S>,
{
all((l, self, r)).map(|(_l, m, _r)| m)
}
fn repeated<O>(self) -> Repeated<Self, fn() -> O>
where
I: Clone,
Self: Clone,
O: Default + Extend<Self::O>,
{
Repeated(self, O::default)
}
fn separated_by<Sep, O>(self, sep: Sep) -> SeparatedBy<Self, Sep, fn() -> O>
where
I: Clone,
Self: Clone,
Sep: Parser<I, S> + Clone,
O: Default + Extend<Self::O>,
{
SeparatedBy(self, sep, O::default)
}
fn opt(self) -> Opt<Self>
where
I: Clone,
{
Opt(self)
}
fn chain<P>(self, other: P) -> ThenMap<I, S, Self, P, Chain<Self::O, P::O>>
where
P: Parser<I, S>,
Self::O: IntoIterator,
P::O: IntoIterator<Item = <Self::O as IntoIterator>::Item>,
{
self.then(other).map(|(l, r)| l.into_iter().chain(r))
}
fn and_then<P: Parser<I, S>, F: FnOnce(Self::O) -> P>(self, f: F) -> AndThen<Self, F> {
AndThen(self, f)
}
}
type Chain<L, R> = core::iter::Chain<<L as IntoIterator>::IntoIter, <R as IntoIterator>::IntoIter>;
impl<I, S, T: Parser<I, S>> Combinator<I, S> for T {}
#[derive(Clone)]
pub struct All<T>(T);
pub fn all<T>(t: T) -> All<T> {
All(t)
}
#[derive(Clone)]
pub struct Any<T>(T);
pub fn any<T>(t: T) -> Any<T> {
Any(t)
}
impl<I: Clone, S, O, P: Parser<I, S, O = O>, const N: usize> Parser<I, S> for Any<[P; N]> {
type O = O;
fn parse(self, input: I, state: &mut S) -> Option<(Self::O, I)> {
let mut iter = IntoIterator::into_iter(self.0).rev();
let last = iter.next()?;
for p in iter.rev() {
if let Some((y, rest)) = p.parse(input.clone(), state) {
return Some((y, rest));
}
}
last.parse(input, state)
}
}
macro_rules! impl_any {
($input:ident, $state:ident, $head:ident $($tail:ident)+) => {
if let Some(y) = $head.parse($input.clone(), $state) {
return Some(y)
}
impl_any!($input, $state, $($tail)+)
};
($input:ident, $state:ident, $head:ident) => {
return $head.parse($input, $state)
}
}
macro_rules! impl_all_any {
($($acc:ident)+; $head:ident $($tail:ident)*) => {
impl_all_any!($($acc)+ ; );
impl_all_any!($($acc)+ $head; $($tail)*);
};
($($parser:ident)+;) => {
#[allow(non_snake_case)]
impl<I, S, $($parser: Parser<I, S>),+> Parser<I, S> for All<($($parser),+,)> {
type O = ($($parser::O),+,);
#[inline(always)]
fn parse(self, input: I, state: &mut S) -> Option<(Self::O, I)> {
let Self(($($parser),+,)) = self;
$(let ($parser, input) = $parser.parse(input, state)?;)+
Some((($($parser),+,), input))
}
}
#[allow(non_snake_case)]
impl<I: Clone, S, O, $($parser: Parser<I, S, O = O>),+> Parser<I, S> for Any<($($parser),+,)> {
type O = O;
#[inline(always)]
fn parse(self, input: I, state: &mut S) -> Option<(Self::O, I)> {
let Self(($($parser),+,)) = self;
impl_any!(input, state, $($parser)*);
}
}
}
}
impl_all_any!(P1; P2 P3 P4 P5 P6 P7 P8 P9);
pub fn decide<T, P>(t: T, last: P) -> Decide<T, P> {
Decide(t, last)
}
#[derive(Clone)]
pub struct Decide<T, Last>(T, Last);
macro_rules! impl_decide {
($($acc:ident)*; $lp:ident $rp:ident $rf:ident $($tail:ident)*) => {
impl_decide!($($acc)* ; );
impl_decide!($($acc)* $lp $rp $rf; $($tail)*);
};
($($lp:ident $rp:ident $rf:ident)*;) => {
#[allow(non_snake_case)]
impl<I: Clone, S, $($lp, $rp, $rf),*, Last: Parser<I, S>> Parser<I, S> for Decide<($(($lp, $rf)),*,), Last>
where
$(
$lp: Parser<I, S>,
$rp: Parser<I, S, O = Last::O>,
$rf: FnOnce($lp::O) -> $rp
),*,
{
type O = Last::O;
#[inline(always)]
fn parse(self, input: I, state: &mut S) -> Option<(Self::O, I)> {
let Self(($(($lp, $rf)),*,), last) = self;
$(
if let Some((y, rest)) = $lp.parse(input.clone(), state) {
core::mem::drop(input);
return $rf(y).parse(rest, state)
})*
return last.parse(input, state)
}
}
}
}
impl_decide!(
L1 R1 F1;
L2 R2 F2
L3 R3 F3
L4 R4 F4
L5 R5 F5
L6 R6 F6
L7 R7 F7
L8 R8 F8
L9 R9 F9
);
#[derive(Clone)]
pub struct Map<P, F>(P, F);
impl<I, S, P: Parser<I, S>, O, F: FnOnce(P::O) -> O> Parser<I, S> for Map<P, F> {
type O = O;
fn parse(self, input: I, state: &mut S) -> Option<(Self::O, I)> {
let (p, f) = (self.0, self.1);
p.parse(input, state).map(|(y, rest)| (f(y), rest))
}
}
#[derive(Clone)]
pub struct MapWith<P, F>(P, F);
impl<I, S, P: Parser<I, S>, O, F: FnOnce(P::O, &mut S) -> O> Parser<I, S> for MapWith<P, F> {
type O = O;
fn parse(self, input: I, state: &mut S) -> Option<(Self::O, I)> {
let (p, f) = (self.0, self.1);
p.parse(input, state).map(|(y, rest)| (f(y, state), rest))
}
}
#[derive(Clone)]
pub struct Filter<P, F>(P, F);
impl<I, S, P: Parser<I, S>, F: FnOnce(&P::O) -> bool> Parser<I, S> for Filter<P, F> {
type O = P::O;
fn parse(self, input: I, state: &mut S) -> Option<(Self::O, I)> {
let (p, f) = (self.0, self.1);
p.parse(input, state).filter(|(y, _rest)| f(y))
}
}
#[derive(Clone)]
pub struct FilterMap<P, F>(P, F);
impl<I, S, P: Parser<I, S>, O, F: FnOnce(P::O) -> Option<O>> Parser<I, S> for FilterMap<P, F> {
type O = O;
fn parse(self, input: I, state: &mut S) -> Option<(Self::O, I)> {
let (p, f) = (self.0, self.1);
p.parse(input, state)
.and_then(|(y, rest)| Some((f(y)?, rest)))
}
}
type ThenMap<I, S, P1, P2, O> =
Map<All<(P1, P2)>, fn((<P1 as Parser<I, S>>::O, <P2 as Parser<I, S>>::O)) -> O>;
type DelimitedBy<L, M, R, LO, MO, RO> = Map<All<(L, M, R)>, fn((LO, MO, RO)) -> MO>;
#[derive(Clone)]
pub struct Opt<P>(P);
impl<I: Clone, S, P: Parser<I, S>> Parser<I, S> for Opt<P> {
type O = Option<P::O>;
fn parse(self, input: I, state: &mut S) -> Option<(Self::O, I)> {
Some(match self.0.parse(input.clone(), state) {
Some((y, rest)) => (Some(y), rest),
None => (None, input),
})
}
}
pub fn repeat<I, S, P: Parser<I, S>, PF: FnMut() -> P, O>(p: PF) -> Repeat<PF, fn() -> O>
where
O: Default + Extend<P::O>,
{
Repeat(p, || O::default())
}
#[derive(Clone)]
pub struct Repeat<P, O>(P, O);
impl<I: Clone, S, P: Parser<I, S>, O: Extend<P::O>, PF: FnMut() -> P, OF: FnOnce() -> O>
Parser<I, S> for Repeat<PF, OF>
{
type O = O;
fn parse(mut self, mut input: I, state: &mut S) -> Option<(Self::O, I)> {
let mut out = self.1();
while let Some((y, rest)) = self.0().parse(input.clone(), state) {
out.extend(core::iter::once(y));
input = rest;
}
Some((out, input))
}
}
#[derive(Clone)]
pub struct Repeated<P, O>(P, O);
impl<I: Clone, S, P: Parser<I, S> + Clone, O: Extend<P::O>, OF: FnOnce() -> O> Parser<I, S>
for Repeated<P, OF>
{
type O = O;
fn parse(self, input: I, state: &mut S) -> Option<(Self::O, I)> {
let (p, o) = (self.0, self.1);
Repeat(|| p.clone(), o).parse(input, state)
}
}
pub fn separate_by<I, S, P: Parser<I, S>, Sep: Parser<I, S>, PF, SepF, O>(
p: PF,
sep: SepF,
) -> SeparateBy<PF, SepF, fn() -> O>
where
PF: FnMut() -> P,
SepF: FnMut() -> Sep,
O: Default + Extend<P::O>,
{
SeparateBy(p, sep, || O::default())
}
#[derive(Clone)]
pub struct SeparateBy<P, Sep, O>(P, Sep, O);
impl<I: Clone, S, P: Parser<I, S>, Sep: Parser<I, S>, O: Extend<P::O>, PF, SepF, OF> Parser<I, S>
for SeparateBy<PF, SepF, OF>
where
PF: FnMut() -> P,
SepF: FnMut() -> Sep,
OF: FnOnce() -> O,
{
type O = O;
fn parse(self, mut input: I, state: &mut S) -> Option<(Self::O, I)> {
let Self(mut p, mut sep, init) = self;
let mut out = init();
if let Some((head, rest)) = p().parse(input.clone(), state) {
out.extend(core::iter::once(head));
input = rest;
} else {
return Some((out, input));
}
while let Some((_, rest)) = sep().parse(input.clone(), state) {
if let Some((y, rest)) = p().parse(rest, state) {
out.extend(core::iter::once(y));
input = rest;
} else {
break;
}
}
Some((out, input))
}
}
#[derive(Clone)]
pub struct SeparatedBy<P, Sep, O>(P, Sep, O);
impl<I: Clone, S, P, Sep, O: Extend<P::O>, OF> Parser<I, S> for SeparatedBy<P, Sep, OF>
where
P: Parser<I, S> + Clone,
Sep: Parser<I, S> + Clone,
OF: FnOnce() -> O,
{
type O = O;
fn parse(self, input: I, state: &mut S) -> Option<(Self::O, I)> {
let (p, sep, o) = (self.0, self.1, self.2);
SeparateBy(|| p.clone(), || sep.clone(), o).parse(input, state)
}
}
#[derive(Clone)]
pub struct AndThen<P, F>(P, F);
impl<I, S, P1: Parser<I, S>, P2: Parser<I, S>, F: FnOnce(P1::O) -> P2> Parser<I, S>
for AndThen<P1, F>
{
type O = P2::O;
fn parse(self, input: I, state: &mut S) -> Option<(Self::O, I)> {
let (y, rest) = self.0.parse(input, state)?;
self.1(y).parse(rest, state)
}
}