From 518edf281f93cceb9793224f700a8f3d63d78ea2 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Wed, 27 Sep 2023 17:19:27 +0200 Subject: break!: remove Emitter::pop_token, use Iterator instead --- src/default_emitter.rs | 14 ++++++++------ src/emitter.rs | 11 +---------- src/naive_parser.rs | 4 ++-- src/tokenizer.rs | 6 +++--- 4 files changed, 14 insertions(+), 21 deletions(-) (limited to 'src') 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 Default for DefaultEmitter { } } -impl Emitter for DefaultEmitter { - type Token = Token; +impl Iterator for DefaultEmitter { + type Item = Token; + + fn next(&mut self) -> Option { + self.emitted_tokens.pop_back() + } +} +impl Emitter for DefaultEmitter { fn emit_eof(&mut self) { self.flush_current_characters(); } @@ -44,10 +50,6 @@ impl Emitter for DefaultEmitter { self.push_error(error, span); } - fn pop_token(&mut self) -> Option { - 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 { - /// 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); - /// 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; - /// 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 Iterator for NaiveParser where R: Reader + Position, O: Offset, - E: Emitter, + E: Emitter + Iterator, { - type Item = Result; + type Item = Result; fn next(&mut self) -> Option { 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 Iterator for Tokenizer where O: Offset, R: Reader + Position, - E: Emitter, + E: Emitter + Iterator, { - type Item = Result, R::Error>; + type Item = Result, R::Error>; fn next(&mut self) -> Option { loop { - if let Some(token) = self.emitter.pop_token() { + if let Some(token) = self.emitter.next() { return Some(Ok(Event::Token(token))); } -- cgit v1.2.3