summaryrefslogtreecommitdiff
path: root/src/parse/scanner.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-10-02 16:52:01 +0200
committerLaurenz <laurmaedje@gmail.com>2020-10-02 16:52:01 +0200
commit343982c56f86d1ee3bcd806d245c304c57eabfe2 (patch)
tree7a20d8615f8d6b2dfd0e5936608c9339b21d783c /src/parse/scanner.rs
parent3533268b1f7a31581e7b8f44dff6d4f553ef348f (diff)
Optimize parser by remembering peeked token ⚡
Diffstat (limited to 'src/parse/scanner.rs')
-rw-r--r--src/parse/scanner.rs20
1 files changed, 10 insertions, 10 deletions
diff --git a/src/parse/scanner.rs b/src/parse/scanner.rs
index 6ff8c801..38c8736f 100644
--- a/src/parse/scanner.rs
+++ b/src/parse/scanner.rs
@@ -81,9 +81,9 @@ impl<'s> Scanner<'s> {
&self.src[start .. self.index]
}
- /// Uneat the last eaten character.
+ /// Uneat the last eaten char.
pub fn uneat(&mut self) {
- self.index = self.prev_index();
+ self.index = self.last_index();
self.reset();
}
@@ -97,9 +97,9 @@ impl<'s> Scanner<'s> {
self.iter.clone().nth(n)
}
- /// Checks whether the next character fulfills a condition.
+ /// Checks whether the next char fulfills a condition.
///
- /// Returns `false` if there is no next character.
+ /// Returns `false` if there is no next char.
pub fn check(&self, f: impl FnMut(char) -> bool) -> bool {
self.peek().map(f).unwrap_or(false)
}
@@ -109,13 +109,8 @@ impl<'s> Scanner<'s> {
self.iter.as_str().is_empty()
}
- /// The current index in the source string.
- pub fn index(&self) -> usize {
- self.index
- }
-
/// The previous index in the source string.
- pub fn prev_index(&self) -> usize {
+ pub fn last_index(&self) -> usize {
self.src[.. self.index]
.chars()
.next_back()
@@ -123,6 +118,11 @@ impl<'s> Scanner<'s> {
.unwrap_or(0)
}
+ /// The current index in the source string.
+ pub fn index(&self) -> usize {
+ self.index
+ }
+
/// Jump to an index in the source string.
pub fn jump(&mut self, index: usize) {
self.index = index;