| 1 | peg::parser!( grammar test() for str { |
| 2 | pub rule alphanumeric() = ['a'..='z' | 'A'..='Z' | '0'..='9']* |
| 3 | pub rule inverted_pat() -> &'input str = "(" s:$([^')']*) ")" {s} |
| 4 | |
| 5 | pub rule capture() -> char = ['a'..='z'] |
| 6 | pub rule capture2() -> (char, char) = a:['a'..='z'] b:['0'..='9'] { (a, b) } |
| 7 | |
| 8 | pub rule open_range() -> char = ['a'..] |
| 9 | |
| 10 | pub rule if_guard() -> char = [x if x.is_ascii_digit()] |
| 11 | }); |
| 12 | |
| 13 | fn main() { |
| 14 | assert!(test::alphanumeric("azAZ09").is_ok()); |
| 15 | assert!(test::alphanumeric("@").is_err()); |
| 16 | |
| 17 | assert_eq!(test::inverted_pat("(asdf)"), Ok("asdf")); |
| 18 | |
| 19 | assert_eq!(test::capture("x"), Ok('x')); |
| 20 | assert_eq!(test::capture2("a1"), Ok(('a', '1'))); |
| 21 | |
| 22 | assert_eq!(test::if_guard("1"), Ok('1')); |
| 23 | assert!(test::if_guard("a").is_err()); |
| 24 | |
| 25 | } |
| 26 |