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
use predicates::{
reflection::{Case, PredicateReflection, Product},
Predicate,
};
use std::{fmt, iter};
use crate::{Captured, CapturedSpan};
pub fn parent<P>(matches: P) -> ParentPredicate<P>
where
P: for<'a> Predicate<CapturedSpan<'a>>,
{
ParentPredicate { matches }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParentPredicate<P> {
matches: P,
}
impl_bool_ops!(ParentPredicate<P>);
impl<P> fmt::Display for ParentPredicate<P>
where
P: for<'a> Predicate<CapturedSpan<'a>>,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "parent({})", self.matches)
}
}
impl<P> PredicateReflection for ParentPredicate<P> where P: for<'a> Predicate<CapturedSpan<'a>> {}
impl<'a, P, T> Predicate<T> for ParentPredicate<P>
where
T: Captured<'a>,
P: for<'p> Predicate<CapturedSpan<'p>>,
{
fn eval(&self, variable: &T) -> bool {
let parent = variable.parent();
parent.map_or(false, |parent| self.matches.eval(&parent))
}
fn find_case(&self, expected: bool, variable: &T) -> Option<Case<'_>> {
let parent = variable.parent();
let parent = if let Some(parent) = parent {
parent
} else {
return if expected {
None } else {
let product = Product::new("parent", "None");
Some(Case::new(Some(self), expected).add_product(product))
};
};
let child = self.matches.find_case(expected, &parent)?;
Some(Case::new(Some(self), expected).add_child(child))
}
}
pub fn ancestor<P>(matches: P) -> AncestorPredicate<P>
where
P: for<'a> Predicate<CapturedSpan<'a>>,
{
AncestorPredicate { matches }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AncestorPredicate<P> {
matches: P,
}
impl_bool_ops!(AncestorPredicate<P>);
impl<P> fmt::Display for AncestorPredicate<P>
where
P: for<'a> Predicate<CapturedSpan<'a>>,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "ancestor({})", self.matches)
}
}
impl<P> PredicateReflection for AncestorPredicate<P> where P: for<'a> Predicate<CapturedSpan<'a>> {}
impl<'a, P, T> Predicate<T> for AncestorPredicate<P>
where
T: Captured<'a>,
P: for<'p> Predicate<CapturedSpan<'p>>,
{
fn eval(&self, variable: &T) -> bool {
let mut ancestors = iter::successors(variable.parent(), CapturedSpan::parent);
ancestors.any(|span| self.matches.eval(&span))
}
fn find_case(&self, expected: bool, variable: &T) -> Option<Case<'_>> {
let mut ancestors = iter::successors(variable.parent(), CapturedSpan::parent);
if expected {
let child = ancestors.find_map(|span| self.matches.find_case(expected, &span))?;
Some(Case::new(Some(self), expected).add_child(child))
} else {
let case = Case::new(Some(self), expected);
ancestors.try_fold(case, |case, span| {
let child = self.matches.find_case(expected, &span)?;
Some(case.add_child(child))
})
}
}
}