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
69
70
71
72
73
use nom::{IResult, Needed, ErrorKind};
named!(pub string<&str, &str>, recognize!(delimited!(
tag!("\""),
take_until!("\""),
tag!("\"")
)));
fn simple(input: &str) -> IResult<&str, &str> {
let mut iter = input.char_indices();
if let Some((_, c)) = iter.next() {
if !(c.is_alphabetic() || c == '_' || c == '.') {
return IResult::Error(ErrorKind::Char);
}
} else {
return IResult::Incomplete(Needed::Size(1));
}
for (i, c) in iter {
if !(c.is_alphanumeric() || c == '_' || c == '.' || c == '-') {
return IResult::Done(&input[i..], &input[..i]);
}
}
IResult::Done(&input[input.len()..], &input[..])
}
named!(pub symbol<&str, &str>, alt!(
string | simple
));
fn is_pattern(c: char) -> bool {
c.is_alphanumeric() || "_.$/\\~=+[]*?-!<>^:".contains(c)
}
named!(simple_pattern<&str, &str>, take_while1!(
is_pattern
));
named!(pub pattern<&str, &str>, alt!(
string | simple_pattern
));
#[cfg(test)]
mod tests {
use idents::*;
#[test]
fn test_symbol() {
assert_done!(symbol(".0"), ".0");
assert_done!(symbol(".text"), ".text");
assert_done!(symbol("a-b"), "a-b");
assert_done!(
symbol("\"spaces are ok, just quote the identifier\""),
"\"spaces are ok, just quote the identifier\""
);
}
#[test]
fn test_pattern() {
assert_done!(pattern("0"), "0");
assert_done!(pattern(".text"), ".text");
assert_done!(pattern("hello*.o"), "hello*.o");
assert_done!(
pattern("\"spaces are ok, just quote the identifier\""),
"\"spaces are ok, just quote the identifier\""
);
assert_done!(
pattern("this+is-another*crazy[example]"),
"this+is-another*crazy[example]"
);
}
}