Skip to content

Commit e28e8a2

Browse files
committed
Use a hand-written parser a string formatting for the match_token! macro…
… instead of the parser and quasi-quoting from Rust’s (unstable) libsyntax. This has significantly worse diagnostics when encountering unexpected syntax (e.g. no indication of which line has the offending code) but this removes all usage of unstable compiler internals that constantly need to be fixed when updating the compiler. Fixes #216.
1 parent 5748ddf commit e28e8a2

File tree

10 files changed

+518
-738
lines changed

10 files changed

+518
-738
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ harness = false
3333
[features]
3434
unstable = ["tendril/unstable", "string_cache/unstable"]
3535
heap_size = ["heapsize", "heapsize_plugin"]
36-
codegen = ["html5ever_macros"]
36+
codegen = [] # Now unused, remove at the next breaking change
3737

3838
[dependencies]
3939
log = "0"
@@ -51,7 +51,6 @@ rustc-test = "0.1.3"
5151
[build-dependencies]
5252
phf_codegen = "0.7.3"
5353
rustc-serialize = "0.3.15"
54-
html5ever_macros = { version = "0.2.6", path = "macros", optional = true }
5554

5655
[profile.dev]
5756
debug = false

STRUCTURE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The module structure is also documented in the output produced by `cargo doc`, a
1414

1515
`dom_sink/`: Types that html5ever can use to represent the DOM, if you do not provide your own DOM implementation.
1616

17-
`macros/`: Rust syntax extensions used within html5ever. Users of the library do not need this crate.
17+
`macros/`: Code used at build-time to expand the `match_token!` "macro" in `src/tree_builder/rules.rs`.
1818

1919
`tests/`: Integration tests. This is a single executable crate that runs html5ever on the various [html5lib-tests](https://github.com/html5lib/html5lib-tests). There are also unit tests throughout the library code. See `README.md` for information on running tests.
2020

build.rs

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ use std::fs::File;
1818
use std::io::Write;
1919
use std::path::Path;
2020

21+
#[path = "macros/match_token.rs"]
22+
mod match_token;
23+
2124
fn main() {
2225
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
2326

2427
let rules_rs = Path::new(&manifest_dir).join("src/tree_builder/rules.rs");
25-
expand_match_tokens(
28+
match_token::expand_match_tokens(
2629
&rules_rs,
27-
// Keep the expanded file in the source directory, so that `cargo publish` ships it.
28-
&rules_rs.with_extension("expanded.rs"));
30+
&Path::new(&env::var("OUT_DIR").unwrap()).join("rules.rs"));
2931

3032
named_entities_to_phf(
3133
&Path::new(&manifest_dir).join("data/entities.json"),
@@ -34,67 +36,6 @@ fn main() {
3436
println!("cargo:rerun-if-changed={}", rules_rs.display());
3537
}
3638

37-
#[cfg(feature = "codegen")]
38-
fn expand_match_tokens(from: &Path, to: &Path) {
39-
extern crate html5ever_macros;
40-
41-
html5ever_macros::pre_expand(from, to);
42-
}
43-
44-
#[cfg(not(feature = "codegen"))]
45-
fn expand_match_tokens(from: &Path, to: &Path) {
46-
use std::io::stderr;
47-
use std::process::exit;
48-
49-
if let Err(error) = check_hash(from, to) {
50-
writeln!(
51-
stderr(),
52-
r"
53-
{} is missing or not up to date with {}:
54-
{}
55-
56-
Run `cargo build --features codegen` to update it.
57-
58-
If you’re using html5ever as a dependency, this is a bad release.
59-
Please file an issue at https://github.com/servo/html5ever/issues/new
60-
with the output of `cargo pkgid html5ever`.
61-
",
62-
to.file_name().unwrap().to_string_lossy(),
63-
from.file_name().unwrap().to_string_lossy(),
64-
error
65-
).unwrap();
66-
exit(1);
67-
}
68-
}
69-
70-
#[cfg(not(feature = "codegen"))]
71-
fn check_hash(from: &Path, to: &Path) -> Result<(), String> {
72-
use std::hash::{Hash, Hasher, SipHasher};
73-
use std::io::Read;
74-
75-
// Unwrap here as the source file is expected to exist.
76-
let mut file_from = File::open(from).unwrap();
77-
let mut source = String::new();
78-
let mut hasher = SipHasher::new();
79-
file_from.read_to_string(&mut source).unwrap();
80-
source.hash(&mut hasher);
81-
let source_hash = hasher.finish();
82-
83-
// IO errors from here indicate we need to regenerate the expanded file.
84-
let mut file_to = try!(File::open(to).map_err(|e| e.to_string()));
85-
let mut expanded = String::new();
86-
try!(file_to.read_to_string(&mut expanded).map_err(|e| e.to_string()));
87-
let prefix = "// source SipHash: ";
88-
let line = try!(expanded.lines().find(|line| line.starts_with(prefix))
89-
.ok_or("source hash not found".to_string()));
90-
let expected_hash = try!(line[prefix.len()..].parse::<u64>().map_err(|e| e.to_string()));
91-
if source_hash == expected_hash {
92-
Ok(())
93-
} else {
94-
Err("different hash".to_string())
95-
}
96-
}
97-
9839
fn named_entities_to_phf(from: &Path, to: &Path) {
9940
// A struct matching the entries in entities.json.
10041
#[derive(RustcDecodable)]

macros/Cargo.toml

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)