difftreelog
refactor add proper getters for LocExpr
in: master
10 files changed
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -53,8 +53,6 @@
serde.workspace = true
anyhow = { workspace = true, optional = true }
-# Serialized stdlib
-bincode = { workspace = true, optional = true }
# Explaining traces
annotate-snippets = { workspace = true, optional = true }
# Better explaining traces
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -7,7 +7,7 @@
use jrsonnet_gcmodule::Trace;
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{BinaryOpType, ExprLocation, LocExpr, Source, SourcePath, UnaryOpType};
+use jrsonnet_parser::{BinaryOpType, LocExpr, Source, SourcePath, Span, UnaryOpType};
use jrsonnet_types::ValType;
use thiserror::Error;
@@ -275,7 +275,7 @@
pub struct StackTraceElement {
/// Source of this frame
/// Some frames only act as description, without attached source
- pub location: Option<ExprLocation>,
+ pub location: Option<Span>,
/// Frame description
pub desc: String,
}
@@ -324,20 +324,20 @@
impl std::error::Error for Error {}
pub trait ErrorSource {
- fn to_location(self) -> Option<ExprLocation>;
+ fn to_location(self) -> Option<Span>;
}
impl ErrorSource for &LocExpr {
- fn to_location(self) -> Option<ExprLocation> {
- Some(self.1.clone())
+ fn to_location(self) -> Option<Span> {
+ Some(self.span())
}
}
-impl ErrorSource for &ExprLocation {
- fn to_location(self) -> Option<ExprLocation> {
+impl ErrorSource for &Span {
+ fn to_location(self) -> Option<Span> {
Some(self.clone())
}
}
impl ErrorSource for CallLocation<'_> {
- fn to_location(self) -> Option<ExprLocation> {
+ fn to_location(self) -> Option<Span> {
self.0.cloned()
}
}
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -26,7 +26,7 @@
pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {
fn is_trivial(expr: &LocExpr) -> bool {
- match &*expr.0 {
+ match expr.expr() {
Expr::Str(_)
| Expr::Num(_)
| Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,
@@ -35,7 +35,7 @@
_ => false,
}
}
- Some(match &*expr.0 {
+ Some(match expr.expr() {
Expr::Str(s) => Val::string(s.clone()),
Expr::Num(n) => {
Val::Num(NumValue::new(*n).expect("parser will not allow non-finite values"))
@@ -72,7 +72,7 @@
Ok(match field_name {
FieldName::Fixed(n) => Some(n.clone()),
FieldName::Dyn(expr) => State::push(
- CallLocation::new(&expr.1),
+ CallLocation::new(&expr.span()),
|| "evaluating field name".to_string(),
|| {
let value = evaluate(ctx, expr)?;
@@ -231,7 +231,7 @@
.field(name.clone())
.with_add(*plus)
.with_visibility(*visibility)
- .with_location(value.1.clone())
+ .with_location(value.span())
.bindable(UnboundValue {
uctx,
value: value.clone(),
@@ -266,7 +266,7 @@
builder
.field(name.clone())
.with_visibility(*visibility)
- .with_location(value.1.clone())
+ .with_location(value.span())
.bindable(UnboundMethod {
uctx,
value: value.clone(),
@@ -385,13 +385,13 @@
let value = &assertion.0;
let msg = &assertion.1;
let assertion_result = State::push(
- CallLocation::new(&value.1),
+ CallLocation::new(&value.span()),
|| "assertion condition".to_owned(),
|| bool::from_untyped(evaluate(ctx.clone(), value)?),
)?;
if !assertion_result {
State::push(
- CallLocation::new(&value.1),
+ CallLocation::new(&value.span()),
|| "assertion failure".to_owned(),
|| {
if let Some(msg) = msg {
@@ -406,8 +406,7 @@
pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {
use Expr::*;
- let LocExpr(raw_expr, _loc) = expr;
- Ok(match &**raw_expr {
+ Ok(match expr.expr() {
Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),
_ => evaluate(ctx, expr)?,
})
@@ -420,8 +419,8 @@
if let Some(trivial) = evaluate_trivial(expr) {
return Ok(trivial);
}
- let LocExpr(expr, loc) = expr;
- Ok(match &**expr {
+ let loc = expr.span();
+ Ok(match expr.expr() {
Literal(LiteralType::This) => {
Val::Obj(ctx.this().ok_or(CantUseSelfOutsideOfObject)?.clone())
}
@@ -448,7 +447,7 @@
// because the standalone super literal is not supported, that is because in other
// implementations `in super` treated differently from in `smth_else`.
BinaryOp(field, BinaryOpType::In, e)
- if matches!(&*e.0, Expr::Literal(LiteralType::Super)) =>
+ if matches!(e.expr(), Expr::Literal(LiteralType::Super)) =>
{
let Some(super_obj) = ctx.super_obj() else {
return Ok(Val::Bool(false));
@@ -459,52 +458,50 @@
BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,
UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,
Var(name) => State::push(
- CallLocation::new(loc),
+ CallLocation::new(&loc),
|| format!("variable <{name}> access"),
|| ctx.binding(name.clone())?.evaluate(),
)?,
Index { indexable, parts } => {
let mut parts = parts.iter();
- let mut indexable = match &indexable {
- // Cheaper to execute than creating object with overriden `this`
- LocExpr(v, _) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {
- let part = parts.next().expect("at least part should exist");
- let Some(super_obj) = ctx.super_obj() else {
- #[cfg(feature = "exp-null-coaelse")]
- if part.null_coaelse {
- return Ok(Val::Null);
- }
- bail!(NoSuperFound)
- };
- let name = evaluate(ctx.clone(), &part.value)?;
+ let mut indexable = if matches!(indexable.expr(), Expr::Literal(LiteralType::Super)) {
+ let part = parts.next().expect("at least part should exist");
+ let Some(super_obj) = ctx.super_obj() else {
+ #[cfg(feature = "exp-null-coaelse")]
+ if part.null_coaelse {
+ return Ok(Val::Null);
+ }
+ bail!(NoSuperFound)
+ };
+ let name = evaluate(ctx.clone(), &part.value)?;
- let Val::Str(name) = name else {
- bail!(ValueIndexMustBeTypeGot(
- ValType::Obj,
- ValType::Str,
- name.value_type(),
- ))
- };
+ let Val::Str(name) = name else {
+ bail!(ValueIndexMustBeTypeGot(
+ ValType::Obj,
+ ValType::Str,
+ name.value_type(),
+ ))
+ };
- let this = ctx
- .this()
- .expect("no this found, while super present, should not happen");
- let name = name.into_flat();
- match super_obj
- .get_for(name.clone(), this.clone())
- .with_description_src(&part.value, || format!("field <{name}> access"))?
- {
- Some(v) => v,
- #[cfg(feature = "exp-null-coaelse")]
- None if part.null_coaelse => return Ok(Val::Null),
- None => {
- let suggestions = suggest_object_fields(super_obj, name.clone());
+ let this = ctx
+ .this()
+ .expect("no this found, while super present, should not happen");
+ let name = name.into_flat();
+ match super_obj
+ .get_for(name.clone(), this.clone())
+ .with_description_src(&part.value, || format!("field <{name}> access"))?
+ {
+ Some(v) => v,
+ #[cfg(feature = "exp-null-coaelse")]
+ None if part.null_coaelse => return Ok(Val::Null),
+ None => {
+ let suggestions = suggest_object_fields(super_obj, name.clone());
- bail!(NoSuchField(name, suggestions))
- }
+ bail!(NoSuchField(name, suggestions))
}
}
- e => evaluate(ctx.clone(), e)?,
+ } else {
+ evaluate(ctx.clone(), indexable)?
};
for part in parts {
@@ -639,7 +636,7 @@
&Val::Obj(evaluate_object(ctx, b)?),
)?,
Apply(value, args, tailstrict) => {
- evaluate_apply(ctx, value, args, CallLocation::new(loc), *tailstrict)?
+ evaluate_apply(ctx, value, args, CallLocation::new(&loc), *tailstrict)?
}
Function(params, body) => {
evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())
@@ -649,7 +646,7 @@
evaluate(ctx, returned)?
}
ErrorStmt(e) => State::push(
- CallLocation::new(loc),
+ CallLocation::new(&loc),
|| "error statement".to_owned(),
|| bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),
)?,
@@ -659,7 +656,7 @@
cond_else,
} => {
if State::push(
- CallLocation::new(loc),
+ CallLocation::new(&loc),
|| "if condition".to_owned(),
|| bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),
)? {
@@ -690,7 +687,7 @@
}
let indexable = evaluate(ctx.clone(), value)?;
- let loc = CallLocation::new(loc);
+ let loc = CallLocation::new(&loc);
let start = parse_idx(loc, &ctx, desc.start.as_ref(), "start")?;
let end = parse_idx(loc, &ctx, desc.end.as_ref(), "end")?;
@@ -699,7 +696,7 @@
IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?
}
i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {
- let Expr::Str(path) = &*path.0 else {
+ let Expr::Str(path) = &path.expr() else {
bail!("computed imports are not supported")
};
let tmp = loc.clone().0;
@@ -707,7 +704,7 @@
let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;
match i {
Import(_) => State::push(
- CallLocation::new(loc),
+ CallLocation::new(&loc),
|| format!("import {:?}", path.clone()),
|| s.import_resolved(resolved_path),
)?,
crates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -4,7 +4,7 @@
use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
pub use jrsonnet_macros::builtin;
-use jrsonnet_parser::{Destruct, Expr, ExprLocation, LocExpr, ParamsDesc};
+use jrsonnet_parser::{Destruct, Expr, LocExpr, ParamsDesc, Span};
use self::{
arglike::OptionalContext,
@@ -22,10 +22,10 @@
/// Function callsite location.
/// Either from other jsonnet code, specified by expression location, or from native (without location).
#[derive(Clone, Copy)]
-pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);
+pub struct CallLocation<'l>(pub Option<&'l Span>);
impl<'l> CallLocation<'l> {
/// Construct new location for calls coming from specified jsonnet expression location.
- pub const fn new(loc: &'l ExprLocation) -> Self {
+ pub const fn new(loc: &'l Span) -> Self {
Self(Some(loc))
}
}
@@ -225,7 +225,7 @@
#[cfg(feature = "exp-destruct")]
_ => return false,
};
- &desc.body.0 as &Expr == &Expr::Var(id.clone())
+ desc.body.expr() == &Expr::Var(id.clone())
}
_ => false,
}
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -45,7 +45,7 @@
#[doc(hidden)]
pub use jrsonnet_macros;
pub use jrsonnet_parser as parser;
-use jrsonnet_parser::{ExprLocation, LocExpr, ParserSettings, Source, SourcePath};
+use jrsonnet_parser::{LocExpr, ParserSettings, Source, SourcePath, Span};
pub use obj::*;
use stack::check_depth;
pub use tla::apply_tla;
@@ -369,7 +369,7 @@
/// Executes code creating a new stack frame
pub fn push_val(
&self,
- e: &ExprLocation,
+ e: &Span,
frame_desc: impl FnOnce() -> String,
f: impl FnOnce() -> Result<Val>,
) -> Result<Val> {
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -8,7 +8,7 @@
use jrsonnet_gcmodule::{Cc, Trace, Weak};
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ExprLocation, Visibility};
+use jrsonnet_parser::{Span, Visibility};
use rustc_hash::FxHashMap;
use crate::{
@@ -135,7 +135,7 @@
flags: ObjFieldFlags,
original_index: FieldIndex,
pub invoke: MaybeUnbound,
- pub location: Option<ExprLocation>,
+ pub location: Option<Span>,
}
pub trait ObjectAssertion: Trace {
@@ -896,7 +896,7 @@
add: bool,
visibility: Visibility,
original_index: FieldIndex,
- location: Option<ExprLocation>,
+ location: Option<Span>,
}
#[allow(clippy::missing_const_for_fn)]
@@ -926,7 +926,7 @@
pub fn hide(self) -> Self {
self.with_visibility(Visibility::Hidden)
}
- pub fn with_location(mut self, location: ExprLocation) -> Self {
+ pub fn with_location(mut self, location: Span) -> Self {
self.location = Some(location);
self
}
crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -5,7 +5,7 @@
};
use jrsonnet_gcmodule::Trace;
-use jrsonnet_parser::{CodeLocation, ExprLocation, Source};
+use jrsonnet_parser::{CodeLocation, Source, Span};
use crate::{error::ErrorKind, Error};
@@ -380,7 +380,7 @@
error: &Error,
) -> Result<(), std::fmt::Error> {
struct ResetData {
- loc: ExprLocation,
+ loc: Span,
}
use hi_doc::{source_to_ansi, Formatting, SnippetBuilder, Text};
@@ -399,7 +399,7 @@
}
let trace = &error.trace();
let snippet_builder: RefCell<Option<SnippetBuilder>> = RefCell::new(None);
- let mut last_location: Option<ExprLocation> = None;
+ let mut last_location: Option<Span> = None;
let mut flush_builder = |data: Option<ResetData>| {
use std::fmt::Write;
let mut out = String::new();
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -376,7 +376,7 @@
State, Val,
function::{builtin::{Builtin, StaticBuiltin, BuiltinParam, ParamName, ParamDefault}, CallLocation, ArgsLike, parse::parse_builtin_call},
Result, Context, typed::Typed,
- parser::ExprLocation,
+ parser::Span,
};
const PARAMS: &'static [BuiltinParam] = &[
#(#params_desc)*
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -385,17 +385,16 @@
#[derive(Clone, PartialEq, Eq, Trace)]
#[trace(skip)]
#[repr(C)]
-pub struct ExprLocation(pub Source, pub u32, pub u32);
-impl ExprLocation {
- pub fn belongs_to(&self, other: &ExprLocation) -> bool {
+pub struct Span(pub Source, pub u32, pub u32);
+impl Span {
+ pub fn belongs_to(&self, other: &Span) -> bool {
other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2
}
}
-#[cfg(target_pointer_width = "64")]
-static_assertions::assert_eq_size!(ExprLocation, [u8; 16]);
+static_assertions::assert_eq_size!(Span, (usize, usize));
-impl Debug for ExprLocation {
+impl Debug for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)
}
@@ -403,19 +402,32 @@
/// Holds AST expression and its location in source file
#[derive(Clone, PartialEq, Trace)]
-pub struct LocExpr(pub Rc<Expr>, pub ExprLocation);
+pub struct LocExpr(Rc<(Expr, Span)>);
+impl LocExpr {
+ pub fn new(expr: Expr, span: Span) -> Self {
+ Self(Rc::new((expr, span)))
+ }
+ #[inline]
+ pub fn span(&self) -> Span {
+ self.0 .1.clone()
+ }
+ #[inline]
+ pub fn expr(&self) -> &Expr {
+ &self.0 .0
+ }
+}
-#[cfg(target_pointer_width = "64")]
-static_assertions::assert_eq_size!(LocExpr, [u8; 24]);
+static_assertions::assert_eq_size!(LocExpr, usize);
impl Debug for LocExpr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ let expr = self.expr();
if f.alternate() {
- write!(f, "{:#?}", self.0)?;
+ write!(f, "{:#?}", expr)?;
} else {
- write!(f, "{:?}", self.0)?;
+ write!(f, "{:?}", expr)?;
}
- write!(f, " from {:?}", self.1)?;
+ write!(f, " from {:?}", self.span())?;
Ok(())
}
}
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth1#![allow(clippy::redundant_closure_call, clippy::derive_partial_eq_without_eq)]23use std::rc::Rc;45use peg::parser;6mod expr;7pub use expr::*;8pub use jrsonnet_interner::IStr;9pub use peg;10mod location;11mod source;12mod unescape;13pub use location::CodeLocation;14pub use source::{15 Source, SourceDirectory, SourceFifo, SourceFile, SourcePath, SourcePathT, SourceVirtual,16};1718pub struct ParserSettings {19 pub source: Source,20}2122macro_rules! expr_bin {23 ($a:ident $op:ident $b:ident) => {24 Expr::BinaryOp($a, $op, $b)25 };26}27macro_rules! expr_un {28 ($op:ident $a:ident) => {29 Expr::UnaryOp($op, $a)30 };31}3233parser! {34 grammar jsonnet_parser() for str {35 use peg::ParseLiteral;3637 rule eof() = quiet!{![_]} / expected!("<eof>")38 rule eol() = "\n" / eof()3940 /// Standard C-like comments41 rule comment()42 = "//" (!eol()[_])* eol()43 / "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"44 / "#" (!eol()[_])* eol()4546 rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")47 rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")4849 /// For comma-delimited elements50 rule comma() = quiet!{_ "," _} / expected!("<comma>")51 rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}52 rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}53 rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']54 /// Sequence of digits55 rule uint_str() -> &'input str = a:$(digit()+) { a }56 /// Number in scientific notation format57 rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")5859 /// Reserved word followed by any non-alphanumberic60 rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()61 rule id() -> IStr = v:$(quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")) { v.into() }6263 rule keyword(id: &'static str) -> ()64 = ##parse_string_literal(id) end_of_ident()6566 pub rule param(s: &ParserSettings) -> expr::Param = name:destruct(s) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }67 pub rule params(s: &ParserSettings) -> expr::ParamsDesc68 = params:param(s) ** comma() comma()? { expr::ParamsDesc(Rc::new(params)) }69 / { expr::ParamsDesc(Rc::new(Vec::new())) }7071 pub rule arg(s: &ParserSettings) -> (Option<IStr>, LocExpr)72 = name:(quiet! { (s:id() _ "=" !['='] _ {s})? } / expected!("<argument name>")) expr:expr(s) {(name, expr)}7374 pub rule args(s: &ParserSettings) -> expr::ArgsDesc75 = args:arg(s)**comma() comma()? {?76 let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();77 let mut unnamed = Vec::with_capacity(unnamed_count);78 let mut named = Vec::with_capacity(args.len() - unnamed_count);79 let mut named_started = false;80 for (name, value) in args {81 if let Some(name) = name {82 named_started = true;83 named.push((name, value));84 } else {85 if named_started {86 return Err("<named argument>")87 }88 unnamed.push(value);89 }90 }91 Ok(expr::ArgsDesc::new(unnamed, named))92 }9394 pub rule destruct_rest() -> expr::DestructRest95 = "..." into:(_ into:id() {into})? {if let Some(into) = into {96 expr::DestructRest::Keep(into)97 } else {expr::DestructRest::Drop}}98 pub rule destruct_array(s: &ParserSettings) -> expr::Destruct99 = "[" _ start:destruct(s)**comma() rest:(100 comma() _ rest:destruct_rest()? end:(101 comma() end:destruct(s)**comma() (_ comma())? {end}102 / comma()? {Vec::new()}103 ) {(rest, end)}104 / comma()? {(None, Vec::new())}105 ) _ "]" {?106 #[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Array {107 start,108 rest: rest.0,109 end: rest.1,110 });111 #[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")112 }113 pub rule destruct_object(s: &ParserSettings) -> expr::Destruct114 = "{" _115 fields:(name:id() into:(_ ":" _ into:destruct(s) {into})? default:(_ "=" _ v:expr(s) {v})? {(name, into, default)})**comma()116 rest:(117 comma() rest:destruct_rest()? {rest}118 / comma()? {None}119 )120 _ "}" {?121 #[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Object {122 fields,123 rest,124 });125 #[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")126 }127 pub rule destruct(s: &ParserSettings) -> expr::Destruct128 = v:id() {expr::Destruct::Full(v)}129 / "?" {?130 #[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Skip);131 #[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")132 }133 / arr:destruct_array(s) {arr}134 / obj:destruct_object(s) {obj}135136 pub rule bind(s: &ParserSettings) -> expr::BindSpec137 = into:destruct(s) _ "=" _ expr:expr(s) {expr::BindSpec::Field{into, value: expr}}138 / name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec::Function{name, params, value: expr}}139140 pub rule assertion(s: &ParserSettings) -> expr::AssertStmt141 = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }142143 pub rule whole_line() -> &'input str144 = str:$((!['\n'][_])* "\n") {str}145 pub rule string_block() -> String146 = "|||" (!['\n']single_whitespace())* "\n"147 empty_lines:$(['\n']*)148 prefix:[' ' | '\t']+ first_line:whole_line()149 lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*150 [' ' | '\t']*<, {prefix.len() - 1}> "|||"151 {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}152153 rule hex_char()154 = quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")155156 rule string_char(c: rule<()>)157 = (!['\\']!c()[_])+158 / "\\\\"159 / "\\u" hex_char() hex_char() hex_char() hex_char()160 / "\\x" hex_char() hex_char()161 / ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))162 pub rule string() -> String163 = ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}164 / ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}165 / quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}166 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}167 / string_block() } / expected!("<string>")168169 pub rule field_name(s: &ParserSettings) -> expr::FieldName170 = name:id() {expr::FieldName::Fixed(name)}171 / name:string() {expr::FieldName::Fixed(name.into())}172 / "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}173 pub rule visibility() -> expr::Visibility174 = ":::" {expr::Visibility::Unhide}175 / "::" {expr::Visibility::Hidden}176 / ":" {expr::Visibility::Normal}177 pub rule field(s: &ParserSettings) -> expr::FieldMember178 = name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{179 name,180 plus: plus.is_some(),181 params: None,182 visibility,183 value,184 }}185 / name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{186 name,187 plus: false,188 params: Some(params),189 visibility,190 value,191 }}192 pub rule obj_local(s: &ParserSettings) -> BindSpec193 = keyword("local") _ bind:bind(s) {bind}194 pub rule member(s: &ParserSettings) -> expr::Member195 = bind:obj_local(s) {expr::Member::BindStmt(bind)}196 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}197 / field:field(s) {expr::Member::Field(field)}198 pub rule objinside(s: &ParserSettings) -> expr::ObjBody199 = pre_locals:(b: obj_local(s) comma() {b})* &"[" field:field(s) post_locals:(comma() b:obj_local(s) {b})* _ ("," _)? forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {200 let mut compspecs = vec![CompSpec::ForSpec(forspec)];201 compspecs.extend(others.unwrap_or_default());202 expr::ObjBody::ObjComp(expr::ObjComp{203 pre_locals,204 field,205 post_locals,206 compspecs,207 })208 }209 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}210 pub rule ifspec(s: &ParserSettings) -> IfSpecData211 = keyword("if") _ expr:expr(s) {IfSpecData(expr)}212 pub rule forspec(s: &ParserSettings) -> ForSpecData213 = keyword("for") _ id:destruct(s) _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}214 pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>215 = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}216 pub rule local_expr(s: &ParserSettings) -> Expr217 = keyword("local") _ binds:bind(s) ** comma() (_ ",")? _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }218 pub rule string_expr(s: &ParserSettings) -> Expr219 = s:string() {Expr::Str(s.into())}220 pub rule obj_expr(s: &ParserSettings) -> Expr221 = "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}222 pub rule array_expr(s: &ParserSettings) -> Expr223 = "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}224 pub rule array_comp_expr(s: &ParserSettings) -> Expr225 = "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {226 let mut specs = vec![CompSpec::ForSpec(forspec)];227 specs.extend(others.unwrap_or_default());228 Expr::ArrComp(expr, specs)229 }230 pub rule number_expr(s: &ParserSettings) -> Expr231 = n:number() { expr::Expr::Num(n) }232 pub rule var_expr(s: &ParserSettings) -> Expr233 = n:id() { expr::Expr::Var(n) }234 pub rule id_loc(s: &ParserSettings) -> LocExpr235 = a:position!() n:id() b:position!() { LocExpr(Rc::new(expr::Expr::Str(n)), ExprLocation(s.source.clone(), a as u32,b as u32)) }236 pub rule if_then_else_expr(s: &ParserSettings) -> Expr237 = cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{238 cond,239 cond_then,240 cond_else,241 }}242243 pub rule literal(s: &ParserSettings) -> Expr244 = v:(245 keyword("null") {LiteralType::Null}246 / keyword("true") {LiteralType::True}247 / keyword("false") {LiteralType::False}248 / keyword("self") {LiteralType::This}249 / keyword("$") {LiteralType::Dollar}250 / keyword("super") {LiteralType::Super}251 ) {Expr::Literal(v)}252253 pub rule expr_basic(s: &ParserSettings) -> Expr254 = literal(s)255256 / string_expr(s) / number_expr(s)257 / array_expr(s)258 / obj_expr(s)259 / array_expr(s)260 / array_comp_expr(s)261262 / keyword("importstr") _ path:expr(s) {Expr::ImportStr(path)}263 / keyword("importbin") _ path:expr(s) {Expr::ImportBin(path)}264 / keyword("import") _ path:expr(s) {Expr::Import(path)}265266 / var_expr(s)267 / local_expr(s)268 / if_then_else_expr(s)269270 / keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}271 / assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }272273 / keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }274275 rule slice_part(s: &ParserSettings) -> Option<LocExpr>276 = _ e:(e:expr(s) _{e})? {e}277 pub rule slice_desc(s: &ParserSettings) -> SliceDesc278 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {279 let (end, step) = if let Some((end, step)) = pair {280 (end, step)281 }else{282 (None, None)283 };284285 SliceDesc { start, end, step }286 }287288 rule binop(x: rule<()>) -> ()289 = quiet!{ x() } / expected!("<binary op>")290 rule unaryop(x: rule<()>) -> ()291 = quiet!{ x() } / expected!("<unary op>")292293 rule ensure_null_coaelse()294 = "" {?295 #[cfg(not(feature = "exp-null-coaelse"))] return Err("!!!experimental null coaelscing was not enabled");296 #[cfg(feature = "exp-null-coaelse")] Ok(())297 }298 use BinaryOpType::*;299 use UnaryOpType::*;300 rule expr(s: &ParserSettings) -> LocExpr301 = precedence! {302 start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.source.clone(), start as u32, end as u32)) }303 --304 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}305 a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {306 #[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);307 unreachable!("ensure_null_coaelse will fail if feature is not enabled")308 }309 --310 a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}311 --312 a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}313 --314 a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}315 --316 a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}317 --318 a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}319 a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}320 --321 a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}322 a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}323 a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}324 a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}325 a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}326 --327 a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}328 a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}329 --330 a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}331 a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}332 --333 a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}334 a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}335 a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}336 --337 unaryop(<"+">) _ b:@ {expr_un!(Plus b)}338 unaryop(<"-">) _ b:@ {expr_un!(Minus b)}339 unaryop(<"!">) _ b:@ {expr_un!(Not b)}340 unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}341 --342 a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}343 indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable, parts}}344 a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}345 a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}346 --347 e:expr_basic(s) {e}348 "(" _ e:expr(s) _ ")" {Expr::Parened(e)}349 }350 pub rule index_part(s: &ParserSettings) -> IndexPart351 = n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {352 value,353 #[cfg(feature = "exp-null-coaelse")]354 null_coaelse: n.is_some(),355 }}356 / n:("?" _ "." _ ensure_null_coaelse())? "[" _ value:expr(s) _ "]" {IndexPart {357 value,358 #[cfg(feature = "exp-null-coaelse")]359 null_coaelse: n.is_some(),360 }}361362 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}363 }364}365366pub type ParseError = peg::error::ParseError<peg::str::LineCol>;367pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {368 jsonnet_parser::jsonnet(str, settings)369}370/// Used for importstr values371pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {372 let len = str.len();373 LocExpr(374 Rc::new(Expr::Str(str)),375 ExprLocation(settings.source.clone(), 0, len as u32),376 )377}378379#[cfg(test)]380pub mod tests {381 use jrsonnet_interner::IStr;382 use BinaryOpType::*;383384 use super::{expr::*, parse};385 use crate::{source::Source, ParserSettings};386387 macro_rules! parse {388 ($s:expr) => {389 parse(390 $s,391 &ParserSettings {392 source: Source::new_virtual("<test>".into(), IStr::empty()),393 },394 )395 .unwrap()396 };397 }398399 macro_rules! el {400 ($expr:expr, $from:expr, $to:expr$(,)?) => {401 LocExpr(402 std::rc::Rc::new($expr),403 ExprLocation(404 Source::new_virtual("<test>".into(), IStr::empty()),405 $from,406 $to,407 ),408 )409 };410 }411412 #[test]413 fn multiline_string() {414 assert_eq!(415 parse!("|||\n Hello world!\n a\n|||"),416 el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),417 );418 assert_eq!(419 parse!("|||\n Hello world!\n a\n|||"),420 el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),421 );422 assert_eq!(423 parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),424 el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),425 );426 assert_eq!(427 parse!("|||\n Hello world!\n a\n |||"),428 el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),429 );430 }431432 #[test]433 fn slice() {434 parse!("a[1:]");435 parse!("a[1::]");436 parse!("a[:1:]");437 parse!("a[::1]");438 parse!("str[:len - 1]");439 }440441 #[test]442 fn string_escaping() {443 assert_eq!(444 parse!(r#""Hello, \"world\"!""#),445 el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),446 );447 assert_eq!(448 parse!(r#"'Hello \'world\'!'"#),449 el!(Expr::Str("Hello 'world'!".into()), 0, 18),450 );451 assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));452 }453454 #[test]455 fn string_unescaping() {456 assert_eq!(457 parse!(r#""Hello\nWorld""#),458 el!(Expr::Str("Hello\nWorld".into()), 0, 14),459 );460 }461462 #[test]463 fn string_verbantim() {464 assert_eq!(465 parse!(r#"@"Hello\n""World""""#),466 el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),467 );468 }469470 #[test]471 fn imports() {472 assert_eq!(473 parse!("import \"hello\""),474 el!(Expr::Import(el!(Expr::Str("hello".into()), 7, 14)), 0, 14),475 );476 assert_eq!(477 parse!("importstr \"garnish.txt\""),478 el!(479 Expr::ImportStr(el!(Expr::Str("garnish.txt".into()), 10, 23)),480 0,481 23482 )483 );484 assert_eq!(485 parse!("importbin \"garnish.bin\""),486 el!(487 Expr::ImportBin(el!(Expr::Str("garnish.bin".into()), 10, 23)),488 0,489 23490 )491 );492 }493494 #[test]495 fn empty_object() {496 assert_eq!(497 parse!("{}"),498 el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)499 );500 }501502 #[test]503 fn basic_math() {504 assert_eq!(505 parse!("2+2*2"),506 el!(507 Expr::BinaryOp(508 el!(Expr::Num(2.0), 0, 1),509 Add,510 el!(511 Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),512 2,513 5514 )515 ),516 0,517 5518 )519 );520 }521522 #[test]523 fn basic_math_with_indents() {524 assert_eq!(525 parse!("2 + 2 * 2 "),526 el!(527 Expr::BinaryOp(528 el!(Expr::Num(2.0), 0, 1),529 Add,530 el!(531 Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),532 7,533 14534 ),535 ),536 0,537 14538 )539 );540 }541542 #[test]543 fn basic_math_parened() {544 assert_eq!(545 parse!("2+(2+2*2)"),546 el!(547 Expr::BinaryOp(548 el!(Expr::Num(2.0), 0, 1),549 Add,550 el!(551 Expr::Parened(el!(552 Expr::BinaryOp(553 el!(Expr::Num(2.0), 3, 4),554 Add,555 el!(556 Expr::BinaryOp(557 el!(Expr::Num(2.0), 5, 6),558 Mul,559 el!(Expr::Num(2.0), 7, 8),560 ),561 5,562 8563 ),564 ),565 3,566 8567 )),568 2,569 9570 ),571 ),572 0,573 9574 )575 );576 }577578 /// Comments should not affect parsing579 #[test]580 fn comments() {581 assert_eq!(582 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),583 el!(584 Expr::BinaryOp(585 el!(Expr::Num(2.0), 0, 1),586 Add,587 el!(588 Expr::BinaryOp(589 el!(Expr::Num(3.0), 22, 23),590 Mul,591 el!(Expr::Num(4.0), 40, 41)592 ),593 22,594 41595 )596 ),597 0,598 41599 )600 );601 }602603 /// Comments should be able to be escaped604 #[test]605 fn comment_escaping() {606 assert_eq!(607 parse!("2/*\\*/+*/ - 22"),608 el!(609 Expr::BinaryOp(el!(Expr::Num(2.0), 0, 1), Sub, el!(Expr::Num(22.0), 12, 14)),610 0,611 14612 )613 );614 }615616 #[test]617 fn suffix() {618 // assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));619 // assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));620 // assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));621 // assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))622 }623624 #[test]625 fn array_comp() {626 use Expr::*;627 /*628 `ArrComp(Apply(Index(Var("std") from "test.jsonnet":1-4, Var("deepJoin") from "test.jsonnet":5-13) from "test.jsonnet":1-13, ArgsDesc { unnamed: [Var("x") from "test.jsonnet":14-15], named: [] }, false) from "test.jsonnet":1-16, [ForSpec(ForSpecData("x", Var("arr") from "test.jsonnet":26-29))]) from "test.jsonnet":0-30`,629 `ArrComp(Apply(Index(Var("std") from "test.jsonnet":1-4, Str("deepJoin") from "test.jsonnet":5-13) from "test.jsonnet":1-13, ArgsDesc { unnamed: [Var("x") from "test.jsonnet":14-15], named: [] }, false) from "test.jsonnet":1-16, [ForSpec(ForSpecData("x", Var("arr") from "test.jsonnet":26-29))]) from "test.jsonnet":0-30`630 */631 assert_eq!(632 parse!("[std.deepJoin(x) for x in arr]"),633 el!(634 ArrComp(635 el!(636 Apply(637 el!(638 Index {639 indexable: el!(Var("std".into()), 1, 4),640 parts: vec![IndexPart {641 value: el!(Str("deepJoin".into()), 5, 13),642 #[cfg(feature = "exp-null-coaelse")]643 null_coaelse: false,644 }],645 },646 1,647 13648 ),649 ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),650 false,651 ),652 1,653 16654 ),655 vec![CompSpec::ForSpec(ForSpecData(656 Destruct::Full("x".into()),657 el!(Var("arr".into()), 26, 29)658 ))]659 ),660 0,661 30662 ),663 )664 }665666 #[test]667 fn reserved() {668 use Expr::*;669 assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));670 assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));671 }672673 #[test]674 fn multiple_args_buf() {675 parse!("a(b, null_fields)");676 }677678 #[test]679 fn infix_precedence() {680 use Expr::*;681 assert_eq!(682 parse!("!a && !b"),683 el!(684 BinaryOp(685 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),686 And,687 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)688 ),689 0,690 8691 )692 );693 }694695 #[test]696 fn infix_precedence_division() {697 use Expr::*;698 assert_eq!(699 parse!("!a / !b"),700 el!(701 BinaryOp(702 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),703 Div,704 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)705 ),706 0,707 7708 )709 );710 }711712 #[test]713 fn double_negation() {714 use Expr::*;715 assert_eq!(716 parse!("!!a"),717 el!(718 UnaryOp(719 UnaryOpType::Not,720 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)721 ),722 0,723 3724 )725 )726 }727728 #[test]729 fn array_test_error() {730 parse!("[a for a in b if c for e in f]");731 // ^^^^ failed code732 }733734 #[test]735 fn missing_newline_between_comment_and_eof() {736 parse!(737 "{a:1}738739 //+213"740 );741 }742743 #[test]744 fn default_param_before_nondefault() {745 parse!("local x(foo = 'foo', bar) = null; null");746 }747748 #[test]749 fn add_location_info_to_all_sub_expressions() {750 use Expr::*;751752 let file_name = Source::new_virtual("<test>".into(), IStr::empty());753 let expr = parse(754 "{} { local x = 1, x: x } + {}",755 &ParserSettings { source: file_name },756 )757 .unwrap();758 assert_eq!(759 expr,760 el!(761 BinaryOp(762 el!(763 ObjExtend(764 el!(Obj(ObjBody::MemberList(vec![])), 0, 2),765 ObjBody::MemberList(vec![766 Member::BindStmt(BindSpec::Field {767 into: Destruct::Full("x".into()),768 value: el!(Num(1.0), 15, 16)769 }),770 Member::Field(FieldMember {771 name: FieldName::Fixed("x".into()),772 plus: false,773 params: None,774 visibility: Visibility::Normal,775 value: el!(Var("x".into()), 21, 22),776 })777 ])778 ),779 0,780 24781 ),782 BinaryOpType::Add,783 el!(Obj(ObjBody::MemberList(vec![])), 27, 29),784 ),785 0,786 29787 ),788 );789 }790}