-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathtokenizer.rs
430 lines (374 loc) · 13.7 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Copyright 2014 The html5ever Project Developers. See the
// COPYRIGHT file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(start, rt, test, slice_patterns)]
#![cfg_attr(feature = "unstable", feature(plugin))]
#![cfg_attr(feature = "unstable", plugin(string_cache_plugin))]
extern crate test;
extern crate rustc_serialize;
#[macro_use] extern crate string_cache;
extern crate tendril;
extern crate html5ever;
mod foreach_html5lib_test;
use foreach_html5lib_test::foreach_html5lib_test;
use std::{char, env, rt};
use std::ffi::OsStr;
use std::mem::replace;
use std::default::Default;
use std::path::Path;
use test::{TestDesc, TestDescAndFn, DynTestName, DynTestFn};
use test::ShouldPanic::No;
use rustc_serialize::json::Json;
use std::collections::BTreeMap;
use std::borrow::Cow::Borrowed;
use html5ever::tokenizer::{Doctype, Attribute, StartTag, EndTag, Tag};
use html5ever::tokenizer::{Token, DoctypeToken, TagToken, CommentToken};
use html5ever::tokenizer::{CharacterTokens, NullCharacterToken, EOFToken, ParseError};
use html5ever::tokenizer::{TokenSink, Tokenizer, TokenizerOpts};
use html5ever::tokenizer::states::{Plaintext, RawData, Rcdata, Rawtext};
use string_cache::{Atom, QualName};
use tendril::{StrTendril, SliceExt};
// Return all ways of splitting the string into at most n
// possibly-empty pieces.
fn splits(s: &str, n: usize) -> Vec<Vec<StrTendril>> {
if n == 1 {
return vec!(vec!(s.to_tendril()));
}
let mut points: Vec<usize> = s.char_indices().map(|(n,_)| n).collect();
points.push(s.len());
// do this with iterators?
let mut out = vec!();
for p in points.into_iter() {
let y = &s[p..];
for mut x in splits(&s[..p], n-1).into_iter() {
x.push(y.to_tendril());
out.push(x);
}
}
out.extend(splits(s, n-1).into_iter());
out
}
struct TokenLogger {
tokens: Vec<Token>,
current_str: StrTendril,
exact_errors: bool,
}
impl TokenLogger {
fn new(exact_errors: bool) -> TokenLogger {
TokenLogger {
tokens: vec!(),
current_str: StrTendril::new(),
exact_errors: exact_errors,
}
}
// Push anything other than character tokens
fn push(&mut self, token: Token) {
self.finish_str();
self.tokens.push(token);
}
fn finish_str(&mut self) {
if self.current_str.len() > 0 {
let s = replace(&mut self.current_str, StrTendril::new());
self.tokens.push(CharacterTokens(s));
}
}
fn get_tokens(mut self) -> Vec<Token> {
self.finish_str();
self.tokens
}
}
impl TokenSink for TokenLogger {
fn process_token(&mut self, token: Token) {
match token {
CharacterTokens(b) => {
self.current_str.push_slice(&b);
}
NullCharacterToken => {
self.current_str.push_char('\0');
}
ParseError(_) => if self.exact_errors {
self.push(ParseError(Borrowed("")));
},
TagToken(mut t) => {
// The spec seems to indicate that one can emit
// erroneous end tags with attrs, but the test
// cases don't contain them.
match t.kind {
EndTag => {
t.self_closing = false;
t.attrs = vec!();
}
_ => t.attrs.sort_by(|a1, a2| a1.name.cmp(&a2.name)),
}
self.push(TagToken(t));
}
EOFToken => (),
_ => self.push(token),
}
}
}
fn tokenize(input: Vec<StrTendril>, opts: TokenizerOpts) -> Vec<Token> {
let sink = TokenLogger::new(opts.exact_errors);
let mut tok = Tokenizer::new(sink, opts);
for chunk in input.into_iter() {
tok.feed(chunk);
}
tok.end();
tok.unwrap().get_tokens()
}
trait JsonExt {
fn get_str(&self) -> String;
fn get_tendril(&self) -> StrTendril;
fn get_nullable_tendril(&self) -> Option<StrTendril>;
fn get_bool(&self) -> bool;
fn get_obj<'t>(&'t self) -> &'t BTreeMap<String, Self>;
fn get_list<'t>(&'t self) -> &'t Vec<Self>;
fn find<'t>(&'t self, key: &str) -> &'t Self;
}
impl JsonExt for Json {
fn get_str(&self) -> String {
match *self {
Json::String(ref s) => s.to_string(),
_ => panic!("Json::get_str: not a String"),
}
}
fn get_tendril(&self) -> StrTendril {
match *self {
Json::String(ref s) => s.to_tendril(),
_ => panic!("Json::get_tendril: not a String"),
}
}
fn get_nullable_tendril(&self) -> Option<StrTendril> {
match *self {
Json::Null => None,
Json::String(ref s) => Some(s.to_tendril()),
_ => panic!("Json::get_nullable_tendril: not a String"),
}
}
fn get_bool(&self) -> bool {
match *self {
Json::Boolean(b) => b,
_ => panic!("Json::get_bool: not a Boolean"),
}
}
fn get_obj<'t>(&'t self) -> &'t BTreeMap<String, Json> {
match *self {
Json::Object(ref m) => &*m,
_ => panic!("Json::get_obj: not an Object"),
}
}
fn get_list<'t>(&'t self) -> &'t Vec<Json> {
match *self {
Json::Array(ref m) => m,
_ => panic!("Json::get_list: not an Array"),
}
}
fn find<'t>(&'t self, key: &str) -> &'t Json {
self.get_obj().get(&key.to_string()).unwrap()
}
}
// Parse a JSON object (other than "ParseError") to a token.
fn json_to_token(js: &Json) -> Token {
let parts = js.get_list();
// Collect refs here so we don't have to use "ref" in all the patterns below.
let args: Vec<&Json> = parts[1..].iter().collect();
match (&parts[0].get_str()[..], &args[..]) {
("DOCTYPE", [name, public_id, system_id, correct]) => DoctypeToken(Doctype {
name: name.get_nullable_tendril(),
public_id: public_id.get_nullable_tendril(),
system_id: system_id.get_nullable_tendril(),
force_quirks: !correct.get_bool(),
}),
("StartTag", [name, attrs, rest..]) => TagToken(Tag {
kind: StartTag,
name: Atom::from_slice(&name.get_str()),
attrs: attrs.get_obj().iter().map(|(k,v)| {
Attribute {
name: QualName::new(ns!(""), Atom::from_slice(&k)),
value: v.get_tendril()
}
}).collect(),
self_closing: match rest {
[ref b, ..] => b.get_bool(),
_ => false,
}
}),
("EndTag", [name]) => TagToken(Tag {
kind: EndTag,
name: Atom::from_slice(&name.get_str()),
attrs: vec!(),
self_closing: false
}),
("Comment", [txt]) => CommentToken(txt.get_tendril()),
("Character", [txt]) => CharacterTokens(txt.get_tendril()),
// We don't need to produce NullCharacterToken because
// the TokenLogger will convert them to CharacterTokens.
_ => panic!("don't understand token {:?}", parts),
}
}
// Parse the "output" field of the test case into a vector of tokens.
fn json_to_tokens(js: &Json, exact_errors: bool) -> Vec<Token> {
// Use a TokenLogger so that we combine character tokens separated
// by an ignored error.
let mut sink = TokenLogger::new(exact_errors);
for tok in js.get_list().iter() {
match *tok {
Json::String(ref s)
if &s[..] == "ParseError" => sink.process_token(ParseError(Borrowed(""))),
_ => sink.process_token(json_to_token(tok)),
}
}
sink.get_tokens()
}
// Undo the escaping in "doubleEscaped" tests.
fn unescape(s: &str) -> Option<String> {
let mut out = String::with_capacity(s.len());
let mut it = s.chars().peekable();
loop {
match it.next() {
None => return Some(out),
Some('\\') => {
if it.peek() != Some(&'u') {
panic!("can't understand escape");
}
drop(it.next());
let hex: String = it.by_ref().take(4).collect();
match u32::from_str_radix(&hex, 16).ok()
.and_then(char::from_u32) {
// Some of the tests use lone surrogates, but we have no
// way to represent them in the UTF-8 input to our parser.
// Since these can only come from script, we will catch
// them there.
None => return None,
Some(c) => out.push(c),
}
}
Some(c) => out.push(c),
}
}
}
fn unescape_json(js: &Json) -> Json {
match *js {
// unwrap is OK here because the spec'd *output* of the tokenizer never
// contains a lone surrogate.
Json::String(ref s) => Json::String(unescape(&s).unwrap()),
Json::Array(ref xs) => Json::Array(xs.iter().map(unescape_json).collect()),
Json::Object(ref obj) => {
let mut new_obj = BTreeMap::new();
for (k,v) in obj.iter() {
new_obj.insert(k.clone(), unescape_json(v));
}
Json::Object(new_obj)
}
_ => js.clone(),
}
}
fn mk_test(desc: String, input: String, expect: Vec<Token>, opts: TokenizerOpts)
-> TestDescAndFn {
TestDescAndFn {
desc: TestDesc {
name: DynTestName(desc),
ignore: false,
should_panic: No,
},
testfn: DynTestFn(Box::new(move || {
// Split up the input at different points to test incremental tokenization.
let insplits = splits(&input, 3);
for input in insplits.into_iter() {
// Clone 'input' so we have it for the failure message.
// Also clone opts. If we don't, we get the wrong
// result but the compiler doesn't catch it!
// Possibly mozilla/rust#12223.
let output = tokenize(input.clone(), opts.clone());
if output != expect {
panic!("\ninput: {:?}\ngot: {:?}\nexpected: {:?}",
input, output, expect);
}
}
})),
}
}
fn mk_tests(tests: &mut Vec<TestDescAndFn>, filename: &str, js: &Json) {
let obj = js.get_obj();
let mut input = js.find("input").unwrap().get_str();
let mut expect = js.find("output").unwrap().clone();
let desc = format!("tok: {}: {}",
filename, js.find("description").unwrap().get_str());
// "Double-escaped" tests require additional processing of
// the input and output.
if obj.get(&"doubleEscaped".to_string()).map_or(false, |j| j.get_bool()) {
match unescape(&input) {
None => return,
Some(i) => input = i,
}
expect = unescape_json(&expect);
}
// Some tests have a last start tag name.
let start_tag = obj.get(&"lastStartTag".to_string()).map(|s| s.get_str());
// Some tests want to start in a state other than Data.
let state_overrides = match obj.get(&"initialStates".to_string()) {
Some(&Json::Array(ref xs)) => xs.iter().map(|s|
Some(match &s.get_str()[..] {
"PLAINTEXT state" => Plaintext,
"RAWTEXT state" => RawData(Rawtext),
"RCDATA state" => RawData(Rcdata),
s => panic!("don't know state {}", s),
})).collect(),
None => vec!(None),
_ => panic!("don't understand initialStates value"),
};
// Build the tests.
for state in state_overrides.into_iter() {
for &exact_errors in [false, true].iter() {
let mut newdesc = desc.clone();
match state {
Some(s) => newdesc = format!("{} (in state {:?})", newdesc, s),
None => (),
};
if exact_errors {
newdesc = format!("{} (exact errors)", newdesc);
}
let expect_toks = json_to_tokens(&expect, exact_errors);
tests.push(mk_test(newdesc, input.clone(), expect_toks, TokenizerOpts {
exact_errors: exact_errors,
initial_state: state,
last_start_tag_name: start_tag.clone(),
// Not discarding a BOM is what the test suite expects; see
// https://github.com/html5lib/html5lib-tests/issues/2
discard_bom: false,
.. Default::default()
}));
}
}
}
fn tests(src_dir: &Path) -> Vec<TestDescAndFn> {
let mut tests = vec!();
foreach_html5lib_test(src_dir, "tokenizer",
OsStr::new("test"), |path, mut file| {
let js = Json::from_reader(&mut file).ok().expect("json parse error");
match js.get_obj().get(&"tests".to_string()) {
Some(&Json::Array(ref lst)) => {
for test in lst.iter() {
mk_tests(&mut tests, path.file_name().unwrap().to_str().unwrap(), test);
}
}
// xmlViolation.test doesn't follow this format.
_ => (),
}
});
tests
}
#[start]
fn start(argc: isize, argv: *const *const u8) -> isize {
unsafe {
rt::args::init(argc, argv);
}
let args: Vec<_> = env::args().collect();
test::test_main(&args, tests(Path::new(env!("CARGO_MANIFEST_DIR"))));
0
}