in_range

Function in_range 

Source
pub fn in_range<'a, Toks, T, A, P, R>(
    p: P,
    range: R,
) -> impl Parser<'a, Toks, T, Vec<A>>
where Toks: Tokens<T> + 'a, T: Clone + Debug, A: 'a, P: Parser<'a, Toks, T, A> + 'a, R: RangeBounds<usize> + 'a,
Expand description

Parses with the given parser an amount of times based on the provided range.

This parser will attempt to use p to parse a number of elements that would fit into the provided range. If p parses less than the lower limit of the range, this parser will fail. If p parses up to the upper limit of the range, this parser will stop parsing and return all of the parsed items.

Note: if p has already parsed the minimum number of elements, but then produces an error value created with the ParseError::other function, this parser will not return the already-parsed elements and will fail instead. This is because such an error was caused by factors outside of the parser chain and should not be recovered from. See the documentation for ParseError more information.

Ranges can be specified with range literals, as well as any other type that implements RangeBounds<usize>. As such, it will also act appropriately with range bounds that cannot normally be expressed with range literals, such as ranges that exclude the start value.

WARNING: if p is able to successfully parse things without consuming any input and the upper limit of the range is unbonded, then running this parser may create an infinite loop.

See also: at_least, at_most, exactly, mult, mult1.

ยงExamples

use bad_parsers::{Parser, token};

let p = token('a').in_range(2..4);

assert!(p.parse("b").is_err());
assert!(p.parse("ab").is_err());
assert_eq!(("", vec!['a', 'a']), p.parse("aa").unwrap());
assert_eq!(("", vec!['a', 'a', 'a']), p.parse("aaa").unwrap());
assert_eq!(("a", vec!['a', 'a', 'a']), p.parse("aaaa").unwrap());