Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pest_derive to generate a list of all rules in Rule::all_rules(). #927

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions generator/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn generate_enum(
uses_eoi: bool,
non_exhaustive: bool,
) -> TokenStream {
let rules = rules.iter().map(|rule| {
let rule_variants = rules.iter().map(|rule| {
let rule_name = format_ident!("r#{}", rule.name);

match doc_comment.line_docs.get(&rule.name) {
Expand Down Expand Up @@ -235,16 +235,30 @@ fn generate_enum(
result.append_all(quote! {
{
EOI,
#( #rules ),*
#( #rule_variants ),*
}
});
} else {
result.append_all(quote! {
{
#( #rules ),*
#( #rule_variants ),*
}
})
};

let rules = rules.iter().map(|rule| {
let rule_name = format_ident!("r#{}", rule.name);
quote! { #rule_name }
});

result.append_all(quote! {
impl Rule {
pub fn all_rules() -> &'static[Rule] {
&[ #(Rule::#rules), * ]
}
}
});

result
}

Expand Down Expand Up @@ -797,6 +811,11 @@ mod tests {
#[doc = "This is rule comment"]
r#f
}
impl Rule {
pub fn all_rules() -> &'static [Rule] {
&[Rule::r#f]
}
}
}
.to_string()
);
Expand Down Expand Up @@ -1128,6 +1147,11 @@ mod tests {
#[doc = "If statement"]
r#if
}
impl Rule {
pub fn all_rules() -> &'static [Rule] {
&[Rule::r#a, Rule::r#if]
}
}

#[allow(clippy::all)]
impl ::pest::Parser<Rule> for MyParser {
Expand Down