-
-
Notifications
You must be signed in to change notification settings - Fork 252
/
docs.rs
127 lines (112 loc) · 3.48 KB
/
docs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
);
}
}