git.delta.rocks / jrsonnet / refs/commits / a9a2382fcb8f

difftreelog

source

crates/jrsonnet-macros/src/lib.rs20.0 KiBsourcehistory
1use std::string::String;23use proc_macro2::TokenStream;4use quote::{quote, quote_spanned};5use syn::{6	parenthesized,7	parse::{Parse, ParseStream},8	parse_macro_input,9	punctuated::Punctuated,10	spanned::Spanned,11	token::{self, Comma},12	Attribute, DeriveInput, Error, Expr, ExprClosure, FnArg, GenericArgument, Ident, ItemFn,13	LitStr, Pat, Path, PathArguments, Result, ReturnType, Token, Type,14};1516fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>17where18	Ident: PartialEq<I>,19{20	let attrs = attrs21		.iter()22		.filter(|a| a.path().is_ident(&ident))23		.collect::<Vec<_>>();24	if attrs.len() > 1 {25		return Err(Error::new(26			attrs[1].span(),27			"this attribute may be specified only once",28		));29	} else if attrs.is_empty() {30		return Ok(None);31	}32	let attr = attrs[0];33	let attr = attr.parse_args::<A>()?;3435	Ok(Some(attr))36}37fn remove_attr<I>(attrs: &mut Vec<Attribute>, ident: I)38where39	Ident: PartialEq<I>,40{41	attrs.retain(|a| !a.path().is_ident(&ident));42}4344fn path_is(path: &Path, needed: &str) -> bool {45	path.leading_colon.is_none()46		&& !path.segments.is_empty()47		&& path.segments.iter().last().unwrap().ident == needed48}4950fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {51	match ty {52		Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {53			let args = &path.path.segments.iter().last().unwrap().arguments;54			Some(args)55		}56		_ => None,57	}58}5960fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {61	let Some(args) = type_is_path(ty, "Option") else {62		return Ok(None);63	};64	// It should have only on angle-bracketed param ("<String>"):65	let PathArguments::AngleBracketed(params) = args else {66		return Err(Error::new(args.span(), "missing option generic"));67	};68	let generic_arg = params.args.iter().next().unwrap();69	// This argument must be a type:70	let GenericArgument::Type(ty) = generic_arg else {71		return Err(Error::new(72			generic_arg.span(),73			"option generic should be a type",74		));75	};76	Ok(Some(ty))77}7879struct Field {80	attrs: Vec<Attribute>,81	name: Ident,82	_colon: Token![:],83	ty: Type,84}85impl Parse for Field {86	fn parse(input: ParseStream) -> syn::Result<Self> {87		Ok(Self {88			attrs: input.call(Attribute::parse_outer)?,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!(alias);100	syn::custom_keyword!(flatten);101	syn::custom_keyword!(add);102	syn::custom_keyword!(hide);103	syn::custom_keyword!(ok);104}105106struct EmptyAttr;107impl Parse for EmptyAttr {108	fn parse(_input: ParseStream) -> Result<Self> {109		Ok(Self)110	}111}112113struct BuiltinAttrs {114	fields: Vec<Field>,115}116impl Parse for BuiltinAttrs {117	fn parse(input: ParseStream) -> syn::Result<Self> {118		if input.is_empty() {119			return Ok(Self { fields: Vec::new() });120		}121		input.parse::<kw::fields>()?;122		let fields;123		parenthesized!(fields in input);124		let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;125		Ok(Self {126			fields: p.into_iter().collect(),127		})128	}129}130131enum Optionality {132	Required,133	Optional,134	Default(Expr),135}136137enum ArgInfo {138	Normal {139		ty: Box<Type>,140		optionality: Optionality,141		name: Option<String>,142		cfg_attrs: Vec<Attribute>,143	},144	Lazy {145		is_option: bool,146		name: Option<String>,147	},148	Context,149	Location,150	This,151}152153impl ArgInfo {154	fn parse(name: &str, arg: &mut FnArg) -> Result<Self> {155		let FnArg::Typed(arg) = arg else {156			unreachable!()157		};158		let ident = match &arg.pat as &Pat {159			Pat::Ident(i) => Some(i.ident.clone()),160			_ => None,161		};162		let ty = &arg.ty;163		if type_is_path(ty, "Context").is_some() {164			return Ok(Self::Context);165		} else if type_is_path(ty, "CallLocation").is_some() {166			return Ok(Self::Location);167		} else if type_is_path(ty, "Thunk").is_some() {168			return Ok(Self::Lazy {169				is_option: false,170				name: ident.map(|v| v.to_string()),171			});172		}173174		match ty as &Type {175			Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),176			_ => {}177		}178179		let (optionality, ty) = if let Some(default) = parse_attr::<_, _>(&arg.attrs, "default")? {180			remove_attr(&mut arg.attrs, "default");181			(Optionality::Default(default), ty.clone())182		} else if let Some(ty) = extract_type_from_option(ty)? {183			if type_is_path(ty, "Thunk").is_some() {184				return Ok(Self::Lazy {185					is_option: true,186					name: ident.map(|v| v.to_string()),187				});188			}189190			(Optionality::Optional, Box::new(ty.clone()))191		} else {192			(Optionality::Required, ty.clone())193		};194195		let cfg_attrs = arg196			.attrs197			.iter()198			.filter(|a| a.path().is_ident("cfg"))199			.cloned()200			.collect();201202		Ok(Self::Normal {203			ty,204			optionality,205			name: ident.map(|v| v.to_string()),206			cfg_attrs,207		})208	}209}210211#[proc_macro_attribute]212pub fn builtin(213	attr: proc_macro::TokenStream,214	item: proc_macro::TokenStream,215) -> proc_macro::TokenStream {216	let attr = parse_macro_input!(attr as BuiltinAttrs);217	let item_fn = parse_macro_input!(item as ItemFn);218219	match builtin_inner(attr, item_fn) {220		Ok(v) => v.into(),221		Err(e) => e.into_compile_error().into(),222	}223}224225#[allow(clippy::too_many_lines)]226fn builtin_inner(attr: BuiltinAttrs, mut fun: ItemFn) -> syn::Result<TokenStream> {227	let ReturnType::Type(_, result) = &fun.sig.output else {228		return Err(Error::new(229			fun.sig.span(),230			"builtin should return something",231		));232	};233234	let name = fun.sig.ident.to_string();235	let args = fun236		.sig237		.inputs238		.iter_mut()239		.map(|arg| ArgInfo::parse(&name, arg))240		.collect::<Result<Vec<_>>>()?;241242	let params_desc = args.iter().filter_map(|a| match a {243		ArgInfo::Normal {244			optionality,245			name,246			cfg_attrs,247			..248		} => {249			let name = name250				.as_ref()251				.map_or_else(|| quote! {None}, |n| quote! {ParamName::new_static(#n)});252			let default = match optionality {253				Optionality::Required => quote!(ParamDefault::None),254				Optionality::Optional => quote!(ParamDefault::Exists),255				Optionality::Default(e) => quote!(ParamDefault::Literal(stringify!(#e))),256			};257			Some(quote! {258				#(#cfg_attrs)*259				BuiltinParam::new(#name, #default),260			})261		}262		ArgInfo::Lazy { is_option, name } => {263			let name = name264				.as_ref()265				.map_or_else(|| quote! {None}, |n| quote! {ParamName::new_static(#n)});266			Some(quote! {267				BuiltinParam::new(#name, ParamDefault::exists(#is_option)),268			})269		}270		ArgInfo::Context | ArgInfo::Location | ArgInfo::This => None,271	});272273	let mut id = 0usize;274	let pass = args275		.iter()276		.map(|a| match a {277			ArgInfo::Normal { .. } | ArgInfo::Lazy { .. } => {278				let cid = id;279				id += 1;280				(quote! {#cid}, a)281			}282			ArgInfo::Context | ArgInfo::Location | ArgInfo::This => {283				(quote! {compile_error!("should not use id")}, a)284			}285		})286		.map(|(id, a)| match a {287			ArgInfo::Normal {288				ty,289				optionality,290				name,291				cfg_attrs,292			} => {293				let name = name.as_ref().map_or("<unnamed>", String::as_str);294				let eval = quote! {jrsonnet_evaluator::in_description_frame(295					|| format!("argument <{}> evaluation", #name),296					|| <#ty>::from_untyped(value.evaluate()?),297				)?};298				let value = match optionality {299					Optionality::Required => quote! {{300						let value = parsed[#id].as_ref().expect("args shape is checked");301						#eval302					},},303					Optionality::Optional => quote! {if let Some(value) = &parsed[#id] {304						Some(#eval)305					} else {306						None307					},},308					Optionality::Default(expr) => quote! {if let Some(value) = &parsed[#id] {309						#eval310					} else {311						let v: #ty = #expr;312						v313					},},314				};315				quote! {316					#(#cfg_attrs)*317					#value318				}319			}320			ArgInfo::Lazy { is_option, .. } => {321				if *is_option {322					quote! {if let Some(value) = &parsed[#id] {323						Some(value.clone())324					} else {325						None326					},}327				} else {328					quote! {329						parsed[#id].as_ref().expect("args shape is correct").clone(),330					}331				}332			}333			ArgInfo::Context => quote! {ctx.clone(),},334			ArgInfo::Location => quote! {location,},335			ArgInfo::This => quote! {self,},336		});337338	let fields = attr.fields.iter().map(|field| {339		let attrs = &field.attrs;340		let name = &field.name;341		let ty = &field.ty;342		quote! {343			#(#attrs)*344			pub #name: #ty,345		}346	});347348	let name = &fun.sig.ident;349	let vis = &fun.vis;350	let static_ext = if attr.fields.is_empty() {351		quote! {352			impl #name {353				pub const INST: &'static dyn StaticBuiltin = &#name {};354			}355			impl StaticBuiltin for #name {}356		}357	} else {358		quote! {}359	};360	let static_derive_copy = if attr.fields.is_empty() {361		quote! {, Copy}362	} else {363		quote! {}364	};365366	Ok(quote! {367		#fun368369		#[doc(hidden)]370		#[allow(non_camel_case_types)]371		#[derive(Clone, jrsonnet_gcmodule::Trace #static_derive_copy)]372		#vis struct #name {373			#(#fields)*374		}375		const _: () = {376			use ::jrsonnet_evaluator::{377				State, Val,378				function::{builtin::{Builtin, StaticBuiltin, BuiltinParam, ParamName, ParamDefault}, CallLocation, ArgsLike, parse::parse_builtin_call},379				Result, Context, typed::Typed,380				parser::Span,381			};382			const PARAMS: &'static [BuiltinParam] = &[383				#(#params_desc)*384			];385386			#static_ext387			impl Builtin for #name388			where389				Self: 'static390			{391				fn name(&self) -> &str {392					stringify!(#name)393				}394				fn params(&self) -> &[BuiltinParam] {395					PARAMS396				}397				#[allow(unused_variables)]398				fn call(&self, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {399					let parsed = parse_builtin_call(ctx.clone(), &PARAMS, args, false)?;400401					let result: #result = #name(#(#pass)*);402					<_ as Typed>::into_result(result)403				}404				fn as_any(&self) -> &dyn ::std::any::Any {405					self406				}407			}408		};409	})410}411412#[derive(Default)]413#[allow(clippy::struct_excessive_bools)]414struct TypedAttr {415	rename: Option<String>,416	aliases: Vec<String>,417	flatten: bool,418	/// flatten(ok) strategy for flattened optionals419	/// field would be None in case of any parsing error (as in serde)420	flatten_ok: bool,421	// Should it be `field+:` instead of `field:`422	add: bool,423	// Should it be `field::` instead of `field:`424	hide: bool,425}426impl Parse for TypedAttr {427	fn parse(input: ParseStream) -> syn::Result<Self> {428		let mut out = Self::default();429		loop {430			let lookahead = input.lookahead1();431			if lookahead.peek(kw::rename) {432				input.parse::<kw::rename>()?;433				input.parse::<Token![=]>()?;434				let name = input.parse::<LitStr>()?;435				if out.rename.is_some() {436					return Err(Error::new(437						name.span(),438						"rename attribute may only be specified once",439					));440				}441				out.rename = Some(name.value());442			} else if lookahead.peek(kw::alias) {443				input.parse::<kw::alias>()?;444				input.parse::<Token![=]>()?;445				let alias = input.parse::<LitStr>()?;446				out.aliases.push(alias.value());447			} else if lookahead.peek(kw::flatten) {448				input.parse::<kw::flatten>()?;449				out.flatten = true;450				if input.peek(token::Paren) {451					let content;452					parenthesized!(content in input);453					let lookahead = content.lookahead1();454					if lookahead.peek(kw::ok) {455						content.parse::<kw::ok>()?;456						out.flatten_ok = true;457					} else {458						return Err(lookahead.error());459					}460				}461			} else if lookahead.peek(kw::add) {462				input.parse::<kw::add>()?;463				out.add = true;464			} else if lookahead.peek(kw::hide) {465				input.parse::<kw::hide>()?;466				out.hide = true;467			} else if input.is_empty() {468				break;469			} else {470				return Err(lookahead.error());471			}472			if input.peek(Token![,]) {473				input.parse::<Token![,]>()?;474			} else {475				break;476			}477		}478		Ok(out)479	}480}481482struct TypedField {483	attr: TypedAttr,484	ident: Ident,485	ty: Type,486	is_option: bool,487	is_lazy: bool,488}489impl TypedField {490	fn parse(field: &syn::Field) -> Result<Self> {491		let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();492		let Some(ident) = field.ident.clone() else {493			return Err(Error::new(494				field.span(),495				"this field should appear in output object, but it has no visible name",496			));497		};498		let (is_option, ty) = extract_type_from_option(&field.ty)?499			.map_or_else(|| (false, field.ty.clone()), |ty| (true, ty.clone()));500		if is_option && attr.flatten {501			if !attr.flatten_ok {502				return Err(Error::new(503					field.span(),504					"strategy should be set when flattening Option",505				));506			}507		} else if attr.flatten_ok {508			return Err(Error::new(509				field.span(),510				"flatten(ok) is only useable on optional fields",511			));512		}513514		let is_lazy = type_is_path(&ty, "Thunk").is_some();515516		Ok(Self {517			attr,518			ident,519			ty,520			is_option,521			is_lazy,522		})523	}524	/// None if this field is flattened in jsonnet output525	fn name(&self) -> Option<String> {526		if self.attr.flatten {527			return None;528		}529		Some(530			self.attr531				.rename532				.clone()533				.unwrap_or_else(|| self.ident.to_string()),534		)535	}536537	fn expand_field(&self) -> Option<TokenStream> {538		if self.is_option {539			return None;540		}541		let name = self.name()?;542		let ty = &self.ty;543		Some(quote! {544			(#name, <#ty as Typed>::TYPE)545		})546	}547548	fn expand_parse(&self) -> TokenStream {549		if self.is_option {550			self.expand_parse_optional()551		} else {552			self.expand_parse_mandatory()553		}554	}555556	fn expand_parse_optional(&self) -> TokenStream {557		let ident = &self.ident;558		let ty = &self.ty;559560		// optional flatten is handled in same way as serde561		if self.attr.flatten {562			return quote! {563				#ident: <#ty as TypedObj>::parse(&obj).ok(),564			};565		}566567		let name = self.name().unwrap();568		let aliases = &self.attr.aliases;569570		quote! {571			#ident: {572				let __value = if let Some(__v) = obj.get(#name.into())? {573					Some(__v)574				} #(else if let Some(__v) = obj.get(#aliases.into())? {575					Some(__v)576				})* else {577					None578				};579580				__value.map(<#ty as Typed>::from_untyped).transpose()?581			},582		}583	}584585	fn expand_parse_mandatory(&self) -> TokenStream {586		let ident = &self.ident;587		let ty = &self.ty;588589		// optional flatten is handled in same way as serde590		if self.attr.flatten {591			return quote! {592				#ident: <#ty as TypedObj>::parse(&obj)?,593			};594		}595596		let name = self.name().unwrap();597		let aliases = &self.attr.aliases;598599		let error_text = if aliases.is_empty() {600			// clippy does not understand name variable usage in quote! macro601			#[allow(clippy::redundant_clone)]602			name.clone()603		} else {604			format!("{name} (alias {})", aliases.join(", "))605		};606607		quote! {608			#ident: {609				let __value = if let Some(__v) = obj.get(#name.into())? {610					__v611				} #(else if let Some(__v) = obj.get(#aliases.into())? {612					__v613				})* else {614					return Err(ErrorKind::NoSuchField(#error_text.into(), vec![]).into());615				};616617				<#ty as Typed>::from_untyped(__value)?618			},619		}620	}621622	fn expand_serialize(&self) -> TokenStream {623		let ident = &self.ident;624		let ty = &self.ty;625		self.name().map_or_else(626			|| {627				if self.is_option {628					quote! {629						if let Some(value) = self.#ident {630							<#ty as TypedObj>::serialize(value, out)?;631						}632					}633				} else {634					quote! {635						<#ty as TypedObj>::serialize(self.#ident, out)?;636					}637				}638			},639			|name| {640				let hide = if self.attr.hide {641					quote! {.hide()}642				} else {643					quote! {}644				};645				let add = if self.attr.add {646					quote! {.add()}647				} else {648					quote! {}649				};650				let value = if self.is_lazy {651					quote! {652						out.field(#name)653							#hide654							#add655							.try_thunk(<#ty as Typed>::into_lazy_untyped(value))?;656					}657				} else {658					quote! {659						out.field(#name)660							#hide661							#add662							.try_value(<#ty as Typed>::into_untyped(value)?)?;663					}664				};665				if self.is_option {666					quote! {667						if let Some(value) = self.#ident {668							#value669						}670					}671				} else {672					quote! {673						{674							let value = self.#ident;675							#value676						}677					}678				}679			},680		)681	}682}683684#[proc_macro_derive(Typed, attributes(typed))]685pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {686	let input = parse_macro_input!(item as DeriveInput);687688	match derive_typed_inner(input) {689		Ok(v) => v.into(),690		Err(e) => e.to_compile_error().into(),691	}692}693694fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {695	let syn::Data::Struct(data) = &input.data else {696		return Err(Error::new(input.span(), "only structs supported"));697	};698699	let ident = &input.ident;700	let fields = data701		.fields702		.iter()703		.map(TypedField::parse)704		.collect::<Result<Vec<_>>>()?;705706	let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();707708	let typed = {709		let fields = fields710			.iter()711			.filter_map(TypedField::expand_field)712			.collect::<Vec<_>>();713		quote! {714			impl #impl_generics Typed for #ident #ty_generics #where_clause {715				const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[716					#(#fields,)*717				]);718719				fn from_untyped(value: Val) -> JrResult<Self> {720					let obj = value.as_obj().expect("shape is correct");721					Self::parse(&obj)722				}723724				fn into_untyped(value: Self) -> JrResult<Val> {725					let mut out = ObjValueBuilder::new();726					value.serialize(&mut out)?;727					Ok(Val::Obj(out.build()))728				}729730			}731		}732	};733734	let fields_parse = fields.iter().map(TypedField::expand_parse);735	let fields_serialize = fields736		.iter()737		.map(TypedField::expand_serialize)738		.collect::<Vec<_>>();739740	Ok(quote! {741		const _: () = {742			use ::jrsonnet_evaluator::{743				typed::{ComplexValType, Typed, TypedObj, CheckType},744				Val, State,745				error::{ErrorKind, Result as JrResult},746				ObjValueBuilder, ObjValue,747			};748749			#typed750751			impl #impl_generics TypedObj for #ident #ty_generics #where_clause {752				fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {753					#(#fields_serialize)*754755					Ok(())756				}757				fn parse(obj: &ObjValue) -> JrResult<Self> {758					Ok(Self {759						#(#fields_parse)*760					})761				}762			}763		};764	})765}766767struct FormatInput {768	formatting: LitStr,769	arguments: Vec<Expr>,770}771impl Parse for FormatInput {772	fn parse(input: ParseStream) -> Result<Self> {773		let formatting = input.parse()?;774		let mut arguments = Vec::new();775776		while input.peek(Token![,]) {777			input.parse::<Token![,]>()?;778			if input.is_empty() {779				// Trailing comma780				break;781			}782			let expr = input.parse()?;783			arguments.push(expr);784		}785786		if !input.is_empty() {787			return Err(syn::Error::new(input.span(), "unexpected trailing input"));788		}789790		Ok(Self {791			formatting,792			arguments,793		})794	}795}796fn is_format_str(i: &str) -> bool {797	let mut is_plain = true;798	// -1 = {799	// +1 = }800	let mut is_bracket = 0i8;801	for ele in i.chars() {802		match ele {803			'{' if is_bracket == -1 => {804				is_bracket = 0;805			}806			'}' if is_bracket == -1 => {807				is_plain = false;808				break;809			}810			'}' if is_bracket == 1 => {811				is_bracket = 0;812			}813			'{' if is_bracket == 1 => {814				is_plain = false;815				break;816			}817			'{' => {818				is_bracket = -1;819			}820			'}' => {821				is_bracket = 1;822			}823			_ if is_bracket != 0 => {824				is_plain = false;825				break;826			}827			_ => {}828		}829	}830	!is_plain || is_bracket != 0831}832impl FormatInput {833	fn expand(self) -> TokenStream {834		let format = self.formatting;835		if is_format_str(&format.value()) {836			let args = self.arguments;837			quote! {838				::jrsonnet_evaluator::IStr::from(format!(#format #(, #args)*))839			}840		} else {841			if let Some(first) = self.arguments.first() {842				return syn::Error::new(843					first.span(),844					"string has no formatting codes, it should not have the arguments",845				)846				.into_compile_error();847			}848			quote! {849				::jrsonnet_evaluator::IStr::from(#format)850			}851		}852	}853}854855/// `IStr` formatting helper856///857/// Using `format!("literal with no codes").into()` is slower than just `"literal with no codes".into()`858/// This macro looks for formatting codes in the input string, and uses859/// `format!()` only when necessary860#[proc_macro]861pub fn format_istr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {862	let input = parse_macro_input!(input as FormatInput);863	input.expand().into()864}865866/// Create Thunk using closure syntax867#[proc_macro]868#[allow(non_snake_case)]869pub fn Thunk(input: proc_macro::TokenStream) -> proc_macro::TokenStream {870	let input = parse_macro_input!(input as ExprClosure);871872	let span = input.inputs.span();873	let move_check = input.capture.is_none().then(|| {874		quote_spanned! {span => {875			compile_error!("Thunk! needs to be called with move closure");876		}}877	});878879	let (env, closure, args) = syn_dissect_closure::split_env(input);880881	let trace_check = args.iter().map(|el| {882		let span = el.span();883		quote_spanned! {span => ::jrsonnet_evaluator::gc::assert_trace(&#el);}884	});885886	quote! {{887		#move_check888		#(#trace_check)*889		::jrsonnet_evaluator::Thunk::new(::jrsonnet_evaluator::val::ThunkValueClosure::new(#env, #closure))890	}}.into()891}