summaryrefslogtreecommitdiff
path: root/src/parse/scanner.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-10-02 15:43:29 +0200
committerLaurenz <laurmaedje@gmail.com>2020-10-02 15:43:29 +0200
commit3533268b1f7a31581e7b8f44dff6d4f553ef348f (patch)
tree3fee21d2df7ce173131f75f46a1ef040f272ed29 /src/parse/scanner.rs
parentf8770d2b2a8ac389704897f92f2753398352835b (diff)
Refactor parser 🏞
Diffstat (limited to 'src/parse/scanner.rs')
-rw-r--r--src/parse/scanner.rs38
1 files changed, 24 insertions, 14 deletions
diff --git a/src/parse/scanner.rs b/src/parse/scanner.rs
index 9447222d..6ff8c801 100644
--- a/src/parse/scanner.rs
+++ b/src/parse/scanner.rs
@@ -4,7 +4,8 @@ use std::fmt::{self, Debug, Formatter};
use std::slice::SliceIndex;
use std::str::Chars;
-/// A low-level featureful char scanner.
+/// A low-level featureful char-based scanner.
+#[derive(Clone)]
pub struct Scanner<'s> {
src: &'s str,
iter: Chars<'s>,
@@ -98,24 +99,22 @@ impl<'s> Scanner<'s> {
/// Checks whether the next character fulfills a condition.
///
- /// Returns `false` is there is no next character.
+ /// Returns `false` if there is no next character.
pub fn check(&self, f: impl FnMut(char) -> bool) -> bool {
self.peek().map(f).unwrap_or(false)
}
- /// Go back to the where the index says.
- fn reset(&mut self) {
- self.iter = self.src[self.index ..].chars();
+ /// Whether the end of the source string is reached.
+ pub fn eof(&self) -> bool {
+ self.iter.as_str().is_empty()
}
-}
-impl<'s> Scanner<'s> {
- /// The current index in the string.
+ /// The current index in the source string.
pub fn index(&self) -> usize {
self.index
}
- /// The previous index in the string.
+ /// The previous index in the source string.
pub fn prev_index(&self) -> usize {
self.src[.. self.index]
.chars()
@@ -124,6 +123,17 @@ impl<'s> Scanner<'s> {
.unwrap_or(0)
}
+ /// Jump to an index in the source string.
+ pub fn jump(&mut self, index: usize) {
+ self.index = index;
+ self.reset();
+ }
+
+ /// The full source string.
+ pub fn src(&self) -> &'s str {
+ self.src
+ }
+
/// Slice a part out of the source string.
pub fn get<I>(&self, index: I) -> &'s str
where
@@ -132,11 +142,6 @@ impl<'s> Scanner<'s> {
&self.src[index]
}
- /// The full source string.
- pub fn src(&self) -> &'s str {
- self.src
- }
-
/// The full source string up to the current index.
pub fn eaten(&self) -> &'s str {
&self.src[.. self.index]
@@ -151,6 +156,11 @@ impl<'s> Scanner<'s> {
pub fn rest(&self) -> &'s str {
&self.src[self.index ..]
}
+
+ /// Go back to the where the index says.
+ fn reset(&mut self) {
+ self.iter = self.src[self.index ..].chars();
+ }
}
impl Debug for Scanner<'_> {