Skip to content

Commit

Permalink
Merge pull request #375 from epage/range
Browse files Browse the repository at this point in the history
feat(token): Add take_till
  • Loading branch information
epage authored Dec 4, 2023
2 parents 109a7a2 + 94c6df0 commit 397fa41
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 61 deletions.
38 changes: 19 additions & 19 deletions assets/trace.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 26 additions & 6 deletions benches/contains_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use criterion::black_box;
use winnow::combinator::alt;
use winnow::combinator::repeat;
use winnow::prelude::*;
use winnow::token::take_till1;
use winnow::token::take_till;
use winnow::token::take_while;

fn contains_token(c: &mut criterion::Criterion) {
Expand Down Expand Up @@ -52,17 +52,29 @@ fn contains_token(c: &mut criterion::Criterion) {

fn parser_slice(input: &mut &str) -> PResult<usize> {
let contains = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'][..];
repeat(0.., alt((take_while(1.., contains), take_till1(contains)))).parse_next(input)
repeat(
0..,
alt((take_while(1.., contains), take_till(1.., contains))),
)
.parse_next(input)
}

fn parser_array(input: &mut &str) -> PResult<usize> {
let contains = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
repeat(0.., alt((take_while(1.., contains), take_till1(contains)))).parse_next(input)
repeat(
0..,
alt((take_while(1.., contains), take_till(1.., contains))),
)
.parse_next(input)
}

fn parser_tuple(input: &mut &str) -> PResult<usize> {
let contains = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
repeat(0.., alt((take_while(1.., contains), take_till1(contains)))).parse_next(input)
repeat(
0..,
alt((take_while(1.., contains), take_till(1.., contains))),
)
.parse_next(input)
}

fn parser_closure_or(input: &mut &str) -> PResult<usize> {
Expand All @@ -78,12 +90,20 @@ fn parser_closure_or(input: &mut &str) -> PResult<usize> {
|| c == '8'
|| c == '9'
};
repeat(0.., alt((take_while(1.., contains), take_till1(contains)))).parse_next(input)
repeat(
0..,
alt((take_while(1.., contains), take_till(1.., contains))),
)
.parse_next(input)
}

fn parser_closure_matches(input: &mut &str) -> PResult<usize> {
let contains = |c: char| matches!(c, '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9');
repeat(0.., alt((take_while(1.., contains), take_till1(contains)))).parse_next(input)
repeat(
0..,
alt((take_while(1.., contains), take_till(1.., contains))),
)
.parse_next(input)
}

const CONTIGUOUS: &str = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
Expand Down
4 changes: 2 additions & 2 deletions examples/ini/parser_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use winnow::{
combinator::opt,
combinator::repeat,
combinator::{delimited, terminated},
token::{take_till0, take_while},
token::{take_till, take_while},
};

pub type Stream<'i> = &'i str;
Expand Down Expand Up @@ -36,7 +36,7 @@ fn keys_and_values<'s>(input: &mut Stream<'s>) -> PResult<HashMap<&'s str, &'s s
fn key_value<'s>(i: &mut Stream<'s>) -> PResult<(&'s str, &'s str)> {
let key = alphanumeric.parse_next(i)?;
let _ = (opt(space), "=", opt(space)).parse_next(i)?;
let val = take_till0(is_line_ending_or_comment).parse_next(i)?;
let val = take_till(0.., is_line_ending_or_comment).parse_next(i)?;
let _ = opt(space).parse_next(i)?;
let _ = opt((";", not_line_ending)).parse_next(i)?;
let _ = opt(space_or_line_ending).parse_next(i)?;
Expand Down
8 changes: 4 additions & 4 deletions examples/string/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use winnow::combinator::fold_repeat;
use winnow::combinator::{delimited, preceded};
use winnow::error::{FromExternalError, ParserError};
use winnow::prelude::*;
use winnow::token::{take_till1, take_while};
use winnow::token::{take_till, take_while};

/// Parse a string. Use a loop of `parse_fragment` and push all of the fragments
/// into an output string.
Expand Down Expand Up @@ -78,13 +78,13 @@ where

/// Parse a non-empty block of text that doesn't include \ or "
fn parse_literal<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PResult<&'a str, E> {
// `take_till1` parses a string of 0 or more characters that aren't one of the
// `take_till` parses a string of 0 or more characters that aren't one of the
// given characters.
let not_quote_slash = take_till1(['"', '\\']);
let not_quote_slash = take_till(1.., ['"', '\\']);

// `verify` runs a parser, then runs a verification function on the output of
// the parser. The verification function accepts the output only if it
// returns true. In this case, we want to ensure that the output of take_till1
// returns true. In this case, we want to ensure that the output of take_till
// is non-empty.
not_quote_slash
.verify(|s: &str| !s.is_empty())
Expand Down
4 changes: 2 additions & 2 deletions src/ascii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::error::{ErrMode, ErrorKind, Needed};
use crate::stream::{AsBStr, AsChar, ParseSlice, Stream, StreamIsPartial};
use crate::stream::{Compare, CompareResult};
use crate::token::one_of;
use crate::token::take_till0;
use crate::token::take_till;
use crate::token::take_while;
use crate::trace::trace;
use crate::PResult;
Expand Down Expand Up @@ -144,7 +144,7 @@ where
I: Compare<&'static str>,
<I as Stream>::Token: AsChar + Clone,
{
let res = take_till0(('\r', '\n')).parse_next(input)?;
let res = take_till(0.., ('\r', '\n')).parse_next(input)?;
if input.compare("\r") == CompareResult::Ok {
let comp = input.compare("\r\n");
match comp {
Expand Down
Loading

0 comments on commit 397fa41

Please sign in to comment.