aboutsummaryrefslogtreecommitdiffstats
path: root/src/construct/partial_whitespace.rs
blob: dd0d2b5720c0594772e1a08bc37067fe9d635c5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! 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)
//!
//! <!-- To do: link stuff -->

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]))
        }
    }
}