-
-
Notifications
You must be signed in to change notification settings - Fork 289
Add to support /// and //! syntax for add doc comment for rules.
#765
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
98ec963
Add to support `///` and `//!` syntax for add doc comment for rules.
huacnlee 65ffea6
Rewrite line_doc extract in pest_generator, avoid change AST.
huacnlee f14a957
Improve grammar_doc, line_doc parse for keep whitespaces in head and …
huacnlee 63985fc
Rewrite line_docs generator by use HashMap with rule_name.
huacnlee 279e18d
Move DocComment methods into docs.rs
huacnlee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| // option. All files in the project carrying such notice may not be copied, | ||
| // modified, or distributed except according to those terms. | ||
|
|
||
| use std::collections::HashMap; | ||
| use std::path::PathBuf; | ||
|
|
||
| use proc_macro2::TokenStream; | ||
|
|
@@ -17,12 +18,42 @@ use pest::unicode::unicode_property_names; | |
| use pest_meta::ast::*; | ||
| use pest_meta::optimizer::*; | ||
|
|
||
| pub fn generate( | ||
| #[derive(Debug)] | ||
| pub(crate) struct DocComment { | ||
| /// Multi-line grammar doc, (joined with `\n`) | ||
| /// | ||
| /// e.g. | ||
| /// | ||
| /// ```ignore | ||
| /// "grammar doc 1\ngrammar doc 2" | ||
| /// ``` | ||
| grammar_doc: String, | ||
| /// HashMap rule name and doc comments (joined with `\n`) | ||
| /// | ||
| /// e.g. | ||
| /// | ||
| /// ```ignore | ||
| /// { "foo": "line doc 1\nline doc 2", "bar": "line doc 3" } | ||
| /// ``` | ||
| line_docs: HashMap<String, String>, | ||
| } | ||
|
|
||
| impl DocComment { | ||
| pub fn new(grammar_doc: String, line_docs: HashMap<String, String>) -> Self { | ||
| Self { | ||
| grammar_doc, | ||
| line_docs, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn generate( | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This because of the So I changed it to |
||
| name: Ident, | ||
| generics: &Generics, | ||
| path: Option<PathBuf>, | ||
| rules: Vec<OptimizedRule>, | ||
| defaults: Vec<&str>, | ||
| doc_comment: &DocComment, | ||
| include_grammar: bool, | ||
| ) -> TokenStream { | ||
| let uses_eoi = defaults.iter().any(|name| *name == "EOI"); | ||
|
|
@@ -36,7 +67,7 @@ pub fn generate( | |
| } else { | ||
| quote!() | ||
| }; | ||
| let rule_enum = generate_enum(&rules, uses_eoi); | ||
| let rule_enum = generate_enum(&rules, doc_comment, uses_eoi); | ||
| let patterns = generate_patterns(&rules, uses_eoi); | ||
| let skip = generate_skip(&rules); | ||
|
|
||
|
|
@@ -181,10 +212,25 @@ fn generate_include(name: &Ident, path: &str) -> TokenStream { | |
| } | ||
| } | ||
|
|
||
| fn generate_enum(rules: &[OptimizedRule], uses_eoi: bool) -> TokenStream { | ||
| let rules = rules.iter().map(|rule| format_ident!("r#{}", rule.name)); | ||
| fn generate_enum(rules: &[OptimizedRule], doc_comment: &DocComment, uses_eoi: bool) -> TokenStream { | ||
| let rules = rules.iter().map(|rule| { | ||
| let rule_name = format_ident!("r#{}", rule.name); | ||
|
|
||
| match doc_comment.line_docs.get(&rule.name) { | ||
| Some(doc) => quote! { | ||
| #[doc = #doc] | ||
| #rule_name | ||
| }, | ||
| None => quote! { | ||
| #rule_name | ||
| }, | ||
| } | ||
| }); | ||
|
|
||
| let grammar_doc = &doc_comment.grammar_doc; | ||
| if uses_eoi { | ||
| quote! { | ||
| #[doc = #grammar_doc] | ||
| #[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)] | ||
| #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] | ||
| pub enum Rule { | ||
|
|
@@ -194,6 +240,7 @@ fn generate_enum(rules: &[OptimizedRule], uses_eoi: bool) -> TokenStream { | |
| } | ||
| } 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)] | ||
| pub enum Rule { | ||
|
|
@@ -208,6 +255,7 @@ fn generate_patterns(rules: &[OptimizedRule], uses_eoi: bool) -> TokenStream { | |
| .iter() | ||
| .map(|rule| { | ||
| let rule = format_ident!("r#{}", rule.name); | ||
|
|
||
| quote! { | ||
| Rule::#rule => rules::#rule(state) | ||
| } | ||
|
|
@@ -669,12 +717,22 @@ mod tests { | |
| 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: "Rule doc\nhello".to_owned(), | ||
| line_docs, | ||
| }; | ||
|
|
||
| assert_eq!( | ||
| generate_enum(&rules, false).to_string(), | ||
| generate_enum(&rules, doc_comment, false).to_string(), | ||
| quote! { | ||
| #[doc = "Rule doc\nhello"] | ||
| #[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 | ||
| } | ||
| } | ||
|
|
@@ -957,7 +1015,7 @@ mod tests { | |
| } | ||
|
|
||
| #[test] | ||
| fn generate_complete() { | ||
| fn test_generate_complete() { | ||
| let name = Ident::new("MyParser", Span::call_site()); | ||
| let generics = Generics::default(); | ||
|
|
||
|
|
@@ -974,22 +1032,32 @@ mod tests { | |
| }, | ||
| ]; | ||
|
|
||
| let mut line_docs = HashMap::new(); | ||
| line_docs.insert("if".to_owned(), "If statement".to_owned()); | ||
|
|
||
| let doc_comment = &DocComment { | ||
| line_docs, | ||
| grammar_doc: "This is Rule doc\nThis is second line".to_owned(), | ||
| }; | ||
|
|
||
| let defaults = vec!["ANY"]; | ||
| let result = result_type(); | ||
| let box_ty = box_type(); | ||
| let mut current_dir = std::env::current_dir().expect("Unable to get current directory"); | ||
| current_dir.push("test.pest"); | ||
| let test_path = current_dir.to_str().expect("path contains invalid unicode"); | ||
| assert_eq!( | ||
| generate(name, &generics, Some(PathBuf::from("test.pest")), rules, defaults, true).to_string(), | ||
| generate(name, &generics, Some(PathBuf::from("test.pest")), rules, defaults, doc_comment, true).to_string(), | ||
| quote! { | ||
| #[allow(non_upper_case_globals)] | ||
| const _PEST_GRAMMAR_MyParser: &'static str = include_str!(#test_path); | ||
|
|
||
| #[doc = "This is Rule doc\nThis is second line"] | ||
| #[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)] | ||
| #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] | ||
| pub enum Rule { | ||
| r#a, | ||
| #[doc = "If statement"] | ||
| r#if | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| //! A parser for JSON file. | ||
| //! And this is a example for JSON parser. | ||
| //! | ||
| //! indent-4-space | ||
|
|
||
| /// Matches foo str, e.g.: `foo` | ||
| foo = { "foo" } | ||
|
|
||
| /// Matches bar str, | ||
| /// Indent 2, e.g: `bar` or `foobar` | ||
|
|
||
| bar = { "bar" | "foobar" } | ||
|
|
||
| bar1 = { "bar1" } | ||
|
|
||
| /// Matches dar | ||
|
|
||
| /// Match dar description | ||
|
|
||
| dar = { "da" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.