forked from kevinmehall/rust-peg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pos_neg_assert.rs
26 lines (20 loc) · 884 Bytes
/
pos_neg_assert.rs
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
extern crate peg;
peg::parser!( grammar lookahead() for str {
pub rule consonants()
= (!['a'|'e'|'i'|'o'|'u']['a'..='z'])+
pub rule neg_lookahead_err() = !(['a']['b']) ['a']['x']
pub rule lookahead_result() -> &'input str
= v:&($(['a'..='c']*)) "abcd" { v }
});
fn main() {
// negative lookahead
assert!(lookahead::consonants("qwrty").is_ok());
assert!(lookahead::consonants("rust").is_err());
// expected characters in negative lookahead should not be reported in parse error messages
let err = lookahead::neg_lookahead_err("ac").err().unwrap();
assert_eq!(err.expected.tokens().count(), 1, "expected set includes: {}", err.expected);
assert_eq!(err.location.offset, 1);
// positive lookahead
assert_eq!(lookahead::lookahead_result("abcd"), Ok("abc"));
assert!(lookahead::lookahead_result("abc").is_err());
}