forked from untitaker/html5gum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.rs
119 lines (107 loc) · 4.25 KB
/
tokenizer.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
use crate::char_validator::CharValidator;
use crate::machine;
use crate::machine_helper::MachineHelper;
use crate::read_helper::ReadHelper;
use crate::utils::{ControlToken, State};
use crate::{DefaultEmitter, Emitter, Never, Readable, Reader};
/// A HTML tokenizer. See crate-level docs for basic usage.
pub struct Tokenizer<R: Reader, E: Emitter = DefaultEmitter> {
eof: bool,
pub(crate) validator: CharValidator,
pub(crate) emitter: E,
pub(crate) reader: ReadHelper<R>,
pub(crate) machine_helper: MachineHelper,
}
impl<R: Reader> Tokenizer<R> {
/// Create a new tokenizer from some input.
///
/// `input` can be `&String` or `&str` at the moment, as those are the types for which
/// [`crate::Readable`] is implemented, but you can implement that trait on your own types.
///
/// Patches are welcome for providing an efficient implementation over async streams,
/// iterators, files, etc, as long as any dependencies come behind featureflags.
pub fn new<'a, S: Readable<'a, Reader = R>>(input: S) -> Self {
Tokenizer::<S::Reader>::new_with_emitter(input, DefaultEmitter::default())
}
}
impl<R: Reader, E: Emitter> Tokenizer<R, E> {
/// Construct a new tokenizer from some input and a custom emitter.
///
/// Use this method over [`Tokenizer::new`] when you want to have more control over string allocation for
/// tokens.
pub fn new_with_emitter<'a, S: Readable<'a, Reader = R>>(input: S, emitter: E) -> Self {
Tokenizer {
eof: false,
validator: CharValidator::default(),
emitter,
reader: ReadHelper::new(input.to_reader()),
machine_helper: MachineHelper::default(),
}
}
/// Test-internal function to override internal state.
///
/// Only available with the `integration-tests` feature which is not public API.
#[cfg(feature = "integration-tests")]
pub fn set_state(&mut self, state: State) {
self.machine_helper.state = state;
}
/// Set the statemachine to start/continue in [plaintext
/// state](https://html.spec.whatwg.org/#plaintext-state).
///
/// This tokenizer never gets into that state naturally.
pub fn set_plaintext_state(&mut self) {
self.machine_helper.state = State::PlainText;
}
/// Test-internal function to override internal state.
///
/// Only available with the `integration-tests` feature which is not public API.
#[cfg(feature = "integration-tests")]
pub fn set_last_start_tag(&mut self, last_start_tag: Option<&str>) {
self.emitter
.set_last_start_tag(last_start_tag.map(str::as_bytes));
}
}
impl<R: Reader, E: Emitter> Iterator for Tokenizer<R, E> {
type Item = Result<E::Token, R::Error>;
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Some(token) = self.emitter.pop_token() {
break Some(Ok(token));
} else if !self.eof {
match machine::consume(self) {
Ok(ControlToken::Continue) => (),
Ok(ControlToken::Eof) => {
self.validator.flush_character_error(&mut self.emitter);
self.eof = true;
self.emitter.emit_eof();
}
Err(e) => break Some(Err(e)),
}
} else {
break None;
}
}
}
}
/// A kind of tokenizer that directly yields tokens when used as an iterator, so `Token` instead of
/// `Result<Token, _>`.
///
/// This is the return value of [`Tokenizer::infallible`].
pub struct InfallibleTokenizer<R: Reader<Error = Never>, E: Emitter>(Tokenizer<R, E>);
impl<R: Reader<Error = Never>, E: Emitter> Tokenizer<R, E> {
/// Statically assert that this iterator is infallible.
///
/// Call this to get rid of error handling when parsing HTML from strings.
pub fn infallible(self) -> InfallibleTokenizer<R, E> {
InfallibleTokenizer(self)
}
}
impl<R: Reader<Error = Never>, E: Emitter> Iterator for InfallibleTokenizer<R, E> {
type Item = E::Token;
fn next(&mut self) -> Option<Self::Item> {
match self.0.next()? {
Ok(token) => Some(token),
Err(e) => match e {},
}
}
}