-
-
Notifications
You must be signed in to change notification settings - Fork 287
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 all 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 |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| use pest::iterators::Pairs; | ||
| use pest_meta::parser::Rule; | ||
| use std::collections::HashMap; | ||
|
|
||
| #[derive(Debug)] | ||
| pub(crate) struct DocComment { | ||
| pub grammar_doc: String, | ||
|
|
||
| /// HashMap for store all doc_comments for rules. | ||
| /// key is rule name, value is doc_comment. | ||
| pub line_docs: HashMap<String, String>, | ||
| } | ||
|
|
||
| /// Consume pairs to matches `Rule::grammar_doc`, `Rule::line_doc` into `DocComment` | ||
| /// | ||
| /// e.g. | ||
| /// | ||
| /// a pest file: | ||
| /// | ||
| /// ```ignore | ||
| /// //! This is a grammar doc | ||
| /// /// line doc 1 | ||
| /// /// line doc 2 | ||
| /// foo = {} | ||
| /// | ||
| /// /// line doc 3 | ||
| /// bar = {} | ||
| /// ``` | ||
| /// | ||
| /// Then will get: | ||
| /// | ||
| /// ```ignore | ||
| /// grammar_doc = "This is a grammar doc" | ||
| /// line_docs = { "foo": "line doc 1\nline doc 2", "bar": "line doc 3" } | ||
| /// ``` | ||
| pub(crate) fn consume(pairs: Pairs<'_, Rule>) -> DocComment { | ||
| let mut grammar_doc = String::new(); | ||
|
|
||
| let mut line_docs: HashMap<String, String> = HashMap::new(); | ||
| let mut line_doc = String::new(); | ||
|
|
||
| for pair in pairs { | ||
| match pair.as_rule() { | ||
| Rule::grammar_doc => { | ||
| // grammar_doc > inner_doc | ||
| let inner_doc = pair.into_inner().next().unwrap(); | ||
| grammar_doc.push_str(inner_doc.as_str()); | ||
| grammar_doc.push('\n'); | ||
| } | ||
| Rule::grammar_rule => { | ||
| if let Some(inner) = pair.into_inner().next() { | ||
| // grammar_rule > line_doc | identifier | ||
| match inner.as_rule() { | ||
| Rule::line_doc => { | ||
| if let Some(inner_doc) = inner.into_inner().next() { | ||
| line_doc.push_str(inner_doc.as_str()); | ||
| line_doc.push('\n'); | ||
| } | ||
| } | ||
| Rule::identifier => { | ||
| if !line_doc.is_empty() { | ||
| let rule_name = inner.as_str().to_owned(); | ||
|
|
||
| // Remove last \n | ||
| line_doc.pop(); | ||
| line_docs.insert(rule_name, line_doc.clone()); | ||
| line_doc.clear(); | ||
| } | ||
| } | ||
| _ => (), | ||
| } | ||
| } | ||
| } | ||
| _ => (), | ||
| } | ||
| } | ||
|
|
||
| if !grammar_doc.is_empty() { | ||
| // Remove last \n | ||
| grammar_doc.pop(); | ||
| } | ||
|
|
||
| DocComment { | ||
| grammar_doc, | ||
| line_docs, | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::collections::HashMap; | ||
|
|
||
| use pest_meta::parser; | ||
| use pest_meta::parser::Rule; | ||
|
|
||
| #[test] | ||
| fn test_doc_comment() { | ||
| let pairs = match parser::parse(Rule::grammar_rules, include_str!("../tests/test.pest")) { | ||
| Ok(pairs) => pairs, | ||
| Err(_) => panic!("error parsing tests/test.pest"), | ||
| }; | ||
|
|
||
| let doc_comment = super::consume(pairs); | ||
|
|
||
| let mut expected = HashMap::new(); | ||
| expected.insert("foo".to_owned(), "Matches foo str, e.g.: `foo`".to_owned()); | ||
| expected.insert( | ||
| "bar".to_owned(), | ||
| "Matches bar str,\n Indent 2, e.g: `bar` or `foobar`".to_owned(), | ||
| ); | ||
| expected.insert( | ||
| "dar".to_owned(), | ||
| "Matches dar\nMatch dar description".to_owned(), | ||
| ); | ||
| assert_eq!(expected, doc_comment.line_docs); | ||
|
|
||
| assert_eq!( | ||
| "A parser for JSON file.\nAnd this is a example for JSON parser.\n\n indent-4-space", | ||
| doc_comment.grammar_doc | ||
| ); | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This because of the
mod generatoris not public.So I changed it to
pub(crate).