Skip to content

Commit

Permalink
feat: piping syntax
Browse files Browse the repository at this point in the history
google/jsonnet#612
Signed-off-by: Yaroslav Bolyukin <iam@lach.pw>
  • Loading branch information
CertainLach committed Jan 29, 2023
1 parent 5acdbdb commit 0fa583a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
16 changes: 16 additions & 0 deletions crates/jrsonnet-evaluator/src/evaluate/mod.rs
Expand Up @@ -643,6 +643,22 @@ pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {

IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?
}
Pipe(value, mappers) => {
let mut value = evaluate(ctx.clone(), value)?;
for mapper in mappers {
let mapper_ctx = ctx.clone().with_var("_".into(), value.clone());
let mapper = evaluate(mapper_ctx, mapper)?;
value = match mapper {
Val::Null => value,
Val::Func(f) => {
let native = f.into_native::<((Val,), Val)>();
native(value)?
}
_ => mapper,
};
}
value
}
i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {
let Expr::Str(path) = &*path.0 else {
throw!("computed imports are not supported")
Expand Down
2 changes: 2 additions & 0 deletions crates/jrsonnet-parser/src/expr.rs
Expand Up @@ -410,6 +410,8 @@ pub enum Expr {
cond_else: Option<LocExpr>,
},
Slice(LocExpr, SliceDesc),
/// |> expr1 |> expr2
Pipe(LocExpr, Vec<LocExpr>),
}

/// file, begin offset, end offset
Expand Down
3 changes: 2 additions & 1 deletion crates/jrsonnet-parser/src/lib.rs
Expand Up @@ -288,13 +288,14 @@ parser! {
rule unaryop(x: rule<()>) -> ()
= quiet!{ x() } / expected!("<unary op>")


use BinaryOpType::*;
use UnaryOpType::*;
rule expr(s: &ParserSettings) -> LocExpr
= precedence! {
start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.source.clone(), start as u32, end as u32)) }
--
a:@ e:(_ "|>" _ e:expr(s) {e})+ {Expr::Pipe(a, e)}
--
a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}
--
a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}
Expand Down

0 comments on commit 0fa583a

Please sign in to comment.