diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2025-02-10 11:54:32 +1300 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2025-02-10 11:54:32 +1300 |
commit | 2d67476d48fcb33cd1c59cbc2e0f82872bc0c217 (patch) | |
tree | 93706b9ca4dcadc66d78c3ee6b62db467e17bef2 /src/tokeniser.rs | |
parent | 404bf4e4bac16a37692a8f7e9e86ad2c89f38abd (diff) | |
download | assembler-2d67476d48fcb33cd1c59cbc2e0f82872bc0c217.zip |
Add tokeniser method to mark previous char as start of token
This enables a parsing technique where characters can be marked as
the start of a token after they've been eaten, instead of having to
determine ahead of time via defensively peeking.
Diffstat (limited to 'src/tokeniser.rs')
-rw-r--r-- | src/tokeniser.rs | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/src/tokeniser.rs b/src/tokeniser.rs index 7d8ddfd..6ae9055 100644 --- a/src/tokeniser.rs +++ b/src/tokeniser.rs @@ -129,8 +129,21 @@ impl Tokeniser { return true; } + /// Mark the next character to be consumed as the start character. pub fn mark_start_position(&mut self) { self.start_position = self.position; + self.consumed.clear(); + } + + /// Mark the previously-consumed character as the start character. + pub fn mark_prev_start_position(&mut self) { + self.start_position = self.prev_position; + let c = self.consumed.chars().last(); + self.consumed.clear(); + // Keep the previously pushed character. + if let Some(c) = c { + self.consumed.push(c); + } } /// Only call this once per span, it consumes the token string. |