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
use super::{CharSeq, ParserResult, Parser, Succ, Error, Fail};
pub struct Keyword<'a> {
keyword: &'a str,
}
static ASCII_LETTERS: &'static str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
pub fn keyword<'a>(k: &'a str) -> Keyword {
return Keyword{keyword: k};
}
impl<'a> Parser<String> for Keyword<'a> {
fn parse(&self, cs: &mut CharSeq) -> ParserResult<String> {
let h = cs.enable_hook;
cs.enable_hook = false;
match cs.accept(&self.keyword) {
Succ(r) => {
cs.enable_hook = h;
if ASCII_LETTERS.contains(cs.current()) {
return cs.fail(cs.current().to_string().as_str());
}
if cs.enable_hook {
cs.do_hook();
}
Succ(r)
},
Fail(m, l) => {cs.enable_hook = h; Fail(m, l)},
Error(m, l) => {cs.enable_hook = h; Error(m, l)}
}
}
#[allow(unused_variables)]
fn _parse(&self, cs: &mut CharSeq) -> ParserResult<String> {
return Succ("".to_string());
}
}
#[cfg(test)]
#[allow(unused_variables)]
#[allow(unused_imports)]
mod tests {
use super::keyword;
use super::super::{CharSeq, skip, ParserResult, Parser, Succ, Fail, Error};
#[test]
fn test_keyword() {
let mut cs = CharSeq::new("keyword keywordxxx", "");
let k = keyword("keyword");
let s = skip(" ");
cs.add_hook(&s);
match cs.accept(&k) {
Succ(r) => (),
_ => assert!(false)
}
match cs.accept(&k) {
Fail(m, l) => (),
_ => assert!(false)
}
}
}