Skip to content

Commit 762a26e

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

File tree

1 file changed

+94
-16
lines changed

1 file changed

+94
-16
lines changed

html5ever/src/tree_builder/rules.rs

Lines changed: 94 additions & 16 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 => {
78156
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 {

0 commit comments

Comments
 (0)