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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use phf::phf_map;
use crate::err::ProcessingResult;
use crate::pattern::TrieNode;
use crate::proc::{Processor, ProcessorRange};
use crate::spec::codepoint::{is_digit, is_hex_digit, is_lower_hex_digit, is_upper_hex_digit};
use crate::ErrorType;
include!(concat!(env!("OUT_DIR"), "/gen_entities.rs"));
fn is_valid_entity_reference_name_char(c: u8) -> bool {
c >= b'0' && c <= b'9' || c >= b'A' && c <= b'Z' || c >= b'a' && c <= b'z'
}
#[derive(Clone, Copy)]
pub enum EntityType {
NonDecodable(ProcessorRange),
Malformed(ProcessorRange),
Ascii(u8),
Named(&'static [u8]),
Numeric(char),
}
impl EntityType {
pub fn is_malformed(&self) -> bool {
if let EntityType::Malformed(_) = self {
true
} else {
false
}
}
}
impl EntityType {
pub fn keep(self, proc: &mut Processor) -> () {
match self {
EntityType::NonDecodable(r) => proc.write_range(r),
EntityType::Malformed(r) => proc.write_range(r),
EntityType::Ascii(c) => proc.write(c),
EntityType::Named(s) => proc.write_slice(s),
EntityType::Numeric(c) => proc.write_utf8(c),
};
}
}
macro_rules! handle_decoded_numeric_code_point {
($proc:ident, $at_least_one_digit:ident, $code_point:ident) => {
if !$at_least_one_digit || !chain!($proc.match_char(b';').discard().matched()) {
return None;
}
return std::char::from_u32($code_point).map(|c| if c.is_ascii() {
EntityType::Ascii(c as u8)
} else {
EntityType::Numeric(c)
});
};
}
fn parse_decimal(proc: &mut Processor) -> Option<EntityType> {
let mut val = 0u32;
let mut at_least_one_digit = false;
for _ in 0..7 {
if let Some(c) = chain!(proc.match_pred(is_digit).discard().maybe_char()) {
at_least_one_digit = true;
val = val * 10 + (c - b'0') as u32;
} else {
break;
}
};
handle_decoded_numeric_code_point!(proc, at_least_one_digit, val);
}
fn parse_hexadecimal(proc: &mut Processor) -> Option<EntityType> {
let mut val = 0u32;
let mut at_least_one_digit = false;
for _ in 0..6 {
if let Some(c) = chain!(proc.match_pred(is_hex_digit).discard().maybe_char()) {
at_least_one_digit = true;
let digit = if is_digit(c) {
c - b'0'
} else if is_upper_hex_digit(c) {
c - b'A' + 10
} else if is_lower_hex_digit(c) {
c - b'a' + 10
} else {
unreachable!();
};
val = val * 16 + digit as u32;
} else {
break;
}
};
handle_decoded_numeric_code_point!(proc, at_least_one_digit, val);
}
fn parse_name(proc: &mut Processor) -> Option<EntityType> {
ENTITY_REFERENCES.get(proc).map(|s| if s.len() == 1 {
EntityType::Ascii(s[0])
} else {
EntityType::Named(s)
})
}
pub fn parse_entity(proc: &mut Processor, decode_left_chevron: bool) -> ProcessingResult<EntityType> {
let checkpoint = proc.checkpoint();
if cfg!(debug_assertions) {
chain!(proc.match_char(b'&').expect().discard());
} else {
proc.skip_expect();
};
let entity_type = if chain!(proc.match_seq(b"#x").discard().matched()) {
parse_hexadecimal(proc)
} else if chain!(proc.match_char(b'#').discard().matched()) {
parse_decimal(proc)
} else if chain!(proc.match_pred(is_valid_entity_reference_name_char).matched()) {
parse_name(proc)
} else {
None
}
.map(|e| match (decode_left_chevron, e) {
(_, EntityType::Ascii(b'&')) | (false, EntityType::Ascii(b'<')) => EntityType::NonDecodable(proc.consumed_range(checkpoint)),
(_, e) => e,
})
.unwrap_or_else(|| EntityType::Malformed(proc.consumed_range(checkpoint)));
if entity_type.is_malformed() && chain!(proc.match_char(b'&').matched()) {
Err(ErrorType::EntityFollowingMalformedEntity)
} else {
Ok(entity_type)
}
}