diff options
author | Martin Fischer <martin@push-f.com> | 2023-09-27 17:19:27 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2023-09-27 17:19:27 +0200 |
commit | 518edf281f93cceb9793224f700a8f3d63d78ea2 (patch) | |
tree | c6c0adfe88da9f329ac9c80f8c1e97607c749742 /src | |
parent | 67a812003a18cca016ff8b918610b4b6276ffd86 (diff) |
break!: remove Emitter::pop_token, use Iterator instead
Diffstat (limited to 'src')
-rw-r--r-- | src/default_emitter.rs | 14 | ||||
-rw-r--r-- | src/emitter.rs | 11 | ||||
-rw-r--r-- | src/naive_parser.rs | 4 | ||||
-rw-r--r-- | src/tokenizer.rs | 6 |
4 files changed, 14 insertions, 21 deletions
diff --git a/src/default_emitter.rs b/src/default_emitter.rs index 9d5ab52..7ef96ec 100644 --- a/src/default_emitter.rs +++ b/src/default_emitter.rs @@ -33,9 +33,15 @@ impl<O> Default for DefaultEmitter<O> { } } -impl<O: Offset> Emitter<O> for DefaultEmitter<O> { - type Token = Token<O>; +impl<O> Iterator for DefaultEmitter<O> { + type Item = Token<O>; + + fn next(&mut self) -> Option<Self::Item> { + self.emitted_tokens.pop_back() + } +} +impl<O: Offset> Emitter<O> for DefaultEmitter<O> { fn emit_eof(&mut self) { self.flush_current_characters(); } @@ -44,10 +50,6 @@ impl<O: Offset> Emitter<O> for DefaultEmitter<O> { self.push_error(error, span); } - fn pop_token(&mut self) -> Option<Self::Token> { - self.emitted_tokens.pop_back() - } - fn emit_string(&mut self, s: &str) { self.current_characters.push_str(s); } diff --git a/src/emitter.rs b/src/emitter.rs index 311c73c..923da1c 100644 --- a/src/emitter.rs +++ b/src/emitter.rs @@ -24,21 +24,12 @@ use crate::Error; #[allow(unused_variables)] // workaround for https://github.com/rust-lang/rust/issues/91074 pub trait Emitter<O> { - /// The token type emitted by this emitter. This controls what type of values the [`Tokenizer`](crate::Tokenizer) - /// yields when used as an iterator. - type Token; - - /// The state machine has reached the end of the file. It will soon call `pop_token` for the - /// last time. + /// The state machine has reached the end of the file. fn emit_eof(&mut self); /// A (probably recoverable) parsing error has occurred. fn emit_error(&mut self, error: Error, span: Range<O>); - /// After every state change, the tokenizer calls this method to retrieve a new token that can - /// be returned via the tokenizer's iterator interface. - fn pop_token(&mut self) -> Option<Self::Token>; - /// Emit a bunch of plain characters as character tokens. fn emit_string(&mut self, c: &str); diff --git a/src/naive_parser.rs b/src/naive_parser.rs index 93c37d7..10eb98d 100644 --- a/src/naive_parser.rs +++ b/src/naive_parser.rs @@ -65,9 +65,9 @@ impl<R, O, E> Iterator for NaiveParser<R, O, E> where R: Reader + Position<O>, O: Offset, - E: Emitter<O>, + E: Emitter<O> + Iterator, { - type Item = Result<E::Token, R::Error>; + type Item = Result<E::Item, R::Error>; fn next(&mut self) -> Option<Self::Item> { loop { diff --git a/src/tokenizer.rs b/src/tokenizer.rs index a37a832..f4c0c48 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -373,13 +373,13 @@ impl<O, R, E> Iterator for Tokenizer<R, O, E> where O: Offset, R: Reader + Position<O>, - E: Emitter<O>, + E: Emitter<O> + Iterator, { - type Item = Result<Event<E::Token>, R::Error>; + type Item = Result<Event<E::Item>, R::Error>; fn next(&mut self) -> Option<Self::Item> { loop { - if let Some(token) = self.emitter.pop_token() { + if let Some(token) = self.emitter.next() { return Some(Ok(Event::Token(token))); } |