From 433680ae0914da8921c4ee762fdc93e7b70cf9f1 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 9 Jun 2022 12:49:11 +0200 Subject: Add basic support for interrupting content --- src/tokenizer.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'src/tokenizer.rs') diff --git a/src/tokenizer.rs b/src/tokenizer.rs index c8b1440..4239520 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -62,7 +62,8 @@ pub enum TokenType { BlankLineWhitespace, Content, - ContentPhrasing, + ContentChunk, + ChunkString, } @@ -377,6 +378,61 @@ impl Tokenizer { ) } + // To do: lifetimes, boxes, lmao. + pub fn attempt_2( + &mut self, + a: impl FnOnce(&mut Tokenizer, Code) -> StateFnResult + 'static, + b: impl FnOnce(&mut Tokenizer, Code) -> StateFnResult + 'static, + done: impl FnOnce(bool) -> Box + 'static, + ) -> Box { + self.call_multiple(false, Some(Box::new(a)), Some(Box::new(b)), None, done) + } + + pub fn attempt_3( + &mut self, + a: impl FnOnce(&mut Tokenizer, Code) -> StateFnResult + 'static, + b: impl FnOnce(&mut Tokenizer, Code) -> StateFnResult + 'static, + c: impl FnOnce(&mut Tokenizer, Code) -> StateFnResult + 'static, + done: impl FnOnce(bool) -> Box + 'static, + ) -> Box { + self.call_multiple( + false, + Some(Box::new(a)), + Some(Box::new(b)), + Some(Box::new(c)), + done, + ) + } + + pub fn call_multiple( + &mut self, + check: bool, + a: Option>, + b: Option>, + c: Option>, + done: impl FnOnce(bool) -> Box + 'static, + ) -> Box { + if let Some(head) = a { + let callback = move |ok| { + if ok { + done(ok) + } else { + Box::new(move |tokenizer: &mut Tokenizer, code| { + tokenizer.call_multiple(check, b, c, None, done)(tokenizer, code) + }) + } + }; + + if check { + self.check(head, callback) + } else { + self.attempt(head, callback) + } + } else { + done(false) + } + } + /// Feed a list of `codes` into `start`. /// /// This is set up to support repeatedly calling `feed`, and thus streaming -- cgit