use std::ops::ControlFlow;
use reborrow_generic::{Reborrow, Reborrowed};
use crate::error::std::StdErr;
use crate::input::{In, SeqInput, ToSpan};
use crate::{ErrorSink, Input, LatestSink, RbBack};
pub mod chain;
pub mod choice;
pub mod flow;
pub mod item;
pub mod many;
pub mod memo;
pub mod prim;
pub mod sep;
pub mod str;
pub mod then;
pub mod token;
pub mod trie;
pub trait SkipParserOnce<
I: Input,
N: Reborrow = (),
L: RbBack = (),
E: ErrorSink<I::Pos> = LatestSink<<I as Input>::Pos, StdErr<<I as Input>::Item>>,
>
{
fn discard_once(self, i: In<I, N, L, E>) -> Option<()>;
#[inline(always)]
fn seq<P>(self, right: P) -> (Self, P)
where
Self: Sized,
{
then::seq(self, right)
}
#[inline(always)]
fn set_env<'a, N1>(self, env: N) -> prim::SetEnv<'a, Self, N1, N>
where
Self: Sized,
N: Reborrowed<'a>,
{
prim::set_env(self, env)
}
#[inline(always)]
fn set_local<'a>(self, local: L) -> prim::SetLocal<'a, Self, L>
where
Self: Sized,
L: Reborrowed<'a> + RbBack,
{
prim::set_local(self, local)
}
#[inline(always)]
fn lookahead(self) -> prim::Lookahead<Self>
where
Self: Sized,
{
prim::lookahead(self)
}
#[inline(always)]
fn uncut(self) -> prim::Uncut<Self>
where
Self: Sized,
{
prim::uncut(self)
}
#[inline(always)]
fn cut_on_ok(self) -> prim::CutIfOk<Self>
where
Self: Sized,
{
prim::cut_on_ok(self)
}
#[inline(always)]
fn or_not(self) -> prim::OrNot<Self>
where
Self: Sized,
{
prim::maybe(self)
}
#[inline(always)]
fn left<P>(self, right: P) -> then::Left<Self, P>
where
Self: Sized,
{
then::left(self, right)
}
#[inline(always)]
fn right<P>(self, right: P) -> then::Right<Self, P>
where
Self: Sized,
{
then::right(self, right)
}
#[inline(always)]
fn between<P, Q>(self, left: P, right: Q) -> then::Between<P, Self, Q>
where
Self: Sized,
P: SkipParserOnce<I, N, L, E>,
Q: SkipParserOnce<I, N, L, E>,
{
then::between(left, right, self)
}
#[inline(always)]
fn with_range(self) -> prim::WithRange<Self>
where
Self: Sized,
{
prim::with_range(self)
}
#[inline(always)]
fn with_span(self) -> prim::WithSpan<Self>
where
Self: Sized,
I::Pos: ToSpan,
{
prim::with_span(self)
}
#[inline(always)]
fn with_seq(self) -> prim::WithSeq<Self>
where
Self: Sized,
I: SeqInput,
{
prim::with_seq(self)
}
#[inline(always)]
fn or<P>(self, other: P) -> choice::Choice<(Self, P)>
where
Self: Sized,
P: SkipParserOnce<I, N, L, E>,
{
choice::choice((self, other))
}
#[inline(always)]
fn to<O>(self, value: O) -> then::To<Self, O>
where
Self: Sized,
{
then::to(self, value)
}
#[inline(always)]
fn skip(self) -> then::Skip<Self>
where
Self: Sized,
{
then::skip(self)
}
}
pub trait SkipParserMut<
I: Input,
N: Reborrow = (),
L: RbBack = (),
E: ErrorSink<I::Pos> = LatestSink<<I as Input>::Pos, StdErr<<I as Input>::Item>>,
>: SkipParserOnce<I, N, L, E>
{
fn discard_mut(&mut self, i: In<I, N, L, E>) -> Option<()>;
#[inline(always)]
fn many_skip(self) -> many::SkipMany<Self>
where
Self: Sized,
{
many::many_skip(self)
}
#[inline(always)]
fn many1_skip(self) -> many::SkipMany1<Self>
where
Self: Sized,
{
many::many1_skip(self)
}
fn by_mut(&mut self) -> prim::MutParser<'_, Self>
where
Self: Sized,
{
prim::MutParser(self)
}
}
pub trait SkipParser<
I: Input,
N: Reborrow = (),
L: RbBack = (),
E: ErrorSink<I::Pos> = LatestSink<<I as Input>::Pos, StdErr<<I as Input>::Item>>,
>: SkipParserMut<I, N, L, E>
{
fn discard(&self, i: In<I, N, L, E>) -> Option<()>;
fn by_ref(&self) -> prim::RefParser<'_, Self>
where
Self: Sized,
{
prim::RefParser(self)
}
}
pub trait ParserOnce<
I: Input,
N: Reborrow = (),
L: RbBack = (),
E: ErrorSink<I::Pos> = LatestSink<<I as Input>::Pos, StdErr<<I as Input>::Item>>,
>: SkipParserOnce<I, N, L, E>
{
type Out;
fn parse_once(self, i: In<I, N, L, E>) -> Option<Self::Out>;
#[inline(always)]
fn then_once<F, O>(self, f: F) -> then::Then<Self, F>
where
Self: Sized,
F: FnOnce(Self::Out, In<I, N, L, E>) -> Option<O>,
{
then::then_once(self, f)
}
#[inline(always)]
fn map_once<F, O>(self, f: F) -> then::Map<Self, F>
where
Self: Sized,
F: FnOnce(Self::Out) -> O,
{
then::map(self, f)
}
#[inline(always)]
fn and_then_once<F, O>(self, f: F) -> then::AndThen<Self, F>
where
Self: Sized,
F: FnOnce(Self::Out) -> Option<O>,
{
then::and_then_once(self, f)
}
#[inline(always)]
fn bind_once<F, P>(self, f: F) -> then::Bind<Self, F>
where
Self: Sized,
F: FnOnce(Self::Out) -> P,
P: ParserOnce<I, N, L, E>,
{
then::bind(self, f)
}
}
pub trait ParserMut<
I: Input,
N: Reborrow = (),
L: RbBack = (),
E: ErrorSink<I::Pos> = LatestSink<<I as Input>::Pos, StdErr<<I as Input>::Item>>,
>: ParserOnce<I, N, L, E> + SkipParserMut<I, N, L, E>
{
fn parse_mut(&mut self, i: In<I, N, L, E>) -> Option<Self::Out>;
#[inline(always)]
fn then_mut<F, O>(self, f: F) -> then::Then<Self, F>
where
Self: Sized,
F: FnMut(Self::Out, In<I, N, L, E>) -> Option<O>,
{
then::then_mut(self, f)
}
#[inline(always)]
fn map_mut<F, O>(self, f: F) -> then::Map<Self, F>
where
Self: Sized,
F: FnMut(Self::Out) -> O,
{
then::map(self, f)
}
#[inline(always)]
fn and_then_mut<F, O>(self, f: F) -> then::AndThen<Self, F>
where
Self: Sized,
F: FnMut(Self::Out) -> Option<O>,
{
then::and_then_mut(self, f)
}
#[inline(always)]
fn bind_mut<F, P>(self, f: F) -> then::Bind<Self, F>
where
Self: Sized,
F: FnMut(Self::Out) -> P,
P: ParserOnce<I, N, L, E>,
{
then::bind(self, f)
}
#[inline(always)]
fn many<O>(self) -> many::Many<Self, O>
where
Self: Sized,
O: FromIterator<Self::Out>,
{
many::many(self)
}
#[inline(always)]
fn many1<O>(self) -> many::Many1<Self, O>
where
Self: Sized,
O: FromIterator<Self::Out>,
{
many::many1(self)
}
#[inline(always)]
fn many_map_once<O, F>(self, f: F) -> many::ManyMap<Self, F>
where
Self: Sized,
F: FnOnce(many::ManyMapIterator<Self, I, N, L, E, Self::Out>) -> O,
{
many::many_map_once(self, f)
}
#[inline(always)]
fn many_map_mut<O, F>(self, f: F) -> many::ManyMap<Self, F>
where
Self: Sized,
F: FnMut(many::ManyMapIterator<Self, I, N, L, E, Self::Out>) -> O,
{
many::many_map_mut(self, f)
}
#[inline(always)]
fn flow_mut<C, S, Step>(self, state: S, step: Step) -> flow::Flow<Self, S, Step>
where
Self: Sized,
Step: FnMut(S, Self::Out) -> ControlFlow<C, S>,
{
flow::Flow::new(self, state, step)
}
#[inline(always)]
fn flow_many<O, C, B>(self) -> flow::FlowMany<Self, O>
where
Self: Sized + ParserMut<I, N, L, E, Out = ControlFlow<B, C>>,
O: FromIterator<C>,
{
flow::FlowMany::new(self)
}
#[inline(always)]
fn many_till<Q, O>(self, till: Q) -> flow::ManyTill<Self, Q, O>
where
Self: Sized + ParserMut<I, N, L, E>,
Q: ParserMut<I, N, L, E>,
O: FromIterator<Self::Out>,
{
flow::many_till(self, till)
}
#[inline(always)]
fn flow_many_map_once<F, O, C, B>(self, f: F) -> flow::FlowManyMap<Self, F>
where
Self: Sized + ParserMut<I, N, L, E, Out = ControlFlow<B, C>>,
F: for<'a, 'b> FnOnce(flow::FlowManyIterator<'a, 'b, Self, I, N, L, E, C, B>) -> O,
{
flow::flow_many_map_once(self, f)
}
#[inline(always)]
fn flow_many_map_mut<F, O, C, B>(self, f: F) -> flow::FlowManyMap<Self, F>
where
Self: Sized + ParserMut<I, N, L, E, Out = ControlFlow<B, C>>,
F: for<'a, 'b> FnMut(flow::FlowManyIterator<'a, 'b, Self, I, N, L, E, C, B>) -> O,
{
flow::flow_many_map_mut(self, f)
}
#[inline(always)]
fn sep<O, Q>(self, sep_p: Q) -> sep::Sep<O, sep::iter::Zero, sep::iter::Allow, Self, Q>
where
Self: Sized,
Q: SkipParserMut<I, N, L, E>,
O: FromIterator<Self::Out>,
{
sep::sep(self, sep_p)
}
#[inline(always)]
fn sep1<O, Q>(self, sep_p: Q) -> sep::Sep<O, sep::iter::One, sep::iter::Allow, Self, Q>
where
Self: Sized,
Q: SkipParserMut<I, N, L, E>,
O: FromIterator<Self::Out>,
{
sep::sep1(self, sep_p)
}
#[inline(always)]
fn sep_map_once<Q, F, O>(
self,
sep_p: Q,
f: F,
) -> sep::SepMap<sep::iter::Zero, sep::iter::Allow, Self, Q, F>
where
Self: Sized,
Q: SkipParserMut<I, N, L, E>,
F: FnOnce(
sep::SepMapIterator<Self, Q, I, N, L, E, Self::Out, sep::iter::Zero, sep::iter::Allow>,
) -> O,
{
sep::sep_map_once(self, sep_p, f)
}
#[inline(always)]
fn sep_map_mut<Q, F, O>(
self,
sep_p: Q,
f: F,
) -> sep::SepMap<sep::iter::Zero, sep::iter::Allow, Self, Q, F>
where
Self: Sized,
Q: SkipParserMut<I, N, L, E>,
F: FnMut(
sep::SepMapIterator<Self, Q, I, N, L, E, Self::Out, sep::iter::Zero, sep::iter::Allow>,
) -> O,
{
sep::sep_map_mut(self, sep_p, f)
}
#[inline(always)]
fn sep_reduce<Q, F, Op>(self, op: Q, f: F) -> sep::SepReduce<Self, Q, F>
where
Self: Sized,
Q: ParserMut<I, N, L, E, Out = Op>,
{
sep::sep_reduce(self, op, f)
}
}
pub trait Parser<
I: Input,
N: Reborrow = (),
L: RbBack = (),
E: ErrorSink<I::Pos> = LatestSink<<I as Input>::Pos, StdErr<<I as Input>::Item>>,
>: ParserMut<I, N, L, E> + SkipParser<I, N, L, E>
{
fn parse(&self, i: In<I, N, L, E>) -> Option<Self::Out>;
#[inline(always)]
fn then<F, O>(self, f: F) -> then::Then<Self, F>
where
Self: Sized,
F: Fn(Self::Out, In<I, N, L, E>) -> Option<O>,
{
then::then(self, f)
}
#[inline(always)]
fn map<F, O>(self, f: F) -> then::Map<Self, F>
where
Self: Sized,
F: Fn(Self::Out) -> O,
{
then::map(self, f)
}
#[inline(always)]
fn and_then<F, O>(self, f: F) -> then::AndThen<Self, F>
where
Self: Sized,
F: Fn(Self::Out) -> Option<O>,
{
then::and_then(self, f)
}
#[inline(always)]
fn bind<F, P>(self, f: F) -> then::Bind<Self, F>
where
Self: Sized,
F: Fn(Self::Out) -> P,
P: ParserOnce<I, N, L, E>,
{
then::bind(self, f)
}
#[inline(always)]
fn many_map<O, F>(self, f: F) -> many::ManyMap<Self, F>
where
Self: Sized,
F: Fn(many::ManyMapIterator<Self, I, N, L, E, Self::Out>) -> O,
{
many::many_map(self, f)
}
#[inline(always)]
fn flow<C, S, Step>(self, state: S, step: Step) -> flow::Flow<Self, S, Step>
where
Self: Sized,
Step: Fn(S, Self::Out) -> ControlFlow<C, S>,
{
flow::Flow::new(self, state, step)
}
#[inline(always)]
fn flow_many_map<F, O, C, B>(self, f: F) -> flow::FlowManyMap<Self, F>
where
Self: Sized + Parser<I, N, L, E, Out = ControlFlow<B, C>>,
F: for<'a, 'b> Fn(flow::FlowManyIterator<'a, 'b, Self, I, N, L, E, C, B>) -> O,
{
flow::flow_many_map(self, f)
}
#[inline(always)]
fn sep_map<Q, F, O>(
self,
sep_p: Q,
f: F,
) -> sep::SepMap<sep::iter::Zero, sep::iter::Allow, Self, Q, F>
where
Self: Sized,
Q: SkipParser<I, N, L, E>,
F: Fn(
sep::SepMapIterator<Self, Q, I, N, L, E, Self::Out, sep::iter::Zero, sep::iter::Allow>,
) -> O,
{
sep::sep_map(self, sep_p, f)
}
#[inline(always)]
fn sep1_map<Q, F, O>(
self,
sep_p: Q,
f: F,
) -> sep::SepMap<sep::iter::One, sep::iter::Allow, Self, Q, F>
where
Self: Sized,
Q: SkipParser<I, N, L, E>,
F: Fn(
sep::SepMapIterator<Self, Q, I, N, L, E, Self::Out, sep::iter::One, sep::iter::Allow>,
) -> O,
{
sep::sep1_map(self, sep_p, f)
}
}