#![cfg_attr(
feature = "cargo-clippy",
allow(inline_always, type_complexity, too_many_arguments)
)]
#![cfg_attr(feature = "cargo-clippy", allow(clippy_lint))]
#![cfg_attr(not(feature = "std"), no_std)]
#[doc(inline)]
pub use crate::error::{ParseError, ParseResult, StdParseResult};
#[cfg(feature = "std")]
#[doc(inline)]
pub use crate::parser::EasyParser;
#[doc(inline)]
pub use crate::parser::Parser;
#[doc(inline)]
pub use crate::stream::{Positioned, RangeStream, RangeStreamOnce, Stream, StreamOnce};
#[doc(inline)]
pub use crate::parser::{
choice::optional,
combinator::{attempt, look_ahead, not_followed_by},
error::{unexpected, unexpected_any},
function::parser,
repeat::{
chainl1, chainr1, count, count_min_max, many, many1, sep_by, sep_by1, sep_end_by,
sep_end_by1, skip_count, skip_count_min_max, skip_many, skip_many1,
},
sequence::between,
token::{
any, eof, none_of, one_of, position, produce, satisfy, satisfy_map, token, tokens, value,
},
};
#[doc(inline)]
pub use crate::parser::choice::choice;
#[doc(inline)]
pub use crate::parser::combinator::from_str;
#[doc(inline)]
pub use crate::parser::token::tokens_cmp;
#[macro_export]
macro_rules! parser {
(
type PartialState = $partial_state: ty;
$(#[$attr:meta])*
$fn_vis: vis fn $name: ident [$($type_params: tt)*]( $($arg: ident : $arg_type: ty),*)
($input_type: ty) -> $output_type: ty
where [$($where_clause: tt)*]
$parser: block
) => {
$crate::combine_parser_impl!{
#[allow(non_camel_case_types)]
#[doc(hidden)]
$fn_vis struct $name;
(type PartialState = ($partial_state);)
$(#[$attr])*
$fn_vis fn $name [$($type_params)*]($($arg : $arg_type),*)($input_type) -> $output_type
where [$($where_clause)*]
$parser
}
};
(
$(#[$derive:meta])*
$struct_vis: vis struct $type_name: ident;
type PartialState = $partial_state: ty;
$(#[$attr:meta])*
$fn_vis: vis fn $name: ident [$($type_params: tt)*]( $($arg: ident : $arg_type: ty),* )
($input_type: ty) -> $output_type: ty
where [$($where_clause: tt)*]
$parser: block
) => {
$crate::combine_parser_impl!{
$(#[$derive])*
$struct_vis struct $type_name;
(type PartialState = ($partial_state);)
$(#[$attr])*
$fn_vis fn $name [$($type_params)*]($($arg : $arg_type),*)($input_type) -> $output_type
where [$($where_clause)*]
$parser
}
};
(
$(#[$attr:meta])*
$fn_vis: vis fn $name: ident [$($type_params: tt)*]( $($arg: ident : $arg_type: ty),*)
($input_type: ty) -> $output_type: ty
where [$($where_clause: tt)*]
$parser: block
) => {
$crate::combine_parser_impl!{
#[allow(non_camel_case_types)]
#[doc(hidden)]
$fn_vis struct $name;
(type PartialState = (());)
$(#[$attr])*
$fn_vis fn $name [$($type_params)*]($($arg : $arg_type),*)($input_type) -> $output_type
where [$($where_clause)*]
$parser
}
};
(
$(#[$derive:meta])*
$struct_vis: vis struct $type_name: ident;
$(#[$attr:meta])*
$fn_vis: vis fn $name: ident [$($type_params: tt)*]( $($arg: ident : $arg_type: ty),* )
($input_type: ty) -> $output_type: ty
where [$($where_clause: tt)*]
$parser: block
) => {
$crate::combine_parser_impl!{
$(#[$derive])*
$struct_vis struct $type_name;
(type PartialState = (());)
$(#[$attr])*
$fn_vis fn $name [$($type_params)*]($($arg : $arg_type),*)($input_type) -> $output_type
where [$($where_clause)*]
$parser
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! combine_parse_partial {
((()) $mode:ident $input:ident $state:ident $parser:block) => {{
let _ = $state;
let mut state = Default::default();
let state = &mut state;
$parser.parse_mode($mode, $input, state)
}};
(($ignored:ty) $mode:ident $input:ident $state:ident $parser:block) => {
$parser.parse_mode($mode, $input, $state)
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! combine_parser_impl {
(
$(#[$derive:meta])*
$struct_vis: vis struct $type_name: ident;
(type PartialState = ($($partial_state: tt)*);)
$(#[$attr:meta])*
$fn_vis: vis fn $name: ident [$($type_params: tt)*]( $($arg: ident : $arg_type: ty),*)
($input_type: ty) -> $output_type: ty
where [$($where_clause: tt)*]
$parser: block
) => {
$(#[$derive])*
$struct_vis struct $type_name<$($type_params)*>
where <$input_type as $crate::stream::StreamOnce>::Error:
$crate::error::ParseError<
<$input_type as $crate::stream::StreamOnce>::Token,
<$input_type as $crate::stream::StreamOnce>::Range,
<$input_type as $crate::stream::StreamOnce>::Position
>,
$input_type: $crate::stream::Stream,
$($where_clause)*
{
$(pub $arg : $arg_type,)*
__marker: $crate::lib::marker::PhantomData<fn ($input_type) -> $output_type>
}
#[allow(non_shorthand_field_patterns)]
impl<$($type_params)*> $crate::Parser<$input_type> for $type_name<$($type_params)*>
where <$input_type as $crate::stream::StreamOnce>::Error:
$crate::error::ParseError<
<$input_type as $crate::stream::StreamOnce>::Token,
<$input_type as $crate::stream::StreamOnce>::Range,
<$input_type as $crate::stream::StreamOnce>::Position
>,
$input_type: $crate::stream::Stream,
$($where_clause)*
{
type Output = $output_type;
type PartialState = $($partial_state)*;
$crate::parse_mode!($input_type);
#[inline]
fn parse_mode_impl<M>(
&mut self,
mode: M,
input: &mut $input_type,
state: &mut Self::PartialState,
) -> $crate::error::ParseResult<$output_type, <$input_type as $crate::stream::StreamOnce>::Error>
where M: $crate::parser::ParseMode
{
let $type_name { $( $arg: ref mut $arg,)* .. } = *self;
$crate::combine_parse_partial!(($($partial_state)*) mode input state $parser)
}
#[inline]
fn add_error(
&mut self,
errors: &mut $crate::error::Tracked<
<$input_type as $crate::stream::StreamOnce>::Error
>)
{
let $type_name { $( $arg : ref mut $arg,)* .. } = *self;
let mut parser = $parser;
{
let _: &mut dyn $crate::Parser< $input_type, Output = $output_type, PartialState = _> = &mut parser;
}
parser.add_error(errors)
}
fn add_committed_expected_error(
&mut self,
errors: &mut $crate::error::Tracked<
<$input_type as $crate::stream::StreamOnce>::Error
>)
{
let $type_name { $( $arg : ref mut $arg,)* .. } = *self;
let mut parser = $parser;
{
let _: &mut dyn $crate::Parser< $input_type, Output = $output_type, PartialState = _> = &mut parser;
}
parser.add_committed_expected_error(errors)
}
}
$(#[$attr])*
#[inline]
$fn_vis fn $name< $($type_params)* >(
$($arg : $arg_type),*
) -> $type_name<$($type_params)*>
where <$input_type as $crate::stream::StreamOnce>::Error:
$crate::error::ParseError<
<$input_type as $crate::stream::StreamOnce>::Token,
<$input_type as $crate::stream::StreamOnce>::Range,
<$input_type as $crate::stream::StreamOnce>::Position
>,
$input_type: $crate::stream::Stream,
$($where_clause)*
{
$type_name {
$($arg : $arg,)*
__marker: $crate::lib::marker::PhantomData
}
}
};
}
macro_rules! forward_parser {
($input: ty, $method: ident $( $methods: ident)*, $($field: tt)*) => {
forward_parser!($input, $method $($field)+);
forward_parser!($input, $($methods)*, $($field)+);
};
($input: ty, parse_mode $($field: tt)+) => {
#[inline]
fn parse_mode_impl<M>(
&mut self,
mode: M,
input: &mut $input,
state: &mut Self::PartialState,
) -> ParseResult<Self::Output, <$input as $crate::StreamOnce>::Error>
where
M: ParseMode,
{
self.$($field)+.parse_mode(mode, input, state).map(|(a, _)| a)
}
};
($input: ty, parse_lazy $($field: tt)+) => {
fn parse_lazy(
&mut self,
input: &mut $input,
) -> ParseResult<Self::Output, <$input as $crate::StreamOnce>::Error> {
self.$($field)+.parse_lazy(input)
}
};
($input: ty, parse_first $($field: tt)+) => {
fn parse_first(
&mut self,
input: &mut $input,
state: &mut Self::PartialState,
) -> ParseResult<Self::Output, <$input as $crate::StreamOnce>::Error> {
self.$($field)+.parse_first(input, state)
}
};
($input: ty, parse_partial $($field: tt)+) => {
fn parse_partial(
&mut self,
input: &mut $input,
state: &mut Self::PartialState,
) -> ParseResult<Self::Output, <$input as $crate::StreamOnce>::Error> {
self.$($field)+.parse_partial(input, state)
}
};
($input: ty, add_error $($field: tt)+) => {
fn add_error(&mut self, error: &mut $crate::error::Tracked<<$input as $crate::StreamOnce>::Error>) {
self.$($field)+.add_error(error)
}
};
($input: ty, add_committed_expected_error $($field: tt)+) => {
fn add_committed_expected_error(&mut self, error: &mut $crate::error::Tracked<<$input as $crate::StreamOnce>::Error>) {
self.$($field)+.add_committed_expected_error(error)
}
};
($input: ty, parser_count $($field: tt)+) => {
fn parser_count(&self) -> $crate::ErrorOffset {
self.$($field)+.parser_count()
}
};
($input: ty, $field: tt) => {
forward_parser!($input, parse_lazy parse_first parse_partial add_error add_committed_expected_error parser_count, $field);
};
($input: ty, $($field: tt)+) => {
};
}
#[doc(hidden)]
pub mod lib {
#[cfg(not(feature = "std"))]
pub use core::*;
#[cfg(feature = "std")]
pub use std::*;
}
#[cfg(feature = "std")]
#[doc(inline)]
pub use crate::stream::easy;
#[macro_use]
pub mod error;
#[macro_use]
pub mod stream;
#[macro_use]
pub mod parser;
#[doc(hidden)]
#[derive(Clone, PartialOrd, PartialEq, Debug, Copy)]
pub struct ErrorOffset(u8);
#[cfg(test)]
mod tests {
use crate::parser::char::{char, string};
use super::*;
#[test]
fn chainl1_error_consume() {
fn first<T, U>(t: T, _: U) -> T {
t
}
let mut p = chainl1(string("abc"), char(',').map(|_| first));
assert!(p.parse("abc,ab").is_err());
}
#[test]
fn choice_strings() {
let mut fruits = [
attempt(string("Apple")),
attempt(string("Banana")),
attempt(string("Cherry")),
attempt(string("Date")),
attempt(string("Fig")),
attempt(string("Grape")),
];
let mut parser = choice(&mut fruits);
assert_eq!(parser.parse("Apple"), Ok(("Apple", "")));
assert_eq!(parser.parse("Banana"), Ok(("Banana", "")));
assert_eq!(parser.parse("Cherry"), Ok(("Cherry", "")));
assert_eq!(parser.parse("DateABC"), Ok(("Date", "ABC")));
assert_eq!(parser.parse("Fig123"), Ok(("Fig", "123")));
assert_eq!(parser.parse("GrapeApple"), Ok(("Grape", "Apple")));
}
}
#[cfg(all(feature = "std", test))]
mod std_tests {
use crate::{
error::StdParseResult,
parser::char::{alpha_num, char, digit, letter, spaces, string},
stream::{
easy,
position::{self, SourcePosition},
},
};
use super::{easy::Error, error::Commit, stream::IteratorStream, *};
#[test]
fn optional_error_consume() {
let mut p = optional(string("abc"));
let err = p.easy_parse(position::Stream::new("ab")).unwrap_err();
assert_eq!(err.position, SourcePosition { line: 1, column: 1 });
}
fn follow<Input>(input: &mut Input) -> StdParseResult<(), Input>
where
Input: Stream<Token = char, Error = easy::ParseError<Input>>,
Input::Position: Default,
Input::Error: std::fmt::Debug,
Input::Token: PartialEq,
Input::Range: PartialEq,
{
let before = input.checkpoint();
match input.uncons() {
Ok(c) => {
if c.is_alphanumeric() {
input.reset(before).unwrap();
let e = Error::Unexpected(c.into());
Err(Commit::Peek(easy::Errors::new(input.position(), e).into()))
} else {
Ok(((), Commit::Peek(())))
}
}
Err(_) => Ok(((), Commit::Peek(()))),
}
}
fn integer<'a, Input>(input: &mut Input) -> StdParseResult<i64, Input>
where
Input: Stream<Token = char>,
Input::Error: ParseError<Input::Token, Input::Range, Input::Position>,
{
let (s, input) = many1::<String, _, _>(digit())
.expected("integer")
.parse_stream(input)
.into_result()?;
let mut n = 0;
for c in s.chars() {
n = n * 10 + (c as i64 - '0' as i64);
}
Ok((n, input))
}
#[test]
fn test_integer() {
let result = parser(integer).parse("123");
assert_eq!(result, Ok((123i64, "")));
}
#[test]
fn list() {
let mut p = sep_by(parser(integer), char(','));
let result = p.parse("123,4,56");
assert_eq!(result, Ok((vec![123i64, 4, 56], "")));
}
#[test]
fn iterator() {
let result = parser(integer)
.parse(position::Stream::new(IteratorStream::new("123".chars())))
.map(|(i, mut input)| (i, input.uncons().is_err()));
assert_eq!(result, Ok((123i64, true)));
}
#[test]
fn field() {
let word = || many(alpha_num());
let c_decl = (word(), spaces(), char(':'), spaces(), word())
.map(|t| (t.0, t.4))
.parse("x: int");
assert_eq!(c_decl, Ok((("x".to_string(), "int".to_string()), "")));
}
#[test]
fn source_position() {
let source = r"
123
";
let mut parsed_state = position::Stream::with_positioner(source, SourcePosition::new());
let result = (spaces(), parser(integer), spaces())
.map(|t| t.1)
.parse_stream(&mut parsed_state)
.into_result();
let state = Commit::Commit(position::Stream {
positioner: SourcePosition { line: 3, column: 1 },
input: "",
});
assert_eq!(
result.map(|(x, c)| (x, c.map(|_| parsed_state))),
Ok((123i64, state))
);
}
#[derive(Debug, PartialEq)]
pub enum Expr {
Id(String),
Int(i64),
Array(Vec<Expr>),
Plus(Box<Expr>, Box<Expr>),
Times(Box<Expr>, Box<Expr>),
}
parser! {
fn expr[Input]()(Input) -> Expr
where
[Input: Stream<Token = char>,]
{
let word = many1(letter()).expected("identifier");
let integer = parser(integer);
let array = between(char('['), char(']'), sep_by(expr(), char(','))).expected("[");
let paren_expr = between(char('('), char(')'), parser(term)).expected("(");
spaces()
.silent()
.with(
word.map(Expr::Id)
.or(integer.map(Expr::Int))
.or(array.map(Expr::Array))
.or(paren_expr),
)
.skip(spaces().silent())
}
}
#[test]
fn expression_basic() {
let result = sep_by(expr(), char(',')).parse("int, 100, [[], 123]");
let exprs = vec![
Expr::Id("int".to_string()),
Expr::Int(100),
Expr::Array(vec![Expr::Array(vec![]), Expr::Int(123)]),
];
assert_eq!(result, Ok((exprs, "")));
}
#[test]
fn expression_error() {
let input = r"
,123
";
let result = expr().easy_parse(position::Stream::new(input));
let err = easy::Errors {
position: SourcePosition { line: 2, column: 1 },
errors: vec![
Error::Unexpected(','.into()),
Error::Expected("integer".into()),
Error::Expected("identifier".into()),
Error::Expected("[".into()),
Error::Expected("(".into()),
],
};
assert_eq!(result, Err(err));
}
fn term<Input>(input: &mut Input) -> StdParseResult<Expr, Input>
where
Input: Stream<Token = char>,
Input::Error: ParseError<Input::Token, Input::Range, Input::Position>,
{
fn times(l: Expr, r: Expr) -> Expr {
Expr::Times(Box::new(l), Box::new(r))
}
fn plus(l: Expr, r: Expr) -> Expr {
Expr::Plus(Box::new(l), Box::new(r))
}
let mul = char('*').map(|_| times);
let add = char('+').map(|_| plus);
let factor = chainl1(expr(), mul);
chainl1(factor, add).parse_stream(input).into()
}
#[test]
fn operators() {
let input = r"
1 * 2 + 3 * test
";
let (result, _) = parser(term).parse(position::Stream::new(input)).unwrap();
let e1 = Expr::Times(Box::new(Expr::Int(1)), Box::new(Expr::Int(2)));
let e2 = Expr::Times(
Box::new(Expr::Int(3)),
Box::new(Expr::Id("test".to_string())),
);
assert_eq!(result, Expr::Plus(Box::new(e1), Box::new(e2)));
}
#[test]
fn error_position() {
let mut p = string("let")
.skip(parser(follow))
.map(|x| x.to_string())
.or(many1(digit()));
match p.easy_parse(position::Stream::new("le123")) {
Ok(_) => assert!(false),
Err(err) => assert_eq!(err.position, SourcePosition { line: 1, column: 1 }),
}
match p.easy_parse(position::Stream::new("let1")) {
Ok(_) => assert!(false),
Err(err) => assert_eq!(err.position, SourcePosition { line: 1, column: 4 }),
}
}
#[test]
fn sep_by_error_consume() {
let mut p = sep_by::<Vec<_>, _, _, _>(string("abc"), char(','));
let err = p.easy_parse(position::Stream::new("ab,abc")).unwrap_err();
assert_eq!(err.position, SourcePosition { line: 1, column: 1 });
}
#[test]
fn inner_error_consume() {
let mut p = many::<Vec<_>, _, _>(between(char('['), char(']'), digit()));
let result = p.easy_parse(position::Stream::new("[1][2][]"));
assert!(result.is_err(), format!("{:?}", result));
let error = result.map(|x| format!("{:?}", x)).unwrap_err();
assert_eq!(error.position, SourcePosition { line: 1, column: 8 });
}
#[test]
fn infinite_recursion_in_box_parser() {
let _: Result<(Vec<_>, _), _> = (many(Box::new(digit()))).parse("1");
}
#[test]
fn unsized_parser() {
let mut parser: Box<dyn Parser<_, Output = char, PartialState = _>> = Box::new(digit());
let borrow_parser = &mut *parser;
assert_eq!(borrow_parser.parse("1"), Ok(('1', "")));
}
#[test]
fn std_error() {
use std::error::Error as StdError;
use std::fmt;
#[derive(Debug)]
struct Error;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "error")
}
}
impl StdError for Error {
fn description(&self) -> &str {
"error"
}
}
let result: Result<((), _), easy::Errors<char, &str, _>> =
EasyParser::easy_parse(&mut string("abc").and_then(|_| Err(Error)), "abc");
assert!(result.is_err());
let _ = result.map_err(|err| {
let err: Box<dyn StdError> = Box::new(err);
err
});
}
#[test]
fn extract_std_error() {
use std::error::Error as StdError;
use std::fmt;
#[derive(Clone, PartialEq, Debug)]
struct CloneOnly(String);
#[derive(Debug)]
struct DisplayVec<T>(Vec<T>);
#[derive(Debug)]
struct ExtractedError(usize, DisplayVec<Error<CloneOnly, DisplayVec<CloneOnly>>>);
impl StdError for ExtractedError {
fn description(&self) -> &str {
"extracted error"
}
}
impl fmt::Display for CloneOnly {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl<T: fmt::Debug> fmt::Display for DisplayVec<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{:?}]", self.0)
}
}
impl fmt::Display for ExtractedError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "Parse error at {}", self.0)?;
Error::fmt_errors(&(self.1).0, f)
}
}
let input = &[CloneOnly("x".to_string()), CloneOnly("y".to_string())][..];
let result = token(CloneOnly("z".to_string()))
.easy_parse(input)
.map_err(|e| e.map_position(|p| p.translate_position(input)))
.map_err(|e| {
ExtractedError(
e.position,
DisplayVec(
e.errors
.into_iter()
.map(|e| e.map_range(|r| DisplayVec(r.to_owned())))
.collect(),
),
)
});
assert!(result.is_err());
let _ = result.map_err(|err| {
let s = format!("{}", err);
assert!(s.starts_with("Parse error at 0"));
assert!(s.contains("Expected"));
let err: Box<dyn StdError> = Box::new(err);
err
});
}
}