difftreelog
refactor keep source code alongside source path
in: master
18 files changed
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -16,6 +16,7 @@
error::{Error::*, Result},
throw, ImportResolver, State,
};
+use jrsonnet_parser::SourcePath;
pub type JsonnetImportCallback = unsafe extern "C" fn(
ctx: *mut c_void,
@@ -29,10 +30,10 @@
pub struct CallbackImportResolver {
cb: JsonnetImportCallback,
ctx: *mut c_void,
- out: RefCell<HashMap<PathBuf, Vec<u8>>>,
+ out: RefCell<HashMap<SourcePath, Vec<u8>>>,
}
impl ImportResolver for CallbackImportResolver {
- fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+ fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
let base = CString::new(from.to_str().unwrap()).unwrap().into_raw();
let rel = CString::new(path).unwrap().into_raw();
let found_here: *mut c_char = null_mut();
@@ -61,7 +62,7 @@
}
let found_here_raw = unsafe { CStr::from_ptr(found_here) };
- let found_here_buf = PathBuf::from(found_here_raw.to_str().unwrap());
+ let found_here_buf = SourcePath::Path(PathBuf::from(found_here_raw.to_str().unwrap()));
unsafe {
let _ = CString::from_raw(found_here);
}
@@ -74,7 +75,7 @@
Ok(found_here_buf)
}
- fn load_file_contents(&self, resolved: &Path) -> Result<Vec<u8>> {
+ fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>> {
Ok(self.out.borrow().get(resolved).unwrap().clone())
}
@@ -108,24 +109,28 @@
}
}
impl ImportResolver for NativeImportResolver {
- fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+ fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
let mut new_path = from.to_owned();
new_path.push(path);
if new_path.exists() {
- Ok(new_path)
+ Ok(SourcePath::Path(new_path))
} else {
for library_path in self.library_paths.borrow().iter() {
let mut cloned = library_path.clone();
cloned.push(path);
if cloned.exists() {
- return Ok(cloned);
+ return Ok(SourcePath::Path(cloned));
}
}
throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
}
}
- fn load_file_contents(&self, id: &Path) -> Result<Vec<u8>> {
- let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
+ fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
+ let path = match id {
+ SourcePath::Path(path) => path,
+ _ => unreachable!("NativeImportResolver::resolve_file may only return plain paths"),
+ };
+ let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
let mut out = Vec::new();
file.read_to_end(&mut out)
.map_err(|e| ImportIo(e.to_string()))?;
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -10,9 +10,9 @@
use std::{
alloc::Layout,
+ env,
ffi::{CStr, CString},
os::raw::{c_char, c_double, c_int, c_uint},
- path::PathBuf,
};
use import::NativeImportResolver;
@@ -112,7 +112,10 @@
) -> *const c_char {
let filename = CStr::from_ptr(filename);
match vm
- .import(PathBuf::from(filename.to_str().unwrap()))
+ .import(
+ &env::current_dir().expect("cwd"),
+ filename.to_str().unwrap(),
+ )
.and_then(|v| vm.with_tla(v))
.and_then(|v| vm.manifest(v))
{
@@ -141,10 +144,7 @@
let filename = CStr::from_ptr(filename);
let snippet = CStr::from_ptr(snippet);
match vm
- .evaluate_snippet(
- filename.to_str().unwrap().into(),
- snippet.to_str().unwrap().into(),
- )
+ .evaluate_snippet(filename.to_str().unwrap().into(), snippet.to_str().unwrap())
.and_then(|v| vm.with_tla(v))
.and_then(|v| vm.manifest(v))
{
@@ -186,7 +186,10 @@
) -> *const c_char {
let filename = CStr::from_ptr(filename);
match vm
- .import(PathBuf::from(filename.to_str().unwrap()))
+ .import(
+ &env::current_dir().expect("cwd"),
+ filename.to_str().unwrap(),
+ )
.and_then(|v| vm.with_tla(v))
.and_then(|v| vm.manifest_multi(v))
{
@@ -213,10 +216,7 @@
let filename = CStr::from_ptr(filename);
let snippet = CStr::from_ptr(snippet);
match vm
- .evaluate_snippet(
- filename.to_str().unwrap().into(),
- snippet.to_str().unwrap().into(),
- )
+ .evaluate_snippet(filename.to_str().unwrap().into(), snippet.to_str().unwrap())
.and_then(|v| vm.with_tla(v))
.and_then(|v| vm.manifest_multi(v))
{
@@ -256,7 +256,10 @@
) -> *const c_char {
let filename = CStr::from_ptr(filename);
match vm
- .import(PathBuf::from(filename.to_str().unwrap()))
+ .import(
+ &env::current_dir().expect("cwd"),
+ filename.to_str().unwrap(),
+ )
.and_then(|v| vm.with_tla(v))
.and_then(|v| vm.manifest_stream(v))
{
@@ -283,10 +286,7 @@
let filename = CStr::from_ptr(filename);
let snippet = CStr::from_ptr(snippet);
match vm
- .evaluate_snippet(
- filename.to_str().unwrap().into(),
- snippet.to_str().unwrap().into(),
- )
+ .evaluate_snippet(filename.to_str().unwrap().into(), snippet.to_str().unwrap())
.and_then(|v| vm.with_tla(v))
.and_then(|v| vm.manifest_stream(v))
{
bindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/vars_tlas.rs
+++ b/bindings/jsonnet/src/vars_tlas.rs
@@ -32,7 +32,7 @@
.as_any()
.downcast_ref::<jrsonnet_stdlib::ContextInitializer>()
.expect("only stdlib context initializer supported")
- .add_ext_code(name.to_str().unwrap(), value.to_str().unwrap().into())
+ .add_ext_code(name.to_str().unwrap(), value.to_str().unwrap())
.unwrap()
}
/// # Safety
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -133,14 +133,14 @@
let input = opts.input.input.ok_or(Error::MissingInputArgument)?;
let val = if opts.input.exec {
- s.evaluate_snippet("<cmdline>".to_owned(), (&input as &str).into())?
+ s.evaluate_snippet("<cmdline>".to_owned(), &input as &str)?
} else if input == "-" {
let mut input = Vec::new();
std::io::stdin().read_to_end(&mut input)?;
- let input_str = std::str::from_utf8(&input)?.into();
+ let input_str = std::str::from_utf8(&input)?;
s.evaluate_snippet("<stdin>".to_owned(), input_str)?
} else {
- s.import(s.resolve_file(¤t_dir().expect("cwd"), &input)?)?
+ s.import(¤t_dir().expect("cwd"), &input)?
};
let val = s.with_tla(val)?;
crates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/stdlib.rs
+++ b/crates/jrsonnet-cli/src/stdlib.rs
@@ -118,10 +118,10 @@
ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
}
for ext in self.ext_code.iter() {
- ctx.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;
+ ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;
}
for ext in self.ext_code_file.iter() {
- ctx.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;
+ ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;
}
s.settings_mut().context_initializer = Box::new(ctx);
Ok(())
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,7 +2,7 @@
use jrsonnet_gcmodule::Trace;
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, UnaryOpType};
+use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, SourcePath, UnaryOpType};
use jrsonnet_types::ValType;
use thiserror::Error;
@@ -138,10 +138,10 @@
#[error("can't resolve {1} from {0}")]
ImportFileNotFound(PathBuf, String),
- #[error("resolved file not found: {0}")]
- ResolvedFileNotFound(PathBuf),
+ #[error("resolved file not found: {:?}", .0)]
+ ResolvedFileNotFound(SourcePath),
#[error("imported file is not valid utf-8: {0:?}")]
- ImportBadFileUtf8(PathBuf),
+ ImportBadFileUtf8(SourcePath),
#[error("import io error: {0}")]
ImportIo(String),
#[error("tried to import {1} from {0}, but imports is not supported")]
@@ -151,12 +151,11 @@
#[error(
"syntax error: expected {}, got {:?}",
.error.expected,
- .source_code.chars().nth(error.location.offset)
+ .path.code().chars().nth(error.location.offset)
.map_or_else(|| "EOF".into(), |c| c.to_string())
)]
ImportSyntaxError {
path: Source,
- source_code: IStr,
#[trace(skip)]
error: Box<jrsonnet_parser::ParseError>,
},
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -416,13 +416,13 @@
Literal(LiteralType::This) => {
Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
}
- Literal(LiteralType::Super) => {
- Val::Obj(ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
+ Literal(LiteralType::Super) => Val::Obj(
+ ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
ctx.this()
.clone()
.expect("if super exists - then this should to"),
- ))
- }
+ ),
+ ),
Literal(LiteralType::Dollar) => {
Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)
}
@@ -643,10 +643,10 @@
Import(_) => s.push(
CallLocation::new(loc),
|| format!("import {:?}", path.clone()),
- || s.import(resolved_path.clone()),
+ || s.import_resolved(resolved_path),
)?,
- ImportStr(_) => Val::Str(s.import_str(resolved_path)?),
- ImportBin(_) => Val::Arr(ArrValue::Bytes(s.import_bin(resolved_path)?)),
+ ImportStr(_) => Val::Str(s.import_resolved_str(resolved_path)?),
+ ImportBin(_) => Val::Arr(ArrValue::Bytes(s.import_resolved_bin(resolved_path)?)),
_ => unreachable!(),
}
}
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -6,6 +6,7 @@
};
use fs::File;
+use jrsonnet_parser::SourcePath;
use crate::{
error::{Error::*, Result},
@@ -17,9 +18,12 @@
/// Resolves real file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond
/// both to `/home/user/manifests/b.libjsonnet` and to `/home/user/${vendor}/b.libjsonnet`
/// where `${vendor}` is a library path.
- fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf>;
+ fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath>;
- fn load_file_contents(&self, resolved: &Path) -> Result<Vec<u8>>;
+ /// Load resolved file
+ /// This should only be called with value returned from `resolve_file`, this cannot be resolved using associated type,
+ /// as evaluator uses object instead of generic for [`ImportResolver`]
+ fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>>;
/// # Safety
///
@@ -32,11 +36,11 @@
/// Dummy resolver, can't resolve/load any file
pub struct DummyImportResolver;
impl ImportResolver for DummyImportResolver {
- fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+ fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
throw!(ImportNotSupported(from.into(), path.into()))
}
- fn load_file_contents(&self, _resolved: &Path) -> Result<Vec<u8>> {
+ fn load_file_contents(&self, _resolved: &SourcePath) -> Result<Vec<u8>> {
panic!("dummy resolver can't load any file")
}
@@ -59,25 +63,35 @@
pub library_paths: Vec<PathBuf>,
}
impl ImportResolver for FileImportResolver {
- fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+ fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
let mut direct = from.to_path_buf();
direct.push(path);
if direct.exists() {
- Ok(direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?)
+ Ok(SourcePath::Path(
+ direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
+ ))
} else {
for library_path in &self.library_paths {
let mut cloned = library_path.clone();
cloned.push(path);
if cloned.exists() {
- return Ok(cloned.canonicalize().map_err(|e| ImportIo(e.to_string()))?);
+ return Ok(SourcePath::Path(
+ cloned.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
+ ));
}
}
throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
}
}
- fn load_file_contents(&self, id: &Path) -> Result<Vec<u8>> {
- let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
+ fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
+ let path = match id {
+ SourcePath::Path(path) => path,
+ _ => {
+ panic!("this resolver can only resolve to path")
+ }
+ };
+ let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
let mut out = Vec::new();
file.read_to_end(&mut out)
.map_err(|e| ImportIo(e.to_string()))?;
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -48,7 +48,7 @@
cell::{Ref, RefCell, RefMut},
collections::HashMap,
fmt::{self, Debug},
- path::{Path, PathBuf},
+ path::Path,
rc::Rc,
};
@@ -65,7 +65,7 @@
pub use jrsonnet_parser as parser;
use jrsonnet_parser::*;
pub use obj::*;
-use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};
+use trace::{CompactFormat, TraceFormat};
pub use val::{ManifestFormat, Thunk, Val};
pub trait Unbound: Trace {
@@ -170,10 +170,7 @@
breakpoints: Breakpoints,
/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
- files: GcHashMap<PathBuf, FileData>,
- /// Contains tla arguments and others, which aren't needed to be obtained by name, however may be used for receiving source
- /// TODO: look into nix approach, storing source code in `Source` object
- volatile_files: GcHashMap<String, String>,
+ files: GcHashMap<SourcePath, FileData>,
}
struct FileData {
string: Option<IStr>,
@@ -249,7 +246,8 @@
pub struct State(Rc<EvaluationStateInternals>);
impl State {
- pub fn import_str(&self, path: PathBuf) -> Result<IStr> {
+ /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
+ pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {
let mut data = self.data_mut();
let mut file = data.files.raw_entry_mut().from_key(&path);
@@ -283,7 +281,8 @@
}
Ok(file.string.as_ref().expect("just set").clone())
}
- pub fn import_bin(&self, path: PathBuf) -> Result<IBytes> {
+ /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
+ pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {
let mut data = self.data_mut();
let mut file = data.files.raw_entry_mut().from_key(&path);
@@ -309,7 +308,8 @@
}
Ok(file.bytes.as_ref().expect("just set").clone())
}
- pub fn import(&self, path: PathBuf) -> Result<Val> {
+ /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
+ pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {
let mut data = self.data_mut();
let mut file = data.files.raw_entry_mut().from_key(&path);
@@ -343,7 +343,8 @@
);
}
let code = file.string.as_ref().expect("just set");
- let file_name = Source::new(path.clone()).expect("resolver should return correct name");
+ let file_name =
+ Source::new(path.clone(), code.clone()).expect("resolver should return correct name");
if file.parsed.is_none() {
file.parsed = Some(
jrsonnet_parser::parse(
@@ -354,7 +355,6 @@
)
.map_err(|e| ImportSyntaxError {
path: file_name.clone(),
- source_code: code.clone(),
error: Box::new(e),
})?,
);
@@ -386,34 +386,11 @@
Ok(v)
}
Err(e) => Err(e),
- }
- }
-
- pub fn get_source(&self, name: Source) -> Option<String> {
- let data = self.data();
- match name.repr() {
- Ok(real) => data
- .files
- .get(real)
- .and_then(|f| f.string.as_ref())
- .map(ToString::to_string),
- Err(e) => data.volatile_files.get(e).map(ToOwned::to_owned),
}
- }
- pub fn map_source_locations(&self, file: Source, locs: &[u32]) -> Vec<CodeLocation> {
- offset_to_location(&self.get_source(file).unwrap_or_else(|| "".into()), locs)
}
- pub fn map_from_source_location(
- &self,
- file: Source,
- line: usize,
- column: usize,
- ) -> Option<usize> {
- location_to_offset(
- &self.get_source(file).expect("file not found"),
- line,
- column,
- )
+ pub fn import(&self, from: &Path, path: &str) -> Result<Val> {
+ let resolved = self.resolve_file(from, path)?;
+ self.import_resolved(resolved)
}
/// Creates context with all passed global variables
@@ -554,7 +531,10 @@
|| {
func.evaluate(
self.clone(),
- self.create_default_context(Source::new_virtual(Cow::Borrowed("<tla>"))),
+ self.create_default_context(Source::new_virtual(
+ Cow::Borrowed("<tla>"),
+ IStr::empty(),
+ )),
CallLocation::native(),
&self.settings().tla_vars,
true,
@@ -568,9 +548,9 @@
/// Internals
impl State {
- fn data(&self) -> Ref<EvaluationData> {
- self.0.data.borrow()
- }
+ // fn data(&self) -> Ref<EvaluationData> {
+ // self.0.data.borrow()
+ // }
fn data_mut(&self) -> RefMut<EvaluationData> {
self.0.data.borrow_mut()
}
@@ -585,8 +565,9 @@
/// Raw methods evaluate passed values but don't perform TLA execution
impl State {
/// Parses and evaluates the given snippet
- pub fn evaluate_snippet(&self, name: String, code: String) -> Result<Val> {
- let source = Source::new_virtual(Cow::Owned(name.clone()));
+ pub fn evaluate_snippet(&self, name: String, code: impl Into<IStr>) -> Result<Val> {
+ let code = code.into();
+ let source = Source::new_virtual(Cow::Owned(name), code.clone());
let parsed = jrsonnet_parser::parse(
&code,
&ParserSettings {
@@ -595,10 +576,8 @@
)
.map_err(|e| ImportSyntaxError {
path: source.clone(),
- source_code: code.clone().into(),
error: Box::new(e),
})?;
- self.data_mut().volatile_files.insert(name, code);
evaluate(self.clone(), self.create_default_context(source), &parsed)
}
}
@@ -617,7 +596,7 @@
}
pub fn add_tla_code(&self, name: IStr, code: &str) -> Result<()> {
let source_name = format!("<top-level-arg:{}>", name);
- let source = Source::new_virtual(Cow::Owned(source_name.clone()));
+ let source = Source::new_virtual(Cow::Owned(source_name.clone()), code.into());
let parsed = jrsonnet_parser::parse(
code,
&ParserSettings {
@@ -626,22 +605,18 @@
)
.map_err(|e| ImportSyntaxError {
path: source,
- source_code: code.into(),
error: Box::new(e),
})?;
- self.data_mut()
- .volatile_files
- .insert(source_name, code.to_owned());
self.settings_mut()
.tla_vars
.insert(name, TlaArg::Code(parsed));
Ok(())
}
- pub fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+ pub fn resolve_file(&self, from: &Path, path: &str) -> Result<SourcePath> {
self.settings()
.import_resolver
- .resolve_file(from, path.as_ref())
+ .resolve_file_relative(from, path.as_ref())
}
pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {
crates/jrsonnet-evaluator/src/trace/location.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/location.rs
+++ /dev/null
@@ -1,124 +0,0 @@
-#[allow(clippy::module_name_repetitions)]
-#[derive(Clone, PartialEq, Eq, Debug)]
-pub struct CodeLocation {
- pub offset: usize,
-
- pub line: usize,
- pub column: usize,
-
- pub line_start_offset: usize,
- pub line_end_offset: usize,
-}
-
-#[allow(clippy::module_name_repetitions)]
-pub fn location_to_offset(mut file: &str, mut line: usize, column: usize) -> Option<usize> {
- let mut offset = 0;
- while line > 1 {
- let pos = file.find('\n')?;
- offset += pos + 1;
- file = &file[pos + 1..];
- line -= 1;
- }
- offset += column - 1;
- Some(offset)
-}
-
-#[allow(clippy::module_name_repetitions)]
-pub fn offset_to_location(file: &str, offsets: &[u32]) -> Vec<CodeLocation> {
- if offsets.is_empty() {
- return vec![];
- }
- let mut line = 1;
- let mut column = 1;
- let max_offset = *offsets.iter().max().expect("offsets is not empty");
-
- let mut offset_map = offsets
- .iter()
- .enumerate()
- .map(|(pos, offset)| (*offset, pos))
- .collect::<Vec<_>>();
- offset_map.sort_by_key(|v| v.0);
- offset_map.reverse();
-
- let mut out = vec![
- CodeLocation {
- offset: 0,
- column: 0,
- line: 0,
- line_start_offset: 0,
- line_end_offset: 0
- };
- offsets.len()
- ];
- let mut with_no_known_line_ending = vec![];
- let mut this_line_offset = 0;
- for (pos, ch) in file
- .chars()
- .enumerate()
- .chain(std::iter::once((file.len(), ' ')))
- {
- column += 1;
- match offset_map.last() {
- Some(x) if x.0 == pos as u32 => {
- let out_idx = x.1;
- with_no_known_line_ending.push(out_idx);
- out[out_idx].offset = pos;
- out[out_idx].line = line;
- out[out_idx].column = column;
- out[out_idx].line_start_offset = this_line_offset;
- offset_map.pop();
- }
- _ => {}
- }
- if ch == '\n' {
- line += 1;
- column = 1;
-
- for idx in with_no_known_line_ending.drain(..) {
- out[idx].line_end_offset = pos;
- }
- this_line_offset = pos + 1;
-
- if pos == max_offset as usize + 1 {
- break;
- }
- }
- }
- let file_end = file.chars().count();
- for idx in with_no_known_line_ending {
- out[idx].line_end_offset = file_end;
- }
-
- out
-}
-
-#[cfg(test)]
-pub mod tests {
- use super::{offset_to_location, CodeLocation};
-
- #[test]
- fn test() {
- assert_eq!(
- offset_to_location(
- "hello world\n_______________________________________________________",
- &[0, 14]
- ),
- vec![
- CodeLocation {
- offset: 0,
- line: 1,
- column: 2,
- line_start_offset: 0,
- line_end_offset: 11,
- },
- CodeLocation {
- offset: 14,
- line: 2,
- column: 4,
- line_start_offset: 12,
- line_end_offset: 67
- }
- ]
- )
- }
-}
crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -1,9 +1,6 @@
-mod location;
-
use std::path::{Path, PathBuf};
-use jrsonnet_parser::Source;
-pub use location::*;
+use jrsonnet_parser::{CodeLocation, Source};
use crate::{error::Error, LocError, State};
@@ -84,31 +81,27 @@
fn write_trace(
&self,
out: &mut dyn std::fmt::Write,
- s: &State,
+ _s: &State,
error: &LocError,
) -> Result<(), std::fmt::Error> {
write!(out, "{}", error.error())?;
- if let Error::ImportSyntaxError {
- path,
- source_code,
- error,
- } = error.error()
- {
+ if let Error::ImportSyntaxError { path, error } = error.error() {
use std::fmt::Write;
writeln!(out)?;
- let mut n = match path.repr() {
- Ok(r) => self.resolver.resolve(r),
- Err(v) => v.to_string(),
+ let mut n = match path.path() {
+ Some(r) => self.resolver.resolve(r),
+ None => path.short_display().to_string(),
};
let mut offset = error.location.offset;
- let is_eof = if offset >= source_code.len() {
- offset = source_code.len().saturating_sub(1);
+ let is_eof = if offset >= path.code().len() {
+ offset = path.code().len().saturating_sub(1);
true
} else {
false
};
- let mut location = offset_to_location(source_code, &[offset as u32])
+ let mut location = path
+ .map_source_locations(&[offset as u32])
.into_iter()
.next()
.unwrap();
@@ -129,13 +122,12 @@
use std::fmt::Write;
#[allow(clippy::option_if_let_else)]
if let Some(location) = location {
- let mut resolved_path = match location.0.repr() {
- Ok(r) => self.resolver.resolve(r),
- Err(v) => v.to_string(),
+ let mut resolved_path = match location.0.path() {
+ Some(r) => self.resolver.resolve(r),
+ None => location.0.short_display().to_string(),
};
// TODO: Process all trace elements first
- let location =
- s.map_source_locations(location.0.clone(), &[location.1, location.2]);
+ let location = location.0.map_source_locations(&[location.1, location.2]);
write!(resolved_path, ":").unwrap();
print_code_location(&mut resolved_path, &location[0], &location[1]).unwrap();
write!(resolved_path, ":").unwrap();
@@ -176,7 +168,7 @@
fn write_trace(
&self,
out: &mut dyn std::fmt::Write,
- s: &State,
+ _s: &State,
error: &LocError,
) -> Result<(), std::fmt::Error> {
write!(out, "{}", error.error())?;
@@ -184,10 +176,10 @@
writeln!(out)?;
let desc = &item.desc;
if let Some(source) = &item.location {
- let start_end = s.map_source_locations(source.0.clone(), &[source.1, source.2]);
- let resolved_path = match source.0.repr() {
- Ok(r) => r.display().to_string(),
- Err(v) => v.to_string(),
+ let start_end = source.0.map_source_locations(&[source.1, source.2]);
+ let resolved_path = match source.0.path() {
+ Some(r) => r.display().to_string(),
+ None => source.0.short_display().to_string(),
};
write!(
@@ -213,19 +205,15 @@
fn write_trace(
&self,
out: &mut dyn std::fmt::Write,
- s: &State,
+ _s: &State,
error: &LocError,
) -> Result<(), std::fmt::Error> {
write!(out, "{}", error.error())?;
- if let Error::ImportSyntaxError {
- path,
- source_code,
- error,
- } = error.error()
- {
+ if let Error::ImportSyntaxError { path, error } = error.error() {
writeln!(out)?;
let offset = error.location.offset;
- let location = offset_to_location(source_code, &[offset as u32])
+ let location = path
+ .map_source_locations(&[offset as u32])
.into_iter()
.next()
.unwrap();
@@ -234,7 +222,7 @@
self.print_snippet(
out,
- source_code,
+ path.code(),
path,
&location,
&end_location,
@@ -246,10 +234,10 @@
writeln!(out)?;
let desc = &item.desc;
if let Some(source) = &item.location {
- let start_end = s.map_source_locations(source.0.clone(), &[source.1, source.2]);
+ let start_end = source.0.map_source_locations(&[source.1, source.2]);
self.print_snippet(
out,
- &s.get_source(source.0.clone()).unwrap(),
+ &source.0.code(),
&source.0,
&start_end[0],
&start_end[1],
@@ -284,9 +272,9 @@
.take(end.line_end_offset - end.line_start_offset)
.collect();
- let origin = match origin.repr() {
- Ok(r) => self.resolver.resolve(r),
- Err(v) => v.to_string(),
+ let origin = match origin.path() {
+ Some(r) => self.resolver.resolve(r),
+ None => origin.short_display().to_string(),
};
let snippet = Snippet {
opt: FormatOptions {
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -33,6 +33,10 @@
impl IStr {
#[must_use]
+ pub fn empty() -> Self {
+ "".into()
+ }
+ #[must_use]
pub fn as_str(&self) -> &str {
self as &str
}
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 source;11mod unescape;12pub use source::Source;1314pub struct ParserSettings {15 pub file_name: Source,16}1718macro_rules! expr_bin {19 ($a:ident $op:ident $b:ident) => {20 Expr::BinaryOp($a, $op, $b)21 };22}23macro_rules! expr_un {24 ($op:ident $a:ident) => {25 Expr::UnaryOp($op, $a)26 };27}2829parser! {30 grammar jsonnet_parser() for str {31 use peg::ParseLiteral;3233 rule eof() = quiet!{![_]} / expected!("<eof>")34 rule eol() = "\n" / eof()3536 /// Standard C-like comments37 rule comment()38 = "//" (!eol()[_])* eol()39 / "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"40 / "#" (!eol()[_])* eol()4142 rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")43 rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")4445 /// For comma-delimited elements46 rule comma() = quiet!{_ "," _} / expected!("<comma>")47 rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}48 rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}49 rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']50 /// Sequence of digits51 rule uint_str() -> &'input str = a:$(digit()+) { a }52 /// Number in scientific notation format53 rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")5455 /// Reserved word followed by any non-alphanumberic56 rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()57 rule id() -> IStr = v:$(quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")) { v.into() }5859 rule keyword(id: &'static str) -> ()60 = ##parse_string_literal(id) end_of_ident()6162 pub rule param(s: &ParserSettings) -> expr::Param = name:destruct(s) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }63 pub rule params(s: &ParserSettings) -> expr::ParamsDesc64 = params:param(s) ** comma() comma()? { expr::ParamsDesc(Rc::new(params)) }65 / { expr::ParamsDesc(Rc::new(Vec::new())) }6667 pub rule arg(s: &ParserSettings) -> (Option<IStr>, LocExpr)68 = quiet! { name:(s:id() _ "=" !['='] _ {s})? expr:expr(s) {(name, expr)} }69 / expected!("<argument>")7071 pub rule args(s: &ParserSettings) -> expr::ArgsDesc72 = args:arg(s)**comma() comma()? {?73 let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();74 let mut unnamed = Vec::with_capacity(unnamed_count);75 let mut named = Vec::with_capacity(args.len() - unnamed_count);76 let mut named_started = false;77 for (name, value) in args {78 if let Some(name) = name {79 named_started = true;80 named.push((name, value));81 } else {82 if named_started {83 return Err("<named argument>")84 }85 unnamed.push(value);86 }87 }88 Ok(expr::ArgsDesc::new(unnamed, named))89 }9091 pub rule destruct_rest() -> expr::DestructRest92 = "..." into:(_ into:id() {into})? {if let Some(into) = into {93 expr::DestructRest::Keep(into)94 } else {expr::DestructRest::Drop}}95 pub rule destruct_array(s: &ParserSettings) -> expr::Destruct96 = "[" _ start:destruct(s)**comma() rest:(97 comma() _ rest:destruct_rest()? end:(98 comma() end:destruct(s)**comma() (_ comma())? {end}99 / comma()? {Vec::new()}100 ) {(rest, end)}101 / comma()? {(None, Vec::new())}102 ) _ "]" {?103 #[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Array {104 start,105 rest: rest.0,106 end: rest.1,107 });108 #[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")109 }110 pub rule destruct_object(s: &ParserSettings) -> expr::Destruct111 = "{" _112 fields:(name:id() into:(_ ":" _ into:destruct(s) {into})? default:(_ "=" _ v:expr(s) {v})? {(name, into, default)})**comma()113 rest:(114 comma() rest:destruct_rest()? {rest}115 / comma()? {None}116 )117 _ "}" {?118 #[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Object {119 fields,120 rest,121 });122 #[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")123 }124 pub rule destruct(s: &ParserSettings) -> expr::Destruct125 = v:id() {expr::Destruct::Full(v)}126 / "?" {?127 #[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Skip);128 #[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")129 }130 / arr:destruct_array(s) {arr}131 / obj:destruct_object(s) {obj}132133 pub rule bind(s: &ParserSettings) -> expr::BindSpec134 = into:destruct(s) _ "=" _ expr:expr(s) {expr::BindSpec::Field{into, value: expr}}135 / name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec::Function{name, params, value: expr}}136137 pub rule assertion(s: &ParserSettings) -> expr::AssertStmt138 = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }139140 pub rule whole_line() -> &'input str141 = str:$((!['\n'][_])* "\n") {str}142 pub rule string_block() -> String143 = "|||" (!['\n']single_whitespace())* "\n"144 empty_lines:$(['\n']*)145 prefix:[' ' | '\t']+ first_line:whole_line()146 lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*147 [' ' | '\t']*<, {prefix.len() - 1}> "|||"148 {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}149150 rule hex_char()151 = quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")152153 rule string_char(c: rule<()>)154 = (!['\\']!c()[_])+155 / "\\\\"156 / "\\u" hex_char() hex_char() hex_char() hex_char()157 / "\\x" hex_char() hex_char()158 / ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))159 pub rule string() -> String160 = ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}161 / ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}162 / quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}163 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}164 / string_block() } / expected!("<string>")165166 pub rule field_name(s: &ParserSettings) -> expr::FieldName167 = name:id() {expr::FieldName::Fixed(name)}168 / name:string() {expr::FieldName::Fixed(name.into())}169 / "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}170 pub rule visibility() -> expr::Visibility171 = ":::" {expr::Visibility::Unhide}172 / "::" {expr::Visibility::Hidden}173 / ":" {expr::Visibility::Normal}174 pub rule field(s: &ParserSettings) -> expr::FieldMember175 = name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{176 name,177 plus: plus.is_some(),178 params: None,179 visibility,180 value,181 }}182 / name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{183 name,184 plus: false,185 params: Some(params),186 visibility,187 value,188 }}189 pub rule obj_local(s: &ParserSettings) -> BindSpec190 = keyword("local") _ bind:bind(s) {bind}191 pub rule member(s: &ParserSettings) -> expr::Member192 = bind:obj_local(s) {expr::Member::BindStmt(bind)}193 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}194 / field:field(s) {expr::Member::Field(field)}195 pub rule objinside(s: &ParserSettings) -> expr::ObjBody196 = pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ plus:"+"? _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {197 let mut compspecs = vec![CompSpec::ForSpec(forspec)];198 compspecs.extend(others.unwrap_or_default());199 expr::ObjBody::ObjComp(expr::ObjComp{200 pre_locals,201 key,202 plus: plus.is_some(),203 value,204 post_locals,205 compspecs,206 })207 }208 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}209 pub rule ifspec(s: &ParserSettings) -> IfSpecData210 = keyword("if") _ expr:expr(s) {IfSpecData(expr)}211 pub rule forspec(s: &ParserSettings) -> ForSpecData212 = keyword("for") _ id:id() _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}213 pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>214 = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}215 pub rule local_expr(s: &ParserSettings) -> Expr216 = keyword("local") _ binds:bind(s) ** comma() (_ ",")? _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }217 pub rule string_expr(s: &ParserSettings) -> Expr218 = s:string() {Expr::Str(s.into())}219 pub rule obj_expr(s: &ParserSettings) -> Expr220 = "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}221 pub rule array_expr(s: &ParserSettings) -> Expr222 = "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}223 pub rule array_comp_expr(s: &ParserSettings) -> Expr224 = "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {225 let mut specs = vec![CompSpec::ForSpec(forspec)];226 specs.extend(others.unwrap_or_default());227 Expr::ArrComp(expr, specs)228 }229 pub rule number_expr(s: &ParserSettings) -> Expr230 = n:number() { expr::Expr::Num(n) }231 pub rule var_expr(s: &ParserSettings) -> Expr232 = n:id() { expr::Expr::Var(n) }233 pub rule id_loc(s: &ParserSettings) -> LocExpr234 = a:position!() n:id() b:position!() { LocExpr(Rc::new(expr::Expr::Str(n)), ExprLocation(s.file_name.clone(), a as u32,b as u32)) }235 pub rule if_then_else_expr(s: &ParserSettings) -> Expr236 = cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{237 cond,238 cond_then,239 cond_else,240 }}241242 pub rule literal(s: &ParserSettings) -> Expr243 = v:(244 keyword("null") {LiteralType::Null}245 / keyword("true") {LiteralType::True}246 / keyword("false") {LiteralType::False}247 / keyword("self") {LiteralType::This}248 / keyword("$") {LiteralType::Dollar}249 / keyword("super") {LiteralType::Super}250 ) {Expr::Literal(v)}251252 pub rule expr_basic(s: &ParserSettings) -> Expr253 = literal(s)254255 / string_expr(s) / number_expr(s)256 / array_expr(s)257 / obj_expr(s)258 / array_expr(s)259 / array_comp_expr(s)260261 / keyword("importstr") _ path:string() {Expr::ImportStr(path.into())}262 / keyword("importbin") _ path:string() {Expr::ImportBin(path.into())}263 / keyword("import") _ path:string() {Expr::Import(path.into())}264265 / var_expr(s)266 / local_expr(s)267 / if_then_else_expr(s)268269 / keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}270 / assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }271272 / keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }273274 rule slice_part(s: &ParserSettings) -> Option<LocExpr>275 = _ e:(e:expr(s) _{e})? {e}276 pub rule slice_desc(s: &ParserSettings) -> SliceDesc277 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {278 let (end, step) = if let Some((end, step)) = pair {279 (end, step)280 }else{281 (None, None)282 };283284 SliceDesc { start, end, step }285 }286287 rule binop(x: rule<()>) -> ()288 = quiet!{ x() } / expected!("<binary op>")289 rule unaryop(x: rule<()>) -> ()290 = quiet!{ x() } / expected!("<unary op>")291292293 use BinaryOpType::*;294 use UnaryOpType::*;295 rule expr(s: &ParserSettings) -> LocExpr296 = precedence! {297 start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.file_name.clone(), start as u32, end as u32)) }298 --299 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}300 --301 a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}302 --303 a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}304 --305 a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}306 --307 a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}308 --309 a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}310 a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}311 --312 a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}313 a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}314 a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}315 a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}316 a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}317 --318 a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}319 a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}320 --321 a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}322 a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}323 --324 a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}325 a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}326 a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}327 --328 unaryop(<"-">) _ b:@ {expr_un!(Minus b)}329 unaryop(<"!">) _ b:@ {expr_un!(Not b)}330 unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}331 --332 a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}333 a:(@) _ "." _ a:position!() e:id_loc(s) b:position!() {Expr::Index(a, e)}334 a:(@) _ "[" _ e:expr(s) _ "]" {Expr::Index(a, e)}335 a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}336 a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}337 --338 e:expr_basic(s) {e}339 "(" _ e:expr(s) _ ")" {Expr::Parened(e)}340 }341342 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}343 }344}345346pub type ParseError = peg::error::ParseError<peg::str::LineCol>;347pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {348 jsonnet_parser::jsonnet(str, settings)349}350/// Used for importstr values351pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {352 let len = str.len();353 LocExpr(354 Rc::new(Expr::Str(str)),355 ExprLocation(settings.file_name.clone(), 0, len as u32),356 )357}358359#[cfg(test)]360pub mod tests {361 use std::borrow::Cow;362363 use BinaryOpType::*;364365 use super::{expr::*, parse};366 use crate::{source::Source, ParserSettings};367368 macro_rules! parse {369 ($s:expr) => {370 parse(371 $s,372 &ParserSettings {373 file_name: Source::new_virtual(Cow::Borrowed("<test>")),374 },375 )376 .unwrap()377 };378 }379380 macro_rules! el {381 ($expr:expr, $from:expr, $to:expr$(,)?) => {382 LocExpr(383 std::rc::Rc::new($expr),384 ExprLocation(Source::new_virtual(Cow::Borrowed("<test>")), $from, $to),385 )386 };387 }388389 #[test]390 fn multiline_string() {391 assert_eq!(392 parse!("|||\n Hello world!\n a\n|||"),393 el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),394 );395 assert_eq!(396 parse!("|||\n Hello world!\n a\n|||"),397 el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),398 );399 assert_eq!(400 parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),401 el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),402 );403 assert_eq!(404 parse!("|||\n Hello world!\n a\n |||"),405 el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),406 );407 }408409 #[test]410 fn slice() {411 parse!("a[1:]");412 parse!("a[1::]");413 parse!("a[:1:]");414 parse!("a[::1]");415 parse!("str[:len - 1]");416 }417418 #[test]419 fn string_escaping() {420 assert_eq!(421 parse!(r#""Hello, \"world\"!""#),422 el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),423 );424 assert_eq!(425 parse!(r#"'Hello \'world\'!'"#),426 el!(Expr::Str("Hello 'world'!".into()), 0, 18),427 );428 assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));429 }430431 #[test]432 fn string_unescaping() {433 assert_eq!(434 parse!(r#""Hello\nWorld""#),435 el!(Expr::Str("Hello\nWorld".into()), 0, 14),436 );437 }438439 #[test]440 fn string_verbantim() {441 assert_eq!(442 parse!(r#"@"Hello\n""World""""#),443 el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),444 );445 }446447 #[test]448 fn imports() {449 assert_eq!(450 parse!("import \"hello\""),451 el!(Expr::Import("hello".into()), 0, 14),452 );453 assert_eq!(454 parse!("importstr \"garnish.txt\""),455 el!(Expr::ImportStr("garnish.txt".into()), 0, 23)456 );457 assert_eq!(458 parse!("importbin \"garnish.bin\""),459 el!(Expr::ImportBin("garnish.bin".into()), 0, 23)460 );461 }462463 #[test]464 fn empty_object() {465 assert_eq!(466 parse!("{}"),467 el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)468 );469 }470471 #[test]472 fn basic_math() {473 assert_eq!(474 parse!("2+2*2"),475 el!(476 Expr::BinaryOp(477 el!(Expr::Num(2.0), 0, 1),478 Add,479 el!(480 Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),481 2,482 5483 )484 ),485 0,486 5487 )488 );489 }490491 #[test]492 fn basic_math_with_indents() {493 assert_eq!(494 parse!("2 + 2 * 2 "),495 el!(496 Expr::BinaryOp(497 el!(Expr::Num(2.0), 0, 1),498 Add,499 el!(500 Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),501 7,502 14503 ),504 ),505 0,506 14507 )508 );509 }510511 #[test]512 fn basic_math_parened() {513 assert_eq!(514 parse!("2+(2+2*2)"),515 el!(516 Expr::BinaryOp(517 el!(Expr::Num(2.0), 0, 1),518 Add,519 el!(520 Expr::Parened(el!(521 Expr::BinaryOp(522 el!(Expr::Num(2.0), 3, 4),523 Add,524 el!(525 Expr::BinaryOp(526 el!(Expr::Num(2.0), 5, 6),527 Mul,528 el!(Expr::Num(2.0), 7, 8),529 ),530 5,531 8532 ),533 ),534 3,535 8536 )),537 2,538 9539 ),540 ),541 0,542 9543 )544 );545 }546547 /// Comments should not affect parsing548 #[test]549 fn comments() {550 assert_eq!(551 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),552 el!(553 Expr::BinaryOp(554 el!(Expr::Num(2.0), 0, 1),555 Add,556 el!(557 Expr::BinaryOp(558 el!(Expr::Num(3.0), 22, 23),559 Mul,560 el!(Expr::Num(4.0), 40, 41)561 ),562 22,563 41564 )565 ),566 0,567 41568 )569 );570 }571572 /// Comments should be able to be escaped573 #[test]574 fn comment_escaping() {575 assert_eq!(576 parse!("2/*\\*/+*/ - 22"),577 el!(578 Expr::BinaryOp(el!(Expr::Num(2.0), 0, 1), Sub, el!(Expr::Num(22.0), 12, 14)),579 0,580 14581 )582 );583 }584585 #[test]586 fn suffix() {587 // assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));588 // assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));589 // assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));590 // assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))591 }592593 #[test]594 fn array_comp() {595 use Expr::*;596 /*597 `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`,598 `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`599 */600 assert_eq!(601 parse!("[std.deepJoin(x) for x in arr]"),602 el!(603 ArrComp(604 el!(605 Apply(606 el!(607 Index(608 el!(Var("std".into()), 1, 4),609 el!(Str("deepJoin".into()), 5, 13)610 ),611 1,612 13613 ),614 ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),615 false,616 ),617 1,618 16619 ),620 vec![CompSpec::ForSpec(ForSpecData(621 "x".into(),622 el!(Var("arr".into()), 26, 29)623 ))]624 ),625 0,626 30627 ),628 )629 }630631 #[test]632 fn reserved() {633 use Expr::*;634 assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));635 assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));636 }637638 #[test]639 fn multiple_args_buf() {640 parse!("a(b, null_fields)");641 }642643 #[test]644 fn infix_precedence() {645 use Expr::*;646 assert_eq!(647 parse!("!a && !b"),648 el!(649 BinaryOp(650 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),651 And,652 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)653 ),654 0,655 8656 )657 );658 }659660 #[test]661 fn infix_precedence_division() {662 use Expr::*;663 assert_eq!(664 parse!("!a / !b"),665 el!(666 BinaryOp(667 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),668 Div,669 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)670 ),671 0,672 7673 )674 );675 }676677 #[test]678 fn double_negation() {679 use Expr::*;680 assert_eq!(681 parse!("!!a"),682 el!(683 UnaryOp(684 UnaryOpType::Not,685 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)686 ),687 0,688 3689 )690 )691 }692693 #[test]694 fn array_test_error() {695 parse!("[a for a in b if c for e in f]");696 // ^^^^ failed code697 }698699 #[test]700 fn missing_newline_between_comment_and_eof() {701 parse!(702 "{a:1}703704 //+213"705 );706 }707708 #[test]709 fn default_param_before_nondefault() {710 parse!("local x(foo = 'foo', bar) = null; null");711 }712713 #[test]714 fn can_parse_stdlib() {715 parse!(jrsonnet_stdlib::STDLIB_STR);716 }717718 #[test]719 fn add_location_info_to_all_sub_expressions() {720 use Expr::*;721722 let file_name = Source::new_virtual(Cow::Borrowed("<test>"));723 let expr = parse(724 "{} { local x = 1, x: x } + {}",725 &ParserSettings { file_name },726 )727 .unwrap();728 assert_eq!(729 expr,730 el!(731 BinaryOp(732 el!(733 ObjExtend(734 el!(Obj(ObjBody::MemberList(vec![])), 0, 2),735 ObjBody::MemberList(vec![736 Member::BindStmt(BindSpec::Field {737 into: Destruct::Full("x".into()),738 value: el!(Num(1.0), 15, 16)739 }),740 Member::Field(FieldMember {741 name: FieldName::Fixed("x".into()),742 plus: false,743 params: None,744 visibility: Visibility::Normal,745 value: el!(Var("x".into()), 21, 22),746 })747 ])748 ),749 0,750 24751 ),752 BinaryOpType::Add,753 el!(Obj(ObjBody::MemberList(vec![])), 27, 29),754 ),755 0,756 29757 ),758 );759 }760}1#![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::{Source, SourcePath};1516pub struct ParserSettings {17 pub file_name: Source,18}1920macro_rules! expr_bin {21 ($a:ident $op:ident $b:ident) => {22 Expr::BinaryOp($a, $op, $b)23 };24}25macro_rules! expr_un {26 ($op:ident $a:ident) => {27 Expr::UnaryOp($op, $a)28 };29}3031parser! {32 grammar jsonnet_parser() for str {33 use peg::ParseLiteral;3435 rule eof() = quiet!{![_]} / expected!("<eof>")36 rule eol() = "\n" / eof()3738 /// Standard C-like comments39 rule comment()40 = "//" (!eol()[_])* eol()41 / "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"42 / "#" (!eol()[_])* eol()4344 rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")45 rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")4647 /// For comma-delimited elements48 rule comma() = quiet!{_ "," _} / expected!("<comma>")49 rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}50 rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}51 rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']52 /// Sequence of digits53 rule uint_str() -> &'input str = a:$(digit()+) { a }54 /// Number in scientific notation format55 rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")5657 /// Reserved word followed by any non-alphanumberic58 rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()59 rule id() -> IStr = v:$(quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")) { v.into() }6061 rule keyword(id: &'static str) -> ()62 = ##parse_string_literal(id) end_of_ident()6364 pub rule param(s: &ParserSettings) -> expr::Param = name:destruct(s) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }65 pub rule params(s: &ParserSettings) -> expr::ParamsDesc66 = params:param(s) ** comma() comma()? { expr::ParamsDesc(Rc::new(params)) }67 / { expr::ParamsDesc(Rc::new(Vec::new())) }6869 pub rule arg(s: &ParserSettings) -> (Option<IStr>, LocExpr)70 = quiet! { name:(s:id() _ "=" !['='] _ {s})? expr:expr(s) {(name, expr)} }71 / expected!("<argument>")7273 pub rule args(s: &ParserSettings) -> expr::ArgsDesc74 = args:arg(s)**comma() comma()? {?75 let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();76 let mut unnamed = Vec::with_capacity(unnamed_count);77 let mut named = Vec::with_capacity(args.len() - unnamed_count);78 let mut named_started = false;79 for (name, value) in args {80 if let Some(name) = name {81 named_started = true;82 named.push((name, value));83 } else {84 if named_started {85 return Err("<named argument>")86 }87 unnamed.push(value);88 }89 }90 Ok(expr::ArgsDesc::new(unnamed, named))91 }9293 pub rule destruct_rest() -> expr::DestructRest94 = "..." into:(_ into:id() {into})? {if let Some(into) = into {95 expr::DestructRest::Keep(into)96 } else {expr::DestructRest::Drop}}97 pub rule destruct_array(s: &ParserSettings) -> expr::Destruct98 = "[" _ start:destruct(s)**comma() rest:(99 comma() _ rest:destruct_rest()? end:(100 comma() end:destruct(s)**comma() (_ comma())? {end}101 / comma()? {Vec::new()}102 ) {(rest, end)}103 / comma()? {(None, Vec::new())}104 ) _ "]" {?105 #[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Array {106 start,107 rest: rest.0,108 end: rest.1,109 });110 #[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")111 }112 pub rule destruct_object(s: &ParserSettings) -> expr::Destruct113 = "{" _114 fields:(name:id() into:(_ ":" _ into:destruct(s) {into})? default:(_ "=" _ v:expr(s) {v})? {(name, into, default)})**comma()115 rest:(116 comma() rest:destruct_rest()? {rest}117 / comma()? {None}118 )119 _ "}" {?120 #[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Object {121 fields,122 rest,123 });124 #[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")125 }126 pub rule destruct(s: &ParserSettings) -> expr::Destruct127 = v:id() {expr::Destruct::Full(v)}128 / "?" {?129 #[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Skip);130 #[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")131 }132 / arr:destruct_array(s) {arr}133 / obj:destruct_object(s) {obj}134135 pub rule bind(s: &ParserSettings) -> expr::BindSpec136 = into:destruct(s) _ "=" _ expr:expr(s) {expr::BindSpec::Field{into, value: expr}}137 / name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec::Function{name, params, value: expr}}138139 pub rule assertion(s: &ParserSettings) -> expr::AssertStmt140 = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }141142 pub rule whole_line() -> &'input str143 = str:$((!['\n'][_])* "\n") {str}144 pub rule string_block() -> String145 = "|||" (!['\n']single_whitespace())* "\n"146 empty_lines:$(['\n']*)147 prefix:[' ' | '\t']+ first_line:whole_line()148 lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*149 [' ' | '\t']*<, {prefix.len() - 1}> "|||"150 {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}151152 rule hex_char()153 = quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")154155 rule string_char(c: rule<()>)156 = (!['\\']!c()[_])+157 / "\\\\"158 / "\\u" hex_char() hex_char() hex_char() hex_char()159 / "\\x" hex_char() hex_char()160 / ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))161 pub rule string() -> String162 = ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}163 / ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}164 / quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}165 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}166 / string_block() } / expected!("<string>")167168 pub rule field_name(s: &ParserSettings) -> expr::FieldName169 = name:id() {expr::FieldName::Fixed(name)}170 / name:string() {expr::FieldName::Fixed(name.into())}171 / "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}172 pub rule visibility() -> expr::Visibility173 = ":::" {expr::Visibility::Unhide}174 / "::" {expr::Visibility::Hidden}175 / ":" {expr::Visibility::Normal}176 pub rule field(s: &ParserSettings) -> expr::FieldMember177 = name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{178 name,179 plus: plus.is_some(),180 params: None,181 visibility,182 value,183 }}184 / name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{185 name,186 plus: false,187 params: Some(params),188 visibility,189 value,190 }}191 pub rule obj_local(s: &ParserSettings) -> BindSpec192 = keyword("local") _ bind:bind(s) {bind}193 pub rule member(s: &ParserSettings) -> expr::Member194 = bind:obj_local(s) {expr::Member::BindStmt(bind)}195 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}196 / field:field(s) {expr::Member::Field(field)}197 pub rule objinside(s: &ParserSettings) -> expr::ObjBody198 = pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ plus:"+"? _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {199 let mut compspecs = vec![CompSpec::ForSpec(forspec)];200 compspecs.extend(others.unwrap_or_default());201 expr::ObjBody::ObjComp(expr::ObjComp{202 pre_locals,203 key,204 plus: plus.is_some(),205 value,206 post_locals,207 compspecs,208 })209 }210 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}211 pub rule ifspec(s: &ParserSettings) -> IfSpecData212 = keyword("if") _ expr:expr(s) {IfSpecData(expr)}213 pub rule forspec(s: &ParserSettings) -> ForSpecData214 = keyword("for") _ id:id() _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}215 pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>216 = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}217 pub rule local_expr(s: &ParserSettings) -> Expr218 = keyword("local") _ binds:bind(s) ** comma() (_ ",")? _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }219 pub rule string_expr(s: &ParserSettings) -> Expr220 = s:string() {Expr::Str(s.into())}221 pub rule obj_expr(s: &ParserSettings) -> Expr222 = "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}223 pub rule array_expr(s: &ParserSettings) -> Expr224 = "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}225 pub rule array_comp_expr(s: &ParserSettings) -> Expr226 = "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {227 let mut specs = vec![CompSpec::ForSpec(forspec)];228 specs.extend(others.unwrap_or_default());229 Expr::ArrComp(expr, specs)230 }231 pub rule number_expr(s: &ParserSettings) -> Expr232 = n:number() { expr::Expr::Num(n) }233 pub rule var_expr(s: &ParserSettings) -> Expr234 = n:id() { expr::Expr::Var(n) }235 pub rule id_loc(s: &ParserSettings) -> LocExpr236 = a:position!() n:id() b:position!() { LocExpr(Rc::new(expr::Expr::Str(n)), ExprLocation(s.file_name.clone(), a as u32,b as u32)) }237 pub rule if_then_else_expr(s: &ParserSettings) -> Expr238 = cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{239 cond,240 cond_then,241 cond_else,242 }}243244 pub rule literal(s: &ParserSettings) -> Expr245 = v:(246 keyword("null") {LiteralType::Null}247 / keyword("true") {LiteralType::True}248 / keyword("false") {LiteralType::False}249 / keyword("self") {LiteralType::This}250 / keyword("$") {LiteralType::Dollar}251 / keyword("super") {LiteralType::Super}252 ) {Expr::Literal(v)}253254 pub rule expr_basic(s: &ParserSettings) -> Expr255 = literal(s)256257 / string_expr(s) / number_expr(s)258 / array_expr(s)259 / obj_expr(s)260 / array_expr(s)261 / array_comp_expr(s)262263 / keyword("importstr") _ path:string() {Expr::ImportStr(path.into())}264 / keyword("importbin") _ path:string() {Expr::ImportBin(path.into())}265 / keyword("import") _ path:string() {Expr::Import(path.into())}266267 / var_expr(s)268 / local_expr(s)269 / if_then_else_expr(s)270271 / keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}272 / assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }273274 / keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }275276 rule slice_part(s: &ParserSettings) -> Option<LocExpr>277 = _ e:(e:expr(s) _{e})? {e}278 pub rule slice_desc(s: &ParserSettings) -> SliceDesc279 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {280 let (end, step) = if let Some((end, step)) = pair {281 (end, step)282 }else{283 (None, None)284 };285286 SliceDesc { start, end, step }287 }288289 rule binop(x: rule<()>) -> ()290 = quiet!{ x() } / expected!("<binary op>")291 rule unaryop(x: rule<()>) -> ()292 = quiet!{ x() } / expected!("<unary op>")293294295 use BinaryOpType::*;296 use UnaryOpType::*;297 rule expr(s: &ParserSettings) -> LocExpr298 = precedence! {299 start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.file_name.clone(), start as u32, end as u32)) }300 --301 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}302 --303 a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}304 --305 a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}306 --307 a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}308 --309 a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}310 --311 a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}312 a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}313 --314 a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}315 a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}316 a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}317 a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}318 a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}319 --320 a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}321 a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}322 --323 a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}324 a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}325 --326 a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}327 a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}328 a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}329 --330 unaryop(<"-">) _ b:@ {expr_un!(Minus b)}331 unaryop(<"!">) _ b:@ {expr_un!(Not b)}332 unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}333 --334 a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}335 a:(@) _ "." _ a:position!() e:id_loc(s) b:position!() {Expr::Index(a, e)}336 a:(@) _ "[" _ e:expr(s) _ "]" {Expr::Index(a, e)}337 a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}338 a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}339 --340 e:expr_basic(s) {e}341 "(" _ e:expr(s) _ ")" {Expr::Parened(e)}342 }343344 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}345 }346}347348pub type ParseError = peg::error::ParseError<peg::str::LineCol>;349pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {350 jsonnet_parser::jsonnet(str, settings)351}352/// Used for importstr values353pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {354 let len = str.len();355 LocExpr(356 Rc::new(Expr::Str(str)),357 ExprLocation(settings.file_name.clone(), 0, len as u32),358 )359}360361#[cfg(test)]362pub mod tests {363 use std::borrow::Cow;364365 use BinaryOpType::*;366367 use super::{expr::*, parse};368 use crate::{source::Source, ParserSettings};369370 macro_rules! parse {371 ($s:expr) => {372 parse(373 $s,374 &ParserSettings {375 file_name: Source::new_virtual(Cow::Borrowed("<test>")),376 },377 )378 .unwrap()379 };380 }381382 macro_rules! el {383 ($expr:expr, $from:expr, $to:expr$(,)?) => {384 LocExpr(385 std::rc::Rc::new($expr),386 ExprLocation(Source::new_virtual(Cow::Borrowed("<test>")), $from, $to),387 )388 };389 }390391 #[test]392 fn multiline_string() {393 assert_eq!(394 parse!("|||\n Hello world!\n a\n|||"),395 el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),396 );397 assert_eq!(398 parse!("|||\n Hello world!\n a\n|||"),399 el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),400 );401 assert_eq!(402 parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),403 el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),404 );405 assert_eq!(406 parse!("|||\n Hello world!\n a\n |||"),407 el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),408 );409 }410411 #[test]412 fn slice() {413 parse!("a[1:]");414 parse!("a[1::]");415 parse!("a[:1:]");416 parse!("a[::1]");417 parse!("str[:len - 1]");418 }419420 #[test]421 fn string_escaping() {422 assert_eq!(423 parse!(r#""Hello, \"world\"!""#),424 el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),425 );426 assert_eq!(427 parse!(r#"'Hello \'world\'!'"#),428 el!(Expr::Str("Hello 'world'!".into()), 0, 18),429 );430 assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));431 }432433 #[test]434 fn string_unescaping() {435 assert_eq!(436 parse!(r#""Hello\nWorld""#),437 el!(Expr::Str("Hello\nWorld".into()), 0, 14),438 );439 }440441 #[test]442 fn string_verbantim() {443 assert_eq!(444 parse!(r#"@"Hello\n""World""""#),445 el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),446 );447 }448449 #[test]450 fn imports() {451 assert_eq!(452 parse!("import \"hello\""),453 el!(Expr::Import("hello".into()), 0, 14),454 );455 assert_eq!(456 parse!("importstr \"garnish.txt\""),457 el!(Expr::ImportStr("garnish.txt".into()), 0, 23)458 );459 assert_eq!(460 parse!("importbin \"garnish.bin\""),461 el!(Expr::ImportBin("garnish.bin".into()), 0, 23)462 );463 }464465 #[test]466 fn empty_object() {467 assert_eq!(468 parse!("{}"),469 el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)470 );471 }472473 #[test]474 fn basic_math() {475 assert_eq!(476 parse!("2+2*2"),477 el!(478 Expr::BinaryOp(479 el!(Expr::Num(2.0), 0, 1),480 Add,481 el!(482 Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),483 2,484 5485 )486 ),487 0,488 5489 )490 );491 }492493 #[test]494 fn basic_math_with_indents() {495 assert_eq!(496 parse!("2 + 2 * 2 "),497 el!(498 Expr::BinaryOp(499 el!(Expr::Num(2.0), 0, 1),500 Add,501 el!(502 Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),503 7,504 14505 ),506 ),507 0,508 14509 )510 );511 }512513 #[test]514 fn basic_math_parened() {515 assert_eq!(516 parse!("2+(2+2*2)"),517 el!(518 Expr::BinaryOp(519 el!(Expr::Num(2.0), 0, 1),520 Add,521 el!(522 Expr::Parened(el!(523 Expr::BinaryOp(524 el!(Expr::Num(2.0), 3, 4),525 Add,526 el!(527 Expr::BinaryOp(528 el!(Expr::Num(2.0), 5, 6),529 Mul,530 el!(Expr::Num(2.0), 7, 8),531 ),532 5,533 8534 ),535 ),536 3,537 8538 )),539 2,540 9541 ),542 ),543 0,544 9545 )546 );547 }548549 /// Comments should not affect parsing550 #[test]551 fn comments() {552 assert_eq!(553 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),554 el!(555 Expr::BinaryOp(556 el!(Expr::Num(2.0), 0, 1),557 Add,558 el!(559 Expr::BinaryOp(560 el!(Expr::Num(3.0), 22, 23),561 Mul,562 el!(Expr::Num(4.0), 40, 41)563 ),564 22,565 41566 )567 ),568 0,569 41570 )571 );572 }573574 /// Comments should be able to be escaped575 #[test]576 fn comment_escaping() {577 assert_eq!(578 parse!("2/*\\*/+*/ - 22"),579 el!(580 Expr::BinaryOp(el!(Expr::Num(2.0), 0, 1), Sub, el!(Expr::Num(22.0), 12, 14)),581 0,582 14583 )584 );585 }586587 #[test]588 fn suffix() {589 // assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));590 // assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));591 // assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));592 // assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))593 }594595 #[test]596 fn array_comp() {597 use Expr::*;598 /*599 `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`,600 `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`601 */602 assert_eq!(603 parse!("[std.deepJoin(x) for x in arr]"),604 el!(605 ArrComp(606 el!(607 Apply(608 el!(609 Index(610 el!(Var("std".into()), 1, 4),611 el!(Str("deepJoin".into()), 5, 13)612 ),613 1,614 13615 ),616 ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),617 false,618 ),619 1,620 16621 ),622 vec![CompSpec::ForSpec(ForSpecData(623 "x".into(),624 el!(Var("arr".into()), 26, 29)625 ))]626 ),627 0,628 30629 ),630 )631 }632633 #[test]634 fn reserved() {635 use Expr::*;636 assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));637 assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));638 }639640 #[test]641 fn multiple_args_buf() {642 parse!("a(b, null_fields)");643 }644645 #[test]646 fn infix_precedence() {647 use Expr::*;648 assert_eq!(649 parse!("!a && !b"),650 el!(651 BinaryOp(652 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),653 And,654 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)655 ),656 0,657 8658 )659 );660 }661662 #[test]663 fn infix_precedence_division() {664 use Expr::*;665 assert_eq!(666 parse!("!a / !b"),667 el!(668 BinaryOp(669 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),670 Div,671 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)672 ),673 0,674 7675 )676 );677 }678679 #[test]680 fn double_negation() {681 use Expr::*;682 assert_eq!(683 parse!("!!a"),684 el!(685 UnaryOp(686 UnaryOpType::Not,687 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)688 ),689 0,690 3691 )692 )693 }694695 #[test]696 fn array_test_error() {697 parse!("[a for a in b if c for e in f]");698 // ^^^^ failed code699 }700701 #[test]702 fn missing_newline_between_comment_and_eof() {703 parse!(704 "{a:1}705706 //+213"707 );708 }709710 #[test]711 fn default_param_before_nondefault() {712 parse!("local x(foo = 'foo', bar) = null; null");713 }714715 #[test]716 fn can_parse_stdlib() {717 parse!(jrsonnet_stdlib::STDLIB_STR);718 }719720 #[test]721 fn add_location_info_to_all_sub_expressions() {722 use Expr::*;723724 let file_name = Source::new_virtual(Cow::Borrowed("<test>"));725 let expr = parse(726 "{} { local x = 1, x: x } + {}",727 &ParserSettings { file_name },728 )729 .unwrap();730 assert_eq!(731 expr,732 el!(733 BinaryOp(734 el!(735 ObjExtend(736 el!(Obj(ObjBody::MemberList(vec![])), 0, 2),737 ObjBody::MemberList(vec![738 Member::BindStmt(BindSpec::Field {739 into: Destruct::Full("x".into()),740 value: el!(Num(1.0), 15, 16)741 }),742 Member::Field(FieldMember {743 name: FieldName::Fixed("x".into()),744 plus: false,745 params: None,746 visibility: Visibility::Normal,747 value: el!(Var("x".into()), 21, 22),748 })749 ])750 ),751 0,752 24753 ),754 BinaryOpType::Add,755 el!(Obj(ObjBody::MemberList(vec![])), 27, 29),756 ),757 0,758 29759 ),760 );761 }762}crates/jrsonnet-parser/src/location.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-parser/src/location.rs
@@ -0,0 +1,124 @@
+#[allow(clippy::module_name_repetitions)]
+#[derive(Clone, PartialEq, Eq, Debug)]
+pub struct CodeLocation {
+ pub offset: usize,
+
+ pub line: usize,
+ pub column: usize,
+
+ pub line_start_offset: usize,
+ pub line_end_offset: usize,
+}
+
+#[allow(clippy::module_name_repetitions)]
+pub fn location_to_offset(mut file: &str, mut line: usize, column: usize) -> Option<usize> {
+ let mut offset = 0;
+ while line > 1 {
+ let pos = file.find('\n')?;
+ offset += pos + 1;
+ file = &file[pos + 1..];
+ line -= 1;
+ }
+ offset += column - 1;
+ Some(offset)
+}
+
+#[allow(clippy::module_name_repetitions)]
+pub fn offset_to_location(file: &str, offsets: &[u32]) -> Vec<CodeLocation> {
+ if offsets.is_empty() {
+ return vec![];
+ }
+ let mut line = 1;
+ let mut column = 1;
+ let max_offset = *offsets.iter().max().expect("offsets is not empty");
+
+ let mut offset_map = offsets
+ .iter()
+ .enumerate()
+ .map(|(pos, offset)| (*offset, pos))
+ .collect::<Vec<_>>();
+ offset_map.sort_by_key(|v| v.0);
+ offset_map.reverse();
+
+ let mut out = vec![
+ CodeLocation {
+ offset: 0,
+ column: 0,
+ line: 0,
+ line_start_offset: 0,
+ line_end_offset: 0
+ };
+ offsets.len()
+ ];
+ let mut with_no_known_line_ending = vec![];
+ let mut this_line_offset = 0;
+ for (pos, ch) in file
+ .chars()
+ .enumerate()
+ .chain(std::iter::once((file.len(), ' ')))
+ {
+ column += 1;
+ match offset_map.last() {
+ Some(x) if x.0 == pos as u32 => {
+ let out_idx = x.1;
+ with_no_known_line_ending.push(out_idx);
+ out[out_idx].offset = pos;
+ out[out_idx].line = line;
+ out[out_idx].column = column;
+ out[out_idx].line_start_offset = this_line_offset;
+ offset_map.pop();
+ }
+ _ => {}
+ }
+ if ch == '\n' {
+ line += 1;
+ column = 1;
+
+ for idx in with_no_known_line_ending.drain(..) {
+ out[idx].line_end_offset = pos;
+ }
+ this_line_offset = pos + 1;
+
+ if pos == max_offset as usize + 1 {
+ break;
+ }
+ }
+ }
+ let file_end = file.chars().count();
+ for idx in with_no_known_line_ending {
+ out[idx].line_end_offset = file_end;
+ }
+
+ out
+}
+
+#[cfg(test)]
+pub mod tests {
+ use super::{offset_to_location, CodeLocation};
+
+ #[test]
+ fn test() {
+ assert_eq!(
+ offset_to_location(
+ "hello world\n_______________________________________________________",
+ &[0, 14]
+ ),
+ vec![
+ CodeLocation {
+ offset: 0,
+ line: 1,
+ column: 2,
+ line_start_offset: 0,
+ line_end_offset: 11,
+ },
+ CodeLocation {
+ offset: 14,
+ line: 2,
+ column: 4,
+ line_start_offset: 12,
+ line_end_offset: 67
+ }
+ ]
+ )
+ }
+}
crates/jrsonnet-parser/src/source.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/source.rs
+++ b/crates/jrsonnet-parser/src/source.rs
@@ -6,21 +6,42 @@
};
use jrsonnet_gcmodule::{Trace, Tracer};
+use jrsonnet_interner::IStr;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
+use crate::location::{location_to_offset, offset_to_location, CodeLocation};
+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
-#[derive(PartialEq, Eq, Debug, Hash)]
-enum Inner {
- Real(PathBuf),
+#[derive(PartialEq, Eq, Debug, Hash, Clone)]
+pub enum SourcePath {
+ /// This file is located on disk
+ Path(PathBuf),
+ /// This file is located somewhere else (I.e http), but it can refer to relative paths, and is egilible for caching
+ Custom(String),
+ /// This file is only located in memory, and can't be cached
Virtual(Cow<'static, str>),
}
+impl Trace for SourcePath {
+ fn trace(&self, _tracer: &mut Tracer) {}
+ fn is_type_tracked() -> bool {
+ false
+ }
+}
+
+impl SourcePath {
+ /// Should import resolver be able to read file by this path?
+ pub fn can_load(&self) -> bool {
+ matches!(self, Self::Path(_) | Self::Custom(_))
+ }
+}
+
/// Either real file, or virtual
/// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, PartialEq, Eq, Debug)]
-pub struct Source(Rc<Inner>);
+pub struct Source(Rc<(SourcePath, IStr)>);
static_assertions::assert_eq_size!(Source, *const ());
impl Trace for Source {
@@ -33,55 +54,63 @@
impl Source {
/// Fails when path contains inner /../ or /./ references, or not absolute
- pub fn new(path: PathBuf) -> Option<Self> {
- if !path.is_absolute()
- || path
- .components()
- .any(|c| matches!(c, Component::CurDir | Component::ParentDir))
- {
- return None;
+ pub fn new(path: SourcePath, code: IStr) -> Option<Self> {
+ if let SourcePath::Path(path) = &path {
+ if !path.is_absolute()
+ || path
+ .components()
+ .any(|c| matches!(c, Component::CurDir | Component::ParentDir))
+ {
+ return None;
+ }
}
- Some(Self(Rc::new(Inner::Real(path))))
+ Some(Self(Rc::new((path, code))))
}
- pub fn new_virtual(n: Cow<'static, str>) -> Self {
- Self(Rc::new(Inner::Virtual(n)))
+ pub fn new_virtual(n: Cow<'static, str>, code: IStr) -> Self {
+ Self(Rc::new((SourcePath::Virtual(n), code)))
}
pub fn short_display(&self) -> ShortDisplay {
ShortDisplay(self.clone())
}
- /// Returns None if file is virtual
+ /// Returns Some if this file is loaded from FS
pub fn path(&self) -> Option<&Path> {
- match self.inner() {
- Inner::Real(r) => Some(r),
- Inner::Virtual(_) => None,
+ match self.source_path() {
+ SourcePath::Path(r) => Some(r),
+ SourcePath::Custom(_) => None,
+ SourcePath::Virtual(_) => None,
}
}
- pub fn repr(&self) -> Result<&Path, &str> {
- match self.inner() {
- Inner::Real(r) => Ok(r),
- Inner::Virtual(v) => Err(v.as_ref()),
- }
+ pub fn code(&self) -> &str {
+ &self.0 .1
+ }
+
+ pub fn source_path(&self) -> &SourcePath {
+ &self.0 .0 as &SourcePath
}
- fn inner(&self) -> &Inner {
- &self.0 as &Inner
+ pub fn map_source_locations(&self, locs: &[u32]) -> Vec<CodeLocation> {
+ offset_to_location(&self.0 .1, locs)
+ }
+ pub fn map_from_source_location(&self, line: usize, column: usize) -> Option<usize> {
+ location_to_offset(&self.0 .1, line, column)
}
}
pub struct ShortDisplay(Source);
impl fmt::Display for ShortDisplay {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match &self.0 .0 as &Inner {
- Inner::Real(r) => {
+ match &self.0 .0 .0 as &SourcePath {
+ SourcePath::Path(r) => {
write!(
f,
"{}",
r.file_name().expect("path is valid").to_string_lossy()
)
}
- Inner::Virtual(n) => write!(f, "{}", n),
+ SourcePath::Custom(r) => write!(f, "{}", r),
+ SourcePath::Virtual(n) => write!(f, "{}", n),
}
}
}
crates/jrsonnet-stdlib/build.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/build.rs
+++ b/crates/jrsonnet-stdlib/build.rs
@@ -7,7 +7,10 @@
let parsed = parse(
include_str!("./src/std.jsonnet"),
&ParserSettings {
- file_name: Source::new_virtual(Cow::Borrowed("<std>")),
+ file_name: Source::new_virtual(
+ Cow::Borrowed("<std>"),
+ include_str!("./src/std.jsonnet").into(),
+ ),
},
)
.expect("parse");
crates/jrsonnet-stdlib/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/expr.rs
+++ b/crates/jrsonnet-stdlib/src/expr.rs
@@ -15,7 +15,7 @@
jrsonnet_parser::parse(
STDLIB_STR,
&ParserSettings {
- file_name: Source::new_virtual(Cow::Borrowed("<std>")),
+ file_name: Source::new_virtual(Cow::Borrowed("<std>"), STDLIB_STR.into()),
},
)
.unwrap()
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -183,10 +183,10 @@
pub struct StdTracePrinter;
impl TracePrinter for StdTracePrinter {
- fn print_trace(&self, s: State, loc: CallLocation, value: IStr) {
+ fn print_trace(&self, _s: State, loc: CallLocation, value: IStr) {
eprint!("TRACE:");
if let Some(loc) = loc.0 {
- let locs = s.map_source_locations(loc.0.clone(), &[loc.1]);
+ let locs = loc.0.map_source_locations(&[loc.1]);
eprint!(" {}:{}", loc.0.short_display(), locs[0].line);
}
eprintln!(" {}", value);
@@ -212,9 +212,9 @@
}
}
-pub fn extvar_source(name: &str) -> Source {
+pub fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {
let source_name = format!("<extvar:{}>", name);
- Source::new_virtual(Cow::Owned(source_name))
+ Source::new_virtual(Cow::Owned(source_name), code.into())
}
pub struct ContextInitializer {
@@ -260,8 +260,9 @@
.ext_vars
.insert(name, TlaArg::String(value));
}
- pub fn add_ext_code(&self, name: &str, code: String) -> Result<()> {
- let source = extvar_source(name);
+ pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {
+ let code = code.into();
+ let source = extvar_source(name, code.clone());
let parsed = jrsonnet_parser::parse(
&code,
&jrsonnet_parser::ParserSettings {
@@ -270,7 +271,6 @@
)
.map_err(|e| ImportSyntaxError {
path: source,
- source_code: code.clone().into(),
error: Box::new(e),
})?;
// self.data_mut().volatile_files.insert(source_name, code);
@@ -297,11 +297,13 @@
.hide()
.value(
s,
- Val::Str(match source.repr() {
- Ok(p) => p.display().to_string().into(),
- // Virtual files end up as empty strings in std.thisFile
- Err(_e) => "".into(),
- }),
+ Val::Str(
+ source
+ .path()
+ .map(|p| p.display().to_string())
+ .unwrap_or_else(String::new)
+ .into(),
+ ),
)
.expect("this object builder is empty");
let stdlib_with_this_file = builder.build();
@@ -343,7 +345,7 @@
settings: Rc<RefCell<Settings>>,
))]
fn builtin_ext_var(this: &builtin_ext_var, s: State, x: IStr) -> Result<Any> {
- let ctx = s.create_default_context(extvar_source(&x));
+ let ctx = s.create_default_context(extvar_source(&x, ""));
Ok(Any(this
.settings
.borrow()