Skip to content

Commit 62ef04d

Browse files
committed
Replace match_token with macro_rules macros
Signed-off-by: Nico Burns <[email protected]>
1 parent a7c9d98 commit 62ef04d

File tree

1 file changed

+148
-41
lines changed

1 file changed

+148
-41
lines changed

html5ever/src/tree_builder/rules.rs

Lines changed: 148 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,59 @@ fn current_node<Handle>(open_elems: &[Handle]) -> &Handle {
3434
open_elems.last().expect("no current element")
3535
}
3636

37+
macro_rules! tag {
38+
($( $tag:tt )|+) => {
39+
$(tag!(__inner:$tag))|+
40+
};
41+
// Named end tag
42+
(__inner:[/$tag:tt]) => {
43+
crate::tokenizer::Tag { kind: crate::tokenizer::EndTag, name: local_name!($tag), .. }
44+
};
45+
// Named start tag
46+
(__inner:[$tag:tt]) => {
47+
crate::tokenizer::Tag { kind: crate::tokenizer::StartTag, name: local_name!($tag), .. }
48+
};
49+
}
50+
51+
macro_rules! is_not_tag {
52+
($input:ident, $( $tag:tt )|+) => {
53+
!matches!($input, $(tag!(__inner:$tag))|+)
54+
};
55+
}
56+
57+
macro_rules! tag_token {
58+
($id:ident @ $( $tag:tt )|+) => {
59+
crate::tree_builder::types::Token::Tag(
60+
$id @ ( tag!($($tag)|+) )
61+
)
62+
};
63+
($($tag:tt)|+) => {
64+
crate::tree_builder::types::Token::Tag(
65+
tag!($($tag)|+)
66+
)
67+
};
68+
}
69+
70+
macro_rules! any_end_tag {
71+
() => {
72+
crate::tokenizer::Tag {
73+
kind: crate::tokenizer::EndTag,
74+
..
75+
}
76+
};
77+
}
78+
79+
macro_rules! any_end_tag_token {
80+
() => {
81+
any_end_tag_token!(_)
82+
};
83+
($tag:ident) => {
84+
crate::tree_builder::types::Token::Tag(
85+
$tag @ any_end_tag!()
86+
)
87+
};
88+
}
89+
3790
#[doc(hidden)]
3891
impl<Handle, Sink> TreeBuilder<Handle, Sink>
3992
where
@@ -45,8 +98,10 @@ where
4598

4699
match mode {
47100
//§ the-initial-insertion-mode
48-
InsertionMode::Initial => match_token!(token {
49-
Token::Characters(SplitStatus::NotSplit, text) => ProcessResult::SplitWhitespace(text),
101+
InsertionMode::Initial => match token {
102+
Token::Characters(SplitStatus::NotSplit, text) => {
103+
ProcessResult::SplitWhitespace(text)
104+
},
50105
Token::Characters(SplitStatus::Whitespace, _) => ProcessResult::Done,
51106
Token::Comment(text) => self.append_comment_to_doc(text),
52107
token => {
@@ -55,30 +110,53 @@ where
55110
self.set_quirks_mode(Quirks);
56111
}
57112
ProcessResult::Reprocess(InsertionMode::BeforeHtml, token)
58-
}
59-
}),
113+
},
114+
},
60115

61116
//§ the-before-html-insertion-mode
62-
InsertionMode::BeforeHtml => match_token!(token {
63-
Token::Characters(SplitStatus::NotSplit, text) => ProcessResult::SplitWhitespace(text),
117+
// InsertionMode::BeforeHtml => match_token!(token {
118+
// Token::Characters(SplitStatus::NotSplit, text) => ProcessResult::SplitWhitespace(text),
119+
// Token::Characters(SplitStatus::Whitespace, _) => ProcessResult::Done,
120+
// Token::Comment(text) => self.append_comment_to_doc(text),
121+
122+
// tag @ <html> => {
123+
// self.create_root(tag.attrs);
124+
// self.mode.set(InsertionMode::BeforeHead);
125+
// ProcessResult::Done
126+
// }
127+
128+
// </head> </body> </html> </br> => else,
129+
130+
// tag @ </_> => self.unexpected(&tag),
131+
132+
// token => {
133+
// self.create_root(vec!());
134+
// ProcessResult::Reprocess(InsertionMode::BeforeHead, token)
135+
// }
136+
// }),
137+
138+
//§ the-before-html-insertion-mode
139+
InsertionMode::BeforeHtml => match token {
140+
Token::Characters(SplitStatus::NotSplit, text) => {
141+
ProcessResult::SplitWhitespace(text)
142+
},
64143
Token::Characters(SplitStatus::Whitespace, _) => ProcessResult::Done,
65144
Token::Comment(text) => self.append_comment_to_doc(text),
66-
67-
tag @ <html> => {
145+
// Token::Tag(tag @ tag!(["html"] | [/"body"])) => {
146+
tag_token!(tag @ ["html"] | [/"body"]) => {
68147
self.create_root(tag.attrs);
69148
self.mode.set(InsertionMode::BeforeHead);
70149
ProcessResult::Done
71-
}
72-
73-
</head> </body> </html> </br> => else,
74-
75-
tag @ </_> => self.unexpected(&tag),
76-
150+
},
151+
// Token::Tag(tag @ any_end_tag!()) if !matches!(tag, tag!([/"head"] | [/"body"] | [/"html"] | [/"br"])) =>
152+
any_end_tag_token!(tag) if is_not_tag!(tag, [/"head"] | [/"body"] | [/"html"] | [/"br"]) => {
153+
self.unexpected(&tag)
154+
},
77155
token => {
78-
self.create_root(vec!());
156+
self.create_root(vec![]);
79157
ProcessResult::Reprocess(InsertionMode::BeforeHead, token)
80-
}
81-
}),
158+
},
159+
},
82160

