/
mod.rs
169 lines (145 loc) · 5.14 KB
/
mod.rs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//! Inline rule chain
use std::collections::HashMap;
use once_cell::sync::OnceCell;
mod state;
pub use state::*;
mod rule;
pub use rule::*;
#[doc(hidden)]
pub mod builtin;
pub use builtin::inline_parser::InlineRoot;
pub use builtin::skip_text::{Text, TextSpecial};
use builtin::skip_text::TextScannerImpl;
use crate::{MarkdownIt, Node};
use crate::common::TypeKey;
use crate::common::ruler::Ruler;
use crate::parser::extset::{InlineRootExtSet, RootExtSet};
use super::node::NodeEmpty;
type RuleFns = (
fn (&mut InlineState) -> Option<usize>,
fn (&mut InlineState) -> Option<(Node, usize)>,
);
#[derive(Debug, Default)]
/// Inline-level tokenizer.
pub struct InlineParser {
ruler: Ruler<TypeKey, RuleFns>,
text_charmap: HashMap<char, Vec<TypeKey>>,
text_impl: OnceCell<TextScannerImpl>,
}
impl InlineParser {
pub fn new() -> Self {
Self::default()
}
// Skip single token by running all rules in validation mode;
// returns `true` if any rule reported success
//
pub fn skip_token(&self, state: &mut InlineState) {
stacker::maybe_grow(64*1024, 1024*1024, || {
let pos = state.pos;
let mut ok = None;
if let Some(x) = state.cache.get(&pos) {
state.pos = *x;
return;
}
if state.level < state.md.max_nesting {
for rule in self.ruler.iter() {
ok = rule.0(state);
if ok.is_some() {
break;
}
}
} else {
// Too much nesting, just skip until the end of the paragraph.
//
// NOTE: this will cause links to behave incorrectly in the following case,
// when an amount of `[` is exactly equal to `maxNesting + 1`:
//
// [[[[[[[[[[[[[[[[[[[[[foo]()
//
// TODO: remove this workaround when CM standard will allow nested links
// (we can replace it by preventing links from being parsed in
// validation mode)
//
state.pos = state.pos_max;
}
if let Some(len) = ok {
state.pos += len;
} else {
let ch = state.src[state.pos..state.pos_max].chars().next().unwrap();
state.pos += ch.len_utf8();
}
state.cache.insert(pos, state.pos);
});
}
// Generate tokens for input range
//
pub fn tokenize(&self, state: &mut InlineState) {
stacker::maybe_grow(64*1024, 1024*1024, || {
let end = state.pos_max;
while state.pos < end {
// Try all possible rules.
// On success, rule should:
//
// - update `state.pos`
// - update `state.tokens`
// - return true
let mut ok = None;
if state.level < state.md.max_nesting {
for rule in self.ruler.iter() {
ok = rule.1(state);
if ok.is_some() {
break;
}
}
}
if let Some((mut node, len)) = ok {
state.pos += len;
if !node.is::<NodeEmpty>() {
node.srcmap = state.get_map(state.pos - len, state.pos);
state.node.children.push(node);
if state.pos >= end { break; }
}
continue;
}
let ch = state.src[state.pos..state.pos_max].chars().next().unwrap();
let len = ch.len_utf8();
state.trailing_text_push(state.pos, state.pos + len);
state.pos += len;
}
});
}
// Process input string and push inline tokens into `out_tokens`
//
pub fn parse(
&self,
src: String,
srcmap: Vec<(usize, usize)>,
node: Node,
md: &MarkdownIt,
root_ext: &mut RootExtSet,
inline_ext: &mut InlineRootExtSet,
) -> Node {
let mut state = InlineState::new(src, srcmap, md, root_ext, inline_ext, node);
self.tokenize(&mut state);
state.node
}
pub fn add_rule<T: InlineRule>(&mut self) -> RuleBuilder<RuleFns> {
if T::MARKER != '\0' {
let charvec = self.text_charmap.entry(T::MARKER).or_insert(vec![]);
charvec.push(TypeKey::of::<T>());
}
let item = self.ruler.add(TypeKey::of::<T>(), (T::check, T::run));
RuleBuilder::new(item)
}
pub fn has_rule<T: InlineRule>(&mut self) -> bool {
self.ruler.contains(TypeKey::of::<T>())
}
pub fn remove_rule<T: InlineRule>(&mut self) {
if T::MARKER != '\0' {
let mut charvec = self.text_charmap.remove(&T::MARKER).unwrap_or_default();
charvec.retain(|x| *x != TypeKey::of::<T>());
self.text_charmap.insert(T::MARKER, charvec);
}
self.ruler.remove(TypeKey::of::<T>());
}
}