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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
use pest::error::{Error, ErrorVariant};
use pest::iterators::{Pair, Pairs};
use pest::Span;
pub use pest_consume_macros::{make_parser, match_inputs};
/// Carries a pest Pair alongside custom user data.
#[derive(Debug)]
pub struct ParseInput<'input, 'data, Rule, Data>
where
Rule: pest::RuleType,
{
pair: Pair<'input, Rule>,
user_data: &'data Data,
}
/// Iterator over `ParseInput`s. It is created by `ParseInput::children`.
#[derive(Debug)]
pub struct ParseInputs<'input, 'data, Rule, Data>
where
Rule: pest::RuleType,
{
pairs: Pairs<'input, Rule>,
span: Span<'input>,
user_data: &'data Data,
}
impl<'input, 'data, Rule, Data> ParseInput<'input, 'data, Rule, Data>
where
Rule: pest::RuleType,
{
pub fn new(pair: Pair<'input, Rule>, user_data: &'data Data) -> Self {
ParseInput { pair, user_data }
}
/// Create an error that points to the span of the input.
pub fn error(&self, message: String) -> Error<Rule> {
Error::new_from_span(
ErrorVariant::CustomError { message },
self.as_span(),
)
}
/// Reconstruct the input with a new pair, passing the user data along.
pub fn with_pair(&self, new_pair: Pair<'input, Rule>) -> Self {
ParseInput {
pair: new_pair,
user_data: self.user_data,
}
}
/// If the contained pair has exactly one child, return a new Self containing it.
pub fn single_child(&self) -> Option<Self> {
let mut children = self.pair.clone().into_inner();
if let Some(child) = children.next() {
if children.next().is_none() {
return Some(self.with_pair(child));
}
}
None
}
/// Return an iterator over the children of this input
// Can't use `-> impl Iterator` because of weird lifetime limitations
// (see https://github.com/rust-lang/rust/issues/61997).
pub fn children(&self) -> ParseInputs<'input, 'data, Rule, Data> {
ParseInputs {
pairs: self.as_pair().clone().into_inner(),
span: self.as_span(),
user_data: self.user_data(),
}
}
pub fn user_data(&self) -> &'data Data {
self.user_data
}
pub fn as_pair(&self) -> &Pair<'input, Rule> {
&self.pair
}
pub fn as_span(&self) -> Span<'input> {
self.pair.as_span()
}
pub fn as_str(&self) -> &'input str {
self.pair.as_str()
}
pub fn as_rule(&self) -> Rule {
self.pair.as_rule()
}
pub fn as_rule_alias<T>(&self) -> String
where
T: PestConsumer<Rule = Rule>,
{
T::rule_alias(self.as_rule())
}
}
impl<'input, 'data, Rule, Data> ParseInputs<'input, 'data, Rule, Data>
where
Rule: pest::RuleType,
{
/// `input` must be the _original_ input that `pairs` is pointing to.
pub fn new(
input: &'input str,
pairs: Pairs<'input, Rule>,
user_data: &'data Data,
) -> Self {
let span = Span::new(input, 0, input.len()).unwrap();
ParseInputs {
pairs,
span,
user_data,
}
}
/// Create an error that points to the span of the input.
pub fn error(&self, message: String) -> Error<Rule> {
Error::new_from_span(
ErrorVariant::CustomError { message },
self.span.clone(),
)
}
pub fn aliased_rules<T>(&self) -> Vec<String>
where
T: PestConsumer<Rule = Rule>,
{
self.clone().map(|p| p.as_rule_alias::<T>()).collect()
}
/// Reconstruct the input with a new pair, passing the user data along.
fn with_pair(
&self,
new_pair: Pair<'input, Rule>,
) -> ParseInput<'input, 'data, Rule, Data> {
ParseInput::new(new_pair, self.user_data)
}
}
/// Used by the macros.
pub trait PestConsumer {
type Rule: pest::RuleType;
fn rule_alias(rule: Self::Rule) -> String;
fn allows_shortcut(rule: Self::Rule) -> bool;
}
impl<'input, 'data, Rule, Data> Iterator
for ParseInputs<'input, 'data, Rule, Data>
where
Rule: pest::RuleType,
{
type Item = ParseInput<'input, 'data, Rule, Data>;
fn next(&mut self) -> Option<Self::Item> {
let child_pair = self.pairs.next()?;
let child = self.with_pair(child_pair);
Some(child)
}
}
impl<'input, 'data, Rule, Data> DoubleEndedIterator
for ParseInputs<'input, 'data, Rule, Data>
where
Rule: pest::RuleType,
{
fn next_back(&mut self) -> Option<Self::Item> {
let child_pair = self.pairs.next_back()?;
let child = self.with_pair(child_pair);
Some(child)
}
}
// Manual impl to avoid stupid `Data: Clone` trait bound
impl<'input, 'data, Rule, Data> Clone for ParseInput<'input, 'data, Rule, Data>
where
Rule: pest::RuleType,
{
fn clone(&self) -> Self {
ParseInput {
pair: self.pair.clone(),
user_data: self.user_data,
}
}
}
// Manual impl to avoid stupid `Data: Clone` trait bound
impl<'input, 'data, Rule, Data> Clone for ParseInputs<'input, 'data, Rule, Data>
where
Rule: pest::RuleType,
{
fn clone(&self) -> Self {
ParseInputs {
pairs: self.pairs.clone(),
span: self.span.clone(),
user_data: self.user_data,
}
}
}
|