difftreelog
refactor skip desugaring for in operator
in: master
3 files changed
crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -60,6 +60,8 @@
use BinaryOpType::*;
use Val::*;
Ok(match (a, op, b) {
+ (Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone(), true)),
+
(a, Add, b) => evaluate_add_op(a, b)?,
(a, Eq, b) => Bool(equals(a, b)?),
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth1use jrsonnet_gc::{unsafe_empty_trace, Finalize, Trace};2use jrsonnet_interner::IStr;3#[cfg(feature = "deserialize")]4use serde::Deserialize;5#[cfg(feature = "serialize")]6use serde::Serialize;7use std::{8 fmt::{Debug, Display},9 ops::Deref,10 path::{Path, PathBuf},11 rc::Rc,12};1314#[cfg_attr(feature = "serialize", derive(Serialize))]15#[cfg_attr(feature = "deserialize", derive(Deserialize))]16#[derive(Debug, PartialEq, Trace)]17#[trivially_drop]18pub enum FieldName {19 /// {fixed: 2}20 Fixed(IStr),21 /// {["dyn"+"amic"]: 3}22 Dyn(LocExpr),23}2425#[cfg_attr(feature = "serialize", derive(Serialize))]26#[cfg_attr(feature = "deserialize", derive(Deserialize))]27#[derive(Debug, Clone, Copy, PartialEq, Trace)]28#[trivially_drop]29pub enum Visibility {30 /// :31 Normal,32 /// ::33 Hidden,34 /// :::35 Unhide,36}3738impl Visibility {39 pub fn is_visible(&self) -> bool {40 matches!(self, Self::Normal | Self::Unhide)41 }42}4344#[cfg_attr(feature = "serialize", derive(Serialize))]45#[cfg_attr(feature = "deserialize", derive(Deserialize))]46#[derive(Clone, Debug, PartialEq, Trace)]47#[trivially_drop]48pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);4950#[cfg_attr(feature = "serialize", derive(Serialize))]51#[cfg_attr(feature = "deserialize", derive(Deserialize))]52#[derive(Debug, PartialEq, Trace)]53#[trivially_drop]54pub struct FieldMember {55 pub name: FieldName,56 pub plus: bool,57 pub params: Option<ParamsDesc>,58 pub visibility: Visibility,59 pub value: LocExpr,60}6162#[cfg_attr(feature = "serialize", derive(Serialize))]63#[cfg_attr(feature = "deserialize", derive(Deserialize))]64#[derive(Debug, PartialEq, Trace)]65#[trivially_drop]66pub enum Member {67 Field(FieldMember),68 BindStmt(BindSpec),69 AssertStmt(AssertStmt),70}7172#[cfg_attr(feature = "serialize", derive(Serialize))]73#[cfg_attr(feature = "deserialize", derive(Deserialize))]74#[derive(Debug, Clone, Copy, PartialEq, Trace)]75#[trivially_drop]76pub enum UnaryOpType {77 Plus,78 Minus,79 BitNot,80 Not,81}8283impl Display for UnaryOpType {84 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {85 use UnaryOpType::*;86 write!(87 f,88 "{}",89 match self {90 Plus => "+",91 Minus => "-",92 BitNot => "~",93 Not => "!",94 }95 )96 }97}9899#[cfg_attr(feature = "serialize", derive(Serialize))]100#[cfg_attr(feature = "deserialize", derive(Deserialize))]101#[derive(Debug, Clone, Copy, PartialEq, Trace)]102#[trivially_drop]103pub enum BinaryOpType {104 Mul,105 Div,106107 /// Implemented as intrinsic, put here for completeness108 Mod,109110 Add,111 Sub,112113 Lhs,114 Rhs,115116 Lt,117 Gt,118 Lte,119 Gte,120121 BitAnd,122 BitOr,123 BitXor,124125 Eq,126 Neq,127128 And,129 Or,130131 // Equialent to std.objectHasEx(a, b, true)132 In,133}134135impl Display for BinaryOpType {136 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {137 use BinaryOpType::*;138 write!(139 f,140 "{}",141 match self {142 Mul => "*",143 Div => "/",144 Mod => "%",145 Add => "+",146 Sub => "-",147 Lhs => "<<",148 Rhs => ">>",149 Lt => "<",150 Gt => ">",151 Lte => "<=",152 Gte => ">=",153 BitAnd => "&",154 BitOr => "|",155 BitXor => "^",156 Eq => "==",157 Neq => "!=",158 And => "&&",159 Or => "||",160 In => "in",161 }162 )163 }164}165166/// name, default value167#[cfg_attr(feature = "serialize", derive(Serialize))]168#[cfg_attr(feature = "deserialize", derive(Deserialize))]169#[derive(Debug, PartialEq, Trace)]170#[trivially_drop]171pub struct Param(pub IStr, pub Option<LocExpr>);172173/// Defined function parameters174#[cfg_attr(feature = "serialize", derive(Serialize))]175#[cfg_attr(feature = "deserialize", derive(Deserialize))]176#[derive(Debug, Clone, PartialEq)]177pub struct ParamsDesc(pub Rc<Vec<Param>>);178179/// Safety:180/// AST is acyclic, and there should be no gc pointers181unsafe impl Trace for ParamsDesc {182 unsafe_empty_trace!();183}184impl Finalize for ParamsDesc {}185186impl Deref for ParamsDesc {187 type Target = Vec<Param>;188 fn deref(&self) -> &Self::Target {189 &self.0190 }191}192193#[cfg_attr(feature = "serialize", derive(Serialize))]194#[cfg_attr(feature = "deserialize", derive(Deserialize))]195#[derive(Debug, PartialEq, Trace)]196#[trivially_drop]197pub struct Arg(pub Option<String>, pub LocExpr);198199#[cfg_attr(feature = "serialize", derive(Serialize))]200#[cfg_attr(feature = "deserialize", derive(Deserialize))]201#[derive(Debug, PartialEq, Trace)]202#[trivially_drop]203pub struct ArgsDesc(pub Vec<Arg>);204205impl Deref for ArgsDesc {206 type Target = Vec<Arg>;207 fn deref(&self) -> &Self::Target {208 &self.0209 }210}211212#[cfg_attr(feature = "serialize", derive(Serialize))]213#[cfg_attr(feature = "deserialize", derive(Deserialize))]214#[derive(Debug, Clone, PartialEq, Trace)]215#[trivially_drop]216pub struct BindSpec {217 pub name: IStr,218 pub params: Option<ParamsDesc>,219 pub value: LocExpr,220}221222#[cfg_attr(feature = "serialize", derive(Serialize))]223#[cfg_attr(feature = "deserialize", derive(Deserialize))]224#[derive(Debug, PartialEq, Trace)]225#[trivially_drop]226pub struct IfSpecData(pub LocExpr);227228#[cfg_attr(feature = "serialize", derive(Serialize))]229#[cfg_attr(feature = "deserialize", derive(Deserialize))]230#[derive(Debug, PartialEq, Trace)]231#[trivially_drop]232pub struct ForSpecData(pub IStr, pub LocExpr);233234#[cfg_attr(feature = "serialize", derive(Serialize))]235#[cfg_attr(feature = "deserialize", derive(Deserialize))]236#[derive(Debug, PartialEq, Trace)]237#[trivially_drop]238pub enum CompSpec {239 IfSpec(IfSpecData),240 ForSpec(ForSpecData),241}242243#[cfg_attr(feature = "serialize", derive(Serialize))]244#[cfg_attr(feature = "deserialize", derive(Deserialize))]245#[derive(Debug, PartialEq, Trace)]246#[trivially_drop]247pub struct ObjComp {248 pub pre_locals: Vec<BindSpec>,249 pub key: LocExpr,250 pub value: LocExpr,251 pub post_locals: Vec<BindSpec>,252 pub compspecs: Vec<CompSpec>,253}254255#[cfg_attr(feature = "serialize", derive(Serialize))]256#[cfg_attr(feature = "deserialize", derive(Deserialize))]257#[derive(Debug, PartialEq, Trace)]258#[trivially_drop]259pub enum ObjBody {260 MemberList(Vec<Member>),261 ObjComp(ObjComp),262}263264#[cfg_attr(feature = "serialize", derive(Serialize))]265#[cfg_attr(feature = "deserialize", derive(Deserialize))]266#[derive(Debug, PartialEq, Clone, Copy, Trace)]267#[trivially_drop]268pub enum LiteralType {269 This,270 Super,271 Dollar,272 Null,273 True,274 False,275}276277#[derive(Debug, PartialEq, Trace)]278#[trivially_drop]279pub struct SliceDesc {280 pub start: Option<LocExpr>,281 pub end: Option<LocExpr>,282 pub step: Option<LocExpr>,283}284285/// Syntax base286#[cfg_attr(feature = "serialize", derive(Serialize))]287#[cfg_attr(feature = "deserialize", derive(Deserialize))]288#[derive(Debug, PartialEq, Trace)]289#[trivially_drop]290pub enum Expr {291 Literal(LiteralType),292293 /// String value: "hello"294 Str(IStr),295 /// Number: 1, 2.0, 2e+20296 Num(f64),297 /// Variable name: test298 Var(IStr),299300 /// Array of expressions: [1, 2, "Hello"]301 Arr(Vec<LocExpr>),302 /// Array comprehension:303 /// ```jsonnet304 /// ingredients: [305 /// { kind: kind, qty: 4 / 3 }306 /// for kind in [307 /// 'Honey Syrup',308 /// 'Lemon Juice',309 /// 'Farmers Gin',310 /// ]311 /// ],312 /// ```313 ArrComp(LocExpr, Vec<CompSpec>),314315 /// Object: {a: 2}316 Obj(ObjBody),317 /// Object extension: var1 {b: 2}318 ObjExtend(LocExpr, ObjBody),319320 /// (obj)321 Parened(LocExpr),322323 /// -2324 UnaryOp(UnaryOpType, LocExpr),325 /// 2 - 2326 BinaryOp(LocExpr, BinaryOpType, LocExpr),327 /// assert 2 == 2 : "Math is broken"328 AssertExpr(AssertStmt, LocExpr),329 /// local a = 2; { b: a }330 LocalExpr(Vec<BindSpec>, LocExpr),331332 /// import "hello"333 Import(PathBuf),334 /// importStr "file.txt"335 ImportStr(PathBuf),336 /// error "I'm broken"337 ErrorStmt(LocExpr),338 /// a(b, c)339 Apply(LocExpr, ArgsDesc, bool),340 /// a[b]341 Index(LocExpr, LocExpr),342 /// function(x) x343 Function(ParamsDesc, LocExpr),344 /// std.primitiveEquals345 Intrinsic(IStr),346 /// if true == false then 1 else 2347 IfElse {348 cond: IfSpecData,349 cond_then: LocExpr,350 cond_else: Option<LocExpr>,351 },352}353354/// file, begin offset, end offset355#[cfg_attr(feature = "serialize", derive(Serialize))]356#[cfg_attr(feature = "deserialize", derive(Deserialize))]357#[derive(Clone, PartialEq, Trace)]358#[trivially_drop]359pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);360361impl Debug for ExprLocation {362 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {363 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)364 }365}366367/// Holds AST expression and its location in source file368#[cfg_attr(feature = "serialize", derive(Serialize))]369#[cfg_attr(feature = "deserialize", derive(Deserialize))]370#[derive(Clone, PartialEq)]371pub struct LocExpr(pub Rc<Expr>, pub Option<ExprLocation>);372/// Safety:373/// AST is acyclic, and there should be no gc pointers374unsafe impl Trace for LocExpr {375 unsafe_empty_trace!();376}377impl Finalize for LocExpr {}378379impl Debug for LocExpr {380 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {381 if f.alternate() {382 write!(f, "{:#?}", self.0)?;383 } else {384 write!(f, "{:?}", self.0)?;385 }386 if let Some(loc) = &self.1 {387 write!(f, " from {:?}", loc)?;388 }389 Ok(())390 }391}392393/// Creates LocExpr from Expr and ExprLocation components394#[macro_export]395macro_rules! loc_expr {396 ($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {397 LocExpr(398 std::rc::Rc::new($expr),399 if $need_loc {400 Some(ExprLocation($name, $start, $end))401 } else {402 None403 },404 )405 };406}407408/// Creates LocExpr without location info409#[macro_export]410macro_rules! loc_expr_todo {411 ($expr:expr) => {412 LocExpr(Rc::new($expr), None)413 };414}crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -217,38 +217,37 @@
rule unaryop(x: rule<()>) -> ()
= quiet!{ x() } / expected!("<unary op>")
+
+ use BinaryOpType::*;
rule expr(s: &ParserSettings) -> LocExpr
= start:position!() a:precedence! {
- a:(@) _ binop(<"||">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}
+ a:(@) _ binop(<"||">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Or, b))}
--
- a:(@) _ binop(<"&&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}
+ a:(@) _ binop(<"&&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, And, b))}
--
- a:(@) _ binop(<"|">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}
+ a:(@) _ binop(<"|">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BitOr, b))}
--
- a:@ _ binop(<"^">) _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}
+ a:@ _ binop(<"^">) _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BitXor, b))}
--
- a:(@) _ binop(<"&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}
+ a:(@) _ binop(<"&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BitAnd, b))}
--
- a:(@) _ binop(<"==">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Eq, b))}
- a:(@) _ binop(<"!=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Neq, b))}
+ a:(@) _ binop(<"==">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Eq, b))}
+ a:(@) _ binop(<"!=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Neq, b))}
--
- a:(@) _ binop(<"<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}
- a:(@) _ binop(<">">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}
- a:(@) _ binop(<"<=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}
- a:(@) _ binop(<">=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}
- a:(@) _ binop(<keyword("in")>) _ b:@ {loc_expr_todo!(Expr::Apply(
- el!(Expr::Intrinsic("objectHasEx".into())), ArgsDesc(vec![Arg(None, b), Arg(None, a), Arg(None, el!(Expr::Literal(LiteralType::True)))]),
- true
- ))}
+ a:(@) _ binop(<"<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Lt, b))}
+ a:(@) _ binop(<">">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Gt, b))}
+ a:(@) _ binop(<"<=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Lte, b))}
+ a:(@) _ binop(<">=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Gte, b))}
+ a:(@) _ binop(<keyword("in")>) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, In, b))}
--
- a:(@) _ binop(<"<<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}
- a:(@) _ binop(<">>">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}
+ a:(@) _ binop(<"<<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Lhs, b))}
+ a:(@) _ binop(<">>">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Rhs, b))}
--
- a:(@) _ binop(<"+">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}
- a:(@) _ binop(<"-">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}
+ a:(@) _ binop(<"+">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Add, b))}
+ a:(@) _ binop(<"-">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Sub, b))}
--
- a:(@) _ binop(<"*">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}
- a:(@) _ binop(<"/">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}
+ a:(@) _ binop(<"*">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Mul, b))}
+ a:(@) _ binop(<"/">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Div, b))}
a:(@) _ binop(<"%">) _ b:@ {loc_expr_todo!(Expr::Apply(
el!(Expr::Intrinsic("mod".into())), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),
false
@@ -306,6 +305,7 @@
use super::{expr::*, parse};
use crate::ParserSettings;
use std::path::PathBuf;
+ use BinaryOpType::*;
macro_rules! parse {
($s:expr) => {
@@ -326,10 +326,10 @@
pub fn basic_math() -> LocExpr {
el!(Expr::BinaryOp(
el!(Expr::Num(2.0)),
- BinaryOpType::Add,
+ Add,
el!(Expr::BinaryOp(
el!(Expr::Num(2.0)),
- BinaryOpType::Mul,
+ Mul,
el!(Expr::Num(2.0)),
)),
))
@@ -417,10 +417,10 @@
parse!("2+2*2"),
el!(Expr::BinaryOp(
el!(Expr::Num(2.0)),
- BinaryOpType::Add,
+ Add,
el!(Expr::BinaryOp(
el!(Expr::Num(2.0)),
- BinaryOpType::Mul,
+ Mul,
el!(Expr::Num(2.0))
))
))
@@ -438,7 +438,7 @@
parse!("2+(2+2*2)"),
el!(Expr::BinaryOp(
el!(Expr::Num(2.0)),
- BinaryOpType::Add,
+ Add,
el!(Expr::Parened(expressions::basic_math())),
))
);
@@ -451,10 +451,10 @@
parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),
el!(Expr::BinaryOp(
el!(Expr::Num(2.0)),
- BinaryOpType::Add,
+ Add,
el!(Expr::BinaryOp(
el!(Expr::Num(3.0)),
- BinaryOpType::Mul,
+ Mul,
el!(Expr::Num(4.0))
))
))
@@ -468,7 +468,7 @@
parse!("2/*\\*/+*/ - 22"),
el!(Expr::BinaryOp(
el!(Expr::Num(2.0)),
- BinaryOpType::Sub,
+ Sub,
el!(Expr::Num(22.0))
))
);
@@ -520,7 +520,7 @@
parse!("!a && !b"),
el!(BinaryOp(
el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),
- BinaryOpType::And,
+ And,
el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))
))
);
@@ -533,7 +533,7 @@
parse!("!a / !b"),
el!(BinaryOp(
el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),
- BinaryOpType::Div,
+ Div,
el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))
))
);