diff --git a/generator/src/generator.rs b/generator/src/generator.rs index 042b2ff9..fd48ec9d 100644 --- a/generator/src/generator.rs +++ b/generator/src/generator.rs @@ -223,10 +223,17 @@ fn generate_enum( }); let grammar_doc = &doc_comment.grammar_doc; - let mut result = quote! { - #[doc = #grammar_doc] - #[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)] - #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] + let mut result = if grammar_doc.is_empty() { + quote! { + #[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)] + #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] + } + } else { + quote! { + #[doc = #grammar_doc] + #[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)] + #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] + } }; if non_exhaustive { result.append_all(quote! { @@ -865,6 +872,41 @@ mod tests { ); } + #[test] + fn rule_empty_doc() { + let rules = vec![OptimizedRule { + name: "f".to_owned(), + ty: RuleType::Normal, + expr: OptimizedExpr::Ident("g".to_owned()), + }]; + + let mut line_docs = HashMap::new(); + line_docs.insert("f".to_owned(), "This is rule comment".to_owned()); + + let doc_comment = &DocComment { + grammar_doc: "".to_owned(), + line_docs, + }; + + assert_eq!( + generate_enum(&rules, doc_comment, false, false).to_string(), + quote! { + #[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)] + #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] + pub enum Rule { + #[doc = "This is rule comment"] + r#f + } + impl Rule { + pub fn all_rules() -> &'static [Rule] { + &[Rule::r#f] + } + } + } + .to_string() + ); + } + #[test] fn sequence() { let expr = OptimizedExpr::Seq(