difftreelog
feat importbin
in: master
9 files changed
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -2,7 +2,7 @@
use jrsonnet_evaluator::{
error::{Error::*, Result},
- throw, EvaluationState, IStr, ImportResolver,
+ throw, EvaluationState, ImportResolver,
};
use std::{
any::Any,
@@ -29,8 +29,7 @@
pub struct CallbackImportResolver {
cb: JsonnetImportCallback,
ctx: *mut c_void,
-
- out: RefCell<HashMap<PathBuf, IStr>>,
+ out: RefCell<HashMap<PathBuf, Vec<u8>>>,
}
impl ImportResolver for CallbackImportResolver {
fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
@@ -75,9 +74,10 @@
Ok(found_here_buf.into())
}
- fn load_file_contents(&self, resolved: &Path) -> Result<IStr> {
+ fn load_file_contents(&self, resolved: &Path) -> Result<Vec<u8>> {
Ok(self.out.borrow().get(resolved).unwrap().clone())
}
+
unsafe fn as_any(&self) -> &dyn Any {
self
}
@@ -124,12 +124,12 @@
throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
}
}
- fn load_file_contents(&self, id: &Path) -> Result<IStr> {
+ fn load_file_contents(&self, id: &Path) -> Result<Vec<u8>> {
let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
- let mut out = String::new();
- file.read_to_string(&mut out)
- .map_err(|_e| ImportBadFileUtf8(id.to_owned()))?;
- Ok(out.into())
+ let mut out = Vec::new();
+ file.read_to_end(&mut out)
+ .map_err(|e| ImportIo(e.to_string()))?;
+ Ok(out)
}
unsafe fn as_any(&self) -> &dyn Any {
self
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -82,6 +82,8 @@
ResolvedFileNotFound(PathBuf),
#[error("imported file is not valid utf-8: {0:?}")]
ImportBadFileUtf8(PathBuf),
+ #[error("import io error: {0}")]
+ ImportIo(String),
#[error("tried to import {1} from {0}, but imports is not supported")]
ImportNotSupported(PathBuf, PathBuf),
#[error(
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -700,5 +700,12 @@
import_location.pop();
Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)
}
+ ImportBin(path) => {
+ let tmp = loc.clone().0;
+ let mut import_location = tmp.to_path_buf();
+ import_location.pop();
+ let bytes = with_state(|s| s.import_file_bin(&import_location, path))?;
+ Val::Arr(ArrValue::Bytes(bytes))
+ }
})
}
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -5,14 +5,12 @@
use fs::File;
use jrsonnet_interner::IStr;
use std::fs;
-use std::io::Read;
use std::{
any::Any,
- cell::RefCell,
- collections::HashMap,
path::{Path, PathBuf},
rc::Rc,
};
+use std::{convert::TryFrom, io::Read};
/// Implements file resolution logic for `import` and `importStr`
pub trait ImportResolver {
@@ -21,9 +19,19 @@
/// where `${vendor}` is a library path.
fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>>;
+ fn load_file_contents(&self, resolved: &Path) -> Result<Vec<u8>>;
+
/// Reads file from filesystem, should be used only with path received from `resolve_file`
- fn load_file_contents(&self, resolved: &Path) -> Result<IStr>;
+ fn load_file_str(&self, resolved: &Path) -> Result<IStr> {
+ Ok(IStr::try_from(&self.load_file_contents(resolved)? as &[u8])
+ .map_err(|_| ImportBadFileUtf8(resolved.to_path_buf()))?)
+ }
+ /// Reads file from filesystem, should be used only with path received from `resolve_file`
+ fn load_file_bin(&self, resolved: &Path) -> Result<Rc<[u8]>> {
+ Ok(self.load_file_contents(resolved)?.into())
+ }
+
/// # Safety
///
/// For use only in bindings, should not be used elsewhere.
@@ -39,8 +47,7 @@
throw!(ImportNotSupported(from.into(), path.into()))
}
- fn load_file_contents(&self, _resolved: &Path) -> Result<IStr> {
- // Can be only caused by library direct consumer, not by supplied jsonnet
+ fn load_file_contents(&self, _resolved: &Path) -> Result<Vec<u8>> {
panic!("dummy resolver can't load any file")
}
@@ -79,41 +86,12 @@
throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
}
}
- fn load_file_contents(&self, id: &Path) -> Result<IStr> {
+ fn load_file_contents(&self, id: &Path) -> Result<Vec<u8>> {
let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
- let mut out = String::new();
- file.read_to_string(&mut out)
- .map_err(|_e| ImportBadFileUtf8(id.to_owned()))?;
- Ok(out.into())
- }
- unsafe fn as_any(&self) -> &dyn Any {
- panic!("this resolver can't be used as any")
- }
-}
-
-type ResolutionData = (PathBuf, PathBuf);
-
-/// Caches results of the underlying resolver
-pub struct CachingImportResolver {
- resolution_cache: RefCell<HashMap<ResolutionData, Result<Rc<Path>>>>,
- loading_cache: RefCell<HashMap<PathBuf, Result<IStr>>>,
- inner: Box<dyn ImportResolver>,
-}
-impl ImportResolver for CachingImportResolver {
- fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
- self.resolution_cache
- .borrow_mut()
- .entry((from.to_owned(), path.to_owned()))
- .or_insert_with(|| self.inner.resolve_file(from, path))
- .clone()
- }
-
- fn load_file_contents(&self, resolved: &Path) -> Result<IStr> {
- self.loading_cache
- .borrow_mut()
- .entry(resolved.to_owned())
- .or_insert_with(|| self.inner.load_file_contents(resolved))
- .clone()
+ let mut out = Vec::new();
+ file.read_to_end(&mut out)
+ .map_err(|e| ImportIo(e.to_string()))?;
+ Ok(out)
}
unsafe fn as_any(&self) -> &dyn Any {
panic!("this resolver can't be used as any")
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -119,8 +119,9 @@
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>,
+ files: GcHashMap<Rc<Path>, FileData>,
+ str_files: GcHashMap<Rc<Path>, IStr>,
+ bin_files: GcHashMap<Rc<Path>, Rc<[u8]>>,
}
pub struct FileData {
@@ -276,18 +277,26 @@
return self.evaluate_loaded_file_raw(&file_path);
}
}
- let contents = self.load_file_contents(&file_path)?;
+ let contents = self.load_file_str(&file_path)?;
self.add_file(file_path.clone(), contents)?;
self.evaluate_loaded_file_raw(&file_path)
}
pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {
let path = self.resolve_file(from, path)?;
if !self.data().str_files.contains_key(&path) {
- let file_str = self.load_file_contents(&path)?;
+ let file_str = self.load_file_str(&path)?;
self.data_mut().str_files.insert(path.clone(), file_str);
}
Ok(self.data().str_files.get(&path).cloned().unwrap())
}
+ pub(crate) fn import_file_bin(&self, from: &Path, path: &Path) -> Result<Rc<[u8]>> {
+ let path = self.resolve_file(from, path)?;
+ if !self.data().bin_files.contains_key(&path) {
+ let file_bin = self.load_file_bin(&path)?;
+ self.data_mut().bin_files.insert(path.clone(), file_bin);
+ }
+ Ok(self.data().bin_files.get(&path).cloned().unwrap())
+ }
fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {
let expr: LocExpr = {
@@ -603,8 +612,11 @@
pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
self.settings().import_resolver.resolve_file(from, path)
}
- pub fn load_file_contents(&self, path: &Path) -> Result<IStr> {
- self.settings().import_resolver.load_file_contents(path)
+ pub fn load_file_str(&self, path: &Path) -> Result<IStr> {
+ self.settings().import_resolver.load_file_str(path)
+ }
+ pub fn load_file_bin(&self, path: &Path) -> Result<Rc<[u8]>> {
+ self.settings().import_resolver.load_file_bin(path)
}
pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {
@@ -661,7 +673,6 @@
EvaluationState,
};
use gcmodule::{Cc, Trace};
- use jrsonnet_interner::IStr;
use jrsonnet_parser::*;
use std::{
path::{Path, PathBuf},
@@ -1165,19 +1176,23 @@
Ok(())
}
- struct TestImportResolver(IStr);
+ struct TestImportResolver(Vec<u8>);
impl crate::import::ImportResolver for TestImportResolver {
fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {
Ok(PathBuf::from("/test").into())
}
- fn load_file_contents(&self, _: &Path) -> crate::error::Result<IStr> {
+ fn load_file_contents(&self, _: &Path) -> crate::error::Result<Vec<u8>> {
Ok(self.0.clone())
}
unsafe fn as_any(&self) -> &dyn std::any::Any {
panic!()
}
+
+ fn load_file_bin(&self, _resolved: &Path) -> crate::error::Result<Rc<[u8]>> {
+ panic!()
+ }
}
#[test]
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -186,6 +186,7 @@
#[derive(Debug, Clone, Trace)]
#[force_tracking]
pub enum ArrValue {
+ Bytes(#[skip_trace] Rc<[u8]>),
Lazy(Cc<Vec<LazyVal>>),
Eager(Cc<Vec<Val>>),
Extended(Box<(Self, Self)>),
@@ -197,6 +198,7 @@
pub fn len(&self) -> usize {
match self {
+ Self::Bytes(i) => i.len(),
Self::Lazy(l) => l.len(),
Self::Eager(e) => e.len(),
Self::Extended(v) => v.0.len() + v.1.len(),
@@ -209,6 +211,9 @@
pub fn get(&self, index: usize) -> Result<Option<Val>> {
match self {
+ Self::Bytes(i) => i
+ .get(index)
+ .map_or(Ok(None), |v| Ok(Some(Val::Num(*v as f64)))),
Self::Lazy(vec) => {
if let Some(v) = vec.get(index) {
Ok(Some(v.evaluate()?))
@@ -230,6 +235,9 @@
pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {
match self {
+ Self::Bytes(i) => i
+ .get(index)
+ .map(|b| LazyVal::new_resolved(Val::Num(*b as f64))),
Self::Lazy(vec) => vec.get(index).cloned(),
Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),
Self::Extended(v) => {
@@ -245,6 +253,13 @@
pub fn evaluated(&self) -> Result<Cc<Vec<Val>>> {
Ok(match self {
+ Self::Bytes(i) => {
+ let mut out = Vec::with_capacity(i.len());
+ for v in i.iter() {
+ out.push(Val::Num(*v as f64));
+ }
+ Cc::new(out)
+ }
Self::Lazy(vec) => {
let mut out = Vec::with_capacity(vec.len());
for item in vec.iter() {
@@ -265,6 +280,7 @@
pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {
(0..self.len()).map(move |idx| match self {
+ Self::Bytes(b) => Ok(Val::Num(b[idx] as f64)),
Self::Lazy(l) => l[idx].evaluate(),
Self::Eager(e) => Ok(e[idx].clone()),
Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),
@@ -273,6 +289,7 @@
pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {
(0..self.len()).map(move |idx| match self {
+ Self::Bytes(b) => LazyVal::new_resolved(Val::Num(b[idx] as f64)),
Self::Lazy(l) => l[idx].clone(),
Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),
Self::Extended(_) => self.get_lazy(idx).unwrap(),
@@ -281,6 +298,11 @@
pub fn reversed(self) -> Self {
match self {
+ Self::Bytes(b) => {
+ let mut out = b.to_vec();
+ out.reverse();
+ Self::Bytes(out.into())
+ }
Self::Lazy(vec) => {
let mut out = (&vec as &Vec<_>).clone();
out.reverse();
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -4,10 +4,12 @@
use std::{
borrow::Cow,
cell::RefCell,
+ convert::TryFrom,
fmt::{self, Display},
hash::{BuildHasherDefault, Hash, Hasher},
ops::Deref,
rc::Rc,
+ str::Utf8Error,
};
#[derive(Clone, PartialOrd, Ord, Eq)]
@@ -85,6 +87,15 @@
}
}
+impl TryFrom<&[u8]> for IStr {
+ type Error = Utf8Error;
+
+ fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
+ let str = std::str::from_utf8(value)?;
+ Ok(str.into())
+ }
+}
+
impl From<String> for IStr {
fn from(str: String) -> Self {
(&str as &str).into()
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth1use gcmodule::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)]17pub enum FieldName {18 /// {fixed: 2}19 Fixed(IStr),20 /// {["dyn"+"amic"]: 3}21 Dyn(LocExpr),22}2324#[cfg_attr(feature = "serialize", derive(Serialize))]25#[cfg_attr(feature = "deserialize", derive(Deserialize))]26#[derive(Debug, Clone, Copy, PartialEq, Trace)]27pub enum Visibility {28 /// :29 Normal,30 /// ::31 Hidden,32 /// :::33 Unhide,34}3536impl Visibility {37 pub fn is_visible(&self) -> bool {38 matches!(self, Self::Normal | Self::Unhide)39 }40}4142#[cfg_attr(feature = "serialize", derive(Serialize))]43#[cfg_attr(feature = "deserialize", derive(Deserialize))]44#[derive(Clone, Debug, PartialEq, Trace)]45pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);4647#[cfg_attr(feature = "serialize", derive(Serialize))]48#[cfg_attr(feature = "deserialize", derive(Deserialize))]49#[derive(Debug, PartialEq, Trace)]50pub struct FieldMember {51 pub name: FieldName,52 pub plus: bool,53 pub params: Option<ParamsDesc>,54 pub visibility: Visibility,55 pub value: LocExpr,56}5758#[cfg_attr(feature = "serialize", derive(Serialize))]59#[cfg_attr(feature = "deserialize", derive(Deserialize))]60#[derive(Debug, PartialEq, Trace)]61pub enum Member {62 Field(FieldMember),63 BindStmt(BindSpec),64 AssertStmt(AssertStmt),65}6667#[cfg_attr(feature = "serialize", derive(Serialize))]68#[cfg_attr(feature = "deserialize", derive(Deserialize))]69#[derive(Debug, Clone, Copy, PartialEq, Trace)]70pub enum UnaryOpType {71 Plus,72 Minus,73 BitNot,74 Not,75}7677impl Display for UnaryOpType {78 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {79 use UnaryOpType::*;80 write!(81 f,82 "{}",83 match self {84 Plus => "+",85 Minus => "-",86 BitNot => "~",87 Not => "!",88 }89 )90 }91}9293#[cfg_attr(feature = "serialize", derive(Serialize))]94#[cfg_attr(feature = "deserialize", derive(Deserialize))]95#[derive(Debug, Clone, Copy, PartialEq, Trace)]96pub enum BinaryOpType {97 Mul,98 Div,99100 /// Implemented as intrinsic, put here for completeness101 Mod,102103 Add,104 Sub,105106 Lhs,107 Rhs,108109 Lt,110 Gt,111 Lte,112 Gte,113114 BitAnd,115 BitOr,116 BitXor,117118 Eq,119 Neq,120121 And,122 Or,123124 // Equialent to std.objectHasEx(a, b, true)125 In,126}127128impl Display for BinaryOpType {129 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {130 use BinaryOpType::*;131 write!(132 f,133 "{}",134 match self {135 Mul => "*",136 Div => "/",137 Mod => "%",138 Add => "+",139 Sub => "-",140 Lhs => "<<",141 Rhs => ">>",142 Lt => "<",143 Gt => ">",144 Lte => "<=",145 Gte => ">=",146 BitAnd => "&",147 BitOr => "|",148 BitXor => "^",149 Eq => "==",150 Neq => "!=",151 And => "&&",152 Or => "||",153 In => "in",154 }155 )156 }157}158159/// name, default value160#[cfg_attr(feature = "serialize", derive(Serialize))]161#[cfg_attr(feature = "deserialize", derive(Deserialize))]162#[derive(Debug, PartialEq, Trace)]163pub struct Param(pub IStr, pub Option<LocExpr>);164165/// Defined function parameters166#[cfg_attr(feature = "serialize", derive(Serialize))]167#[cfg_attr(feature = "deserialize", derive(Deserialize))]168#[derive(Debug, Clone, PartialEq, Trace)]169pub struct ParamsDesc(pub Rc<Vec<Param>>);170171impl Deref for ParamsDesc {172 type Target = Vec<Param>;173 fn deref(&self) -> &Self::Target {174 &self.0175 }176}177178#[cfg_attr(feature = "serialize", derive(Serialize))]179#[cfg_attr(feature = "deserialize", derive(Deserialize))]180#[derive(Debug, PartialEq, Trace)]181pub struct ArgsDesc {182 pub unnamed: Vec<LocExpr>,183 pub named: Vec<(IStr, LocExpr)>,184}185impl ArgsDesc {186 pub fn new(unnamed: Vec<LocExpr>, named: Vec<(IStr, LocExpr)>) -> Self {187 Self { unnamed, named }188 }189}190191#[cfg_attr(feature = "serialize", derive(Serialize))]192#[cfg_attr(feature = "deserialize", derive(Deserialize))]193#[derive(Debug, Clone, PartialEq, Trace)]194pub struct BindSpec {195 pub name: IStr,196 pub params: Option<ParamsDesc>,197 pub value: LocExpr,198}199200#[cfg_attr(feature = "serialize", derive(Serialize))]201#[cfg_attr(feature = "deserialize", derive(Deserialize))]202#[derive(Debug, PartialEq, Trace)]203pub struct IfSpecData(pub LocExpr);204205#[cfg_attr(feature = "serialize", derive(Serialize))]206#[cfg_attr(feature = "deserialize", derive(Deserialize))]207#[derive(Debug, PartialEq, Trace)]208pub struct ForSpecData(pub IStr, pub LocExpr);209210#[cfg_attr(feature = "serialize", derive(Serialize))]211#[cfg_attr(feature = "deserialize", derive(Deserialize))]212#[derive(Debug, PartialEq, Trace)]213pub enum CompSpec {214 IfSpec(IfSpecData),215 ForSpec(ForSpecData),216}217218#[cfg_attr(feature = "serialize", derive(Serialize))]219#[cfg_attr(feature = "deserialize", derive(Deserialize))]220#[derive(Debug, PartialEq, Trace)]221pub struct ObjComp {222 pub pre_locals: Vec<BindSpec>,223 pub key: LocExpr,224 pub plus: bool,225 pub value: LocExpr,226 pub post_locals: Vec<BindSpec>,227 pub compspecs: Vec<CompSpec>,228}229230#[cfg_attr(feature = "serialize", derive(Serialize))]231#[cfg_attr(feature = "deserialize", derive(Deserialize))]232#[derive(Debug, PartialEq, Trace)]233pub enum ObjBody {234 MemberList(Vec<Member>),235 ObjComp(ObjComp),236}237238#[cfg_attr(feature = "serialize", derive(Serialize))]239#[cfg_attr(feature = "deserialize", derive(Deserialize))]240#[derive(Debug, PartialEq, Clone, Copy, Trace)]241pub enum LiteralType {242 This,243 Super,244 Dollar,245 Null,246 True,247 False,248}249250#[cfg_attr(feature = "serialize", derive(Serialize))]251#[cfg_attr(feature = "deserialize", derive(Deserialize))]252#[derive(Debug, PartialEq, Trace)]253pub struct SliceDesc {254 pub start: Option<LocExpr>,255 pub end: Option<LocExpr>,256 pub step: Option<LocExpr>,257}258259/// Syntax base260#[cfg_attr(feature = "serialize", derive(Serialize))]261#[cfg_attr(feature = "deserialize", derive(Deserialize))]262#[derive(Debug, PartialEq, Trace)]263pub enum Expr {264 Literal(LiteralType),265266 /// String value: "hello"267 Str(IStr),268 /// Number: 1, 2.0, 2e+20269 Num(f64),270 /// Variable name: test271 Var(IStr),272273 /// Array of expressions: [1, 2, "Hello"]274 Arr(Vec<LocExpr>),275 /// Array comprehension:276 /// ```jsonnet277 /// ingredients: [278 /// { kind: kind, qty: 4 / 3 }279 /// for kind in [280 /// 'Honey Syrup',281 /// 'Lemon Juice',282 /// 'Farmers Gin',283 /// ]284 /// ],285 /// ```286 ArrComp(LocExpr, Vec<CompSpec>),287288 /// Object: {a: 2}289 Obj(ObjBody),290 /// Object extension: var1 {b: 2}291 ObjExtend(LocExpr, ObjBody),292293 /// (obj)294 Parened(LocExpr),295296 /// -2297 UnaryOp(UnaryOpType, LocExpr),298 /// 2 - 2299 BinaryOp(LocExpr, BinaryOpType, LocExpr),300 /// assert 2 == 2 : "Math is broken"301 AssertExpr(AssertStmt, LocExpr),302 /// local a = 2; { b: a }303 LocalExpr(Vec<BindSpec>, LocExpr),304305 /// import "hello"306 Import(PathBuf),307 /// importStr "file.txt"308 ImportStr(PathBuf),309 /// error "I'm broken"310 ErrorStmt(LocExpr),311 /// a(b, c)312 Apply(LocExpr, ArgsDesc, bool),313 /// a[b]314 Index(LocExpr, LocExpr),315 /// function(x) x316 Function(ParamsDesc, LocExpr),317 /// std.primitiveEquals318 Intrinsic(IStr),319 /// if true == false then 1 else 2320 IfElse {321 cond: IfSpecData,322 cond_then: LocExpr,323 cond_else: Option<LocExpr>,324 },325 Slice(LocExpr, SliceDesc),326}327328/// file, begin offset, end offset329#[cfg_attr(feature = "serialize", derive(Serialize))]330#[cfg_attr(feature = "deserialize", derive(Deserialize))]331#[derive(Clone, PartialEq, Trace)]332#[skip_trace]333pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);334impl ExprLocation {335 pub fn belongs_to(&self, other: &ExprLocation) -> bool {336 other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2337 }338}339340impl Debug for ExprLocation {341 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {342 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)343 }344}345346/// Holds AST expression and its location in source file347#[cfg_attr(feature = "serialize", derive(Serialize))]348#[cfg_attr(feature = "deserialize", derive(Deserialize))]349#[derive(Clone, PartialEq, Trace)]350pub struct LocExpr(pub Rc<Expr>, pub ExprLocation);351352impl Debug for LocExpr {353 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {354 if f.alternate() {355 write!(f, "{:#?}", self.0)?;356 } else {357 write!(f, "{:?}", self.0)?;358 }359 write!(f, " from {:?}", self.1)?;360 Ok(())361 }362}1use gcmodule::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)]17pub enum FieldName {18 /// {fixed: 2}19 Fixed(IStr),20 /// {["dyn"+"amic"]: 3}21 Dyn(LocExpr),22}2324#[cfg_attr(feature = "serialize", derive(Serialize))]25#[cfg_attr(feature = "deserialize", derive(Deserialize))]26#[derive(Debug, Clone, Copy, PartialEq, Trace)]27pub enum Visibility {28 /// :29 Normal,30 /// ::31 Hidden,32 /// :::33 Unhide,34}3536impl Visibility {37 pub fn is_visible(&self) -> bool {38 matches!(self, Self::Normal | Self::Unhide)39 }40}4142#[cfg_attr(feature = "serialize", derive(Serialize))]43#[cfg_attr(feature = "deserialize", derive(Deserialize))]44#[derive(Clone, Debug, PartialEq, Trace)]45pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);4647#[cfg_attr(feature = "serialize", derive(Serialize))]48#[cfg_attr(feature = "deserialize", derive(Deserialize))]49#[derive(Debug, PartialEq, Trace)]50pub struct FieldMember {51 pub name: FieldName,52 pub plus: bool,53 pub params: Option<ParamsDesc>,54 pub visibility: Visibility,55 pub value: LocExpr,56}5758#[cfg_attr(feature = "serialize", derive(Serialize))]59#[cfg_attr(feature = "deserialize", derive(Deserialize))]60#[derive(Debug, PartialEq, Trace)]61pub enum Member {62 Field(FieldMember),63 BindStmt(BindSpec),64 AssertStmt(AssertStmt),65}6667#[cfg_attr(feature = "serialize", derive(Serialize))]68#[cfg_attr(feature = "deserialize", derive(Deserialize))]69#[derive(Debug, Clone, Copy, PartialEq, Trace)]70pub enum UnaryOpType {71 Plus,72 Minus,73 BitNot,74 Not,75}7677impl Display for UnaryOpType {78 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {79 use UnaryOpType::*;80 write!(81 f,82 "{}",83 match self {84 Plus => "+",85 Minus => "-",86 BitNot => "~",87 Not => "!",88 }89 )90 }91}9293#[cfg_attr(feature = "serialize", derive(Serialize))]94#[cfg_attr(feature = "deserialize", derive(Deserialize))]95#[derive(Debug, Clone, Copy, PartialEq, Trace)]96pub enum BinaryOpType {97 Mul,98 Div,99100 /// Implemented as intrinsic, put here for completeness101 Mod,102103 Add,104 Sub,105106 Lhs,107 Rhs,108109 Lt,110 Gt,111 Lte,112 Gte,113114 BitAnd,115 BitOr,116 BitXor,117118 Eq,119 Neq,120121 And,122 Or,123124 // Equialent to std.objectHasEx(a, b, true)125 In,126}127128impl Display for BinaryOpType {129 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {130 use BinaryOpType::*;131 write!(132 f,133 "{}",134 match self {135 Mul => "*",136 Div => "/",137 Mod => "%",138 Add => "+",139 Sub => "-",140 Lhs => "<<",141 Rhs => ">>",142 Lt => "<",143 Gt => ">",144 Lte => "<=",145 Gte => ">=",146 BitAnd => "&",147 BitOr => "|",148 BitXor => "^",149 Eq => "==",150 Neq => "!=",151 And => "&&",152 Or => "||",153 In => "in",154 }155 )156 }157}158159/// name, default value160#[cfg_attr(feature = "serialize", derive(Serialize))]161#[cfg_attr(feature = "deserialize", derive(Deserialize))]162#[derive(Debug, PartialEq, Trace)]163pub struct Param(pub IStr, pub Option<LocExpr>);164165/// Defined function parameters166#[cfg_attr(feature = "serialize", derive(Serialize))]167#[cfg_attr(feature = "deserialize", derive(Deserialize))]168#[derive(Debug, Clone, PartialEq, Trace)]169pub struct ParamsDesc(pub Rc<Vec<Param>>);170171impl Deref for ParamsDesc {172 type Target = Vec<Param>;173 fn deref(&self) -> &Self::Target {174 &self.0175 }176}177178#[cfg_attr(feature = "serialize", derive(Serialize))]179#[cfg_attr(feature = "deserialize", derive(Deserialize))]180#[derive(Debug, PartialEq, Trace)]181pub struct ArgsDesc {182 pub unnamed: Vec<LocExpr>,183 pub named: Vec<(IStr, LocExpr)>,184}185impl ArgsDesc {186 pub fn new(unnamed: Vec<LocExpr>, named: Vec<(IStr, LocExpr)>) -> Self {187 Self { unnamed, named }188 }189}190191#[cfg_attr(feature = "serialize", derive(Serialize))]192#[cfg_attr(feature = "deserialize", derive(Deserialize))]193#[derive(Debug, Clone, PartialEq, Trace)]194pub struct BindSpec {195 pub name: IStr,196 pub params: Option<ParamsDesc>,197 pub value: LocExpr,198}199200#[cfg_attr(feature = "serialize", derive(Serialize))]201#[cfg_attr(feature = "deserialize", derive(Deserialize))]202#[derive(Debug, PartialEq, Trace)]203pub struct IfSpecData(pub LocExpr);204205#[cfg_attr(feature = "serialize", derive(Serialize))]206#[cfg_attr(feature = "deserialize", derive(Deserialize))]207#[derive(Debug, PartialEq, Trace)]208pub struct ForSpecData(pub IStr, pub LocExpr);209210#[cfg_attr(feature = "serialize", derive(Serialize))]211#[cfg_attr(feature = "deserialize", derive(Deserialize))]212#[derive(Debug, PartialEq, Trace)]213pub enum CompSpec {214 IfSpec(IfSpecData),215 ForSpec(ForSpecData),216}217218#[cfg_attr(feature = "serialize", derive(Serialize))]219#[cfg_attr(feature = "deserialize", derive(Deserialize))]220#[derive(Debug, PartialEq, Trace)]221pub struct ObjComp {222 pub pre_locals: Vec<BindSpec>,223 pub key: LocExpr,224 pub plus: bool,225 pub value: LocExpr,226 pub post_locals: Vec<BindSpec>,227 pub compspecs: Vec<CompSpec>,228}229230#[cfg_attr(feature = "serialize", derive(Serialize))]231#[cfg_attr(feature = "deserialize", derive(Deserialize))]232#[derive(Debug, PartialEq, Trace)]233pub enum ObjBody {234 MemberList(Vec<Member>),235 ObjComp(ObjComp),236}237238#[cfg_attr(feature = "serialize", derive(Serialize))]239#[cfg_attr(feature = "deserialize", derive(Deserialize))]240#[derive(Debug, PartialEq, Clone, Copy, Trace)]241pub enum LiteralType {242 This,243 Super,244 Dollar,245 Null,246 True,247 False,248}249250#[cfg_attr(feature = "serialize", derive(Serialize))]251#[cfg_attr(feature = "deserialize", derive(Deserialize))]252#[derive(Debug, PartialEq, Trace)]253pub struct SliceDesc {254 pub start: Option<LocExpr>,255 pub end: Option<LocExpr>,256 pub step: Option<LocExpr>,257}258259/// Syntax base260#[cfg_attr(feature = "serialize", derive(Serialize))]261#[cfg_attr(feature = "deserialize", derive(Deserialize))]262#[derive(Debug, PartialEq, Trace)]263pub enum Expr {264 Literal(LiteralType),265266 /// String value: "hello"267 Str(IStr),268 /// Number: 1, 2.0, 2e+20269 Num(f64),270 /// Variable name: test271 Var(IStr),272273 /// Array of expressions: [1, 2, "Hello"]274 Arr(Vec<LocExpr>),275 /// Array comprehension:276 /// ```jsonnet277 /// ingredients: [278 /// { kind: kind, qty: 4 / 3 }279 /// for kind in [280 /// 'Honey Syrup',281 /// 'Lemon Juice',282 /// 'Farmers Gin',283 /// ]284 /// ],285 /// ```286 ArrComp(LocExpr, Vec<CompSpec>),287288 /// Object: {a: 2}289 Obj(ObjBody),290 /// Object extension: var1 {b: 2}291 ObjExtend(LocExpr, ObjBody),292293 /// (obj)294 Parened(LocExpr),295296 /// -2297 UnaryOp(UnaryOpType, LocExpr),298 /// 2 - 2299 BinaryOp(LocExpr, BinaryOpType, LocExpr),300 /// assert 2 == 2 : "Math is broken"301 AssertExpr(AssertStmt, LocExpr),302 /// local a = 2; { b: a }303 LocalExpr(Vec<BindSpec>, LocExpr),304305 /// import "hello"306 Import(PathBuf),307 /// importStr "file.txt"308 ImportStr(PathBuf),309 /// importBin "file.txt"310 ImportBin(PathBuf),311 /// error "I'm broken"312 ErrorStmt(LocExpr),313 /// a(b, c)314 Apply(LocExpr, ArgsDesc, bool),315 /// a[b]316 Index(LocExpr, LocExpr),317 /// function(x) x318 Function(ParamsDesc, LocExpr),319 /// std.primitiveEquals320 Intrinsic(IStr),321 /// if true == false then 1 else 2322 IfElse {323 cond: IfSpecData,324 cond_then: LocExpr,325 cond_else: Option<LocExpr>,326 },327 Slice(LocExpr, SliceDesc),328}329330/// file, begin offset, end offset331#[cfg_attr(feature = "serialize", derive(Serialize))]332#[cfg_attr(feature = "deserialize", derive(Deserialize))]333#[derive(Clone, PartialEq, Trace)]334#[skip_trace]335pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);336impl ExprLocation {337 pub fn belongs_to(&self, other: &ExprLocation) -> bool {338 other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2339 }340}341342impl Debug for ExprLocation {343 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {344 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)345 }346}347348/// Holds AST expression and its location in source file349#[cfg_attr(feature = "serialize", derive(Serialize))]350#[cfg_attr(feature = "deserialize", derive(Deserialize))]351#[derive(Clone, PartialEq, Trace)]352pub struct LocExpr(pub Rc<Expr>, pub ExprLocation);353354impl Debug for LocExpr {355 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {356 if f.alternate() {357 write!(f, "{:#?}", self.0)?;358 } else {359 write!(f, "{:?}", self.0)?;360 }361 write!(f, " from {:?}", self.1)?;362 Ok(())363 }364}crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -53,7 +53,7 @@
rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")
/// Reserved word followed by any non-alphanumberic
- rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()
+ rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()
rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")
rule keyword(id: &'static str) -> ()
@@ -218,6 +218,7 @@
/ array_comp_expr(s)
/ keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}
+ / keyword("importbin") _ path:string() {Expr::ImportBin(PathBuf::from(path))}
/ keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}
/ var_expr(s)