git.delta.rocks / jrsonnet / refs/commits / 8f3ed48f89ef

difftreelog

refactor simplify and unify builtins

Yaroslav Bolyukin2021-12-27parent: #393dcbd.patch.diff
in: master

14 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -83,17 +83,16 @@
 		std::process::exit(0);
 	};
 
-	let success;
-	if let Some(size) = opts.debug.os_stack {
-		success = std::thread::Builder::new()
+	let success = if let Some(size) = opts.debug.os_stack {
+		std::thread::Builder::new()
 			.stack_size(size * 1024 * 1024)
 			.spawn(|| main_catch(opts))
 			.expect("new thread spawned")
 			.join()
-			.expect("thread finished successfully");
+			.expect("thread finished successfully")
 	} else {
-		success = main_catch(opts)
-	}
+		main_catch(opts)
+	};
 	if !success {
 		std::process::exit(1);
 	}
modifiedcrates/jrsonnet-evaluator/src/builtin/format.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/format.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/format.rs
@@ -577,10 +577,8 @@
 			}
 		}
 		ConvTypeV::Char => match value.clone() {
-			Val::Num(n) => tmp_out.push(
-				std::char::from_u32(n as u32)
-					.ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?,
-			),
+			Val::Num(n) => tmp_out
+				.push(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?),
 			Val::Str(s) => {
 				if s.chars().count() != 1 {
 					throw!(RuntimeError(
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,3 +1,4 @@
+use crate::function::StaticBuiltin;
 use crate::typed::{Any, Either, Null, PositiveF64, VecVal, M1};
 use crate::{self as jrsonnet_evaluator, ObjValue};
 use crate::{
@@ -5,21 +6,16 @@
 	equals,
 	error::{Error::*, Result},
 	operator::evaluate_mod_op,
-	primitive_equals, push_frame, throw, with_state, ArrValue, Context, FuncVal,
-	IndexableVal, Val,
+	primitive_equals, push_frame, throw, with_state, ArrValue, Context, FuncVal, IndexableVal, Val,
 };
 use format::{format_arr, format_obj};
 use gcmodule::Cc;
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ArgsDesc, ExprLocation};
+use jrsonnet_parser::ExprLocation;
 use serde::Deserialize;
 use serde_yaml::DeserializingQuirks;
-use std::{
-	collections::HashMap,
-	convert::{TryFrom, TryInto},
-	path::PathBuf,
-	rc::Rc,
-};
+use std::collections::HashMap;
+use std::convert::{TryFrom, TryInto};
 
 pub mod stdlib;
 pub use stdlib::*;
@@ -32,7 +28,7 @@
 
 pub fn std_format(str: IStr, vals: Val) -> Result<String> {
 	push_frame(
-		&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0),
+		None,
 		|| format!("std.format of {}", str),
 		|| {
 			Ok(match vals {
@@ -75,86 +71,85 @@
 		)),
 	}
 }
-
-type Builtin = fn(context: Context, loc: &ExprLocation, args: &ArgsDesc) -> Result<Val>;
 
-type BuiltinsType = HashMap<Box<str>, Builtin>;
+type BuiltinsType = HashMap<IStr, &'static dyn StaticBuiltin>;
 
 thread_local! {
-	static BUILTINS: BuiltinsType = {
+	pub static BUILTINS: BuiltinsType = {
 		[
-			("length".into(), builtin_length as Builtin),
-			("type".into(), builtin_type),
-			("makeArray".into(), builtin_make_array),
-			("codepoint".into(), builtin_codepoint),
-			("objectFieldsEx".into(), builtin_object_fields_ex),
-			("objectHasEx".into(), builtin_object_has_ex),
-			("slice".into(), builtin_slice),
-			("substr".into(), builtin_substr),
-			("primitiveEquals".into(), builtin_primitive_equals),
-			("equals".into(), builtin_equals),
-			("modulo".into(), builtin_modulo),
-			("mod".into(), builtin_mod),
-			("floor".into(), builtin_floor),
-			("ceil".into(), builtin_ceil),
-			("log".into(), builtin_log),
-			("pow".into(), builtin_pow),
-			("sqrt".into(), builtin_sqrt),
-			("sin".into(), builtin_sin),
-			("cos".into(), builtin_cos),
-			("tan".into(), builtin_tan),
-			("asin".into(), builtin_asin),
-			("acos".into(), builtin_acos),
-			("atan".into(), builtin_atan),
-			("exp".into(), builtin_exp),
-			("mantissa".into(), builtin_mantissa),
-			("exponent".into(), builtin_exponent),
-			("extVar".into(), builtin_ext_var),
-			("native".into(), builtin_native),
-			("filter".into(), builtin_filter),
-			("map".into(), builtin_map),
-			("flatMap".into(), builtin_flatmap),
-			("foldl".into(), builtin_foldl),
-			("foldr".into(), builtin_foldr),
-			("sortImpl".into(), builtin_sort_impl),
-			("format".into(), builtin_format),
-			("range".into(), builtin_range),
-			("char".into(), builtin_char),
-			("encodeUTF8".into(), builtin_encode_utf8),
-			("decodeUTF8".into(), builtin_decode_utf8),
-			("md5".into(), builtin_md5),
-			("base64".into(), builtin_base64),
-			("base64DecodeBytes".into(), builtin_base64_decode_bytes),
-			("base64Decode".into(), builtin_base64_decode),
-			("trace".into(), builtin_trace),
-			("join".into(), builtin_join),
-			("escapeStringJson".into(), builtin_escape_string_json),
-			("manifestJsonEx".into(), builtin_manifest_json_ex),
-			("manifestYamlDocImpl".into(), builtin_manifest_yaml_doc),
-			("reverse".into(), builtin_reverse),
-			("id".into(), builtin_id),
-			("strReplace".into(), builtin_str_replace),
-			("splitLimit".into(), builtin_splitlimit),
-			("parseJson".into(), builtin_parse_json),
-			("parseYaml".into(), builtin_parse_yaml),
-			("asciiUpper".into(), builtin_ascii_upper),
-			("asciiLower".into(), builtin_ascii_lower),
-			("member".into(), builtin_member),
-			("count".into(), builtin_count),
+			("length".into(), builtin_length::INST),
+			("type".into(), builtin_type::INST),
+			("makeArray".into(), builtin_make_array::INST),
+			("codepoint".into(), builtin_codepoint::INST),
+			("objectFieldsEx".into(), builtin_object_fields_ex::INST),
+			("objectHasEx".into(), builtin_object_has_ex::INST),
+			("slice".into(), builtin_slice::INST),
+			("substr".into(), builtin_substr::INST),
+			("primitiveEquals".into(), builtin_primitive_equals::INST),
+			("equals".into(), builtin_equals::INST),
+			("modulo".into(), builtin_modulo::INST),
+			("mod".into(), builtin_mod::INST),
+			("floor".into(), builtin_floor::INST),
+			("ceil".into(), builtin_ceil::INST),
+			("log".into(), builtin_log::INST),
+			("pow".into(), builtin_pow::INST),
+			("sqrt".into(), builtin_sqrt::INST),
+			("sin".into(), builtin_sin::INST),
+			("cos".into(), builtin_cos::INST),
+			("tan".into(), builtin_tan::INST),
+			("asin".into(), builtin_asin::INST),
+			("acos".into(), builtin_acos::INST),
+			("atan".into(), builtin_atan::INST),
+			("exp".into(), builtin_exp::INST),
+			("mantissa".into(), builtin_mantissa::INST),
+			("exponent".into(), builtin_exponent::INST),
+			("extVar".into(), builtin_ext_var::INST),
+			("native".into(), builtin_native::INST),
+			("filter".into(), builtin_filter::INST),
+			("map".into(), builtin_map::INST),
+			("flatMap".into(), builtin_flatmap::INST),
+			("foldl".into(), builtin_foldl::INST),
+			("foldr".into(), builtin_foldr::INST),
+			("sort".into(), builtin_sort::INST),
+			("format".into(), builtin_format::INST),
+			("range".into(), builtin_range::INST),
+			("char".into(), builtin_char::INST),
+			("encodeUTF8".into(), builtin_encode_utf8::INST),
+			("decodeUTF8".into(), builtin_decode_utf8::INST),
+			("md5".into(), builtin_md5::INST),
+			("base64".into(), builtin_base64::INST),
+			("base64DecodeBytes".into(), builtin_base64_decode_bytes::INST),
+			("base64Decode".into(), builtin_base64_decode::INST),
+			("trace".into(), builtin_trace::INST),
+			("join".into(), builtin_join::INST),
+			("escapeStringJson".into(), builtin_escape_string_json::INST),
+			("manifestJsonEx".into(), builtin_manifest_json_ex::INST),
+			("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),
+			("reverse".into(), builtin_reverse::INST),
+			("id".into(), builtin_id::INST),
+			("strReplace".into(), builtin_str_replace::INST),
+			("splitLimit".into(), builtin_splitlimit::INST),
+			("parseJson".into(), builtin_parse_json::INST),
+			("parseYaml".into(), builtin_parse_yaml::INST),
+			("asciiUpper".into(), builtin_ascii_upper::INST),
+			("asciiLower".into(), builtin_ascii_lower::INST),
+			("member".into(), builtin_member::INST),
+			("count".into(), builtin_count::INST),
 		].iter().cloned().collect()
 	};
 }
 
 #[jrsonnet_macros::builtin]
-fn builtin_length(x: Either<IStr, Either<VecVal, ObjValue>>) -> Result<usize> {
+fn builtin_length(x: Either<IStr, Either<VecVal, Either<ObjValue, Cc<FuncVal>>>>) -> Result<usize> {
 	Ok(match x {
 		Either::Left(x) => x.len(),
 		Either::Right(Either::Left(x)) => x.0.len(),
-		Either::Right(Either::Right(x)) => x
+		Either::Right(Either::Right(Either::Left(x))) => x
 			.fields_visibility()
 			.into_iter()
 			.filter(|(_k, v)| *v)
 			.count(),
+		Either::Right(Either::Right(Either::Right(f))) => f.args_len(),
 	})
 }
 
@@ -167,7 +162,7 @@
 fn builtin_make_array(sz: usize, func: Cc<FuncVal>) -> Result<VecVal> {
 	let mut out = Vec::with_capacity(sz);
 	for i in 0..sz {
-		out.push(func.evaluate_values(&[Val::Num(i as f64)])?)
+		out.push(func.evaluate_simple(&[i as f64].as_slice())?)
 	}
 	Ok(VecVal(out))
 }
@@ -354,12 +349,12 @@
 
 #[jrsonnet_macros::builtin]
 fn builtin_filter(func: Cc<FuncVal>, arr: ArrValue) -> Result<ArrValue> {
-	arr.filter(|val| bool::try_from(func.evaluate_values(&[val.clone()])?))
+	arr.filter(|val| bool::try_from(func.evaluate_simple(&[Any(val.clone())].as_slice())?))
 }
 
 #[jrsonnet_macros::builtin]
 fn builtin_map(func: Cc<FuncVal>, arr: ArrValue) -> Result<ArrValue> {
-	arr.map(|val| func.evaluate_values(&[val]))
+	arr.map(|val| func.evaluate_simple(&[Any(val)].as_slice()))
 }
 
 #[jrsonnet_macros::builtin]
@@ -368,7 +363,7 @@
 		IndexableVal::Str(s) => {
 			let mut out = String::new();
 			for c in s.chars() {
-				match func.evaluate_values(&[Val::Str(c.to_string().into())])? {
+				match func.evaluate_simple(&[c.to_string()].as_slice())? {
 					Val::Str(o) => out.push_str(&o),
 					_ => throw!(RuntimeError(
 						"in std.join all items should be strings".into()
@@ -381,7 +376,7 @@
 			let mut out = Vec::new();
 			for el in a.iter() {
 				let el = el?;
-				match func.evaluate_values(&[el])? {
+				match func.evaluate_simple(&[Any(el)].as_slice())? {
 					Val::Arr(o) => {
 						for oe in o.iter() {
 							out.push(oe?)
@@ -401,7 +396,7 @@
 fn builtin_foldl(func: Cc<FuncVal>, arr: ArrValue, init: Any) -> Result<Any> {
 	let mut acc = init.0;
 	for i in arr.iter() {
-		acc = func.evaluate_values(&[acc, i?])?;
+		acc = func.evaluate_simple(&[Any(acc), Any(i?)].as_slice())?;
 	}
 	Ok(Any(acc))
 }
@@ -410,18 +405,21 @@
 fn builtin_foldr(func: Cc<FuncVal>, arr: ArrValue, init: Any) -> Result<Any> {
 	let mut acc = init.0;
 	for i in arr.iter().rev() {
-		acc = func.evaluate_values(&[i?, acc])?;
+		acc = func.evaluate_simple(&[Any(i?), Any(acc)].as_slice())?;
 	}
 	Ok(Any(acc))
 }
 
 #[jrsonnet_macros::builtin]
 #[allow(non_snake_case)]
-fn builtin_sort_impl(arr: ArrValue, keyF: Cc<FuncVal>) -> Result<ArrValue> {
+fn builtin_sort(arr: ArrValue, keyF: Option<Cc<FuncVal>>) -> Result<ArrValue> {
 	if arr.len() <= 1 {
 		return Ok(arr);
 	}
-	Ok(ArrValue::Eager(sort::sort(arr.evaluated()?, &keyF)?))
+	Ok(ArrValue::Eager(sort::sort(
+		arr.evaluated()?,
+		keyF.as_deref(),
+	)?))
 }
 
 #[jrsonnet_macros::builtin]
@@ -443,7 +441,7 @@
 
 #[jrsonnet_macros::builtin]
 fn builtin_char(n: u32) -> Result<char> {
-	Ok(std::char::from_u32(n as u32).ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?)
+	Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)
 }
 
 #[jrsonnet_macros::builtin]
@@ -466,16 +464,18 @@
 }
 
 #[jrsonnet_macros::builtin]
-fn builtin_trace(#[location] loc: &ExprLocation, str: IStr, rest: Any) -> Result<Any> {
+fn builtin_trace(#[location] loc: Option<&ExprLocation>, str: IStr, rest: Any) -> Result<Any> {
 	eprint!("TRACE:");
-	with_state(|s| {
-		let locs = s.map_source_locations(&loc.0, &[loc.1]);
-		eprint!(
-			" {}:{}",
-			loc.0.file_name().unwrap().to_str().unwrap(),
-			locs[0].line
-		);
-	});
+	if let Some(loc) = loc {
+		with_state(|s| {
+			let locs = s.map_source_locations(&loc.0, &[loc.1]);
+			eprint!(
+				" {}:{}",
+				loc.0.file_name().unwrap().to_str().unwrap(),
+				locs[0].line
+			);
+		});
+	}
 	eprintln!(" {}", str);
 	Ok(rest) as Result<Any>
 }
@@ -574,15 +574,19 @@
 #[jrsonnet_macros::builtin]
 fn builtin_manifest_yaml_doc(
 	value: Any,
-	indent_array_in_object: bool,
-	quote_keys: bool,
+	indent_array_in_object: Option<bool>,
+	quote_keys: Option<bool>,
 ) -> Result<String> {
 	manifest_yaml_ex(
 		&value.0,
 		&ManifestYamlOptions {
 			padding: "  ",
-			arr_element_padding: if indent_array_in_object { "  " } else { "" },
-			quote_keys,
+			arr_element_padding: if indent_array_in_object.unwrap_or(false) {
+				"  "
+			} else {
+				""
+			},
+			quote_keys: quote_keys.unwrap_or(true),
 		},
 	)
 }
@@ -648,15 +652,4 @@
 		}
 	}
 	Ok(count)
-}
-
-pub fn call_builtin(
-	context: Context,
-	loc: &ExprLocation,
-	name: &str,
-	args: &ArgsDesc,
-) -> Result<Val> {
-	BUILTINS
-		.with(|builtins| builtins.get(name).copied())
-		.ok_or_else(|| IntrinsicNotFound(name.into()))?(context, loc, args)
 }
modifiedcrates/jrsonnet-evaluator/src/builtin/sort.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/sort.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/sort.rs
@@ -1,6 +1,8 @@
 use crate::{
 	error::{Error, LocError, Result},
-	throw, FuncVal, Val,
+	throw,
+	typed::Any,
+	FuncVal, Val,
 };
 use gcmodule::{Cc, Trace};
 
@@ -59,42 +61,47 @@
 	Ok(sort_type)
 }
 
-pub fn sort(values: Cc<Vec<Val>>, key_getter: &FuncVal) -> Result<Cc<Vec<Val>>> {
+pub fn sort(values: Cc<Vec<Val>>, key_getter: Option<&FuncVal>) -> Result<Cc<Vec<Val>>> {
 	if values.len() <= 1 {
 		return Ok(values);
 	}
-	if key_getter.is_ident() {
-		let mut mvalues = (*values).clone();
-		let sort_type = get_sort_type(&mut mvalues, |k| k)?;
+	if let Some(key_getter) = key_getter {
+		// Slow path, user provided key getter
+		let mut vk = Vec::with_capacity(values.len());
+		for value in values.iter() {
+			vk.push((
+				value.clone(),
+				key_getter.evaluate_simple(&[Any(value.clone())].as_slice())?,
+			));
+		}
+		let sort_type = get_sort_type(&mut vk, |v| &mut v.1)?;
 		match sort_type {
-			SortKeyType::Number => mvalues.sort_by_key(|v| match v {
-				Val::Num(n) => NonNaNf64(*n),
+			SortKeyType::Number => vk.sort_by_key(|v| match v.1 {
+				Val::Num(n) => NonNaNf64(n),
 				_ => unreachable!(),
 			}),
-			SortKeyType::String => mvalues.sort_by_key(|v| match v {
+			SortKeyType::String => vk.sort_by_key(|v| match &v.1 {
 				Val::Str(s) => s.clone(),
 				_ => unreachable!(),
 			}),
 			SortKeyType::Unknown => unreachable!(),
 		};
-		Ok(Cc::new(mvalues))
+		Ok(Cc::new(vk.into_iter().map(|v| v.0).collect()))
 	} else {
-		let mut vk = Vec::with_capacity(values.len());
-		for value in values.iter() {
-			vk.push((value.clone(), key_getter.evaluate_values(&[value.clone()])?));
-		}
-		let sort_type = get_sort_type(&mut vk, |v| &mut v.1)?;
+		// Fast path, identity key getter
+		let mut mvalues = (*values).clone();
+		let sort_type = get_sort_type(&mut mvalues, |k| k)?;
 		match sort_type {
-			SortKeyType::Number => vk.sort_by_key(|v| match v.1 {
-				Val::Num(n) => NonNaNf64(n),
+			SortKeyType::Number => mvalues.sort_unstable_by_key(|v| match v {
+				Val::Num(n) => NonNaNf64(*n),
 				_ => unreachable!(),
 			}),
-			SortKeyType::String => vk.sort_by_key(|v| match &v.1 {
+			SortKeyType::String => mvalues.sort_unstable_by_key(|v| match v {
 				Val::Str(s) => s.clone(),
 				_ => unreachable!(),
 			}),
 			SortKeyType::Unknown => unreachable!(),
 		};
-		Ok(Cc::new(vk.into_iter().map(|v| v.0).collect()))
+		Ok(Cc::new(mvalues))
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -1,7 +1,7 @@
 use std::convert::TryFrom;
 
 use crate::{
-	builtin::std_slice,
+	builtin::{std_slice, BUILTINS},
 	error::Error::*,
 	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
 	gc::TraceBox,
@@ -192,7 +192,7 @@
 	Ok(match field_name {
 		jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),
 		jrsonnet_parser::FieldName::Dyn(expr) => push_frame(
-			&expr.1,
+			Some(&expr.1),
 			|| "evaluating field name".to_string(),
 			|| {
 				let value = evaluate(context, expr)?;
@@ -442,7 +442,7 @@
 	context: Context,
 	value: &LocExpr,
 	args: &ArgsDesc,
-	loc: &ExprLocation,
+	loc: Option<&ExprLocation>,
 	tailstrict: bool,
 ) -> Result<Val> {
 	let value = evaluate(context.clone(), value)?;
@@ -463,13 +463,13 @@
 	let value = &assertion.0;
 	let msg = &assertion.1;
 	let assertion_result = push_frame(
-		&value.1,
+		Some(&value.1),
 		|| "assertion condition".to_owned(),
 		|| bool::try_from(evaluate(context.clone(), value)?),
 	)?;
 	if !assertion_result {
 		push_frame(
-			&value.1,
+			Some(&value.1),
 			|| "assertion failure".to_owned(),
 			|| {
 				if let Some(msg) = msg {
@@ -519,7 +519,7 @@
 		BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,
 		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,
 		Var(name) => push_frame(
-			loc,
+			Some(loc),
 			|| format!("variable <{}> access", name),
 			|| context.binding(name.clone())?.evaluate(),
 		)?,
@@ -528,7 +528,7 @@
 				(Val::Obj(v), Val::Str(s)) => {
 					let sn = s.clone();
 					push_frame(
-						loc,
+						Some(loc),
 						|| format!("field <{}> access", sn),
 						|| {
 							if let Some(v) = v.get(s.clone())? {
@@ -624,17 +624,23 @@
 			&evaluate(context.clone(), s)?,
 			&Val::Obj(evaluate_object(context, t)?),
 		)?,
-		Apply(value, args, tailstrict) => evaluate_apply(context, value, args, loc, *tailstrict)?,
+		Apply(value, args, tailstrict) => {
+			evaluate_apply(context, value, args, Some(loc), *tailstrict)?
+		}
 		Function(params, body) => {
 			evaluate_method(context, "anonymous".into(), params.clone(), body.clone())
 		}
-		Intrinsic(name) => Val::Func(Cc::new(FuncVal::Intrinsic(name.clone()))),
+		Intrinsic(name) => Val::Func(Cc::new(FuncVal::StaticBuiltin(
+			BUILTINS
+				.with(|b| b.get(name).copied())
+				.ok_or_else(|| IntrinsicNotFound(name.clone()))?,
+		))),
 		AssertExpr(assert, returned) => {
 			evaluate_assert(context.clone(), assert)?;
 			evaluate(context, returned)?
 		}
 		ErrorStmt(e) => push_frame(
-			loc,
+			Some(loc),
 			|| "error statement".to_owned(),
 			|| throw!(RuntimeError(IStr::try_from(evaluate(context, e)?)?,)),
 		)?,
@@ -644,7 +650,7 @@
 			cond_else,
 		} => {
 			if push_frame(
-				loc,
+				Some(loc),
 				|| "if condition".to_owned(),
 				|| bool::try_from(evaluate(context.clone(), &cond.0)?),
 			)? {
@@ -683,7 +689,7 @@
 			let mut import_location = tmp.to_path_buf();
 			import_location.pop();
 			push_frame(
-				loc,
+				Some(loc),
 				|| format!("import {:?}", path),
 				|| with_state(|s| s.import_file(&import_location, path)),
 			)?
modifiedcrates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -1,15 +1,16 @@
 use crate::{
-	error::Error::*, evaluate, evaluate_named, gc::TraceBox, throw, Context, FutureWrapper,
-	GcHashMap, LazyVal, LazyValValue, Result, Val,
+	error::{Error::*, LocError},
+	evaluate, evaluate_named,
+	gc::TraceBox,
+	throw,
+	typed::Typed,
+	Context, FutureWrapper, GcHashMap, LazyVal, LazyValValue, Result, Val,
 };
 use gcmodule::Trace;
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ArgsDesc, LocExpr, ParamsDesc};
-use std::collections::HashMap;
+use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
+use std::{borrow::Cow, collections::HashMap, convert::TryFrom};
 
-const NO_DEFAULT_CONTEXT: &str =
-	"no default context set for call with defined default parameter value";
-
 #[derive(Trace)]
 struct EvaluateLazyVal {
 	context: Context,
@@ -21,6 +22,248 @@
 	}
 }
 
+pub trait ArgLike {
+	fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<LazyVal>;
+}
+impl ArgLike for &LocExpr {
+	fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<LazyVal> {
+		Ok(if tailstrict {
+			LazyVal::new_resolved(evaluate(ctx, self)?)
+		} else {
+			LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
+				context: ctx,
+				expr: (*self).clone(),
+			})))
+		})
+	}
+}
+impl<T> ArgLike for T
+where
+	T: Typed + Clone,
+	Val: TryFrom<T, Error = LocError>,
+{
+	fn evaluate_arg(&self, _ctx: Context, _tailstrict: bool) -> Result<LazyVal> {
+		let val: Val = Val::try_from(self.clone())?;
+		Ok(LazyVal::new_resolved(val))
+	}
+}
+pub enum TlaArg {
+	String(IStr),
+	Code(LocExpr),
+	Val(Val),
+}
+impl ArgLike for TlaArg {
+	fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<LazyVal> {
+		match self {
+			TlaArg::String(s) => Ok(LazyVal::new_resolved(Val::Str(s.clone()))),
+			TlaArg::Code(code) => Ok(if tailstrict {
+				LazyVal::new_resolved(evaluate(ctx, code)?)
+			} else {
+				LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
+					context: ctx,
+					expr: code.clone(),
+				})))
+			}),
+			TlaArg::Val(val) => Ok(LazyVal::new_resolved(val.clone())),
+		}
+	}
+}
+
+pub trait ArgsLike {
+	fn unnamed_len(&self) -> usize;
+	fn unnamed_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+	) -> Result<()>;
+	fn named_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+	) -> Result<()>;
+	fn named_names(&self, handler: &mut dyn FnMut(&IStr));
+}
+
+impl ArgsLike for ArgsDesc {
+	fn unnamed_len(&self) -> usize {
+		self.unnamed.len()
+	}
+
+	fn unnamed_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		for (id, arg) in self.unnamed.iter().enumerate() {
+			handler(
+				id,
+				if tailstrict {
+					LazyVal::new_resolved(evaluate(ctx.clone(), arg)?)
+				} else {
+					LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
+						context: ctx.clone(),
+						expr: arg.clone(),
+					})))
+				},
+			)?;
+		}
+		Ok(())
+	}
+
+	fn named_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		for (name, arg) in self.named.iter() {
+			handler(
+				name,
+				if tailstrict {
+					LazyVal::new_resolved(evaluate(ctx.clone(), arg)?)
+				} else {
+					LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
+						context: ctx.clone(),
+						expr: arg.clone(),
+					})))
+				},
+			)?;
+		}
+		Ok(())
+	}
+
+	fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {
+		for (name, _) in self.named.iter() {
+			handler(name)
+		}
+	}
+}
+
+impl<A: ArgLike> ArgsLike for [(IStr, A)] {
+	fn unnamed_len(&self) -> usize {
+		0
+	}
+
+	fn unnamed_iter(
+		&self,
+		_ctx: Context,
+		_tailstrict: bool,
+		_handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		Ok(())
+	}
+
+	fn named_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		for (name, val) in self.iter() {
+			handler(name, val.evaluate_arg(ctx.clone(), tailstrict)?)?;
+		}
+		Ok(())
+	}
+
+	fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {
+		for (name, _) in self.iter() {
+			handler(name);
+		}
+	}
+}
+
+impl<A: ArgLike> ArgsLike for HashMap<IStr, A> {
+	fn unnamed_len(&self) -> usize {
+		0
+	}
+
+	fn unnamed_iter(
+		&self,
+		_ctx: Context,
+		_tailstrict: bool,
+		_handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		Ok(())
+	}
+
+	fn named_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		for (name, value) in self.iter() {
+			handler(name, value.evaluate_arg(ctx.clone(), tailstrict)?)?;
+		}
+		Ok(())
+	}
+
+	fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {
+		for (name, _) in self.iter() {
+			handler(name);
+		}
+	}
+}
+
+impl<A: ArgLike> ArgsLike for [A] {
+	fn unnamed_len(&self) -> usize {
+		self.len()
+	}
+
+	fn unnamed_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		for (i, arg) in self.iter().enumerate() {
+			handler(i, arg.evaluate_arg(ctx.clone(), tailstrict)?)?;
+		}
+		Ok(())
+	}
+
+	fn named_iter(
+		&self,
+		_ctx: Context,
+		_tailstrict: bool,
+		_handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		Ok(())
+	}
+
+	fn named_names(&self, _handler: &mut dyn FnMut(&IStr)) {}
+}
+impl<A: ArgLike> ArgsLike for &[A] {
+	fn unnamed_len(&self) -> usize {
+		(*self).unnamed_len()
+	}
+
+	fn unnamed_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		(*self).unnamed_iter(ctx, tailstrict, handler)
+	}
+
+	fn named_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+	) -> Result<()> {
+		(*self).named_iter(ctx, tailstrict, handler)
+	}
+
+	fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {
+		(*self).named_names(handler)
+	}
+}
+
 /// Creates correct [context](Context) for function body evaluation returning error on invalid call.
 ///
 /// ## Parameters
@@ -33,55 +276,34 @@
 	ctx: Context,
 	body_ctx: Context,
 	params: &ParamsDesc,
-	args: &ArgsDesc,
+	args: &dyn ArgsLike,
 	tailstrict: bool,
 ) -> Result<Context> {
 	let mut passed_args = GcHashMap::with_capacity(params.len());
-	if args.unnamed.len() > params.len() {
+	if args.unnamed_len() > params.len() {
 		throw!(TooManyArgsFunctionHas(params.len()))
 	}
 
 	let mut filled_args = 0;
 
-	for (id, arg) in args.unnamed.iter().enumerate() {
+	args.unnamed_iter(ctx.clone(), tailstrict, &mut |id, arg| {
 		let name = params[id].0.clone();
-		passed_args.insert(
-			name,
-			if tailstrict {
-				LazyVal::new_resolved(evaluate(ctx.clone(), arg)?)
-			} else {
-				LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
-					context: ctx.clone(),
-					expr: arg.clone(),
-				})))
-			},
-		);
+		passed_args.insert(name, arg);
 		filled_args += 1;
-	}
+		Ok(())
+	})?;
 
-	for (name, value) in args.named.iter() {
+	args.named_iter(ctx, tailstrict, &mut |name, value| {
 		// FIXME: O(n) for arg existence check
 		if !params.iter().any(|p| &p.0 == name) {
 			throw!(UnknownFunctionParameter((name as &str).to_owned()));
 		}
-		if passed_args
-			.insert(
-				name.clone(),
-				if tailstrict {
-					LazyVal::new_resolved(evaluate(ctx.clone(), value)?)
-				} else {
-					LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
-						context: ctx.clone(),
-						expr: value.clone(),
-					})))
-				},
-			)
-			.is_some()
-		{
+		if passed_args.insert(name.clone(), value).is_some() {
 			throw!(BindingParameterASecondTime(name.clone()));
 		}
 		filled_args += 1;
-	}
+		Ok(())
+	})?;
 
 	if filled_args < params.len() {
 		// Some args are unset, but maybe we have defaults for them
@@ -123,8 +345,14 @@
 
 		// Some args still wasn't filled
 		if filled_args != params.len() {
-			for param in params.iter().skip(args.unnamed.len()) {
-				if !args.named.iter().any(|a| a.0 == param.0) {
+			for param in params.iter().skip(args.unnamed_len()) {
+				let mut found = false;
+				args.named_names(&mut |name| {
+					if name == &param.0 {
+						found = true;
+					}
+				});
+				if !found {
 					throw!(FunctionParameterNotBoundInCall(param.0.clone()));
 				}
 			}
@@ -141,12 +369,33 @@
 	}
 }
 
-#[derive(Clone, Copy)]
+type BuiltinParamName = Cow<'static, str>;
+
+#[derive(Clone)]
 pub struct BuiltinParam {
-	pub name: &'static str,
+	pub name: BuiltinParamName,
 	pub has_default: bool,
 }
 
+pub trait Builtin: Trace {
+	fn name(&self) -> &str;
+	fn params(&self) -> &[BuiltinParam];
+	fn call(
+		&self,
+		context: Context,
+		loc: Option<&ExprLocation>,
+		args: &dyn ArgsLike,
+	) -> Result<Val>;
+}
+
+pub trait StaticBuiltin: Builtin + Send + Sync
+where
+	Self: 'static,
+{
+	// In impl, to make it object safe:
+	// const INST: &'static Self;
+}
+
 /// You shouldn't probally use this function, use jrsonnet_macros::builtin instead
 ///
 /// ## Parameters
@@ -154,58 +403,38 @@
 /// * `params`: function parameters' definition
 /// * `args`: passed function arguments
 /// * `tailstrict`: if set to `true` function arguments are eagerly executed, otherwise - lazily
-pub fn parse_builtin_call<'k>(
+pub fn parse_builtin_call(
 	ctx: Context,
-	params: &'static [BuiltinParam],
-	args: &'k ArgsDesc,
+	params: &[BuiltinParam],
+	args: &dyn ArgsLike,
 	tailstrict: bool,
-) -> Result<GcHashMap<&'k str, LazyVal>> {
+) -> Result<GcHashMap<BuiltinParamName, LazyVal>> {
 	let mut passed_args = GcHashMap::with_capacity(params.len());
-	if args.unnamed.len() > params.len() {
+	if args.unnamed_len() > params.len() {
 		throw!(TooManyArgsFunctionHas(params.len()))
 	}
 
 	let mut filled_args = 0;
 
-	for (id, arg) in args.unnamed.iter().enumerate() {
-		let name = params[id].name;
-		passed_args.insert(
-			name,
-			if tailstrict {
-				LazyVal::new_resolved(evaluate(ctx.clone(), arg)?)
-			} else {
-				LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
-					context: ctx.clone(),
-					expr: arg.clone(),
-				})))
-			},
-		);
+	args.unnamed_iter(ctx.clone(), tailstrict, &mut |id, arg| {
+		let name = params[id].name.clone();
+		passed_args.insert(name, arg);
 		filled_args += 1;
-	}
+		Ok(())
+	})?;
 
-	for (name, value) in args.named.iter() {
+	args.named_iter(ctx, tailstrict, &mut |name, arg| {
 		// FIXME: O(n) for arg existence check
-		if !params.iter().any(|p| p.name == name as &str) {
-			throw!(UnknownFunctionParameter((name as &str).to_owned()));
-		}
-		if passed_args
-			.insert(
-				name,
-				if tailstrict {
-					LazyVal::new_resolved(evaluate(ctx.clone(), value)?)
-				} else {
-					LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
-						context: ctx.clone(),
-						expr: value.clone(),
-					})))
-				},
-			)
-			.is_some()
-		{
+		let p = params
+			.iter()
+			.find(|p| p.name == name as &str)
+			.ok_or_else(|| UnknownFunctionParameter((name as &str).to_owned()))?;
+		if passed_args.insert(p.name.clone(), arg).is_some() {
 			throw!(BindingParameterASecondTime(name.clone()));
 		}
 		filled_args += 1;
-	}
+		Ok(())
+	})?;
 
 	if filled_args < params.len() {
 		for param in params.iter().filter(|p| p.has_default) {
@@ -217,97 +446,19 @@
 
 		// Some args still wasn't filled
 		if filled_args != params.len() {
-			for param in params.iter().skip(args.unnamed.len()) {
-				if !args.named.iter().any(|a| &a.0 as &str == param.name) {
-					throw!(FunctionParameterNotBoundInCall(param.name.into()));
+			for param in params.iter().skip(args.unnamed_len()) {
+				let mut found = false;
+				args.named_names(&mut |name| {
+					if name as &str == &param.name as &str {
+						found = true;
+					}
+				});
+				if !found {
+					throw!(FunctionParameterNotBoundInCall(param.name.clone().into()));
 				}
 			}
 			unreachable!();
 		}
 	}
 	Ok(passed_args)
-}
-
-pub fn parse_function_call_map(
-	ctx: Context,
-	body_ctx: Option<Context>,
-	params: &ParamsDesc,
-	args: &HashMap<IStr, Val>,
-	tailstrict: bool,
-) -> Result<Context> {
-	let mut out = GcHashMap::with_capacity(params.len());
-	let mut positioned_args = vec![None; params.0.len()];
-	for (name, val) in args.iter() {
-		let idx = params
-			.iter()
-			.position(|p| *p.0 == **name)
-			.ok_or_else(|| UnknownFunctionParameter((name as &str).to_owned()))?;
-
-		if idx >= params.len() {
-			throw!(TooManyArgsFunctionHas(params.len()));
-		}
-		if positioned_args[idx].is_some() {
-			throw!(BindingParameterASecondTime(params[idx].0.clone()));
-		}
-		positioned_args[idx] = Some(val.clone());
-	}
-	// Fill defaults
-	for (id, p) in params.iter().enumerate() {
-		let val = if let Some(arg) = positioned_args[id].take() {
-			LazyVal::new_resolved(arg)
-		} else if let Some(default) = &p.1 {
-			if tailstrict {
-				LazyVal::new_resolved(evaluate(
-					body_ctx.clone().expect(NO_DEFAULT_CONTEXT),
-					default,
-				)?)
-			} else {
-				let body_ctx = body_ctx.clone();
-				let default = default.clone();
-				#[derive(Trace)]
-				struct EvaluateLazyVal {
-					body_ctx: Option<Context>,
-					default: LocExpr,
-				}
-				impl LazyValValue for EvaluateLazyVal {
-					fn get(self: Box<Self>) -> Result<Val> {
-						evaluate(
-							self.body_ctx.clone().expect(NO_DEFAULT_CONTEXT),
-							&self.default,
-						)
-					}
-				}
-				LazyVal::new(TraceBox(Box::new(EvaluateLazyVal { body_ctx, default })))
-			}
-		} else {
-			throw!(FunctionParameterNotBoundInCall(p.0.clone()));
-		};
-		out.insert(p.0.clone(), val);
-	}
-
-	Ok(body_ctx.unwrap_or(ctx).extend(out, None, None, None))
-}
-
-pub fn place_args(body_ctx: Context, params: &ParamsDesc, args: &[Val]) -> Result<Context> {
-	let mut out = GcHashMap::with_capacity(params.len());
-	let mut positioned_args = vec![None; params.0.len()];
-	for (id, arg) in args.iter().enumerate() {
-		if id >= params.len() {
-			throw!(TooManyArgsFunctionHas(params.len()));
-		}
-		positioned_args[id] = Some(arg);
-	}
-	// Fill defaults
-	for (id, p) in params.iter().enumerate() {
-		let val = if let Some(arg) = &positioned_args[id] {
-			(*arg).clone()
-		} else if let Some(default) = &p.1 {
-			evaluate(body_ctx.clone(), default)?
-		} else {
-			throw!(FunctionParameterNotBoundInCall(p.0.clone()));
-		};
-		out.insert(p.0.clone(), LazyVal::new_resolved(val));
-	}
-
-	Ok(body_ctx.extend(out, None, None, None))
 }
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -25,6 +25,7 @@
 use error::{Error::*, LocError, Result, StackTraceElement};
 pub use evaluate::*;
 pub use function::parse_function_call;
+use function::TlaArg;
 use gc::{GcHashMap, TraceBox};
 use gcmodule::{Cc, Trace};
 pub use import::*;
@@ -77,7 +78,7 @@
 	/// Used for ext.native
 	pub ext_natives: HashMap<IStr, Cc<NativeCallback>>,
 	/// TLA vars
-	pub tla_vars: HashMap<IStr, Val>,
+	pub tla_vars: HashMap<IStr, TlaArg>,
 	/// Global variables are inserted in default context
 	pub globals: HashMap<IStr, Val>,
 	/// Used to resolve file locations/contents
@@ -174,7 +175,7 @@
 	EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))
 }
 pub(crate) fn push_frame<T>(
-	e: &ExprLocation,
+	e: Option<&ExprLocation>,
 	frame_desc: impl FnOnce() -> String,
 	f: impl FnOnce() -> Result<T>,
 ) -> Result<T> {
@@ -203,24 +204,21 @@
 
 impl EvaluationState {
 	/// Parses and adds file as loaded
-	pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<()> {
-		self.add_parsed_file(
-			path.clone(),
-			source_code.clone(),
-			parse(
-				&source_code,
-				&ParserSettings {
-					file_name: path.clone(),
-				},
-			)
-			.map_err(|error| ImportSyntaxError {
-				error: Box::new(error),
-				path: path.to_owned(),
-				source_code,
-			})?,
-		)?;
+	pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<LocExpr> {
+		let parsed = parse(
+			&source_code,
+			&ParserSettings {
+				file_name: path.clone(),
+			},
+		)
+		.map_err(|error| ImportSyntaxError {
+			error: Box::new(error),
+			path: path.to_owned(),
+			source_code: source_code.clone(),
+		})?;
+		self.add_parsed_file(path, source_code, parsed.clone())?;
 
-		Ok(())
+		Ok(parsed)
 	}
 
 	pub fn reset_evaluation_state(&self, name: &Path) {
@@ -341,7 +339,7 @@
 	/// Executes code creating a new stack frame
 	pub fn push<T>(
 		&self,
-		e: &ExprLocation,
+		e: Option<&ExprLocation>,
 		frame_desc: impl FnOnce() -> String,
 		f: impl FnOnce() -> Result<T>,
 	) -> Result<T> {
@@ -364,7 +362,7 @@
 		}
 		if let Err(mut err) = result {
 			err.trace_mut().0.push(StackTraceElement {
-				location: Some(e.clone()),
+				location: e.cloned(),
 				desc: frame_desc(),
 			});
 			return Err(err);
@@ -506,8 +504,9 @@
 				Val::Func(func) => push_description_frame(
 					|| "during TLA call".to_owned(),
 					|| {
-						func.evaluate_map(
+						func.evaluate(
 							self.create_default_context(),
+							None,
 							&self.settings().tla_vars,
 							true,
 						)
@@ -581,15 +580,20 @@
 	}
 
 	pub fn add_tla(&self, name: IStr, value: Val) {
-		self.settings_mut().tla_vars.insert(name, value);
+		self.settings_mut()
+			.tla_vars
+			.insert(name, TlaArg::Val(value));
 	}
 	pub fn add_tla_str(&self, name: IStr, value: IStr) {
-		self.add_tla(name, Val::Str(value));
+		self.settings_mut()
+			.tla_vars
+			.insert(name, TlaArg::String(value));
 	}
 	pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {
-		let value =
-			self.evaluate_snippet_raw(PathBuf::from(format!("tla_code {}", name)).into(), code)?;
-		self.add_tla(name, value);
+		let parsed = self.add_file(PathBuf::from(format!("tla_code {}", name)).into(), code)?;
+		self.settings_mut()
+			.tla_vars
+			.insert(name, TlaArg::Code(parsed));
 		Ok(())
 	}
 
@@ -668,11 +672,11 @@
 		state.run_in_state(|| {
 			state
 				.push(
-					&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20),
+					Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),
 					|| "outer".to_owned(),
 					|| {
 						state.push(
-							&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40),
+							Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),
 							|| "inner".to_owned(),
 							|| Err(RuntimeError("".into()).into()),
 						)?;
@@ -975,15 +979,8 @@
 	fn parse_json() {
 		assert_json!(
 			r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,
-			r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#
-		);
-		// TODO: this should in fact fail as is no proper JSON syntax
-		assert_json!(
-			r#"std.parseJson("{a:-1, b:1, c:3.141, d:[]}")"#,
 			r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#
 		);
-		// TODO: this is also no valid JSON
-		assert_json!(r#"std.parseJson('local x = 2; x * x')"#, r#"4"#);
 	}
 
 	#[test]
modifiedcrates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -8,6 +8,7 @@
 use std::path::Path;
 use std::rc::Rc;
 
+#[deprecated(note = "Use builtins instead")]
 pub trait NativeCallbackHandler: Trace {
 	fn call(&self, from: Rc<Path>, args: &[Val]) -> Result<Val>;
 }
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -37,7 +37,7 @@
 								.into()
 							))
 						}
-						Ok(n as $ty)
+						Ok(n as Self)
 					}
 					_ => unreachable!(),
 				}
@@ -249,6 +249,7 @@
 
 /// To be used in Vec<Any>
 /// Regular Val can't be used here, because it has wrong TryFrom::Error type
+#[derive(Clone)]
 pub struct Any(pub Val);
 
 impl Typed for Any {
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -1,24 +1,20 @@
 use crate::{
-	builtin::{
-		call_builtin,
-		manifest::{
-			manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType,
-			ManifestYamlOptions,
-		},
+	builtin::manifest::{
+		manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,
 	},
 	cc_ptr_eq,
 	error::{Error::*, LocError},
 	evaluate,
-	function::{parse_function_call, parse_function_call_map, place_args},
+	function::{parse_function_call, ArgsLike, Builtin, StaticBuiltin},
 	gc::TraceBox,
 	native::NativeCallback,
 	throw, Context, ObjValue, Result,
 };
 use gcmodule::{Cc, Trace};
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
+use jrsonnet_parser::{ExprLocation, LocExpr, ParamsDesc};
 use jrsonnet_types::ValType;
-use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
+use std::{cell::RefCell, fmt::Debug, rc::Rc};
 
 pub trait LazyValValue: Trace {
 	fn get(self: Box<Self>) -> Result<Val>;
@@ -41,6 +37,10 @@
 	pub fn new_resolved(val: Val) -> Self {
 		Self(Cc::new(RefCell::new(LazyValInternals::Computed(val))))
 	}
+	pub fn force(&self) -> Result<()> {
+		self.evaluate()?;
+		Ok(())
+	}
 	pub fn evaluate(&self) -> Result<Val> {
 		match &*self.0.borrow() {
 			LazyValInternals::Computed(v) => return Ok(v.clone()),
@@ -86,42 +86,63 @@
 	pub body: LocExpr,
 }
 
-#[derive(Debug, Trace)]
+#[derive(Trace)]
 pub enum FuncVal {
 	/// Plain function implemented in jsonnet
 	Normal(FuncDesc),
 	/// Standard library function
-	Intrinsic(IStr),
+	StaticBuiltin(#[skip_trace] &'static dyn StaticBuiltin),
+
+	Builtin(TraceBox<dyn Builtin>),
 	/// Library functions implemented in native
 	NativeExt(IStr, Cc<NativeCallback>),
 }
 
+impl Debug for FuncVal {
+	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+		match self {
+			Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),
+			Self::StaticBuiltin(arg0) => f.debug_tuple("Intrinsic").field(&arg0.name()).finish(),
+			Self::Builtin(arg0) => f.debug_tuple("Intrinsic").field(&arg0.name()).finish(),
+			Self::NativeExt(arg0, arg1) => {
+				f.debug_tuple("NativeExt").field(arg0).field(arg1).finish()
+			}
+		}
+	}
+}
+
 impl PartialEq for FuncVal {
 	fn eq(&self, other: &Self) -> bool {
 		match (self, other) {
 			(Self::Normal(a), Self::Normal(b)) => a == b,
-			(Self::Intrinsic(an), Self::Intrinsic(bn)) => an == bn,
+			(Self::StaticBuiltin(an), Self::StaticBuiltin(bn)) => std::ptr::eq(*an, *bn),
 			(Self::NativeExt(an, _), Self::NativeExt(bn, _)) => an == bn,
 			(..) => false,
 		}
 	}
 }
 impl FuncVal {
-	pub fn is_ident(&self) -> bool {
-		matches!(&self, Self::Intrinsic(n) if n as &str == "id")
+	pub fn args_len(&self) -> usize {
+		match self {
+			Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),
+			Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default).count(),
+			Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default).count(),
+			Self::NativeExt(_, n) => n.params.iter().filter(|p| p.1.is_none()).count(),
+		}
 	}
 	pub fn name(&self) -> IStr {
 		match self {
 			Self::Normal(normal) => normal.name.clone(),
-			Self::Intrinsic(name) => format!("std.{}", name).into(),
+			Self::StaticBuiltin(builtin) => builtin.name().into(),
+			Self::Builtin(builtin) => builtin.name().into(),
 			Self::NativeExt(n, _) => format!("native.{}", n).into(),
 		}
 	}
 	pub fn evaluate(
 		&self,
 		call_ctx: Context,
-		loc: &ExprLocation,
-		args: &ArgsDesc,
+		loc: Option<&ExprLocation>,
+		args: &dyn ArgsLike,
 		tailstrict: bool,
 	) -> Result<Val> {
 		match self {
@@ -135,7 +156,8 @@
 				)?;
 				evaluate(ctx, &func.body)
 			}
-			Self::Intrinsic(name) => call_builtin(call_ctx, loc, name, args),
+			Self::StaticBuiltin(name) => name.call(call_ctx, loc, args),
+			Self::Builtin(b) => b.call(call_ctx, loc, args),
 			Self::NativeExt(_name, handler) => {
 				let args =
 					parse_function_call(call_ctx, Context::new(), &handler.params, args, true)?;
@@ -143,42 +165,12 @@
 				for p in handler.params.0.iter() {
 					out_args.push(args.binding(p.0.clone())?.evaluate()?);
 				}
-				Ok(handler.call(loc.0.clone(), &out_args)?)
-			}
-		}
-	}
-
-	pub fn evaluate_map(
-		&self,
-		call_ctx: Context,
-		args: &HashMap<IStr, Val>,
-		tailstrict: bool,
-	) -> Result<Val> {
-		match self {
-			Self::Normal(func) => {
-				let ctx = parse_function_call_map(
-					call_ctx,
-					Some(func.ctx.clone()),
-					&func.params,
-					args,
-					tailstrict,
-				)?;
-				evaluate(ctx, &func.body)
+				Ok(handler.call(loc.expect("todo").0.clone(), &out_args)?)
 			}
-			Self::Intrinsic(_) => todo!(),
-			Self::NativeExt(_, _) => todo!(),
 		}
 	}
-
-	pub fn evaluate_values(&self, args: &[Val]) -> Result<Val> {
-		match self {
-			Self::Normal(func) => {
-				let ctx = place_args(func.ctx.clone(), &func.params, args)?;
-				evaluate(ctx, &func.body)
-			}
-			Self::Intrinsic(_) => todo!(),
-			Self::NativeExt(_, _) => todo!(),
-		}
+	pub fn evaluate_simple(&self, args: &dyn ArgsLike) -> Result<Val> {
+		self.evaluate(Context::default(), None, args, true)
 	}
 }
 
modifiedcrates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -2,6 +2,7 @@
 use rustc_hash::FxHashMap;
 use serde::{Deserialize, Serialize};
 use std::{
+	borrow::Cow,
 	cell::RefCell,
 	fmt::{self, Display},
 	hash::{BuildHasherDefault, Hash, Hasher},
@@ -90,6 +91,12 @@
 	}
 }
 
+impl<'i> From<Cow<'i, str>> for IStr {
+	fn from(c: Cow<'i, str>) -> Self {
+		(&c as &str).into()
+	}
+}
+
 impl Serialize for IStr {
 	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
 	where
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -1,11 +1,50 @@
-use proc_macro2::Span;
 use quote::quote;
-use syn::{parse_macro_input, FnArg, Ident, ItemFn, Pat, PatType};
+use syn::{
+	parse_macro_input, FnArg, GenericArgument, ItemFn, Pat, PatType, Path, PathArguments, Type,
+};
 
 fn is_location_arg(t: &PatType) -> bool {
 	t.attrs.iter().any(|a| a.path.is_ident("location"))
 }
 
+trait RetainHad<T> {
+	fn retain_had(&mut self, h: impl FnMut(&T) -> bool) -> bool;
+}
+impl<T> RetainHad<T> for Vec<T> {
+	fn retain_had(&mut self, h: impl FnMut(&T) -> bool) -> bool {
+		let before = self.len();
+		self.retain(h);
+		let after = self.len();
+		before != after
+	}
+}
+
+fn extract_type_from_option(ty: &Type) -> Option<&Type> {
+	fn path_is_option(path: &Path) -> bool {
+		path.leading_colon.is_none()
+			&& path.segments.len() == 1
+			&& path.segments.iter().next().unwrap().ident == "Option"
+	}
+
+	match ty {
+		Type::Path(typepath) if typepath.qself.is_none() && path_is_option(&typepath.path) => {
+			// Get the first segment of the path (there is only one, in fact: "Option"):
+			let type_params = &typepath.path.segments.iter().next().unwrap().arguments;
+			// It should have only on angle-bracketed param ("<String>"):
+			let generic_arg = match type_params {
+				PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),
+				_ => panic!("missing option generic"),
+			};
+			// This argument must be a type:
+			match generic_arg {
+				GenericArgument::Type(ty) => Some(ty),
+				_ => panic!("option generic should be a type"),
+			}
+		}
+		_ => None,
+	}
+}
+
 #[proc_macro_attribute]
 pub fn builtin(
 	_attr: proc_macro::TokenStream,
@@ -33,11 +72,10 @@
 				Pat::Ident(i) => i.ident.to_string(),
 				_ => panic!("only idents supported yet"),
 			};
-			// TODO: Check if ty == Option<_>
-			let optional = false;
+			let optional = extract_type_from_option(&t.ty).is_some();
 			quote! {
 				BuiltinParam {
-					name: #ident,
+					name: std::borrow::Cow::Borrowed(#ident),
 					has_default: #optional,
 				}
 			}
@@ -53,10 +91,7 @@
 			FnArg::Typed(t) => t,
 		})
 		.map(|t| {
-			let count_before = t.attrs.len();
-			t.attrs.retain(|a| !a.path.is_ident("location"));
-			let count_after = t.attrs.len();
-			let is_location = count_before != count_after;
+			let is_location = t.attrs.retain_had(|a| !a.path.is_ident("location"));
 			if is_location {
 				quote! {{
 					loc
@@ -67,38 +102,68 @@
 					_ => panic!("only idents supported yet"),
 				};
 				let ty = &t.ty;
-				quote! {{
-					let value = parsed.get(#ident).unwrap();
+				if let Some(opt_ty) = extract_type_from_option(&t.ty) {
+					quote! {{
+						if let Some(value) = parsed.get(#ident) {
+							Some(jrsonnet_evaluator::push_description_frame(
+								|| format!("argument <{}> evaluation", #ident),
+								|| <#opt_ty>::try_from(value.evaluate()?),
+							)?)
+						} else {
+							None
+						}
+					}}
+				} else {
+					quote! {{
+						let value = parsed.get(#ident).unwrap();
 
-					jrsonnet_evaluator::push_description_frame(
-						|| format!("argument <{}> evaluation", #ident),
-						|| <#ty>::try_from(value.evaluate()?),
-					)?
-				}}
+						jrsonnet_evaluator::push_description_frame(
+							|| format!("argument <{}> evaluation", #ident),
+							|| <#ty>::try_from(value.evaluate()?),
+						)?
+					}}
+				}
 			}
-		}).collect::<Vec<_>>();
-	
-	let inner_name = Ident::new("inner", Span::call_site());
-	let mut inner_fun = fun.clone();
-	inner_fun.sig.ident = inner_name.clone();
+		})
+		.collect::<Vec<_>>();
 
-	let attrs = &fun.attrs;
+	let name = &fun.sig.ident;
 	let vis = &fun.vis;
-	let name = &fun.sig.ident;
 	(quote! {
-		#(#attrs)*
-		#vis fn #name(context: Context, loc: &ExprLocation, args: &ArgsDesc) -> Result<Val> {
-			#inner_fun
-			use jrsonnet_evaluator::function::BuiltinParam;
+		#fun
+		#[doc(hidden)]
+		#[allow(non_camel_case_types)]
+		#[derive(Clone, Copy, gcmodule::Trace)]
+		#vis struct #name {}
+		const _: () = {
+			use jrsonnet_evaluator::function::{Builtin, StaticBuiltin, BuiltinParam, ArgsLike};
 			const PARAMS: &'static [BuiltinParam] = &[
 				#(#params),*
 			];
-			let parsed = jrsonnet_evaluator::function::parse_builtin_call(context, &PARAMS, args, false)?;
 
-			let result: #result = #inner_name(#(#args),*);
-			let result = result?;
-			result.try_into()
-		}
+			impl #name {
+				pub const INST: &'static dyn StaticBuiltin = &#name {};
+			}
+			impl StaticBuiltin for #name {}
+			impl Builtin for #name
+			where
+				Self: 'static
+			{
+				fn name(&self) -> &str {
+					stringify!(#name)
+				}
+				fn params(&self) -> &[BuiltinParam] {
+					PARAMS
+				}
+				fn call(&self, context: Context, loc: Option<&ExprLocation>, args: &dyn ArgsLike) -> Result<Val> {
+					let parsed = jrsonnet_evaluator::function::parse_builtin_call(context, &PARAMS, args, false)?;
+
+					let result: #result = #name(#(#args),*);
+					let result = result?;
+					result.try_into()
+				}
+			}
+		};
 	})
 	.into()
 }
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-parser/src/lib.rs
1#![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 _() = single_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" / "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("import") _ path:string() {Expr::Import(PathBuf::from(path))}222223			/ var_expr(s)224			/ local_expr(s)225			/ if_then_else_expr(s)226227			/ keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}228			/ assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }229230			/ keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }231232		rule slice_part(s: &ParserSettings) -> Option<LocExpr>233			= e:(_ e:expr(s) _{e})? {e}234		pub rule slice_desc(s: &ParserSettings) -> SliceDesc235			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {236				let (end, step) = if let Some((end, step)) = pair {237					(end, step)238				}else{239					(None, None)240				};241242				SliceDesc { start, end, step }243			}244245		rule binop(x: rule<()>) -> ()246			= quiet!{ x() } / expected!("<binary op>")247		rule unaryop(x: rule<()>) -> ()248			= quiet!{ x() } / expected!("<unary op>")249250251		use BinaryOpType::*;252		use UnaryOpType::*;253		rule expr(s: &ParserSettings) -> LocExpr254			= precedence! {255				start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.file_name.clone(), start, end)) }256				--257				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}258				--259				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}260				--261				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}262				--263				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}264				--265				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}266				--267				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}268				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}269				--270				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}271				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}272				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}273				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}274				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}275				--276				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}277				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}278				--279				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}280				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}281				--282				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}283				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}284				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}285				--286						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}287						unaryop(<"!">) _ b:@ {expr_un!(Not b)}288						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}289				--290				a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}291				a:(@) _ "." _ a:position!() e:id_loc(s) b:position!() {Expr::Index(a, e)}292				a:(@) _ "[" _ e:expr(s) _ "]" {Expr::Index(a, e)}293				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}294				a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}295				--296				e:expr_basic(s) {e}297				"(" _ e:expr(s) _ ")" {Expr::Parened(e)}298			}299300		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}301	}302}303304pub type ParseError = peg::error::ParseError<peg::str::LineCol>;305pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {306	jsonnet_parser::jsonnet(str, settings)307}308309#[cfg(test)]310pub mod tests {311	use super::{expr::*, parse};312	use crate::ParserSettings;313	use std::path::PathBuf;314	use BinaryOpType::*;315316	macro_rules! parse {317		($s:expr) => {318			parse(319				$s,320				&ParserSettings {321					file_name: PathBuf::from("test.jsonnet").into(),322				},323			)324			.unwrap()325		};326	}327328	macro_rules! el {329		($expr:expr, $from:expr, $to:expr$(,)?) => {330			LocExpr(331				std::rc::Rc::new($expr),332				ExprLocation(PathBuf::from("test.jsonnet").into(), $from, $to),333			)334		};335	}336337	#[test]338	fn multiline_string() {339		assert_eq!(340			parse!("|||\n    Hello world!\n     a\n|||"),341			el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),342		);343		assert_eq!(344			parse!("|||\n  Hello world!\n   a\n|||"),345			el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),346		);347		assert_eq!(348			parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),349			el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),350		);351		assert_eq!(352			parse!("|||\n   Hello world!\n    a\n |||"),353			el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),354		);355	}356357	#[test]358	fn slice() {359		parse!("a[1:]");360		parse!("a[1::]");361		parse!("a[:1:]");362		parse!("a[::1]");363		parse!("str[:len - 1]");364	}365366	#[test]367	fn string_escaping() {368		assert_eq!(369			parse!(r#""Hello, \"world\"!""#),370			el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),371		);372		assert_eq!(373			parse!(r#"'Hello \'world\'!'"#),374			el!(Expr::Str("Hello 'world'!".into()), 0, 18),375		);376		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));377	}378379	#[test]380	fn string_unescaping() {381		assert_eq!(382			parse!(r#""Hello\nWorld""#),383			el!(Expr::Str("Hello\nWorld".into()), 0, 14),384		);385	}386387	#[test]388	fn string_verbantim() {389		assert_eq!(390			parse!(r#"@"Hello\n""World""""#),391			el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),392		);393	}394395	#[test]396	fn imports() {397		assert_eq!(398			parse!("import \"hello\""),399			el!(Expr::Import(PathBuf::from("hello")), 0, 14),400		);401		assert_eq!(402			parse!("importstr \"garnish.txt\""),403			el!(Expr::ImportStr(PathBuf::from("garnish.txt")), 0, 23)404		);405	}406407	#[test]408	fn empty_object() {409		assert_eq!(410			parse!("{}"),411			el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)412		);413	}414415	#[test]416	fn basic_math() {417		assert_eq!(418			parse!("2+2*2"),419			el!(420				Expr::BinaryOp(421					el!(Expr::Num(2.0), 0, 1),422					Add,423					el!(424						Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),425						2,426						5427					)428				),429				0,430				5431			)432		);433	}434435	#[test]436	fn basic_math_with_indents() {437		assert_eq!(438			parse!("2	+ 	  2	  *	2   	"),439			el!(440				Expr::BinaryOp(441					el!(Expr::Num(2.0), 0, 1),442					Add,443					el!(444						Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),445						7,446						14447					),448				),449				0,450				14451			)452		);453	}454455	#[test]456	fn basic_math_parened() {457		assert_eq!(458			parse!("2+(2+2*2)"),459			el!(460				Expr::BinaryOp(461					el!(Expr::Num(2.0), 0, 1),462					Add,463					el!(464						Expr::Parened(el!(465							Expr::BinaryOp(466								el!(Expr::Num(2.0), 3, 4),467								Add,468								el!(469									Expr::BinaryOp(470										el!(Expr::Num(2.0), 5, 6),471										Mul,472										el!(Expr::Num(2.0), 7, 8),473									),474									5,475									8476								),477							),478							3,479							8480						)),481						2,482						9483					),484				),485				0,486				9487			)488		);489	}490491	/// Comments should not affect parsing492	#[test]493	fn comments() {494		assert_eq!(495			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),496			el!(497				Expr::BinaryOp(498					el!(Expr::Num(2.0), 0, 1),499					Add,500					el!(501						Expr::BinaryOp(502							el!(Expr::Num(3.0), 22, 23),503							Mul,504							el!(Expr::Num(4.0), 40, 41)505						),506						22,507						41508					)509				),510				0,511				41512			)513		);514	}515516	/// Comments should be able to be escaped517	#[test]518	fn comment_escaping() {519		assert_eq!(520			parse!("2/*\\*/+*/ - 22"),521			el!(522				Expr::BinaryOp(el!(Expr::Num(2.0), 0, 1), Sub, el!(Expr::Num(22.0), 12, 14)),523				0,524				14525			)526		);527	}528529	#[test]530	fn suffix() {531		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));532		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));533		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));534		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))535	}536537	#[test]538	fn array_comp() {539		use Expr::*;540		/*541		`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`,542		`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`543				*/544		assert_eq!(545			parse!("[std.deepJoin(x) for x in arr]"),546			el!(547				ArrComp(548					el!(549						Apply(550							el!(551								Index(552									el!(Var("std".into()), 1, 4),553									el!(Str("deepJoin".into()), 5, 13)554								),555								1,556								13557							),558							ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),559							false,560						),561						1,562						16563					),564					vec![CompSpec::ForSpec(ForSpecData(565						"x".into(),566						el!(Var("arr".into()), 26, 29)567					))]568				),569				0,570				30571			),572		)573	}574575	#[test]576	fn reserved() {577		use Expr::*;578		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));579		assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));580	}581582	#[test]583	fn multiple_args_buf() {584		parse!("a(b, null_fields)");585	}586587	#[test]588	fn infix_precedence() {589		use Expr::*;590		assert_eq!(591			parse!("!a && !b"),592			el!(593				BinaryOp(594					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),595					And,596					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)597				),598				0,599				8600			)601		);602	}603604	#[test]605	fn infix_precedence_division() {606		use Expr::*;607		assert_eq!(608			parse!("!a / !b"),609			el!(610				BinaryOp(611					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),612					Div,613					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)614				),615				0,616				7617			)618		);619	}620621	#[test]622	fn double_negation() {623		use Expr::*;624		assert_eq!(625			parse!("!!a"),626			el!(627				UnaryOp(628					UnaryOpType::Not,629					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)630				),631				0,632				3633			)634		)635	}636637	#[test]638	fn array_test_error() {639		parse!("[a for a in b if c for e in f]");640		//                    ^^^^ failed code641	}642643	#[test]644	fn missing_newline_between_comment_and_eof() {645		parse!(646			"{a:1}647648			//+213"649		);650	}651652	#[test]653	fn default_param_before_nondefault() {654		parse!("local x(foo = 'foo', bar) = null; null");655	}656657	#[test]658	fn can_parse_stdlib() {659		parse!(jrsonnet_stdlib::STDLIB_STR);660	}661662	#[test]663	fn add_location_info_to_all_sub_expressions() {664		use Expr::*;665666		let file_name: std::rc::Rc<std::path::Path> = PathBuf::from("test.jsonnet").into();667		let expr = parse(668			"{} { local x = 1, x: x } + {}",669			&ParserSettings {670				file_name: file_name.clone(),671			},672		)673		.unwrap();674		assert_eq!(675			expr,676			el!(677				BinaryOp(678					el!(679						ObjExtend(680							el!(Obj(ObjBody::MemberList(vec![])), 0, 2),681							ObjBody::MemberList(vec![682								Member::BindStmt(BindSpec {683									name: "x".into(),684									params: None,685									value: el!(Num(1.0), 15, 16)686								}),687								Member::Field(FieldMember {688									name: FieldName::Fixed("x".into()),689									plus: false,690									params: None,691									visibility: Visibility::Normal,692									value: el!(Var("x".into()), 21, 22),693								})694							])695						),696						0,697						24698					),699					BinaryOpType::Add,700					el!(Obj(ObjBody::MemberList(vec![])), 27, 29),701				),702				0,703				29704			),705		);706	}707	// From source code708	/*709	#[bench]710	fn bench_parse_peg(b: &mut Bencher) {711		b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))712	}713	*/714}
after · crates/jrsonnet-parser/src/lib.rs
1#![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" / "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("import") _ path:string() {Expr::Import(PathBuf::from(path))}222223			/ var_expr(s)224			/ local_expr(s)225			/ if_then_else_expr(s)226227			/ keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}228			/ assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }229230			/ keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }231232		rule slice_part(s: &ParserSettings) -> Option<LocExpr>233			= e:(_ e:expr(s) _{e})? {e}234		pub rule slice_desc(s: &ParserSettings) -> SliceDesc235			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {236				let (end, step) = if let Some((end, step)) = pair {237					(end, step)238				}else{239					(None, None)240				};241242				SliceDesc { start, end, step }243			}244245		rule binop(x: rule<()>) -> ()246			= quiet!{ x() } / expected!("<binary op>")247		rule unaryop(x: rule<()>) -> ()248			= quiet!{ x() } / expected!("<unary op>")249250251		use BinaryOpType::*;252		use UnaryOpType::*;253		rule expr(s: &ParserSettings) -> LocExpr254			= precedence! {255				start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.file_name.clone(), start, end)) }256				--257				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}258				--259				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}260				--261				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}262				--263				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}264				--265				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}266				--267				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}268				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}269				--270				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}271				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}272				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}273				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}274				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}275				--276				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}277				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}278				--279				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}280				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}281				--282				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}283				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}284				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}285				--286						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}287						unaryop(<"!">) _ b:@ {expr_un!(Not b)}288						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}289				--290				a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}291				a:(@) _ "." _ a:position!() e:id_loc(s) b:position!() {Expr::Index(a, e)}292				a:(@) _ "[" _ e:expr(s) _ "]" {Expr::Index(a, e)}293				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}294				a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}295				--296				e:expr_basic(s) {e}297				"(" _ e:expr(s) _ ")" {Expr::Parened(e)}298			}299300		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}301	}302}303304pub type ParseError = peg::error::ParseError<peg::str::LineCol>;305pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {306	jsonnet_parser::jsonnet(str, settings)307}308/// Used for importstr values309pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {310	let len = str.len();311	LocExpr(312		Rc::new(Expr::Str(str)),313		ExprLocation(settings.file_name.clone(), 0, len),314	)315}316317#[cfg(test)]318pub mod tests {319	use super::{expr::*, parse};320	use crate::ParserSettings;321	use std::path::PathBuf;322	use BinaryOpType::*;323324	macro_rules! parse {325		($s:expr) => {326			parse(327				$s,328				&ParserSettings {329					file_name: PathBuf::from("test.jsonnet").into(),330				},331			)332			.unwrap()333		};334	}335336	macro_rules! el {337		($expr:expr, $from:expr, $to:expr$(,)?) => {338			LocExpr(339				std::rc::Rc::new($expr),340				ExprLocation(PathBuf::from("test.jsonnet").into(), $from, $to),341			)342		};343	}344345	#[test]346	fn multiline_string() {347		assert_eq!(348			parse!("|||\n    Hello world!\n     a\n|||"),349			el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),350		);351		assert_eq!(352			parse!("|||\n  Hello world!\n   a\n|||"),353			el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),354		);355		assert_eq!(356			parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),357			el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),358		);359		assert_eq!(360			parse!("|||\n   Hello world!\n    a\n |||"),361			el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),362		);363	}364365	#[test]366	fn slice() {367		parse!("a[1:]");368		parse!("a[1::]");369		parse!("a[:1:]");370		parse!("a[::1]");371		parse!("str[:len - 1]");372	}373374	#[test]375	fn string_escaping() {376		assert_eq!(377			parse!(r#""Hello, \"world\"!""#),378			el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),379		);380		assert_eq!(381			parse!(r#"'Hello \'world\'!'"#),382			el!(Expr::Str("Hello 'world'!".into()), 0, 18),383		);384		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));385	}386387	#[test]388	fn string_unescaping() {389		assert_eq!(390			parse!(r#""Hello\nWorld""#),391			el!(Expr::Str("Hello\nWorld".into()), 0, 14),392		);393	}394395	#[test]396	fn string_verbantim() {397		assert_eq!(398			parse!(r#"@"Hello\n""World""""#),399			el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),400		);401	}402403	#[test]404	fn imports() {405		assert_eq!(406			parse!("import \"hello\""),407			el!(Expr::Import(PathBuf::from("hello")), 0, 14),408		);409		assert_eq!(410			parse!("importstr \"garnish.txt\""),411			el!(Expr::ImportStr(PathBuf::from("garnish.txt")), 0, 23)412		);413	}414415	#[test]416	fn empty_object() {417		assert_eq!(418			parse!("{}"),419			el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)420		);421	}422423	#[test]424	fn basic_math() {425		assert_eq!(426			parse!("2+2*2"),427			el!(428				Expr::BinaryOp(429					el!(Expr::Num(2.0), 0, 1),430					Add,431					el!(432						Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),433						2,434						5435					)436				),437				0,438				5439			)440		);441	}442443	#[test]444	fn basic_math_with_indents() {445		assert_eq!(446			parse!("2	+ 	  2	  *	2   	"),447			el!(448				Expr::BinaryOp(449					el!(Expr::Num(2.0), 0, 1),450					Add,451					el!(452						Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),453						7,454						14455					),456				),457				0,458				14459			)460		);461	}462463	#[test]464	fn basic_math_parened() {465		assert_eq!(466			parse!("2+(2+2*2)"),467			el!(468				Expr::BinaryOp(469					el!(Expr::Num(2.0), 0, 1),470					Add,471					el!(472						Expr::Parened(el!(473							Expr::BinaryOp(474								el!(Expr::Num(2.0), 3, 4),475								Add,476								el!(477									Expr::BinaryOp(478										el!(Expr::Num(2.0), 5, 6),479										Mul,480										el!(Expr::Num(2.0), 7, 8),481									),482									5,483									8484								),485							),486							3,487							8488						)),489						2,490						9491					),492				),493				0,494				9495			)496		);497	}498499	/// Comments should not affect parsing500	#[test]501	fn comments() {502		assert_eq!(503			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),504			el!(505				Expr::BinaryOp(506					el!(Expr::Num(2.0), 0, 1),507					Add,508					el!(509						Expr::BinaryOp(510							el!(Expr::Num(3.0), 22, 23),511							Mul,512							el!(Expr::Num(4.0), 40, 41)513						),514						22,515						41516					)517				),518				0,519				41520			)521		);522	}523524	/// Comments should be able to be escaped525	#[test]526	fn comment_escaping() {527		assert_eq!(528			parse!("2/*\\*/+*/ - 22"),529			el!(530				Expr::BinaryOp(el!(Expr::Num(2.0), 0, 1), Sub, el!(Expr::Num(22.0), 12, 14)),531				0,532				14533			)534		);535	}536537	#[test]538	fn suffix() {539		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));540		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));541		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));542		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))543	}544545	#[test]546	fn array_comp() {547		use Expr::*;548		/*549		`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`,550		`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`551				*/552		assert_eq!(553			parse!("[std.deepJoin(x) for x in arr]"),554			el!(555				ArrComp(556					el!(557						Apply(558							el!(559								Index(560									el!(Var("std".into()), 1, 4),561									el!(Str("deepJoin".into()), 5, 13)562								),563								1,564								13565							),566							ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),567							false,568						),569						1,570						16571					),572					vec![CompSpec::ForSpec(ForSpecData(573						"x".into(),574						el!(Var("arr".into()), 26, 29)575					))]576				),577				0,578				30579			),580		)581	}582583	#[test]584	fn reserved() {585		use Expr::*;586		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));587		assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));588	}589590	#[test]591	fn multiple_args_buf() {592		parse!("a(b, null_fields)");593	}594595	#[test]596	fn infix_precedence() {597		use Expr::*;598		assert_eq!(599			parse!("!a && !b"),600			el!(601				BinaryOp(602					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),603					And,604					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)605				),606				0,607				8608			)609		);610	}611612	#[test]613	fn infix_precedence_division() {614		use Expr::*;615		assert_eq!(616			parse!("!a / !b"),617			el!(618				BinaryOp(619					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),620					Div,621					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)622				),623				0,624				7625			)626		);627	}628629	#[test]630	fn double_negation() {631		use Expr::*;632		assert_eq!(633			parse!("!!a"),634			el!(635				UnaryOp(636					UnaryOpType::Not,637					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)638				),639				0,640				3641			)642		)643	}644645	#[test]646	fn array_test_error() {647		parse!("[a for a in b if c for e in f]");648		//                    ^^^^ failed code649	}650651	#[test]652	fn missing_newline_between_comment_and_eof() {653		parse!(654			"{a:1}655656			//+213"657		);658	}659660	#[test]661	fn default_param_before_nondefault() {662		parse!("local x(foo = 'foo', bar) = null; null");663	}664665	#[test]666	fn can_parse_stdlib() {667		parse!(jrsonnet_stdlib::STDLIB_STR);668	}669670	#[test]671	fn add_location_info_to_all_sub_expressions() {672		use Expr::*;673674		let file_name: std::rc::Rc<std::path::Path> = PathBuf::from("test.jsonnet").into();675		let expr = parse(676			"{} { local x = 1, x: x } + {}",677			&ParserSettings {678				file_name: file_name.clone(),679			},680		)681		.unwrap();682		assert_eq!(683			expr,684			el!(685				BinaryOp(686					el!(687						ObjExtend(688							el!(Obj(ObjBody::MemberList(vec![])), 0, 2),689							ObjBody::MemberList(vec![690								Member::BindStmt(BindSpec {691									name: "x".into(),692									params: None,693									value: el!(Num(1.0), 15, 16)694								}),695								Member::Field(FieldMember {696									name: FieldName::Fixed("x".into()),697									plus: false,698									params: None,699									visibility: Visibility::Normal,700									value: el!(Var("x".into()), 21, 22),701								})702							])703						),704						0,705						24706					),707					BinaryOpType::Add,708					el!(Obj(ObjBody::MemberList(vec![])), 27, 29),709				),710				0,711				29712			),713		);714	}715	// From source code716	/*717	#[bench]718	fn bench_parse_peg(b: &mut Bencher) {719		b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))720	}721	*/722}
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -375,9 +375,7 @@
 
   manifestJsonEx:: $intrinsic(manifestJsonEx),
 
-  manifestYamlDocImpl:: $intrinsic(manifestYamlDocImpl),
-
-  manifestYamlDoc(value, indent_array_in_object=false, quote_keys=true):: std.manifestYamlDocImpl(value, indent_array_in_object, quote_keys),
+  manifestYamlDoc:: $intrinsic(manifestYamlDoc),
 
   manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::
     if !std.isArray(value) then
@@ -443,10 +441,7 @@
 
   reverse:: $intrinsic(reverse),
 
-  sortImpl:: $intrinsic(sortImpl),
-
-  sort(arr, keyF=id)::
-    std.sortImpl(arr, keyF),
+  sort:: $intrinsic(sort),
 
   uniq(arr, keyF=id)::
     local f(a, b) =