use super::ast::Expr;
use std::ops::Range;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Selection {
pub name: String,
pub range: Range<usize>,
}
pub(super) fn selections(expr: &Expr, path: &[&str]) -> Vec<Selection> {
let mut found = Vec::new();
walk(expr, path, &mut Vec::new(), &mut found);
found.sort_by_key(|selection| selection.range.start);
found
}
fn is_path(expr: &Expr, path: &[&str], bound: &[String]) -> bool {
match (expr, path.split_last()) {
(Expr::Ident(name, _), Some((last, []))) => name == last && !bound.contains(name),
(Expr::Select(operand, field, _), Some((last, rest))) if !rest.is_empty() => {
field == last && is_path(operand, rest, bound)
}
_ => false,
}
}
fn walk(expr: &Expr, path: &[&str], bound: &mut Vec<String>, found: &mut Vec<Selection>) {
match expr {
Expr::Literal(..) | Expr::Ident(..) => {}
Expr::Select(operand, field, at) | Expr::Has(operand, field, _, at) => {
if is_path(operand, path, bound) {
found.push(Selection {
name: field.clone(),
range: *at..*at + field.chars().count(),
});
}
walk(operand, path, bound, found);
}
Expr::Unary(_, operand, _) => walk(operand, path, bound, found),
Expr::Index(left, right, _)
| Expr::Binary(_, left, right, _)
| Expr::And(left, right, _)
| Expr::Or(left, right, _) => {
walk(left, path, bound, found);
walk(right, path, bound, found);
}
Expr::Conditional(condition, then, otherwise, _) => {
for expr in [condition, then, otherwise] {
walk(expr, path, bound, found);
}
}
Expr::Call { target, args, .. } => {
for expr in target.iter().map(AsRef::as_ref).chain(args) {
walk(expr, path, bound, found);
}
}
Expr::List(items, _) => {
for item in items {
walk(item, path, bound, found);
}
}
Expr::Map(entries, _) => {
for (key, value) in entries {
walk(key, path, bound, found);
walk(value, path, bound, found);
}
}
Expr::Comprehension {
range,
var,
filter,
body,
..
} => {
walk(range, path, bound, found);
bound.push(var.clone());
for expr in filter.iter().map(AsRef::as_ref).chain([body.as_ref()]) {
walk(expr, path, bound, found);
}
bound.pop();
}
}
}
#[cfg(test)]
mod tests {
use crate::cel::Program;
fn selected(source: &str) -> Vec<(String, String)> {
let chars: Vec<char> = source.chars().collect();
Program::parse(source)
.expect("an expression")
.selections("md.doc")
.into_iter()
.map(|selection| {
let written: String = chars[selection.range.clone()].iter().collect();
(selection.name, written)
})
.collect()
}
fn names(source: &str) -> Vec<String> {
selected(source)
.into_iter()
.map(|(name, written)| {
assert_eq!(name, written, "the range covers the name");
name
})
.collect()
}
#[test]
fn each_field_selected_by_dot_with_its_range() {
assert_eq!(names("md.doc.price * 2 + md.doc.tax"), ["price", "tax"]);
assert_eq!(
names("has(md.doc.due) ? md.doc.due : md.today()"),
["due", "due"]
);
assert_eq!(
names("md.doc.title.size() > 0 && md.doc . note != ''"),
["title", "note"]
);
assert_eq!(
selected("'é' + md.doc.name"),
[("name".into(), "name".into())]
);
}
#[test]
fn what_isn_t_a_field_of_the_path_selected_by_dot_is_left_out() {
let none: [String; 0] = [];
assert_eq!(
names(r#""md.doc.a" + 'md.doc.b' + r"md.doc.c" + """md.doc.d""""#),
none
);
assert_eq!(
names("md.doc['a'] + md.data.b + string(md.doc) + string(md.doc.size())"),
none
);
assert_eq!(names("doc.a + x.md.doc.b"), none);
assert_eq!(names("[1].map(md, md.doc.a) + [md.doc.b]"), ["b"]);
}
}