Skip to main content

time/format_description/parse/
lexer.rs

1//! Lexer for parsing format descriptions.
2
3use core::iter;
4
5use super::{Error, Location, Spanned, SpannedValue, attach_location, unused};
6use crate::format_description::FormatDescriptionVersion;
7
8/// An iterator over the lexed tokens.
9pub(super) struct Lexed<I>
10where
11    I: Iterator,
12{
13    /// The internal iterator.
14    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    /// Peek at the next item in the iterator.
34    #[inline]
35    pub(super) fn peek(&mut self) -> Option<&I::Item> {
36        self.iter.peek()
37    }
38
39    /// Consume the next token if it is whitespace.
40    #[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(); // consume
48            Some(value)
49        } else {
50            None
51        }
52    }
53
54    /// Consume the next token if it is a component item that is not whitespace.
55    #[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(); // consume
63            Some(value)
64        } else {
65            None
66        }
67    }
68
69    /// Consume the next token if it is an opening bracket.
70    #[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(); // consume
78            Some(location)
79        } else {
80            None
81        }
82    }
83
84    /// Peek at the next token if it is a closing bracket.
85    #[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    /// Consume the next token if it is a closing bracket.
99    #[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(); // consume
107            Some(location)
108        } else {
109            None
110        }
111    }
112}
113
114/// A token emitted by the lexer. There is no semantic meaning at this stage.
115pub(super) enum Token<'a> {
116    /// A literal string, formatted and parsed as-is.
117    Literal(Spanned<&'a [u8]>),
118    /// An opening or closing bracket. May or may not be the start or end of a component.
119    Bracket {
120        /// Whether the bracket is opening or closing.
121        kind: BracketKind,
122        /// Where the bracket was in the format string.
123        location: Location,
124    },
125    /// One part of a component. This could be its name, a modifier, or whitespace.
126    ComponentPart {
127        /// Whether the part is whitespace or not.
128        kind: ComponentKind,
129        /// The part itself.
130        value: Spanned<&'a [u8]>,
131    },
132}
133
134/// What type of bracket is present.
135pub(super) enum BracketKind {
136    /// An opening bracket: `[`
137    Opening,
138    /// A closing bracket: `]`
139    Closing,
140}
141
142/// Indicates whether the component is whitespace or not.
143pub(super) enum ComponentKind {
144    Whitespace,
145    NotWhitespace,
146}
147
148/// Parse the string into a series of [`Token`]s.
149///
150/// `VERSION` controls the version of the format description that is being parsed. Currently, this
151/// must be 1 or 2.
152///
153/// - When `VERSION` is 1, `[[` is the only escape sequence, resulting in a literal `[`.
154/// - When `VERSION` is 2, all escape sequences begin with `\`. The only characters that may
155///   currently follow are `\`, `[`, and `]`, all of which result in the literal character. All
156///   other characters result in a lex error.
157#[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        // The flag is only set when version is zero.
168        if version.is_v1() {
169            // There is a flag set to emit the second half of an escaped bracket pair.
170            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            // possible escape sequence
180            (b'\\', backslash_loc) if version.is_at_least_v2() => {
181                match iter.next() {
182                    Some((b'\\' | b'[' | b']', char_loc)) => {
183                        // The escaped character is emitted as-is.
184                        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            // potentially escaped opening bracket
216            (b'[', location) if version.is_v1() => {
217                if let Some((_, second_location)) = iter.next_if(|&(&byte, _)| byte == b'[') {
218                    // Escaped bracket. Store the location of the second so we can emit it later.
219                    second_bracket_location = Some(second_location);
220                    input = &input[2..];
221                } else {
222                    // opening bracket
223                    depth += 1;
224                    input = &input[1..];
225                }
226
227                Token::Bracket {
228                    kind: BracketKind::Opening,
229                    location,
230                }
231            }
232            // opening bracket
233            (b'[', location) => {
234                depth += 1;
235                input = &input[1..];
236
237                Token::Bracket {
238                    kind: BracketKind::Opening,
239                    location,
240                }
241            }
242            // closing bracket
243            (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            // literal
253            (_, 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            // component part
270            (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}