git.delta.rocks / jrsonnet / refs/commits / 349c41065b93

difftreelog

feat lazy values in builtin

Yaroslav Bolyukin2022-04-20parent: #c7fb888.patch.diff
in: master

8 files changed

modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,4 +1,4 @@
-use crate::function::StaticBuiltin;
+use crate::function::{CallLocation, StaticBuiltin};
 use crate::typed::{Any, PositiveF64, VecVal, M1};
 use crate::{
 	builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},
@@ -12,7 +12,6 @@
 use crate::{Either, ObjValue};
 use format::{format_arr, format_obj};
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::ExprLocation;
 use serde::Deserialize;
 use serde_yaml::DeserializingQuirks;
 use std::collections::HashMap;
@@ -29,7 +28,7 @@
 
 pub fn std_format(str: IStr, vals: Val) -> Result<String> {
 	push_frame(
-		None,
+		CallLocation::native(),
 		|| format!("std.format of {}", str),
 		|| {
 			Ok(match vals {
@@ -467,9 +466,9 @@
 }
 
 #[jrsonnet_macros::builtin]
-fn builtin_trace(#[location] loc: Option<&ExprLocation>, str: IStr, rest: Any) -> Result<Any> {
+fn builtin_trace(loc: CallLocation, str: IStr, rest: Any) -> Result<Any> {
 	eprint!("TRACE:");
-	if let Some(loc) = loc {
+	if let Some(loc) = loc.0 {
 		with_state(|s| {
 			let locs = s.map_source_locations(&loc.0, &[loc.1]);
 			eprint!(
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -4,6 +4,7 @@
 	builtin::{std_slice, BUILTINS},
 	error::Error::*,
 	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
+	function::CallLocation,
 	gc::TraceBox,
 	push_frame, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,
 	FutureWrapper, GcHashMap, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder,
@@ -12,8 +13,8 @@
 use gcmodule::{Cc, Trace};
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{
-	ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, ExprLocation, FieldMember, ForSpecData,
-	IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,
+	ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, ForSpecData, IfSpecData,
+	LiteralType, LocExpr, Member, ObjBody, ParamsDesc,
 };
 use jrsonnet_types::ValType;
 pub mod operator;
@@ -192,7 +193,7 @@
 	Ok(match field_name {
 		jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),
 		jrsonnet_parser::FieldName::Dyn(expr) => push_frame(
-			Some(&expr.1),
+			CallLocation::new(&expr.1),
 			|| "evaluating field name".to_string(),
 			|| {
 				let value = evaluate(context, expr)?;
@@ -442,7 +443,7 @@
 	context: Context,
 	value: &LocExpr,
 	args: &ArgsDesc,
-	loc: Option<&ExprLocation>,
+	loc: CallLocation,
 	tailstrict: bool,
 ) -> Result<Val> {
 	let value = evaluate(context.clone(), value)?;
@@ -463,13 +464,13 @@
 	let value = &assertion.0;
 	let msg = &assertion.1;
 	let assertion_result = push_frame(
-		Some(&value.1),
+		CallLocation::new(&value.1),
 		|| "assertion condition".to_owned(),
 		|| bool::try_from(evaluate(context.clone(), value)?),
 	)?;
 	if !assertion_result {
 		push_frame(
-			Some(&value.1),
+			CallLocation::new(&value.1),
 			|| "assertion failure".to_owned(),
 			|| {
 				if let Some(msg) = msg {
@@ -519,7 +520,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(
-			Some(loc),
+			CallLocation::new(loc),
 			|| format!("variable <{}> access", name),
 			|| context.binding(name.clone())?.evaluate(),
 		)?,
@@ -528,7 +529,7 @@
 				(Val::Obj(v), Val::Str(s)) => {
 					let sn = s.clone();
 					push_frame(
-						Some(loc),
+						CallLocation::new(loc),
 						|| format!("field <{}> access", sn),
 						|| {
 							if let Some(v) = v.get(s.clone())? {
@@ -625,7 +626,7 @@
 			&Val::Obj(evaluate_object(context, t)?),
 		)?,
 		Apply(value, args, tailstrict) => {
-			evaluate_apply(context, value, args, Some(loc), *tailstrict)?
+			evaluate_apply(context, value, args, CallLocation::new(loc), *tailstrict)?
 		}
 		Function(params, body) => {
 			evaluate_method(context, "anonymous".into(), params.clone(), body.clone())
@@ -640,7 +641,7 @@
 			evaluate(context, returned)?
 		}
 		ErrorStmt(e) => push_frame(
-			Some(loc),
+			CallLocation::new(loc),
 			|| "error statement".to_owned(),
 			|| throw!(RuntimeError(IStr::try_from(evaluate(context, e)?)?,)),
 		)?,
@@ -650,7 +651,7 @@
 			cond_else,
 		} => {
 			if push_frame(
-				Some(loc),
+				CallLocation::new(loc),
 				|| "if condition".to_owned(),
 				|| bool::try_from(evaluate(context.clone(), &cond.0)?),
 			)? {
@@ -689,7 +690,7 @@
 			let mut import_location = tmp.to_path_buf();
 			import_location.pop();
 			push_frame(
-				Some(loc),
+				CallLocation::new(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
@@ -12,6 +12,19 @@
 use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
 use std::{borrow::Cow, collections::HashMap, convert::TryFrom};
 
+#[derive(Clone, Copy)]
+pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);
+impl<'l> CallLocation<'l> {
+	pub fn new(loc: &'l ExprLocation) -> Self {
+		Self(Some(loc))
+	}
+}
+impl CallLocation<'static> {
+	pub fn native() -> Self {
+		Self(None)
+	}
+}
+
 #[derive(Trace)]
 struct EvaluateLazyVal {
 	context: Context,
@@ -383,12 +396,7 @@
 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>;
+	fn call(&self, context: Context, loc: CallLocation, args: &dyn ArgsLike) -> Result<Val>;
 }
 
 pub trait StaticBuiltin: Builtin + Send + Sync
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -28,7 +28,7 @@
 pub use dynamic::*;
 use error::{Error::*, LocError, Result, StackTraceElement};
 pub use evaluate::*;
-use function::{Builtin, TlaArg};
+use function::{Builtin, CallLocation, TlaArg};
 use gc::{GcHashMap, TraceBox};
 use gcmodule::{Cc, Trace, Weak};
 pub use import::*;
@@ -173,6 +173,7 @@
 	/// Global state is fine here.
 	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)
 }
+
 pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {
 	EVAL_STATE.with(|s| {
 		f(s.borrow().as_ref().expect(
@@ -181,7 +182,7 @@
 	})
 }
 pub fn push_frame<T>(
-	e: Option<&ExprLocation>,
+	e: CallLocation,
 	frame_desc: impl FnOnce() -> String,
 	f: impl FnOnce() -> Result<T>,
 ) -> Result<T> {
@@ -345,7 +346,7 @@
 	/// Executes code creating a new stack frame
 	pub fn push<T>(
 		&self,
-		e: Option<&ExprLocation>,
+		e: CallLocation,
 		frame_desc: impl FnOnce() -> String,
 		f: impl FnOnce() -> Result<T>,
 	) -> Result<T> {
@@ -368,7 +369,7 @@
 		}
 		if let Err(mut err) = result {
 			err.trace_mut().0.push(StackTraceElement {
-				location: e.cloned(),
+				location: e.0.cloned(),
 				desc: frame_desc(),
 			});
 			return Err(err);
@@ -512,7 +513,7 @@
 					|| {
 						func.evaluate(
 							self.create_default_context(),
-							None,
+							CallLocation::native(),
 							&self.settings().tla_vars,
 							true,
 						)
modifiedcrates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -1,11 +1,10 @@
 #![allow(clippy::type_complexity)]
 
-use crate::function::{parse_builtin_call, ArgsLike, Builtin, BuiltinParam};
+use crate::function::{parse_builtin_call, ArgsLike, Builtin, BuiltinParam, CallLocation};
 use crate::gc::TraceBox;
 use crate::Context;
 use crate::{error::Result, Val};
 use gcmodule::Trace;
-use jrsonnet_parser::ExprLocation;
 use std::path::Path;
 use std::rc::Rc;
 
@@ -32,18 +31,13 @@
 		&self.params
 	}
 
-	fn call(
-		&self,
-		context: Context,
-		loc: Option<&ExprLocation>,
-		args: &dyn ArgsLike,
-	) -> Result<Val> {
+	fn call(&self, context: Context, loc: CallLocation, args: &dyn ArgsLike) -> Result<Val> {
 		let args = parse_builtin_call(context, &self.params, args, true)?;
 		let mut out_args = Vec::with_capacity(self.params.len());
 		for p in self.params.iter() {
 			out_args.push(args[&p.name].evaluate()?);
 		}
-		self.handler.call(loc.map(|l| l.0.clone()), &out_args)
+		self.handler.call(loc.0.map(|l| l.0.clone()), &out_args)
 	}
 }
 
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -1,5 +1,6 @@
 use std::convert::{TryFrom, TryInto};
 
+use gcmodule::Cc;
 use jrsonnet_interner::IStr;
 pub use jrsonnet_macros::Typed;
 use jrsonnet_types::{ComplexValType, ValType};
@@ -8,7 +9,7 @@
 	error::{Error::*, LocError, Result},
 	throw,
 	typed::CheckType,
-	ArrValue, FuncVal, IndexableVal, ObjValue, ObjValueBuilder, Val,
+	ArrValue, FuncDesc, FuncVal, IndexableVal, ObjValue, ObjValueBuilder, Val,
 };
 
 pub trait TypedObj: Typed {
@@ -431,6 +432,30 @@
 		Ok(Self::Func(value))
 	}
 }
+
+impl Typed for Cc<FuncDesc> {
+	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);
+}
+impl TryFrom<Val> for Cc<FuncDesc> {
+	type Error = LocError;
+
+	fn try_from(value: Val) -> Result<Self, Self::Error> {
+		<Self as Typed>::TYPE.check(&value)?;
+		match value {
+			Val::Func(FuncVal::Normal(desc)) => Ok(desc.clone()),
+			Val::Func(_) => throw!(RuntimeError("expected normal function, not builtin".into())),
+			_ => unreachable!(),
+		}
+	}
+}
+impl TryFrom<Cc<FuncDesc>> for Val {
+	type Error = LocError;
+
+	fn try_from(value: Cc<FuncDesc>) -> Result<Self, Self::Error> {
+		Ok(Self::Func(FuncVal::Normal(value)))
+	}
+}
+
 impl Typed for ObjValue {
 	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);
 }
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -6,14 +6,15 @@
 	error::{Error::*, LocError},
 	evaluate,
 	function::{
-		parse_default_function_call, parse_function_call, ArgsLike, Builtin, StaticBuiltin,
+		parse_default_function_call, parse_function_call, ArgsLike, Builtin, CallLocation,
+		StaticBuiltin,
 	},
 	gc::TraceBox,
 	throw, Context, ObjValue, Result,
 };
 use gcmodule::{Cc, Trace};
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ExprLocation, LocExpr, ParamsDesc};
+use jrsonnet_parser::{LocExpr, ParamsDesc};
 use jrsonnet_types::ValType;
 use std::{cell::RefCell, fmt::Debug, rc::Rc};
 
@@ -152,7 +153,7 @@
 	pub fn evaluate(
 		&self,
 		call_ctx: Context,
-		loc: Option<&ExprLocation>,
+		loc: CallLocation,
 		args: &dyn ArgsLike,
 		tailstrict: bool,
 	) -> Result<Val> {
@@ -166,7 +167,7 @@
 		}
 	}
 	pub fn evaluate_simple(&self, args: &dyn ArgsLike) -> Result<Val> {
-		self.evaluate(Context::default(), None, args, true)
+		self.evaluate(Context::default(), CallLocation::native(), args, true)
 	}
 }
 
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-macros/src/lib.rs
1use proc_macro2::TokenStream;2use quote::{quote, quote_spanned};3use syn::{4	parenthesized,5	parse::{Parse, ParseStream},6	parse_macro_input,7	punctuated::Punctuated,8	spanned::Spanned,9	token::Comma,10	Attribute, DeriveInput, Error, FnArg, GenericArgument, Ident, ItemFn, LitStr, Pat, PatType,11	Path, PathArguments, Result, Token, Type,12};1314fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>15where16	Ident: PartialEq<I>,17{18	let attrs = attrs19		.iter()20		.filter(|a| a.path.is_ident(&ident))21		.collect::<Vec<_>>();22	if attrs.len() > 1 {23		return Err(Error::new(24			attrs[1].span(),25			"this attribute may be specified only once",26		));27	} else if attrs.is_empty() {28		return Ok(None);29	}30	let attr = attrs[0];31	let attr = attr.parse_args::<A>()?;3233	Ok(Some(attr))34}3536fn is_location_arg(t: &PatType) -> bool {37	t.attrs.iter().any(|a| a.path.is_ident("location"))38}39fn is_self_arg(t: &PatType) -> bool {40	t.attrs.iter().any(|a| a.path.is_ident("self"))41}4243trait RetainHad<T> {44	fn retain_had(&mut self, h: impl FnMut(&T) -> bool) -> bool;45}46impl<T> RetainHad<T> for Vec<T> {47	fn retain_had(&mut self, h: impl FnMut(&T) -> bool) -> bool {48		let before = self.len();49		self.retain(h);50		let after = self.len();51		before != after52	}53}5455fn extract_type_from_option(ty: &Type) -> Option<&Type> {56	fn path_is_option(path: &Path) -> bool {57		path.leading_colon.is_none()58			&& path.segments.len() == 159			&& path.segments.iter().next().unwrap().ident == "Option"60	}6162	match ty {63		Type::Path(typepath) if typepath.qself.is_none() && path_is_option(&typepath.path) => {64			// Get the first segment of the path (there is only one, in fact: "Option"):65			let type_params = &typepath.path.segments.iter().next().unwrap().arguments;66			// It should have only on angle-bracketed param ("<String>"):67			let generic_arg = match type_params {68				PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),69				_ => panic!("missing option generic"),70			};71			// This argument must be a type:72			match generic_arg {73				GenericArgument::Type(ty) => Some(ty),74				_ => panic!("option generic should be a type"),75			}76		}77		_ => None,78	}79}8081struct Field {82	name: Ident,83	_colon: Token![:],84	ty: Type,85}86impl Parse for Field {87	fn parse(input: ParseStream) -> syn::Result<Self> {88		Ok(Self {89			name: input.parse()?,90			_colon: input.parse()?,91			ty: input.parse()?,92		})93	}94}9596mod kw {97	syn::custom_keyword!(fields);98	syn::custom_keyword!(rename);99	syn::custom_keyword!(flatten);100}101102struct EmptyAttr;103impl Parse for EmptyAttr {104	fn parse(input: ParseStream) -> Result<Self> {105		Ok(Self)106	}107}108109struct BuiltinAttrs {110	fields: Vec<Field>,111}112impl Parse for BuiltinAttrs {113	fn parse(input: ParseStream) -> syn::Result<Self> {114		if input.is_empty() {115			return Ok(Self { fields: Vec::new() });116		}117		input.parse::<kw::fields>()?;118		let fields;119		parenthesized!(fields in input);120		let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;121		Ok(Self {122			fields: p.into_iter().collect(),123		})124	}125}126127#[proc_macro_attribute]128pub fn builtin(129	attr: proc_macro::TokenStream,130	item: proc_macro::TokenStream,131) -> proc_macro::TokenStream {132	let attrs = parse_macro_input!(attr as BuiltinAttrs);133	let mut fun: ItemFn = parse_macro_input!(item);134135	let result = match fun.sig.output {136		syn::ReturnType::Default => {137			return quote_spanned! { fun.sig.span() =>138				compile_error!("builtins should return something");139			}140			.into()141		}142		syn::ReturnType::Type(_, ref ty) => ty.clone(),143	};144145	let params = fun146		.sig147		.inputs148		.iter()149		.map(|i| match i {150			FnArg::Receiver(_) => unreachable!(),151			FnArg::Typed(t) => t,152		})153		.filter(|a| !is_location_arg(a) && !is_self_arg(a))154		.map(|t| {155			let ident = match &t.pat as &Pat {156				Pat::Ident(i) => i.ident.to_string(),157				_ => {158					return quote_spanned! { t.pat.span() =>159						compile_error!("args should be plain identifiers")160					}161					.into()162				}163			};164			let optional = extract_type_from_option(&t.ty).is_some();165			quote! {166				BuiltinParam {167					name: std::borrow::Cow::Borrowed(#ident),168					has_default: #optional,169				}170			}171		})172		.collect::<Vec<_>>();173174	let args = fun175		.sig176		.inputs177		.iter_mut()178		.map(|i| match i {179			FnArg::Receiver(_) => unreachable!(),180			FnArg::Typed(t) => t,181		})182		.map(|t| {183			if t.attrs.retain_had(|a| !a.path.is_ident("location")) {184				quote! {{185					loc186				}}187			} else if t.attrs.retain_had(|a| !a.path.is_ident("self")) {188				quote! {{189					self190				}}191			} else {192				let ident = match &t.pat as &Pat {193					Pat::Ident(i) => i.ident.to_string(),194					_ => {195						return quote_spanned! { t.pat.span() =>196							compile_error!("args should be plain identifiers")197						}198						.into()199					}200				};201				let ty = &t.ty;202				if let Some(opt_ty) = extract_type_from_option(&t.ty) {203					quote! {{204						if let Some(value) = parsed.get(#ident) {205							Some(::jrsonnet_evaluator::push_description_frame(206								|| format!("argument <{}> evaluation", #ident),207								|| <#opt_ty>::try_from(value.evaluate()?),208							)?)209						} else {210							None211						}212					}}213				} else {214					quote! {{215						let value = parsed.get(#ident).unwrap();216217						::jrsonnet_evaluator::push_description_frame(218							|| format!("argument <{}> evaluation", #ident),219							|| <#ty>::try_from(value.evaluate()?),220						)?221					}}222				}223			}224		})225		.collect::<Vec<_>>();226227	let fields = attrs.fields.iter().map(|field| {228		let name = &field.name;229		let ty = &field.ty;230		quote! {231			pub #name: #ty,232		}233	});234235	let name = &fun.sig.ident;236	let vis = &fun.vis;237	let static_ext = if attrs.fields.is_empty() {238		quote! {239			impl #name {240				pub const INST: &'static dyn StaticBuiltin = &#name {};241			}242			impl StaticBuiltin for #name {}243		}244	} else {245		quote! {}246	};247	let static_derive_copy = if attrs.fields.is_empty() {248		quote! {, Copy}249	} else {250		quote! {}251	};252253	(quote! {254		#fun255		#[doc(hidden)]256		#[allow(non_camel_case_types)]257		#[derive(Clone, gcmodule::Trace #static_derive_copy)]258		#vis struct #name {259			#(#fields)*260		}261		const _: () = {262			use ::jrsonnet_evaluator::{263				function::{Builtin, StaticBuiltin, BuiltinParam, ArgsLike, parse_builtin_call},264				error::Result, Context,265				parser::ExprLocation,266			};267			const PARAMS: &'static [BuiltinParam] = &[268				#(#params),*269			];270271			#static_ext272			impl Builtin for #name273			where274				Self: 'static275			{276				fn name(&self) -> &str {277					stringify!(#name)278				}279				fn params(&self) -> &[BuiltinParam] {280					PARAMS281				}282				fn call(&self, context: Context, loc: Option<&ExprLocation>, args: &dyn ArgsLike) -> Result<Val> {283					let parsed = parse_builtin_call(context, &PARAMS, args, false)?;284285					let result: #result = #name(#(#args),*);286					let result = result?;287					result.try_into()288				}289			}290		};291	})292	.into()293}294295#[derive(Default)]296struct TypedAttr {297	rename: Option<String>,298	flatten: bool,299}300impl Parse for TypedAttr {301	fn parse(input: ParseStream) -> syn::Result<Self> {302		let mut out = Self::default();303		loop {304			let lookahead = input.lookahead1();305			if lookahead.peek(kw::rename) {306				input.parse::<kw::rename>()?;307				input.parse::<Token![=]>()?;308				let name = input.parse::<LitStr>()?;309				if out.rename.is_some() {310					return Err(Error::new(311						name.span(),312						"rename attribute may only be specified once",313					));314				}315				out.rename = Some(name.value());316			} else if lookahead.peek(kw::flatten) {317				input.parse::<kw::flatten>()?;318				out.flatten = true;319			} else if input.is_empty() {320				break;321			} else {322				return Err(lookahead.error());323			}324			if input.peek(Token![,]) {325				input.parse::<Token![,]>()?;326			} else {327				break;328			}329		}330		// input.parse::<kw::rename>()?;331		// input.parse::<Token![=]>()?;332		// let rename = input.parse::<LitStr>()?.value();333		Ok(out)334	}335}336337struct TypedField<'f>(&'f syn::Field, TypedAttr);338impl<'f> TypedField<'f> {339	fn try_new(field: &'f syn::Field) -> Result<Self> {340		let attr =341			parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_else(Default::default);342		if field.ident.is_none() {343			return Err(Error::new(344				field.span(),345				"this field should appear in output object, but it has no visible name",346			));347		}348		Ok(Self(field, attr))349	}350	fn ident(&self) -> Ident {351		self.0352			.ident353			.clone()354			.expect("constructor disallows fields without name")355	}356	/// None if this field is flattened in jsonnet output357	fn name(&self) -> Option<String> {358		if self.1.flatten {359			return None;360		}361		Some(362			self.1363				.rename364				.clone()365				.unwrap_or_else(|| self.ident().to_string()),366		)367	}368369	fn expand_shallow_field(&self) -> Option<TokenStream> {370		if self.is_option() {371			return None;372		}373		let name = self.name()?;374		Some(quote! {375			(#name, ComplexValType::Any)376		})377	}378	fn expand_field(&self) -> Option<TokenStream> {379		if self.is_option() {380			return None;381		}382		let name = self.name()?;383		let ty = &self.0.ty;384		Some(quote! {385			(#name, <#ty>::TYPE)386		})387	}388	fn expand_parse(&self) -> TokenStream {389		let ident = self.ident();390		let ty = &self.0.ty;391		if self.1.flatten {392			// optional flatten is handled in same way as serde393			return if self.is_option() {394				quote! {395					#ident: <#ty>::parse(&obj).ok(),396				}397			} else {398				quote! {399					#ident: <#ty>::parse(&obj)?,400				}401			};402		};403404		let name = self.name().unwrap();405		let value = if let Some(ty) = self.as_option() {406			quote! {407				if let Some(value) = obj.get(#name.into())? {408					Some(<#ty>::try_from(vakue)?)409				} else {410					None411				}412			}413		} else {414			quote! {415				<#ty>::try_from(obj.get(#name.into())?.ok_or_else(|| Error::NoSuchField(#name.into()))?)?416			}417		};418419		quote! {420			#ident: #value,421		}422	}423	fn expand_serialize(&self) -> TokenStream {424		let ident = self.ident();425		if let Some(name) = self.name() {426			if self.is_option() {427				quote! {428					if let Some(value) = self.#ident {429						out.member(#name.into()).value(value.try_into()?);430					}431				}432			} else {433				quote! {434					out.member(#name.into()).value(self.#ident.try_into()?);435				}436			}437		} else {438			if self.is_option() {439				quote! {440					if let Some(value) = self.#ident {441						value.serialize(out)?;442					}443				}444			} else {445				quote! {446					self.#ident.serialize(out)?;447				}448			}449		}450	}451452	fn as_option(&self) -> Option<&Type> {453		extract_type_from_option(&self.0.ty)454	}455	fn is_option(&self) -> bool {456		self.as_option().is_some()457	}458}459460#[proc_macro_derive(Typed, attributes(typed))]461pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {462	let input = parse_macro_input!(item as DeriveInput);463	let data = match &input.data {464		syn::Data::Struct(s) => s,465		_ => {466			return syn::Error::new(input.span(), "only structs supported")467				.to_compile_error()468				.into()469		}470	};471472	let ident = &input.ident;473	let fields = match data474		.fields475		.iter()476		.map(TypedField::try_new)477		.collect::<Result<Vec<_>>>()478	{479		Ok(v) => v,480		Err(e) => return e.to_compile_error().into(),481	};482483	let typed = {484		let fields = fields485			.iter()486			.flat_map(TypedField::expand_field)487			.collect::<Vec<_>>();488		let len = fields.len();489		quote! {490			const ITEMS: [(&'static str, &'static ComplexValType); #len] = [491				#(#fields,)*492			];493			impl Typed for #ident {494				const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&ITEMS);495			}496		}497	};498499	let fields_parse = fields.iter().map(TypedField::expand_parse);500	let fields_serialize = fields.iter().map(TypedField::expand_serialize);501502	quote! {503		const _: () = {504			use ::jrsonnet_evaluator::{505				typed::{ComplexValType, Typed, TypedObj, CheckType},506				Val,507				error::{LocError, Error},508				ObjValueBuilder, ObjValue,509			};510511			#typed512513			impl #ident {514				fn serialize(self, out: &mut ObjValueBuilder) -> Result<(), LocError> {515					#(#fields_serialize)*516517					Ok(())518				}519				fn parse(obj: &ObjValue) -> Result<Self, LocError> {520					Ok(Self {521						#(#fields_parse)*522					})523				}524			}525526			impl TryFrom<Val> for #ident {527				type Error = LocError;528				fn try_from(value: Val) -> Result<Self, Self::Error> {529					let obj = value.as_obj().expect("shape is correct");530					Self::parse(&obj)531				}532			}533			impl TryInto<Val> for #ident {534				type Error = LocError;535				fn try_into(self) -> Result<Val, Self::Error> {536					let mut out = ObjValueBuilder::new();537					self.serialize(&mut out)?;538					Ok(Val::Obj(out.build()))539				}540			}541			()542		};543	}544	.into()545}
after · crates/jrsonnet-macros/src/lib.rs
1use proc_macro2::TokenStream;2use quote::quote;3use syn::{4	parenthesized,5	parse::{Parse, ParseStream},6	parse_macro_input,7	punctuated::Punctuated,8	spanned::Spanned,9	token::Comma,10	Attribute, DeriveInput, Error, FnArg, GenericArgument, Ident, ItemFn, LitStr, Pat, Path,11	PathArguments, Result, ReturnType, Token, Type,12};1314fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>15where16	Ident: PartialEq<I>,17{18	let attrs = attrs19		.iter()20		.filter(|a| a.path.is_ident(&ident))21		.collect::<Vec<_>>();22	if attrs.len() > 1 {23		return Err(Error::new(24			attrs[1].span(),25			"this attribute may be specified only once",26		));27	} else if attrs.is_empty() {28		return Ok(None);29	}30	let attr = attrs[0];31	let attr = attr.parse_args::<A>()?;3233	Ok(Some(attr))34}3536fn path_is(path: &Path, needed: &str) -> bool {37	path.leading_colon.is_none()38		&& path.segments.len() >= 139		&& path.segments.iter().last().unwrap().ident == needed40}4142fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {43	match ty {44		Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {45			let args = &path.path.segments.iter().last().unwrap().arguments;46			Some(args)47		}48		_ => None,49	}50}5152fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {53	Ok(if let Some(args) = type_is_path(ty, "Option") {54		// It should have only on angle-bracketed param ("<String>"):55		let generic_arg = match args {56			PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),57			_ => return Err(Error::new(args.span(), "missing option generic")),58		};59		// This argument must be a type:60		match generic_arg {61			GenericArgument::Type(ty) => Some(ty),62			_ => {63				return Err(Error::new(64					generic_arg.span(),65					"option generic should be a type",66				))67			}68		}69	} else {70		None71	})72}7374struct Field {75	name: Ident,76	_colon: Token![:],77	ty: Type,78}79impl Parse for Field {80	fn parse(input: ParseStream) -> syn::Result<Self> {81		Ok(Self {82			name: input.parse()?,83			_colon: input.parse()?,84			ty: input.parse()?,85		})86	}87}8889mod kw {90	syn::custom_keyword!(fields);91	syn::custom_keyword!(rename);92	syn::custom_keyword!(flatten);93}9495struct EmptyAttr;96impl Parse for EmptyAttr {97	fn parse(_input: ParseStream) -> Result<Self> {98		Ok(Self)99	}100}101102struct BuiltinAttrs {103	fields: Vec<Field>,104}105impl Parse for BuiltinAttrs {106	fn parse(input: ParseStream) -> syn::Result<Self> {107		if input.is_empty() {108			return Ok(Self { fields: Vec::new() });109		}110		input.parse::<kw::fields>()?;111		let fields;112		parenthesized!(fields in input);113		let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;114		Ok(Self {115			fields: p.into_iter().collect(),116		})117	}118}119120enum ArgInfo {121	Normal {122		ty: Type,123		is_option: bool,124		name: String,125		// ident: Ident,126	},127	Lazy {128		is_option: bool,129		name: String,130	},131	Location,132	This,133}134135impl ArgInfo {136	fn parse(arg: &FnArg) -> Result<Self> {137		let typed = match arg {138			FnArg::Receiver(_) => unreachable!(),139			FnArg::Typed(a) => a,140		};141		let ident = match &typed.pat as &Pat {142			Pat::Ident(i) => i.ident.clone(),143			_ => {144				return Err(Error::new(145					typed.pat.span(),146					"arg should be plain identifier",147				))148			}149		};150		let ty = &typed.ty as &Type;151		if type_is_path(&ty, "CallLocation").is_some() {152			return Ok(Self::Location);153		} else if type_is_path(&ty, "Self").is_some() {154			return Ok(Self::This);155		} else if type_is_path(&ty, "LazyVal").is_some() {156			return Ok(Self::Lazy {157				is_option: false,158				name: ident.to_string(),159			});160		}161162		let (is_option, ty) = if let Some(ty) = extract_type_from_option(&ty)? {163			if type_is_path(&ty, "LazyVal").is_some() {164				return Ok(Self::Lazy {165					is_option: true,166					name: ident.to_string(),167				});168			}169170			(true, ty.clone())171		} else {172			(false, ty.clone())173		};174175		Ok(Self::Normal {176			ty,177			is_option,178			name: ident.to_string(),179			// ident,180		})181	}182}183184#[proc_macro_attribute]185pub fn builtin(186	attr: proc_macro::TokenStream,187	item: proc_macro::TokenStream,188) -> proc_macro::TokenStream {189	let attr = parse_macro_input!(attr as BuiltinAttrs);190	let item: ItemFn = parse_macro_input!(item);191192	match builtin_inner(attr, item) {193		Ok(v) => v.into(),194		Err(e) => e.into_compile_error().into(),195	}196}197198fn builtin_inner(attr: BuiltinAttrs, fun: ItemFn) -> syn::Result<TokenStream> {199	let result = match fun.sig.output {200		ReturnType::Default => {201			return Err(Error::new(202				fun.sig.span(),203				"builtin should return something",204			))205		}206		ReturnType::Type(_, ref ty) => ty.clone(),207	};208209	let args = fun210		.sig211		.inputs212		.iter()213		.map(|a| ArgInfo::parse(a))214		.collect::<Result<Vec<_>>>()?;215216	let params_desc = args.iter().flat_map(|a| match a {217		ArgInfo::Normal {218			is_option, name, ..219		}220		| ArgInfo::Lazy { is_option, name } => Some(quote! {221			BuiltinParam {222				name: std::borrow::Cow::Borrowed(#name),223				has_default: #is_option,224			}225		}),226		ArgInfo::Location => None,227		ArgInfo::This => None,228	});229230	let pass = args.iter().map(|a| match a {231		ArgInfo::Normal {232			ty,233			is_option,234			name,235			// ident,236		} => {237			let eval = quote! {::jrsonnet_evaluator::push_description_frame(238				|| format!("argument <{}> evaluation", #name),239				|| <#ty>::try_from(value.evaluate()?),240			)?};241			if *is_option {242				quote! {if let Some(value) = parsed.get(#name) {243					Some(#eval)244				} else {245					None246				}}247			} else {248				quote! {{249					let value = parsed.get(#name).expect("args shape is checked");250					#eval251				}}252			}253		}254		ArgInfo::Lazy { is_option, name } => {255			if *is_option {256				quote! {if let Some(value) = parsed.get(#name) {257					Some(value.clone())258				} else {259					None260				}}261			} else {262				quote! {263					parsed.get(#name).expect("args shape is correct").clone()264				}265			}266		}267		ArgInfo::Location => quote! {location},268		ArgInfo::This => quote! {self},269	});270271	let fields = attr.fields.iter().map(|field| {272		let name = &field.name;273		let ty = &field.ty;274		quote! {275			pub #name: #ty,276		}277	});278279	let name = &fun.sig.ident;280	let vis = &fun.vis;281	let static_ext = if attr.fields.is_empty() {282		quote! {283			impl #name {284				pub const INST: &'static dyn StaticBuiltin = &#name {};285			}286			impl StaticBuiltin for #name {}287		}288	} else {289		quote! {}290	};291	let static_derive_copy = if attr.fields.is_empty() {292		quote! {, Copy}293	} else {294		quote! {}295	};296297	Ok(quote! {298		#fun299		#[doc(hidden)]300		#[allow(non_camel_case_types)]301		#[derive(Clone, gcmodule::Trace #static_derive_copy)]302		#vis struct #name {303			#(#fields)*304		}305		const _: () = {306			use ::jrsonnet_evaluator::{307				function::{Builtin, CallLocation, StaticBuiltin, BuiltinParam, ArgsLike, parse_builtin_call},308				error::Result, Context,309				parser::ExprLocation,310			};311			const PARAMS: &'static [BuiltinParam] = &[312				#(#params_desc),*313			];314315			#static_ext316			impl Builtin for #name317			where318				Self: 'static319			{320				fn name(&self) -> &str {321					stringify!(#name)322				}323				fn params(&self) -> &[BuiltinParam] {324					PARAMS325				}326				fn call(&self, context: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {327					let parsed = parse_builtin_call(context, &PARAMS, args, false)?;328329					let result: #result = #name(#(#pass),*);330					let result = result?;331					result.try_into()332				}333			}334		};335	})336}337338#[derive(Default)]339struct TypedAttr {340	rename: Option<String>,341	flatten: bool,342}343impl Parse for TypedAttr {344	fn parse(input: ParseStream) -> syn::Result<Self> {345		let mut out = Self::default();346		loop {347			let lookahead = input.lookahead1();348			if lookahead.peek(kw::rename) {349				input.parse::<kw::rename>()?;350				input.parse::<Token![=]>()?;351				let name = input.parse::<LitStr>()?;352				if out.rename.is_some() {353					return Err(Error::new(354						name.span(),355						"rename attribute may only be specified once",356					));357				}358				out.rename = Some(name.value());359			} else if lookahead.peek(kw::flatten) {360				input.parse::<kw::flatten>()?;361				out.flatten = true;362			} else if input.is_empty() {363				break;364			} else {365				return Err(lookahead.error());366			}367			if input.peek(Token![,]) {368				input.parse::<Token![,]>()?;369			} else {370				break;371			}372		}373		// input.parse::<kw::rename>()?;374		// input.parse::<Token![=]>()?;375		// let rename = input.parse::<LitStr>()?.value();376		Ok(out)377	}378}379380struct TypedField<'f>(&'f syn::Field, TypedAttr);381impl<'f> TypedField<'f> {382	fn try_new(field: &'f syn::Field) -> Result<Self> {383		let attr =384			parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_else(Default::default);385		if field.ident.is_none() {386			return Err(Error::new(387				field.span(),388				"this field should appear in output object, but it has no visible name",389			));390		}391		Ok(Self(field, attr))392	}393	fn ident(&self) -> Ident {394		self.0395			.ident396			.clone()397			.expect("constructor disallows fields without name")398	}399	/// None if this field is flattened in jsonnet output400	fn name(&self) -> Option<String> {401		if self.1.flatten {402			return None;403		}404		Some(405			self.1406				.rename407				.clone()408				.unwrap_or_else(|| self.ident().to_string()),409		)410	}411412	fn expand_field(&self) -> Option<TokenStream> {413		if self.is_option() {414			return None;415		}416		let name = self.name()?;417		let ty = &self.0.ty;418		Some(quote! {419			(#name, <#ty>::TYPE)420		})421	}422	fn expand_parse(&self) -> TokenStream {423		let ident = self.ident();424		let ty = &self.0.ty;425		if self.1.flatten {426			// optional flatten is handled in same way as serde427			return if self.is_option() {428				quote! {429					#ident: <#ty>::parse(&obj).ok(),430				}431			} else {432				quote! {433					#ident: <#ty>::parse(&obj)?,434				}435			};436		};437438		let name = self.name().unwrap();439		let value = if let Some(ty) = self.as_option() {440			quote! {441				if let Some(value) = obj.get(#name.into())? {442					Some(<#ty>::try_from(vakue)?)443				} else {444					None445				}446			}447		} else {448			quote! {449				<#ty>::try_from(obj.get(#name.into())?.ok_or_else(|| Error::NoSuchField(#name.into()))?)?450			}451		};452453		quote! {454			#ident: #value,455		}456	}457	fn expand_serialize(&self) -> TokenStream {458		let ident = self.ident();459		if let Some(name) = self.name() {460			if self.is_option() {461				quote! {462					if let Some(value) = self.#ident {463						out.member(#name.into()).value(value.try_into()?);464					}465				}466			} else {467				quote! {468					out.member(#name.into()).value(self.#ident.try_into()?);469				}470			}471		} else {472			if self.is_option() {473				quote! {474					if let Some(value) = self.#ident {475						value.serialize(out)?;476					}477				}478			} else {479				quote! {480					self.#ident.serialize(out)?;481				}482			}483		}484	}485486	fn as_option(&self) -> Option<&Type> {487		extract_type_from_option(&self.0.ty).unwrap()488	}489	fn is_option(&self) -> bool {490		self.as_option().is_some()491	}492}493494#[proc_macro_derive(Typed, attributes(typed))]495pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {496	let input = parse_macro_input!(item as DeriveInput);497498	match derive_typed_inner(input) {499		Ok(v) => v.into(),500		Err(e) => e.to_compile_error().into(),501	}502}503504fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {505	let data = match &input.data {506		syn::Data::Struct(s) => s,507		_ => return Err(Error::new(input.span(), "only structs supported")),508	};509510	let ident = &input.ident;511	let fields = data512		.fields513		.iter()514		.map(TypedField::try_new)515		.collect::<Result<Vec<_>>>()?;516517	let typed = {518		let fields = fields519			.iter()520			.flat_map(TypedField::expand_field)521			.collect::<Vec<_>>();522		let len = fields.len();523		quote! {524			const ITEMS: [(&'static str, &'static ComplexValType); #len] = [525				#(#fields,)*526			];527			impl Typed for #ident {528				const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&ITEMS);529			}530		}531	};532533	let fields_parse = fields.iter().map(TypedField::expand_parse);534	let fields_serialize = fields.iter().map(TypedField::expand_serialize);535536	Ok(quote! {537		const _: () = {538			use ::jrsonnet_evaluator::{539				typed::{ComplexValType, Typed, TypedObj, CheckType},540				Val,541				error::{LocError, Error},542				ObjValueBuilder, ObjValue,543			};544545			#typed546547			impl #ident {548				fn serialize(self, out: &mut ObjValueBuilder) -> Result<(), LocError> {549					#(#fields_serialize)*550551					Ok(())552				}553				fn parse(obj: &ObjValue) -> Result<Self, LocError> {554					Ok(Self {555						#(#fields_parse)*556					})557				}558			}559560			impl TryFrom<Val> for #ident {561				type Error = LocError;562				fn try_from(value: Val) -> Result<Self, Self::Error> {563					let obj = value.as_obj().expect("shape is correct");564					Self::parse(&obj)565				}566			}567			impl TryInto<Val> for #ident {568				type Error = LocError;569				fn try_into(self) -> Result<Val, Self::Error> {570					let mut out = ObjValueBuilder::new();571					self.serialize(&mut out)?;572					Ok(Val::Obj(out.build()))573				}574			}575			()576		};577	})578}