-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy patherrors.rs
68 lines (52 loc) · 2.01 KB
/
errors.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
64
65
66
67
68
extern crate peg;
peg::parser!{ grammar parser() for str {
pub rule one_letter() = ['a'..='z']
pub rule parse() -> usize
= v:( "a" / "\n" )* { v.len() }
pub rule error_pos() = ("a" / "\n" / "\r")*
pub rule q() = (quiet!{
("a" / "b" / "c") ("1" / "2")
} / expected!("letter followed by number"))+
pub rule var(s: &'static str) = expected!(s)
}}
fn main() {
// errors at eof
assert_eq!(parser::one_letter("t"), Ok(()));
let err = parser::one_letter("tt").unwrap_err();
assert_eq!(err.location.line, 1);
assert_eq!(err.location.column, 2);
assert_eq!(err.location.offset, 1);
assert_eq!(format!("{}", err.expected), "EOF");
// expected character set
let err = parser::parse(r#"
aaaa
aaaaaa
aaaabaaaa
"#).unwrap_err();
assert_eq!(err.location.line, 4);
assert_eq!(err.location.column, 5);
assert_eq!(err.location.offset, 17);
assert_eq!(format!("{}", err.expected), r#"one of "\n", "a", EOF"#);
// error position reporting
let err = parser::error_pos("aab\n").unwrap_err();
assert_eq!(err.location.line, 1);
assert_eq!(err.location.column, 3);
assert_eq!(err.location.offset, 2);
assert_eq!(err.expected.to_string(), r#"one of "\n", "\r", "a", EOF"#);
let err = parser::error_pos("aa\naaaa\nbaaa\n").unwrap_err();
assert_eq!(err.location.line, 3);
assert_eq!(err.location.column, 1);
let err = parser::error_pos("aa\naaaa\naaab\naa").unwrap_err();
assert_eq!(err.location.line, 3);
assert_eq!(err.location.column, 4);
let err = parser::error_pos("aa\r\naaaa\r\naaab\r\naa").unwrap_err();
assert_eq!(err.location.line, 3);
assert_eq!(err.location.column, 4);
parser::q("a1").unwrap();
parser::q("a1b2").unwrap();
let err = parser::q("a1bb").unwrap_err();
assert_eq!(err.location.offset, 2);
assert_eq!(err.expected.to_string(), "one of EOF, letter followed by number");
let err = parser::var("", "asdf").unwrap_err();
assert_eq!(err.expected.to_string(), "asdf");
}