Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs
This commit is contained in:
Vendored
+661
@@ -0,0 +1,661 @@
|
||||
//! Module containing parsers specialized on byte streams.
|
||||
|
||||
use crate::{
|
||||
error::{self, ParseResult::*},
|
||||
parser::{
|
||||
combinator::no_partial,
|
||||
range::{take_fn, TakeRange},
|
||||
repeat::skip_many,
|
||||
token::{satisfy, token, tokens_cmp, Token},
|
||||
},
|
||||
stream::{RangeStream, Stream},
|
||||
Parser,
|
||||
};
|
||||
|
||||
/// Parses a byte and succeeds if the byte is equal to `c`.
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::byte;
|
||||
/// assert_eq!(byte(b'!').parse(&b"!"[..]), Ok((b'!', &b""[..])));
|
||||
/// assert!(byte(b'A').parse(&b""[..]).is_err());
|
||||
/// assert!(byte(b'A').parse(&b"!"[..]).is_err());
|
||||
/// ```
|
||||
pub fn byte<Input>(c: u8) -> Token<Input>
|
||||
where
|
||||
Input: Stream<Token = u8>,
|
||||
{
|
||||
token(c)
|
||||
}
|
||||
|
||||
macro_rules! byte_parser {
|
||||
($name:ident, $ty:ident, $f: ident) => {{
|
||||
satisfy(|c: u8| c.$f())
|
||||
.expected(stringify!($name))
|
||||
}};
|
||||
($name:ident, $ty:ident, $f: ident $($args:tt)+) => {{
|
||||
satisfy(|c: u8| c.$f $($args)+)
|
||||
.expected(stringify!($name))
|
||||
}};
|
||||
}
|
||||
|
||||
/// Parses a base-10 digit (0–9).
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::digit;
|
||||
/// assert_eq!(digit().parse(&b"9"[..]), Ok((b'9', &b""[..])));
|
||||
/// assert!(digit().parse(&b"A"[..]).is_err());
|
||||
/// ```
|
||||
pub fn digit<Input>() -> impl Parser<Input, Output = u8, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = u8>,
|
||||
{
|
||||
byte_parser!(digit, Digit, is_ascii_digit())
|
||||
}
|
||||
|
||||
/// Parses a `b' '`, `b'\t'`, `b'\n'` or `'b\'r'`.
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::space;
|
||||
/// assert_eq!(space().parse(&b" "[..]), Ok((b' ', &b""[..])));
|
||||
/// assert_eq!(space().parse(&b" "[..]), Ok((b' ', &b" "[..])));
|
||||
/// assert!(space().parse(&b"!"[..]).is_err());
|
||||
/// assert!(space().parse(&b""[..]).is_err());
|
||||
/// ```
|
||||
pub fn space<Input>() -> impl Parser<Input, Output = u8, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = u8>,
|
||||
{
|
||||
byte_parser!(space, Space, is_ascii_whitespace)
|
||||
}
|
||||
|
||||
/// Skips over [`space`] zero or more times
|
||||
///
|
||||
/// [`space`]: fn.space.html
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::spaces;
|
||||
/// assert_eq!(spaces().parse(&b""[..]), Ok(((), &b""[..])));
|
||||
/// assert_eq!(spaces().parse(&b" "[..]), Ok(((), &b""[..])));
|
||||
/// ```
|
||||
pub fn spaces<Input>() -> impl Parser<Input, Output = ()>
|
||||
where
|
||||
Input: Stream<Token = u8>,
|
||||
{
|
||||
skip_many(space()).expected("whitespaces")
|
||||
}
|
||||
|
||||
/// Parses a newline byte (`b'\n'`).
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::newline;
|
||||
/// assert_eq!(newline().parse(&b"\n"[..]), Ok((b'\n', &b""[..])));
|
||||
/// assert!(newline().parse(&b"\r"[..]).is_err());
|
||||
/// ```
|
||||
pub fn newline<Input>() -> impl Parser<Input, Output = u8, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = u8>,
|
||||
{
|
||||
satisfy(|ch: u8| ch == b'\n').expected("lf newline")
|
||||
}
|
||||
|
||||
/// Parses carriage return and newline (`&b"\r\n"`), returning the newline byte.
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::crlf;
|
||||
/// assert_eq!(crlf().parse(&b"\r\n"[..]), Ok((b'\n', &b""[..])));
|
||||
/// assert!(crlf().parse(&b"\r"[..]).is_err());
|
||||
/// assert!(crlf().parse(&b"\n"[..]).is_err());
|
||||
/// ```
|
||||
pub fn crlf<Input>() -> impl Parser<Input, Output = u8, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = u8>,
|
||||
{
|
||||
no_partial(satisfy(|ch: u8| ch == b'\r').with(newline())).expected("crlf newline")
|
||||
}
|
||||
|
||||
/// Parses a tab byte (`b'\t'`).
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::tab;
|
||||
/// assert_eq!(tab().parse(&b"\t"[..]), Ok((b'\t', &b""[..])));
|
||||
/// assert!(tab().parse(&b" "[..]).is_err());
|
||||
/// ```
|
||||
pub fn tab<Input>() -> impl Parser<Input, Output = u8, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = u8>,
|
||||
{
|
||||
satisfy(|ch| ch == b'\t').expected("tab")
|
||||
}
|
||||
|
||||
/// Parses an uppercase ASCII letter (A–Z).
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::upper;
|
||||
/// assert_eq!(upper().parse(&b"A"[..]), Ok((b'A', &b""[..])));
|
||||
/// assert!(upper().parse(&b"a"[..]).is_err());
|
||||
/// ```
|
||||
pub fn upper<Input>() -> impl Parser<Input, Output = u8, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = u8>,
|
||||
{
|
||||
byte_parser!(upper, Upper, is_ascii_uppercase)
|
||||
}
|
||||
|
||||
/// Parses an lowercase ASCII letter (a–z).
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::lower;
|
||||
/// assert_eq!(lower().parse(&b"a"[..]), Ok((b'a', &b""[..])));
|
||||
/// assert!(lower().parse(&b"A"[..]).is_err());
|
||||
/// ```
|
||||
pub fn lower<Input>() -> impl Parser<Input, Output = u8, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = u8>,
|
||||
{
|
||||
byte_parser!(lower, Lower, is_ascii_lowercase)
|
||||
}
|
||||
|
||||
/// Parses either an ASCII alphabet letter or digit (a–z, A–Z, 0–9).
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::alpha_num;
|
||||
/// assert_eq!(alpha_num().parse(&b"A"[..]), Ok((b'A', &b""[..])));
|
||||
/// assert_eq!(alpha_num().parse(&b"1"[..]), Ok((b'1', &b""[..])));
|
||||
/// assert!(alpha_num().parse(&b"!"[..]).is_err());
|
||||
/// ```
|
||||
pub fn alpha_num<Input>() -> impl Parser<Input, Output = u8, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = u8>,
|
||||
{
|
||||
byte_parser!(alpha_num, AlphaNum, is_ascii_alphanumeric)
|
||||
}
|
||||
|
||||
/// Parses an ASCII alphabet letter (a–z, A–Z).
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::letter;
|
||||
/// assert_eq!(letter().parse(&b"a"[..]), Ok((b'a', &b""[..])));
|
||||
/// assert_eq!(letter().parse(&b"A"[..]), Ok((b'A', &b""[..])));
|
||||
/// assert!(letter().parse(&b"9"[..]).is_err());
|
||||
/// ```
|
||||
pub fn letter<Input>() -> impl Parser<Input, Output = u8, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = u8>,
|
||||
{
|
||||
byte_parser!(letter, Letter, is_ascii_alphabetic)
|
||||
}
|
||||
|
||||
/// Parses an octal digit.
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::oct_digit;
|
||||
/// assert_eq!(oct_digit().parse(&b"7"[..]), Ok((b'7', &b""[..])));
|
||||
/// assert!(oct_digit().parse(&b"8"[..]).is_err());
|
||||
/// ```
|
||||
pub fn oct_digit<Input>() -> impl Parser<Input, Output = u8, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = u8>,
|
||||
{
|
||||
satisfy(|ch| (b'0'..=b'7').contains(&ch)).expected("octal digit")
|
||||
}
|
||||
|
||||
/// Parses an ASCII hexdecimal digit (accepts both uppercase and lowercase).
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::hex_digit;
|
||||
/// assert_eq!(hex_digit().parse(&b"F"[..]), Ok((b'F', &b""[..])));
|
||||
/// assert!(hex_digit().parse(&b"H"[..]).is_err());
|
||||
/// ```
|
||||
pub fn hex_digit<Input>() -> impl Parser<Input, Output = u8, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = u8>,
|
||||
{
|
||||
byte_parser!(hex_digit, HexDigit, is_ascii_hexdigit())
|
||||
}
|
||||
|
||||
parser! {
|
||||
/// Parses the bytes `s`.
|
||||
///
|
||||
/// If you have a stream implementing [`RangeStream`] such as `&[u8]` you can also use the
|
||||
/// [`range`] parser which may be more efficient.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::parser::byte::bytes;
|
||||
/// # fn main() {
|
||||
/// let result = bytes(&b"rust"[..])
|
||||
/// .parse(&b"rust"[..])
|
||||
/// .map(|x| x.0);
|
||||
/// assert_eq!(result, Ok(&b"rust"[..]));
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`RangeStream`]: super::super::stream::RangeStream
|
||||
/// [`range`]: super::range::range
|
||||
pub fn bytes['a, 'b, Input](s: &'static [u8])(Input) -> &'a [u8]
|
||||
where [
|
||||
Input: Stream<Token = u8, Range = &'b [u8]>,
|
||||
]
|
||||
{
|
||||
bytes_cmp(s, |l: u8, r: u8| l == r)
|
||||
}
|
||||
}
|
||||
|
||||
parser! {
|
||||
/// Parses the bytes `s` using `cmp` to compare each token.
|
||||
///
|
||||
/// If you have a stream implementing [`RangeStream`] such as `&[u8]` you can also use the
|
||||
/// [`range`] parser which may be more efficient.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::parser::byte::bytes_cmp;
|
||||
/// # use combine::stream::easy::Info;
|
||||
/// # fn main() {
|
||||
/// let result = bytes_cmp(&b"abc"[..], |l, r| l.eq_ignore_ascii_case(&r))
|
||||
/// .parse(&b"AbC"[..]);
|
||||
/// assert_eq!(result, Ok((&b"abc"[..], &b""[..])));
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`RangeStream`]: super::super::stream::RangeStream
|
||||
/// [`range`]: super::range::range
|
||||
pub fn bytes_cmp['a, 'b, C, Input](s: &'static [u8], cmp: C)(Input) -> &'a [u8]
|
||||
where [
|
||||
C: FnMut(u8, u8) -> bool,
|
||||
Input: Stream<Token = u8, Range = &'b [u8]>,
|
||||
]
|
||||
{
|
||||
let s = *s;
|
||||
tokens_cmp(s.iter().cloned(), cmp)
|
||||
.map(move |_| s)
|
||||
.expected(error::Range(s))
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! take_until {
|
||||
(
|
||||
$(#[$attr:meta])*
|
||||
$type_name: ident, $func_name: ident, $memchr: ident, $($param: ident),+
|
||||
) => {
|
||||
parser!{
|
||||
#[derive(Clone)]
|
||||
pub struct $type_name;
|
||||
type PartialState = usize;
|
||||
$(#[$attr])*
|
||||
pub fn $func_name[Input]($($param : u8),*)(Input) -> Input::Range
|
||||
where [
|
||||
Input: RangeStream,
|
||||
Input::Range: AsRef<[u8]> + crate::stream::Range,
|
||||
]
|
||||
{
|
||||
take_fn(move |haystack: Input::Range| {
|
||||
let haystack = haystack.as_ref();
|
||||
match ::memchr::$memchr( $(*$param),+ , haystack) {
|
||||
Some(i) => TakeRange::Found(i),
|
||||
None => TakeRange::NotFound(haystack.len()),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
take_until! {
|
||||
/// Zero-copy parser which reads a range of 0 or more tokens until `a` is found.
|
||||
///
|
||||
/// If `a` is not found, the parser will return an error.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::parser::byte::take_until_byte;
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let mut parser = take_until_byte(b'\r');
|
||||
/// let result = parser.parse("To: user@example.com\r\n");
|
||||
/// assert_eq!(result, Ok(("To: user@example.com", "\r\n")));
|
||||
/// let result = parser.parse("Hello, world\n");
|
||||
/// assert!(result.is_err());
|
||||
/// # }
|
||||
/// ```
|
||||
TakeUntilByte, take_until_byte, memchr, a
|
||||
}
|
||||
take_until! {
|
||||
/// Zero-copy parser which reads a range of 0 or more tokens until `a` or `b` is found.
|
||||
///
|
||||
/// If `a` or `b` is not found, the parser will return an error.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::parser::byte::take_until_byte2;
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let mut parser = take_until_byte2(b'\r', b'\n');
|
||||
/// let result = parser.parse("To: user@example.com\r\n");
|
||||
/// assert_eq!(result, Ok(("To: user@example.com", "\r\n")));
|
||||
/// let result = parser.parse("Hello, world\n");
|
||||
/// assert_eq!(result, Ok(("Hello, world", "\n")));
|
||||
/// # }
|
||||
/// ```
|
||||
TakeUntilByte2, take_until_byte2, memchr2, a, b
|
||||
}
|
||||
take_until! {
|
||||
/// Zero-copy parser which reads a range of 0 or more tokens until `a`, 'b' or `c` is found.
|
||||
///
|
||||
/// If `a`, 'b' or `c` is not found, the parser will return an error.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::parser::byte::take_until_byte3;
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let mut parser = take_until_byte3(b'\r', b'\n', b' ');
|
||||
/// let result = parser.parse("To: user@example.com\r\n");
|
||||
/// assert_eq!(result, Ok(("To:", " user@example.com\r\n")));
|
||||
/// let result = parser.parse("Helloworld");
|
||||
/// assert!(result.is_err());
|
||||
/// # }
|
||||
/// ```
|
||||
TakeUntilByte3, take_until_byte3, memchr3, a, b, c
|
||||
}
|
||||
|
||||
parser! {
|
||||
type PartialState = usize;
|
||||
/// Zero-copy parser which reads a range of 0 or more tokens until `needle` is found.
|
||||
///
|
||||
/// If `a`, 'b' or `c` is not found, the parser will return an error.
|
||||
///
|
||||
/// Optimized variant of [`take_until_range`](../range/fn.take_until_range.html)
|
||||
///
|
||||
/// ```
|
||||
/// use combine::*;
|
||||
/// use combine::parser::byte::take_until_bytes;
|
||||
/// assert_eq!(
|
||||
/// take_until_bytes(&b"\r\n"[..]).easy_parse(&b"abc\r\n"[..]).map(|(x, _)| x),
|
||||
/// Ok((&b"abc"[..]))
|
||||
/// );
|
||||
/// // Also works on strings as long as `needle` is UTF-8
|
||||
/// assert_eq!(
|
||||
/// take_until_bytes("\r\n".as_bytes()).easy_parse("abc\r\n").map(|(x, _)| x),
|
||||
/// Ok(("abc"))
|
||||
/// );
|
||||
/// ```
|
||||
pub fn take_until_bytes['a, Input](needle: &'a [u8])(Input) -> Input::Range
|
||||
where [
|
||||
Input: RangeStream,
|
||||
Input::Range: AsRef<[u8]> + crate::stream::Range,
|
||||
]
|
||||
{
|
||||
take_fn(move |haystack: Input::Range| {
|
||||
let haystack = haystack.as_ref();
|
||||
match memslice(needle, haystack) {
|
||||
Some(i) => TakeRange::Found(i),
|
||||
None => TakeRange::NotFound(haystack.len().saturating_sub(needle.len() - 1)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn memslice(needle: &[u8], haystack: &[u8]) -> Option<usize> {
|
||||
let (&prefix, suffix) = match needle.split_first() {
|
||||
Some(x) => x,
|
||||
None => return Some(0),
|
||||
};
|
||||
for i in memchr::memchr_iter(prefix, haystack) {
|
||||
if haystack[i + 1..].starts_with(suffix) {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Parsers for decoding numbers in big-endian or little-endian order.
|
||||
pub mod num {
|
||||
|
||||
use crate::{error::ResultExt, lib::mem::size_of, parser::function::parser, stream::uncons};
|
||||
|
||||
use super::*;
|
||||
|
||||
macro_rules! integer_parser {
|
||||
(
|
||||
$(#[$attr:meta])*
|
||||
pub $type_name: ident,
|
||||
$output_type: ident, $be_name: ident, $le_name: ident, $read_name: ident
|
||||
) => {
|
||||
$(#[$attr])*
|
||||
pub fn $be_name<'a, Input>() -> impl Parser<Input, Output = $output_type, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = u8>,
|
||||
{
|
||||
parser(|input: &mut Input| {
|
||||
let checkpoint = input.checkpoint();
|
||||
let result = (|input: &mut Input| {
|
||||
let mut buffer = [0u8; size_of::<$output_type>()];
|
||||
for elem in &mut buffer[..] {
|
||||
*elem = ctry!(uncons(input)).0;
|
||||
}
|
||||
CommitOk($output_type::from_be_bytes(buffer))
|
||||
})(input);
|
||||
if result.is_err() {
|
||||
input.reset(checkpoint).committed().into_result()?;
|
||||
}
|
||||
result.into_result()
|
||||
})
|
||||
}
|
||||
|
||||
$(#[$attr])*
|
||||
pub fn $le_name<'a, Input>() -> impl Parser<Input, Output = $output_type, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = u8>,
|
||||
{
|
||||
parser(|input: &mut Input| {
|
||||
let checkpoint = input.checkpoint();
|
||||
let result = (|input: &mut Input| {
|
||||
let mut buffer = [0u8; size_of::<$output_type>()];
|
||||
for elem in &mut buffer[..] {
|
||||
*elem = ctry!(uncons(input)).0;
|
||||
}
|
||||
CommitOk($output_type::from_le_bytes(buffer))
|
||||
})(input);
|
||||
if result.is_err() {
|
||||
input.reset(checkpoint).committed().into_result()?;
|
||||
}
|
||||
result.into_result()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
integer_parser!(
|
||||
/// Reads a u16 out of the byte stream with the specified endianess
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::num::le_u16;
|
||||
///
|
||||
/// assert_eq!(le_u16().parse(&b"\x01\0"[..]), Ok((1, &b""[..])));
|
||||
/// assert!(le_u16().parse(&b"\0"[..]).is_err());
|
||||
/// ```
|
||||
pub U16, u16, be_u16, le_u16, read_u16
|
||||
);
|
||||
integer_parser!(
|
||||
/// Reads a u32 out of the byte stream with the specified endianess
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::num::le_u32;
|
||||
///
|
||||
/// assert_eq!(le_u32().parse(&b"\x01\0\0\0"[..]), Ok((1, &b""[..])));
|
||||
/// assert!(le_u32().parse(&b"\x01\0\0"[..]).is_err());
|
||||
/// ```
|
||||
pub U32, u32, be_u32, le_u32, read_u32
|
||||
);
|
||||
integer_parser!(
|
||||
/// Reads a u64 out of the byte stream with the specified endianess
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::num::le_u64;
|
||||
///
|
||||
/// assert_eq!(le_u64().parse(&b"\x01\0\0\0\0\0\0\0"[..]), Ok((1, &b""[..])));
|
||||
/// assert!(le_u64().parse(&b"\x01\0\0\0\0\0\0"[..]).is_err());
|
||||
/// ```
|
||||
pub U64, u64, be_u64, le_u64, read_u64
|
||||
);
|
||||
|
||||
integer_parser!(
|
||||
/// Reads a i16 out of the byte stream with the specified endianess
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::num::le_i16;
|
||||
///
|
||||
/// assert_eq!(le_i16().parse(&b"\x01\0"[..]), Ok((1, &b""[..])));
|
||||
/// assert!(le_i16().parse(&b"\x01"[..]).is_err());
|
||||
/// ```
|
||||
pub I16, i16, be_i16, le_i16, read_i16
|
||||
);
|
||||
|
||||
integer_parser!(
|
||||
/// Reads a i32 out of the byte stream with the specified endianess
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::num::le_i32;
|
||||
///
|
||||
/// assert_eq!(le_i32().parse(&b"\x01\0\0\0"[..]), Ok((1, &b""[..])));
|
||||
/// assert!(le_i32().parse(&b"\x01\0\0"[..]).is_err());
|
||||
/// ```
|
||||
pub I32, i32, be_i32, le_i32, read_i32
|
||||
);
|
||||
integer_parser!(
|
||||
/// Reads a i64 out of the byte stream with the specified endianess
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::num::le_i64;
|
||||
///
|
||||
/// assert_eq!(le_i64().parse(&b"\x01\0\0\0\0\0\0\0"[..]), Ok((1, &b""[..])));
|
||||
/// assert!(le_i64().parse(&b"\x01\0\0\0\0\0\0"[..]).is_err());
|
||||
/// ```
|
||||
pub I64, i64, be_i64, le_i64, read_i64
|
||||
);
|
||||
|
||||
integer_parser!(
|
||||
/// Reads a i32 out of the byte stream with the specified endianess
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::num::le_f32;
|
||||
///
|
||||
/// let buf = 123.45f32.to_le_bytes();
|
||||
/// assert_eq!(le_f32().parse(&buf[..]), Ok((123.45, &b""[..])));
|
||||
/// assert!(le_f32().parse(&b"\x01\0\0"[..]).is_err());
|
||||
/// ```
|
||||
pub F32, f32, be_f32, le_f32, read_f32
|
||||
);
|
||||
integer_parser!(
|
||||
/// Reads a i64 out of the byte stream with the specified endianess
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::byte::num::le_f64;
|
||||
///
|
||||
/// let buf = 123.45f64.to_le_bytes();
|
||||
/// assert_eq!(le_f64().parse(&buf[..]), Ok((123.45, &b""[..])));
|
||||
/// assert!(le_f64().parse(&b"\x01\0\0\0\0\0\0"[..]).is_err());
|
||||
/// ```
|
||||
pub F64, f64, be_f64, le_f64, read_f64
|
||||
);
|
||||
|
||||
#[cfg(all(feature = "std", test))]
|
||||
mod tests {
|
||||
|
||||
use crate::stream::{buffered, position, IteratorStream};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn no_rangestream() {
|
||||
let buf = 123.45f64.to_le_bytes();
|
||||
assert_eq!(
|
||||
le_f64()
|
||||
.parse(buffered::Stream::new(
|
||||
position::Stream::new(IteratorStream::new(buf.iter().cloned())),
|
||||
1
|
||||
))
|
||||
.map(|(t, _)| t),
|
||||
Ok(123.45)
|
||||
);
|
||||
assert_eq!(
|
||||
le_f64()
|
||||
.parse(buffered::Stream::new(
|
||||
position::Stream::new(IteratorStream::new(buf.iter().cloned())),
|
||||
1
|
||||
))
|
||||
.map(|(t, _)| t),
|
||||
Ok(123.45)
|
||||
);
|
||||
let buf = 123.45f64.to_be_bytes();
|
||||
assert_eq!(
|
||||
be_f64()
|
||||
.parse(buffered::Stream::new(
|
||||
position::Stream::new(IteratorStream::new(buf.iter().cloned())),
|
||||
1
|
||||
))
|
||||
.map(|(t, _)| t),
|
||||
Ok(123.45)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "std", test))]
|
||||
mod tests {
|
||||
|
||||
use crate::stream::{buffered, position, read};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn memslice_basic() {
|
||||
let haystack = b"abc123";
|
||||
assert_eq!(memslice(b"", haystack), Some(0));
|
||||
assert_eq!(memslice(b"a", haystack), Some(0));
|
||||
assert_eq!(memslice(b"ab", haystack), Some(0));
|
||||
assert_eq!(memslice(b"c12", haystack), Some(2));
|
||||
|
||||
let haystack2 = b"abcab2";
|
||||
assert_eq!(memslice(b"abc", haystack2), Some(0));
|
||||
assert_eq!(memslice(b"ab2", haystack2), Some(3));
|
||||
|
||||
let haystack3 = b"aaabaaaa";
|
||||
assert_eq!(memslice(b"aaaa", haystack3), Some(4));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bytes_read_stream() {
|
||||
assert!(bytes(b"abc")
|
||||
.parse(buffered::Stream::new(
|
||||
position::Stream::new(read::Stream::new("abc".as_bytes())),
|
||||
1
|
||||
))
|
||||
.is_ok());
|
||||
}
|
||||
}
|
||||
Vendored
+319
@@ -0,0 +1,319 @@
|
||||
//! Module containing parsers specialized on character streams.
|
||||
|
||||
use crate::{
|
||||
parser::{
|
||||
combinator::no_partial,
|
||||
repeat::skip_many,
|
||||
token::{satisfy, token, tokens_cmp, Token},
|
||||
},
|
||||
stream::Stream,
|
||||
Parser,
|
||||
};
|
||||
|
||||
/// Parses a character and succeeds if the character is equal to `c`.
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::char::char;
|
||||
/// assert_eq!(char('!').parse("!"), Ok(('!', "")));
|
||||
/// assert!(char('A').parse("!").is_err());
|
||||
/// ```
|
||||
pub fn char<Input>(c: char) -> Token<Input>
|
||||
where
|
||||
Input: Stream<Token = char>,
|
||||
{
|
||||
token(c)
|
||||
}
|
||||
|
||||
parser! {
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Digit;
|
||||
/// Parses a base-10 digit.
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::char::digit;
|
||||
/// assert_eq!(digit().parse("9"), Ok(('9', "")));
|
||||
/// assert!(digit().parse("A").is_err());
|
||||
/// ```
|
||||
pub fn digit[Input]()(Input) -> char
|
||||
where
|
||||
[Input: Stream<Token = char>,]
|
||||
{
|
||||
satisfy(|c: char| c.is_digit(10)).expected("digit")
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a single whitespace according to [`std::char::is_whitespace`].
|
||||
///
|
||||
/// This includes space characters, tabs and newlines.
|
||||
///
|
||||
/// [`std::char::is_whitespace`]: https://doc.rust-lang.org/std/primitive.char.html#method.is_whitespace
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::char::space;
|
||||
/// assert_eq!(space().parse(" "), Ok((' ', "")));
|
||||
/// assert_eq!(space().parse(" "), Ok((' ', " ")));
|
||||
/// assert!(space().parse("!").is_err());
|
||||
/// assert!(space().parse("").is_err());
|
||||
/// ```
|
||||
pub fn space<Input>() -> impl Parser<Input, Output = char, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = char>,
|
||||
{
|
||||
let f: fn(char) -> bool = char::is_whitespace;
|
||||
satisfy(f).expected("whitespace")
|
||||
}
|
||||
|
||||
/// Skips over zero or more spaces according to [`std::char::is_whitespace`].
|
||||
///
|
||||
/// This includes space characters, tabs and newlines.
|
||||
///
|
||||
/// [`std::char::is_whitespace`]: https://doc.rust-lang.org/std/primitive.char.html#method.is_whitespace
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::char::spaces;
|
||||
/// assert_eq!(spaces().parse(""), Ok(((), "")));
|
||||
/// assert_eq!(spaces().parse(" "), Ok(((), "")));
|
||||
/// ```
|
||||
pub fn spaces<Input>() -> impl Parser<Input, Output = ()>
|
||||
where
|
||||
Input: Stream<Token = char>,
|
||||
{
|
||||
skip_many(space()).expected("whitespaces")
|
||||
}
|
||||
|
||||
/// Parses a newline character (`'\n'`).
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::char::newline;
|
||||
/// assert_eq!(newline().parse("\n"), Ok(('\n', "")));
|
||||
/// assert!(newline().parse("\r").is_err());
|
||||
/// ```
|
||||
pub fn newline<Input>() -> impl Parser<Input, Output = char, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = char>,
|
||||
{
|
||||
satisfy(|ch: char| ch == '\n').expected("lf newline")
|
||||
}
|
||||
|
||||
/// Parses carriage return and newline (`"\r\n"`), returning the newline character.
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::char::crlf;
|
||||
/// assert_eq!(crlf().parse("\r\n"), Ok(('\n', "")));
|
||||
/// assert!(crlf().parse("\r").is_err());
|
||||
/// assert!(crlf().parse("\n").is_err());
|
||||
/// ```
|
||||
pub fn crlf<Input>() -> impl Parser<Input, Output = char, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = char>,
|
||||
{
|
||||
no_partial(satisfy(|ch: char| ch == '\r').with(newline())).expected("crlf newline")
|
||||
}
|
||||
|
||||
/// Parses a tab character (`'\t'`).
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::char::tab;
|
||||
/// assert_eq!(tab().parse("\t"), Ok(('\t', "")));
|
||||
/// assert!(tab().parse(" ").is_err());
|
||||
/// ```
|
||||
pub fn tab<Input>() -> impl Parser<Input, Output = char, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = char>,
|
||||
{
|
||||
satisfy(|ch: char| ch == '\t').expected("tab")
|
||||
}
|
||||
|
||||
/// Parses an uppercase letter according to [`std::char::is_uppercase`].
|
||||
///
|
||||
/// [`std::char::is_uppercase`]: https://doc.rust-lang.org/std/primitive.char.html#method.is_uppercase
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::char::upper;
|
||||
/// assert_eq!(upper().parse("A"), Ok(('A', "")));
|
||||
/// assert!(upper().parse("a").is_err());
|
||||
/// ```
|
||||
pub fn upper<Input>() -> impl Parser<Input, Output = char, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = char>,
|
||||
{
|
||||
satisfy(|ch: char| ch.is_uppercase()).expected("uppercase letter")
|
||||
}
|
||||
|
||||
/// Parses an lowercase letter according to [`std::char::is_lowercase`].
|
||||
///
|
||||
/// [`std::char::is_lowercase`]: https://doc.rust-lang.org/std/primitive.char.html#method.is_lowercase
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::char::lower;
|
||||
/// assert_eq!(lower().parse("a"), Ok(('a', "")));
|
||||
/// assert!(lower().parse("A").is_err());
|
||||
/// ```
|
||||
pub fn lower<Input>() -> impl Parser<Input, Output = char, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = char>,
|
||||
{
|
||||
satisfy(|ch: char| ch.is_lowercase()).expected("lowercase letter")
|
||||
}
|
||||
|
||||
/// Parses either an alphabet letter or digit according to [`std::char::is_alphanumeric`].
|
||||
///
|
||||
/// [`std::char::is_alphanumeric`]: https://doc.rust-lang.org/std/primitive.char.html#method.is_alphanumeric
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::char::alpha_num;
|
||||
/// assert_eq!(alpha_num().parse("A"), Ok(('A', "")));
|
||||
/// assert_eq!(alpha_num().parse("1"), Ok(('1', "")));
|
||||
/// assert!(alpha_num().parse("!").is_err());
|
||||
/// ```
|
||||
pub fn alpha_num<Input>() -> impl Parser<Input, Output = char, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = char>,
|
||||
{
|
||||
satisfy(|ch: char| ch.is_alphanumeric()).expected("letter or digit")
|
||||
}
|
||||
|
||||
/// Parses an alphabet letter according to [`std::char::is_alphabetic`].
|
||||
///
|
||||
/// [`std::char::is_alphabetic`]: https://doc.rust-lang.org/std/primitive.char.html#method.is_alphabetic
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::char::letter;
|
||||
/// assert_eq!(letter().parse("a"), Ok(('a', "")));
|
||||
/// assert_eq!(letter().parse("A"), Ok(('A', "")));
|
||||
/// assert!(letter().parse("9").is_err());
|
||||
/// ```
|
||||
pub fn letter<Input>() -> impl Parser<Input, Output = char, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = char>,
|
||||
{
|
||||
satisfy(|ch: char| ch.is_alphabetic()).expected("letter")
|
||||
}
|
||||
|
||||
/// Parses an octal digit.
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::char::oct_digit;
|
||||
/// assert_eq!(oct_digit().parse("7"), Ok(('7', "")));
|
||||
/// assert!(oct_digit().parse("8").is_err());
|
||||
/// ```
|
||||
pub fn oct_digit<Input>() -> impl Parser<Input, Output = char, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = char>,
|
||||
{
|
||||
satisfy(|ch: char| ch.is_digit(8)).expected("octal digit")
|
||||
}
|
||||
|
||||
/// Parses a hexdecimal digit with uppercase and lowercase.
|
||||
///
|
||||
/// ```
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::char::hex_digit;
|
||||
/// assert_eq!(hex_digit().parse("F"), Ok(('F', "")));
|
||||
/// assert!(hex_digit().parse("H").is_err());
|
||||
/// ```
|
||||
pub fn hex_digit<Input>() -> impl Parser<Input, Output = char, PartialState = ()>
|
||||
where
|
||||
Input: Stream<Token = char>,
|
||||
{
|
||||
satisfy(|ch: char| ch.is_digit(0x10)).expected("hexadecimal digit")
|
||||
}
|
||||
|
||||
/// Parses the string `s`.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::parser::char::string;
|
||||
/// # fn main() {
|
||||
/// let result = string("rust")
|
||||
/// .parse("rust")
|
||||
/// .map(|x| x.0);
|
||||
/// assert_eq!(result, Ok("rust"));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn string<'a, Input>(s: &'static str) -> impl Parser<Input, Output = &'a str>
|
||||
where
|
||||
Input: Stream<Token = char>,
|
||||
{
|
||||
string_cmp(s, |l, r| l == r)
|
||||
}
|
||||
|
||||
/// Parses the string `s`, using `cmp` to compare each character.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::parser::char::string_cmp;
|
||||
/// # fn main() {
|
||||
/// let result = string_cmp("rust", |l, r| l.eq_ignore_ascii_case(&r))
|
||||
/// .parse("RusT")
|
||||
/// .map(|x| x.0);
|
||||
/// assert_eq!(result, Ok("rust"));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn string_cmp<'a, C, Input>(s: &'static str, cmp: C) -> impl Parser<Input, Output = &'a str>
|
||||
where
|
||||
C: FnMut(char, char) -> bool,
|
||||
Input: Stream<Token = char>,
|
||||
{
|
||||
tokens_cmp(s.chars(), cmp).map(move |_| s).expected(s)
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "std", test))]
|
||||
mod tests {
|
||||
|
||||
use crate::{
|
||||
parser::EasyParser,
|
||||
stream::{
|
||||
easy::{Error, Errors},
|
||||
position::{self, SourcePosition},
|
||||
},
|
||||
};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn space_error() {
|
||||
let result = space().easy_parse("");
|
||||
assert!(result.is_err());
|
||||
assert_eq!(
|
||||
result.unwrap_err().errors,
|
||||
vec![Error::end_of_input(), Error::Expected("whitespace".into())]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn string_committed() {
|
||||
let result = string("a").easy_parse(position::Stream::new("b"));
|
||||
assert!(result.is_err());
|
||||
assert_eq!(
|
||||
result.unwrap_err().position,
|
||||
SourcePosition { line: 1, column: 1 }
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn string_error() {
|
||||
let result = string("abc").easy_parse(position::Stream::new("bc"));
|
||||
assert_eq!(
|
||||
result,
|
||||
Err(Errors {
|
||||
position: SourcePosition { line: 1, column: 1 },
|
||||
errors: vec![Error::Unexpected('b'.into()), Error::Expected("abc".into())],
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
Vendored
+850
@@ -0,0 +1,850 @@
|
||||
//! Combinators which take one or more parsers and attempts to parse successfully with at least one
|
||||
//! of them.
|
||||
|
||||
use crate::{
|
||||
error::{
|
||||
ParseError,
|
||||
ParseResult::{self, *},
|
||||
ResultExt, StreamError, Tracked,
|
||||
},
|
||||
parser::ParseMode,
|
||||
ErrorOffset, Parser, Stream, StreamOnce,
|
||||
};
|
||||
|
||||
/// Takes a number of parsers and tries to apply them each in order.
|
||||
/// Fails if all the parsers fails or if an applied parser fails after it has committed to its
|
||||
/// parse.
|
||||
///
|
||||
/// ```
|
||||
/// # #[macro_use]
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::parser::char::{digit, letter, string};
|
||||
/// # use combine::stream::easy::Error;
|
||||
/// # fn main() {
|
||||
/// let mut parser = choice!(
|
||||
/// many1(digit()),
|
||||
/// string("let").map(|s| s.to_string()),
|
||||
/// many1(letter()));
|
||||
/// assert_eq!(parser.parse("let"), Ok(("let".to_string(), "")));
|
||||
/// assert_eq!(parser.parse("123abc"), Ok(("123".to_string(), "abc")));
|
||||
/// assert!(parser.parse(":123").is_err());
|
||||
/// # }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! choice {
|
||||
($first : expr) => {
|
||||
$first
|
||||
};
|
||||
($first : expr, $($rest : expr),+) => {
|
||||
$first.or(choice!($($rest),+))
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! parse_mode_choice {
|
||||
(Input) => {
|
||||
fn parse_partial(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error> {
|
||||
self.parse_mode_choice($crate::parser::PartialMode::default(), input, state)
|
||||
}
|
||||
|
||||
fn parse_first(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, Input::Error> {
|
||||
self.parse_mode_choice($crate::parser::FirstMode, input, state)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// `ChoiceParser` represents a parser which may parse one of several different choices depending
|
||||
/// on the input.
|
||||
///
|
||||
/// This is an internal trait used to overload the `choice` function.
|
||||
pub trait ChoiceParser<Input: Stream> {
|
||||
type Output;
|
||||
type PartialState: Default;
|
||||
|
||||
fn parse_first(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>;
|
||||
|
||||
fn parse_partial(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>;
|
||||
|
||||
fn parse_mode_choice<M>(
|
||||
&mut self,
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
Self: Sized;
|
||||
|
||||
fn add_error_choice(&mut self, error: &mut Tracked<<Input as StreamOnce>::Error>);
|
||||
}
|
||||
|
||||
impl<'a, Input, P> ChoiceParser<Input> for &'a mut P
|
||||
where
|
||||
Input: Stream,
|
||||
P: ?Sized + ChoiceParser<Input>,
|
||||
{
|
||||
type Output = P::Output;
|
||||
type PartialState = P::PartialState;
|
||||
|
||||
parse_mode_choice!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_choice<M>(
|
||||
&mut self,
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
if mode.is_first() {
|
||||
(**self).parse_first(input, state)
|
||||
} else {
|
||||
(**self).parse_partial(input, state)
|
||||
}
|
||||
}
|
||||
|
||||
fn add_error_choice(&mut self, error: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
(**self).add_error_choice(error)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! merge {
|
||||
($head: ident) => {
|
||||
$head.error
|
||||
};
|
||||
($head: ident $($tail: ident)+) => {
|
||||
$head.error.merge(merge!($($tail)+))
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! do_choice {
|
||||
(
|
||||
$input: ident
|
||||
$before_position: ident
|
||||
$before: ident
|
||||
$partial_state: ident
|
||||
$state: ident
|
||||
( )
|
||||
$($parser: ident $error: ident)+
|
||||
) => { {
|
||||
let mut error = Tracked::from(merge!($($error)+));
|
||||
// If offset != 1 then the nested parser is a sequence of parsers where 1 or
|
||||
// more parsers returned `PeekOk` before the parser finally failed with
|
||||
// `PeekErr`. Since we lose the offsets of the nested parsers when we merge
|
||||
// the errors we must first extract the errors before we do the merge.
|
||||
// If the offset == 0 on the other hand (which should be the common case) then
|
||||
// we can delay the addition of the error since we know for certain that only
|
||||
// the first parser in the sequence were tried
|
||||
$(
|
||||
if $error.offset != ErrorOffset(1) {
|
||||
error.offset = $error.offset;
|
||||
$parser.add_error(&mut error);
|
||||
error.offset = ErrorOffset(0);
|
||||
}
|
||||
)+
|
||||
PeekErr(error)
|
||||
} };
|
||||
(
|
||||
$input: ident
|
||||
$before_position: ident
|
||||
$before: ident
|
||||
$partial_state: ident
|
||||
$state: ident
|
||||
( $head: ident $($tail: ident)* )
|
||||
$($all: ident)*
|
||||
) => { {
|
||||
let parser = $head;
|
||||
let mut state = $head::PartialState::default();
|
||||
match parser.parse_mode(crate::parser::FirstMode, $input, &mut state) {
|
||||
CommitOk(x) => CommitOk(x),
|
||||
PeekOk(x) => PeekOk(x),
|
||||
CommitErr(err) => {
|
||||
// If we get `CommitErr` but the input is the same this is a partial parse we
|
||||
// cannot commit to so leave the state as `Peek` to retry all the parsers
|
||||
// on the next call to `parse_partial`
|
||||
if $input.position() != $before_position {
|
||||
*$state = self::$partial_state::$head(state);
|
||||
}
|
||||
CommitErr(err)
|
||||
}
|
||||
PeekErr($head) => {
|
||||
ctry!($input.reset($before.clone()).committed());
|
||||
do_choice!(
|
||||
$input
|
||||
$before_position
|
||||
$before
|
||||
$partial_state
|
||||
$state
|
||||
( $($tail)* )
|
||||
$($all)*
|
||||
parser
|
||||
$head
|
||||
)
|
||||
}
|
||||
}
|
||||
} }
|
||||
}
|
||||
|
||||
macro_rules! tuple_choice_parser {
|
||||
($head: ident) => {
|
||||
tuple_choice_parser_inner!($head; $head);
|
||||
};
|
||||
($head: ident $($id: ident)+) => {
|
||||
tuple_choice_parser_inner!($head; $head $($id)+);
|
||||
tuple_choice_parser!($($id)+);
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! tuple_choice_parser_inner {
|
||||
($partial_state: ident; $($id: ident)+) => {
|
||||
#[doc(hidden)]
|
||||
pub enum $partial_state<$($id),+> {
|
||||
Peek,
|
||||
$(
|
||||
$id($id),
|
||||
)+
|
||||
}
|
||||
|
||||
impl<$($id),+> Default for self::$partial_state<$($id),+> {
|
||||
fn default() -> Self {
|
||||
self::$partial_state::Peek
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
impl<Input, Output $(,$id)+> ChoiceParser<Input> for ($($id,)+)
|
||||
where
|
||||
Input: Stream,
|
||||
$($id: Parser< Input, Output = Output>),+
|
||||
{
|
||||
|
||||
type Output = Output;
|
||||
type PartialState = self::$partial_state<$($id::PartialState),+>;
|
||||
|
||||
parse_mode_choice!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_choice<Mode>(
|
||||
&mut self,
|
||||
mode: Mode,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
Mode: ParseMode,
|
||||
{
|
||||
let ($(ref mut $id,)+) = *self;
|
||||
let empty = match *state {
|
||||
self::$partial_state::Peek => true,
|
||||
_ => false,
|
||||
};
|
||||
if mode.is_first() || empty {
|
||||
let before_position = input.position();
|
||||
let before = input.checkpoint();
|
||||
do_choice!(input before_position before $partial_state state ( $($id)+ ) )
|
||||
} else {
|
||||
match *state {
|
||||
self::$partial_state::Peek => unreachable!(),
|
||||
$(
|
||||
self::$partial_state::$id(_) => {
|
||||
let result = match *state {
|
||||
self::$partial_state::$id(ref mut state) => {
|
||||
$id.parse_mode(mode, input, state)
|
||||
}
|
||||
_ => unreachable!()
|
||||
};
|
||||
if result.is_ok() {
|
||||
*state = self::$partial_state::Peek;
|
||||
}
|
||||
result
|
||||
}
|
||||
)+
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add_error_choice(
|
||||
&mut self,
|
||||
error: &mut Tracked<<Input as StreamOnce>::Error>
|
||||
) {
|
||||
if error.offset != ErrorOffset(0) {
|
||||
let ($(ref mut $id,)+) = *self;
|
||||
// Reset the offset to 1 on every add so that we always (and only) takes the
|
||||
// error of the first parser. If we don't do this the first parser will consume
|
||||
// the offset to the detriment for all the other parsers.
|
||||
$(
|
||||
error.offset = ErrorOffset(1);
|
||||
$id.add_error(error);
|
||||
)+
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tuple_choice_parser!(A B C D E F G H I J K L M N O P Q R S T U V X Y Z);
|
||||
|
||||
macro_rules! array_choice_parser {
|
||||
($($t: tt)+) => {
|
||||
$(
|
||||
impl<Input, P> ChoiceParser<Input> for [P; $t]
|
||||
where
|
||||
Input: Stream,
|
||||
P: Parser<Input>,
|
||||
{
|
||||
|
||||
type Output = P::Output;
|
||||
type PartialState = <[P] as ChoiceParser<Input>>::PartialState;
|
||||
|
||||
parse_mode_choice!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_choice<M>(
|
||||
&mut self,
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
if mode.is_first() {
|
||||
self[..].parse_first(input, state)
|
||||
} else {
|
||||
self[..].parse_partial(input, state)
|
||||
}
|
||||
}
|
||||
fn add_error_choice(
|
||||
&mut self,
|
||||
error: &mut Tracked<<Input as StreamOnce>::Error>
|
||||
) {
|
||||
self[..].add_error_choice(error)
|
||||
}
|
||||
}
|
||||
)+
|
||||
};
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
array_choice_parser!(
|
||||
0 1 2 3 4 5 6 7 8 9
|
||||
10 11 12 13 14 15 16 17 18 19
|
||||
20 21 22 23 24 25 26 27 28 29
|
||||
30 31 32
|
||||
);
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Choice<P>(P);
|
||||
|
||||
impl<Input, P> Parser<Input> for Choice<P>
|
||||
where
|
||||
Input: Stream,
|
||||
P: ChoiceParser<Input>,
|
||||
{
|
||||
type Output = P::Output;
|
||||
type PartialState = P::PartialState;
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_impl<M>(
|
||||
&mut self,
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
self.0.parse_mode_choice(mode, input, state)
|
||||
}
|
||||
|
||||
fn add_error(&mut self, error: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
let before = error.offset.0;
|
||||
self.0.add_error_choice(error);
|
||||
error.offset.0 = before.saturating_sub(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn slice_parse_mode<Input, P, M>(
|
||||
self_: &mut [P],
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut (usize, P::PartialState),
|
||||
) -> ParseResult<P::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
P: Parser<Input>,
|
||||
Input: Stream,
|
||||
M: ParseMode,
|
||||
{
|
||||
let mut prev_err = None;
|
||||
let mut last_parser_having_non_1_offset = 0;
|
||||
let before = input.checkpoint();
|
||||
|
||||
let (ref mut index_state, ref mut child_state) = *state;
|
||||
if !mode.is_first() && *index_state != 0 {
|
||||
return self_[*index_state - 1]
|
||||
.parse_partial(input, child_state)
|
||||
.map(|x| {
|
||||
*index_state = 0;
|
||||
x
|
||||
});
|
||||
}
|
||||
|
||||
for i in 0..self_.len() {
|
||||
ctry!(input.reset(before.clone()).committed());
|
||||
|
||||
match self_[i].parse_mode(mode, input, child_state) {
|
||||
committed_err @ CommitErr(_) => {
|
||||
*index_state = i + 1;
|
||||
return committed_err;
|
||||
}
|
||||
PeekErr(err) => {
|
||||
prev_err = match prev_err {
|
||||
None => Some(err),
|
||||
Some(mut prev_err) => {
|
||||
if prev_err.offset != ErrorOffset(1) {
|
||||
// First add the errors of all the preceding parsers which did not
|
||||
// have a sequence of parsers returning `PeekOk` before failing
|
||||
// with `PeekErr`.
|
||||
let offset = prev_err.offset;
|
||||
for p in &mut self_[last_parser_having_non_1_offset..(i - 1)] {
|
||||
prev_err.offset = ErrorOffset(1);
|
||||
p.add_error(&mut prev_err);
|
||||
}
|
||||
// Then add the errors if the current parser
|
||||
prev_err.offset = offset;
|
||||
self_[i - 1].add_error(&mut prev_err);
|
||||
last_parser_having_non_1_offset = i;
|
||||
}
|
||||
Some(Tracked {
|
||||
error: prev_err.error.merge(err.error),
|
||||
offset: err.offset,
|
||||
})
|
||||
}
|
||||
};
|
||||
}
|
||||
ok @ CommitOk(_) | ok @ PeekOk(_) => {
|
||||
*index_state = 0;
|
||||
return ok;
|
||||
}
|
||||
}
|
||||
}
|
||||
PeekErr(match prev_err {
|
||||
None => Input::Error::from_error(
|
||||
input.position(),
|
||||
StreamError::message_static_message("parser choice is empty"),
|
||||
)
|
||||
.into(),
|
||||
Some(mut prev_err) => {
|
||||
if prev_err.offset != ErrorOffset(1) {
|
||||
let offset = prev_err.offset;
|
||||
let len = self_.len();
|
||||
for p in &mut self_[last_parser_having_non_1_offset..(len - 1)] {
|
||||
prev_err.offset = ErrorOffset(1);
|
||||
p.add_error(&mut prev_err);
|
||||
}
|
||||
prev_err.offset = offset;
|
||||
self_.last_mut().unwrap().add_error(&mut prev_err);
|
||||
prev_err.offset = ErrorOffset(0);
|
||||
}
|
||||
prev_err
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
impl<Input, O, P> ChoiceParser<Input> for [P]
|
||||
where
|
||||
Input: Stream,
|
||||
P: Parser<Input, Output = O>,
|
||||
{
|
||||
type Output = O;
|
||||
type PartialState = (usize, P::PartialState);
|
||||
|
||||
#[inline]
|
||||
fn parse_partial(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error> {
|
||||
slice_parse_mode(self, crate::parser::PartialMode::default(), input, state)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn parse_first(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error> {
|
||||
slice_parse_mode(self, crate::parser::FirstMode, input, state)
|
||||
}
|
||||
|
||||
#[allow(dead_code)] // this needs to be there for rust 1.85
|
||||
#[inline]
|
||||
fn parse_mode_choice<M>(
|
||||
&mut self,
|
||||
_mode: M,
|
||||
_input: &mut Input,
|
||||
_state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
fn add_error_choice(&mut self, error: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
if error.offset != ErrorOffset(0) {
|
||||
for p in self {
|
||||
error.offset = ErrorOffset(1);
|
||||
p.add_error(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes a tuple, a slice or an array of parsers and tries to apply them each in order.
|
||||
/// Fails if all the parsers fails or if an applied parser consumes input before failing.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::parser::char::{digit, string};
|
||||
/// # fn main() {
|
||||
/// // `choice` is overloaded on tuples so that different types of parsers can be used
|
||||
/// // (each parser must still have the same input and output types)
|
||||
/// let mut parser = choice((
|
||||
/// string("Apple").map(|s| s.to_string()),
|
||||
/// many1(digit()),
|
||||
/// string("Orange").map(|s| s.to_string()),
|
||||
/// ));
|
||||
/// assert_eq!(parser.parse("1234"), Ok(("1234".to_string(), "")));
|
||||
/// assert_eq!(parser.parse("Orangexx"), Ok(("Orange".to_string(), "xx")));
|
||||
/// assert!(parser.parse("Appl").is_err());
|
||||
/// assert!(parser.parse("Pear").is_err());
|
||||
///
|
||||
/// // If arrays or slices are used then all parsers must have the same type
|
||||
/// // (`string` in this case)
|
||||
/// let mut parser2 = choice([string("one"), string("two"), string("three")]);
|
||||
/// // Fails as the parser for "two" consumes the first 't' before failing
|
||||
/// assert!(parser2.parse("three").is_err());
|
||||
///
|
||||
/// // Use 'attempt' to make failing parsers always act as if they have not committed any input
|
||||
/// let mut parser3 = choice([attempt(string("one")), attempt(string("two")), attempt(string("three"))]);
|
||||
/// assert_eq!(parser3.parse("three"), Ok(("three", "")));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn choice<Input, P>(ps: P) -> Choice<P>
|
||||
where
|
||||
Input: Stream,
|
||||
P: ChoiceParser<Input>,
|
||||
{
|
||||
Choice(ps)
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Or<P1, P2>(Choice<(P1, P2)>);
|
||||
impl<Input, O, P1, P2> Parser<Input> for Or<P1, P2>
|
||||
where
|
||||
Input: Stream,
|
||||
P1: Parser<Input, Output = O>,
|
||||
P2: Parser<Input, Output = O>,
|
||||
{
|
||||
type Output = O;
|
||||
type PartialState = <Choice<(P1, P2)> as Parser<Input>>::PartialState;
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_impl<M>(
|
||||
&mut self,
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
self.0.parse_mode(mode, input, state)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
if errors.offset != ErrorOffset(0) {
|
||||
self.0.add_error(errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Equivalent to [`p1.or(p2)`].
|
||||
///
|
||||
/// If you are looking to chain 3 or more parsers using `or` you may consider using the
|
||||
/// [`choice!`] macro instead, which can be clearer and may result in a faster parser.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::parser::choice::or;
|
||||
/// # use combine::parser::char::{digit, string};
|
||||
/// # fn main() {
|
||||
/// let mut parser = or(
|
||||
/// string("let"),
|
||||
/// or(digit().map(|_| "digit"), string("led")),
|
||||
/// );
|
||||
/// assert_eq!(parser.parse("let"), Ok(("let", "")));
|
||||
/// assert_eq!(parser.parse("1"), Ok(("digit", "")));
|
||||
/// assert!(parser.parse("led").is_err());
|
||||
///
|
||||
/// let mut parser2 = or(string("two"), string("three"));
|
||||
/// // Fails as the parser for "two" consumes the first 't' before failing
|
||||
/// assert!(parser2.parse("three").is_err());
|
||||
///
|
||||
/// // Use 'attempt' to make failing parsers always act as if they have not committed any input
|
||||
/// let mut parser3 = or(attempt(string("two")), attempt(string("three")));
|
||||
/// assert_eq!(parser3.parse("three"), Ok(("three", "")));
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`choice!`]: ../../macro.choice.html
|
||||
/// [`p1.or(p2)`]: ../trait.Parser.html#method.or
|
||||
pub fn or<Input, P1, P2>(p1: P1, p2: P2) -> Or<P1, P2>
|
||||
where
|
||||
Input: Stream,
|
||||
P1: Parser<Input>,
|
||||
P2: Parser<Input, Output = P1::Output>,
|
||||
{
|
||||
Or(choice((p1, p2)))
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Optional<P>(P);
|
||||
impl<Input, P> Parser<Input> for Optional<P>
|
||||
where
|
||||
Input: Stream,
|
||||
P: Parser<Input>,
|
||||
{
|
||||
type Output = Option<P::Output>;
|
||||
type PartialState = P::PartialState;
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_impl<M>(
|
||||
&mut self,
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
let before = input.checkpoint();
|
||||
match self.0.parse_mode(mode, input, state) {
|
||||
PeekOk(x) => PeekOk(Some(x)),
|
||||
CommitOk(x) => CommitOk(Some(x)),
|
||||
CommitErr(err) => CommitErr(err),
|
||||
PeekErr(_) => {
|
||||
ctry!(input.reset(before).committed());
|
||||
PeekOk(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
forward_parser!(Input, add_error parser_count, 0);
|
||||
}
|
||||
|
||||
/// Parses `parser` and outputs `Some(value)` if it succeeds, `None` if it fails without
|
||||
/// consuming any input. Fails if `parser` fails after having committed some input.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::parser::char::string;
|
||||
/// # fn main() {
|
||||
/// let mut parser = optional(string("hello"));
|
||||
/// assert_eq!(parser.parse("hello"), Ok((Some("hello"), "")));
|
||||
/// assert_eq!(parser.parse("world"), Ok((None, "world")));
|
||||
/// assert!(parser.parse("heya").is_err());
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn optional<Input, P>(parser: P) -> Optional<P>
|
||||
where
|
||||
Input: Stream,
|
||||
P: Parser<Input>,
|
||||
{
|
||||
Optional(parser)
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! parse_mode_dispatch {
|
||||
() => {
|
||||
fn parse_partial(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error> {
|
||||
self.parse_mode_dispatch($crate::parser::PartialMode::default(), input, state)
|
||||
}
|
||||
|
||||
fn parse_first(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error> {
|
||||
self.parse_mode_dispatch($crate::parser::FirstMode, input, state)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! dispatch_parser_impl {
|
||||
($parser_name: ident [$first_ident: ident $($id: ident)*] [$($collected_idents: ident)*] $expr: expr, $($rest: expr,)*) => {
|
||||
$crate::dispatch_parser_impl!{ $parser_name [ $($id)* ] [$($collected_idents)* $first_ident] $($rest,)*}
|
||||
};
|
||||
($parser_name: ident [$($id: ident)*] [$($collected_idents: ident)*]) => {
|
||||
$crate::dispatch_parser_impl!{ $parser_name; $($collected_idents)* }
|
||||
};
|
||||
|
||||
($parser_name: ident; $($id: ident)*) => {
|
||||
pub enum $parser_name<$($id),*> {
|
||||
$(
|
||||
$id($id),
|
||||
)*
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
impl<Input, Output, $($id),*> $crate::Parser<Input> for $parser_name<$($id),*>
|
||||
where
|
||||
$( $id: $crate::Parser<Input, Output = Output>, )*
|
||||
Input: $crate::Stream,
|
||||
{
|
||||
type Output = Output;
|
||||
type PartialState = Option<$parser_name<$($id::PartialState),*>>;
|
||||
|
||||
$crate::parse_mode!(Input);
|
||||
fn parse_mode<Mode>(
|
||||
&mut self,
|
||||
mode: Mode,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> $crate::error::ParseResult<Self::Output, <Input as $crate::StreamOnce>::Error>
|
||||
where
|
||||
Mode: $crate::parser::ParseMode,
|
||||
{
|
||||
match self {
|
||||
$(
|
||||
$parser_name::$id($id) => {
|
||||
let state = match state {
|
||||
Some($parser_name::$id(s)) => s,
|
||||
_ => {
|
||||
*state = Some($parser_name::$id(Default::default()));
|
||||
match state {
|
||||
Some($parser_name::$id(s)) => s,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
};
|
||||
$id.parse_mode(mode, input, state)
|
||||
}
|
||||
)*
|
||||
}
|
||||
}
|
||||
|
||||
fn add_error(&mut self, error: &mut $crate::error::Tracked<<Input as $crate::StreamOnce>::Error>) {
|
||||
match self {
|
||||
$(
|
||||
$parser_name::$id($id) => $id.add_error(error),
|
||||
)*
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! dispatch_inner {
|
||||
($expr_ident: ident [$first_ident: ident $($id: ident)*] [$($collected: tt)*] $($pat: pat)|+ $(if $pred:expr)? => $expr: expr, $($rest_alt: tt)*) => {
|
||||
$crate::dispatch_inner!{ $expr_ident [ $($id)* ] [$($collected)* $first_ident $($pat)|+ $(if $pred)? => $expr,] $($rest_alt)*}
|
||||
};
|
||||
($expr_ident: ident [$($id: ident)*] [$($collected: tt)*]) => {
|
||||
$crate::dispatch_inner!{ $expr_ident $($collected)* }
|
||||
};
|
||||
($expr_ident: ident [$($ident_tt: tt)*]) => {
|
||||
unreachable!()
|
||||
};
|
||||
($expr_ident: ident $( $ident: ident $($pat: pat)|+ $(if $pred:expr)? => $expr: expr,)+ ) => {
|
||||
match $expr_ident {
|
||||
$(
|
||||
$($pat)|+ $(if $pred)? => Dispatch::$ident(check_parser($expr)),
|
||||
)+
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `dispatch!` allows a parser to be constructed depending on earlier input, without forcing each
|
||||
/// branch to have the same type of parser
|
||||
///
|
||||
/// ```
|
||||
/// use combine::{dispatch, any, token, satisfy, EasyParser, Parser};
|
||||
///
|
||||
/// let mut parser = any().then(|e| {
|
||||
/// dispatch!(e;
|
||||
/// 'a' => token('a'),
|
||||
/// 'b' => satisfy(|b| b == 'b'),
|
||||
/// t if t == 'c' => any(),
|
||||
/// _ => token('d')
|
||||
/// )
|
||||
/// });
|
||||
/// assert_eq!(parser.easy_parse("aa"), Ok(('a', "")));
|
||||
/// assert_eq!(parser.easy_parse("cc"), Ok(('c', "")));
|
||||
/// assert_eq!(parser.easy_parse("cd"), Ok(('d', "")));
|
||||
/// assert!(parser.easy_parse("ab").is_err());
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! dispatch {
|
||||
($match_expr: expr; $( $($pat: pat)|+ $(if $pred:expr)? => $expr: expr ),+ $(,)? ) => {
|
||||
{
|
||||
$crate::dispatch_parser_impl!{ Dispatch [A B C D E F G H I J K L M N O P Q R S T U V X Y Z] [] $($expr,)+ }
|
||||
|
||||
fn check_parser<Input, P>(p: P) -> P where P: $crate::Parser<Input>, Input: $crate::Stream { p }
|
||||
|
||||
let e = $match_expr;
|
||||
let parser = $crate::dispatch_inner!(e [A B C D E F G H I J K L M N O P Q R S T U V X Y Z] []
|
||||
$(
|
||||
$($pat)|+ $(if $pred)? => $expr,
|
||||
)*
|
||||
);
|
||||
parser
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "std", test))]
|
||||
mod tests {
|
||||
|
||||
use crate::parser::{token::any, EasyParser};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn choice_single_parser() {
|
||||
assert!(choice((any(),),).easy_parse("a").is_ok());
|
||||
}
|
||||
}
|
||||
+1582
File diff suppressed because it is too large
Load Diff
Vendored
+245
@@ -0,0 +1,245 @@
|
||||
//! Parsers which cause errors or modifies the returned error on parse failure.
|
||||
|
||||
use crate::{
|
||||
error::{
|
||||
ErrorInfo, ParseError,
|
||||
ParseResult::{self, *},
|
||||
StreamError, Tracked,
|
||||
},
|
||||
lib::marker::PhantomData,
|
||||
parser::ParseMode,
|
||||
Parser, Stream, StreamOnce,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Unexpected<I, T, E>(E, PhantomData<fn(I) -> (I, T)>)
|
||||
where
|
||||
I: Stream;
|
||||
impl<Input, T, E> Parser<Input> for Unexpected<Input, T, E>
|
||||
where
|
||||
Input: Stream,
|
||||
E: for<'s> ErrorInfo<'s, Input::Token, Input::Range>,
|
||||
{
|
||||
type Output = T;
|
||||
type PartialState = ();
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<T, <Input as StreamOnce>::Error> {
|
||||
PeekErr(<Input as StreamOnce>::Error::empty(input.position()).into())
|
||||
}
|
||||
fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
errors.error.add(StreamError::unexpected(&self.0));
|
||||
}
|
||||
}
|
||||
/// Always fails with `message` as an unexpected error.
|
||||
/// Never consumes any input.
|
||||
///
|
||||
/// Has `()` the output type
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::error::StreamError;
|
||||
/// # fn main() {
|
||||
/// let result = unexpected("token")
|
||||
/// .easy_parse("a");
|
||||
/// assert!(result.is_err());
|
||||
/// assert!(
|
||||
/// result.err()
|
||||
/// .unwrap()
|
||||
/// .errors
|
||||
/// .iter()
|
||||
/// .any(|m| *m == StreamError::unexpected("token"))
|
||||
/// );
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn unexpected<Input, S>(message: S) -> Unexpected<Input, (), S>
|
||||
where
|
||||
Input: Stream,
|
||||
S: for<'s> ErrorInfo<'s, Input::Token, Input::Range>,
|
||||
{
|
||||
unexpected_any(message)
|
||||
}
|
||||
|
||||
/// Always fails with `message` as an unexpected error.
|
||||
/// Never consumes any input.
|
||||
///
|
||||
/// May have anything as the output type but must be used such that the output type can inferred.
|
||||
/// The `unexpected` parser can be used if the output type does not matter
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::parser::error::unexpected_any;
|
||||
/// # use combine::error::StreamError;
|
||||
/// # fn main() {
|
||||
/// let result = token('b').or(unexpected_any("token"))
|
||||
/// .easy_parse("a");
|
||||
/// assert!(result.is_err());
|
||||
/// assert!(
|
||||
/// result.err()
|
||||
/// .unwrap()
|
||||
/// .errors
|
||||
/// .iter()
|
||||
/// .any(|m| *m == StreamError::unexpected("token"))
|
||||
/// );
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn unexpected_any<Input, S, T>(message: S) -> Unexpected<Input, T, S>
|
||||
where
|
||||
Input: Stream,
|
||||
S: for<'s> ErrorInfo<'s, Input::Token, Input::Range>,
|
||||
{
|
||||
Unexpected(message, PhantomData)
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Message<P, S>(P, S);
|
||||
impl<Input, P, S> Parser<Input> for Message<P, S>
|
||||
where
|
||||
Input: Stream,
|
||||
P: Parser<Input>,
|
||||
S: for<'s> ErrorInfo<'s, Input::Token, Input::Range>,
|
||||
{
|
||||
type Output = P::Output;
|
||||
type PartialState = P::PartialState;
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_impl<M>(
|
||||
&mut self,
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
match self.0.parse_mode(mode, input, state) {
|
||||
CommitOk(x) => CommitOk(x),
|
||||
PeekOk(x) => PeekOk(x),
|
||||
|
||||
// The message should always be added even if some input was committed before failing
|
||||
CommitErr(mut err) => {
|
||||
err.add_message(&self.1);
|
||||
CommitErr(err)
|
||||
}
|
||||
|
||||
// The message will be added in `add_error`
|
||||
PeekErr(err) => PeekErr(err),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
self.0.add_error(errors);
|
||||
errors.error.add_message(&self.1);
|
||||
}
|
||||
|
||||
forward_parser!(Input, parser_count add_committed_expected_error, 0);
|
||||
}
|
||||
|
||||
/// Equivalent to [`p1.message(msg)`].
|
||||
///
|
||||
/// [`p1.message(msg)`]: ../trait.Parser.html#method.message
|
||||
pub fn message<Input, P, S>(p: P, msg: S) -> Message<P, S>
|
||||
where
|
||||
P: Parser<Input>,
|
||||
Input: Stream,
|
||||
S: for<'s> ErrorInfo<'s, Input::Token, Input::Range>,
|
||||
{
|
||||
Message(p, msg)
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Expected<P, S>(P, S);
|
||||
impl<Input, P, S> Parser<Input> for Expected<P, S>
|
||||
where
|
||||
P: Parser<Input>,
|
||||
Input: Stream,
|
||||
S: for<'s> ErrorInfo<'s, Input::Token, Input::Range>,
|
||||
{
|
||||
type Output = P::Output;
|
||||
type PartialState = P::PartialState;
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_impl<M>(
|
||||
&mut self,
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
self.0.parse_mode(mode, input, state)
|
||||
}
|
||||
|
||||
fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
ParseError::set_expected(errors, StreamError::expected(&self.1), |errors| {
|
||||
self.0.add_error(errors);
|
||||
})
|
||||
}
|
||||
|
||||
forward_parser!(Input, parser_count add_committed_expected_error, 0);
|
||||
}
|
||||
|
||||
/// Equivalent to [`p.expected(info)`].
|
||||
///
|
||||
/// [`p.expected(info)`]: ../trait.Parser.html#method.expected
|
||||
pub fn expected<Input, P, S>(p: P, info: S) -> Expected<P, S>
|
||||
where
|
||||
P: Parser<Input>,
|
||||
Input: Stream,
|
||||
S: for<'s> ErrorInfo<'s, Input::Token, Input::Range>,
|
||||
{
|
||||
Expected(p, info)
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Silent<P>(P);
|
||||
impl<Input, P> Parser<Input> for Silent<P>
|
||||
where
|
||||
P: Parser<Input>,
|
||||
Input: Stream,
|
||||
{
|
||||
type Output = P::Output;
|
||||
type PartialState = P::PartialState;
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_impl<M>(
|
||||
&mut self,
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
self.0.parse_mode(mode, input, state).map_err(|mut err| {
|
||||
err.clear_expected();
|
||||
err
|
||||
})
|
||||
}
|
||||
|
||||
fn add_error(&mut self, _errors: &mut Tracked<<Input as StreamOnce>::Error>) {}
|
||||
|
||||
fn add_committed_expected_error(
|
||||
&mut self,
|
||||
_errors: &mut Tracked<<Input as StreamOnce>::Error>,
|
||||
) {
|
||||
}
|
||||
|
||||
forward_parser!(Input, parser_count, 0);
|
||||
}
|
||||
|
||||
/// Equivalent to [`p.silent()`].
|
||||
///
|
||||
/// [`p.silent()`]: ../trait.Parser.html#method.silent
|
||||
pub fn silent<Input, P>(p: P) -> Silent<P>
|
||||
where
|
||||
P: Parser<Input>,
|
||||
Input: Stream,
|
||||
{
|
||||
Silent(p)
|
||||
}
|
||||
+178
@@ -0,0 +1,178 @@
|
||||
//! Parsers constructor from regular functions
|
||||
|
||||
use crate::{
|
||||
error::{ParseResult, StdParseResult},
|
||||
lib::marker::PhantomData,
|
||||
stream::Stream,
|
||||
Parser,
|
||||
};
|
||||
|
||||
impl<'a, Input: Stream, O> Parser<Input>
|
||||
for dyn FnMut(&mut Input) -> StdParseResult<O, Input> + 'a
|
||||
{
|
||||
type Output = O;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<O, Input::Error> {
|
||||
self(input).into()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct FnParser<Input, F>(F, PhantomData<fn(Input) -> Input>);
|
||||
|
||||
/// Wraps a function, turning it into a parser.
|
||||
///
|
||||
/// Mainly needed to turn closures into parsers as function types can be casted to function pointers
|
||||
/// to make them usable as a parser.
|
||||
///
|
||||
/// ```
|
||||
/// extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::parser::char::digit;
|
||||
/// # use combine::error::{Commit, StreamError};
|
||||
/// # use combine::stream::easy;
|
||||
/// # fn main() {
|
||||
/// let mut even_digit = parser(|input| {
|
||||
/// // Help type inference out
|
||||
/// let _: &mut easy::Stream<&str> = input;
|
||||
/// let position = input.position();
|
||||
/// let (char_digit, committed) = digit().parse_stream(input).into_result()?;
|
||||
/// let d = (char_digit as i32) - ('0' as i32);
|
||||
/// if d % 2 == 0 {
|
||||
/// Ok((d, committed))
|
||||
/// }
|
||||
/// else {
|
||||
/// //Return an empty error since we only tested the first token of the stream
|
||||
/// let errors = easy::Errors::new(
|
||||
/// position,
|
||||
/// StreamError::expected("even number")
|
||||
/// );
|
||||
/// Err(Commit::Peek(errors.into()))
|
||||
/// }
|
||||
/// });
|
||||
/// let result = even_digit
|
||||
/// .easy_parse("8")
|
||||
/// .map(|x| x.0);
|
||||
/// assert_eq!(result, Ok(8));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn parser<Input, O, F>(f: F) -> FnParser<Input, F>
|
||||
where
|
||||
Input: Stream,
|
||||
F: FnMut(&mut Input) -> StdParseResult<O, Input>,
|
||||
{
|
||||
FnParser(f, PhantomData)
|
||||
}
|
||||
|
||||
impl<Input, O, F> Parser<Input> for FnParser<Input, F>
|
||||
where
|
||||
Input: Stream,
|
||||
F: FnMut(&mut Input) -> StdParseResult<O, Input>,
|
||||
{
|
||||
type Output = O;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<O, Input::Error> {
|
||||
(self.0)(input).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Input, O> Parser<Input> for fn(&mut Input) -> StdParseResult<O, Input>
|
||||
where
|
||||
Input: Stream,
|
||||
{
|
||||
type Output = O;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<O, Input::Error> {
|
||||
self(input).into()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy)]
|
||||
pub struct EnvParser<E, Input, T>
|
||||
where
|
||||
Input: Stream,
|
||||
{
|
||||
env: E,
|
||||
parser: fn(E, &mut Input) -> StdParseResult<T, Input>,
|
||||
}
|
||||
|
||||
impl<E, Input, T> Clone for EnvParser<E, Input, T>
|
||||
where
|
||||
Input: Stream,
|
||||
E: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
EnvParser {
|
||||
env: self.env.clone(),
|
||||
parser: self.parser,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Input, E, O> Parser<Input> for EnvParser<E, Input, O>
|
||||
where
|
||||
E: Clone,
|
||||
Input: Stream,
|
||||
{
|
||||
type Output = O;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<O, Input::Error> {
|
||||
(self.parser)(self.env.clone(), input).into()
|
||||
}
|
||||
}
|
||||
|
||||
/// Constructs a parser out of an environment and a function which needs the given environment to
|
||||
/// do the parsing. This is commonly useful to allow multiple parsers to share some environment
|
||||
/// while still allowing the parsers to be written in separate functions.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use std::collections::HashMap;
|
||||
/// # use combine::*;
|
||||
/// # use combine::parser::function::env_parser;
|
||||
/// # use combine::parser::char::letter;
|
||||
/// # fn main() {
|
||||
/// struct Interner(HashMap<String, u32>);
|
||||
/// impl Interner {
|
||||
/// fn string<Input>(&self, input: &mut Input) -> StdParseResult<u32, Input>
|
||||
/// where Input: Stream<Token = char>,
|
||||
/// {
|
||||
/// many(letter())
|
||||
/// .map(|s: String| self.0.get(&s).cloned().unwrap_or(0))
|
||||
/// .parse_stream(input)
|
||||
/// .into_result()
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// let mut map = HashMap::new();
|
||||
/// map.insert("hello".into(), 1);
|
||||
/// map.insert("test".into(), 2);
|
||||
///
|
||||
/// let env = Interner(map);
|
||||
/// let mut parser = env_parser(&env, Interner::string);
|
||||
///
|
||||
/// let result = parser.parse("hello");
|
||||
/// assert_eq!(result, Ok((1, "")));
|
||||
///
|
||||
/// let result = parser.parse("world");
|
||||
/// assert_eq!(result, Ok((0, "")));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn env_parser<E, Input, O>(
|
||||
env: E,
|
||||
parser: fn(E, &mut Input) -> StdParseResult<O, Input>,
|
||||
) -> EnvParser<E, Input, O>
|
||||
where
|
||||
E: Clone,
|
||||
Input: Stream,
|
||||
{
|
||||
EnvParser { env, parser }
|
||||
}
|
||||
Vendored
+1209
File diff suppressed because it is too large
Load Diff
Vendored
+767
@@ -0,0 +1,767 @@
|
||||
//! Module containing zero-copy parsers.
|
||||
//!
|
||||
//! These parsers require the [`RangeStream`][] bound instead of a plain [`Stream`][].
|
||||
//!
|
||||
//! [`RangeStream`]: ../../stream/trait.RangeStream.html
|
||||
//! [`Stream`]: ../../stream/trait.Stream.html
|
||||
|
||||
use crate::{
|
||||
error::{
|
||||
self, ParseError,
|
||||
ParseResult::{self, *},
|
||||
ResultExt, StreamError, Tracked,
|
||||
},
|
||||
lib::{convert::TryFrom, marker::PhantomData},
|
||||
parser::ParseMode,
|
||||
};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use crate::lib::error::Error as StdError;
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
use crate::lib::fmt;
|
||||
|
||||
use crate::stream::{
|
||||
uncons_range, uncons_while, uncons_while1, wrap_stream_error, Range as StreamRange,
|
||||
RangeStream, StreamErrorFor, StreamOnce,
|
||||
};
|
||||
|
||||
use crate::Parser;
|
||||
|
||||
pub struct Range<Input>(Input::Range)
|
||||
where
|
||||
Input: RangeStream;
|
||||
|
||||
impl<Input> Parser<Input> for Range<Input>
|
||||
where
|
||||
Input: RangeStream,
|
||||
Input::Range: PartialEq + crate::stream::Range,
|
||||
{
|
||||
type Output = Input::Range;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error> {
|
||||
use crate::stream::Range;
|
||||
|
||||
let position = input.position();
|
||||
match input.uncons_range(self.0.len()) {
|
||||
Ok(other) => {
|
||||
if other == self.0 {
|
||||
CommitOk(other)
|
||||
} else {
|
||||
PeekErr(Input::Error::empty(position).into())
|
||||
}
|
||||
}
|
||||
Err(err) => wrap_stream_error(input, err),
|
||||
}
|
||||
}
|
||||
fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
// TODO Add unexpected message?
|
||||
errors.error.add_expected(error::Range(self.0.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
parser! {
|
||||
#[derive(Clone)]
|
||||
pub struct Recognize;
|
||||
type PartialState = <RecognizeWithValue<P> as Parser<Input>>::PartialState;
|
||||
/// Zero-copy parser which returns committed input range.
|
||||
///
|
||||
/// [`combinator::recognize`][] is a non-`RangeStream` alternative.
|
||||
///
|
||||
/// [`combinator::recognize`]: ../../parser/combinator/fn.recognize.html
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::parser::range::recognize;
|
||||
/// # use combine::parser::char::letter;
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let mut parser = recognize(skip_many1(letter()));
|
||||
/// assert_eq!(parser.parse("hello world"), Ok(("hello", " world")));
|
||||
/// assert!(parser.parse("!").is_err());
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn recognize[Input, P](parser: P)(Input) -> <Input as StreamOnce>::Range
|
||||
where [
|
||||
P: Parser<Input>,
|
||||
Input: RangeStream,
|
||||
<Input as StreamOnce>::Range: crate::stream::Range,
|
||||
]
|
||||
{
|
||||
recognize_with_value(parser).map(|(range, _)| range)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn parse_partial_range<M, F, G, S, Input>(
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
distance_state: &mut usize,
|
||||
state: S,
|
||||
first: F,
|
||||
resume: G,
|
||||
) -> ParseResult<Input::Range, Input::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
F: FnOnce(&mut Input, S) -> ParseResult<Input::Range, <Input as StreamOnce>::Error>,
|
||||
G: FnOnce(&mut Input, S) -> ParseResult<Input::Range, <Input as StreamOnce>::Error>,
|
||||
Input: RangeStream,
|
||||
{
|
||||
let before = input.checkpoint();
|
||||
|
||||
if !input.is_partial() {
|
||||
first(input, state)
|
||||
} else if mode.is_first() || *distance_state == 0 {
|
||||
let result = first(input, state);
|
||||
if let CommitErr(_) = result {
|
||||
*distance_state = input.distance(&before);
|
||||
ctry!(input.reset(before).committed());
|
||||
}
|
||||
result
|
||||
} else {
|
||||
if input.uncons_range(*distance_state).is_err() {
|
||||
panic!("recognize errored when restoring the input stream to its expected state");
|
||||
}
|
||||
|
||||
match resume(input, state) {
|
||||
CommitOk(_) | PeekOk(_) => (),
|
||||
PeekErr(err) => return PeekErr(err),
|
||||
CommitErr(err) => {
|
||||
*distance_state = input.distance(&before);
|
||||
ctry!(input.reset(before).committed());
|
||||
return CommitErr(err);
|
||||
}
|
||||
}
|
||||
|
||||
let distance = input.distance(&before);
|
||||
ctry!(input.reset(before).committed());
|
||||
take(distance).parse_lazy(input).map(|range| {
|
||||
*distance_state = 0;
|
||||
range
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RecognizeWithValue<P>(P);
|
||||
|
||||
impl<Input, P> Parser<Input> for RecognizeWithValue<P>
|
||||
where
|
||||
P: Parser<Input>,
|
||||
Input: RangeStream,
|
||||
<Input as StreamOnce>::Range: crate::stream::Range,
|
||||
{
|
||||
type Output = (<Input as StreamOnce>::Range, P::Output);
|
||||
type PartialState = (usize, P::PartialState);
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode<M>(
|
||||
&mut self,
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
let (ref mut distance_state, ref mut child_state) = *state;
|
||||
|
||||
let before = input.checkpoint();
|
||||
if !mode.is_first() && input.uncons_range(*distance_state).is_err() {
|
||||
panic!("recognize errored when restoring the input stream to its expected state");
|
||||
}
|
||||
|
||||
let value = match self.0.parse_mode(mode, input, child_state) {
|
||||
CommitOk(x) | PeekOk(x) => x,
|
||||
PeekErr(err) => return PeekErr(err),
|
||||
CommitErr(err) => {
|
||||
*distance_state = input.distance(&before);
|
||||
ctry!(input.reset(before).committed());
|
||||
return CommitErr(err);
|
||||
}
|
||||
};
|
||||
|
||||
let distance = input.distance(&before);
|
||||
ctry!(input.reset(before).committed());
|
||||
take(distance).parse_lazy(input).map(|range| {
|
||||
*distance_state = 0;
|
||||
(range, value)
|
||||
})
|
||||
}
|
||||
fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
self.0.add_error(errors)
|
||||
}
|
||||
}
|
||||
|
||||
/// Zero-copy parser which returns a pair: (committed input range, parsed value).
|
||||
///
|
||||
///
|
||||
/// [`combinator::recognize_with_value`] is a non-`RangeStream` alternative.
|
||||
///
|
||||
/// [`combinator::recognize_with_value`]: recognize_with_value
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::parser::range::recognize_with_value;
|
||||
/// # use combine::parser::char::{digit, char};
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let mut parser = recognize_with_value((
|
||||
/// skip_many1(digit()),
|
||||
/// optional((attempt(char('.')), skip_many1(digit()))),
|
||||
/// ).map(|(_, opt)| opt.is_some()));
|
||||
///
|
||||
/// assert_eq!(parser.parse("1234!"), Ok((("1234", false), "!")));
|
||||
/// assert_eq!(parser.parse("1234.0001!"), Ok((("1234.0001", true), "!")));
|
||||
/// assert!(parser.parse("!").is_err());
|
||||
/// assert!(parser.parse("1234.").is_err());
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn recognize_with_value<Input, P>(parser: P) -> RecognizeWithValue<P>
|
||||
where
|
||||
P: Parser<Input>,
|
||||
Input: RangeStream,
|
||||
<Input as StreamOnce>::Range: crate::stream::Range,
|
||||
{
|
||||
RecognizeWithValue(parser)
|
||||
}
|
||||
|
||||
/// Zero-copy parser which reads a range of length `i.len()` and succeeds if `i` is equal to that
|
||||
/// range.
|
||||
///
|
||||
/// [`tokens`] is a non-`RangeStream` alternative.
|
||||
///
|
||||
/// [`tokens`]: super::token::tokens
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::parser::range::range;
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let mut parser = range("hello");
|
||||
/// let result = parser.parse("hello world");
|
||||
/// assert_eq!(result, Ok(("hello", " world")));
|
||||
/// let result = parser.parse("hel world");
|
||||
/// assert!(result.is_err());
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn range<Input>(i: Input::Range) -> Range<Input>
|
||||
where
|
||||
Input: RangeStream,
|
||||
Input::Range: PartialEq,
|
||||
{
|
||||
Range(i)
|
||||
}
|
||||
|
||||
pub struct Take<Input>(usize, PhantomData<fn(Input)>);
|
||||
impl<Input> Parser<Input> for Take<Input>
|
||||
where
|
||||
Input: RangeStream,
|
||||
{
|
||||
type Output = Input::Range;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error> {
|
||||
uncons_range(input, self.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Zero-copy parser which reads a range of length `n`.
|
||||
///
|
||||
/// [`count_min_max`][] is a non-`RangeStream` alternative.
|
||||
///
|
||||
/// [`count_min_max`]: ../../parser/repeat/fn.count_min_max.html
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::parser::range::take;
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let mut parser = take(1);
|
||||
/// let result = parser.parse("1");
|
||||
/// assert_eq!(result, Ok(("1", "")));
|
||||
/// let mut parser = take(4);
|
||||
/// let result = parser.parse("123abc");
|
||||
/// assert_eq!(result, Ok(("123a", "bc")));
|
||||
/// let result = parser.parse("abc");
|
||||
/// assert!(result.is_err());
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn take<Input>(n: usize) -> Take<Input>
|
||||
where
|
||||
Input: RangeStream,
|
||||
{
|
||||
Take(n, PhantomData)
|
||||
}
|
||||
|
||||
pub struct TakeWhile<Input, F>(F, PhantomData<fn(Input) -> Input>);
|
||||
impl<Input, F> Parser<Input> for TakeWhile<Input, F>
|
||||
where
|
||||
Input: RangeStream,
|
||||
Input::Range: crate::stream::Range,
|
||||
F: FnMut(Input::Token) -> bool,
|
||||
{
|
||||
type Output = Input::Range;
|
||||
type PartialState = usize;
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_impl<M>(
|
||||
&mut self,
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
parse_partial_range(
|
||||
mode,
|
||||
input,
|
||||
state,
|
||||
&mut self.0,
|
||||
|input, predicate| uncons_while(input, predicate),
|
||||
|input, predicate| uncons_while(input, predicate),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Zero-copy parser which reads a range of 0 or more tokens which satisfy `f`.
|
||||
///
|
||||
/// [`many`][] is a non-`RangeStream` alternative.
|
||||
///
|
||||
/// [`many`]: ../../parser/repeat/fn.many.html
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::parser::range::take_while;
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let mut parser = take_while(|c: char| c.is_digit(10));
|
||||
/// let result = parser.parse("123abc");
|
||||
/// assert_eq!(result, Ok(("123", "abc")));
|
||||
/// let result = parser.parse("abc");
|
||||
/// assert_eq!(result, Ok(("", "abc")));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn take_while<Input, F>(f: F) -> TakeWhile<Input, F>
|
||||
where
|
||||
Input: RangeStream,
|
||||
Input::Range: crate::stream::Range,
|
||||
F: FnMut(Input::Token) -> bool,
|
||||
{
|
||||
TakeWhile(f, PhantomData)
|
||||
}
|
||||
|
||||
pub struct TakeWhile1<Input, F>(F, PhantomData<fn(Input) -> Input>);
|
||||
impl<Input, F> Parser<Input> for TakeWhile1<Input, F>
|
||||
where
|
||||
Input: RangeStream,
|
||||
Input::Range: crate::stream::Range,
|
||||
F: FnMut(Input::Token) -> bool,
|
||||
{
|
||||
type Output = Input::Range;
|
||||
type PartialState = usize;
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_impl<M>(
|
||||
&mut self,
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
parse_partial_range(
|
||||
mode,
|
||||
input,
|
||||
state,
|
||||
&mut self.0,
|
||||
|input, predicate| uncons_while1(input, predicate),
|
||||
|input, predicate| uncons_while(input, predicate),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Zero-copy parser which reads a range of 1 or more tokens which satisfy `f`.
|
||||
///
|
||||
/// [`many1`][] is a non-`RangeStream` alternative.
|
||||
///
|
||||
/// [`many1`]: ../../parser/repeat/fn.many1.html
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::parser::range::take_while1;
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let mut parser = take_while1(|c: char| c.is_digit(10));
|
||||
/// let result = parser.parse("123abc");
|
||||
/// assert_eq!(result, Ok(("123", "abc")));
|
||||
/// let result = parser.parse("abc");
|
||||
/// assert!(result.is_err());
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn take_while1<Input, F>(f: F) -> TakeWhile1<Input, F>
|
||||
where
|
||||
Input: RangeStream,
|
||||
Input::Range: crate::stream::Range,
|
||||
F: FnMut(Input::Token) -> bool,
|
||||
{
|
||||
TakeWhile1(f, PhantomData)
|
||||
}
|
||||
|
||||
pub struct TakeUntilRange<Input>(Input::Range)
|
||||
where
|
||||
Input: RangeStream;
|
||||
impl<Input> Parser<Input> for TakeUntilRange<Input>
|
||||
where
|
||||
Input: RangeStream,
|
||||
Input::Range: PartialEq + crate::stream::Range,
|
||||
{
|
||||
type Output = Input::Range;
|
||||
type PartialState = usize;
|
||||
|
||||
#[inline]
|
||||
fn parse_partial(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
to_consume: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error> {
|
||||
use crate::stream::Range;
|
||||
|
||||
let len = self.0.len();
|
||||
let before = input.checkpoint();
|
||||
let mut first_stream_error = None;
|
||||
|
||||
// Skip until the end of the last parse attempt
|
||||
ctry!(uncons_range(input, *to_consume));
|
||||
|
||||
loop {
|
||||
let look_ahead_input = input.checkpoint();
|
||||
|
||||
match input.uncons_range(len) {
|
||||
Ok(xs) => {
|
||||
if xs == self.0 {
|
||||
let distance = input.distance(&before) - len;
|
||||
ctry!(input.reset(before).committed());
|
||||
|
||||
if let Ok(committed) = input.uncons_range(distance) {
|
||||
if distance == 0 {
|
||||
return PeekOk(committed);
|
||||
} else {
|
||||
*to_consume = 0;
|
||||
return CommitOk(committed);
|
||||
}
|
||||
}
|
||||
|
||||
// We are guaranteed able to uncons to_consume characters here
|
||||
// because we've already done it on look_ahead_input.
|
||||
unreachable!();
|
||||
} else {
|
||||
// Reset the stream back to where it was when we entered the top of the loop
|
||||
ctry!(input.reset(look_ahead_input).committed());
|
||||
|
||||
// Advance the stream by one token
|
||||
if input.uncons().is_err() {
|
||||
unreachable!();
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(first_error) => {
|
||||
// If we are unable to find a successful parse even after advancing with `uncons`
|
||||
// below we must reset the stream to its state before the first error.
|
||||
// If we don't we may try and match the range `::` against `:<EOF>` which would
|
||||
// fail as only one `:` is present at this parse attempt. But when we later resume
|
||||
// with more input we must start parsing again at the first time we errored so we
|
||||
// can see the entire `::`
|
||||
if first_stream_error.is_none() {
|
||||
first_stream_error = Some((first_error, input.distance(&before)));
|
||||
}
|
||||
|
||||
// Reset the stream back to where it was when we entered the top of the loop
|
||||
ctry!(input.reset(look_ahead_input).committed());
|
||||
|
||||
// See if we can advance anyway
|
||||
if input.uncons().is_err() {
|
||||
let (first_error, first_error_distance) = first_stream_error.unwrap();
|
||||
|
||||
// Reset the stream
|
||||
ctry!(input.reset(before).committed());
|
||||
*to_consume = first_error_distance;
|
||||
|
||||
// Return the original error if uncons failed
|
||||
return wrap_stream_error(input, first_error);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Zero-copy parser which reads a range of 0 or more tokens until `r` is found.
|
||||
///
|
||||
/// The range `r` will not be committed. If `r` is not found, the parser will
|
||||
/// return an error.
|
||||
///
|
||||
/// [`repeat::take_until`][] is a non-`RangeStream` alternative.
|
||||
///
|
||||
/// [`repeat::take_until`]: ../../parser/repeat/fn.take_until.html
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::parser::range::{range, take_until_range};
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let mut parser = take_until_range("\r\n");
|
||||
/// let result = parser.parse("To: user@example.com\r\n");
|
||||
/// assert_eq!(result, Ok(("To: user@example.com", "\r\n")));
|
||||
/// let result = parser.parse("Hello, world\n");
|
||||
/// assert!(result.is_err());
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn take_until_range<Input>(r: Input::Range) -> TakeUntilRange<Input>
|
||||
where
|
||||
Input: RangeStream,
|
||||
{
|
||||
TakeUntilRange(r)
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum TakeRange {
|
||||
/// Found the pattern at this offset
|
||||
Found(usize),
|
||||
/// Did not find the pattern but the parser can skip ahead to this offset.
|
||||
NotFound(usize),
|
||||
}
|
||||
|
||||
impl From<Option<usize>> for TakeRange {
|
||||
fn from(opt: Option<usize>) -> TakeRange {
|
||||
match opt {
|
||||
Some(i) => TakeRange::Found(i),
|
||||
None => TakeRange::NotFound(0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TakeFn<F, Input> {
|
||||
searcher: F,
|
||||
_marker: PhantomData<fn(Input)>,
|
||||
}
|
||||
|
||||
impl<Input, F, R> Parser<Input> for TakeFn<F, Input>
|
||||
where
|
||||
F: FnMut(Input::Range) -> R,
|
||||
R: Into<TakeRange>,
|
||||
Input: RangeStream,
|
||||
Input::Range: crate::stream::Range,
|
||||
{
|
||||
type Output = Input::Range;
|
||||
type PartialState = usize;
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode<M>(
|
||||
&mut self,
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
offset: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
let checkpoint = input.checkpoint();
|
||||
|
||||
if mode.is_first() {
|
||||
*offset = 0;
|
||||
} else {
|
||||
let _ = input.uncons_range(*offset);
|
||||
}
|
||||
|
||||
match (self.searcher)(input.range()).into() {
|
||||
TakeRange::Found(i) => {
|
||||
ctry!(input.reset(checkpoint).committed());
|
||||
let result = uncons_range(input, *offset + i);
|
||||
if result.is_ok() {
|
||||
*offset = 0;
|
||||
}
|
||||
result
|
||||
}
|
||||
TakeRange::NotFound(next_offset) => {
|
||||
*offset = next_offset;
|
||||
|
||||
let range = input.range();
|
||||
let _ = input.uncons_range(range.len());
|
||||
let position = input.position();
|
||||
ctry!(input.reset(checkpoint).committed());
|
||||
|
||||
let err = Input::Error::from_error(position, StreamError::end_of_input());
|
||||
if !input.is_partial() && range.is_empty() {
|
||||
PeekErr(err.into())
|
||||
} else {
|
||||
CommitErr(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Searches the entire range using `searcher` and then consumes a range of `Some(n)`.
|
||||
/// If `f` can not find anything in the range it must return `None/NotFound` which indicates an end of input error.
|
||||
///
|
||||
/// If partial parsing is used the `TakeRange` enum can be returned instead of `Option`. By
|
||||
/// returning `TakeRange::NotFound(n)` it indicates that the input can skip ahead until `n`
|
||||
/// when parsing is next resumed.
|
||||
///
|
||||
/// See [`take_until_bytes`](../byte/fn.take_until_bytes.html) for a usecase.
|
||||
pub fn take_fn<F, R, Input>(searcher: F) -> TakeFn<F, Input>
|
||||
where
|
||||
F: FnMut(Input::Range) -> R,
|
||||
R: Into<TakeRange>,
|
||||
Input: RangeStream,
|
||||
Input::Range: crate::stream::Range,
|
||||
{
|
||||
TakeFn {
|
||||
searcher,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
parser! {
|
||||
/// Takes a parser which parses a `length` then extracts a range of that length and returns it.
|
||||
/// Commonly used in binary formats
|
||||
///
|
||||
/// ```
|
||||
/// # use combine::parser::{byte::num::be_u16, range::length_prefix};
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let mut input = Vec::new();
|
||||
/// input.extend_from_slice(&3u16.to_be_bytes());
|
||||
/// input.extend_from_slice(b"1234");
|
||||
///
|
||||
/// let mut parser = length_prefix(be_u16());
|
||||
/// let result = parser.parse(&input[..]);
|
||||
/// assert_eq!(result, Ok((&b"123"[..], &b"4"[..])));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn length_prefix[Input, P](len: P)(Input) -> Input::Range
|
||||
where [
|
||||
Input: RangeStream,
|
||||
P: Parser<Input>,
|
||||
usize: TryFrom<P::Output>,
|
||||
<usize as TryFrom<P::Output>>::Error: StdError + Send + Sync + 'static,
|
||||
]
|
||||
{
|
||||
len
|
||||
.and_then(|u| {
|
||||
usize::try_from(u)
|
||||
.map_err(StreamErrorFor::<Input>::other)
|
||||
})
|
||||
.then_partial(|&mut len| take(len))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
parser! {
|
||||
/// Takes a parser which parses a `length` then extracts a range of that length and returns it.
|
||||
/// Commonly used in binary formats
|
||||
///
|
||||
/// ```
|
||||
/// # use combine::parser::{byte::num::be_u16, range::length_prefix};
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let mut input = Vec::new();
|
||||
/// input.extend_from_slice(&3u16.to_be_bytes());
|
||||
/// input.extend_from_slice(b"1234");
|
||||
///
|
||||
/// let mut parser = length_prefix(be_u16());
|
||||
/// let result = parser.parse(&input[..]);
|
||||
/// assert_eq!(result, Ok((&b"123"[..], &b"4"[..])));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn length_prefix[Input, P](len: P)(Input) -> Input::Range
|
||||
where [
|
||||
Input: RangeStream,
|
||||
P: Parser<Input>,
|
||||
usize: TryFrom<P::Output>,
|
||||
<usize as TryFrom<P::Output>>::Error: fmt::Display + Send + Sync + 'static,
|
||||
]
|
||||
{
|
||||
len
|
||||
.and_then(|u| {
|
||||
usize::try_from(u)
|
||||
.map_err(StreamErrorFor::<Input>::message_format)
|
||||
})
|
||||
.then_partial(|&mut len| take(len))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use crate::Parser;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn take_while_test() {
|
||||
let result = take_while(|c: char| c.is_digit(10)).parse("123abc");
|
||||
assert_eq!(result, Ok(("123", "abc")));
|
||||
let result = take_while(|c: char| c.is_digit(10)).parse("abc");
|
||||
assert_eq!(result, Ok(("", "abc")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_while1_test() {
|
||||
let result = take_while1(|c: char| c.is_digit(10)).parse("123abc");
|
||||
assert_eq!(result, Ok(("123", "abc")));
|
||||
let result = take_while1(|c: char| c.is_digit(10)).parse("abc");
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn range_string_no_char_boundary_error() {
|
||||
let mut parser = range("hello");
|
||||
let result = parser.parse("hell\u{00EE} world");
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_until_range_1() {
|
||||
let result = take_until_range("\"").parse("Foo baz bar quux\"");
|
||||
assert_eq!(result, Ok(("Foo baz bar quux", "\"")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_until_range_2() {
|
||||
let result = take_until_range("===").parse("if ((pointless_comparison == 3) === true) {");
|
||||
assert_eq!(
|
||||
result,
|
||||
Ok(("if ((pointless_comparison == 3) ", "=== true) {"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_until_range_unicode_1() {
|
||||
let result = take_until_range("🦀")
|
||||
.parse("😃 Ferris the friendly rustacean 🦀 and his snake friend 🐍");
|
||||
assert_eq!(
|
||||
result,
|
||||
Ok((
|
||||
"😃 Ferris the friendly rustacean ",
|
||||
"🦀 and his snake friend 🐍"
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_until_range_unicode_2() {
|
||||
let result = take_until_range("⁘⁙/⁘").parse("⚙️🛠️🦀=🏎️⁘⁙⁘⁘⁙/⁘⁘⁙/⁘");
|
||||
assert_eq!(result, Ok(("⚙️🛠️🦀=🏎️⁘⁙⁘", "⁘⁙/⁘⁘⁙/⁘")));
|
||||
}
|
||||
}
|
||||
Vendored
+547
@@ -0,0 +1,547 @@
|
||||
//! Module containing regex parsers on streams returning ranges of `&str` or `&[u8]`.
|
||||
//!
|
||||
//! All regex parsers are overloaded on `&str` and `&[u8]` ranges and can take a `Regex` by value
|
||||
//! or shared reference (`&`).
|
||||
//!
|
||||
//! Enabled using the `regex` feature (for `regex-0.2`) or the `regex-1` feature for `regex-1.0`.
|
||||
//!
|
||||
//! ```
|
||||
//! use once_cell::sync::Lazy;
|
||||
//! use regex::{bytes, Regex};
|
||||
//! use combine::Parser;
|
||||
//! use combine::parser::regex::{find_many, match_};
|
||||
//!
|
||||
//! fn main() {
|
||||
//! let regex = bytes::Regex::new("[0-9]+").unwrap();
|
||||
//! // Shared references to any regex works as well
|
||||
//! assert_eq!(
|
||||
//! find_many(®ex).parse(&b"123 456 "[..]),
|
||||
//! Ok((vec![&b"123"[..], &b"456"[..]], &b" "[..]))
|
||||
//! );
|
||||
//! assert_eq!(
|
||||
//! find_many(regex).parse(&b""[..]),
|
||||
//! Ok((vec![], &b""[..]))
|
||||
//! );
|
||||
//!
|
||||
//! static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new("[:alpha:]+").unwrap());
|
||||
//! assert_eq!(
|
||||
//! match_(&*REGEX).parse("abc123"),
|
||||
//! Ok(("abc123", "abc123"))
|
||||
//! );
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
use std::{iter::FromIterator, marker::PhantomData};
|
||||
|
||||
use crate::{
|
||||
error::{
|
||||
ParseError,
|
||||
ParseResult::{self, *},
|
||||
StreamError, Tracked,
|
||||
},
|
||||
parser::range::take,
|
||||
stream::{RangeStream, StreamOnce},
|
||||
Parser,
|
||||
};
|
||||
|
||||
struct First<T>(Option<T>);
|
||||
|
||||
impl<A> FromIterator<A> for First<A> {
|
||||
fn from_iter<T>(iter: T) -> Self
|
||||
where
|
||||
T: IntoIterator<Item = A>,
|
||||
{
|
||||
First(iter.into_iter().next())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait MatchFind {
|
||||
type Range;
|
||||
fn end(&self) -> usize;
|
||||
fn as_match(&self) -> Self::Range;
|
||||
}
|
||||
|
||||
pub trait Regex<Range> {
|
||||
fn is_match(&self, range: Range) -> bool;
|
||||
fn find_iter<F>(&self, range: Range) -> (usize, F)
|
||||
where
|
||||
F: FromIterator<Range>;
|
||||
fn captures<F, G>(&self, range: Range) -> (usize, G)
|
||||
where
|
||||
F: FromIterator<Range>,
|
||||
G: FromIterator<F>;
|
||||
fn as_str(&self) -> &str;
|
||||
}
|
||||
|
||||
impl<'a, R, Range> Regex<Range> for &'a R
|
||||
where
|
||||
R: Regex<Range>,
|
||||
{
|
||||
fn is_match(&self, range: Range) -> bool {
|
||||
(**self).is_match(range)
|
||||
}
|
||||
fn find_iter<F>(&self, range: Range) -> (usize, F)
|
||||
where
|
||||
F: FromIterator<Range>,
|
||||
{
|
||||
(**self).find_iter(range)
|
||||
}
|
||||
fn captures<F, G>(&self, range: Range) -> (usize, G)
|
||||
where
|
||||
F: FromIterator<Range>,
|
||||
G: FromIterator<F>,
|
||||
{
|
||||
(**self).captures(range)
|
||||
}
|
||||
fn as_str(&self) -> &str {
|
||||
(**self).as_str()
|
||||
}
|
||||
}
|
||||
|
||||
fn find_iter<'a, Input, F>(iterable: Input) -> (usize, F)
|
||||
where
|
||||
Input: IntoIterator,
|
||||
Input::Item: MatchFind,
|
||||
F: FromIterator<<Input::Item as MatchFind>::Range>,
|
||||
{
|
||||
let mut end = 0;
|
||||
let value = iterable
|
||||
.into_iter()
|
||||
.map(|m| {
|
||||
end = m.end();
|
||||
m.as_match()
|
||||
})
|
||||
.collect();
|
||||
(end, value)
|
||||
}
|
||||
|
||||
#[cfg(feature = "regex")]
|
||||
mod regex {
|
||||
pub extern crate regex;
|
||||
|
||||
use std::iter::FromIterator;
|
||||
|
||||
use super::{find_iter, MatchFind, Regex};
|
||||
|
||||
impl<'t> MatchFind for regex::Match<'t> {
|
||||
type Range = &'t str;
|
||||
fn end(&self) -> usize {
|
||||
regex::Match::end(self)
|
||||
}
|
||||
fn as_match(&self) -> Self::Range {
|
||||
self.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'t> MatchFind for regex::bytes::Match<'t> {
|
||||
type Range = &'t [u8];
|
||||
fn end(&self) -> usize {
|
||||
regex::bytes::Match::end(self)
|
||||
}
|
||||
fn as_match(&self) -> Self::Range {
|
||||
self.as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Regex<&'a str> for regex::Regex {
|
||||
fn is_match(&self, range: &'a str) -> bool {
|
||||
regex::Regex::is_match(self, range)
|
||||
}
|
||||
fn find_iter<F>(&self, range: &'a str) -> (usize, F)
|
||||
where
|
||||
F: FromIterator<&'a str>,
|
||||
{
|
||||
find_iter(regex::Regex::find_iter(self, range))
|
||||
}
|
||||
fn captures<F, G>(&self, range: &'a str) -> (usize, G)
|
||||
where
|
||||
F: FromIterator<&'a str>,
|
||||
G: FromIterator<F>,
|
||||
{
|
||||
let mut end = 0;
|
||||
let value = regex::Regex::captures_iter(self, range)
|
||||
.map(|captures| {
|
||||
let mut captures_iter = captures.iter();
|
||||
// The first group is the match on the entire regex
|
||||
let first_match = captures_iter.next().unwrap().unwrap();
|
||||
end = first_match.end();
|
||||
Some(Some(first_match))
|
||||
.into_iter()
|
||||
.chain(captures_iter)
|
||||
.filter_map(|match_| match_.map(|m| m.as_match()))
|
||||
.collect()
|
||||
})
|
||||
.collect();
|
||||
(end, value)
|
||||
}
|
||||
fn as_str(&self) -> &str {
|
||||
regex::Regex::as_str(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Regex<&'a [u8]> for regex::bytes::Regex {
|
||||
fn is_match(&self, range: &'a [u8]) -> bool {
|
||||
regex::bytes::Regex::is_match(self, range)
|
||||
}
|
||||
fn find_iter<F>(&self, range: &'a [u8]) -> (usize, F)
|
||||
where
|
||||
F: FromIterator<&'a [u8]>,
|
||||
{
|
||||
find_iter(regex::bytes::Regex::find_iter(self, range))
|
||||
}
|
||||
fn captures<F, G>(&self, range: &'a [u8]) -> (usize, G)
|
||||
where
|
||||
F: FromIterator<&'a [u8]>,
|
||||
G: FromIterator<F>,
|
||||
{
|
||||
let mut end = 0;
|
||||
let value = regex::bytes::Regex::captures_iter(self, range)
|
||||
.map(|captures| {
|
||||
let mut captures_iter = captures.iter();
|
||||
// The first group is the match on the entire regex
|
||||
let first_match = captures_iter.next().unwrap().unwrap();
|
||||
end = first_match.end();
|
||||
Some(Some(first_match))
|
||||
.into_iter()
|
||||
.chain(captures_iter)
|
||||
.filter_map(|match_| match_.map(|m| m.as_match()))
|
||||
.collect()
|
||||
})
|
||||
.collect();
|
||||
(end, value)
|
||||
}
|
||||
fn as_str(&self) -> &str {
|
||||
regex::bytes::Regex::as_str(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Match<R, Input>(R, PhantomData<Input>);
|
||||
|
||||
impl<'a, Input, R> Parser<Input> for Match<R, Input>
|
||||
where
|
||||
R: Regex<Input::Range>,
|
||||
Input: RangeStream,
|
||||
{
|
||||
type Output = Input::Range;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error> {
|
||||
if self.0.is_match(input.range()) {
|
||||
PeekOk(input.range())
|
||||
} else {
|
||||
PeekErr(Input::Error::empty(input.position()).into())
|
||||
}
|
||||
}
|
||||
fn add_error(&mut self, error: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
error.error.add(StreamError::expected_format(format_args!(
|
||||
"/{}/",
|
||||
self.0.as_str()
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Matches `regex` on the input returning the entire input if it matches.
|
||||
/// Never consumes any input.
|
||||
///
|
||||
/// ```
|
||||
/// extern crate regex;
|
||||
/// extern crate combine;
|
||||
/// use regex::Regex;
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::regex::match_;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let regex = Regex::new("[:alpha:]+").unwrap();
|
||||
/// assert_eq!(
|
||||
/// match_(®ex).parse("abc123"),
|
||||
/// Ok(("abc123", "abc123"))
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
pub fn match_<R, Input>(regex: R) -> Match<R, Input>
|
||||
where
|
||||
R: Regex<Input::Range>,
|
||||
Input: RangeStream,
|
||||
{
|
||||
Match(regex, PhantomData)
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Find<R, Input>(R, PhantomData<fn() -> Input>);
|
||||
|
||||
impl<'a, Input, R> Parser<Input> for Find<R, Input>
|
||||
where
|
||||
R: Regex<Input::Range>,
|
||||
Input: RangeStream,
|
||||
Input::Range: crate::stream::Range,
|
||||
{
|
||||
type Output = Input::Range;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error> {
|
||||
let (end, First(value)) = self.0.find_iter(input.range());
|
||||
match value {
|
||||
Some(value) => take(end).parse_lazy(input).map(|_| value),
|
||||
None => PeekErr(Input::Error::empty(input.position()).into()),
|
||||
}
|
||||
}
|
||||
fn add_error(&mut self, error: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
error.error.add(StreamError::expected_format(format_args!(
|
||||
"/{}/",
|
||||
self.0.as_str()
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Matches `regex` on the input by running `find` on the input and returns the first match.
|
||||
/// Consumes all input up until the end of the first match.
|
||||
///
|
||||
/// ```
|
||||
/// extern crate regex;
|
||||
/// extern crate combine;
|
||||
/// use regex::Regex;
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::regex::find;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let mut digits = find(Regex::new("^[0-9]+").unwrap());
|
||||
/// assert_eq!(digits.parse("123 456 "), Ok(("123", " 456 ")));
|
||||
/// assert!(
|
||||
/// digits.parse("abc 123 456 ").is_err());
|
||||
///
|
||||
/// let mut digits2 = find(Regex::new("[0-9]+").unwrap());
|
||||
/// assert_eq!(digits2.parse("123 456 "), Ok(("123", " 456 ")));
|
||||
/// assert_eq!(digits2.parse("abc 123 456 "), Ok(("123", " 456 ")));
|
||||
/// }
|
||||
/// ```
|
||||
pub fn find<R, Input>(regex: R) -> Find<R, Input>
|
||||
where
|
||||
R: Regex<Input::Range>,
|
||||
Input: RangeStream,
|
||||
Input::Range: crate::stream::Range,
|
||||
{
|
||||
Find(regex, PhantomData)
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FindMany<F, R, Input>(R, PhantomData<fn() -> (Input, F)>);
|
||||
|
||||
impl<'a, Input, F, R> Parser<Input> for FindMany<F, R, Input>
|
||||
where
|
||||
F: FromIterator<Input::Range>,
|
||||
R: Regex<Input::Range>,
|
||||
Input: RangeStream,
|
||||
Input::Range: crate::stream::Range,
|
||||
{
|
||||
type Output = F;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error> {
|
||||
let (end, value) = self.0.find_iter(input.range());
|
||||
take(end).parse_lazy(input).map(|_| value)
|
||||
}
|
||||
fn add_error(&mut self, error: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
error.error.add(StreamError::expected_format(format_args!(
|
||||
"/{}/",
|
||||
self.0.as_str()
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Matches `regex` on the input by running `find_iter` on the input.
|
||||
/// Returns all matches in a `F: FromIterator<Input::Range>`.
|
||||
/// Consumes all input up until the end of the last match.
|
||||
///
|
||||
/// ```
|
||||
/// extern crate regex;
|
||||
/// extern crate combine;
|
||||
/// use regex::Regex;
|
||||
/// use regex::bytes;
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::regex::find_many;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let mut digits = find_many(Regex::new("[0-9]+").unwrap());
|
||||
/// assert_eq!(digits.parse("123 456 "), Ok((vec!["123", "456"], " ")));
|
||||
/// assert_eq!(digits.parse("abc 123 456 "), Ok((vec!["123", "456"], " ")));
|
||||
/// assert_eq!(digits.parse("abc"), Ok((vec![], "abc")));
|
||||
/// }
|
||||
/// ```
|
||||
pub fn find_many<F, R, Input>(regex: R) -> FindMany<F, R, Input>
|
||||
where
|
||||
F: FromIterator<Input::Range>,
|
||||
R: Regex<Input::Range>,
|
||||
Input: RangeStream,
|
||||
Input::Range: crate::stream::Range,
|
||||
{
|
||||
FindMany(regex, PhantomData)
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Captures<F, R, Input>(R, PhantomData<fn() -> (Input, F)>);
|
||||
|
||||
impl<'a, Input, F, R> Parser<Input> for Captures<F, R, Input>
|
||||
where
|
||||
F: FromIterator<Input::Range>,
|
||||
R: Regex<Input::Range>,
|
||||
Input: RangeStream,
|
||||
Input::Range: crate::stream::Range,
|
||||
{
|
||||
type Output = F;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error> {
|
||||
let (end, First(value)) = self.0.captures(input.range());
|
||||
match value {
|
||||
Some(value) => take(end).parse_lazy(input).map(|_| value),
|
||||
None => PeekErr(Input::Error::empty(input.position()).into()),
|
||||
}
|
||||
}
|
||||
fn add_error(&mut self, error: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
error.error.add(StreamError::expected_format(format_args!(
|
||||
"/{}/",
|
||||
self.0.as_str()
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Matches `regex` on the input by running `captures_iter` on the input.
|
||||
/// Returns the captures of the first match and consumes the input up until the end of that match.
|
||||
///
|
||||
/// ```
|
||||
/// extern crate regex;
|
||||
/// extern crate combine;
|
||||
/// use regex::Regex;
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::regex::captures;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let mut fields = captures(Regex::new("([a-z]+):([0-9]+)").unwrap());
|
||||
/// assert_eq!(
|
||||
/// fields.parse("test:123 field:456 "),
|
||||
/// Ok((vec!["test:123", "test", "123"],
|
||||
/// " field:456 "
|
||||
/// ))
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// fields.parse("test:123 :456 "),
|
||||
/// Ok((vec!["test:123", "test", "123"],
|
||||
/// " :456 "
|
||||
/// ))
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
pub fn captures<F, R, Input>(regex: R) -> Captures<F, R, Input>
|
||||
where
|
||||
F: FromIterator<Input::Range>,
|
||||
R: Regex<Input::Range>,
|
||||
Input: RangeStream,
|
||||
Input::Range: crate::stream::Range,
|
||||
{
|
||||
Captures(regex, PhantomData)
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CapturesMany<F, G, R, Input>(R, PhantomData<fn() -> (Input, F, G)>);
|
||||
|
||||
impl<'a, Input, F, G, R> Parser<Input> for CapturesMany<F, G, R, Input>
|
||||
where
|
||||
F: FromIterator<Input::Range>,
|
||||
G: FromIterator<F>,
|
||||
R: Regex<Input::Range>,
|
||||
Input: RangeStream,
|
||||
Input::Range: crate::stream::Range,
|
||||
{
|
||||
type Output = G;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error> {
|
||||
let (end, value) = self.0.captures(input.range());
|
||||
take(end).parse_lazy(input).map(|_| value)
|
||||
}
|
||||
fn add_error(&mut self, error: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
error.error.add(StreamError::expected_format(format_args!(
|
||||
"/{}/",
|
||||
self.0.as_str()
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Matches `regex` on the input by running `captures_iter` on the input.
|
||||
/// Returns all captures which is part of the match in a `F: FromIterator<Input::Range>`.
|
||||
/// Consumes all input up until the end of the last match.
|
||||
///
|
||||
/// ```
|
||||
/// extern crate regex;
|
||||
/// extern crate combine;
|
||||
/// use regex::Regex;
|
||||
/// use combine::Parser;
|
||||
/// use combine::parser::regex::captures_many;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let mut fields = captures_many(Regex::new("([a-z]+):([0-9]+)").unwrap());
|
||||
/// assert_eq!(
|
||||
/// fields.parse("test:123 field:456 "),
|
||||
/// Ok((vec![vec!["test:123", "test", "123"],
|
||||
/// vec!["field:456", "field", "456"]],
|
||||
/// " "
|
||||
/// ))
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// fields.parse("test:123 :456 "),
|
||||
/// Ok((vec![vec!["test:123", "test", "123"]],
|
||||
/// " :456 "
|
||||
/// ))
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
pub fn captures_many<F, G, R, Input>(regex: R) -> CapturesMany<F, G, R, Input>
|
||||
where
|
||||
F: FromIterator<Input::Range>,
|
||||
G: FromIterator<F>,
|
||||
R: Regex<Input::Range>,
|
||||
Input: RangeStream,
|
||||
Input::Range: crate::stream::Range,
|
||||
{
|
||||
CapturesMany(regex, PhantomData)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
use crate::{parser::regex::find, Parser};
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
let mut digits = find(Regex::new("^[0-9]+").unwrap());
|
||||
assert_eq!(digits.parse("123 456 "), Ok(("123", " 456 ")));
|
||||
assert!(digits.parse("abc 123 456 ").is_err());
|
||||
|
||||
let mut digits2 = find(Regex::new("[0-9]+").unwrap());
|
||||
assert_eq!(digits2.parse("123 456 "), Ok(("123", " 456 ")));
|
||||
assert_eq!(digits2.parse("abc 123 456 "), Ok(("123", " 456 ")));
|
||||
}
|
||||
}
|
||||
Vendored
+1620
File diff suppressed because it is too large
Load Diff
+907
@@ -0,0 +1,907 @@
|
||||
//! Combinators which take multiple parsers and applies them one after another.
|
||||
|
||||
use crate::{
|
||||
error::{
|
||||
ParseError,
|
||||
ParseResult::{self, *},
|
||||
StreamError, Tracked,
|
||||
},
|
||||
lib::marker::PhantomData,
|
||||
parser::{
|
||||
combinator::{ignore, Ignore, Map},
|
||||
ParseMode,
|
||||
},
|
||||
ErrorOffset, Parser, Stream, StreamOnce,
|
||||
};
|
||||
|
||||
macro_rules! count {
|
||||
() => { 0 };
|
||||
($f: ident) => { 1 };
|
||||
($f: ident, $($rest: ident),+) => { 1 + count!($($rest),*) };
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct SequenceState<T, U> {
|
||||
pub value: Option<T>,
|
||||
pub state: U,
|
||||
}
|
||||
|
||||
impl<T, U: Default> Default for SequenceState<T, U> {
|
||||
fn default() -> Self {
|
||||
SequenceState {
|
||||
value: None,
|
||||
state: U::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U> SequenceState<T, U>
|
||||
where
|
||||
U: Default,
|
||||
{
|
||||
unsafe fn unwrap_value(&mut self) -> T {
|
||||
match self.value.take() {
|
||||
Some(t) => t,
|
||||
None => core::hint::unreachable_unchecked(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! last_ident {
|
||||
($id: ident) => { $id };
|
||||
($id: ident, $($rest: ident),+) => { last_ident!($($rest),+) };
|
||||
}
|
||||
|
||||
fn add_sequence_error<Input>(
|
||||
i: &mut usize,
|
||||
first_empty_parser: usize,
|
||||
inner_offset: ErrorOffset,
|
||||
err: &mut Tracked<Input::Error>,
|
||||
parser: &mut impl Parser<Input>,
|
||||
) -> bool
|
||||
where
|
||||
Input: Stream,
|
||||
{
|
||||
if *i + 1 == first_empty_parser {
|
||||
Parser::add_committed_expected_error(parser, err);
|
||||
}
|
||||
if *i >= first_empty_parser {
|
||||
if err.offset <= ErrorOffset(1) {
|
||||
// We reached the last parser we need to add errors to (and the
|
||||
// parser that actually returned the error), use the returned
|
||||
// offset for that parser.
|
||||
err.offset = inner_offset;
|
||||
}
|
||||
Parser::add_error(parser, err);
|
||||
if err.offset <= ErrorOffset(1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
err.offset = ErrorOffset(err.offset.0.saturating_sub(Parser::parser_count(parser).0));
|
||||
|
||||
*i += 1;
|
||||
true
|
||||
}
|
||||
|
||||
macro_rules! tuple_parser {
|
||||
($partial_state: ident; $h: ident $(, $id: ident)*) => {
|
||||
#[allow(non_snake_case)]
|
||||
#[derive(Default)]
|
||||
pub struct $partial_state < $h $(, $id )* > {
|
||||
pub $h: $h,
|
||||
$(
|
||||
pub $id: $id,
|
||||
)*
|
||||
#[allow(dead_code)]
|
||||
offset: u8,
|
||||
_marker: PhantomData <( $h, $( $id),* )>,
|
||||
}
|
||||
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
impl<$h $(, $id)*> $partial_state<$h $(, $id)*> {
|
||||
#[allow(dead_code)]
|
||||
fn add_errors<Input>(
|
||||
input: &mut Input,
|
||||
mut err: Tracked<Input::Error>,
|
||||
first_empty_parser: usize,
|
||||
offset: u8,
|
||||
$h: &mut $h $(, $id : &mut $id )*
|
||||
) -> ParseResult<($h::Output, $($id::Output),*), <Input as StreamOnce>::Error>
|
||||
where Input: Stream,
|
||||
$h: Parser<Input>,
|
||||
$($id: Parser<Input>),*
|
||||
{
|
||||
let inner_offset = err.offset;
|
||||
err.offset = ErrorOffset(offset);
|
||||
if first_empty_parser != 0 {
|
||||
if let Ok(t) = input.uncons() {
|
||||
err.error.add(StreamError::unexpected_token(t));
|
||||
}
|
||||
|
||||
#[allow(unused_assignments)]
|
||||
let mut i = 0;
|
||||
loop {
|
||||
if !add_sequence_error(&mut i, first_empty_parser, inner_offset, &mut err, $h) {
|
||||
break;
|
||||
}
|
||||
$(
|
||||
if !add_sequence_error(&mut i, first_empty_parser, inner_offset, &mut err, $id) {
|
||||
break;
|
||||
}
|
||||
)*
|
||||
break;
|
||||
}
|
||||
CommitErr(err.error)
|
||||
} else {
|
||||
PeekErr(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
impl <Input: Stream, $h:, $($id:),*> Parser<Input> for ($h, $($id),*)
|
||||
where Input: Stream,
|
||||
$h: Parser<Input>,
|
||||
$($id: Parser<Input>),*
|
||||
{
|
||||
|
||||
type Output = ($h::Output, $($id::Output),*);
|
||||
type PartialState = $partial_state<
|
||||
SequenceState<$h::Output, $h::PartialState>
|
||||
$(, SequenceState<$id::Output, $id::PartialState>)*
|
||||
>;
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_impl<MODE>(
|
||||
&mut self,
|
||||
mut mode: MODE,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
MODE: ParseMode,
|
||||
{
|
||||
let (ref mut $h, $(ref mut $id),*) = *self;
|
||||
let mut first_empty_parser = 0;
|
||||
#[allow(unused_mut)]
|
||||
let mut current_parser = 0;
|
||||
|
||||
#[allow(unused_macros)]
|
||||
macro_rules! add_errors {
|
||||
($err: ident, $offset: expr) => {
|
||||
$partial_state::add_errors(
|
||||
input, $err, first_empty_parser, $offset, $h, $($id),*
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if mode.is_first() || state.$h.value.is_none() {
|
||||
let temp = match $h.parse_mode(mode, input, &mut state.$h.state) {
|
||||
CommitOk(x) => {
|
||||
first_empty_parser = current_parser + 1;
|
||||
x
|
||||
}
|
||||
PeekErr(err) => return PeekErr(err),
|
||||
CommitErr(err) => return CommitErr(err),
|
||||
PeekOk(x) => {
|
||||
x
|
||||
}
|
||||
};
|
||||
state.offset = $h.parser_count().0.saturating_add(1);
|
||||
// SAFETY: must be set to avoid UB below when unwrapping
|
||||
state.$h.value = Some(temp);
|
||||
|
||||
// Once we have successfully parsed the partial input we may resume parsing in
|
||||
// "first mode"
|
||||
mode.set_first();
|
||||
}
|
||||
|
||||
$(
|
||||
if mode.is_first() || state.$id.value.is_none() {
|
||||
current_parser += 1;
|
||||
let before = input.checkpoint();
|
||||
let temp = match $id.parse_mode(mode, input, &mut state.$id.state) {
|
||||
CommitOk(x) => {
|
||||
first_empty_parser = current_parser + 1;
|
||||
x
|
||||
}
|
||||
PeekErr(err) => {
|
||||
if let Err(err) = input.reset(before) {
|
||||
return if first_empty_parser != 0 {
|
||||
CommitErr(err.into())
|
||||
} else {
|
||||
PeekErr(err.into())
|
||||
};
|
||||
}
|
||||
return add_errors!(err, state.offset)
|
||||
}
|
||||
CommitErr(err) => return CommitErr(err),
|
||||
PeekOk(x) => {
|
||||
x
|
||||
}
|
||||
};
|
||||
state.offset = state.offset.saturating_add($id.parser_count().0);
|
||||
// SAFETY: must be set to avoid UB below when unwrapping
|
||||
state.$id.value = Some(temp);
|
||||
|
||||
// Once we have successfully parsed the partial input we may resume parsing in
|
||||
// "first mode"
|
||||
mode.set_first();
|
||||
}
|
||||
)*
|
||||
|
||||
// SAFETY: requires both $h and $id to be set, see previous SAFETY comments
|
||||
let value = unsafe { (state.$h.unwrap_value(), $(state.$id.unwrap_value()),*) };
|
||||
if first_empty_parser != 0 {
|
||||
CommitOk(value)
|
||||
} else {
|
||||
PeekOk(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn parser_count(&self) -> ErrorOffset {
|
||||
let (ref $h, $(ref $id),*) = *self;
|
||||
ErrorOffset($h.parser_count().0 $( + $id.parser_count().0)*)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
let (ref mut $h, $(ref mut $id),*) = *self;
|
||||
let prev = errors.offset;
|
||||
$h.add_error(errors);
|
||||
if errors.offset <= ErrorOffset(1) {
|
||||
errors.offset = ErrorOffset(
|
||||
errors.offset.0.saturating_sub(1)
|
||||
);
|
||||
return;
|
||||
}
|
||||
if errors.offset == prev {
|
||||
errors.offset = ErrorOffset(errors.offset.0.saturating_sub($h.parser_count().0));
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
const LAST: usize = count!($($id),*);
|
||||
#[allow(unused_mut, unused_variables)]
|
||||
let mut i = 0;
|
||||
$(
|
||||
i += 1;
|
||||
let prev = errors.offset;
|
||||
$id.add_error(errors);
|
||||
if errors.offset <= ErrorOffset(1) {
|
||||
errors.offset = ErrorOffset(
|
||||
errors.offset.0.saturating_sub(1)
|
||||
);
|
||||
return;
|
||||
}
|
||||
if i != LAST && errors.offset == prev {
|
||||
errors.offset = ErrorOffset(
|
||||
errors.offset.0.saturating_sub($id.parser_count().0)
|
||||
);
|
||||
}
|
||||
)*
|
||||
}
|
||||
|
||||
fn add_committed_expected_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
#[allow(unused_variables)]
|
||||
let (ref mut $h, $(ref mut $id),*) = *self;
|
||||
last_ident!($h $(, $id)*).add_committed_expected_error(errors)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tuple_parser!(PartialState1; A);
|
||||
tuple_parser!(PartialState2; A, B);
|
||||
tuple_parser!(PartialState3; A, B, C);
|
||||
tuple_parser!(PartialState4; A, B, C, D);
|
||||
tuple_parser!(PartialState5; A, B, C, D, E);
|
||||
tuple_parser!(PartialState6; A, B, C, D, E, F);
|
||||
tuple_parser!(PartialState7; A, B, C, D, E, F, G);
|
||||
tuple_parser!(PartialState8; A, B, C, D, E, F, G, H);
|
||||
tuple_parser!(PartialState9; A, B, C, D, E, F, G, H, I);
|
||||
tuple_parser!(PartialState10; A, B, C, D, E, F, G, H, I, J);
|
||||
tuple_parser!(PartialState11; A, B, C, D, E, F, G, H, I, J, K);
|
||||
tuple_parser!(PartialState12; A, B, C, D, E, F, G, H, I, J, K, L);
|
||||
tuple_parser!(PartialState13; A, B, C, D, E, F, G, H, I, J, K, L, M);
|
||||
tuple_parser!(PartialState14; A, B, C, D, E, F, G, H, I, J, K, L, M, N);
|
||||
tuple_parser!(PartialState15; A, B, C, D, E, F, G, H, I, J, K, L, M, N, P);
|
||||
tuple_parser!(PartialState16; A, B, C, D, E, F, G, H, I, J, K, L, M, N, P, Q);
|
||||
tuple_parser!(PartialState17; A, B, C, D, E, F, G, H, I, J, K, L, M, N, P, Q, R);
|
||||
tuple_parser!(PartialState18; A, B, C, D, E, F, G, H, I, J, K, L, M, N, P, Q, R, S);
|
||||
tuple_parser!(PartialState19; A, B, C, D, E, F, G, H, I, J, K, L, M, N, P, Q, R, S, T);
|
||||
tuple_parser!(PartialState20; A, B, C, D, E, F, G, H, I, J, K, L, M, N, P, Q, R, S, T, U);
|
||||
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! seq_parser_expr {
|
||||
(; $($tt: tt)*) => {
|
||||
( $($tt)* )
|
||||
};
|
||||
( (_ : $first_parser: expr, $($remaining: tt)+ ); $($tt: tt)*) => {
|
||||
$crate::seq_parser_expr!( ( $($remaining)+ ) ; $($tt)* $first_parser, )
|
||||
};
|
||||
( ($first_field: ident : $first_parser: expr, $($remaining: tt)+ ); $($tt: tt)*) => {
|
||||
$crate::seq_parser_expr!( ( $($remaining)+ ) ; $($tt)* $first_parser, )
|
||||
};
|
||||
( (_ : $first_parser: expr ); $($tt: tt)*) => {
|
||||
( $($tt)* $first_parser, )
|
||||
};
|
||||
( ($first_field: ident : $first_parser: expr, ); $($tt: tt)*) => {
|
||||
$crate::seq_parser_expr!(; $($tt)* $first_parser,)
|
||||
};
|
||||
( (_ : $first_parser: expr, ); $($tt: tt)*) => {
|
||||
( $($tt)* $first_parser, )
|
||||
};
|
||||
( ($first_field: ident : $first_parser: expr ); $($tt: tt)*) => {
|
||||
$crate::seq_parser_expr!(; $($tt)* $first_parser,)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! seq_parser_pattern {
|
||||
(; $($tt: tt)*) => {
|
||||
( $($tt)* )
|
||||
};
|
||||
( (_ : $first_parser: expr, $($remaining: tt)+ ); $($tt: tt)*) => {
|
||||
$crate::seq_parser_pattern!( ( $($remaining)+ ) ; $($tt)* _, )
|
||||
};
|
||||
( ($first_field: ident : $first_parser: expr, $($remaining: tt)+ ); $($tt: tt)*) => {
|
||||
$crate::seq_parser_pattern!( ( $($remaining)+ ) ; $($tt)* $first_field, )
|
||||
};
|
||||
( ( _ : $first_parser: expr ); $($tt: tt)*) => {
|
||||
$crate::seq_parser_pattern!(; $($tt)* _, )
|
||||
};
|
||||
( ($first_field: ident : $first_parser: expr ); $($tt: tt)*) => {
|
||||
$crate::seq_parser_pattern!(; $($tt)* $first_field,)
|
||||
};
|
||||
( ( _ : $first_parser: expr, ); $($tt: tt)*) => {
|
||||
$crate::seq_parser_pattern!(; $($tt)* _, )
|
||||
};
|
||||
( ($first_field: ident : $first_parser: expr, ); $($tt: tt)*) => {
|
||||
$crate::seq_parser_pattern!(; $($tt)* $first_field,)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! seq_parser_impl {
|
||||
(; $name: ident $($tt: tt)*) => {
|
||||
$name { $($tt)* }
|
||||
};
|
||||
( (_ : $first_parser: expr, $($remaining: tt)+ ); $name: ident $($tt: tt)*) => {
|
||||
$crate::seq_parser_impl!( ( $($remaining)+ ) ; $name $($tt)* )
|
||||
};
|
||||
( ($first_field: ident : $first_parser: expr, $($remaining: tt)+ );
|
||||
$name: ident $($tt: tt)*) =>
|
||||
{
|
||||
$crate::seq_parser_impl!( ( $($remaining)+ ) ; $name $($tt)* $first_field: $first_field, )
|
||||
};
|
||||
( ( _ : $first_parser: expr ); $name: ident $($tt: tt)*) => {
|
||||
$crate::seq_parser_impl!( ; $name $($tt)* )
|
||||
};
|
||||
( ($first_field: ident : $first_parser: expr ); $name: ident $($tt: tt)*) => {
|
||||
$crate::seq_parser_impl!(; $name $($tt)* $first_field: $first_field,)
|
||||
};
|
||||
( ( _ : $first_parser: expr, ); $name: ident $($tt: tt)*) => {
|
||||
$crate::seq_parser_impl!(; $name $($tt)*)
|
||||
};
|
||||
( ($first_field: ident : $first_parser: expr, ); $name: ident $($tt: tt)*) => {
|
||||
$crate::seq_parser_impl!(; $name $($tt)* $first_field: $first_field,)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! seq_tuple_extract {
|
||||
(; ; $name: ident ; $($arg: expr),* $(,)? ) => {
|
||||
$name( $($arg,)* )
|
||||
};
|
||||
|
||||
( (_ : $first_parser: expr, $($remaining: tt)+ ); ( $first_arg: expr, $($arg: expr),* ) ; $($tt: tt)*) => {
|
||||
$crate::seq_tuple_extract!( ( $($remaining)+ ); ( $($arg),* ) ; $($tt)* )
|
||||
};
|
||||
|
||||
( ($first_parser: expr, $($remaining: tt)+ ); ( $first_arg: expr, $($arg: expr),* ) ; $($tt: tt)*) => {
|
||||
$crate::seq_tuple_extract!( ( $($remaining)+ ) ; ( $($arg),* ) ; $($tt)* $first_arg, )
|
||||
};
|
||||
|
||||
( (_ : $first_parser: expr $(,)? ); ( $first_arg: expr, $($arg: expr),* ) ; $($tt: tt)*) => {
|
||||
$crate::seq_tuple_extract!(; ; $($tt)*)
|
||||
};
|
||||
|
||||
( ($first_parser: expr $(,)? ); ( $first_arg: expr, $($arg: expr),* ) ; $($tt: tt)*) => {
|
||||
$crate::seq_tuple_extract!(; ; $($tt)* $first_arg)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! seq_tuple_parser_impl {
|
||||
(; $($tt: tt)*) => {
|
||||
($($tt)*)
|
||||
};
|
||||
|
||||
( (_ : $first_parser: expr, $($remaining: tt)+ ); $($tt: tt)*) => {
|
||||
$crate::seq_tuple_parser_impl!( ( $($remaining)+ ) ; $($tt)* $first_parser, )
|
||||
};
|
||||
|
||||
( ($first_parser: expr, $($remaining: tt)+ ); $($tt: tt)*) => {
|
||||
$crate::seq_tuple_parser_impl!( ( $($remaining)+ ) ; $($tt)* $first_parser, )
|
||||
};
|
||||
|
||||
( (_ : $first_parser: expr $(,)? ); $($tt: tt)*) => {
|
||||
$crate::seq_tuple_parser_impl!(; $($tt)* $first_parser, )
|
||||
};
|
||||
|
||||
( ($first_parser: expr $(,)? ); $($tt: tt)*) => {
|
||||
$crate::seq_tuple_parser_impl!(; $($tt)* $first_parser, )
|
||||
};
|
||||
}
|
||||
|
||||
/// Sequences multiple parsers and builds a struct out of them.
|
||||
///
|
||||
/// ```
|
||||
/// use combine::{Parser, between, from_str, many, struct_parser, token};
|
||||
/// use combine::parser::range::take_while1;
|
||||
/// use combine::parser::byte::{letter, spaces};
|
||||
///
|
||||
/// #[derive(Debug, PartialEq)]
|
||||
/// struct Point(u32, u32);
|
||||
///
|
||||
/// #[derive(Debug, PartialEq)]
|
||||
/// struct Field {
|
||||
/// name: Vec<u8>,
|
||||
/// value: Vec<u8>,
|
||||
/// point: Point,
|
||||
/// }
|
||||
/// fn main() {
|
||||
/// let num = || from_str(take_while1(|b: u8| b >= b'0' && b <= b'9'));
|
||||
/// let spaced = |b| between(spaces(), spaces(), token(b));
|
||||
/// let mut parser = struct_parser!{
|
||||
/// Field {
|
||||
/// name: many(letter()),
|
||||
/// // `_` fields are ignored when building the struct
|
||||
/// _: spaced(b':'),
|
||||
/// value: many(letter()),
|
||||
/// _: spaced(b':'),
|
||||
/// point: struct_parser!(Point(num(), _: spaced(b','), num())),
|
||||
/// }
|
||||
/// };
|
||||
/// assert_eq!(
|
||||
/// parser.parse(&b"test: data: 123 , 4"[..]),
|
||||
/// Ok((
|
||||
/// Field {
|
||||
/// name: b"test"[..].to_owned(),
|
||||
/// value: b"data"[..].to_owned(),
|
||||
/// point: Point(123, 4),
|
||||
/// },
|
||||
/// &b""[..]
|
||||
/// )),
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! struct_parser {
|
||||
($name: ident { $($tt: tt)* }) => {
|
||||
$crate::seq_parser_expr!( ( $($tt)* ); )
|
||||
.map(|$crate::seq_parser_pattern!( ( $($tt)* ); )|
|
||||
$crate::seq_parser_impl!(( $($tt)* ); $name )
|
||||
)
|
||||
};
|
||||
|
||||
($name: ident ( $($arg: tt)* )) => {
|
||||
$crate::seq_tuple_parser_impl!( ( $($arg)* ) ; )
|
||||
.map(|t|
|
||||
$crate::seq_tuple_extract!(
|
||||
( $($arg)* );
|
||||
(t.0, t.1, t.2, t.3, t.4, t.5, t.6, t.7, t.8, t.9, t.10, t.11, t.12, t.13, t.14);
|
||||
$name ;
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct With<P1, P2>((Ignore<P1>, P2));
|
||||
impl<Input, P1, P2> Parser<Input> for With<P1, P2>
|
||||
where
|
||||
Input: Stream,
|
||||
P1: Parser<Input>,
|
||||
P2: Parser<Input>,
|
||||
{
|
||||
type Output = P2::Output;
|
||||
type PartialState = <(Ignore<P1>, P2) as Parser<Input>>::PartialState;
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(
|
||||
&mut self,
|
||||
input: &mut Input,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error> {
|
||||
self.0.parse_lazy(input).map(|(_, b)| b)
|
||||
}
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_impl<M>(
|
||||
&mut self,
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
self.0.parse_mode(mode, input, state).map(|(_, b)| b)
|
||||
}
|
||||
|
||||
forward_parser!(Input, add_error add_committed_expected_error parser_count, 0);
|
||||
}
|
||||
|
||||
/// Equivalent to [`p1.with(p2)`].
|
||||
///
|
||||
/// [`p1.with(p2)`]: ../trait.Parser.html#method.with
|
||||
pub fn with<Input, P1, P2>(p1: P1, p2: P2) -> With<P1, P2>
|
||||
where
|
||||
Input: Stream,
|
||||
P1: Parser<Input>,
|
||||
P2: Parser<Input>,
|
||||
{
|
||||
With((ignore(p1), p2))
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Skip<P1, P2>((P1, Ignore<P2>));
|
||||
impl<Input, P1, P2> Parser<Input> for Skip<P1, P2>
|
||||
where
|
||||
Input: Stream,
|
||||
P1: Parser<Input>,
|
||||
P2: Parser<Input>,
|
||||
{
|
||||
type Output = P1::Output;
|
||||
type PartialState = <(P1, Ignore<P2>) as Parser<Input>>::PartialState;
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_impl<M>(
|
||||
&mut self,
|
||||
mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
self.0.parse_mode(mode, input, state).map(|(a, _)| a)
|
||||
}
|
||||
|
||||
forward_parser!(Input, add_error add_committed_expected_error parser_count, 0);
|
||||
}
|
||||
|
||||
pub fn skip<Input, P1, P2>(p1: P1, p2: P2) -> Skip<P1, P2>
|
||||
where
|
||||
Input: Stream,
|
||||
P1: Parser<Input>,
|
||||
P2: Parser<Input>,
|
||||
{
|
||||
Skip((p1, ignore(p2)))
|
||||
}
|
||||
|
||||
parser! {
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Between;
|
||||
type PartialState = <Map<(L, P, R), fn ((L::Output, P::Output, R::Output)) -> P::Output> as Parser<Input>>::PartialState;
|
||||
/// Parses `open` followed by `parser` followed by `close`.
|
||||
/// Returns the value of `parser`.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::parser::char::string;
|
||||
/// # fn main() {
|
||||
/// let result = between(token('['), token(']'), string("rust"))
|
||||
/// .parse("[rust]")
|
||||
/// .map(|x| x.0);
|
||||
/// assert_eq!(result, Ok("rust"));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn between[Input, L, R, P](open: L, close: R, parser: P)(Input) -> P::Output
|
||||
where [
|
||||
Input: Stream,
|
||||
L: Parser< Input>,
|
||||
R: Parser< Input>,
|
||||
P: Parser< Input>,
|
||||
]
|
||||
{
|
||||
fn middle<T, U, V>((_, x, _): (T, U, V)) -> U {
|
||||
x
|
||||
}
|
||||
(open, parser, close).map(middle)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Then<P, F>(P, F);
|
||||
impl<Input, P, N, F> Parser<Input> for Then<P, F>
|
||||
where
|
||||
Input: Stream,
|
||||
F: FnMut(P::Output) -> N,
|
||||
P: Parser<Input>,
|
||||
N: Parser<Input>,
|
||||
{
|
||||
type Output = N::Output;
|
||||
type PartialState = (P::PartialState, Option<(bool, N)>, N::PartialState);
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_impl<M>(
|
||||
&mut self,
|
||||
mut mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
let (ref mut p_state, ref mut n_parser_cache, ref mut n_state) = *state;
|
||||
|
||||
if mode.is_first() || n_parser_cache.is_none() {
|
||||
debug_assert!(n_parser_cache.is_none());
|
||||
|
||||
let (value, committed) = match self.0.parse_mode(mode, input, p_state) {
|
||||
PeekOk(value) => (value, false),
|
||||
CommitOk(value) => (value, true),
|
||||
|
||||
PeekErr(err) => return PeekErr(err),
|
||||
CommitErr(err) => return CommitErr(err),
|
||||
};
|
||||
|
||||
*n_parser_cache = Some((committed, (self.1)(value)));
|
||||
mode.set_first();
|
||||
}
|
||||
|
||||
let result = n_parser_cache
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.1
|
||||
.parse_committed_mode(mode, input, n_state);
|
||||
match result {
|
||||
PeekOk(x) => {
|
||||
let (committed, _) = *n_parser_cache.as_ref().unwrap();
|
||||
*n_parser_cache = None;
|
||||
if committed {
|
||||
CommitOk(x)
|
||||
} else {
|
||||
PeekOk(x)
|
||||
}
|
||||
}
|
||||
CommitOk(x) => {
|
||||
*n_parser_cache = None;
|
||||
CommitOk(x)
|
||||
}
|
||||
PeekErr(x) => {
|
||||
let (committed, _) = *n_parser_cache.as_ref().unwrap();
|
||||
*n_parser_cache = None;
|
||||
if committed {
|
||||
CommitErr(x.error)
|
||||
} else {
|
||||
PeekErr(x)
|
||||
}
|
||||
}
|
||||
CommitErr(x) => CommitErr(x),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
self.0.add_error(errors);
|
||||
}
|
||||
}
|
||||
|
||||
/// Equivalent to [`p.then(f)`].
|
||||
///
|
||||
/// [`p.then(f)`]: ../trait.Parser.html#method.then
|
||||
pub fn then<Input, P, F, N>(p: P, f: F) -> Then<P, F>
|
||||
where
|
||||
Input: Stream,
|
||||
F: FnMut(P::Output) -> N,
|
||||
P: Parser<Input>,
|
||||
N: Parser<Input>,
|
||||
{
|
||||
Then(p, f)
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct ThenPartial<P, F>(P, F);
|
||||
impl<Input, P, N, F> Parser<Input> for ThenPartial<P, F>
|
||||
where
|
||||
Input: Stream,
|
||||
F: FnMut(&mut P::Output) -> N,
|
||||
P: Parser<Input>,
|
||||
N: Parser<Input>,
|
||||
{
|
||||
type Output = N::Output;
|
||||
type PartialState = (P::PartialState, Option<(bool, P::Output)>, N::PartialState);
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_impl<M>(
|
||||
&mut self,
|
||||
mut mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
let (ref mut p_state, ref mut n_parser_cache, ref mut n_state) = *state;
|
||||
|
||||
if mode.is_first() || n_parser_cache.is_none() {
|
||||
debug_assert!(n_parser_cache.is_none());
|
||||
|
||||
match self.0.parse_mode(mode, input, p_state) {
|
||||
PeekOk(value) => {
|
||||
*n_parser_cache = Some((false, value));
|
||||
}
|
||||
CommitOk(value) => {
|
||||
*n_parser_cache = Some((true, value));
|
||||
}
|
||||
PeekErr(err) => return PeekErr(err),
|
||||
CommitErr(err) => return CommitErr(err),
|
||||
}
|
||||
mode.set_first();
|
||||
}
|
||||
|
||||
let result = (self.1)(&mut n_parser_cache.as_mut().unwrap().1)
|
||||
.parse_committed_mode(mode, input, n_state);
|
||||
match result {
|
||||
PeekOk(x) => {
|
||||
let (committed, _) = n_parser_cache.take().unwrap();
|
||||
if committed {
|
||||
CommitOk(x)
|
||||
} else {
|
||||
PeekOk(x)
|
||||
}
|
||||
}
|
||||
CommitOk(x) => {
|
||||
*n_parser_cache = None;
|
||||
CommitOk(x)
|
||||
}
|
||||
PeekErr(x) => {
|
||||
let (committed, _) = n_parser_cache.take().unwrap();
|
||||
if committed {
|
||||
CommitErr(x.error)
|
||||
} else {
|
||||
PeekErr(x)
|
||||
}
|
||||
}
|
||||
CommitErr(x) => CommitErr(x),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
self.0.add_error(errors);
|
||||
}
|
||||
}
|
||||
|
||||
/// Equivalent to [`p.then_partial(f)`].
|
||||
///
|
||||
/// [`p.then_partial(f)`]: ../trait.Parser.html#method.then_partial
|
||||
pub fn then_partial<Input, P, F, N>(p: P, f: F) -> ThenPartial<P, F>
|
||||
where
|
||||
Input: Stream,
|
||||
F: FnMut(&mut P::Output) -> N,
|
||||
P: Parser<Input>,
|
||||
N: Parser<Input>,
|
||||
{
|
||||
ThenPartial(p, f)
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "std", test))]
|
||||
mod tests {
|
||||
|
||||
use crate::parser::{token::any, EasyParser};
|
||||
|
||||
#[test]
|
||||
fn sequence_single_parser() {
|
||||
assert!((any(),).easy_parse("a").is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct ThenRef<P, F>(P, F);
|
||||
impl<Input, P, N, F> Parser<Input> for ThenRef<P, F>
|
||||
where
|
||||
Input: Stream,
|
||||
F: FnMut(&P::Output) -> N,
|
||||
P: Parser<Input>,
|
||||
N: Parser<Input>,
|
||||
{
|
||||
type Output = (P::Output, N::Output);
|
||||
type PartialState = (
|
||||
P::PartialState,
|
||||
Option<(bool, P::Output, N)>,
|
||||
N::PartialState,
|
||||
);
|
||||
|
||||
parse_mode!(Input);
|
||||
#[inline]
|
||||
fn parse_mode_impl<M>(
|
||||
&mut self,
|
||||
mut mode: M,
|
||||
input: &mut Input,
|
||||
state: &mut Self::PartialState,
|
||||
) -> ParseResult<Self::Output, <Input as StreamOnce>::Error>
|
||||
where
|
||||
M: ParseMode,
|
||||
{
|
||||
let (ref mut p_state, ref mut n_parser_cache, ref mut n_state) = *state;
|
||||
|
||||
if mode.is_first() || n_parser_cache.is_none() {
|
||||
debug_assert!(n_parser_cache.is_none());
|
||||
|
||||
let (value, committed) = match self.0.parse_mode(mode, input, p_state) {
|
||||
PeekOk(value) => (value, false),
|
||||
CommitOk(value) => (value, true),
|
||||
|
||||
PeekErr(err) => return PeekErr(err),
|
||||
CommitErr(err) => return CommitErr(err),
|
||||
};
|
||||
|
||||
let parser = (self.1)(&value);
|
||||
*n_parser_cache = Some((committed, value, parser));
|
||||
|
||||
mode.set_first();
|
||||
}
|
||||
|
||||
let result = n_parser_cache
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.2
|
||||
.parse_committed_mode(mode, input, n_state);
|
||||
match result {
|
||||
PeekOk(x) => {
|
||||
let (committed, in_value, _) = n_parser_cache.take().unwrap();
|
||||
if committed {
|
||||
CommitOk((in_value, x))
|
||||
} else {
|
||||
PeekOk((in_value, x))
|
||||
}
|
||||
}
|
||||
CommitOk(x) => {
|
||||
let (_, in_value, _) = n_parser_cache.take().unwrap();
|
||||
*n_parser_cache = None;
|
||||
CommitOk((in_value, x))
|
||||
}
|
||||
PeekErr(x) => {
|
||||
let (committed, _, _) = n_parser_cache.take().unwrap();
|
||||
*n_parser_cache = None;
|
||||
if committed {
|
||||
CommitErr(x.error)
|
||||
} else {
|
||||
PeekErr(x)
|
||||
}
|
||||
}
|
||||
CommitErr(x) => CommitErr(x),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
self.0.add_error(errors);
|
||||
}
|
||||
}
|
||||
|
||||
/// Equivalent to [`p.then_ref(f)`].
|
||||
///
|
||||
/// [`p.then_ref(f)`]: ../trait.Parser.html#method.then
|
||||
pub fn then_ref<Input, P, F, N>(p: P, f: F) -> ThenRef<P, F>
|
||||
where
|
||||
Input: Stream,
|
||||
F: FnMut(&P::Output) -> N,
|
||||
P: Parser<Input>,
|
||||
N: Parser<Input>,
|
||||
{
|
||||
ThenRef(p, f)
|
||||
}
|
||||
Vendored
+700
@@ -0,0 +1,700 @@
|
||||
//! Parsers working with single stream items.
|
||||
|
||||
use crate::{
|
||||
error::{
|
||||
self, ErrorInfo, ParseError,
|
||||
ParseResult::{self, *},
|
||||
ResultExt, StreamError, Tracked,
|
||||
},
|
||||
lib::marker::PhantomData,
|
||||
stream::{uncons, Stream, StreamOnce},
|
||||
Parser,
|
||||
};
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Any<Input>(PhantomData<fn(Input) -> Input>);
|
||||
|
||||
impl<Input> Parser<Input> for Any<Input>
|
||||
where
|
||||
Input: Stream,
|
||||
{
|
||||
type Output = Input::Token;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Input::Token, Input::Error> {
|
||||
uncons(input)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses any token.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let mut char_parser = any();
|
||||
/// assert_eq!(char_parser.parse("!").map(|x| x.0), Ok('!'));
|
||||
/// assert!(char_parser.parse("").is_err());
|
||||
/// let mut byte_parser = any();
|
||||
/// assert_eq!(byte_parser.parse(&b"!"[..]).map(|x| x.0), Ok(b'!'));
|
||||
/// assert!(byte_parser.parse(&b""[..]).is_err());
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn any<Input>() -> Any<Input>
|
||||
where
|
||||
Input: Stream,
|
||||
{
|
||||
Any(PhantomData)
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Satisfy<Input, P> {
|
||||
predicate: P,
|
||||
_marker: PhantomData<Input>,
|
||||
}
|
||||
|
||||
fn satisfy_impl<Input, P, R>(input: &mut Input, mut predicate: P) -> ParseResult<R, Input::Error>
|
||||
where
|
||||
Input: Stream,
|
||||
P: FnMut(Input::Token) -> Option<R>,
|
||||
{
|
||||
let position = input.position();
|
||||
match uncons(input) {
|
||||
PeekOk(c) | CommitOk(c) => match predicate(c) {
|
||||
Some(c) => CommitOk(c),
|
||||
None => PeekErr(Input::Error::empty(position).into()),
|
||||
},
|
||||
PeekErr(err) => PeekErr(err),
|
||||
CommitErr(err) => CommitErr(err),
|
||||
}
|
||||
}
|
||||
|
||||
impl<Input, P> Parser<Input> for Satisfy<Input, P>
|
||||
where
|
||||
Input: Stream,
|
||||
P: FnMut(Input::Token) -> bool,
|
||||
{
|
||||
type Output = Input::Token;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Self::Output, Input::Error> {
|
||||
satisfy_impl(input, |c| {
|
||||
if (self.predicate)(c.clone()) {
|
||||
Some(c)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses a token and succeeds depending on the result of `predicate`.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let mut parser = satisfy(|c| c == '!' || c == '?');
|
||||
/// assert_eq!(parser.parse("!").map(|x| x.0), Ok('!'));
|
||||
/// assert_eq!(parser.parse("?").map(|x| x.0), Ok('?'));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn satisfy<Input, P>(predicate: P) -> Satisfy<Input, P>
|
||||
where
|
||||
Input: Stream,
|
||||
P: FnMut(Input::Token) -> bool,
|
||||
{
|
||||
Satisfy {
|
||||
predicate,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct SatisfyMap<Input, P> {
|
||||
predicate: P,
|
||||
_marker: PhantomData<Input>,
|
||||
}
|
||||
|
||||
impl<Input, P, R> Parser<Input> for SatisfyMap<Input, P>
|
||||
where
|
||||
Input: Stream,
|
||||
P: FnMut(Input::Token) -> Option<R>,
|
||||
{
|
||||
type Output = R;
|
||||
type PartialState = ();
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Self::Output, Input::Error> {
|
||||
satisfy_impl(input, &mut self.predicate)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses a token and passes it to `predicate`. If `predicate` returns `Some` the parser succeeds
|
||||
/// and returns the value inside the `Option`. If `predicate` returns `None` the parser fails
|
||||
/// without consuming any input.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// #[derive(Debug, PartialEq)]
|
||||
/// enum YesNo {
|
||||
/// Yes,
|
||||
/// No,
|
||||
/// }
|
||||
/// let mut parser = satisfy_map(|c| {
|
||||
/// match c {
|
||||
/// 'Y' => Some(YesNo::Yes),
|
||||
/// 'N' => Some(YesNo::No),
|
||||
/// _ => None,
|
||||
/// }
|
||||
/// });
|
||||
/// assert_eq!(parser.parse("Y").map(|x| x.0), Ok(YesNo::Yes));
|
||||
/// assert!(parser.parse("A").map(|x| x.0).is_err());
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn satisfy_map<Input, P, R>(predicate: P) -> SatisfyMap<Input, P>
|
||||
where
|
||||
Input: Stream,
|
||||
P: FnMut(Input::Token) -> Option<R>,
|
||||
{
|
||||
SatisfyMap {
|
||||
predicate,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Token<Input>
|
||||
where
|
||||
Input: Stream,
|
||||
Input::Token: PartialEq,
|
||||
{
|
||||
c: Input::Token,
|
||||
_marker: PhantomData<Input>,
|
||||
}
|
||||
|
||||
impl<Input> Parser<Input> for Token<Input>
|
||||
where
|
||||
Input: Stream,
|
||||
Input::Token: PartialEq + Clone,
|
||||
{
|
||||
type Output = Input::Token;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Input::Token, Input::Error> {
|
||||
satisfy_impl(input, |c| if c == self.c { Some(c) } else { None })
|
||||
}
|
||||
fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
errors.error.add_expected(error::Token(self.c.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses a character and succeeds if the character is equal to `c`.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let result = token('!')
|
||||
/// .parse("!")
|
||||
/// .map(|x| x.0);
|
||||
/// assert_eq!(result, Ok('!'));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn token<Input>(c: Input::Token) -> Token<Input>
|
||||
where
|
||||
Input: Stream,
|
||||
Input::Token: PartialEq,
|
||||
{
|
||||
Token {
|
||||
c,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Tokens<C, E, T, Input>
|
||||
where
|
||||
Input: Stream,
|
||||
{
|
||||
cmp: C,
|
||||
expected: E,
|
||||
tokens: T,
|
||||
_marker: PhantomData<Input>,
|
||||
}
|
||||
|
||||
impl<Input, C, E, T> Parser<Input> for Tokens<C, E, T, Input>
|
||||
where
|
||||
C: FnMut(T::Item, Input::Token) -> bool,
|
||||
E: for<'s> ErrorInfo<'s, Input::Token, Input::Range>,
|
||||
T: Clone + IntoIterator,
|
||||
Input: Stream,
|
||||
{
|
||||
type Output = T;
|
||||
type PartialState = ();
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<T, Input::Error> {
|
||||
let start = input.position();
|
||||
let mut committed = false;
|
||||
for c in self.tokens.clone() {
|
||||
match crate::stream::uncons(input) {
|
||||
CommitOk(other) | PeekOk(other) => {
|
||||
if !(self.cmp)(c, other.clone()) {
|
||||
return if committed {
|
||||
let mut errors = <Input as StreamOnce>::Error::from_error(
|
||||
start,
|
||||
StreamError::unexpected_token(other),
|
||||
);
|
||||
errors.add_expected(&self.expected);
|
||||
CommitErr(errors)
|
||||
} else {
|
||||
PeekErr(<Input as StreamOnce>::Error::empty(start).into())
|
||||
};
|
||||
}
|
||||
committed = true;
|
||||
}
|
||||
PeekErr(mut error) => {
|
||||
error.error.set_position(start);
|
||||
return if committed {
|
||||
CommitErr(error.error)
|
||||
} else {
|
||||
PeekErr(error)
|
||||
};
|
||||
}
|
||||
CommitErr(mut error) => {
|
||||
error.set_position(start);
|
||||
return CommitErr(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
if committed {
|
||||
CommitOk(self.tokens.clone())
|
||||
} else {
|
||||
PeekOk(self.tokens.clone())
|
||||
}
|
||||
}
|
||||
fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
errors.error.add_expected(&self.expected);
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses multiple tokens.
|
||||
///
|
||||
/// Consumes items from the input and compares them to the values from `tokens` using the
|
||||
/// comparison function `cmp`. Succeeds if all the items from `tokens` are matched in the input
|
||||
/// stream and fails otherwise with `expected` used as part of the error.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::error;
|
||||
/// # fn main() {
|
||||
/// let result = tokens(|l, r| l.eq_ignore_ascii_case(&r), "abc", "abc".chars())
|
||||
/// .parse("AbC")
|
||||
/// .map(|x| x.0.as_str());
|
||||
/// assert_eq!(result, Ok("abc"));
|
||||
/// let result = tokens(
|
||||
/// |&l, r| (if l < r { r - l } else { l - r }) <= 2,
|
||||
/// error::Range(&b"025"[..]),
|
||||
/// &b"025"[..]
|
||||
/// )
|
||||
/// .parse(&b"123"[..])
|
||||
/// .map(|x| x.0);
|
||||
/// assert_eq!(result, Ok(&b"025"[..]));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn tokens<C, E, T, Input>(cmp: C, expected: E, tokens: T) -> Tokens<C, E, T, Input>
|
||||
where
|
||||
C: FnMut(T::Item, Input::Token) -> bool,
|
||||
T: Clone + IntoIterator,
|
||||
Input: Stream,
|
||||
{
|
||||
Tokens {
|
||||
cmp,
|
||||
expected,
|
||||
tokens,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TokensCmp<C, T, Input>
|
||||
where
|
||||
Input: Stream,
|
||||
{
|
||||
cmp: C,
|
||||
tokens: T,
|
||||
_marker: PhantomData<Input>,
|
||||
}
|
||||
|
||||
impl<Input, C, T> Parser<Input> for TokensCmp<C, T, Input>
|
||||
where
|
||||
C: FnMut(T::Item, Input::Token) -> bool,
|
||||
T: Clone + IntoIterator,
|
||||
Input: Stream,
|
||||
{
|
||||
type Output = T;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<T, Input::Error> {
|
||||
let start = input.position();
|
||||
let mut committed = false;
|
||||
for c in self.tokens.clone() {
|
||||
match crate::stream::uncons(input) {
|
||||
CommitOk(other) | PeekOk(other) => {
|
||||
if !(self.cmp)(c, other.clone()) {
|
||||
return if committed {
|
||||
let errors = <Input as StreamOnce>::Error::from_error(
|
||||
start,
|
||||
StreamError::unexpected_token(other),
|
||||
);
|
||||
CommitErr(errors)
|
||||
} else {
|
||||
PeekErr(<Input as StreamOnce>::Error::empty(start).into())
|
||||
};
|
||||
}
|
||||
committed = true;
|
||||
}
|
||||
PeekErr(mut error) => {
|
||||
error.error.set_position(start);
|
||||
return if committed {
|
||||
CommitErr(error.error)
|
||||
} else {
|
||||
PeekErr(error)
|
||||
};
|
||||
}
|
||||
CommitErr(mut error) => {
|
||||
error.set_position(start);
|
||||
return CommitErr(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
if committed {
|
||||
CommitOk(self.tokens.clone())
|
||||
} else {
|
||||
PeekOk(self.tokens.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses multiple tokens.
|
||||
///
|
||||
/// Consumes items from the input and compares them to the values from `tokens` using the
|
||||
/// comparison function `cmp`. Succeeds if all the items from `tokens` are matched in the input
|
||||
/// stream and fails otherwise.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// # #[allow(deprecated)]
|
||||
/// # use std::ascii::AsciiExt;
|
||||
/// let result = tokens_cmp("abc".chars(), |l, r| l.eq_ignore_ascii_case(&r))
|
||||
/// .parse("AbC")
|
||||
/// .map(|x| x.0.as_str());
|
||||
/// assert_eq!(result, Ok("abc"));
|
||||
/// let result = tokens_cmp(
|
||||
/// &b"025"[..],
|
||||
/// |&l, r| (if l < r { r - l } else { l - r }) <= 2,
|
||||
/// )
|
||||
/// .parse(&b"123"[..])
|
||||
/// .map(|x| x.0);
|
||||
/// assert_eq!(result, Ok(&b"025"[..]));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn tokens_cmp<C, T, I>(tokens: T, cmp: C) -> TokensCmp<C, T, I>
|
||||
where
|
||||
C: FnMut(T::Item, I::Token) -> bool,
|
||||
T: Clone + IntoIterator,
|
||||
I: Stream,
|
||||
{
|
||||
TokensCmp {
|
||||
cmp,
|
||||
tokens,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Position<Input>
|
||||
where
|
||||
Input: Stream,
|
||||
{
|
||||
_marker: PhantomData<Input>,
|
||||
}
|
||||
|
||||
impl<Input> Parser<Input> for Position<Input>
|
||||
where
|
||||
Input: Stream,
|
||||
{
|
||||
type Output = Input::Position;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Input::Position, Input::Error> {
|
||||
PeekOk(input.position())
|
||||
}
|
||||
}
|
||||
|
||||
/// Parser which just returns the current position in the stream.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::stream::position::{self, SourcePosition};
|
||||
/// # fn main() {
|
||||
/// let result = (position(), token('!'), position())
|
||||
/// .parse(position::Stream::new("!"))
|
||||
/// .map(|x| x.0);
|
||||
/// assert_eq!(result, Ok((SourcePosition { line: 1, column: 1 },
|
||||
/// '!',
|
||||
/// SourcePosition { line: 1, column: 2 })));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn position<Input>() -> Position<Input>
|
||||
where
|
||||
Input: Stream,
|
||||
{
|
||||
Position {
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct OneOf<T, Input>
|
||||
where
|
||||
Input: Stream,
|
||||
{
|
||||
tokens: T,
|
||||
_marker: PhantomData<Input>,
|
||||
}
|
||||
|
||||
impl<Input, T> Parser<Input> for OneOf<T, Input>
|
||||
where
|
||||
T: Clone + IntoIterator<Item = Input::Token>,
|
||||
Input: Stream,
|
||||
Input::Token: PartialEq,
|
||||
{
|
||||
type Output = Input::Token;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Input::Token, Input::Error> {
|
||||
satisfy(|c| self.tokens.clone().into_iter().any(|t| t == c)).parse_lazy(input)
|
||||
}
|
||||
|
||||
fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
for expected in self.tokens.clone() {
|
||||
errors.error.add_expected(error::Token(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract one token and succeeds if it is part of `tokens`.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let result = many(one_of("abc".chars()))
|
||||
/// .parse("abd");
|
||||
/// assert_eq!(result, Ok((String::from("ab"), "d")));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn one_of<T, Input>(tokens: T) -> OneOf<T, Input>
|
||||
where
|
||||
T: Clone + IntoIterator,
|
||||
Input: Stream,
|
||||
Input::Token: PartialEq<T::Item>,
|
||||
{
|
||||
OneOf {
|
||||
tokens,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct NoneOf<T, Input>
|
||||
where
|
||||
Input: Stream,
|
||||
{
|
||||
tokens: T,
|
||||
_marker: PhantomData<Input>,
|
||||
}
|
||||
|
||||
impl<Input, T> Parser<Input> for NoneOf<T, Input>
|
||||
where
|
||||
T: Clone + IntoIterator<Item = Input::Token>,
|
||||
Input: Stream,
|
||||
Input::Token: PartialEq,
|
||||
{
|
||||
type Output = Input::Token;
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Input::Token, Input::Error> {
|
||||
satisfy(|c| self.tokens.clone().into_iter().all(|t| t != c)).parse_lazy(input)
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract one token and succeeds if it is not part of `tokens`.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::stream::easy;
|
||||
/// # use combine::stream::position;
|
||||
/// # fn main() {
|
||||
/// let mut parser = many1(none_of(b"abc".iter().cloned()));
|
||||
/// let result = parser.easy_parse(position::Stream::new(&b"xyb"[..]))
|
||||
/// .map(|(output, input)| (output, input.input));
|
||||
/// assert_eq!(result, Ok((b"xy"[..].to_owned(), &b"b"[..])));
|
||||
///
|
||||
/// let result = parser.easy_parse(position::Stream::new(&b"ab"[..]));
|
||||
/// assert_eq!(result, Err(easy::Errors {
|
||||
/// position: 0,
|
||||
/// errors: vec![
|
||||
/// easy::Error::Unexpected(easy::Info::Token(b'a')),
|
||||
/// ]
|
||||
/// }));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn none_of<T, Input>(tokens: T) -> NoneOf<T, Input>
|
||||
where
|
||||
T: Clone + IntoIterator,
|
||||
Input: Stream,
|
||||
Input::Token: PartialEq<T::Item>,
|
||||
{
|
||||
NoneOf {
|
||||
tokens,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Value<Input, T>(T, PhantomData<fn(Input) -> Input>);
|
||||
impl<Input, T> Parser<Input> for Value<Input, T>
|
||||
where
|
||||
Input: Stream,
|
||||
T: Clone,
|
||||
{
|
||||
type Output = T;
|
||||
type PartialState = ();
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, _: &mut Input) -> ParseResult<T, Input::Error> {
|
||||
PeekOk(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
/// Always returns the value `v` without consuming any input.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # fn main() {
|
||||
/// let result = value(42)
|
||||
/// .parse("hello world")
|
||||
/// .map(|x| x.0);
|
||||
/// assert_eq!(result, Ok(42));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn value<Input, T>(v: T) -> Value<Input, T>
|
||||
where
|
||||
Input: Stream,
|
||||
T: Clone,
|
||||
{
|
||||
Value(v, PhantomData)
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Produce<Input, F>(F, PhantomData<fn(Input) -> Input>);
|
||||
impl<Input, F, R> Parser<Input> for Produce<Input, F>
|
||||
where
|
||||
Input: Stream,
|
||||
F: FnMut() -> R,
|
||||
{
|
||||
type Output = R;
|
||||
type PartialState = ();
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, _: &mut Input) -> ParseResult<R, Input::Error> {
|
||||
PeekOk((self.0)())
|
||||
}
|
||||
}
|
||||
|
||||
/// Always returns the value produced by calling `f`.
|
||||
///
|
||||
/// Can be used when `value` is unable to be used for lack of `Clone` implementation on the value.
|
||||
///
|
||||
/// ```
|
||||
/// # use combine::*;
|
||||
/// #[derive(Debug, PartialEq)]
|
||||
/// struct NoClone;
|
||||
/// let result = produce(|| vec![NoClone])
|
||||
/// .parse("hello world")
|
||||
/// .map(|x| x.0);
|
||||
/// assert_eq!(result, Ok(vec![NoClone]));
|
||||
/// ```
|
||||
pub fn produce<Input, F, R>(f: F) -> Produce<Input, F>
|
||||
where
|
||||
Input: Stream,
|
||||
F: FnMut() -> R,
|
||||
{
|
||||
Produce(f, PhantomData)
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Eof<Input>(PhantomData<Input>);
|
||||
impl<Input> Parser<Input> for Eof<Input>
|
||||
where
|
||||
Input: Stream,
|
||||
{
|
||||
type Output = ();
|
||||
type PartialState = ();
|
||||
|
||||
#[inline]
|
||||
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<(), Input::Error> {
|
||||
let before = input.checkpoint();
|
||||
match input.uncons() {
|
||||
Err(ref err) if err.is_unexpected_end_of_input() => PeekOk(()),
|
||||
_ => {
|
||||
ctry!(input.reset(before).committed());
|
||||
PeekErr(<Input as StreamOnce>::Error::empty(input.position()).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
|
||||
errors.error.add_expected("end of input");
|
||||
}
|
||||
}
|
||||
|
||||
/// Succeeds only if the stream is at end of input, fails otherwise.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate combine;
|
||||
/// # use combine::*;
|
||||
/// # use combine::stream::easy;
|
||||
/// # use combine::stream::position::{self, SourcePosition};
|
||||
/// # fn main() {
|
||||
/// let mut parser = eof();
|
||||
/// assert_eq!(parser.easy_parse(position::Stream::new("")), Ok(((), position::Stream::new(""))));
|
||||
/// assert_eq!(parser.easy_parse(position::Stream::new("x")), Err(easy::Errors {
|
||||
/// position: SourcePosition::default(),
|
||||
/// errors: vec![
|
||||
/// easy::Error::Unexpected('x'.into()),
|
||||
/// easy::Error::Expected("end of input".into())
|
||||
/// ]
|
||||
/// }));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn eof<Input>() -> Eof<Input>
|
||||
where
|
||||
Input: Stream,
|
||||
{
|
||||
Eof(PhantomData)
|
||||
}
|
||||
Reference in New Issue
Block a user