Skip to content

Commit b8354e4

Browse files
author
bors-servo
authored
Auto merge of #217 - servo:diy-macros, r=nox
Use syn and quote crates 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. r? @nox <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/html5ever/217) <!-- Reviewable:end -->
2 parents 0bcf057 + dd9f2cc commit b8354e4

File tree

11 files changed

+772
-740
lines changed

11 files changed

+772
-740
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ harness = false
3333
[features]
3434
unstable = ["tendril/unstable", "string_cache/unstable"]
3535
heap_size = ["heapsize", "heapsize_plugin"]
36-
codegen = ["html5ever_macros"]
3736

3837
[dependencies]
3938
log = "0"
@@ -50,8 +49,9 @@ rustc-test = "0.1.3"
5049

5150
[build-dependencies]
5251
phf_codegen = "0.7.3"
52+
quote = "0.3"
5353
rustc-serialize = "0.3.15"
54-
html5ever_macros = { version = "0.2.6", path = "macros", optional = true }
54+
syn = { version = "0.9.1", features = ["full", "visit"]}
5555

5656
[profile.dev]
5757
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: 7 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
// except according to those terms.
99

1010
extern crate phf_codegen;
11+
#[macro_use] extern crate quote;
1112
extern crate rustc_serialize;
13+
extern crate syn;
1214

1315
use rustc_serialize::json::{Json, Decoder};
1416
use rustc_serialize::Decodable;
@@ -18,14 +20,16 @@ use std::fs::File;
1820
use std::io::Write;
1921
use std::path::Path;
2022

23+
#[path = "macros/match_token.rs"]
24+
mod match_token;
25+
2126
fn main() {
2227
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
2328

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

3034
named_entities_to_phf(
3135
&Path::new(&manifest_dir).join("data/entities.json"),
@@ -34,67 +38,6 @@ fn main() {
3438
println!("cargo:rerun-if-changed={}", rules_rs.display());
3539
}
3640

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-
9841
fn named_entities_to_phf(from: &Path, to: &Path) {
9942
// A struct matching the entries in entities.json.
10043
#[derive(RustcDecodable)]

macros/Cargo.toml

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

0 commit comments

Comments
 (0)