forked from nix-community/nixpkgs-fmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern.rs
178 lines (160 loc) · 5.47 KB
/
pattern.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
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
//! This module defines `Pattern`: a predicate over syntax elements
use std::{
collections::{HashMap, HashSet},
fmt, iter, ops,
sync::Arc,
};
use rnix::{SyntaxElement, SyntaxKind};
/// A convenience function to convert something a pattern for use with `&` and
/// `|` operators
pub(crate) fn p(p: impl Into<Pattern>) -> Pattern {
p.into()
}
/// Pattern is boolean function on `SyntaxElement`.
///
/// It is like `Box<dyn Fn(SyntaxElement)> -> bool`, but with additional
/// convenience methods (for example, you can `&` two patterns). `Pattern` also
/// knows the set of node types which *could* match, which allows to implement
/// matching over a set of patterns efficiently.
///
/// Currently, we liberally box predicates inside of `Pattern`s, as there's only
/// a constant amount of patterns.
#[derive(Clone)]
pub(crate) struct Pattern {
kinds: Option<HashSet<SyntaxKind>>,
pred: Arc<dyn (Fn(&SyntaxElement) -> bool)>,
}
impl AsRef<Pattern> for Pattern {
fn as_ref(&self) -> &Pattern {
self
}
}
impl fmt::Debug for Pattern {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("Pattern { ... }")
}
}
impl Pattern {
fn new(
kinds: Option<HashSet<SyntaxKind>>,
pred: impl Fn(&SyntaxElement) -> bool + 'static,
) -> Pattern {
Pattern { kinds, pred: Arc::new(pred) }
}
fn filter_by_kind(kinds: impl Iterator<Item = SyntaxKind>) -> Pattern {
Pattern::new(Some(kinds.collect()), |_| true)
}
/// Creates a pattern which matches the same elements as `self` with the
/// additional constraint that their parent matches `parent`.
pub(crate) fn with_parent(self, parent: Pattern) -> Pattern {
let Pattern { kinds, pred } = self;
Pattern::new(kinds, move |element| {
(pred)(element) && element.parent().map(|it| parent.matches(&it.into())) == Some(true)
})
}
/// Checks if this pattern matches an element
pub(crate) fn matches(&self, element: &SyntaxElement) -> bool {
if let Some(kinds) = self.kinds.as_ref() {
if !kinds.contains(&element.kind()) {
return false;
}
}
(self.pred)(element)
}
}
/// `pat1 & pat2` operator
impl ops::BitAnd for Pattern {
type Output = Pattern;
fn bitand(self, other: Pattern) -> Pattern {
let kinds = match (self.kinds, other.kinds) {
(Some(lhs), Some(rhs)) => Some(lhs.intersection(&rhs).cloned().collect::<HashSet<_>>()),
(Some(it), None) | (None, Some(it)) => Some(it),
(None, None) => None,
};
let (p1, p2) = (self.pred, other.pred);
Pattern::new(kinds, move |element| p1(element) && p2(element))
}
}
/// `pat1 | pat2` operator
impl ops::BitOr for Pattern {
type Output = Pattern;
fn bitor(self, other: Pattern) -> Pattern {
let kinds = match (self.kinds, other.kinds) {
(Some(lhs), Some(rhs)) => Some(lhs.union(&rhs).cloned().collect::<HashSet<_>>()),
(Some(it), None) | (None, Some(it)) => Some(it),
(None, None) => None,
};
let (p1, p2) = (self.pred, other.pred);
Pattern::new(kinds, move |element| p1(element) || p2(element))
}
}
/// Construct pattern from closure.
impl<F> From<F> for Pattern
where
F: for<'a> Fn(&SyntaxElement) -> bool + 'static,
{
fn from(f: F) -> Pattern {
Pattern::new(None, f)
}
}
/// Construct pattern from a single `SyntaxKind`.
impl From<SyntaxKind> for Pattern {
fn from(kind: SyntaxKind) -> Pattern {
Pattern::filter_by_kind(iter::once(kind))
}
}
/// Construct pattern from several `SyntaxKind`s (using slices).
impl From<&'_ [SyntaxKind]> for Pattern {
fn from(kinds: &[SyntaxKind]) -> Pattern {
Pattern::filter_by_kind(kinds.iter().cloned())
}
}
/// Construct pattern from several `SyntaxKind`s (using arrays).
macro_rules! from_array {
($($arity:literal),*) => {$(
impl From<[SyntaxKind; $arity]> for Pattern {
fn from(kinds: [SyntaxKind; $arity]) -> Pattern {
Pattern::from(&kinds[..])
}
}
)*}
}
from_array!(0, 1, 2, 3, 4, 5, 6, 7, 8);
/// `PatternSet` allows to match many patterns at the same time efficiently.
///
/// This is generic over `P: AsRef<Pattern>`, so it works with any type which
/// contains a pattern.
pub(crate) struct PatternSet<P> {
by_kind: HashMap<SyntaxKind, Vec<P>>,
unconstrained: Vec<P>,
}
impl<'a, P: AsRef<Pattern>> PatternSet<&'a P> {
pub(crate) fn new(patterns: impl Iterator<Item = &'a P>) -> PatternSet<&'a P> {
let mut by_kind: HashMap<SyntaxKind, Vec<&'a P>> = HashMap::new();
let mut unconstrained = vec![];
patterns.for_each(|item| {
let pat: &Pattern = item.as_ref();
if let Some(kinds) = &pat.kinds {
for &kind in kinds {
by_kind.entry(kind).or_default().push(item)
}
} else {
unconstrained.push(item)
}
});
PatternSet { by_kind, unconstrained }
}
/// Returns an iterator of patterns that match
pub(crate) fn matching<'b>(
&'b self,
element: SyntaxElement,
) -> impl Iterator<Item = &'a P> + 'b {
self.by_kind
.get(&element.kind())
.into_iter()
.flat_map(|vec| vec.iter())
.chain(self.unconstrained.iter())
.copied()
.filter(move |p| p.as_ref().matches(&element))
}
}