diff options
| author | Botahamec <botahamec@outlook.com> | 2023-07-29 22:35:51 -0400 |
|---|---|---|
| committer | Botahamec <botahamec@outlook.com> | 2023-07-29 22:35:51 -0400 |
| commit | f212e70c7acc0aa94d5bc2b41f662f795d299e29 (patch) | |
| tree | 6965ff26984a00a868e08b1f0e21acc5647c76de | |
| parent | 0e1b248556a658618c1d2ebe22775362ba264d03 (diff) | |
Methods for reading the source directly
| -rw-r--r-- | src/scanner.rs | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/src/scanner.rs b/src/scanner.rs index 4e5ca21..b5b1850 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -13,6 +13,22 @@ impl Scanner { } } + pub fn source(&self) -> &[char] { + &self.source + } + + pub fn len(&self) -> usize { + self.source.len() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get_char_at(&self, index: usize) -> Option<char> { + self.source.get(index).cloned() + } + pub fn position(&self) -> usize { self.position } @@ -33,8 +49,9 @@ impl Scanner { Some(production) } - pub fn advance(&mut self, amount: usize) -> Option<String> { - self.goto(self.position + amount) + pub fn advance(&mut self, amount: isize) -> Option<String> { + let position = self.position.checked_add_signed(amount)?; + self.goto(position) } pub fn find_substring(&self, substring: impl AsRef<str>) -> Option<usize> { |
