|
| 1 | +#![allow(dead_code)] |
| 2 | + |
| 3 | +use std::str::Chars; |
| 4 | + |
| 5 | +pub(super) const EOF_CHAR: char = '\0'; |
| 6 | + |
| 7 | +/// A cursor represents a pointer in the source code. |
| 8 | +/// |
| 9 | +/// Based on [`rustc`'s `Cursor`](https://github.com/rust-lang/rust/blob/d1b7355d3d7b4ead564dbecb1d240fcc74fff21b/compiler/rustc_lexer/src/cursor.rs) |
| 10 | +#[derive(Clone, Debug)] |
| 11 | +pub(super) struct Cursor<'src> { |
| 12 | + /// An iterator over the [`char`]'s of the source code. |
| 13 | + chars: Chars<'src>, |
| 14 | + |
| 15 | + /// Stores the previous character for debug assertions. |
| 16 | + #[cfg(debug_assertions)] |
| 17 | + prev_char: char, |
| 18 | +} |
| 19 | + |
| 20 | +impl<'src> Cursor<'src> { |
| 21 | + pub(super) fn new(source: &'src str) -> Self { |
| 22 | + Self { |
| 23 | + chars: source.chars(), |
| 24 | + #[cfg(debug_assertions)] |
| 25 | + prev_char: EOF_CHAR, |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /// Returns the previous character. Useful for debug assertions. |
| 30 | + #[cfg(debug_assertions)] |
| 31 | + pub(super) const fn previous(&self) -> char { |
| 32 | + self.prev_char |
| 33 | + } |
| 34 | + |
| 35 | + /// Peeks the next character from the input stream without consuming it. |
| 36 | + /// Returns [`EOF_CHAR`] if the position is past the end of the file. |
| 37 | + pub(super) fn first(&self) -> char { |
| 38 | + self.chars.clone().next().unwrap_or(EOF_CHAR) |
| 39 | + } |
| 40 | + |
| 41 | + /// Peeks the second character from the input stream without consuming it. |
| 42 | + /// Returns [`EOF_CHAR`] if the position is past the end of the file. |
| 43 | + pub(super) fn second(&self) -> char { |
| 44 | + let mut chars = self.chars.clone(); |
| 45 | + chars.next(); |
| 46 | + chars.next().unwrap_or(EOF_CHAR) |
| 47 | + } |
| 48 | + |
| 49 | + /// Returns the remaining text to lex. |
| 50 | + /// |
| 51 | + /// Use [`Cursor::text_len`] to get the length of the remaining text. |
| 52 | + pub(super) fn rest(&self) -> &'src str { |
| 53 | + self.chars.as_str() |
| 54 | + } |
| 55 | + |
| 56 | + /// Returns `true` if the cursor is at the end of file. |
| 57 | + pub(super) fn is_eof(&self) -> bool { |
| 58 | + self.chars.as_str().is_empty() |
| 59 | + } |
| 60 | + |
| 61 | + /// Moves the cursor to the next character, returning the previous character. |
| 62 | + /// Returns [`None`] if there is no next character. |
| 63 | + pub(super) fn bump(&mut self) -> Option<char> { |
| 64 | + let prev = self.chars.next()?; |
| 65 | + |
| 66 | + #[cfg(debug_assertions)] |
| 67 | + { |
| 68 | + self.prev_char = prev; |
| 69 | + } |
| 70 | + |
| 71 | + Some(prev) |
| 72 | + } |
| 73 | + |
| 74 | + pub(super) fn eat_char(&mut self, c: char) -> bool { |
| 75 | + if self.first() == c { |
| 76 | + self.bump(); |
| 77 | + true |
| 78 | + } else { |
| 79 | + false |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + pub(super) fn eat_char2(&mut self, c1: char, c2: char) -> bool { |
| 84 | + let mut chars = self.chars.clone(); |
| 85 | + if chars.next() == Some(c1) && chars.next() == Some(c2) { |
| 86 | + self.bump(); |
| 87 | + self.bump(); |
| 88 | + true |
| 89 | + } else { |
| 90 | + false |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + pub(super) fn eat_char3(&mut self, c1: char, c2: char, c3: char) -> bool { |
| 95 | + let mut chars = self.chars.clone(); |
| 96 | + if chars.next() == Some(c1) && chars.next() == Some(c2) && chars.next() == Some(c3) { |
| 97 | + self.bump(); |
| 98 | + self.bump(); |
| 99 | + self.bump(); |
| 100 | + true |
| 101 | + } else { |
| 102 | + false |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + pub(super) fn eat_if<F>(&mut self, mut predicate: F) -> Option<char> |
| 107 | + where |
| 108 | + F: FnMut(char) -> bool, |
| 109 | + { |
| 110 | + if predicate(self.first()) && !self.is_eof() { |
| 111 | + self.bump() |
| 112 | + } else { |
| 113 | + None |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /// Eats symbols while predicate returns true or until the end of file is reached. |
| 118 | + #[inline] |
| 119 | + pub(super) fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) { |
| 120 | + // It was tried making optimized version of this for eg. line comments, but |
| 121 | + // LLVM can inline all of this and compile it down to fast iteration over bytes. |
| 122 | + while predicate(self.first()) && !self.is_eof() { |
| 123 | + self.bump(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /// Skips the next `count` bytes. |
| 128 | + /// |
| 129 | + /// ## Panics |
| 130 | + /// - If `count` is larger than the remaining bytes in the input stream. |
| 131 | + /// - If `count` indexes into a multi-byte character. |
| 132 | + pub(super) fn skip_bytes(&mut self, count: usize) { |
| 133 | + #[cfg(debug_assertions)] |
| 134 | + { |
| 135 | + self.prev_char = self.chars.as_str()[..count] |
| 136 | + .chars() |
| 137 | + .next_back() |
| 138 | + .unwrap_or('\0'); |
| 139 | + } |
| 140 | + |
| 141 | + self.chars = self.chars.as_str()[count..].chars(); |
| 142 | + } |
| 143 | + |
| 144 | + /// Skips to the end of the input stream. |
| 145 | + pub(super) fn skip_to_end(&mut self) { |
| 146 | + self.chars = "".chars(); |
| 147 | + } |
| 148 | +} |
0 commit comments