use lib::marker::PhantomData;
use Parser;
use error::{ConsumedResult, ParseResult};
use stream::Stream;
impl<'a, I: Stream, O> Parser for FnMut(&mut I) -> ParseResult<O, I> + 'a {
type Input = I;
type Output = O;
type PartialState = ();
#[inline]
fn parse_lazy(&mut self, input: &mut Self::Input) -> ConsumedResult<O, I> {
self(input).into()
}
}
#[derive(Copy, Clone)]
pub struct FnParser<I, F>(F, PhantomData<fn(I) -> I>);
#[inline(always)]
pub fn parser<I, O, F>(f: F) -> FnParser<I, F>
where
I: Stream,
F: FnMut(&mut I) -> ParseResult<O, I>,
{
FnParser(f, PhantomData)
}
impl<I, O, F> Parser for FnParser<I, F>
where
I: Stream,
F: FnMut(&mut I) -> ParseResult<O, I>,
{
type Input = I;
type Output = O;
type PartialState = ();
#[inline]
fn parse_lazy(&mut self, input: &mut Self::Input) -> ConsumedResult<O, I> {
(self.0)(input).into()
}
}
impl<I, O> Parser for fn(&mut I) -> ParseResult<O, I>
where
I: Stream,
{
type Input = I;
type Output = O;
type PartialState = ();
#[inline]
fn parse_lazy(&mut self, input: &mut Self::Input) -> ConsumedResult<O, I> {
self(input).into()
}
}
#[derive(Copy)]
pub struct EnvParser<E, I, T>
where
I: Stream,
{
env: E,
parser: fn(E, &mut I) -> ParseResult<T, I>,
}
impl<E, I, T> Clone for EnvParser<E, I, T>
where
I: Stream,
E: Clone,
{
fn clone(&self) -> Self {
EnvParser {
env: self.env.clone(),
parser: self.parser,
}
}
}
impl<E, I, O> Parser for EnvParser<E, I, O>
where
E: Clone,
I: Stream,
{
type Input = I;
type Output = O;
type PartialState = ();
#[inline]
fn parse_lazy(&mut self, input: &mut Self::Input) -> ConsumedResult<O, I> {
(self.parser)(self.env.clone(), input).into()
}
}
#[inline(always)]
pub fn env_parser<E, I, O>(env: E, parser: fn(E, &mut I) -> ParseResult<O, I>) -> EnvParser<E, I, O>
where
E: Clone,
I: Stream,
{
EnvParser {
env: env,
parser: parser,
}
}