pub mod atomic;
pub mod combinator;
pub mod iterable;
pub mod wrapper;
mod future;
mod utils;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[cfg(feature = "alloc")]
use core::fmt::Display;
use core::ops::RangeBounds;
use core::pin::Pin;
use core::task::Context;
use crate::error::{Expects, PolledResult};
use crate::stream::{Input, Positioned};
use atomic::*;
use combinator::*;
use future::ParseFuture;
use iterable::assert_iterable_parser;
use iterable::generator::*;
use wrapper::*;
#[inline]
pub fn function<F, I, O, E, C>(f: F) -> Function<F, I, C>
where
F: FnMut(Pin<&mut I>, &mut Context<'_>, &mut C) -> PolledResult<O, I>,
I: Positioned + ?Sized,
C: Default,
{
assert_parser(Function::new(f))
}
#[inline]
pub fn lazy<F, P>(f: F) -> Lazy<F>
where
F: FnMut() -> P,
{
Lazy::new(f)
}
#[inline]
pub fn any<I: Positioned + ?Sized>() -> Any<I> {
assert_parser(Any::new())
}
#[inline]
pub fn eof<I: Positioned + ?Sized>() -> Eof<I> {
assert_parser(Eof::new())
}
#[inline]
pub fn value<I: Positioned + ?Sized, T: Clone>(value: T) -> Value<I, T> {
assert_parser(Value::new(value))
}
#[inline]
pub fn value_fn<I: Positioned + ?Sized, F: FnMut() -> T, T>(f: F) -> ValueFn<I, F> {
assert_parser(ValueFn::new(f))
}
#[inline]
pub fn position<I: Positioned + ?Sized>() -> Position<I> {
assert_parser(Position::new())
}
#[inline]
#[cfg(feature = "alloc")]
pub fn token<I>(token: I::Ok) -> Token<I, I::Ok>
where
I: Positioned + ?Sized,
I::Ok: PartialEq + Display,
{
assert_parser(Token::new(token))
}
#[inline]
#[cfg(not(feature = "alloc"))]
pub fn token<I>(token: I::Ok) -> Token<I, I::Ok>
where
I: Positioned + ?Sized,
I::Ok: PartialEq,
{
assert_parser(Token::new(token))
}
#[inline]
#[cfg(feature = "alloc")]
pub fn not<I>(token: I::Ok) -> Not<I, I::Ok>
where
I: Positioned + ?Sized,
I::Ok: PartialEq + Display,
{
assert_parser(Not::new(token))
}
#[inline]
#[cfg(not(feature = "alloc"))]
pub fn not<I>(token: I::Ok) -> Not<I, I::Ok>
where
I: Positioned + ?Sized,
I::Ok: PartialEq,
{
assert_parser(Not::new(token))
}
#[inline]
pub fn one_of<I, S>(set: S) -> OneOf<I, S>
where
I: Positioned + ?Sized,
S: Set<I::Ok>,
{
assert_parser(OneOf::new(set))
}
#[inline]
pub fn none_of<I, S>(set: S) -> NoneOf<I, S>
where
I: Positioned + ?Sized,
S: Set<I::Ok>,
{
assert_parser(NoneOf::new(set))
}
#[inline]
pub fn is<I, F>(cond: F) -> Is<I, F>
where
I: Positioned + ?Sized,
F: FnMut(&I::Ok) -> bool,
{
assert_parser(Is::new(cond))
}
#[inline]
pub fn is_not<I, F>(cond: F) -> IsNot<I, F>
where
I: Positioned + ?Sized,
F: FnMut(&I::Ok) -> bool,
{
assert_parser(IsNot::new(cond))
}
#[inline]
pub fn is_some<I, F, O>(cond: F) -> IsSome<I, F>
where
I: Positioned + ?Sized,
F: FnMut(I::Ok) -> Option<O>,
{
assert_parser(IsSome::new(cond))
}
#[inline]
pub fn tokens<'a, I, T>(tokens: T) -> Tokens<'a, I, T>
where
I: Positioned + ?Sized,
I::Ok: PartialEq,
T: IntoIterator<Item = &'a I::Ok> + Clone,
{
assert_parser(Tokens::new(tokens))
}
pub fn tag<I>(tag: &'static str) -> Tag<I>
where
I: Positioned<Ok = char> + ?Sized,
{
assert_parser(Tag::new(tag))
}
#[inline]
pub fn choice<C, I>(choice: C) -> C::Parser
where
C: ChoiceParser<I>,
I: Positioned + ?Sized,
{
assert_parser(choice.into_parser())
}
#[cfg_attr(feature = "nightly", doc(notable_trait))]
pub trait Parser<I: Positioned + ?Sized> {
type Output;
type State: Default;
fn poll_parse(
&mut self,
input: Pin<&mut I>,
cx: &mut Context<'_>,
state: &mut Self::State,
) -> PolledResult<Self::Output, I>;
}
pub trait ParserExt<I: Positioned + ?Sized>: Parser<I> {
#[inline]
fn parse<'a, 'b>(&'a mut self, input: &'b mut I) -> ParseFuture<'a, 'b, Self, I, Self::State>
where
I: Unpin,
{
ParseFuture::new(self, input)
}
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "alloc")))]
#[inline]
fn boxed<'a>(self) -> Box<dyn Parser<I, Output = Self::Output, State = Self::State> + 'a>
where
Self: Sized + 'a,
{
assert_parser(Box::new(self))
}
#[inline]
fn no_state(self) -> NoState<Self, Self::State>
where
Self: Sized,
{
assert_parser(NoState::new(self))
}
#[inline]
fn left<R>(self) -> Either<Self, R>
where
Self: Sized,
R: Parser<I, Output = Self::Output>,
{
assert_parser(Either::Left(self))
}
#[inline]
fn right<L>(self) -> Either<L, Self>
where
Self: Sized,
L: Parser<I, Output = Self::Output>,
{
assert_parser(Either::Right(self))
}
#[inline]
fn complete(self) -> Skip<Self, Eof<I>>
where
Self: Sized,
{
assert_parser(self.skip(eof()))
}
#[inline]
fn with_position(self) -> WithPosition<Self>
where
Self: Sized,
{
assert_parser(WithPosition::new(self))
}
#[inline]
fn peek(self) -> Peek<Self>
where
Self: Sized,
I: Input,
{
assert_parser(Peek::new(self))
}
#[inline]
fn fail(self) -> Fail<Self>
where
Self: Sized,
I: Input,
{
assert_parser(Fail::new(self))
}
#[inline]
fn and<P>(self, p: P) -> (Self, P)
where
Self: Sized,
P: Parser<I>,
{
assert_parser((self, p))
}
#[inline]
fn skip<P>(self, p: P) -> Skip<Self, P>
where
Self: Sized,
P: Parser<I>,
{
assert_parser(Skip::new(self, p))
}
#[inline]
fn prefix<P>(self, p: P) -> Prefix<Self, P>
where
Self: Sized,
{
Prefix::new(self, p)
}
#[inline]
fn between<L, R>(self, left: L, right: R) -> Skip<Prefix<L, Self>, R>
where
Self: Sized,
L: Parser<I>,
R: Parser<I>,
{
assert_parser(Skip::new(Prefix::new(left, self), right))
}
#[inline]
fn or<P>(self, other: P) -> Or<Self, P>
where
Self: Sized,
I: Input,
P: Parser<I, Output = Self::Output>,
{
assert_parser(Or::new(self, other))
}
#[inline]
fn opt(self) -> Opt<Self>
where
Self: Sized,
I: Input,
{
assert_parser(Opt::new(self))
}
#[inline]
fn once(self) -> Times<Self>
where
Self: Sized,
I: Positioned,
{
assert_iterable_parser(Times::new(self, 1))
}
#[inline]
fn times(self, n: usize) -> Times<Self>
where
Self: Sized,
I: Positioned,
{
assert_iterable_parser(Times::new(self, n))
}
#[inline]
fn sep_by_times<P, R>(self, sep: P, count: usize) -> SepByTimes<Self, P>
where
Self: Sized,
P: Parser<I>,
{
assert_iterable_parser(SepByTimes::new(self, sep, count))
}
#[inline]
fn sep_by_end_times<P, R>(self, sep: P, count: usize) -> SepByEndTimes<Self, P>
where
Self: Sized,
I: Input,
P: Parser<I>,
{
assert_iterable_parser(SepByEndTimes::new(self, sep, count))
}
#[inline]
fn repeat<R>(self, range: R) -> Repeat<Self, R>
where
Self: Sized,
R: RangeBounds<usize>,
I: Input,
{
assert_iterable_parser(Repeat::new(self, range))
}
#[inline]
fn sep_by<P, R>(self, sep: P, range: R) -> SepBy<Self, P, R>
where
Self: Sized,
P: Parser<I>,
R: RangeBounds<usize>,
I: Input,
{
assert_iterable_parser(SepBy::new(self, sep, range))
}
#[inline]
fn sep_by_end<P, R>(self, sep: P, range: R) -> SepByEnd<Self, P, R>
where
Self: Sized,
P: Parser<I>,
R: RangeBounds<usize>,
I: Input,
{
assert_iterable_parser(SepByEnd::new(self, sep, range))
}
#[inline]
fn then<F, Q>(self, f: F) -> Then<Self, F>
where
Self: Sized,
F: FnMut(Self::Output) -> Q,
{
Then::new(self, f)
}
#[inline]
fn try_then<F, Q, E>(self, f: F) -> TryThen<Self, F>
where
Self: Sized,
F: FnMut(Self::Output) -> Result<Q, E>,
E: Into<Expects>,
{
TryThen::new(self, f)
}
#[inline]
fn until<P>(self, end: P) -> Until<Self, P>
where
Self: Sized,
P: Parser<I>,
I: Input,
{
assert_iterable_parser(Until::new(self, end))
}
#[inline]
fn discard(self) -> Discard<Self>
where
Self: Sized,
{
assert_parser(Discard::new(self))
}
#[inline]
fn map<F, O>(self, f: F) -> Map<Self, F>
where
Self: Sized,
F: FnMut(Self::Output) -> O,
{
assert_parser(Map::new(self, f))
}
#[inline]
fn try_map<F, O, E>(self, f: F) -> TryMap<Self, F>
where
Self: Sized,
F: FnMut(Self::Output) -> Result<O, E>,
E: Into<Expects>,
{
assert_parser(TryMap::new(self, f))
}
#[inline]
fn satisfy<F, O>(self, f: F) -> Satisfy<Self, F>
where
Self: Sized,
F: FnMut(&Self::Output) -> bool,
{
assert_parser(Satisfy::new(self, f))
}
#[inline]
fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
where
Self: Sized,
F: FnMut(Expects) -> Expects,
E: Into<Expects>,
{
assert_parser(MapErr::new(self, f))
}
#[inline]
fn expect<E: Into<Expects>>(self, expected: E) -> Expect<Self>
where
Self: Sized,
I::Ok: Clone,
{
assert_parser(Expect::new(self, expected.into()))
}
#[inline]
fn spanned(self) -> Spanned<Self>
where
Self: Sized,
{
assert_parser(Spanned::new(self))
}
#[inline]
fn exclusive<E: Into<Expects>>(self, expected: E) -> Exclusive<Self>
where
Self: Sized,
I::Ok: Clone,
{
assert_parser(Exclusive::new(self, expected.into()))
}
#[inline]
fn rewindable(self) -> Rewindable<Self>
where
Self: Sized,
{
assert_parser(Rewindable::new(self))
}
}
impl<P: Parser<I>, I: Positioned + ?Sized> ParserExt<I> for P {}
impl<'a, P: Parser<I> + ?Sized, I: Positioned + ?Sized> Parser<I> for &'a mut P {
type Output = P::Output;
type State = P::State;
#[inline]
fn poll_parse(
&mut self,
input: Pin<&mut I>,
cx: &mut Context<'_>,
state: &mut Self::State,
) -> PolledResult<Self::Output, I> {
(**self).poll_parse(input, cx, state)
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "alloc")))]
impl<P: Parser<I> + ?Sized, I: Positioned + ?Sized> Parser<I> for Box<P> {
type Output = P::Output;
type State = P::State;
#[inline]
fn poll_parse(
&mut self,
input: Pin<&mut I>,
cx: &mut Context<'_>,
state: &mut Self::State,
) -> PolledResult<Self::Output, I> {
(**self).poll_parse(input, cx, state)
}
}
#[inline]
fn assert_parser<P: Parser<I>, I: Positioned + ?Sized>(parser: P) -> P {
parser
}