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
|
//! Trailing whitespace occurs in [string][] and [text][].
//!
//! It occurs at the start or end of the whole, or around line endings.
//! This whitespace is ignored
//!
//! They’re formed with the following BNF:
//!
//! ```bnf
//! ; Restriction: the start and end here count as an eol.
//! whitespace ::= 0.*space_or_tab eol 0.*space_or_tab
//! ```
//!
//! ## References
//!
//! * [`initialize/text.js` in `micromark`](https://github.com/micromark/micromark/blob/main/packages/micromark/dev/lib/initialize/text.js)
//!
//! [string]: crate::content::string
//! [text]: crate::content::text
use super::partial_space_or_tab::space_or_tab;
use crate::tokenizer::{Code, State, StateFnResult, Tokenizer};
/// Parse initial or final whitespace.
pub fn whitespace(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult {
tokenizer.go(
// Nothing if there’s no whitespace.
space_or_tab(),
if matches!(
tokenizer.previous,
Code::None | Code::CarriageReturnLineFeed | Code::Char('\r' | '\n')
) {
// If there’s whitespace, and we were at an eol/eof, `ok`
ok
} else {
// If there’s whitespace, and we were not at an eol/eof, there must be one here.
at_eol
},
)(tokenizer, code)
}
/// After whitespace, at an eol/eof.
fn at_eol(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult {
if matches!(
code,
Code::None | Code::CarriageReturnLineFeed | Code::Char('\r' | '\n')
) {
ok(tokenizer, code)
} else {
(State::Nok, None)
}
}
/// Fine.
fn ok(_tokenizer: &mut Tokenizer, code: Code) -> StateFnResult {
(State::Ok, Some(vec![code]))
}
|