Skip to content

Commit

Permalink
Handle non simple refs in chained expressions (#182)
Browse files Browse the repository at this point in the history
Signed-off-by: Anand Krishnamoorthi <[email protected]>
  • Loading branch information
anakrish authored Mar 15, 2024
1 parent 4898222 commit 7e3fc08
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
14 changes: 6 additions & 8 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2926,25 +2926,23 @@ impl Interpreter {
pub fn get_path_string(refr: &Expr, document: Option<&str>) -> Result<String> {
let mut comps = vec![];
let mut expr = Some(refr);
while expr.is_some() {
match expr {
Some(Expr::RefDot { refr, field, .. }) => {
while let Some(e) = expr {
match e {
Expr::RefDot { refr, field, .. } => {
comps.push(field.text());
expr = Some(refr);
}
Some(Expr::RefBrack { refr, index, .. })
if matches!(index.as_ref(), Expr::String(_)) =>
{
Expr::RefBrack { refr, index, .. } if matches!(index.as_ref(), Expr::String(_)) => {
if let Expr::String(s) = index.as_ref() {
comps.push(s.text());
expr = Some(refr);
}
}
Some(Expr::Var(v)) => {
Expr::Var(v) => {
comps.push(v.text());
expr = None;
}
_ => bail!(format!("internal error: not a simplee ref {expr:?}")),
_ => bail!(e.span().error("invalid ref expression")),
}
}
if let Some(d) = document {
Expand Down
3 changes: 2 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,12 @@ pub fn gather_functions(modules: &[Ref<Module>]) -> Result<FunctionTable> {
}

pub fn get_root_var(mut expr: &Expr) -> Result<SourceStr> {
let empty = expr.span().source_str().clone_empty();
loop {
match expr {
Expr::Var(v) => return Ok(v.source_str()),
Expr::RefDot { refr, .. } | Expr::RefBrack { refr, .. } => expr = refr,
_ => bail!("internal error: analyzer: could not get rule prefix"),
_ => return Ok(empty),
}
}
}
14 changes: 14 additions & 0 deletions tests/interpreter/cases/refr/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
cases:
- note: ref
modules:
- |
package test
x = [k |
[1, 2, 3][[1, 2, 3][k]]
]
query: data.test
want_result:
x: [0, 1]

0 comments on commit 7e3fc08

Please sign in to comment.