-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathast.rs
223 lines (203 loc) · 6.38 KB
/
ast.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use std::collections::HashSet;
use std::sync::Arc;
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum RelationOp {
LessThan,
LessThanEq,
GreaterThan,
GreaterThanEq,
Equals,
NotEquals,
In,
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum ArithmeticOp {
Add,
Subtract,
Divide,
Multiply,
Modulus,
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum UnaryOp {
Not,
DoubleNot,
Minus,
DoubleMinus,
}
#[derive(Debug, PartialEq, Clone)]
pub enum Expression {
Arithmetic(Box<Expression>, ArithmeticOp, Box<Expression>),
Relation(Box<Expression>, RelationOp, Box<Expression>),
Ternary(Box<Expression>, Box<Expression>, Box<Expression>),
Or(Box<Expression>, Box<Expression>),
And(Box<Expression>, Box<Expression>),
Unary(UnaryOp, Box<Expression>),
Member(Box<Expression>, Box<Member>),
FunctionCall(Box<Expression>, Option<Box<Expression>>, Vec<Expression>),
List(Vec<Expression>),
Map(Vec<(Expression, Expression)>),
Atom(Atom),
Ident(Arc<String>),
}
#[derive(Debug, PartialEq, Clone)]
pub enum Member {
Attribute(Arc<String>),
Index(Box<Expression>),
Fields(Vec<(Arc<String>, Expression)>),
}
#[derive(Debug, PartialEq, Clone)]
pub enum Atom {
Int(i64),
UInt(u64),
Float(f64),
String(Arc<String>),
Bytes(Arc<Vec<u8>>),
Bool(bool),
Null,
}
/// A collection of all the references that an expression makes to variables and functions.
pub struct ExpressionReferences<'expr> {
variables: HashSet<&'expr str>,
functions: HashSet<&'expr str>,
}
impl ExpressionReferences<'_> {
/// Returns true if the expression references the provided variable name.
///
/// # Example
/// ```rust
/// # use cel_parser::parse;
/// let expression = parse("foo.bar == true").unwrap();
/// let references = expression.references();
/// assert!(references.has_variable("foo"));
/// ```
pub fn has_variable(&self, name: impl AsRef<str>) -> bool {
self.variables.contains(name.as_ref())
}
/// Returns true if the expression references the provided variable name.
///
/// # Example
/// ```rust
/// # use cel_parser::parse;
/// let expression = parse("size(foo) > 0").unwrap();
/// let references = expression.references();
/// assert!(references.has_function("size"));
/// ```
pub fn has_function(&self, name: impl AsRef<str>) -> bool {
self.functions.contains(name.as_ref())
}
/// Returns a list of all variables referenced in the expression.
///
/// # Example
/// ```rust
/// # use cel_parser::parse;
/// let expression = parse("foo.bar == true").unwrap();
/// let references = expression.references();
/// assert_eq!(vec!["foo"], references.variables());
/// ```
pub fn variables(&self) -> Vec<&str> {
self.variables.iter().copied().collect()
}
/// Returns a list of all functions referenced in the expression.
///
/// # Example
/// ```rust
/// # use cel_parser::parse;
/// let expression = parse("size(foo) > 0").unwrap();
/// let references = expression.references();
/// assert_eq!(vec!["size"], references.functions());
/// ```
pub fn functions(&self) -> Vec<&str> {
self.functions.iter().copied().collect()
}
}
impl Expression {
/// Returns a set of all variables referenced in the expression. Variable identifiers
/// are represented internally as [`Arc<String>`] and this function simply clones those
/// references into a [`HashSet`].
///
/// # Example
/// ```rust
/// # use cel_parser::parse;
/// let expression = parse("foo && size(foo) > 0").unwrap();
/// let references = expression.references();
///
/// assert!(references.has_variable("foo"));
/// assert!(references.has_function("size"));
/// ```
pub fn references(&self) -> ExpressionReferences {
let mut variables = HashSet::new();
let mut functions = HashSet::new();
self._references(&mut variables, &mut functions);
ExpressionReferences {
variables,
functions,
}
}
/// Internal recursive function to collect all variable and function references in the expression.
fn _references<'expr>(
&'expr self,
variables: &mut HashSet<&'expr str>,
functions: &mut HashSet<&'expr str>,
) {
match self {
Expression::Arithmetic(e1, _, e2)
| Expression::Relation(e1, _, e2)
| Expression::Ternary(e1, _, e2)
| Expression::Or(e1, e2)
| Expression::And(e1, e2) => {
e1._references(variables, functions);
e2._references(variables, functions);
}
Expression::Unary(_, e) => {
e._references(variables, functions);
}
Expression::Member(e, _) => {
e._references(variables, functions);
}
Expression::FunctionCall(name, target, args) => {
if let Expression::Ident(v) = &**name {
functions.insert(v.as_str());
}
if let Some(target) = target {
target._references(variables, functions);
}
for e in args {
e._references(variables, functions);
}
}
Expression::List(e) => {
for e in e {
e._references(variables, functions);
}
}
Expression::Map(v) => {
for (e1, e2) in v {
e1._references(variables, functions);
e2._references(variables, functions);
}
}
Expression::Atom(_) => {}
Expression::Ident(v) => {
variables.insert(v.as_str());
}
}
}
}
#[cfg(test)]
mod tests {
use crate::parse;
#[test]
fn test_references() {
let expr =
parse("foo.bar.baz == true && size(foo) > 0 && foo[0] == 1 && bar.startsWith('a')")
.unwrap();
let refs = expr.references();
assert!(refs.has_variable("foo"));
assert!(refs.has_variable("bar"));
assert_eq!(refs.variables.len(), 2);
assert!(refs.has_function("size"));
assert!(refs.has_function("startsWith"));
assert_eq!(refs.functions.len(), 2);
}
}