Skip to content

Commit

Permalink
Add Tokenizer::consume_char()
Browse files Browse the repository at this point in the history
  • Loading branch information
sile committed Aug 5, 2023
1 parent 24f915f commit 27770b3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ impl Position {
self.column += text.len();
self
}

pub(crate) fn step_by_char(mut self, c: char) -> Position {
if c == '\n' {
self.offset += 1;
self.line += 1;
self.column = 1;
} else {
self.offset += 1;
self.column += 1;
}
self
}
}

impl Default for Position {
Expand Down
26 changes: 26 additions & 0 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,32 @@ where
pub fn set_position(&mut self, position: Position) {
self.next_pos = position;
}

/// Consumes the next char.
///
/// This method can be used to recover from a tokenization error.
///
/// # Examples
///
/// ```
/// use erl_tokenize::Tokenizer;
///
/// let src = r#"io:format("Hello")."#;
///
/// let mut tokenizer = Tokenizer::new(src);
/// assert_eq!(tokenizer.next_position().offset(), 0);
///
/// tokenizer.consume_char();
/// assert_eq!(tokenizer.next_position().offset(), 1);
/// ```
pub fn consume_char(&mut self) -> Option<char> {
if let Some(c) = self.text.as_ref()[self.next_pos.offset()..].chars().next() {
self.next_pos = self.next_pos.clone().step_by_char(c);
Some(c)
} else {
None
}
}
}
impl<T> Iterator for Tokenizer<T>
where
Expand Down

0 comments on commit 27770b3

Please sign in to comment.