difftreelog
feat breakpoints
in: master
9 files changed
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -1,5 +1,6 @@
use crate::error::Error::*;
use crate::error::Result;
+use crate::push_frame;
use crate::{throw, Val};
#[derive(PartialEq, Clone, Copy)]
@@ -102,12 +103,13 @@
buf.push_str(cur_padding);
escape_string_json_buf(&field, buf);
buf.push_str(": ");
- crate::push(
+ push_frame(
None,
|| format!("field <{}> manifestification", field.clone()),
|| {
let value = obj.get(field.clone())?.unwrap();
- manifest_json_ex_buf(&value, buf, cur_padding, options)
+ manifest_json_ex_buf(&value, buf, cur_padding, options)?;
+ Ok(Val::Null)
},
)?;
}
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -3,8 +3,8 @@
equals,
error::{Error::*, Result},
operator::evaluate_mod_op,
- parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, EvaluationState,
- FuncVal, IndexableVal, LazyVal, Val,
+ parse_args, primitive_equals, push_frame, throw, with_state, ArrValue, Context,
+ EvaluationState, FuncVal, IndexableVal, LazyVal, Val,
};
use format::{format_arr, format_obj};
use jrsonnet_gc::Gc;
@@ -23,7 +23,7 @@
pub mod sort;
pub fn std_format(str: IStr, vals: Val) -> Result<Val> {
- push(
+ push_frame(
Some(&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),
|| format!("std.format of {}", str),
|| {
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -1,7 +1,4 @@
-use crate::{
- builtin::{format::FormatError, sort::SortError},
- typed::TypeLocError,
-};
+use crate::{Val, builtin::{format::FormatError, sort::SortError}, typed::TypeLocError};
use jrsonnet_gc::Trace;
use jrsonnet_interner::IStr;
use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -2,7 +2,7 @@
builtin::std_slice,
error::Error::*,
evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
- push, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,
+ push_frame, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,
FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder, ObjectAssertion,
Result, Val,
};
@@ -464,7 +464,7 @@
if tailstrict {
body()?
} else {
- push(loc, || format!("function <{}> call", f.name()), body)?
+ push_frame(loc, || format!("function <{}> call", f.name()), body)?
}
}
v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),
@@ -474,7 +474,7 @@
pub fn evaluate_assert(context: Context, assertion: &AssertStmt) -> Result<()> {
let value = &assertion.0;
let msg = &assertion.1;
- let assertion_result = push(
+ let assertion_result = push_frame(
value.1.as_ref(),
|| "assertion condition".to_owned(),
|| {
@@ -483,7 +483,7 @@
},
)?;
if !assertion_result {
- push(
+ push_frame(
value.1.as_ref(),
|| "assertion failure".to_owned(),
|| {
@@ -510,6 +510,7 @@
pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {
use Expr::*;
let LocExpr(expr, loc) = expr;
+ // let bp = with_state(|s| s.0.stop_at.borrow().clone());
Ok(match &**expr {
Literal(LiteralType::This) => {
Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
@@ -532,7 +533,7 @@
Num(v) => Val::new_checked_num(*v)?,
BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,
UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,
- Var(name) => push(
+ Var(name) => push_frame(
loc.as_ref(),
|| format!("variable <{}>", name),
|| context.binding(name.clone())?.evaluate(),
@@ -541,7 +542,7 @@
match (evaluate(context.clone(), value)?, evaluate(context, index)?) {
(Val::Obj(v), Val::Str(s)) => {
let sn = s.clone();
- push(
+ push_frame(
loc.as_ref(),
|| format!("field <{}> access", sn),
|| {
@@ -652,7 +653,7 @@
evaluate_assert(context.clone(), assert)?;
evaluate(context, returned)?
}
- ErrorStmt(e) => push(
+ ErrorStmt(e) => push_frame(
loc.as_ref(),
|| "error statement".to_owned(),
|| {
@@ -666,7 +667,7 @@
cond_then,
cond_else,
} => {
- if push(
+ if push_frame(
loc.as_ref(),
|| "if condition".to_owned(),
|| evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"),
@@ -708,7 +709,7 @@
.0;
let mut import_location = tmp.to_path_buf();
import_location.pop();
- push(
+ push_frame(
loc.as_ref(),
|| format!("import {:?}", path),
|| with_state(|s| s.import_file(&import_location, path)),
crates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -243,7 +243,7 @@
($ctx: expr, $fn_name: expr, $args: expr, $total_args: expr, [
$($id: expr, $name: ident: $ty: expr $(=>$match: path)?);+ $(;)?
], $handler:block) => {{
- use $crate::{error::Error::*, throw, evaluate, push_stack_frame, typed::CheckType};
+ use $crate::{error::Error::*, throw, evaluate, push_frame, typed::CheckType};
let args = $args;
if args.unnamed.len() + args.named.len() > $total_args {
@@ -263,7 +263,7 @@
} else {
&$args.unnamed[$id]
};
- let $name = push_stack_frame(None, || format!("evaluating argument"), || {
+ let $name = push_frame(None, || format!("evaluating argument"), || {
let value = evaluate($ctx.clone(), &$name)?;
$ty.check(&value)?;
Ok(value)
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -40,7 +40,7 @@
path::{Path, PathBuf},
rc::Rc,
};
-use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};
+use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};
pub use val::*;
pub trait Bindable: Trace {
@@ -109,6 +109,10 @@
struct EvaluationData {
/// Used for stack overflow detection, stacktrace is populated on unwind
stack_depth: usize,
+ /// Updated every time stack entry is popt
+ stack_generation: usize,
+
+ breakpoints: Breakpoints,
/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
files: HashMap<Rc<Path>, FileData>,
str_files: HashMap<Rc<Path>, IStr>,
@@ -119,6 +123,38 @@
parsed: LocExpr,
evaluated: Option<Val>,
}
+
+pub struct Breakpoint {
+ loc: ExprLocation,
+ collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,
+}
+#[derive(Default)]
+struct Breakpoints(Vec<Rc<Breakpoint>>);
+impl Breakpoints {
+ fn insert(
+ &self,
+ stack_depth: usize,
+ stack_generation: usize,
+ loc: &ExprLocation,
+ result: Result<Val>,
+ ) -> Result<Val> {
+ if self.0.is_empty() {
+ return result;
+ }
+ for item in self.0.iter() {
+ if item.loc.belongs_to(loc) {
+ let mut collected = item.collected.borrow_mut();
+ let (depth, vals) = collected.entry(stack_generation).or_default();
+ if stack_depth > *depth {
+ vals.clear();
+ }
+ vals.push(result.clone());
+ }
+ }
+ result
+ }
+}
+
#[derive(Default)]
pub struct EvaluationStateInternals {
/// Internal state
@@ -135,7 +171,7 @@
pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {
EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))
}
-pub(crate) fn push<T>(
+pub(crate) fn push_frame<T>(
e: Option<&ExprLocation>,
frame_desc: impl FnOnce() -> String,
f: impl FnOnce() -> Result<T>,
@@ -143,12 +179,12 @@
with_state(|s| s.push(e, frame_desc, f))
}
-pub fn push_stack_frame<T>(
+pub(crate) fn push_val_frame(
e: Option<&ExprLocation>,
frame_desc: impl FnOnce() -> String,
- f: impl FnOnce() -> Result<T>,
-) -> Result<T> {
- push(e, frame_desc, f)
+ f: impl FnOnce() -> Result<Val>,
+) -> Result<Val> {
+ with_state(|s| s.push(e, frame_desc, f))
}
/// Maintains stack trace and import resolution
@@ -178,6 +214,15 @@
Ok(())
}
+ pub fn reset_evaluation_state(&self, name: &Path) {
+ self.data_mut()
+ .files
+ .get_mut(name)
+ .unwrap()
+ .evaluated
+ .take();
+ }
+
/// Adds file by source code and parsed expr
pub fn add_parsed_file(
&self,
@@ -203,8 +248,15 @@
pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {
offset_to_location(&self.get_source(file).unwrap(), locs)
}
-
- pub(crate) fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {
+ pub fn map_from_source_location(
+ &self,
+ file: &Path,
+ line: usize,
+ column: usize,
+ ) -> Option<usize> {
+ location_to_offset(&self.get_source(file).unwrap(), line, column)
+ }
+ pub fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {
let file_path = self.resolve_file(from, path)?;
{
let data = self.data();
@@ -297,7 +349,54 @@
}
}
let result = f();
- self.data_mut().stack_depth -= 1;
+ {
+ let mut data = self.data_mut();
+ data.stack_depth -= 1;
+ data.stack_generation += 1;
+ // if let Some(e) = e {
+ // result =
+ // data.breakpoints
+ // .insert(data.stack_depth, data.stack_generation, &e, result)
+ // }
+ }
+ if let Err(mut err) = result {
+ err.trace_mut().0.push(StackTraceElement {
+ location: e.cloned(),
+ desc: frame_desc(),
+ });
+ return Err(err);
+ }
+ result
+ }
+ /// Executes code creating a new stack frame
+ pub fn push_val(
+ &self,
+ e: Option<&ExprLocation>,
+ frame_desc: impl FnOnce() -> String,
+ f: impl FnOnce() -> Result<Val>,
+ ) -> Result<Val> {
+ {
+ let mut data = self.data_mut();
+ let stack_depth = &mut data.stack_depth;
+ if *stack_depth > self.max_stack() {
+ // Error creation uses data, so i drop guard here
+ drop(data);
+ throw!(StackOverflow);
+ } else {
+ *stack_depth += 1;
+ }
+ }
+ let mut result = f();
+ {
+ let mut data = self.data_mut();
+ data.stack_depth -= 1;
+ data.stack_generation += 1;
+ if let Some(e) = e {
+ result =
+ data.breakpoints
+ .insert(data.stack_depth, data.stack_generation, &e, result)
+ }
+ }
if let Err(mut err) = result {
err.trace_mut().0.push(StackTraceElement {
location: e.cloned(),
@@ -322,7 +421,26 @@
result
})
}
+ pub fn run_in_state_with_breakpoint(
+ &self,
+ bp: Rc<Breakpoint>,
+ f: impl FnOnce() -> Result<()>,
+ ) -> Result<()> {
+ {
+ let mut data = self.data_mut();
+ data.breakpoints.0.push(bp);
+ }
+
+ let result = self.run_in_state(f);
+ {
+ let mut data = self.data_mut();
+ data.breakpoints.0.pop();
+ }
+
+ result
+ }
+
pub fn stringify_err(&self, e: &LocError) -> String {
let mut out = String::new();
self.settings()
@@ -346,7 +464,7 @@
pub fn with_tla(&self, val: Val) -> Result<Val> {
self.run_in_state(|| {
Ok(match val {
- Val::Func(func) => push(
+ Val::Func(func) => push_frame(
None,
|| "during TLA call".to_owned(),
|| {
@@ -514,7 +632,7 @@
|| "inner".to_owned(),
|| Err(RuntimeError("".into()).into()),
)?;
- Ok(())
+ Ok(Val::Null)
},
)
.unwrap();
crates/jrsonnet-evaluator/src/trace/location.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/location.rs
+++ b/crates/jrsonnet-evaluator/src/trace/location.rs
@@ -9,6 +9,18 @@
pub line_end_offset: usize,
}
+pub fn location_to_offset(mut file: &str, mut line: usize, column: usize) -> Option<usize> {
+ let mut offset = 0;
+ while line > 1 {
+ let pos = file.find('\n')?;
+ offset += pos + 1;
+ file = &file[pos + 1..];
+ line -= 1;
+ }
+ offset += column - 1;
+ Some(offset)
+}
+
pub fn offset_to_location(file: &str, offsets: &[usize]) -> Vec<CodeLocation> {
if offsets.is_empty() {
return vec![];
crates/jrsonnet-evaluator/src/typed.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed.rs
+++ b/crates/jrsonnet-evaluator/src/typed.rs
@@ -2,7 +2,7 @@
use crate::{
error::{Error, LocError, Result},
- push, Val,
+ push_frame, Val,
};
use jrsonnet_gc::Trace;
use jrsonnet_parser::ExprLocation;
@@ -103,7 +103,7 @@
path: impl Fn() -> ValuePathItem,
item: impl Fn() -> Result<()>,
) -> Result<()> {
- push(location, error_reason, || match item() {
+ push_frame(location, error_reason, || match item() {
Ok(_) => Ok(()),
Err(mut e) => {
if let Error::TypeError(e) = &mut e.error_mut() {
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 ArgsDesc {198 pub unnamed: Vec<LocExpr>,199 pub named: Vec<(IStr, LocExpr)>,200}201impl ArgsDesc {202 pub fn new(unnamed: Vec<LocExpr>, named: Vec<(IStr, LocExpr)>) -> Self {203 Self { unnamed, named }204 }205}206207#[cfg_attr(feature = "serialize", derive(Serialize))]208#[cfg_attr(feature = "deserialize", derive(Deserialize))]209#[derive(Debug, Clone, PartialEq, Trace)]210#[trivially_drop]211pub struct BindSpec {212 pub name: IStr,213 pub params: Option<ParamsDesc>,214 pub value: LocExpr,215}216217#[cfg_attr(feature = "serialize", derive(Serialize))]218#[cfg_attr(feature = "deserialize", derive(Deserialize))]219#[derive(Debug, PartialEq, Trace)]220#[trivially_drop]221pub struct IfSpecData(pub LocExpr);222223#[cfg_attr(feature = "serialize", derive(Serialize))]224#[cfg_attr(feature = "deserialize", derive(Deserialize))]225#[derive(Debug, PartialEq, Trace)]226#[trivially_drop]227pub struct ForSpecData(pub IStr, pub LocExpr);228229#[cfg_attr(feature = "serialize", derive(Serialize))]230#[cfg_attr(feature = "deserialize", derive(Deserialize))]231#[derive(Debug, PartialEq, Trace)]232#[trivially_drop]233pub enum CompSpec {234 IfSpec(IfSpecData),235 ForSpec(ForSpecData),236}237238#[cfg_attr(feature = "serialize", derive(Serialize))]239#[cfg_attr(feature = "deserialize", derive(Deserialize))]240#[derive(Debug, PartialEq, Trace)]241#[trivially_drop]242pub struct ObjComp {243 pub pre_locals: Vec<BindSpec>,244 pub key: LocExpr,245 pub plus: bool,246 pub value: LocExpr,247 pub post_locals: Vec<BindSpec>,248 pub compspecs: Vec<CompSpec>,249}250251#[cfg_attr(feature = "serialize", derive(Serialize))]252#[cfg_attr(feature = "deserialize", derive(Deserialize))]253#[derive(Debug, PartialEq, Trace)]254#[trivially_drop]255pub enum ObjBody {256 MemberList(Vec<Member>),257 ObjComp(ObjComp),258}259260#[cfg_attr(feature = "serialize", derive(Serialize))]261#[cfg_attr(feature = "deserialize", derive(Deserialize))]262#[derive(Debug, PartialEq, Clone, Copy, Trace)]263#[trivially_drop]264pub enum LiteralType {265 This,266 Super,267 Dollar,268 Null,269 True,270 False,271}272273#[cfg_attr(feature = "serialize", derive(Serialize))]274#[cfg_attr(feature = "deserialize", derive(Deserialize))]275#[derive(Debug, PartialEq, Trace)]276#[trivially_drop]277pub struct SliceDesc {278 pub start: Option<LocExpr>,279 pub end: Option<LocExpr>,280 pub step: Option<LocExpr>,281}282283/// Syntax base284#[cfg_attr(feature = "serialize", derive(Serialize))]285#[cfg_attr(feature = "deserialize", derive(Deserialize))]286#[derive(Debug, PartialEq, Trace)]287#[trivially_drop]288pub enum Expr {289 Literal(LiteralType),290291 /// String value: "hello"292 Str(IStr),293 /// Number: 1, 2.0, 2e+20294 Num(f64),295 /// Variable name: test296 Var(IStr),297298 /// Array of expressions: [1, 2, "Hello"]299 Arr(Vec<LocExpr>),300 /// Array comprehension:301 /// ```jsonnet302 /// ingredients: [303 /// { kind: kind, qty: 4 / 3 }304 /// for kind in [305 /// 'Honey Syrup',306 /// 'Lemon Juice',307 /// 'Farmers Gin',308 /// ]309 /// ],310 /// ```311 ArrComp(LocExpr, Vec<CompSpec>),312313 /// Object: {a: 2}314 Obj(ObjBody),315 /// Object extension: var1 {b: 2}316 ObjExtend(LocExpr, ObjBody),317318 /// (obj)319 Parened(LocExpr),320321 /// -2322 UnaryOp(UnaryOpType, LocExpr),323 /// 2 - 2324 BinaryOp(LocExpr, BinaryOpType, LocExpr),325 /// assert 2 == 2 : "Math is broken"326 AssertExpr(AssertStmt, LocExpr),327 /// local a = 2; { b: a }328 LocalExpr(Vec<BindSpec>, LocExpr),329330 /// import "hello"331 Import(PathBuf),332 /// importStr "file.txt"333 ImportStr(PathBuf),334 /// error "I'm broken"335 ErrorStmt(LocExpr),336 /// a(b, c)337 Apply(LocExpr, ArgsDesc, bool),338 /// a[b]339 Index(LocExpr, LocExpr),340 /// function(x) x341 Function(ParamsDesc, LocExpr),342 /// std.primitiveEquals343 Intrinsic(IStr),344 /// if true == false then 1 else 2345 IfElse {346 cond: IfSpecData,347 cond_then: LocExpr,348 cond_else: Option<LocExpr>,349 },350 Slice(LocExpr, SliceDesc),351}352353/// file, begin offset, end offset354#[cfg_attr(feature = "serialize", derive(Serialize))]355#[cfg_attr(feature = "deserialize", derive(Deserialize))]356#[derive(Clone, PartialEq, Trace)]357#[trivially_drop]358pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);359360impl Debug for ExprLocation {361 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {362 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)363 }364}365366/// Holds AST expression and its location in source file367#[cfg_attr(feature = "serialize", derive(Serialize))]368#[cfg_attr(feature = "deserialize", derive(Deserialize))]369#[derive(Clone, PartialEq)]370pub struct LocExpr(pub Rc<Expr>, pub Option<ExprLocation>);371/// Safety:372/// AST is acyclic, and there should be no gc pointers373unsafe impl Trace for LocExpr {374 unsafe_empty_trace!();375}376impl Finalize for LocExpr {}377378impl Debug for LocExpr {379 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {380 if f.alternate() {381 write!(f, "{:#?}", self.0)?;382 } else {383 write!(f, "{:?}", self.0)?;384 }385 if let Some(loc) = &self.1 {386 write!(f, " from {:?}", loc)?;387 }388 Ok(())389 }390}391392/// Creates LocExpr from Expr and ExprLocation components393#[macro_export]394macro_rules! loc_expr {395 ($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {396 LocExpr(397 std::rc::Rc::new($expr),398 if $need_loc {399 Some(ExprLocation($name, $start, $end))400 } else {401 None402 },403 )404 };405}1use 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 ArgsDesc {198 pub unnamed: Vec<LocExpr>,199 pub named: Vec<(IStr, LocExpr)>,200}201impl ArgsDesc {202 pub fn new(unnamed: Vec<LocExpr>, named: Vec<(IStr, LocExpr)>) -> Self {203 Self { unnamed, named }204 }205}206207#[cfg_attr(feature = "serialize", derive(Serialize))]208#[cfg_attr(feature = "deserialize", derive(Deserialize))]209#[derive(Debug, Clone, PartialEq, Trace)]210#[trivially_drop]211pub struct BindSpec {212 pub name: IStr,213 pub params: Option<ParamsDesc>,214 pub value: LocExpr,215}216217#[cfg_attr(feature = "serialize", derive(Serialize))]218#[cfg_attr(feature = "deserialize", derive(Deserialize))]219#[derive(Debug, PartialEq, Trace)]220#[trivially_drop]221pub struct IfSpecData(pub LocExpr);222223#[cfg_attr(feature = "serialize", derive(Serialize))]224#[cfg_attr(feature = "deserialize", derive(Deserialize))]225#[derive(Debug, PartialEq, Trace)]226#[trivially_drop]227pub struct ForSpecData(pub IStr, pub LocExpr);228229#[cfg_attr(feature = "serialize", derive(Serialize))]230#[cfg_attr(feature = "deserialize", derive(Deserialize))]231#[derive(Debug, PartialEq, Trace)]232#[trivially_drop]233pub enum CompSpec {234 IfSpec(IfSpecData),235 ForSpec(ForSpecData),236}237238#[cfg_attr(feature = "serialize", derive(Serialize))]239#[cfg_attr(feature = "deserialize", derive(Deserialize))]240#[derive(Debug, PartialEq, Trace)]241#[trivially_drop]242pub struct ObjComp {243 pub pre_locals: Vec<BindSpec>,244 pub key: LocExpr,245 pub plus: bool,246 pub value: LocExpr,247 pub post_locals: Vec<BindSpec>,248 pub compspecs: Vec<CompSpec>,249}250251#[cfg_attr(feature = "serialize", derive(Serialize))]252#[cfg_attr(feature = "deserialize", derive(Deserialize))]253#[derive(Debug, PartialEq, Trace)]254#[trivially_drop]255pub enum ObjBody {256 MemberList(Vec<Member>),257 ObjComp(ObjComp),258}259260#[cfg_attr(feature = "serialize", derive(Serialize))]261#[cfg_attr(feature = "deserialize", derive(Deserialize))]262#[derive(Debug, PartialEq, Clone, Copy, Trace)]263#[trivially_drop]264pub enum LiteralType {265 This,266 Super,267 Dollar,268 Null,269 True,270 False,271}272273#[cfg_attr(feature = "serialize", derive(Serialize))]274#[cfg_attr(feature = "deserialize", derive(Deserialize))]275#[derive(Debug, PartialEq, Trace)]276#[trivially_drop]277pub struct SliceDesc {278 pub start: Option<LocExpr>,279 pub end: Option<LocExpr>,280 pub step: Option<LocExpr>,281}282283/// Syntax base284#[cfg_attr(feature = "serialize", derive(Serialize))]285#[cfg_attr(feature = "deserialize", derive(Deserialize))]286#[derive(Debug, PartialEq, Trace)]287#[trivially_drop]288pub enum Expr {289 Literal(LiteralType),290291 /// String value: "hello"292 Str(IStr),293 /// Number: 1, 2.0, 2e+20294 Num(f64),295 /// Variable name: test296 Var(IStr),297298 /// Array of expressions: [1, 2, "Hello"]299 Arr(Vec<LocExpr>),300 /// Array comprehension:301 /// ```jsonnet302 /// ingredients: [303 /// { kind: kind, qty: 4 / 3 }304 /// for kind in [305 /// 'Honey Syrup',306 /// 'Lemon Juice',307 /// 'Farmers Gin',308 /// ]309 /// ],310 /// ```311 ArrComp(LocExpr, Vec<CompSpec>),312313 /// Object: {a: 2}314 Obj(ObjBody),315 /// Object extension: var1 {b: 2}316 ObjExtend(LocExpr, ObjBody),317318 /// (obj)319 Parened(LocExpr),320321 /// -2322 UnaryOp(UnaryOpType, LocExpr),323 /// 2 - 2324 BinaryOp(LocExpr, BinaryOpType, LocExpr),325 /// assert 2 == 2 : "Math is broken"326 AssertExpr(AssertStmt, LocExpr),327 /// local a = 2; { b: a }328 LocalExpr(Vec<BindSpec>, LocExpr),329330 /// import "hello"331 Import(PathBuf),332 /// importStr "file.txt"333 ImportStr(PathBuf),334 /// error "I'm broken"335 ErrorStmt(LocExpr),336 /// a(b, c)337 Apply(LocExpr, ArgsDesc, bool),338 /// a[b]339 Index(LocExpr, LocExpr),340 /// function(x) x341 Function(ParamsDesc, LocExpr),342 /// std.primitiveEquals343 Intrinsic(IStr),344 /// if true == false then 1 else 2345 IfElse {346 cond: IfSpecData,347 cond_then: LocExpr,348 cond_else: Option<LocExpr>,349 },350 Slice(LocExpr, SliceDesc),351}352353/// file, begin offset, end offset354#[cfg_attr(feature = "serialize", derive(Serialize))]355#[cfg_attr(feature = "deserialize", derive(Deserialize))]356#[derive(Clone, PartialEq, Trace)]357#[trivially_drop]358pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);359impl ExprLocation {360 pub fn belongs_to(&self, other: &ExprLocation) -> bool {361 other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2362 }363}364365impl Debug for ExprLocation {366 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {367 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)368 }369}370371/// Holds AST expression and its location in source file372#[cfg_attr(feature = "serialize", derive(Serialize))]373#[cfg_attr(feature = "deserialize", derive(Deserialize))]374#[derive(Clone, PartialEq)]375pub struct LocExpr(pub Rc<Expr>, pub Option<ExprLocation>);376/// Safety:377/// AST is acyclic, and there should be no gc pointers378unsafe impl Trace for LocExpr {379 unsafe_empty_trace!();380}381impl Finalize for LocExpr {}382383impl Debug for LocExpr {384 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {385 if f.alternate() {386 write!(f, "{:#?}", self.0)?;387 } else {388 write!(f, "{:?}", self.0)?;389 }390 if let Some(loc) = &self.1 {391 write!(f, " from {:?}", loc)?;392 }393 Ok(())394 }395}396397/// Creates LocExpr from Expr and ExprLocation components398#[macro_export]399macro_rules! loc_expr {400 ($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {401 LocExpr(402 std::rc::Rc::new($expr),403 if $need_loc {404 Some(ExprLocation($name, $start, $end))405 } else {406 None407 },408 )409 };410}