From 65dd765cceee8bdccc74c08066eec59a579a16b1 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 20 Jun 2022 17:06:00 +0200 Subject: Add improved whitespace handling * add several helpers for parsing betwen x and y `space_or_tab`s * use those helpers in a bunch of places * move initial indent parsing to flow constructs themselves --- src/construct/partial_whitespace.rs | 64 ------------------------------------- 1 file changed, 64 deletions(-) delete mode 100644 src/construct/partial_whitespace.rs (limited to 'src/construct/partial_whitespace.rs') diff --git a/src/construct/partial_whitespace.rs b/src/construct/partial_whitespace.rs deleted file mode 100644 index b8cf9a7..0000000 --- a/src/construct/partial_whitespace.rs +++ /dev/null @@ -1,64 +0,0 @@ -//! A little helper to parse `space_or_tab` -//! -//! They’re formed with the following BNF: -//! -//! ```bnf -//! space_or_tab ::= 1*(' ' '\t') -//! ``` -//! -//! Depending on where whitespace can occur, it can be optional (or not), -//! and present in the rendered result (or not). -//! -//! ## References -//! -//! * [`micromark-factory-space/index.js` in `micromark`](https://github.com/micromark/micromark/blob/main/packages/micromark-factory-space/dev/index.js) - -use crate::tokenizer::{Code, State, StateFnResult, TokenType, Tokenizer}; - -// To do: should `token_type` be a `Some`, with `None` defaulting to something? -// To do: should `max: Some(usize)` be added? - -/// Before whitespace. -/// -/// ```markdown -/// alpha| bravo -/// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code, token_type: TokenType) -> StateFnResult { - match code { - Code::VirtualSpace | Code::Char('\t' | ' ') => { - // To do: lifetimes. - let clone = token_type.clone(); - tokenizer.enter(token_type); - tokenizer.consume(code); - ( - State::Fn(Box::new(|tokenizer, code| inside(tokenizer, code, clone))), - None, - ) - } - _ => (State::Nok, None), - } -} - -/// In whitespace. -/// -/// ```markdown -/// alpha |bravo -/// alpha | bravo -/// ``` -fn inside(tokenizer: &mut Tokenizer, code: Code, token_type: TokenType) -> StateFnResult { - match code { - Code::VirtualSpace | Code::Char('\t' | ' ') => { - tokenizer.consume(code); - ( - State::Fn(Box::new(|tokenizer, code| { - inside(tokenizer, code, token_type) - })), - None, - ) - } - _ => { - tokenizer.exit(token_type); - (State::Ok, Some(vec![code])) - } - } -} -- cgit