time/format_description/parse/
lexer.rs1use core::iter;
4
5use super::{Error, Location, Spanned, SpannedValue, attach_location, unused};
6use crate::format_description::FormatDescriptionVersion;
7
8pub(super) struct Lexed<I>
10where
11 I: Iterator,
12{
13 iter: iter::Peekable<I>,
15}
16
17impl<I> Iterator for Lexed<I>
18where
19 I: Iterator,
20{
21 type Item = I::Item;
22
23 fn next(&mut self) -> Option<Self::Item> {
24 self.iter.next()
25 }
26}
27
28impl<'iter, 'token, I> Lexed<I>
29where
30 'token: 'iter,
31 I: Iterator<Item = Result<Token<'token>, Error>> + 'iter,
32{
33 #[inline]
35 pub(super) fn peek(&mut self) -> Option<&I::Item> {
36 self.iter.peek()
37 }
38
39 #[inline]
41 pub(super) fn next_if_whitespace(&mut self) -> Option<Spanned<&'token [u8]>> {
42 if let Some(&Ok(Token::ComponentPart {
43 kind: ComponentKind::Whitespace,
44 value,
45 })) = self.peek()
46 {
47 self.next(); Some(value)
49 } else {
50 None
51 }
52 }
53
54 #[inline]
56 pub(super) fn next_if_not_whitespace(&mut self) -> Option<Spanned<&'token [u8]>> {
57 if let Some(&Ok(Token::ComponentPart {
58 kind: ComponentKind::NotWhitespace,
59 value,
60 })) = self.peek()
61 {
62 self.next(); Some(value)
64 } else {
65 None
66 }
67 }
68
69 #[inline]
71 pub(super) fn next_if_opening_bracket(&mut self) -> Option<Location> {
72 if let Some(&Ok(Token::Bracket {
73 kind: BracketKind::Opening,
74 location,
75 })) = self.peek()
76 {
77 self.next(); Some(location)
79 } else {
80 None
81 }
82 }
83
84 #[inline]
86 pub(super) fn peek_closing_bracket(&'iter mut self) -> Option<&'iter Location> {
87 if let Some(Ok(Token::Bracket {
88 kind: BracketKind::Closing,
89 location,
90 })) = self.peek()
91 {
92 Some(location)
93 } else {
94 None
95 }
96 }
97
98 #[inline]
100 pub(super) fn next_if_closing_bracket(&mut self) -> Option<Location> {
101 if let Some(&Ok(Token::Bracket {
102 kind: BracketKind::Closing,
103 location,
104 })) = self.peek()
105 {
106 self.next(); Some(location)
108 } else {
109 None
110 }
111 }
112}
113
114pub(super) enum Token<'a> {
116 Literal(Spanned<&'a [u8]>),
118 Bracket {
120 kind: BracketKind,
122 location: Location,
124 },
125 ComponentPart {
127 kind: ComponentKind,
129 value: Spanned<&'a [u8]>,
131 },
132}
133
134pub(super) enum BracketKind {
136 Opening,
138 Closing,
140}
141
142pub(super) enum ComponentKind {
144 Whitespace,
145 NotWhitespace,
146}
147
148#[inline]
158pub(super) fn lex(
159 version: FormatDescriptionVersion,
160 mut input: &[u8],
161) -> Lexed<impl Iterator<Item = Result<Token<'_>, Error>>> {
162 let mut depth: u32 = 0;
163 let mut iter = attach_location(input.iter()).peekable();
164 let mut second_bracket_location = None;
165
166 let iter = iter::from_fn(move || {
167 if version.is_v1() {
169 if let Some(location) = second_bracket_location.take() {
171 return Some(Ok(Token::Bracket {
172 kind: BracketKind::Opening,
173 location,
174 }));
175 }
176 }
177
178 Some(Ok(match iter.next()? {
179 (b'\\', backslash_loc) if version.is_at_least_v2() => {
181 match iter.next() {
182 Some((b'\\' | b'[' | b']', char_loc)) => {
183 let char = &input[1..2];
185 input = &input[2..];
186 if depth == 0 {
187 Token::Literal(char.spanned(backslash_loc.to(char_loc)))
188 } else {
189 Token::ComponentPart {
190 kind: ComponentKind::NotWhitespace,
191 value: char.spanned(backslash_loc.to(char_loc)),
192 }
193 }
194 }
195 Some((_, loc)) => {
196 return Some(Err(Error {
197 _inner: unused(loc.error("invalid escape sequence")),
198 public: crate::error::InvalidFormatDescription::Expected {
199 what: "valid escape sequence",
200 index: loc.byte as usize,
201 },
202 }));
203 }
204 None => {
205 return Some(Err(Error {
206 _inner: unused(backslash_loc.error("unexpected end of input")),
207 public: crate::error::InvalidFormatDescription::Expected {
208 what: "valid escape sequence",
209 index: backslash_loc.byte as usize,
210 },
211 }));
212 }
213 }
214 }
215 (b'[', location) if version.is_v1() => {
217 if let Some((_, second_location)) = iter.next_if(|&(&byte, _)| byte == b'[') {
218 second_bracket_location = Some(second_location);
220 input = &input[2..];
221 } else {
222 depth += 1;
224 input = &input[1..];
225 }
226
227 Token::Bracket {
228 kind: BracketKind::Opening,
229 location,
230 }
231 }
232 (b'[', location) => {
234 depth += 1;
235 input = &input[1..];
236
237 Token::Bracket {
238 kind: BracketKind::Opening,
239 location,
240 }
241 }
242 (b']', location) if depth > 0 => {
244 depth -= 1;
245 input = &input[1..];
246
247 Token::Bracket {
248 kind: BracketKind::Closing,
249 location,
250 }
251 }
252 (_, start_location) if depth == 0 => {
254 let mut bytes = 1;
255 let mut end_location = start_location;
256
257 while let Some((_, location)) = iter.next_if(|&(&byte, _)| {
258 !((version.is_at_least_v2() && byte == b'\\') || byte == b'[')
259 }) {
260 end_location = location;
261 bytes += 1;
262 }
263
264 let value = &input[..bytes];
265 input = &input[bytes..];
266
267 Token::Literal(value.spanned(start_location.to(end_location)))
268 }
269 (byte, start_location) => {
271 let mut bytes = 1;
272 let mut end_location = start_location;
273 let is_whitespace = byte.is_ascii_whitespace();
274
275 while let Some((_, location)) = iter.next_if(|&(byte, _)| {
276 !matches!(byte, b'\\' | b'[' | b']')
277 && is_whitespace == byte.is_ascii_whitespace()
278 }) {
279 end_location = location;
280 bytes += 1;
281 }
282
283 let value = &input[..bytes];
284 input = &input[bytes..];
285
286 Token::ComponentPart {
287 kind: if is_whitespace {
288 ComponentKind::Whitespace
289 } else {
290 ComponentKind::NotWhitespace
291 },
292 value: value.spanned(start_location.to(end_location)),
293 }
294 }
295 }))
296 });
297
298 Lexed {
299 iter: iter.peekable(),
300 }
301}