Skip to content

Commit

Permalink
fix: Forward the partial mode through the parser! macro correctly
Browse files Browse the repository at this point in the history
Fixes #168
  • Loading branch information
Marwes committed Jun 30, 2018
1 parent ed12602 commit 5257988
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,14 +549,13 @@ macro_rules! parser {
#[doc(hidden)]
#[macro_export]
macro_rules! combine_parse_partial {
((()) $input:ident $state:ident $parser:block) => {{
((()) $mode:ident $input:ident $state:ident $parser:block) => {{
let _ = $state;
let ref mut state = Default::default();
$parser.parse_partial($input, state)
$parser.parse_mode($mode, $input, state)
}};
(($ignored:ty) $input:ident $state:ident $parser:block) => {

$parser.parse_partial($input, $state)
(($ignored:ty) $mode:ident $input:ident $state:ident $parser:block) => {
$parser.parse_mode($mode, $input, $state)
};
}

Expand Down Expand Up @@ -605,15 +604,18 @@ macro_rules! combine_parser_impl {
type Output = $output_type;
type PartialState = $($partial_state)*;

parse_mode!();
#[inline]
fn parse_partial(
fn parse_mode_impl<M>(
&mut self,
mode: M,
input: &mut Self::Input,
state: &mut Self::PartialState,
) -> $crate::error::ConsumedResult<$output_type, $input_type>
where M: $crate::parser::ParseMode
{
let $type_name { $( $arg: ref mut $arg,)* __marker: _ } = *self;
combine_parse_partial!(($($partial_state)*) input state $parser)
combine_parse_partial!(($($partial_state)*) mode input state $parser)
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ macro_rules! parse_mode {
&mut self,
input: &mut Self::Input,
state: &mut Self::PartialState,
) -> ConsumedResult<Self::Output, Self::Input> {
) -> $crate::error::ConsumedResult<Self::Output, Self::Input> {
self.parse_mode($crate::parser::PartialMode::default(), input, state)
}

Expand All @@ -68,7 +68,7 @@ macro_rules! parse_mode {
&mut self,
input: &mut Self::Input,
state: &mut Self::PartialState,
) -> ConsumedResult<Self::Output, Self::Input> {
) -> $crate::error::ConsumedResult<Self::Output, Self::Input> {
self.parse_mode($crate::parser::FirstMode, input, state)
}
}
Expand Down
14 changes: 11 additions & 3 deletions tests/parser.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
extern crate combine;

use combine::Parser;
use combine::parser::char::{digit, letter};
use combine::parser::choice::{choice, optional};
use combine::parser::combinator::{no_partial, not_followed_by, try};
use combine::parser::error::unexpected;
use combine::parser::item::{any, eof, token, value, Token};
use combine::parser::range::range;
use combine::parser::repeat::{count_min_max, sep_by, take_until, sep_end_by1};
use combine::parser::repeat::{count_min_max, sep_by, sep_end_by1, skip_until, take_until};
use combine::Parser;

#[test]
fn choice_empty() {
Expand Down Expand Up @@ -43,10 +43,10 @@ fn not_followed_by_does_not_consume_any_input() {
#[cfg(feature = "std")]
mod tests_std {
use super::*;
use combine::Parser;
use combine::parser::char::{char, digit, letter};
use combine::stream::easy::{Error, Errors, ParseError};
use combine::stream::state::{SourcePosition, State};
use combine::Parser;

#[derive(Clone, PartialEq, Debug)]
struct CloneOnly {
Expand Down Expand Up @@ -391,4 +391,12 @@ mod tests_std {
Ok((String::from("aa"), "ab"))
);
}

#[test]
fn parser_macro_must_impl_parse_mode_issue_168() {
assert_eq!(
skip_until(try((char('a'), char('b')))).parse("aaab"),
Ok(((), "ab"))
);
}
}

0 comments on commit 5257988

Please sign in to comment.