difftreelog
style enforce import style
in: master
38 files changed
.rustfmt.tomldiffbeforeafterboth--- a/.rustfmt.toml
+++ b/.rustfmt.toml
@@ -1 +1,3 @@
hard_tabs = true
+imports_granularity = "crate"
+group_imports = "stdexternalcrate"
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -1,9 +1,5 @@
//! Import resolution manipulation utilities
-use jrsonnet_evaluator::{
- error::{Error::*, Result},
- throw, EvaluationState, ImportResolver,
-};
use std::{
any::Any,
cell::RefCell,
@@ -17,6 +13,11 @@
rc::Rc,
};
+use jrsonnet_evaluator::{
+ error::{Error::*, Result},
+ throw, EvaluationState, ImportResolver,
+};
+
pub type JsonnetImportCallback = unsafe extern "C" fn(
ctx: *mut c_void,
base: *const c_char,
bindings/jsonnet/src/interop.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/interop.rs
+++ b/bindings/jsonnet/src/interop.rs
@@ -1,12 +1,14 @@
//! Jrsonnet specific additional binding helpers
-use crate::{import::jsonnet_import_callback, native::jsonnet_native_callback};
-use jrsonnet_evaluator::{EvaluationState, Val};
use std::{
ffi::c_void,
os::raw::{c_char, c_int},
};
+use jrsonnet_evaluator::{EvaluationState, Val};
+
+use crate::{import::jsonnet_import_callback, native::jsonnet_native_callback};
+
extern "C" {
pub fn _jrsonnet_static_import_callback(
ctx: *mut c_void,
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -8,8 +8,6 @@
pub mod val_modify;
pub mod vars_tlas;
-use import::NativeImportResolver;
-use jrsonnet_evaluator::{EvaluationState, IStr, ManifestFormat, Val};
use std::{
alloc::Layout,
ffi::{CStr, CString},
@@ -17,6 +15,9 @@
path::PathBuf,
};
+use import::NativeImportResolver;
+use jrsonnet_evaluator::{EvaluationState, IStr, ManifestFormat, Val};
+
/// WASM stub
#[cfg(target_arch = "wasm32")]
#[no_mangle]
bindings/jsonnet/src/native.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/native.rs
+++ b/bindings/jsonnet/src/native.rs
@@ -1,3 +1,11 @@
+use std::{
+ convert::TryFrom,
+ ffi::{c_void, CStr},
+ os::raw::{c_char, c_int},
+ path::Path,
+ rc::Rc,
+};
+
use gcmodule::Cc;
use jrsonnet_evaluator::{
error::{Error, LocError},
@@ -5,13 +13,6 @@
gc::TraceBox,
native::{NativeCallback, NativeCallbackHandler},
EvaluationState, IStr, Val,
-};
-use std::{
- convert::TryFrom,
- ffi::{c_void, CStr},
- os::raw::{c_char, c_int},
- path::Path,
- rc::Rc,
};
type JsonnetNativeCallback = unsafe extern "C" fn(
bindings/jsonnet/src/val_extract.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/val_extract.rs
+++ b/bindings/jsonnet/src/val_extract.rs
@@ -1,12 +1,12 @@
//! Extract values from VM
-use jrsonnet_evaluator::{EvaluationState, Val};
-
use std::{
ffi::CString,
os::raw::{c_char, c_double, c_int},
};
+use jrsonnet_evaluator::{EvaluationState, Val};
+
#[no_mangle]
pub extern "C" fn jsonnet_json_extract_string(_vm: &EvaluationState, v: &Val) -> *mut c_char {
match v {
bindings/jsonnet/src/val_make.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/val_make.rs
+++ b/bindings/jsonnet/src/val_make.rs
@@ -1,12 +1,13 @@
//! Create values in VM
-use gcmodule::Cc;
-use jrsonnet_evaluator::{ArrValue, EvaluationState, ObjValue, Val};
use std::{
ffi::CStr,
os::raw::{c_char, c_double, c_int},
};
+use gcmodule::Cc;
+use jrsonnet_evaluator::{val::ArrValue, EvaluationState, ObjValue, Val};
+
/// # Safety
///
/// This function is safe, if received v is a pointer to normal C string
bindings/jsonnet/src/val_modify.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/val_modify.rs
+++ b/bindings/jsonnet/src/val_modify.rs
@@ -2,10 +2,11 @@
//! Only tested with variables, which haven't altered by code before appearing here
//! In jrsonnet every value is immutable, and this code is probally broken
+use std::{ffi::CStr, os::raw::c_char};
+
use gcmodule::Cc;
-use jrsonnet_evaluator::{ArrValue, EvaluationState, LazyBinding, LazyVal, ObjMember, Val};
+use jrsonnet_evaluator::{val::ArrValue, EvaluationState, LazyBinding, LazyVal, ObjMember, Val};
use jrsonnet_parser::Visibility;
-use std::{ffi::CStr, os::raw::c_char};
/// # Safety
///
bindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/vars_tlas.rs
+++ b/bindings/jsonnet/src/vars_tlas.rs
@@ -1,8 +1,9 @@
//! Manipulate external variables and top level arguments
-use jrsonnet_evaluator::EvaluationState;
use std::{ffi::CStr, os::raw::c_char};
+use jrsonnet_evaluator::EvaluationState;
+
/// # Safety
#[no_mangle]
pub unsafe extern "C" fn jsonnet_ext_var(
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -1,13 +1,13 @@
+use std::{
+ fs::{create_dir_all, File},
+ io::{Read, Write},
+ path::PathBuf,
+};
+
use clap::{AppSettings, IntoApp, Parser};
use clap_complete::Shell;
use jrsonnet_cli::{ConfigureState, GcOpts, GeneralOpts, InputOpts, ManifestOpts, OutputOpts};
use jrsonnet_evaluator::{error::LocError, EvaluationState};
-use std::{
- fs::{create_dir_all, File},
- io::Read,
- io::Write,
- path::PathBuf,
-};
#[cfg(feature = "mimalloc")]
#[global_allocator]
crates/jrsonnet-cli/src/ext.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/ext.rs
+++ b/crates/jrsonnet-cli/src/ext.rs
@@ -1,8 +1,10 @@
-use crate::ConfigureState;
+use std::{fs::read_to_string, str::FromStr};
+
use clap::Parser;
use jrsonnet_evaluator::{error::Result, EvaluationState};
-use std::{fs::read_to_string, str::FromStr};
+use crate::ConfigureState;
+
#[derive(Clone)]
pub struct ExtStr {
pub name: String,
crates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/lib.rs
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -3,14 +3,14 @@
mod tla;
mod trace;
+use std::{env, path::PathBuf};
+
+use clap::Parser;
pub use ext::*;
+use jrsonnet_evaluator::{error::Result, EvaluationState, FileImportResolver};
pub use manifest::*;
pub use tla::*;
pub use trace::*;
-
-use clap::Parser;
-use jrsonnet_evaluator::{error::Result, EvaluationState, FileImportResolver};
-use std::{env, path::PathBuf};
pub trait ConfigureState {
fn configure(&self, state: &EvaluationState) -> Result<()>;
crates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -1,8 +1,10 @@
-use crate::ConfigureState;
+use std::{path::PathBuf, str::FromStr};
+
use clap::Parser;
use jrsonnet_evaluator::{error::Result, EvaluationState, ManifestFormat};
-use std::{path::PathBuf, str::FromStr};
+use crate::ConfigureState;
+
pub enum ManifestFormatName {
/// Expect string as output, and write them directly
String,
crates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/tla.rs
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -1,7 +1,8 @@
-use crate::{ConfigureState, ExtFile, ExtStr};
use clap::Parser;
use jrsonnet_evaluator::{error::Result, EvaluationState};
+use crate::{ConfigureState, ExtFile, ExtStr};
+
#[derive(Parser)]
#[clap(next_help_heading = "TOP LEVEL ARGUMENTS")]
pub struct TLAOpts {
crates/jrsonnet-cli/src/trace.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/trace.rs
+++ b/crates/jrsonnet-cli/src/trace.rs
@@ -1,12 +1,14 @@
-use crate::ConfigureState;
+use std::str::FromStr;
+
use clap::Parser;
use jrsonnet_evaluator::{
error::Result,
trace::{CompactFormat, ExplainingFormat, PathResolver},
EvaluationState,
};
-use std::str::FromStr;
+use crate::ConfigureState;
+
#[derive(PartialEq)]
pub enum TraceFormatName {
Compact,
crates/jrsonnet-evaluator/build.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/build.rs
+++ b/crates/jrsonnet-evaluator/build.rs
@@ -1,6 +1,3 @@
-use bincode::serialize;
-use jrsonnet_parser::{parse, ParserSettings};
-use jrsonnet_stdlib::STDLIB_STR;
use std::{
env,
fs::File,
@@ -8,6 +5,10 @@
path::{Path, PathBuf},
};
+use bincode::serialize;
+use jrsonnet_parser::{parse, ParserSettings};
+use jrsonnet_stdlib::STDLIB_STR;
+
fn main() {
let parsed = parse(
STDLIB_STR,
crates/jrsonnet-evaluator/src/builtin/format.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/format.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/format.rs
@@ -1,13 +1,15 @@
//! faster std.format impl
#![allow(clippy::too_many_arguments)]
-use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val};
+use std::convert::TryFrom;
+
use gcmodule::Trace;
use jrsonnet_interner::IStr;
use jrsonnet_types::ValType;
-use std::convert::TryFrom;
use thiserror::Error;
+use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val};
+
#[derive(Debug, Clone, Error, Trace)]
pub enum FormatError {
#[error("truncated format code")]
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -1,7 +1,7 @@
-use crate::error::Error::*;
-use crate::error::Result;
-use crate::push_description_frame;
-use crate::{throw, Val};
+use crate::{
+ error::{Error::*, Result},
+ push_description_frame, throw, Val,
+};
#[derive(PartialEq, Clone, Copy)]
pub enum ManifestType {
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,23 +1,25 @@
-use crate::function::{CallLocation, StaticBuiltin};
-use crate::typed::{Any, Bytes, PositiveF64, VecVal, M1};
-use crate::{
- builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},
- equals,
- error::{Error::*, Result},
- operator::evaluate_mod_op,
- primitive_equals, push_frame, throw,
- typed::{Either2, Either4},
- with_state, ArrValue, FuncVal, IndexableVal, Val,
+use std::{
+ collections::HashMap,
+ convert::{TryFrom, TryInto},
};
-use crate::{Either, ObjValue};
+
use format::{format_arr, format_obj};
use gcmodule::Cc;
use jrsonnet_interner::IStr;
use serde::Deserialize;
use serde_yaml::DeserializingQuirks;
-use std::collections::HashMap;
-use std::convert::{TryFrom, TryInto};
+use crate::{
+ builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},
+ error::{Error::*, Result},
+ function::{CallLocation, StaticBuiltin},
+ operator::evaluate_mod_op,
+ push_frame, throw,
+ typed::{Any, BoundedUsize, Bytes, Either2, Either4, PositiveF64, VecVal, M1},
+ val::{equals, primitive_equals, ArrValue, FuncVal, IndexableVal, Slice},
+ with_state, Either, ObjValue, Val,
+};
+
pub mod stdlib;
pub use stdlib::*;
@@ -58,12 +60,12 @@
}
Ok(Val::Str(
- (s.chars()
- .skip(index)
- .take(end - index)
- .step_by(step)
- .collect::<String>())
- .into(),
+ (s.chars()
+ .skip(index)
+ .take(end - index)
+ .step_by(step)
+ .collect::<String>())
+ .into(),
))
}
IndexableVal::Arr(arr) => {
crates/jrsonnet-evaluator/src/builtin/sort.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/sort.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/sort.rs
@@ -1,10 +1,12 @@
+use gcmodule::{Cc, Trace};
+
use crate::{
error::{Error, LocError, Result},
throw,
typed::Any,
- FuncVal, Val,
+ val::FuncVal,
+ Val,
};
-use gcmodule::{Cc, Trace};
#[derive(Debug, Clone, thiserror::Error, Trace)]
pub enum SortError {
crates/jrsonnet-evaluator/src/builtin/stdlib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/stdlib.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/stdlib.rs
@@ -1,6 +1,7 @@
-use jrsonnet_parser::{LocExpr, ParserSettings};
use std::path::PathBuf;
+use jrsonnet_parser::{LocExpr, ParserSettings};
+
thread_local! {
/// To avoid parsing again when issued from the same thread
#[allow(unreachable_code)]
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -1,12 +1,12 @@
-use crate::cc_ptr_eq;
-use crate::gc::GcHashMap;
+use std::fmt::Debug;
+
+use gcmodule::{Cc, Trace};
+use jrsonnet_interner::IStr;
+
use crate::{
- error::Error::*, map::LayeredHashMap, FutureWrapper, LazyBinding, LazyVal, ObjValue, Result,
- Val,
+ cc_ptr_eq, error::Error::*, gc::GcHashMap, map::LayeredHashMap, FutureWrapper, LazyBinding,
+ LazyVal, ObjValue, Result, Val,
};
-use gcmodule::{Cc, Trace};
-use jrsonnet_interner::IStr;
-use std::fmt::Debug;
#[derive(Clone, Trace)]
pub struct ContextCreator(pub Context, pub FutureWrapper<GcHashMap<IStr, LazyBinding>>);
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -1,16 +1,18 @@
-use crate::{
- builtin::{format::FormatError, sort::SortError},
- typed::TypeLocError,
+use std::{
+ path::{Path, PathBuf},
+ rc::Rc,
};
+
use gcmodule::Trace;
use jrsonnet_interner::IStr;
use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
use jrsonnet_types::ValType;
-use std::{
- path::{Path, PathBuf},
- rc::Rc,
+use thiserror::Error;
+
+use crate::{
+ builtin::{format::FormatError, sort::SortError},
+ typed::TypeLocError,
};
-use thiserror::Error;
#[derive(Error, Debug, Clone, Trace)]
pub enum Error {
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -1,15 +1,5 @@
use std::convert::TryFrom;
-use crate::{
- builtin::{std_slice, BUILTINS},
- error::Error::*,
- evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
- function::CallLocation,
- gc::TraceBox,
- push_frame, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,
- FutureWrapper, GcHashMap, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder,
- ObjectAssertion, Result, Val,
-};
use gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{
@@ -17,6 +7,19 @@
LiteralType, LocExpr, Member, ObjBody, ParamsDesc,
};
use jrsonnet_types::ValType;
+
+use crate::{
+ builtin::{std_slice, BUILTINS},
+ error::Error::*,
+ evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
+ function::CallLocation,
+ gc::TraceBox,
+ push_frame, throw,
+ typed::BoundedUsize,
+ val::{ArrValue, FuncDesc, FuncVal, LazyValValue},
+ with_state, Bindable, Context, ContextCreator, FutureWrapper, GcHashMap, LazyBinding, LazyVal,
+ ObjValue, ObjValueBuilder, ObjectAssertion, Result, Val,
+};
pub mod operator;
pub fn evaluate_binding_in_future(
crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -1,10 +1,11 @@
use std::convert::TryInto;
-use crate::builtin::std_format;
-use crate::{equals, evaluate, Context, Val};
-use crate::{error::Error::*, throw, Result};
use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};
+use crate::{
+ builtin::std_format, error::Error::*, evaluate, throw, val::equals, Context, Result, Val,
+};
+
pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {
use UnaryOpType::*;
use Val::*;
crates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -1,16 +1,19 @@
+use std::{borrow::Cow, collections::HashMap, convert::TryFrom};
+
+use gcmodule::Trace;
+use jrsonnet_interner::IStr;
+pub use jrsonnet_macros::builtin;
+use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
+
use crate::{
error::{Error::*, LocError},
evaluate, evaluate_named,
gc::TraceBox,
throw,
typed::Typed,
- Context, FutureWrapper, GcHashMap, LazyVal, LazyValValue, Result, Val,
+ val::LazyValValue,
+ Context, FutureWrapper, GcHashMap, LazyVal, Result, Val,
};
-use gcmodule::Trace;
-use jrsonnet_interner::IStr;
-pub use jrsonnet_macros::builtin;
-use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
-use std::{borrow::Cow, collections::HashMap, convert::TryFrom};
#[derive(Clone, Copy)]
pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -1,17 +1,20 @@
-use crate::{
- error::{Error::*, Result},
- throw,
-};
-use fs::File;
-use jrsonnet_interner::IStr;
-use std::fs;
use std::{
any::Any,
+ convert::TryFrom,
+ fs,
+ io::Read,
path::{Path, PathBuf},
rc::Rc,
};
-use std::{convert::TryFrom, io::Read};
+use fs::File;
+use jrsonnet_interner::IStr;
+
+use crate::{
+ error::{Error::*, Result},
+ throw,
+};
+
/// Implements file resolution logic for `import` and `importStr`
pub trait ImportResolver {
/// Resolves real file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -1,9 +1,11 @@
+use std::convert::{TryFrom, TryInto};
+
+use serde_json::{Map, Number, Value};
+
use crate::{
error::{Error::*, LocError, Result},
throw, ObjValueBuilder, Val,
};
-use serde_json::{Map, Number, Value};
-use std::convert::{TryFrom, TryInto};
impl TryFrom<&Val> for Value {
type Error = LocError;
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -13,6 +13,7 @@
pub mod error;
mod evaluate;
pub mod function;
+pub mod gc;
mod import;
mod integrations;
mod map;
@@ -20,9 +21,15 @@
mod obj;
pub mod trace;
pub mod typed;
-mod val;
+pub mod val;
-pub use jrsonnet_parser as parser;
+use std::{
+ cell::{Ref, RefCell, RefMut},
+ collections::HashMap,
+ fmt::Debug,
+ path::{Path, PathBuf},
+ rc::Rc,
+};
pub use ctx::*;
pub use dynamic::*;
@@ -33,18 +40,11 @@
use gcmodule::{Cc, Trace, Weak};
pub use import::*;
pub use jrsonnet_interner::IStr;
+pub use jrsonnet_parser as parser;
use jrsonnet_parser::*;
pub use obj::*;
-use std::{
- cell::{Ref, RefCell, RefMut},
- collections::HashMap,
- fmt::Debug,
- path::{Path, PathBuf},
- rc::Rc,
-};
use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};
-pub use val::*;
-pub mod gc;
+pub use val::{LazyVal, ManifestFormat, Val};
pub trait Bindable: Trace + 'static {
fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal>;
@@ -693,18 +693,24 @@
#[cfg(test)]
pub mod tests {
- use super::Val;
- use crate::{
- error::Error::*, function::BuiltinParam, gc::TraceBox, native::NativeCallbackHandler,
- primitive_equals, EvaluationState,
- };
- use gcmodule::{Cc, Trace};
- use jrsonnet_parser::*;
use std::{
path::{Path, PathBuf},
rc::Rc,
};
+ use gcmodule::{Cc, Trace};
+ use jrsonnet_parser::*;
+
+ use super::Val;
+ use crate::{
+ error::Error::*,
+ function::{BuiltinParam, CallLocation},
+ gc::TraceBox,
+ native::NativeCallbackHandler,
+ val::primitive_equals,
+ EvaluationState,
+ };
+
#[test]
#[should_panic]
fn eval_state_stacktrace() {
@@ -712,11 +718,15 @@
state.run_in_state(|| {
state
.push(
- Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),
+ CallLocation::new(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),
|| "outer".to_owned(),
|| {
state.push(
- Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),
+ CallLocation::new(&ExprLocation(
+ PathBuf::from("test2.jsonnet").into(),
+ 30,
+ 40,
+ )),
|| "inner".to_owned(),
|| Err(RuntimeError("".into()).into()),
)?;
@@ -1290,10 +1300,11 @@
}
mod derive_typed {
+ use std::path::PathBuf;
+
use crate::{typed::Typed, EvaluationState};
- use std::path::PathBuf;
- #[derive(Typed, PartialEq, Debug)]
+ #[derive(PartialEq, Debug, Typed)]
struct MyTyped {
a: u32,
#[typed(rename = "b")]
crates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -1,13 +1,16 @@
#![allow(clippy::type_complexity)]
-use crate::function::{parse_builtin_call, ArgsLike, Builtin, BuiltinParam, CallLocation};
-use crate::gc::TraceBox;
-use crate::Context;
-use crate::{error::Result, Val};
+use std::{path::Path, rc::Rc};
+
use gcmodule::Trace;
-use std::path::Path;
-use std::rc::Rc;
+use crate::{
+ error::Result,
+ function::{parse_builtin_call, ArgsLike, Builtin, BuiltinParam, CallLocation},
+ gc::TraceBox,
+ Context, Val,
+};
+
#[derive(Trace)]
pub struct NativeCallback {
pub(crate) params: Vec<BuiltinParam>,
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -1,20 +1,23 @@
-use crate::error::LocError;
-use crate::function::CallLocation;
-use crate::gc::{GcHashMap, GcHashSet, TraceBox};
-use crate::operator::evaluate_add_op;
-use crate::push_frame;
-use crate::{
- cc_ptr_eq, error::Error::*, throw, weak_ptr_eq, weak_raw, Bindable, LazyBinding, LazyVal,
- Result, Val,
+use std::{
+ cell::RefCell,
+ fmt::Debug,
+ hash::{Hash, Hasher},
};
+
use gcmodule::{Cc, Trace, Weak};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{ExprLocation, Visibility};
use rustc_hash::FxHashMap;
-use std::cell::RefCell;
-use std::fmt::Debug;
-use std::hash::{Hash, Hasher};
+use crate::{
+ cc_ptr_eq,
+ error::{Error::*, LocError},
+ function::CallLocation,
+ gc::{GcHashMap, GcHashSet, TraceBox},
+ operator::evaluate_add_op,
+ push_frame, throw, weak_ptr_eq, weak_raw, Bindable, LazyBinding, LazyVal, Result, Val,
+};
+
#[derive(Debug, Trace)]
pub struct ObjMember {
pub add: bool,
crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -1,8 +1,10 @@
mod location;
+use std::path::{Path, PathBuf};
+
+pub use location::*;
+
use crate::{error::Error, EvaluationState, LocError};
-pub use location::*;
-use std::path::{Path, PathBuf};
/// The way paths should be displayed
pub enum PathResolver {
crates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -2,14 +2,14 @@
mod conversions;
pub use conversions::*;
+use gcmodule::Trace;
+pub use jrsonnet_types::{ComplexValType, ValType};
+use thiserror::Error;
use crate::{
error::{Error, LocError, Result},
push_description_frame, Val,
};
-use gcmodule::Trace;
-pub use jrsonnet_types::{ComplexValType, ValType};
-use thiserror::Error;
#[derive(Debug, Error, Clone, Trace)]
pub enum TypeError {
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -1,3 +1,10 @@
+use std::{cell::RefCell, fmt::Debug, rc::Rc};
+
+use gcmodule::{Cc, Trace};
+use jrsonnet_interner::IStr;
+use jrsonnet_parser::{LocExpr, ParamsDesc};
+use jrsonnet_types::ValType;
+
use crate::{
builtin::manifest::{
manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,
@@ -12,11 +19,6 @@
gc::TraceBox,
throw, Context, ObjValue, Result,
};
-use gcmodule::{Cc, Trace};
-use jrsonnet_interner::IStr;
-use jrsonnet_parser::{LocExpr, ParamsDesc};
-use jrsonnet_types::ValType;
-use std::{cell::RefCell, fmt::Debug, rc::Rc};
pub trait LazyValValue: Trace {
fn get(self: Box<Self>) -> Result<Val>;
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -1,6 +1,3 @@
-use gcmodule::Trace;
-use rustc_hash::FxHashMap;
-use serde::{Deserialize, Serialize};
use std::{
borrow::Cow,
cell::RefCell,
@@ -12,6 +9,10 @@
str::Utf8Error,
};
+use gcmodule::Trace;
+use rustc_hash::FxHashMap;
+use serde::{Deserialize, Serialize};
+
#[derive(Clone, PartialOrd, Ord, Eq)]
pub struct IStr(Rc<str>);
impl Trace for IStr {
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -1,7 +1,3 @@
-use gcmodule::Trace;
-use jrsonnet_interner::IStr;
-#[cfg(feature = "serde")]
-use serde::{Deserialize, Serialize};
use std::{
fmt::{Debug, Display},
ops::Deref,
@@ -9,6 +5,11 @@
rc::Rc,
};
+use gcmodule::Trace;
+use jrsonnet_interner::IStr;
+#[cfg(feature = "serde")]
+use serde::{Deserialize, Serialize};
+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Trace)]
pub enum FieldName {
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth1#![allow(clippy::redundant_closure_call)]23use peg::parser;4use std::{5 path::{Path, PathBuf},6 rc::Rc,7};8mod expr;9pub use expr::*;10pub use jrsonnet_interner::IStr;11pub use peg;12mod unescape;1314pub struct ParserSettings {15 pub file_name: Rc<Path>,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() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")5859 rule keyword(id: &'static str) -> ()60 = ##parse_string_literal(id) end_of_ident()6162 pub rule param(s: &ParserSettings) -> expr::Param = name:$(id()) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name.into(), 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.map(Into::into), 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 bind(s: &ParserSettings) -> expr::BindSpec92 = name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}93 / name:$(id()) _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: Some(params), value: expr}}94 pub rule assertion(s: &ParserSettings) -> expr::AssertStmt95 = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }9697 pub rule whole_line() -> &'input str98 = str:$((!['\n'][_])* "\n") {str}99 pub rule string_block() -> String100 = "|||" (!['\n']single_whitespace())* "\n"101 empty_lines:$(['\n']*)102 prefix:[' ' | '\t']+ first_line:whole_line()103 lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*104 [' ' | '\t']*<, {prefix.len() - 1}> "|||"105 {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}106107 rule hex_char()108 = quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")109110 rule string_char(c: rule<()>)111 = (!['\\']!c()[_])+112 / "\\\\"113 / "\\u" hex_char() hex_char() hex_char() hex_char()114 / "\\x" hex_char() hex_char()115 / ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't'] / c() } / expected!("<escape character>"))116 pub rule string() -> String117 = ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}118 / ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}119 / quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}120 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}121 / string_block() } / expected!("<string>")122123 pub rule field_name(s: &ParserSettings) -> expr::FieldName124 = name:$(id()) {expr::FieldName::Fixed(name.into())}125 / name:string() {expr::FieldName::Fixed(name.into())}126 / "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}127 pub rule visibility() -> expr::Visibility128 = ":::" {expr::Visibility::Unhide}129 / "::" {expr::Visibility::Hidden}130 / ":" {expr::Visibility::Normal}131 pub rule field(s: &ParserSettings) -> expr::FieldMember132 = name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{133 name,134 plus: plus.is_some(),135 params: None,136 visibility,137 value,138 }}139 / name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{140 name,141 plus: false,142 params: Some(params),143 visibility,144 value,145 }}146 pub rule obj_local(s: &ParserSettings) -> BindSpec147 = keyword("local") _ bind:bind(s) {bind}148 pub rule member(s: &ParserSettings) -> expr::Member149 = bind:obj_local(s) {expr::Member::BindStmt(bind)}150 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}151 / field:field(s) {expr::Member::Field(field)}152 pub rule objinside(s: &ParserSettings) -> expr::ObjBody153 = 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})? {154 let mut compspecs = vec![CompSpec::ForSpec(forspec)];155 compspecs.extend(others.unwrap_or_default());156 expr::ObjBody::ObjComp(expr::ObjComp{157 pre_locals,158 key,159 plus: plus.is_some(),160 value,161 post_locals,162 compspecs,163 })164 }165 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}166 pub rule ifspec(s: &ParserSettings) -> IfSpecData167 = keyword("if") _ expr:expr(s) {IfSpecData(expr)}168 pub rule forspec(s: &ParserSettings) -> ForSpecData169 = keyword("for") _ id:$(id()) _ keyword("in") _ cond:expr(s) {ForSpecData(id.into(), cond)}170 pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>171 = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}172 pub rule local_expr(s: &ParserSettings) -> Expr173 = keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }174 pub rule string_expr(s: &ParserSettings) -> Expr175 = s:string() {Expr::Str(s.into())}176 pub rule obj_expr(s: &ParserSettings) -> Expr177 = "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}178 pub rule array_expr(s: &ParserSettings) -> Expr179 = "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}180 pub rule array_comp_expr(s: &ParserSettings) -> Expr181 = "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {182 let mut specs = vec![CompSpec::ForSpec(forspec)];183 specs.extend(others.unwrap_or_default());184 Expr::ArrComp(expr, specs)185 }186 pub rule number_expr(s: &ParserSettings) -> Expr187 = n:number() { expr::Expr::Num(n) }188 pub rule var_expr(s: &ParserSettings) -> Expr189 = n:$(id()) { expr::Expr::Var(n.into()) }190 pub rule id_loc(s: &ParserSettings) -> LocExpr191 = a:position!() n:$(id()) b:position!() { LocExpr(Rc::new(expr::Expr::Str(n.into())), ExprLocation(s.file_name.clone(), a,b)) }192 pub rule if_then_else_expr(s: &ParserSettings) -> Expr193 = cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{194 cond,195 cond_then,196 cond_else,197 }}198199 pub rule literal(s: &ParserSettings) -> Expr200 = v:(201 keyword("null") {LiteralType::Null}202 / keyword("true") {LiteralType::True}203 / keyword("false") {LiteralType::False}204 / keyword("self") {LiteralType::This}205 / keyword("$") {LiteralType::Dollar}206 / keyword("super") {LiteralType::Super}207 ) {Expr::Literal(v)}208209 pub rule expr_basic(s: &ParserSettings) -> Expr210 = literal(s)211212 / quiet!{"$intrinsic(" name:$(id()) ")" {Expr::Intrinsic(name.into())}}213214 / string_expr(s) / number_expr(s)215 / array_expr(s)216 / obj_expr(s)217 / array_expr(s)218 / array_comp_expr(s)219220 / keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}221 / keyword("importbin") _ path:string() {Expr::ImportBin(PathBuf::from(path))}222 / keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}223224 / var_expr(s)225 / local_expr(s)226 / if_then_else_expr(s)227228 / keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}229 / assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }230231 / keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }232233 rule slice_part(s: &ParserSettings) -> Option<LocExpr>234 = e:(_ e:expr(s) _{e})? {e}235 pub rule slice_desc(s: &ParserSettings) -> SliceDesc236 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {237 let (end, step) = if let Some((end, step)) = pair {238 (end, step)239 }else{240 (None, None)241 };242243 SliceDesc { start, end, step }244 }245246 rule binop(x: rule<()>) -> ()247 = quiet!{ x() } / expected!("<binary op>")248 rule unaryop(x: rule<()>) -> ()249 = quiet!{ x() } / expected!("<unary op>")250251252 use BinaryOpType::*;253 use UnaryOpType::*;254 rule expr(s: &ParserSettings) -> LocExpr255 = precedence! {256 start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.file_name.clone(), start, end)) }257 --258 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}259 --260 a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}261 --262 a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}263 --264 a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}265 --266 a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}267 --268 a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}269 a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}270 --271 a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}272 a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}273 a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}274 a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}275 a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}276 --277 a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}278 a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}279 --280 a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}281 a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}282 --283 a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}284 a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}285 a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}286 --287 unaryop(<"-">) _ b:@ {expr_un!(Minus b)}288 unaryop(<"!">) _ b:@ {expr_un!(Not b)}289 unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}290 --291 a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}292 a:(@) _ "." _ a:position!() e:id_loc(s) b:position!() {Expr::Index(a, e)}293 a:(@) _ "[" _ e:expr(s) _ "]" {Expr::Index(a, e)}294 a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}295 a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}296 --297 e:expr_basic(s) {e}298 "(" _ e:expr(s) _ ")" {Expr::Parened(e)}299 }300301 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}302 }303}304305pub type ParseError = peg::error::ParseError<peg::str::LineCol>;306pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {307 jsonnet_parser::jsonnet(str, settings)308}309/// Used for importstr values310pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {311 let len = str.len();312 LocExpr(313 Rc::new(Expr::Str(str)),314 ExprLocation(settings.file_name.clone(), 0, len),315 )316}317318#[cfg(test)]319pub mod tests {320 use super::{expr::*, parse};321 use crate::ParserSettings;322 use std::path::PathBuf;323 use BinaryOpType::*;324325 macro_rules! parse {326 ($s:expr) => {327 parse(328 $s,329 &ParserSettings {330 file_name: PathBuf::from("test.jsonnet").into(),331 },332 )333 .unwrap()334 };335 }336337 macro_rules! el {338 ($expr:expr, $from:expr, $to:expr$(,)?) => {339 LocExpr(340 std::rc::Rc::new($expr),341 ExprLocation(PathBuf::from("test.jsonnet").into(), $from, $to),342 )343 };344 }345346 #[test]347 fn multiline_string() {348 assert_eq!(349 parse!("|||\n Hello world!\n a\n|||"),350 el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),351 );352 assert_eq!(353 parse!("|||\n Hello world!\n a\n|||"),354 el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),355 );356 assert_eq!(357 parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),358 el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),359 );360 assert_eq!(361 parse!("|||\n Hello world!\n a\n |||"),362 el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),363 );364 }365366 #[test]367 fn slice() {368 parse!("a[1:]");369 parse!("a[1::]");370 parse!("a[:1:]");371 parse!("a[::1]");372 parse!("str[:len - 1]");373 }374375 #[test]376 fn string_escaping() {377 assert_eq!(378 parse!(r#""Hello, \"world\"!""#),379 el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),380 );381 assert_eq!(382 parse!(r#"'Hello \'world\'!'"#),383 el!(Expr::Str("Hello 'world'!".into()), 0, 18),384 );385 assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));386 }387388 #[test]389 fn string_unescaping() {390 assert_eq!(391 parse!(r#""Hello\nWorld""#),392 el!(Expr::Str("Hello\nWorld".into()), 0, 14),393 );394 }395396 #[test]397 fn string_verbantim() {398 assert_eq!(399 parse!(r#"@"Hello\n""World""""#),400 el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),401 );402 }403404 #[test]405 fn imports() {406 assert_eq!(407 parse!("import \"hello\""),408 el!(Expr::Import(PathBuf::from("hello")), 0, 14),409 );410 assert_eq!(411 parse!("importstr \"garnish.txt\""),412 el!(Expr::ImportStr(PathBuf::from("garnish.txt")), 0, 23)413 );414 }415416 #[test]417 fn empty_object() {418 assert_eq!(419 parse!("{}"),420 el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)421 );422 }423424 #[test]425 fn basic_math() {426 assert_eq!(427 parse!("2+2*2"),428 el!(429 Expr::BinaryOp(430 el!(Expr::Num(2.0), 0, 1),431 Add,432 el!(433 Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),434 2,435 5436 )437 ),438 0,439 5440 )441 );442 }443444 #[test]445 fn basic_math_with_indents() {446 assert_eq!(447 parse!("2 + 2 * 2 "),448 el!(449 Expr::BinaryOp(450 el!(Expr::Num(2.0), 0, 1),451 Add,452 el!(453 Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),454 7,455 14456 ),457 ),458 0,459 14460 )461 );462 }463464 #[test]465 fn basic_math_parened() {466 assert_eq!(467 parse!("2+(2+2*2)"),468 el!(469 Expr::BinaryOp(470 el!(Expr::Num(2.0), 0, 1),471 Add,472 el!(473 Expr::Parened(el!(474 Expr::BinaryOp(475 el!(Expr::Num(2.0), 3, 4),476 Add,477 el!(478 Expr::BinaryOp(479 el!(Expr::Num(2.0), 5, 6),480 Mul,481 el!(Expr::Num(2.0), 7, 8),482 ),483 5,484 8485 ),486 ),487 3,488 8489 )),490 2,491 9492 ),493 ),494 0,495 9496 )497 );498 }499500 /// Comments should not affect parsing501 #[test]502 fn comments() {503 assert_eq!(504 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),505 el!(506 Expr::BinaryOp(507 el!(Expr::Num(2.0), 0, 1),508 Add,509 el!(510 Expr::BinaryOp(511 el!(Expr::Num(3.0), 22, 23),512 Mul,513 el!(Expr::Num(4.0), 40, 41)514 ),515 22,516 41517 )518 ),519 0,520 41521 )522 );523 }524525 /// Comments should be able to be escaped526 #[test]527 fn comment_escaping() {528 assert_eq!(529 parse!("2/*\\*/+*/ - 22"),530 el!(531 Expr::BinaryOp(el!(Expr::Num(2.0), 0, 1), Sub, el!(Expr::Num(22.0), 12, 14)),532 0,533 14534 )535 );536 }537538 #[test]539 fn suffix() {540 // assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));541 // assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));542 // assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));543 // assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))544 }545546 #[test]547 fn array_comp() {548 use Expr::*;549 /*550 `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`,551 `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`552 */553 assert_eq!(554 parse!("[std.deepJoin(x) for x in arr]"),555 el!(556 ArrComp(557 el!(558 Apply(559 el!(560 Index(561 el!(Var("std".into()), 1, 4),562 el!(Str("deepJoin".into()), 5, 13)563 ),564 1,565 13566 ),567 ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),568 false,569 ),570 1,571 16572 ),573 vec![CompSpec::ForSpec(ForSpecData(574 "x".into(),575 el!(Var("arr".into()), 26, 29)576 ))]577 ),578 0,579 30580 ),581 )582 }583584 #[test]585 fn reserved() {586 use Expr::*;587 assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));588 assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));589 }590591 #[test]592 fn multiple_args_buf() {593 parse!("a(b, null_fields)");594 }595596 #[test]597 fn infix_precedence() {598 use Expr::*;599 assert_eq!(600 parse!("!a && !b"),601 el!(602 BinaryOp(603 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),604 And,605 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)606 ),607 0,608 8609 )610 );611 }612613 #[test]614 fn infix_precedence_division() {615 use Expr::*;616 assert_eq!(617 parse!("!a / !b"),618 el!(619 BinaryOp(620 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),621 Div,622 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)623 ),624 0,625 7626 )627 );628 }629630 #[test]631 fn double_negation() {632 use Expr::*;633 assert_eq!(634 parse!("!!a"),635 el!(636 UnaryOp(637 UnaryOpType::Not,638 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)639 ),640 0,641 3642 )643 )644 }645646 #[test]647 fn array_test_error() {648 parse!("[a for a in b if c for e in f]");649 // ^^^^ failed code650 }651652 #[test]653 fn missing_newline_between_comment_and_eof() {654 parse!(655 "{a:1}656657 //+213"658 );659 }660661 #[test]662 fn default_param_before_nondefault() {663 parse!("local x(foo = 'foo', bar) = null; null");664 }665666 #[test]667 fn can_parse_stdlib() {668 parse!(jrsonnet_stdlib::STDLIB_STR);669 }670671 #[test]672 fn add_location_info_to_all_sub_expressions() {673 use Expr::*;674675 let file_name: std::rc::Rc<std::path::Path> = PathBuf::from("test.jsonnet").into();676 let expr = parse(677 "{} { local x = 1, x: x } + {}",678 &ParserSettings {679 file_name: file_name.clone(),680 },681 )682 .unwrap();683 assert_eq!(684 expr,685 el!(686 BinaryOp(687 el!(688 ObjExtend(689 el!(Obj(ObjBody::MemberList(vec![])), 0, 2),690 ObjBody::MemberList(vec![691 Member::BindStmt(BindSpec {692 name: "x".into(),693 params: None,694 value: el!(Num(1.0), 15, 16)695 }),696 Member::Field(FieldMember {697 name: FieldName::Fixed("x".into()),698 plus: false,699 params: None,700 visibility: Visibility::Normal,701 value: el!(Var("x".into()), 21, 22),702 })703 ])704 ),705 0,706 24707 ),708 BinaryOpType::Add,709 el!(Obj(ObjBody::MemberList(vec![])), 27, 29),710 ),711 0,712 29713 ),714 );715 }716 // From source code717 /*718 #[bench]719 fn bench_parse_peg(b: &mut Bencher) {720 b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))721 }722 */723}1#![allow(clippy::redundant_closure_call)]23use std::{4 path::{Path, PathBuf},5 rc::Rc,6};78use peg::parser;9mod expr;10pub use expr::*;11pub use jrsonnet_interner::IStr;12pub use peg;13mod unescape;1415pub struct ParserSettings {16 pub file_name: Rc<Path>,17}1819macro_rules! expr_bin {20 ($a:ident $op:ident $b:ident) => {21 Expr::BinaryOp($a, $op, $b)22 };23}24macro_rules! expr_un {25 ($op:ident $a:ident) => {26 Expr::UnaryOp($op, $a)27 };28}2930parser! {31 grammar jsonnet_parser() for str {32 use peg::ParseLiteral;3334 rule eof() = quiet!{![_]} / expected!("<eof>")35 rule eol() = "\n" / eof()3637 /// Standard C-like comments38 rule comment()39 = "//" (!eol()[_])* eol()40 / "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"41 / "#" (!eol()[_])* eol()4243 rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")44 rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")4546 /// For comma-delimited elements47 rule comma() = quiet!{_ "," _} / expected!("<comma>")48 rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}49 rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}50 rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']51 /// Sequence of digits52 rule uint_str() -> &'input str = a:$(digit()+) { a }53 /// Number in scientific notation format54 rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")5556 /// Reserved word followed by any non-alphanumberic57 rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()58 rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")5960 rule keyword(id: &'static str) -> ()61 = ##parse_string_literal(id) end_of_ident()6263 pub rule param(s: &ParserSettings) -> expr::Param = name:$(id()) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name.into(), expr) }64 pub rule params(s: &ParserSettings) -> expr::ParamsDesc65 = params:param(s) ** comma() comma()? { expr::ParamsDesc(Rc::new(params)) }66 / { expr::ParamsDesc(Rc::new(Vec::new())) }6768 pub rule arg(s: &ParserSettings) -> (Option<IStr>, LocExpr)69 = quiet! { name:(s:$(id()) _ "=" _ {s})? expr:expr(s) {(name.map(Into::into), expr)} }70 / expected!("<argument>")7172 pub rule args(s: &ParserSettings) -> expr::ArgsDesc73 = args:arg(s)**comma() comma()? {?74 let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();75 let mut unnamed = Vec::with_capacity(unnamed_count);76 let mut named = Vec::with_capacity(args.len() - unnamed_count);77 let mut named_started = false;78 for (name, value) in args {79 if let Some(name) = name {80 named_started = true;81 named.push((name, value));82 } else {83 if named_started {84 return Err("<named argument>")85 }86 unnamed.push(value);87 }88 }89 Ok(expr::ArgsDesc::new(unnamed, named))90 }9192 pub rule bind(s: &ParserSettings) -> expr::BindSpec93 = name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}94 / name:$(id()) _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: Some(params), value: expr}}95 pub rule assertion(s: &ParserSettings) -> expr::AssertStmt96 = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }9798 pub rule whole_line() -> &'input str99 = str:$((!['\n'][_])* "\n") {str}100 pub rule string_block() -> String101 = "|||" (!['\n']single_whitespace())* "\n"102 empty_lines:$(['\n']*)103 prefix:[' ' | '\t']+ first_line:whole_line()104 lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*105 [' ' | '\t']*<, {prefix.len() - 1}> "|||"106 {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}107108 rule hex_char()109 = quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")110111 rule string_char(c: rule<()>)112 = (!['\\']!c()[_])+113 / "\\\\"114 / "\\u" hex_char() hex_char() hex_char() hex_char()115 / "\\x" hex_char() hex_char()116 / ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't'] / c() } / expected!("<escape character>"))117 pub rule string() -> String118 = ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}119 / ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}120 / quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}121 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}122 / string_block() } / expected!("<string>")123124 pub rule field_name(s: &ParserSettings) -> expr::FieldName125 = name:$(id()) {expr::FieldName::Fixed(name.into())}126 / name:string() {expr::FieldName::Fixed(name.into())}127 / "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}128 pub rule visibility() -> expr::Visibility129 = ":::" {expr::Visibility::Unhide}130 / "::" {expr::Visibility::Hidden}131 / ":" {expr::Visibility::Normal}132 pub rule field(s: &ParserSettings) -> expr::FieldMember133 = name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{134 name,135 plus: plus.is_some(),136 params: None,137 visibility,138 value,139 }}140 / name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{141 name,142 plus: false,143 params: Some(params),144 visibility,145 value,146 }}147 pub rule obj_local(s: &ParserSettings) -> BindSpec148 = keyword("local") _ bind:bind(s) {bind}149 pub rule member(s: &ParserSettings) -> expr::Member150 = bind:obj_local(s) {expr::Member::BindStmt(bind)}151 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}152 / field:field(s) {expr::Member::Field(field)}153 pub rule objinside(s: &ParserSettings) -> expr::ObjBody154 = 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})? {155 let mut compspecs = vec![CompSpec::ForSpec(forspec)];156 compspecs.extend(others.unwrap_or_default());157 expr::ObjBody::ObjComp(expr::ObjComp{158 pre_locals,159 key,160 plus: plus.is_some(),161 value,162 post_locals,163 compspecs,164 })165 }166 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}167 pub rule ifspec(s: &ParserSettings) -> IfSpecData168 = keyword("if") _ expr:expr(s) {IfSpecData(expr)}169 pub rule forspec(s: &ParserSettings) -> ForSpecData170 = keyword("for") _ id:$(id()) _ keyword("in") _ cond:expr(s) {ForSpecData(id.into(), cond)}171 pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>172 = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}173 pub rule local_expr(s: &ParserSettings) -> Expr174 = keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }175 pub rule string_expr(s: &ParserSettings) -> Expr176 = s:string() {Expr::Str(s.into())}177 pub rule obj_expr(s: &ParserSettings) -> Expr178 = "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}179 pub rule array_expr(s: &ParserSettings) -> Expr180 = "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}181 pub rule array_comp_expr(s: &ParserSettings) -> Expr182 = "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {183 let mut specs = vec![CompSpec::ForSpec(forspec)];184 specs.extend(others.unwrap_or_default());185 Expr::ArrComp(expr, specs)186 }187 pub rule number_expr(s: &ParserSettings) -> Expr188 = n:number() { expr::Expr::Num(n) }189 pub rule var_expr(s: &ParserSettings) -> Expr190 = n:$(id()) { expr::Expr::Var(n.into()) }191 pub rule id_loc(s: &ParserSettings) -> LocExpr192 = a:position!() n:$(id()) b:position!() { LocExpr(Rc::new(expr::Expr::Str(n.into())), ExprLocation(s.file_name.clone(), a,b)) }193 pub rule if_then_else_expr(s: &ParserSettings) -> Expr194 = cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{195 cond,196 cond_then,197 cond_else,198 }}199200 pub rule literal(s: &ParserSettings) -> Expr201 = v:(202 keyword("null") {LiteralType::Null}203 / keyword("true") {LiteralType::True}204 / keyword("false") {LiteralType::False}205 / keyword("self") {LiteralType::This}206 / keyword("$") {LiteralType::Dollar}207 / keyword("super") {LiteralType::Super}208 ) {Expr::Literal(v)}209210 pub rule expr_basic(s: &ParserSettings) -> Expr211 = literal(s)212213 / quiet!{"$intrinsic(" name:$(id()) ")" {Expr::Intrinsic(name.into())}}214215 / string_expr(s) / number_expr(s)216 / array_expr(s)217 / obj_expr(s)218 / array_expr(s)219 / array_comp_expr(s)220221 / keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}222 / keyword("importbin") _ path:string() {Expr::ImportBin(PathBuf::from(path))}223 / keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}224225 / var_expr(s)226 / local_expr(s)227 / if_then_else_expr(s)228229 / keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}230 / assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }231232 / keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }233234 rule slice_part(s: &ParserSettings) -> Option<LocExpr>235 = e:(_ e:expr(s) _{e})? {e}236 pub rule slice_desc(s: &ParserSettings) -> SliceDesc237 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {238 let (end, step) = if let Some((end, step)) = pair {239 (end, step)240 }else{241 (None, None)242 };243244 SliceDesc { start, end, step }245 }246247 rule binop(x: rule<()>) -> ()248 = quiet!{ x() } / expected!("<binary op>")249 rule unaryop(x: rule<()>) -> ()250 = quiet!{ x() } / expected!("<unary op>")251252253 use BinaryOpType::*;254 use UnaryOpType::*;255 rule expr(s: &ParserSettings) -> LocExpr256 = precedence! {257 start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.file_name.clone(), start, end)) }258 --259 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}260 --261 a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}262 --263 a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}264 --265 a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}266 --267 a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}268 --269 a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}270 a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}271 --272 a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}273 a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}274 a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}275 a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}276 a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}277 --278 a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}279 a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}280 --281 a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}282 a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}283 --284 a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}285 a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}286 a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}287 --288 unaryop(<"-">) _ b:@ {expr_un!(Minus b)}289 unaryop(<"!">) _ b:@ {expr_un!(Not b)}290 unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}291 --292 a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}293 a:(@) _ "." _ a:position!() e:id_loc(s) b:position!() {Expr::Index(a, e)}294 a:(@) _ "[" _ e:expr(s) _ "]" {Expr::Index(a, e)}295 a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}296 a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}297 --298 e:expr_basic(s) {e}299 "(" _ e:expr(s) _ ")" {Expr::Parened(e)}300 }301302 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}303 }304}305306pub type ParseError = peg::error::ParseError<peg::str::LineCol>;307pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {308 jsonnet_parser::jsonnet(str, settings)309}310/// Used for importstr values311pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {312 let len = str.len();313 LocExpr(314 Rc::new(Expr::Str(str)),315 ExprLocation(settings.file_name.clone(), 0, len),316 )317}318319#[cfg(test)]320pub mod tests {321 use std::path::PathBuf;322323 use BinaryOpType::*;324325 use super::{expr::*, parse};326 use crate::ParserSettings;327328 macro_rules! parse {329 ($s:expr) => {330 parse(331 $s,332 &ParserSettings {333 file_name: PathBuf::from("test.jsonnet").into(),334 },335 )336 .unwrap()337 };338 }339340 macro_rules! el {341 ($expr:expr, $from:expr, $to:expr$(,)?) => {342 LocExpr(343 std::rc::Rc::new($expr),344 ExprLocation(PathBuf::from("test.jsonnet").into(), $from, $to),345 )346 };347 }348349 #[test]350 fn multiline_string() {351 assert_eq!(352 parse!("|||\n Hello world!\n a\n|||"),353 el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),354 );355 assert_eq!(356 parse!("|||\n Hello world!\n a\n|||"),357 el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),358 );359 assert_eq!(360 parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),361 el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),362 );363 assert_eq!(364 parse!("|||\n Hello world!\n a\n |||"),365 el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),366 );367 }368369 #[test]370 fn slice() {371 parse!("a[1:]");372 parse!("a[1::]");373 parse!("a[:1:]");374 parse!("a[::1]");375 parse!("str[:len - 1]");376 }377378 #[test]379 fn string_escaping() {380 assert_eq!(381 parse!(r#""Hello, \"world\"!""#),382 el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),383 );384 assert_eq!(385 parse!(r#"'Hello \'world\'!'"#),386 el!(Expr::Str("Hello 'world'!".into()), 0, 18),387 );388 assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));389 }390391 #[test]392 fn string_unescaping() {393 assert_eq!(394 parse!(r#""Hello\nWorld""#),395 el!(Expr::Str("Hello\nWorld".into()), 0, 14),396 );397 }398399 #[test]400 fn string_verbantim() {401 assert_eq!(402 parse!(r#"@"Hello\n""World""""#),403 el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),404 );405 }406407 #[test]408 fn imports() {409 assert_eq!(410 parse!("import \"hello\""),411 el!(Expr::Import(PathBuf::from("hello")), 0, 14),412 );413 assert_eq!(414 parse!("importstr \"garnish.txt\""),415 el!(Expr::ImportStr(PathBuf::from("garnish.txt")), 0, 23)416 );417 }418419 #[test]420 fn empty_object() {421 assert_eq!(422 parse!("{}"),423 el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)424 );425 }426427 #[test]428 fn basic_math() {429 assert_eq!(430 parse!("2+2*2"),431 el!(432 Expr::BinaryOp(433 el!(Expr::Num(2.0), 0, 1),434 Add,435 el!(436 Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),437 2,438 5439 )440 ),441 0,442 5443 )444 );445 }446447 #[test]448 fn basic_math_with_indents() {449 assert_eq!(450 parse!("2 + 2 * 2 "),451 el!(452 Expr::BinaryOp(453 el!(Expr::Num(2.0), 0, 1),454 Add,455 el!(456 Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),457 7,458 14459 ),460 ),461 0,462 14463 )464 );465 }466467 #[test]468 fn basic_math_parened() {469 assert_eq!(470 parse!("2+(2+2*2)"),471 el!(472 Expr::BinaryOp(473 el!(Expr::Num(2.0), 0, 1),474 Add,475 el!(476 Expr::Parened(el!(477 Expr::BinaryOp(478 el!(Expr::Num(2.0), 3, 4),479 Add,480 el!(481 Expr::BinaryOp(482 el!(Expr::Num(2.0), 5, 6),483 Mul,484 el!(Expr::Num(2.0), 7, 8),485 ),486 5,487 8488 ),489 ),490 3,491 8492 )),493 2,494 9495 ),496 ),497 0,498 9499 )500 );501 }502503 /// Comments should not affect parsing504 #[test]505 fn comments() {506 assert_eq!(507 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),508 el!(509 Expr::BinaryOp(510 el!(Expr::Num(2.0), 0, 1),511 Add,512 el!(513 Expr::BinaryOp(514 el!(Expr::Num(3.0), 22, 23),515 Mul,516 el!(Expr::Num(4.0), 40, 41)517 ),518 22,519 41520 )521 ),522 0,523 41524 )525 );526 }527528 /// Comments should be able to be escaped529 #[test]530 fn comment_escaping() {531 assert_eq!(532 parse!("2/*\\*/+*/ - 22"),533 el!(534 Expr::BinaryOp(el!(Expr::Num(2.0), 0, 1), Sub, el!(Expr::Num(22.0), 12, 14)),535 0,536 14537 )538 );539 }540541 #[test]542 fn suffix() {543 // assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));544 // assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));545 // assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));546 // assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))547 }548549 #[test]550 fn array_comp() {551 use Expr::*;552 /*553 `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`,554 `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`555 */556 assert_eq!(557 parse!("[std.deepJoin(x) for x in arr]"),558 el!(559 ArrComp(560 el!(561 Apply(562 el!(563 Index(564 el!(Var("std".into()), 1, 4),565 el!(Str("deepJoin".into()), 5, 13)566 ),567 1,568 13569 ),570 ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),571 false,572 ),573 1,574 16575 ),576 vec![CompSpec::ForSpec(ForSpecData(577 "x".into(),578 el!(Var("arr".into()), 26, 29)579 ))]580 ),581 0,582 30583 ),584 )585 }586587 #[test]588 fn reserved() {589 use Expr::*;590 assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));591 assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));592 }593594 #[test]595 fn multiple_args_buf() {596 parse!("a(b, null_fields)");597 }598599 #[test]600 fn infix_precedence() {601 use Expr::*;602 assert_eq!(603 parse!("!a && !b"),604 el!(605 BinaryOp(606 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),607 And,608 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)609 ),610 0,611 8612 )613 );614 }615616 #[test]617 fn infix_precedence_division() {618 use Expr::*;619 assert_eq!(620 parse!("!a / !b"),621 el!(622 BinaryOp(623 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),624 Div,625 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)626 ),627 0,628 7629 )630 );631 }632633 #[test]634 fn double_negation() {635 use Expr::*;636 assert_eq!(637 parse!("!!a"),638 el!(639 UnaryOp(640 UnaryOpType::Not,641 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)642 ),643 0,644 3645 )646 )647 }648649 #[test]650 fn array_test_error() {651 parse!("[a for a in b if c for e in f]");652 // ^^^^ failed code653 }654655 #[test]656 fn missing_newline_between_comment_and_eof() {657 parse!(658 "{a:1}659660 //+213"661 );662 }663664 #[test]665 fn default_param_before_nondefault() {666 parse!("local x(foo = 'foo', bar) = null; null");667 }668669 #[test]670 fn can_parse_stdlib() {671 parse!(jrsonnet_stdlib::STDLIB_STR);672 }673674 #[test]675 fn add_location_info_to_all_sub_expressions() {676 use Expr::*;677678 let file_name: std::rc::Rc<std::path::Path> = PathBuf::from("test.jsonnet").into();679 let expr = parse(680 "{} { local x = 1, x: x } + {}",681 &ParserSettings {682 file_name: file_name.clone(),683 },684 )685 .unwrap();686 assert_eq!(687 expr,688 el!(689 BinaryOp(690 el!(691 ObjExtend(692 el!(Obj(ObjBody::MemberList(vec![])), 0, 2),693 ObjBody::MemberList(vec![694 Member::BindStmt(BindSpec {695 name: "x".into(),696 params: None,697 value: el!(Num(1.0), 15, 16)698 }),699 Member::Field(FieldMember {700 name: FieldName::Fixed("x".into()),701 plus: false,702 params: None,703 visibility: Visibility::Normal,704 value: el!(Var("x".into()), 21, 22),705 })706 ])707 ),708 0,709 24710 ),711 BinaryOpType::Add,712 el!(Obj(ObjBody::MemberList(vec![])), 27, 29),713 ),714 0,715 29716 ),717 );718 }719 // From source code720 /*721 #[bench]722 fn bench_parse_peg(b: &mut Bencher) {723 b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))724 }725 */726}crates/jrsonnet-types/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-types/src/lib.rs
+++ b/crates/jrsonnet-types/src/lib.rs
@@ -1,8 +1,9 @@
#![allow(clippy::redundant_closure_call)]
-use gcmodule::Trace;
use std::fmt::Display;
+use gcmodule::Trace;
+
#[macro_export]
macro_rules! ty {
((Array<number>)) => {{