mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
Handle non simple refs in chained expressions (#182)
Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
committed by
GitHub
parent
48982222c5
commit
7e3fc08a14
@@ -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 {
|
||||
|
||||
@@ -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
tests/interpreter/cases/refr/tests.yaml
Normal file
14
tests/interpreter/cases/refr/tests.yaml
Normal 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]
|
||||
Reference in New Issue
Block a user