83161
//§ the-before-head-insertion-mode
84162
InsertionMode::BeforeHead => match_token!(token {
@@ -949,43 +1027,72 @@ where
9491027
}),
9501028

9511029
//§ parsing-main-incolgroup
952-
InsertionMode::InColumnGroup => match_token!(token {
953-
Token::Characters(SplitStatus::NotSplit, text) => ProcessResult::SplitWhitespace(text),
1030+
InsertionMode::InColumnGroup => match token {
1031+
Token::Characters(SplitStatus::NotSplit, text) => {
1032+
ProcessResult::SplitWhitespace(text)
1033+
},
9541034
Token::Characters(SplitStatus::Whitespace, text) => self.append_text(text),
9551035
Token::Comment(text) => self.append_comment(text),
956-
957-
<html> => self.step(InsertionMode::InBody, token),
958-
959-
tag @ <col> => {
1036+
crate::tree_builder::types::Token::Tag(crate::tokenizer::Tag {
1037+
kind: crate::tokenizer::StartTag,
1038+
name: local_name!("html"),
1039+
..
1040+
}) => self.step(InsertionMode::InBody, token),
1041+
crate::tree_builder::types::Token::Tag(
1042+
tag @ crate::tokenizer::Tag {
1043+
kind: crate::tokenizer::StartTag,
1044+
name: local_name!("col"),
1045+
..
1046+
},
1047+
) => {
9601048
self.insert_and_pop_element_for(tag);
9611049
ProcessResult::DoneAckSelfClosing
962-
}
963-
964-
</colgroup> => {
1050+
},
1051+
crate::tree_builder::types::Token::Tag(crate::tokenizer::Tag {
1052+
kind: crate::tokenizer::EndTag,
1053+
name: local_name!("colgroup"),
1054+
..
1055+
}) => {
9651056
if self.current_node_named(local_name!("colgroup")) {
9661057
self.pop();
9671058
self.mode.set(InsertionMode::InTable);
9681059
} else {
9691060
self.unexpected(&token);
9701061
}
9711062
ProcessResult::Done
972-
}
973-
974-
</col> => self.unexpected(&token),
975-
976-
<template> </template> => self.step(InsertionMode::InHead, token),
977-
1063+
},
1064+
crate::tree_builder::types::Token::Tag(crate::tokenizer::Tag {
1065+
kind: crate::tokenizer::EndTag,
1066+
name: local_name!("col"),
1067+
..
1068+
}) => self.unexpected(&token),
1069+
crate::tree_builder::types::Token::Tag(crate::tokenizer::Tag {
1070+
kind: crate::tokenizer::StartTag,
1071+
name: local_name!("template"),
1072+
..
1073+
})
1074+
| crate::tree_builder::types::Token::Tag(crate::tokenizer::Tag {
1075+
kind: crate::tokenizer::EndTag,
1076+
name: local_name!("template"),
1077+
..
1078+
}) => self.step(InsertionMode::InHead, token),
9781079
Token::Eof => self.step(InsertionMode::InBody, token),
979-
980-
token => {
981-
if self.current_node_named(local_name!("colgroup")) {
982-
self.pop();
983-
ProcessResult::Reprocess(InsertionMode::InTable, token)
984-
} else {
985-
self.unexpected(&token)
1080+
last_arm_token => {
1081+
let enable_wildcards = match last_arm_token {
1082+
_ => true,
1083+
};
1084+
match (enable_wildcards, last_arm_token) {
1085+
(_, token) => {
1086+
if self.current_node_named(local_name!("colgroup")) {
1087+
self.pop();
1088+
ProcessResult::Reprocess(InsertionMode::InTable, token)
1089+
} else {
1090+
self.unexpected(&token)
1091+
}
1092+
},
9861093
}
987-
}
988-
}),
1094+
},
1095+
},
9891096

9901097
//§ parsing-main-intbody
9911098
InsertionMode::InTableBody => match_token!(token {

0 commit comments

Comments
 (0)