syntax_parser_generator/readers/reader.rs
1/// A unique interface for accessing a sequence of input items.
2///
3/// This interface is used by the lexical analyzer when accessing input text, and needs to be
4/// implemented for reading bytes ([`Reader<u8>`]) before the underlying input text can be parsed.
5///
6/// # Implementation
7///
8/// A [Reader] should maintain 3 conceptual pointers into the input sequence of items, dubbed
9/// `head`, `cursor`, and `tail`. All should initially point to the beginning of the sequence. The
10/// interface provides an API for managing these pointers, and accessing the underlying data. This
11/// API is internally used by lexical analyzers.
12pub trait Reader<T> {
13 /// Reads the item pointed by `cursor`, and advance `cursor` to the next item.
14 ///
15 /// Returns [None] if `cursor` points to no valid item (for example - when the input is
16 /// exhausted).
17 fn read_next(&mut self) -> Option<T>;
18
19 /// Set `head` to point to the item pointed by `cursor`.
20 fn set_head(&mut self);
21
22 /// Set `tail` to point to the item pointed by `cursor`.
23 fn set_tail(&mut self);
24
25 /// Set `cursor` to point to the item pointed by `tail`.
26 fn move_cursor_to_tail(&mut self);
27
28 /// Get an iterator over all items between `head` (inclusive) and `tail` (exclusive).
29 fn get_sequence(&self) -> impl Iterator<Item=T>;
30
31 /// Set `cursor` and `head` to point to the item pointed by `tail`.
32 fn restart_from_tail(&mut self) {
33 self.move_cursor_to_tail();
34 self.set_head();
35 self.set_tail();
36 }
37}