difftreelog
refactor only keep used spans in IR
in: master
12 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -730,6 +730,7 @@
version = "0.5.0-pre97"
dependencies = [
"insta",
+ "jrsonnet-gcmodule",
"jrsonnet-ir",
"peg",
]
crates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -38,7 +38,7 @@
Self::new(RangeArray::empty())
}
- pub fn expr(ctx: Context, exprs: Rc<Vec<Spanned<Expr>>>) -> Self {
+ pub fn expr(ctx: Context, exprs: Rc<Vec<Expr>>) -> Self {
Self::new(ExprArray::new(ctx, exprs))
}
crates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -118,11 +118,11 @@
#[derive(Debug, Trace, Clone)]
pub struct ExprArray {
ctx: Context,
- src: Rc<Vec<Spanned<Expr>>>,
+ src: Rc<Vec<Expr>>,
cached: Cc<RefCell<Vec<ArrayThunk>>>,
}
impl ExprArray {
- pub fn new(ctx: Context, src: Rc<Vec<Spanned<Expr>>>) -> Self {
+ pub fn new(ctx: Context, src: Rc<Vec<Expr>>) -> Self {
Self {
ctx,
cached: Cc::new(RefCell::new(vec![ArrayThunk::Waiting; src.len()])),
crates/jrsonnet-evaluator/src/async_import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/async_import.rs
+++ b/crates/jrsonnet-evaluator/src/async_import.rs
@@ -139,7 +139,7 @@
if let Expr::Str(s) = &***v {
out.0.push(Import {
path: ResolvePathOwned::Str(s.to_string()),
- expression: matches!(&**expr, Expr::Import(ImportKind::Normal, _)),
+ expression: todo!(),
});
}
// Non-string import will fail in runtime
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -53,7 +53,7 @@
Expr::Str(_)
| Expr::Num(_)
| Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,
- Expr::Arr(a) => a.iter().all(|e| is_trivial(&**e)),
+ Expr::Arr(a) => a.iter().all(|e| is_trivial(&*e)),
_ => false,
}
}
@@ -71,7 +71,7 @@
}
Val::Arr(ArrValue::eager(
n.iter()
- .map(|e| evaluate_trivial(&**e))
+ .map(|e| evaluate_trivial(&*e))
.map(|e| e.expect("checked trivial"))
.collect(),
))
@@ -80,12 +80,7 @@
})
}
-pub fn evaluate_method(
- ctx: Context,
- name: IStr,
- params: ExprParams,
- body: Rc<Spanned<Expr>>,
-) -> Val {
+pub fn evaluate_method(ctx: Context, name: IStr, params: ExprParams, body: Rc<Expr>) -> Val {
Val::Func(FuncVal::Normal(Cc::new(FuncDesc {
name,
ctx,
@@ -97,18 +92,21 @@
pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {
Ok(match field_name {
FieldName::Fixed(n) => Some(n.clone()),
- FieldName::Dyn(expr) => in_frame(
- CallLocation::new(&expr.span()),
- || "evaluating field name".to_string(),
- || {
- let value = evaluate(ctx, expr)?;
- if matches!(value, Val::Null) {
- Ok(None)
- } else {
- Ok(Some(IStr::from_untyped(value)?))
- }
- },
- )?,
+ FieldName::Dyn(expr) => {
+ // FIXME: Span
+ let value = evaluate(ctx, expr)?;
+ if matches!(value, Val::Null) {
+ None
+ } else {
+ Some(IStr::from_untyped(value)?)
+ }
+ } //
+ // in_frame(
+ // CallLocation::new(&expr.span()),
+ // || "evaluating field name".to_string(),
+ // || {
+ // },
+ // )?,
})
}
@@ -119,46 +117,48 @@
) -> Result<()> {
match specs.first() {
None => callback(ctx)?,
- Some(CompSpec::IfSpec(IfSpecData(cond))) => {
+ Some(CompSpec::IfSpec(Spanned(IfSpecData(cond), _))) => {
if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {
evaluate_comp(ctx, &specs[1..], callback)?;
}
}
- Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {
- Val::Arr(list) => {
- for item in list.iter_lazy() {
- let fctx = Pending::new();
- let mut new_bindings = FxHashMap::with_capacity(var.binds_len());
- destruct(var, item, fctx.clone(), &mut new_bindings)?;
- let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);
+ Some(CompSpec::ForSpec(Spanned(ForSpecData(var, expr), _))) => {
+ match evaluate(ctx.clone(), expr)? {
+ Val::Arr(list) => {
+ for item in list.iter_lazy() {
+ let fctx = Pending::new();
+ let mut new_bindings = FxHashMap::with_capacity(var.binds_len());
+ destruct(var, item, fctx.clone(), &mut new_bindings)?;
+ let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);
- evaluate_comp(ctx, &specs[1..], callback)?;
+ evaluate_comp(ctx, &specs[1..], callback)?;
+ }
}
- }
- #[cfg(feature = "exp-object-iteration")]
- Val::Obj(obj) => {
- for field in obj.fields(
- // TODO: Should there be ability to preserve iteration order?
- #[cfg(feature = "exp-preserve-order")]
- false,
- ) {
- let fctx = Pending::new();
- let mut new_bindings = FxHashMap::with_capacity(var.binds_len());
- let obj = obj.clone();
- let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![
- Thunk::evaluated(Val::string(field.clone())),
- Thunk!(move || obj.get(field).transpose().expect(
- "field exists, as field name was obtained from object.fields()",
- )),
- ])));
- destruct(var, value, fctx.clone(), &mut new_bindings)?;
- let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);
+ #[cfg(feature = "exp-object-iteration")]
+ Val::Obj(obj) => {
+ for field in obj.fields(
+ // TODO: Should there be ability to preserve iteration order?
+ #[cfg(feature = "exp-preserve-order")]
+ false,
+ ) {
+ let fctx = Pending::new();
+ let mut new_bindings = FxHashMap::with_capacity(var.binds_len());
+ let obj = obj.clone();
+ let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![
+ Thunk::evaluated(Val::string(field.clone())),
+ Thunk!(move || obj.get(field).transpose().expect(
+ "field exists, as field name was obtained from object.fields()",
+ )),
+ ])));
+ destruct(var, value, fctx.clone(), &mut new_bindings)?;
+ let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);
- evaluate_comp(ctx, &specs[1..], callback)?;
+ evaluate_comp(ctx, &specs[1..], callback)?;
+ }
}
+ _ => bail!(InComprehensionCanOnlyIterateOverArray),
}
- _ => bail!(InComprehensionCanOnlyIterateOverArray),
- },
+ }
}
Ok(())
}
@@ -221,7 +221,7 @@
#[derive(Trace)]
struct UnboundValue<B: Trace> {
uctx: B,
- value: Rc<Spanned<Expr>>,
+ value: Rc<Expr>,
name: IStr,
}
impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {
@@ -235,7 +235,8 @@
.field(name.clone())
.with_add(*plus)
.with_visibility(*visibility)
- .with_location(value.span())
+ // FIXME
+ // .with_location(value.span())
.bindable(UnboundValue {
uctx,
value: value.clone(),
@@ -251,7 +252,7 @@
#[derive(Trace)]
struct UnboundMethod<B: Trace> {
uctx: B,
- value: Rc<Spanned<Expr>>,
+ value: Rc<Expr>,
params: ExprParams,
name: IStr,
}
@@ -270,7 +271,7 @@
builder
.field(name.clone())
.with_visibility(*visibility)
- .with_location(value.span())
+ // .with_location(value.span())
.bindable(UnboundMethod {
uctx,
value: value.clone(),
@@ -337,7 +338,7 @@
pub fn evaluate_apply(
ctx: Context,
- value: &Spanned<Expr>,
+ value: &Expr,
args: &ArgsDesc,
loc: CallLocation<'_>,
tailstrict: bool,
@@ -379,16 +380,16 @@
Ok(())
}
-pub fn evaluate_named_param(ctx: Context, expr: &Spanned<Expr>, name: ParamName) -> Result<Val> {
+pub fn evaluate_named_param(ctx: Context, expr: &Expr, name: ParamName) -> Result<Val> {
match name {
ParamName::Named(name) => evaluate_named(ctx, expr, name),
ParamName::Unnamed => evaluate(ctx, expr),
}
}
-pub fn evaluate_named(ctx: Context, expr: &Spanned<Expr>, name: IStr) -> Result<Val> {
+pub fn evaluate_named(ctx: Context, expr: &Expr, name: IStr) -> Result<Val> {
use Expr::*;
- Ok(match &**expr {
+ Ok(match &*expr {
Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),
_ => evaluate(ctx, expr)?,
})
@@ -417,7 +418,7 @@
// because the standalone super literal is not supported, that is because in other
// implementations `in super` treated differently from `in smth_else`.
BinaryOp(bin)
- if matches!(&*bin.rhs, Expr::Literal(LiteralType::Super))
+ if matches!(&bin.rhs, Expr::Literal(LiteralType::Super))
&& bin.op == BinaryOpType::In =>
{
let sup_this = ctx.try_sup_this()?;
@@ -433,12 +434,12 @@
UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,
Var(name) => in_frame(
CallLocation::new(&name.span()),
- || format!("local <{name}> access"),
+ || format!("local <{}> access", &**name),
|| ctx.binding((**name).clone())?.evaluate(),
)?,
Index { indexable, parts } => ensure_sufficient_stack(|| {
let mut parts = parts.iter();
- let mut indexable = if matches!(&***indexable, Expr::Literal(LiteralType::Super)) {
+ let mut indexable = if matches!(&**indexable, Expr::Literal(LiteralType::Super)) {
let part = parts.next().expect("at least part should exist");
// sup_this existence check might also be skipped here for null-coalesce...
// But I believe this might cause errors.
@@ -463,7 +464,7 @@
let name = name.into_flat();
match sup_this
.get_super(name.clone())
- .with_description_src(&part.value, || format!("field <{name}> access"))?
+ .with_description_src(&part.span, || format!("field <{name}> access"))?
{
Some(v) => v,
#[cfg(feature = "exp-null-coaelse")]
@@ -485,7 +486,7 @@
indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {
(Val::Obj(v), Val::Str(key)) => match v
.get(key.clone().into_flat())
- .with_description_src(&part.value, || format!("field <{key}> access"))?
+ .with_description_src(&part.span, || format!("field <{key}> access"))?
{
Some(v) => v,
#[cfg(feature = "exp-null-coaelse")]
@@ -497,7 +498,7 @@
key.clone().into_flat(),
suggestions,
)))
- .with_description_src(&part.value, || format!("field <{key}> access"));
+ .with_description_src(&part.span, || format!("field <{key}> access"));
}
},
(Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(
@@ -605,17 +606,21 @@
evaluate_assert(ctx.clone(), &assert.assert)?;
evaluate(ctx, &assert.rest)?
}
- ErrorStmt(e) => in_frame(
- CallLocation::new(&e.span()),
+ ErrorStmt(s, e) => in_frame(
+ CallLocation::new(&s),
|| "error statement".to_owned(),
|| bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),
)?,
IfElse(if_else) => {
- if in_frame(
- CallLocation::new(&if_else.cond.0.span()),
- || "if condition".to_owned(),
- || bool::from_untyped(evaluate(ctx.clone(), &if_else.cond.0)?),
- )? {
+ if
+ // FIXME
+ //in_frame(
+ // CallLocation::new(&if_else.cond.0.span()),
+ // || "if condition".to_owned(),
+ // ||
+ bool::from_untyped(evaluate(ctx.clone(), &if_else.cond.0)?)?
+ // )?
+ {
evaluate(ctx, &if_else.cond_then)?
} else {
match &if_else.cond_else {
@@ -626,14 +631,13 @@
}
Slice(slice) => {
fn parse_idx<T: Typed + FromUntyped>(
- loc: CallLocation<'_>,
ctx: Context,
expr: Option<&Spanned<Expr>>,
desc: &'static str,
) -> Result<Option<T>> {
if let Some(value) = expr {
Ok(in_frame(
- loc,
+ CallLocation::new(&value.span()),
|| format!("slice {desc}"),
|| <Option<T>>::from_untyped(evaluate(ctx, value)?),
)?)
@@ -643,24 +647,23 @@
}
let indexable = evaluate(ctx.clone(), &slice.value)?;
- let loc = CallLocation::new(&loc);
- let start = parse_idx(loc, ctx.clone(), slice.slice.start.as_ref(), "start")?;
- let end = parse_idx(loc, ctx.clone(), slice.slice.end.as_ref(), "end")?;
- let step = parse_idx(loc, ctx, slice.slice.step.as_ref(), "step")?;
+ let start = parse_idx(ctx.clone(), slice.slice.start.as_ref(), "start")?;
+ let end = parse_idx(ctx.clone(), slice.slice.end.as_ref(), "end")?;
+ let step = parse_idx(ctx, slice.slice.step.as_ref(), "step")?;
IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?
}
Import(kind, path) => {
- let Expr::Str(path) = &***path else {
+ let Expr::Str(path) = &**path else {
bail!("computed imports are not supported")
};
- let tmp = loc.clone().0;
with_state(|s| {
- let resolved_path = s.resolve_from(tmp.source_path(), path)?;
- Ok(match kind {
+ let span = kind.span();
+ let resolved_path = s.resolve_from(span.0.source_path(), path)?;
+ Ok(match &**kind {
ImportKind::Normal => in_frame(
- CallLocation::new(&loc),
+ CallLocation::new(&span),
|| format!("import {:?}", path.clone()),
|| s.import_resolved(resolved_path),
)?,
crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -147,9 +147,9 @@
pub fn evaluate_binary_op_special(
ctx: Context,
- a: &Spanned<Expr>,
+ a: &Expr,
op: BinaryOpType,
- b: &Spanned<Expr>,
+ b: &Expr,
) -> Result<Val> {
use BinaryOpType::*;
use Val::*;
crates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -3,8 +3,8 @@
use educe::Educe;
use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
+use jrsonnet_ir::{ArgsDesc, Destruct, Expr, ExprParams, Span, Spanned};
pub use jrsonnet_macros::builtin;
-use jrsonnet_ir::{ArgsDesc, Destruct, Expr, ExprParams, Span, Spanned};
use self::{
builtin::{Builtin, StaticBuiltin},
@@ -71,7 +71,7 @@
/// Function parameter definition
pub params: ExprParams,
/// Function body
- pub body: Rc<Spanned<Expr>>,
+ pub body: Rc<Expr>,
}
impl FuncDesc {
/// Create body context, but fill arguments without defaults with lazy error
@@ -256,7 +256,7 @@
#[cfg(feature = "exp-destruct")]
_ => return false,
};
- **desc.body == Expr::Var(id.clone())
+ matches!(&*desc.body, Expr::Var(v) if &**v == id)
}
_ => false,
}
crates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -2,7 +2,7 @@
use jrsonnet_ir::{
function::{FunctionSignature, ParamName},
- ArgsDesc, Expr, ExprParams, Spanned,
+ ArgsDesc, Expr, ExprParams,
};
use rustc_hash::FxHashMap;
@@ -15,7 +15,7 @@
Context, Pending, Thunk, Val,
};
-fn eval_arg(ctx: Context, arg: &Rc<Spanned<Expr>>, tailstrict: bool) -> Result<Thunk<Val>> {
+fn eval_arg(ctx: Context, arg: &Rc<Expr>, tailstrict: bool) -> Result<Thunk<Val>> {
if tailstrict {
Ok(Thunk::evaluated(evaluate(ctx, arg)?))
} else {
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -5,7 +5,7 @@
extern crate self as jrsonnet_evaluator;
mod arr;
-pub mod async_import;
+// pub mod async_import;
mod ctx;
mod dynamic;
pub mod error;
@@ -187,7 +187,7 @@
struct FileData {
string: Option<IStr>,
bytes: Option<IBytes>,
- parsed: Option<Rc<Spanned<Expr>>>,
+ parsed: Option<Rc<Expr>>,
evaluated: Option<Val>,
evaluating: bool,
crates/jrsonnet-ir/src/expr.rsdiffbeforeafterboth1use std::{2 fmt::{self, Debug, Display},3 ops::Deref,4 rc::Rc,5};67use jrsonnet_gcmodule::Acyclic;8use jrsonnet_interner::IStr;910use crate::{11 function::{FunctionSignature, ParamDefault, ParamName, ParamParse},12 source::Source,13};1415#[derive(Debug, PartialEq, Acyclic)]16pub enum FieldName {17 /// {fixed: 2}18 Fixed(IStr),19 /// {["dyn"+"amic"]: 3}20 Dyn(Spanned<Expr>),21}2223#[derive(Debug, Clone, Copy, PartialEq, Eq, Acyclic)]24#[repr(u8)]25pub enum Visibility {26 /// :27 Normal,28 /// ::29 Hidden,30 /// :::31 Unhide,32}3334impl Visibility {35 pub fn is_visible(&self) -> bool {36 matches!(self, Self::Normal | Self::Unhide)37 }38}3940#[derive(Debug, PartialEq, Acyclic)]41pub struct AssertStmt(pub Spanned<Expr>, pub Option<Spanned<Expr>>);4243#[derive(Debug, PartialEq, Acyclic)]44pub struct FieldMember {45 pub name: FieldName,46 pub plus: bool,47 pub params: Option<ExprParams>,48 pub visibility: Visibility,49 pub value: Rc<Spanned<Expr>>,50}5152#[derive(Debug, PartialEq, Acyclic)]53pub enum Member {54 Field(FieldMember),55 BindStmt(BindSpec),56 AssertStmt(AssertStmt),57}5859#[derive(Debug, Clone, Copy, PartialEq, Eq, Acyclic)]60pub enum UnaryOpType {61 Plus,62 Minus,63 BitNot,64 Not,65}6667impl Display for UnaryOpType {68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {69 use UnaryOpType::*;70 write!(71 f,72 "{}",73 match self {74 Plus => "+",75 Minus => "-",76 BitNot => "~",77 Not => "!",78 }79 )80 }81}8283#[derive(Debug, Clone, Copy, PartialEq, Eq, Acyclic)]84pub enum BinaryOpType {85 Mul,86 Div,8788 /// Implemented as intrinsic, put here for completeness89 Mod,9091 Add,92 Sub,9394 Lhs,95 Rhs,9697 Lt,98 Gt,99 Lte,100 Gte,101102 BitAnd,103 BitOr,104 BitXor,105106 Eq,107 Neq,108109 And,110 Or,111 #[cfg(feature = "exp-null-coaelse")]112 NullCoaelse,113114 // Equialent to std.objectHasEx(a, b, true)115 In,116}117118impl Display for BinaryOpType {119 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {120 use BinaryOpType::*;121 write!(122 f,123 "{}",124 match self {125 Mul => "*",126 Div => "/",127 Mod => "%",128 Add => "+",129 Sub => "-",130 Lhs => "<<",131 Rhs => ">>",132 Lt => "<",133 Gt => ">",134 Lte => "<=",135 Gte => ">=",136 BitAnd => "&",137 BitOr => "|",138 BitXor => "^",139 Eq => "==",140 Neq => "!=",141 And => "&&",142 Or => "||",143 In => "in",144 #[cfg(feature = "exp-null-coaelse")]145 NullCoaelse => "??",146 }147 )148 }149}150151/// name, default value152#[derive(Debug, PartialEq, Acyclic)]153pub struct ExprParam {154 pub destruct: Destruct,155 pub default: Option<Rc<Spanned<Expr>>>,156}157158/// Defined function parameters159#[derive(Debug, Clone, PartialEq, Acyclic)]160pub struct ExprParams {161 pub exprs: Rc<Vec<ExprParam>>,162 pub signature: FunctionSignature,163 binds_len: usize,164}165impl ExprParams {166 pub fn len(&self) -> usize {167 self.exprs.len()168 }169 pub fn is_empty(&self) -> bool {170 self.exprs.is_empty()171 }172173 pub fn binds_len(&self) -> usize {174 self.binds_len175 }176 pub fn new(exprs: Vec<ExprParam>) -> Self {177 Self {178 signature: FunctionSignature::new(179 exprs180 .iter()181 .map(|p| {182 ParamParse::new(183 p.destruct.name(),184 ParamDefault::exists(p.default.is_some()),185 )186 })187 .collect(),188 ),189 binds_len: exprs.iter().map(|v| v.destruct.binds_len()).sum(),190 exprs: Rc::new(exprs),191 }192 }193}194195#[derive(Debug, PartialEq, Acyclic)]196pub struct ArgsDesc {197 pub unnamed: Vec<Rc<Spanned<Expr>>>,198 pub named: Vec<(IStr, Rc<Spanned<Expr>>)>,199}200impl ArgsDesc {201 pub fn new(unnamed: Vec<Rc<Spanned<Expr>>>, named: Vec<(IStr, Rc<Spanned<Expr>>)>) -> Self {202 Self { unnamed, named }203 }204}205206#[derive(Debug, Clone, PartialEq, Eq, Acyclic)]207pub enum DestructRest {208 /// ...rest209 Keep(IStr),210 /// ...211 Drop,212}213214#[derive(Debug, Clone, PartialEq, Acyclic)]215pub enum Destruct {216 Full(IStr),217 #[cfg(feature = "exp-destruct")]218 Skip,219 #[cfg(feature = "exp-destruct")]220 Array {221 start: Vec<Destruct>,222 rest: Option<DestructRest>,223 end: Vec<Destruct>,224 },225 #[cfg(feature = "exp-destruct")]226 Object {227 #[allow(clippy::type_complexity)]228 fields: Vec<(IStr, Option<Destruct>, Option<Rc<Spanned<Expr>>>)>,229 rest: Option<DestructRest>,230 },231}232impl Destruct {233 /// Name of destructure, used for function parameter names234 pub fn name(&self) -> ParamName {235 match self {236 Self::Full(name) => ParamName::Named(name.clone()),237 #[cfg(feature = "exp-destruct")]238 _ => ParamName::Unnamed,239 }240 }241 pub fn binds_len(&self) -> usize {242 #[cfg(feature = "exp-destruct")]243 fn cap_rest(rest: &Option<DestructRest>) -> usize {244 match rest {245 Some(DestructRest::Keep(_)) => 1,246 Some(DestructRest::Drop) => 0,247 None => 0,248 }249 }250 match self {251 Self::Full(_) => 1,252 #[cfg(feature = "exp-destruct")]253 Self::Skip => 0,254 #[cfg(feature = "exp-destruct")]255 Self::Array { start, rest, end } => {256 start.iter().map(Destruct::binds_len).sum::<usize>()257 + end.iter().map(Destruct::binds_len).sum::<usize>()258 + cap_rest(rest)259 }260 #[cfg(feature = "exp-destruct")]261 Self::Object { fields, rest } => {262 let mut out = 0;263 for (_, into, _) in fields {264 match into {265 Some(v) => out += v.binds_len(),266 // Field is destructured to default name267 None => out += 1,268 }269 }270 out + cap_rest(rest)271 }272 }273 }274}275276#[derive(Debug, PartialEq, Acyclic)]277pub enum BindSpec {278 Field {279 into: Destruct,280 value: Rc<Spanned<Expr>>,281 },282 Function {283 name: IStr,284 params: ExprParams,285 value: Rc<Spanned<Expr>>,286 },287}288impl BindSpec {289 pub fn binds_len(&self) -> usize {290 match self {291 BindSpec::Field { into, .. } => into.binds_len(),292 BindSpec::Function { .. } => 1,293 }294 }295}296297#[derive(Debug, PartialEq, Acyclic)]298pub struct IfSpecData(pub Spanned<Expr>);299300#[derive(Debug, PartialEq, Acyclic)]301pub struct ForSpecData(pub Destruct, pub Spanned<Expr>);302303#[derive(Debug, PartialEq, Acyclic)]304pub enum CompSpec {305 IfSpec(IfSpecData),306 ForSpec(ForSpecData),307}308309#[derive(Debug, PartialEq, Acyclic)]310pub struct ObjComp {311 pub locals: Rc<Vec<BindSpec>>,312 pub field: Rc<FieldMember>,313 pub compspecs: Vec<CompSpec>,314}315316#[derive(Debug, PartialEq, Acyclic)]317pub struct ObjMembers {318 pub locals: Rc<Vec<BindSpec>>,319 pub asserts: Rc<Vec<AssertStmt>>,320 pub fields: Vec<FieldMember>,321}322323#[derive(Debug, PartialEq, Acyclic)]324pub enum ObjBody {325 MemberList(ObjMembers),326 ObjComp(ObjComp),327}328329#[derive(Debug, PartialEq, Eq, Clone, Copy, Acyclic)]330pub enum LiteralType {331 This,332 Super,333 Dollar,334 Null,335 True,336 False,337}338339#[derive(Debug, PartialEq, Acyclic)]340pub struct SliceDesc {341 pub start: Option<Spanned<Expr>>,342 pub end: Option<Spanned<Expr>>,343 pub step: Option<Spanned<Expr>>,344}345346#[derive(Debug, PartialEq, Acyclic)]347pub struct AssertExpr {348 pub assert: AssertStmt,349 pub rest: Spanned<Expr>,350}351352#[derive(Debug, PartialEq, Acyclic)]353pub struct BinaryOp {354 pub lhs: Spanned<Expr>,355 pub op: BinaryOpType,356 pub rhs: Spanned<Expr>,357}358359#[derive(Debug, PartialEq, Acyclic)]360pub enum ImportKind {361 Normal,362 Str,363 Bin,364}365366#[derive(Debug, PartialEq, Acyclic)]367pub struct IfElse {368 pub cond: IfSpecData,369 pub cond_then: Spanned<Expr>,370 pub cond_else: Option<Spanned<Expr>>,371}372373#[derive(Debug, PartialEq, Acyclic)]374pub struct Slice {375 pub value: Spanned<Expr>,376 pub slice: SliceDesc,377}378379/// Syntax base380#[derive(Debug, PartialEq, Acyclic)]381pub enum Expr {382 Literal(LiteralType),383384 /// String value: "hello"385 Str(IStr),386 /// Number: 1, 2.0, 2e+20387 Num(f64),388 /// Variable name: test389 Var(Spanned<IStr>),390391 /// Array of expressions: [1, 2, "Hello"]392 Arr(Rc<Vec<Spanned<Expr>>>),393 /// Array comprehension:394 /// ```jsonnet395 /// ingredients: [396 /// { kind: kind, qty: 4 / 3 }397 /// for kind in [398 /// 'Honey Syrup',399 /// 'Lemon Juice',400 /// 'Farmers Gin',401 /// ]402 /// ],403 /// ```404 ArrComp(Rc<Spanned<Expr>>, Vec<CompSpec>),405406 /// Object: {a: 2}407 Obj(ObjBody),408 /// Object extension: var1 {b: 2}409 ObjExtend(Rc<Spanned<Expr>>, ObjBody),410411 /// -2412 UnaryOp(UnaryOpType, Box<Spanned<Expr>>),413 /// 2 - 2414 BinaryOp(Box<BinaryOp>),415 /// assert 2 == 2 : "Math is broken"416 AssertExpr(Rc<AssertExpr>),417 /// local a = 2; { b: a }418 LocalExpr(Vec<BindSpec>, Box<Spanned<Expr>>),419420 /// import* "hello"421 Import(ImportKind, Box<Spanned<Expr>>),422 /// error "I'm broken"423 ErrorStmt(Box<Spanned<Expr>>),424 /// a(b, c)425 Apply(Box<Spanned<Expr>>, Spanned<ArgsDesc>, bool),426 /// a[b], a.b, a?.b427 Index {428 indexable: Box<Spanned<Expr>>,429 parts: Vec<IndexPart>,430 },431 /// function(x) x432 Function(ExprParams, Rc<Spanned<Expr>>),433 /// if true == false then 1 else 2434 IfElse(Box<IfElse>),435 Slice(Box<Slice>),436}437438#[derive(Debug, PartialEq, Acyclic)]439pub struct IndexPart {440 pub value: Spanned<Expr>,441 #[cfg(feature = "exp-null-coaelse")]442 pub null_coaelse: bool,443}444445/// file, begin offset, end offset446#[derive(Clone, PartialEq, Eq, Acyclic)]447#[repr(C)]448pub struct Span(pub Source, pub u32, pub u32);449impl Span {450 pub fn belongs_to(&self, other: &Span) -> bool {451 other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2452 }453}454455static_assertions::assert_eq_size!(Span, (usize, usize));456457impl Debug for Span {458 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {459 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)460 }461}462463#[derive(Clone, PartialEq, Acyclic)]464pub struct Spanned<T: Acyclic>(T, Span);465impl<T: Acyclic> Deref for Spanned<T> {466 type Target = T;467 fn deref(&self) -> &Self::Target {468 &self.0469 }470}471impl<T: Acyclic> Spanned<T> {472 #[inline]473 pub fn new(v: T, s: Span) -> Self {474 Self(v, s)475 }476 #[inline]477 pub fn span(&self) -> Span {478 self.1.clone()479 }480}481482impl<T: Debug + Acyclic> Debug for Spanned<T> {483 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {484 let expr = &**self;485 if f.alternate() {486 write!(f, "{:#?}", expr)?;487 } else {488 write!(f, "{:?}", expr)?;489 }490 write!(f, " from {:?}", self.span())?;491 Ok(())492 }493}crates/jrsonnet-peg-parser/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-peg-parser/Cargo.toml
+++ b/crates/jrsonnet-peg-parser/Cargo.toml
@@ -7,6 +7,7 @@
version.workspace = true
[dependencies]
+jrsonnet-gcmodule.workspace = true
jrsonnet-ir.workspace = true
peg.workspace = true
crates/jrsonnet-peg-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-peg-parser/src/lib.rs
+++ b/crates/jrsonnet-peg-parser/src/lib.rs
@@ -1,7 +1,9 @@
+use jrsonnet_gcmodule::Acyclic;
use jrsonnet_ir::{
- BinaryOp, Expr, ExprParams, IStr, IndexPart, Member, Slice, SliceDesc, Source, Span, Spanned,
- ExprParam, ArgsDesc, AssertExpr, ImportKind, LiteralType, IfElse, CompSpec, ForSpecData, IfSpecData, ObjMembers, ObjBody,
- ObjComp, FieldMember, Visibility, FieldName, unescape, AssertStmt, BindSpec, Destruct, DestructRest,
+ unescape, ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BindSpec, CompSpec, Destruct,
+ DestructRest, Expr, ExprParam, ExprParams, FieldMember, FieldName, ForSpecData, IStr, IfElse,
+ IfSpecData, ImportKind, IndexPart, LiteralType, Member, ObjBody, ObjComp, ObjMembers, Slice,
+ SliceDesc, Source, Span, Spanned, Visibility,
};
use peg::parser;
use std::rc::Rc;
@@ -63,7 +65,7 @@
= params:param(s) ** comma() comma()? { ExprParams::new(params) }
/ { ExprParams::new(Vec::new()) }
- pub rule arg(s: &ParserSettings) -> (Option<IStr>, Rc<Spanned<Expr>>)
+ pub rule arg(s: &ParserSettings) -> (Option<IStr>, Rc<Expr>)
= name:(quiet! { (s:id() _ "=" !['='] _ {s})? } / expected!("<argument name>")) expr:expr(s) {(name, Rc::new(expr))}
pub rule args(s: &ParserSettings) -> ArgsDesc
@@ -133,7 +135,7 @@
/ name:id() _ "(" _ params:params(s) _ ")" _ "=" _ value:expr(s) {BindSpec::Function{name, params, value: Rc::new(value)}}
pub rule assertion(s: &ParserSettings) -> AssertStmt
- = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { AssertStmt(cond, msg) }
+ = keyword("assert") _ cond:spanned(<expr(s)>, s) msg:(_ ":" _ e:spanned(<expr(s)>, s) {e})? { AssertStmt(cond, msg) }
pub rule whole_line() -> &'input str
= str:$((!['\n'][_])* "\n") {str}
@@ -241,7 +243,7 @@
pub rule forspec(s: &ParserSettings) -> ForSpecData
= keyword("for") _ id:destruct(s) _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}
rule compspec(s: &ParserSettings) -> CompSpec
- = i:ifspec(s) { CompSpec::IfSpec(i) } / f:forspec(s) {CompSpec::ForSpec(f)}
+ = i:spanned(<ifspec(s)>, s) { CompSpec::IfSpec(i) } / f:spanned(<forspec(s)>, s) {CompSpec::ForSpec(f)}
pub rule compspecs(s: &ParserSettings) -> Vec<CompSpec>
= specs:compspec(s) ++ _ {?
if !matches!(specs[0], CompSpec::ForSpec(_)) {
@@ -267,8 +269,12 @@
} else {
Err("!!!numbers are finite")
}}
+
+ rule spanned<T: Acyclic>(x: rule<T>, s: &ParserSettings) -> Spanned<T>
+ = a:position!() n:x() b:position!() { Spanned::new(n, Span(s.source.clone(), a as u32, b as u32)) }
+
pub rule var_expr(s: &ParserSettings) -> Expr
- = n:id() { Expr::Var(n) }
+ = n:spanned(<id()>, s) { Expr::Var(n) }
pub rule id_loc(s: &ParserSettings) -> Spanned<Expr>
= a:position!() n:id() b:position!() { Spanned::new(Expr::Str(n), Span(s.source.clone(), a as u32,b as u32)) }
pub rule if_then_else_expr(s: &ParserSettings) -> Expr
@@ -302,7 +308,7 @@
/ array_expr(s)
/ array_comp_expr(s)
- / kind:import_kind() _ path:expr(s) {Expr::Import(kind, Box::new(path))}
+ / kind:spanned(<import_kind()>, s) _ path:expr(s) {Expr::Import(kind, Box::new(path))}
/ var_expr(s)
/ local_expr(s)
@@ -313,10 +319,10 @@
assert, rest
})) }
- / keyword("error") _ expr:expr(s) { Expr::ErrorStmt(Box::new(expr)) }
+ / err_kw:spanned(<keyword("error")>, s) _ expr:expr(s) { Expr::ErrorStmt(err_kw.1, Box::new(expr)) }
rule slice_part(s: &ParserSettings) -> Option<Spanned<Expr>>
- = _ e:(e:expr(s) _{e})? {e}
+ = _ e:(e:spanned(<expr(s)>, s) _{e})? {e}
pub rule slice_desc(s: &ParserSettings) -> SliceDesc
= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {
let (end, step) = if let Some((end, step)) = pair {
@@ -340,11 +346,8 @@
}
use jrsonnet_ir::BinaryOpType::*;
use jrsonnet_ir::UnaryOpType::*;
- rule expr(s: &ParserSettings) -> Spanned<Expr>
+ rule expr(s: &ParserSettings) -> Expr
= precedence! {
- "(" _ e:expr(s) _ ")" {e}
- start:position!() v:@ end:position!() { Spanned::new(v, Span(s.source.clone(), start as u32, end as u32)) }
- --
a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}
a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {
#[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);
@@ -385,29 +388,32 @@
--
value:(@) _ "[" _ slice:slice_desc(s) _ "]" {Expr::Slice(Box::new(Slice{value, slice}))}
indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable: Box::new(indexable), parts}}
- a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(Box::new(a), args, ts.is_some())}
+ a:(@) _ args:spanned(<"(" _ a:args(s) _ ")" {a}>, s) ts:(_ keyword("tailstrict"))? {Expr::Apply(Box::new(a), args, ts.is_some())}
a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(Rc::new(a), body)}
--
e:expr_basic(s) {e}
+ "(" _ e:expr(s) _ ")" {e}
}
pub rule index_part(s: &ParserSettings) -> IndexPart
= n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {
- value,
+ span: value.1,
+ value: value.0,
#[cfg(feature = "exp-null-coaelse")]
null_coaelse: n.is_some(),
}}
- / n:("?" _ "." _ ensure_null_coaelse())? "[" _ value:expr(s) _ "]" {IndexPart {
- value,
+ / n:("?" _ "." _ ensure_null_coaelse())? value:spanned(<"[" _ v:expr(s) _ "]" {v}>, s) {IndexPart {
+ span: value.1,
+ value: value.0,
#[cfg(feature = "exp-null-coaelse")]
null_coaelse: n.is_some(),
}}
- pub rule jsonnet(s: &ParserSettings) -> Spanned<Expr> = _ e:expr(s) _ {e}
+ pub rule jsonnet(s: &ParserSettings) -> Expr = _ e:expr(s) _ {e}
}
}
pub type ParseError = peg::error::ParseError<peg::str::LineCol>;
-pub fn parse(str: &str, settings: &ParserSettings) -> Result<Spanned<Expr>, ParseError> {
+pub fn parse(str: &str, settings: &ParserSettings) -> Result<Expr, ParseError> {
jsonnet_parser::jsonnet(str, settings)
}
/// Used for importstr values