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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use super::{combinator::RawParseResult, reserved::is_reserved};
use nom::{branch::*, character::complete::*, multi::*, sequence::*, Parser};
pub fn letter(input: &str) -> RawParseResult<char> {
satisfy(|c| matches!(c, 'A'..='Z' | 'a'..='z')).parse(input)
}
pub fn digit(input: &str) -> RawParseResult<char> {
satisfy(|c| matches!(c, '0'..='9')).parse(input)
}
pub fn hex_digit(input: &str) -> RawParseResult<u8> {
let hex_letter = satisfy(|c| matches!(c, 'A'..='Z' | 'a'..='f'));
alt((digit, hex_letter))
.map(|c| c.to_digit(16).unwrap() as u8)
.parse(input)
}
pub fn octet(input: &str) -> RawParseResult<u8> {
tuple((hex_digit, hex_digit))
.map(|(u, l)| {
assert!(u < 16);
assert!(l < 16);
u * 16 + l
})
.parse(input)
}
pub fn encoded_character(input: &str) -> RawParseResult<[u8; 4]> {
tuple((octet, octet, octet, octet))
.map(|(a, b, c, d)| [a, b, c, d])
.parse(input)
}
pub fn encoded_string_literal(input: &str) -> RawParseResult<String> {
tuple((char('"'), many1(encoded_character), char('"')))
.map(|(_openq, chars, _closeq)| {
let raw_chars: Vec<u8> = chars.iter().flat_map(|c| c.iter()).cloned().collect();
String::from_utf8(raw_chars).expect("non UTF8 input")
})
.parse(input)
}
pub fn simple_string_literal(input: &str) -> RawParseResult<String> {
tuple((char('\''), many0(none_of("'")), char('\'')))
.map(|(_open, chars, _close)| chars.into_iter().collect())
.parse(input)
}
pub fn simple_id(input: &str) -> RawParseResult<String> {
if let Ok((input, id)) = tuple((letter, many0(alt((letter, digit, char('_'))))))
.map(|(head, tail)| format!("{}{}", head, tail.into_iter().collect::<String>()))
.parse(input)
{
if is_reserved(id.as_str()) {
Err(nom::Err::Error(nom::error::VerboseError {
errors: Vec::new(),
}))
} else {
Ok((input, id))
}
} else {
Err(nom::Err::Error(nom::error::VerboseError {
errors: Vec::new(),
}))
}
}
#[cfg(test)]
mod tests {
use nom::Finish;
#[test]
fn letter() {
let (residual, l) = super::letter("h").finish().unwrap();
assert_eq!(l, 'h');
assert_eq!(residual, "");
let (residual, l) = super::letter("abc").finish().unwrap();
assert_eq!(l, 'a');
assert_eq!(residual, "bc");
let (residual, l) = super::letter("H").finish().unwrap();
assert_eq!(l, 'H');
assert_eq!(residual, "");
let (residual, l) = super::letter("Hi").finish().unwrap();
assert_eq!(l, 'H');
assert_eq!(residual, "i");
assert!(super::letter("2").finish().is_err());
assert!(super::letter("\\").finish().is_err());
}
#[test]
fn digit() {
let (residual, l) = super::digit("123").finish().unwrap();
assert_eq!(l, '1');
assert_eq!(residual, "23");
assert!(super::digit("h").finish().is_err());
}
#[test]
fn hex_digit() {
let (residual, l) = super::hex_digit("a23").finish().unwrap();
assert_eq!(l, 10);
assert_eq!(residual, "23");
let (residual, l) = super::hex_digit("F23").finish().unwrap();
assert_eq!(l, 15);
assert_eq!(residual, "23");
assert!(super::hex_digit("x").finish().is_err());
}
#[test]
fn encoded_character() {
let (residual, l) = super::encoded_character("a0b1c2d3").finish().unwrap();
assert_eq!(l, [0xa0, 0xb1, 0xc2, 0xd3]);
assert_eq!(residual, "");
}
#[test]
fn simple_id_valid() {
let (residual, id) = super::simple_id("h").finish().unwrap();
assert_eq!(id, "h");
assert_eq!(residual, "");
let (residual, id) = super::simple_id("homhom").finish().unwrap();
assert_eq!(id, "homhom");
assert_eq!(residual, "");
let (residual, id) = super::simple_id("homHom").finish().unwrap();
assert_eq!(id, "homHom");
assert_eq!(residual, "");
let (residual, id) = super::simple_id("ho_mhom").finish().unwrap();
assert_eq!(id, "ho_mhom");
assert_eq!(residual, "");
let (residual, id) = super::simple_id("h10o_1mh2om").finish().unwrap();
assert_eq!(id, "h10o_1mh2om");
assert_eq!(residual, "");
}
#[test]
fn simple_id_invalid() {
assert!(super::simple_id("_homhom").finish().is_err());
assert!(super::simple_id("1homhom").finish().is_err());
assert!(super::simple_id("").finish().is_err());
assert!(super::simple_id("end").finish().is_err());
assert!(super::simple_id("end_entity").finish().is_err());
}
}