loqom 0.1.0

A `nom` fork: a zero-copy, parser combinators library
Documentation
//! Combinators applying parsers in sequence

#[cfg(test)]
mod tests;

use crate::error::ParseError;
use crate::internal::Parser;
use crate::{Check, OutputM, OutputMode, PResult};

/// Matches an object from the first parser and discards it,
/// then gets an object from the second parser.
///
/// # Arguments
/// * `first` The opening parser.
/// * `second` The second parser to get object.
///
/// ```rust
/// # use loqom::{Err, error::ErrorKind, Needed, Parser};
/// # use loqom::Needed::Size;
/// use loqom::sequence::preceded;
/// use loqom::bytes::tag;
///
/// let mut parser = preceded(tag("abc"), tag("efg"));
///
/// assert_eq!(parser.parse_complete("abcefg"), Ok(("", "efg")));
/// assert_eq!(parser.parse_complete("abcefghij"), Ok(("hij", "efg")));
/// assert_eq!(parser.parse_complete(""), Err(Err::Error(("", ErrorKind::Tag))));
/// assert_eq!(parser.parse_complete("123"), Err(Err::Error(("123", ErrorKind::Tag))));
/// ```
pub fn preceded<I, O, E: ParseError<I>, F, G>(
  first: F,
  second: G,
) -> impl Parser<I, Output = O, Error = E>
where
  F: Parser<I, Error = E>,
  G: Parser<I, Output = O, Error = E>,
{
  Preceded {
    f: first,
    g: second,
  }
}

/// Parser wrapper for `preceded`
pub struct Preceded<F, G> {
  f: F,
  g: G,
}

impl<I, E: ParseError<I>, F: Parser<I, Error = E>, G: Parser<I, Error = E>> Parser<I>
  for Preceded<F, G>
{
  type Output = <G as Parser<I>>::Output;
  type Error = E;

  #[inline(always)]
  fn process<OM: OutputMode>(&mut self, i: I) -> PResult<OM, I, Self::Output, Self::Error> {
    let (i, ()) = self
      .f
      .process::<OutputM<Check, OM::Error, OM::Incomplete>>(i)?;
    let (i, o2) = self.g.process::<OM>(i)?;

    Ok((i, o2))
  }
}

/// Gets an object from the first parser,
/// then matches an object from the second parser and discards it.
///
/// # Arguments
/// * `first` The first parser to apply.
/// * `second` The second parser to match an object.
///
/// ```rust
/// # use loqom::{Err, error::ErrorKind, Needed, Parser};
/// # use loqom::Needed::Size;
/// use loqom::sequence::terminated;
/// use loqom::bytes::tag;
///
/// let mut parser = terminated(tag("abc"), tag("efg"));
///
/// assert_eq!(parser.parse_complete("abcefg"), Ok(("", "abc")));
/// assert_eq!(parser.parse_complete("abcefghij"), Ok(("hij", "abc")));
/// assert_eq!(parser.parse_complete(""), Err(Err::Error(("", ErrorKind::Tag))));
/// assert_eq!(parser.parse_complete("123"), Err(Err::Error(("123", ErrorKind::Tag))));
/// ```
pub fn terminated<I, O, E: ParseError<I>, F, G>(
  first: F,
  second: G,
) -> impl Parser<I, Output = O, Error = E>
where
  F: Parser<I, Output = O, Error = E>,
  G: Parser<I, Error = E>,
{
  Terminated {
    f: first,
    g: second,
  }
}

/// Parser wrapper for `terminated`
pub struct Terminated<F, G> {
  f: F,
  g: G,
}

impl<I, E: ParseError<I>, F: Parser<I, Error = E>, G: Parser<I, Error = E>> Parser<I>
  for Terminated<F, G>
{
  type Output = <F as Parser<I>>::Output;
  type Error = E;

  #[inline(always)]
  fn process<OM: OutputMode>(&mut self, i: I) -> PResult<OM, I, Self::Output, Self::Error> {
    let (i, o1) = self.f.process::<OM>(i)?;
    let (i, ()) = self
      .g
      .process::<OutputM<Check, OM::Error, OM::Incomplete>>(i)?;

    Ok((i, o1))
  }
}

/// Gets an object from the first parser,
/// then matches an object from the sep_parser and discards it,
/// then gets another object from the second parser.
///
/// # Arguments
/// * `first` The first parser to apply.
/// * `sep` The separator parser to apply.
/// * `second` The second parser to apply.
///
/// ```rust
/// # use loqom::{Err, error::ErrorKind, Needed, Parser};
/// # use loqom::Needed::Size;
/// use loqom::sequence::separated_pair;
/// use loqom::bytes::tag;
///
/// let mut parser = separated_pair(tag("abc"), tag("|"), tag("efg"));
///
/// assert_eq!(parser.parse_complete("abc|efg"), Ok(("", ("abc", "efg"))));
/// assert_eq!(parser.parse_complete("abc|efghij"), Ok(("hij", ("abc", "efg"))));
/// assert_eq!(parser.parse_complete(""), Err(Err::Error(("", ErrorKind::Tag))));
/// assert_eq!(parser.parse_complete("123"), Err(Err::Error(("123", ErrorKind::Tag))));
/// ```
pub fn separated_pair<I, O1, O2, E: ParseError<I>, F, G, H>(
  first: F,
  sep: G,
  second: H,
) -> impl Parser<I, Output = (O1, O2), Error = E>
where
  F: Parser<I, Output = O1, Error = E>,
  G: Parser<I, Error = E>,
  H: Parser<I, Output = O2, Error = E>,
{
  first.and(preceded(sep, second))
}

/// Matches an object from the first parser and discards it,
/// then gets an object from the second parser,
/// and finally matches an object from the third parser and discards it.
///
/// # Arguments
/// * `first` The first parser to apply and discard.
/// * `second` The second parser to apply.
/// * `third` The third parser to apply and discard.
///
/// ```rust
/// # use loqom::{Err, error::ErrorKind, Needed, Parser};
/// # use loqom::Needed::Size;
/// use loqom::sequence::delimited;
/// use loqom::bytes::tag;
///
/// let mut parser = delimited(tag("("), tag("abc"), tag(")"));
///
/// assert_eq!(parser.parse_complete("(abc)"), Ok(("", "abc")));
/// assert_eq!(parser.parse_complete("(abc)def"), Ok(("def", "abc")));
/// assert_eq!(parser.parse_complete(""), Err(Err::Error(("", ErrorKind::Tag))));
/// assert_eq!(parser.parse_complete("123"), Err(Err::Error(("123", ErrorKind::Tag))));
/// ```
pub fn delimited<I, O, E: ParseError<I>, F, G, H>(
  first: F,
  second: G,
  third: H,
) -> impl Parser<I, Output = O, Error = E>
where
  F: Parser<I, Error = E>,
  G: Parser<I, Output = O, Error = E>,
  H: Parser<I, Error = E>,
{
  preceded(first, terminated(second, third))
}