| 1 |
extern crate peg; |
| 2 |
|
| 3 |
peg::parser!( grammar lookahead() for str { |
| 4 |
pub rule consonants() |
| 5 |
= (!['a'|'e'|'i'|'o'|'u']['a'..='z'])+ |
| 6 |
|
| 7 |
pub rule neg_lookahead_err() = !(['a']['b']) ['a']['x'] |
| 8 |
|
| 9 |
pub rule lookahead_result() -> &'input str |
| 10 |
= v:&($(['a'..='c']*)) "abcd" { v } |
| 11 |
}); |
| 12 |
|
| 13 |
fn main() { |
| 14 |
|
| 15 |
assert!(lookahead::consonants("qwrty").is_ok()); |
| 16 |
assert!(lookahead::consonants("rust").is_err()); |
| 17 |
|
| 18 |
|
| 19 |
let err = lookahead::neg_lookahead_err("ac").err().unwrap(); |
| 20 |
assert_eq!(err.expected.tokens().count(), 1, "expected set includes: {}", err.expected); |
| 21 |
assert_eq!(err.location.offset, 1); |
| 22 |
|
| 23 |
|
| 24 |
assert_eq!(lookahead::lookahead_result("abcd"), Ok("abc")); |
| 25 |
assert!(lookahead::lookahead_result("abc").is_err()); |
| 26 |
} |