Skip to content

Conversation

nicoburns
Copy link
Contributor

@nicoburns nicoburns commented Jun 30, 2025

Experimental (feedback very welcome):

Allows the match_token! procedural macro to be replaced with a series of simpler macro_rules! macros combined with a regular match expression.

The following macro:

//§ the-before-html-insertion-mode
InsertionMode::BeforeHtml => match_token!(token {
    Token::Characters(SplitStatus::NotSplit, text) => ProcessResult::SplitWhitespace(text),
    Token::Characters(SplitStatus::Whitespace, _) => ProcessResult::Done,
    Token::Comment(text) => self.append_comment_to_doc(text),

    tag @ <html> => {
        self.create_root(tag.attrs);
        self.mode.set(InsertionMode::BeforeHead);
        ProcessResult::Done
    }

    </head> </body> </html> </br> => else,

    tag @ </_> => self.unexpected(&tag),

    token => {
        self.create_root(vec!());
        ProcessResult::Reprocess(InsertionMode::BeforeHead, token)
    }
}),

would be replaced with:

//§ the-before-html-insertion-mode
InsertionMode::BeforeHtml => match token {
    Token::Characters(SplitStatus::NotSplit, text) => {
        ProcessResult::SplitWhitespace(text)
    },
    Token::Characters(SplitStatus::Whitespace, _) => ProcessResult::Done,
    Token::Comment(text) => self.append_comment_to_doc(text),

    // Token::Tag(tag @ tag!(["html"] | [/"body"])) => {
    tag_token!(tag @ ["html"] | [/"body"]) => {
        self.create_root(tag.attrs);
        self.mode.set(InsertionMode::BeforeHead);
        ProcessResult::Done
    },

    // Token::Tag(tag @ any_end_tag!()) if !matches!(tag, tag!([/"head"] | [/"body"] | [/"html"] | [/"br"])) =>
    any_end_tag_token!(tag) if is_not_tag!(tag, [/"head"] | [/"body"] | [/"html"] | [/"br"]) => {
        self.unexpected(&tag)
    },

    token => {
        self.create_root(vec!());
        ProcessResult::Reprocess(InsertionMode::BeforeHead, token)
    },
},

(commented out lines are an alternative variant that is also possible (would replace the line immediately below themselves))

@nicoburns nicoburns force-pushed the declarative_match_token branch from 62ef04d to 762a26e Compare June 30, 2025 12:49
@nicoburns nicoburns force-pushed the declarative_match_token branch from 762a26e to 1ead52f Compare June 30, 2025 12:50
Copy link
Contributor

@simonwuelker simonwuelker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea!

Having to write tag_token!(tag @ ["html"] | [/"body"]) is odd though.
Can't it be tag_token!(tag @ <html> | </body>) if the macro match arm is changed from (__inner:[/$tag:tt]) to something like (__inner:</$tag:ident>)?

You could convert the ident to a tt using the stringify macro and that would remove the need for quotes at the macro invocation site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants