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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
use pear::{macros::*, parsers::*};
type Input<'a> = pear::input::Pear<pear::input::Text<'a>>;
type Result<'a, T> = pear::input::Result<T, Input<'a>>;
#[parser(rewind, peek)]
fn ab<'a>(input: &mut Input<'a>) -> Result<'a, ()> {
eat('a')?;
eat('b')?;
eof()?;
}
#[parser(rewind, peek)]
fn abc<'a>(input: &mut Input<'a>) -> Result<'a, ()> {
eat('a')?;
eat('b')?;
eat('c')?;
eof()?;
}
#[parser(rewind, peek)]
fn abcd<'a>(input: &mut Input<'a>) -> Result<'a, ()> {
eat('a')?;
eat('b')?;
eat('c')?;
eat('d')?;
eof()?;
}
#[parser]
fn combo<'a>(input: &mut Input<'a>) -> Result<'a, &'a str> {
switch! {
ab() => eat_slice("ab")?,
abc() => eat_slice("abc")?,
abcd() => eat_slice("abcd")?,
_ => parse_error!("not ab, abc, or abcd")?
}
}
#[test]
fn test_peeking_ab() {
let result = parse!(combo: Input::new("ab")).unwrap();
assert_eq!(result, "ab")
}
#[test]
fn test_peeking_abc() {
let result = parse!(combo: Input::new("abc")).unwrap();
assert_eq!(result, "abc")
}
#[test]
fn test_peeking_abcd() {
let result = parse!(combo: Input::new("abcd")).unwrap();
assert_eq!(result, "abcd")
}
#[test]
fn test_peeking_fail() {
let result = parse!(combo: Input::new("a"));
assert!(result.is_err());
let result = parse!(combo: Input::new("abcdef"));
assert!(result.is_err());
}
|