Skip to content

Commit

Permalink
Replace manual code generation with procedural macro (#567)
Browse files Browse the repository at this point in the history
* Replace manual code generation with procedural macros

This makes LSPs like rust-analyzer pick up the rules.rs
file, making it much easier to edit.

Signed-off-by: Simon Wülker <[email protected]>

* Format rules.rs

Signed-off-by: Simon Wülker <[email protected]>

---------

Signed-off-by: Simon Wülker <[email protected]>
  • Loading branch information
simonwuelker authored Jan 8, 2025
1 parent 8415d50 commit feced06
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 226 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ members = [
"markup5ever",
"html5ever",
"rcdom",
"xml5ever"
"xml5ever",
"match_token"
]

[workspace.dependencies]
match_token = { path = "match_token" }

resolver = "2"
7 changes: 1 addition & 6 deletions html5ever/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ license = "MIT OR Apache-2.0"
repository = "https://github.com/servo/html5ever"
description = "High-performance browser-grade HTML5 parser"
documentation = "https://docs.rs/html5ever"
build = "build.rs"
categories = [ "parser-implementations", "web-programming" ]
keywords = ["html", "html5", "parser", "parsing"]
edition = "2021"
Expand All @@ -19,16 +18,12 @@ trace_tokenizer = []
log = "0.4"
mac = "0.1"
markup5ever = { version = "0.14", path = "../markup5ever" }
match_token = { workspace = true }

[dev-dependencies]
criterion = "0.5"
typed-arena = "2.0.2"

[build-dependencies]
quote = "1"
syn = { version = "2", features = ["extra-traits", "full", "fold"] }
proc-macro2 = "1"

[[bench]]
name = "html5ever"
harness = false
39 changes: 0 additions & 39 deletions html5ever/build.rs

This file was deleted.

6 changes: 1 addition & 5 deletions html5ever/src/tree_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,9 @@ pub use self::PushFlag::*;
mod tag_sets;

mod data;
mod rules;
mod types;

#[allow(warnings)]
mod autogenerated {
include!(concat!(env!("OUT_DIR"), "/rules.rs"));
}

/// Tree builder options, with an impl for Default.
#[derive(Copy, Clone)]
pub struct TreeBuilderOpts {
Expand Down
16 changes: 6 additions & 10 deletions html5ever/src/tree_builder/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// The tree builder rules, as a single, enormous nested match expression.

use crate::interface::Quirks;
use crate::tokenizer::states::{Plaintext, Rawtext, Rcdata, ScriptData};
use crate::tokenizer::states::{Rawtext, Rcdata, ScriptData};
use crate::tokenizer::TagKind::{EndTag, StartTag};
use crate::tree_builder::tag_sets::*;
use crate::tree_builder::types::*;
Expand All @@ -19,12 +19,11 @@ use crate::tree_builder::{
TreeSink,
};
use crate::QualName;
use markup5ever::{expanded_name, local_name, namespace_prefix, namespace_url, ns};
use markup5ever::{expanded_name, local_name, namespace_url, ns};
use std::borrow::Cow::Borrowed;

use std::borrow::ToOwned;

use crate::tendril::SliceExt;
use match_token::match_token;

fn any_not_whitespace(x: &StrTendril) -> bool {
// FIXME: this might be much faster as a byte scan
Expand Down Expand Up @@ -421,12 +420,9 @@ where
}
}

match to_close {
Some(name) => {
self.generate_implied_end_except(name.clone());
self.expect_to_close(name);
}
None => (),
if let Some(name) = to_close {
self.generate_implied_end_except(name.clone());
self.expect_to_close(name);
}

self.close_p_element_in_button_scope();
Expand Down
12 changes: 12 additions & 0 deletions match_token/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "match_token"
version = "0.1.0"
edition = "2021"

[dependencies]
syn = "2"
quote = "1"
proc-macro2 = "1"

[lib]
proc-macro = true
Loading

0 comments on commit feced06

Please sign in to comment.