Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move DocComment methods into docs.rs
- Loading branch information
Showing
3 changed files
with
134 additions
and
127 deletions.
There are no files selected for viewing
This file contains 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,127 @@ | ||
use pest::iterators::Pairs; | ||
use pest_meta::parser::Rule; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct DocComment { | ||
/// Multi-line grammar doc, (joined with `\n`) | ||
/// | ||
/// e.g. | ||
/// | ||
/// ```ignore | ||
/// "grammar doc 1\ngrammar doc 2" | ||
/// ``` | ||
pub 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" } | ||
/// ``` | ||
pub line_docs: HashMap<String, String>, | ||
} | ||
|
||
pub(crate) fn consume(pairs: Pairs<'_, Rule>) -> DocComment { | ||
let grammar_doc = consume_grammar_doc(pairs.clone()); | ||
let line_docs = consume_line_docs(pairs); | ||
|
||
DocComment { | ||
grammar_doc, | ||
line_docs, | ||
} | ||
} | ||
|
||
/// Consume grammar doc into String, multi-line joined with `\n` | ||
fn consume_grammar_doc(pairs: Pairs<'_, Rule>) -> String { | ||
let mut docs = vec![]; | ||
for pair in pairs { | ||
if pair.as_rule() == Rule::grammar_doc { | ||
let inner_doc = pair.into_inner().next().unwrap(); | ||
docs.push(inner_doc.as_str()); | ||
} | ||
} | ||
|
||
docs.join("\n") | ||
} | ||
|
||
/// Consume line docs into HashMap<rule_name, doc> | ||
/// | ||
/// Example a `test.pest`: | ||
/// | ||
/// ```ignore | ||
/// /// Line doc 1 | ||
/// foo = {} | ||
/// | ||
/// /// Line doc 2 | ||
/// /// Line doc 3 | ||
/// bar = {} | ||
/// ``` | ||
/// | ||
/// Will returns `{ "foo": "This is line comment", "bar": "Line doc 2\n/// Line doc 3" }` | ||
fn consume_line_docs(pairs: Pairs<'_, Rule>) -> HashMap<String, String> { | ||
let mut docs: HashMap<String, String> = HashMap::new(); | ||
let mut comments = vec![]; | ||
|
||
for pair in pairs { | ||
let rule = pair.as_rule(); | ||
|
||
if rule == 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() { | ||
comments.push(inner_doc.as_str()) | ||
} | ||
} | ||
Rule::identifier => { | ||
if !comments.is_empty() { | ||
let rule_name = inner.as_str().to_owned(); | ||
docs.insert(rule_name, comments.join("\n")); | ||
comments = vec![]; | ||
} | ||
} | ||
_ => (), | ||
} | ||
} | ||
} | ||
} | ||
|
||
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 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 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