Handle non simple refs in chained expressions (#182)

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2024-03-15 07:13:54 -07:00
committed by GitHub
parent 48982222c5
commit 7e3fc08a14
3 changed files with 22 additions and 9 deletions

View File

@@ -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 {

View File

@@ -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),
}
}
}

View File

@@ -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]