forked from kevinmehall/rust-peg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repeats.rs
63 lines (46 loc) · 1.76 KB
/
repeats.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
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
extern crate peg;
peg::parser!( grammar repeats() for str {
rule number() -> i64
= n:$(['0'..='9']+) { n.parse().unwrap() }
pub rule list() -> Vec<i64>
= number() ** ","
rule digit() -> i64
= n:$(['0'..='9']) {n.parse().unwrap() }
pub rule repeat_n() -> Vec<i64>
= digit()*<4>
pub rule repeat_min() -> Vec<i64>
= digit()*<2,>
pub rule repeat_max() -> Vec<i64>
= digit()*<,2>
pub rule repeat_min_max() -> Vec<i64>
= digit()*<2,3>
pub rule repeat_sep_3() -> Vec<i64>
= digit() **<3> ","
pub rule repeat_variable() -> Vec<&'input str>
= (count:digit() s:$(['a'..='z'|'0'..='9']*<{count as usize}>) {s})*
});
use repeats::*;
fn main() {
assert_eq!(list("5"), Ok(vec![5]));
assert_eq!(list("1,2,3,4"), Ok(vec![1,2,3,4]));
assert!(repeat_n("123").is_err());
assert_eq!(repeat_n("1234"), Ok(vec![1,2,3,4]));
assert!(repeat_n("12345").is_err());
assert!(repeat_min("").is_err());
assert!(repeat_min("1").is_err());
assert_eq!(repeat_min("12"), Ok(vec![1,2]));
assert_eq!(repeat_min("123"), Ok(vec![1,2,3]));
assert_eq!(repeat_max(""), Ok(vec![]));
assert_eq!(repeat_max("1"), Ok(vec![1]));
assert_eq!(repeat_max("12"), Ok(vec![1,2]));
assert!(repeat_max("123").is_err());
assert!(repeat_min_max("").is_err());
assert!(repeat_min_max("1").is_err());
assert_eq!(repeat_min_max("12"), Ok(vec![1,2]));
assert_eq!(repeat_min_max("123"), Ok(vec![1,2,3]));
assert!(repeat_min_max("1234").is_err());
assert!(repeat_sep_3("1,2").is_err());
assert!(repeat_sep_3("1,2,3,4").is_err());
assert_eq!(repeat_sep_3("1,2,3"), Ok(vec![1,2,3]));
assert_eq!(repeat_variable("1a3abc222"), Ok(vec!["a", "abc", "22"]));
}