git.delta.rocks / jrsonnet / refs/commits / 393dcbd419fd

difftreelog

feat(macro) pass call location to builtins

Yaroslav Bolyukin2021-12-26parent: #a5471f2.patch.diff
in: master

8 files changed

addedcmds/jrsonnet-fmt/Cargo.tomldiffbeforeafterboth
--- /dev/null
+++ b/cmds/jrsonnet-fmt/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "jrsonnet-fmt"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
+dprint-core = "0.47.1"
+jrsonnet-parser = { path = "../../crates/jrsonnet-parser" }
addedcmds/jrsonnet-fmt/src/main.rsdiffbeforeafterboth
after · cmds/jrsonnet-fmt/src/main.rs
1use std::path::PathBuf;23use dprint_core::formatting::{PrintItems, PrintOptions, Signal};4use jrsonnet_parser::{5	ArgsDesc, BinaryOpType, BindSpec, Expr, FieldName, LocExpr, Member, ObjBody, Param, ParamsDesc,6	ParserSettings, Visibility,7};89pub trait Printable {10	fn print(&self) -> PrintItems;11}1213macro_rules! pi {14	(@i; $($t:tt)*) => {{15		let mut o = PrintItems::new();16		pi!(@s; o: $($t)*);17		o18	}};19	(@s; $o:ident: str($e:expr) $($t:tt)*) => {{20		$o.push_str($e);21		pi!(@s; $o: $($t)*);22	}};23	(@s; $o:ident: nl $($t:tt)*) => {{24		$o.push_signal(Signal::NewLine);25		pi!(@s; $o: $($t)*);26	}};27	(@s; $o:ident: >i $($t:tt)*) => {{28		$o.push_signal(Signal::StartIndent);29		pi!(@s; $o: $($t)*);30	}};31	(@s; $o:ident: <i $($t:tt)*) => {{32		$o.push_signal(Signal::FinishIndent);33		pi!(@s; $o: $($t)*);34	}};35	(@s; $o:ident: {$expr:expr} $($t:tt)*) => {{36		$o.extend($expr.print());37		pi!(@s; $o: $($t)*);38	}};39	(@s; $o:ident: if ($e:expr)($($then:tt)*) $($t:tt)*) => {{40		if $e {41			pi!(@s; $o: $($then)*);42		}43		pi!(@s; $o: $($t)*);44	}};45	(@s; $o:ident: ifelse ($e:expr)($($then:tt)*)($($else:tt)*) $($t:tt)*) => {{46		if $e {47			pi!(@s; $o: $($then)*);48		} else {49			pi!(@s; $o: $($else)*);50		}51		pi!(@s; $o: $($t)*);52	}};53	(@s; $i:ident:) => {}54}55macro_rules! p {56	(new: $($t:tt)*) => {57		pi!(@i; $($t)*)58	};59	($o:ident: $($t:tt)*) => {60		pi!(@s; $o: $($t)*)61	};62}6364impl Printable for FieldName {65	fn print(&self) -> PrintItems {66		match self {67			FieldName::Fixed(f) => {68				p!(new: str(&f))69			}70			FieldName::Dyn(_) => todo!(),71		}72	}73}7475impl Printable for Visibility {76	fn print(&self) -> PrintItems {77		match self {78			Visibility::Normal => p!(new: str(":")),79			Visibility::Hidden => p!(new: str("::")),80			Visibility::Unhide => p!(new: str(":::")),81		}82	}83}8485impl Printable for BinaryOpType {86	fn print(&self) -> PrintItems {87		let o = self.to_string();88		p!(new: str(&o))89	}90}9192impl<T: Printable> Printable for Option<T> {93	fn print(&self) -> PrintItems {94		if let Some(v) = self {95			v.print()96		} else {97			PrintItems::new()98		}99	}100}101102impl Printable for Param {103	fn print(&self) -> PrintItems {104		p!(new:105			str(&self.0)106			if(self.1.is_some())(str(" = ") {self.1})107		)108	}109}110111impl Printable for ParamsDesc {112	fn print(&self) -> PrintItems {113		let mut out = PrintItems::new();114		for (i, item) in self.0.iter().enumerate() {115			if i != 0 {116				p!(out: str(", "));117			}118			out.extend(item.print());119		}120		out121	}122}123124impl Printable for ArgsDesc {125	fn print(&self) -> PrintItems {126		let mut out = PrintItems::new();127		let mut first = Some(());128		for u in self.unnamed.iter() {129			if first.take().is_none() {130				p!(out: str(", "));131			}132			p!(out: {u})133		}134		for (n, u) in self.named.iter() {135			if first.take().is_none() {136				p!(out: str(", "));137			}138			p!(out: str(&n) str(" = ") {u})139		}140141		out142	}143}144145impl Printable for BindSpec {146	fn print(&self) -> PrintItems {147		p!(new: str(&self.name) if(self.params.is_some())(str("(") {self.params} str(")")) str(" = ") {self.value})148	}149}150151struct StrExpr<'s>(&'s str);152153impl<'s> Printable for StrExpr<'s> {154	fn print(&self) -> PrintItems {155		todo!()156	}157}158159impl Printable for ObjBody {160	fn print(&self) -> PrintItems {161		let mut pi = PrintItems::new();162		p!(pi: str("{"));163		match self {164			ObjBody::MemberList(m) => {165				if !m.is_empty() {166					p!(pi: nl > i);167					for m in m {168						match m {169							Member::Field(f) => {170								p!(pi:171									{f.name} {f.params}172									if(f.plus)(str("+"))173									{f.visibility} str(" ")174									{f.value}175									str(",") nl176								);177							}178							Member::BindStmt(s) => {179								p!(pi: str("local ") {s} str(",") nl)180							}181							Member::AssertStmt(a) => p!(pi: str("assert ") {a.0} if(a.1.is_some())(182								str(" : ") {a.1}183							) str(",") nl),184						}185					}186					p!(pi: <i);187				} else {188					p!(pi: str(" "))189				}190			}191			ObjBody::ObjComp(_) => todo!(),192		}193		p!(pi: str("}"));194		pi195	}196}197198impl Printable for Expr {199	fn print(&self) -> PrintItems {200		let mut pi = PrintItems::new();201		match self {202			Expr::Literal(l) => match l {203				jrsonnet_parser::LiteralType::This => p!(pi: str("self")),204				jrsonnet_parser::LiteralType::Super => p!(pi: str("super")),205				jrsonnet_parser::LiteralType::Dollar => p!(pi: str("$")),206				jrsonnet_parser::LiteralType::Null => p!(pi: str("null")),207				jrsonnet_parser::LiteralType::True => p!(pi: str("true")),208				jrsonnet_parser::LiteralType::False => p!(pi: str("false")),209			},210			Expr::Str(s) => {211				p!(pi: str("\"") str(s) str("\""))212			}213			Expr::Num(n) => {214				let n = n.to_string();215				p!(pi: str(&n));216			}217			Expr::Var(v) => p!(pi: str(&v)),218			Expr::Arr(a) => {219				p!(pi: str("["));220				for (i, v) in a.iter().enumerate() {221					if i != 0 {222						p!(pi: str(", "));223					}224					p!(pi: {v})225				}226				p!(pi: str("]"));227			}228			Expr::ArrComp(_, _) => todo!(),229			Expr::Obj(o) => {230				p!(pi: {o});231			}232			Expr::ObjExtend(a, b) => p!(pi: {a} str(" ") {b}),233			Expr::Parened(v) => {234				if let Expr::Parened(_) = &v.0 as &Expr {235					p!(pi: {v})236				} else {237					p!(pi: str("(") {v} str(")"))238				}239			}240			Expr::UnaryOp(_, _) => todo!(),241			Expr::BinaryOp(a, o, b) => {242				p!(pi:243					{a} str(" ") if(!matches!(&b.0 as &Expr, Expr::Obj(_)))({o} str(" ")) {b}244				)245			}246			Expr::AssertExpr(_, _) => todo!(),247			Expr::LocalExpr(s, v) => {248				p!(pi:249					str("local") nl >i250				);251				for spec in s.iter() {252					p!(pi: {spec} str(";") nl)253				}254				p!(pi:255					<i256					{v}257				);258			}259			Expr::Import(i) => {260				let v = i.to_str().unwrap();261				p!(pi: str("import \"") str(&v) str("\""));262			}263			Expr::ImportStr(_) => todo!(),264			Expr::ErrorStmt(_) => todo!(),265			Expr::Apply(f, a, t) => p!(pi:266				{f} str("(") {a} str(")") if(*t)(str("tailstrict"))267			),268			Expr::Index(a, b) => p!(pi: {a} str("[") {b} str("]")),269			Expr::Function(_, _) => todo!(),270			Expr::Intrinsic(_) => todo!(),271			Expr::IfElse {272				cond,273				cond_then,274				cond_else,275			} => p!(pi:276				str("if ") {cond.0} str(" then") ifelse(cond_else.is_some())(277					nl >i278						{cond_then} nl279					<i str("else") nl >i280						{cond_else}281					<i282				)(str(" ") {cond_then})283			),284			Expr::Slice(v, d) => {285				p!(pi:286					{v}287					str("[") {d.start} str(":") {d.end}288					if(d.step.is_some())(289						str(":")290						{d.step}291					)292					str("]")293				)294			}295		}296		pi297	}298}299300impl Printable for LocExpr {301	fn print(&self) -> PrintItems {302		self.0.print()303	}304}305306fn main() {307	let parsed = jrsonnet_parser::parse(308		r#"309	310	311		# Edit me!312		local b = import "b.libsonnet";  # comment313		local a = import "a.libsonnet";314		315			 local f(x,y)=x+y;316		317		318		local Template = {z: "foo"};319		320		Template + {321						local322323					h = 3,324					assert self.a == 1325		  326					: "error",327		"f": ((((((3)))))) ,328		"g g":329		f(4,2),330		arr: [[331		  1, 2,332		  ],333		  3,334		  {335			  b: {336				  c: {337					  k: [16]338				  }339			  }340		  }341		  ],342		  m: a[1::],343		  m: b[::],344		  k: if a         == b    then 345346347		  2348349		  else Template {}350		}351		352	353"#,354		&ParserSettings {355			file_name: PathBuf::from("example").into(),356		},357	)358	.unwrap();359360	let o = dprint_core::formatting::format(361		|| {362			let print_items = parsed.print();363			print_items364		},365		PrintOptions {366			indent_width: 2,367			max_width: 100,368			use_tabs: false,369			new_line_text: "\n",370		},371	);372	println!("{}", o);373}
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -5,14 +5,13 @@
 	equals,
 	error::{Error::*, Result},
 	operator::evaluate_mod_op,
-	parse_args, primitive_equals, push_frame, throw, with_state, ArrValue, Context, FuncVal,
+	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_types::ty;
 use serde::Deserialize;
 use serde_yaml::DeserializingQuirks;
 use std::{
@@ -466,19 +465,19 @@
 	Ok(format!("{:x}", md5::compute(&str.as_bytes())))
 }
 
-fn builtin_trace(context: Context, loc: &ExprLocation, args: &ArgsDesc) -> Result<Val> {
-	parse_args!(context, "trace", args, 2, [
-		0, str: ty!(string) => Val::Str;
-		1, rest: ty!(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);
-			});
-		eprintln!(" {}", str);
-		Ok(rest)
-	})
+#[jrsonnet_macros::builtin]
+fn builtin_trace(#[location] loc: &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
+		);
+	});
+	eprintln!(" {}", str);
+	Ok(rest) as Result<Any>
 }
 
 #[jrsonnet_macros::builtin]
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -16,8 +16,6 @@
 pub enum Error {
 	#[error("intrinsic not found: {0}")]
 	IntrinsicNotFound(IStr),
-	#[error("argument reordering in intrisics not supported yet")]
-	IntrinsicArgumentReorderingIsNotSupportedYet,
 
 	#[error("operator {0} does not operate on type {1}")]
 	UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),
modifiedcrates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -311,45 +311,3 @@
 
 	Ok(body_ctx.extend(out, None, None, None))
 }
-
-#[macro_export]
-macro_rules! parse_args {
-	($ctx: expr, $fn_name: expr, $args: expr, $total_args: expr, [
-		$($id: expr, $name: ident: $ty: expr $(=>$match: path)?);+ $(;)?
-	], $handler:block) => {{
-		use $crate::{error::Error::*, throw, evaluate, push_description_frame, typed::CheckType};
-
-		let args = $args;
-		if args.unnamed.len() + args.named.len() > $total_args {
-			throw!(TooManyArgsFunctionHas($total_args));
-		}
-		$(
-			if args.unnamed.len() + args.named.len() <= $id {
-				throw!(FunctionParameterNotBoundInCall(stringify!($name).into()));
-			}
-			// Is named
-			let $name = if $id >= $args.unnamed.len() {
-				let named = &args.named[$id - $args.unnamed.len()];
-				if &named.0 != stringify!($name) {
-					throw!(IntrinsicArgumentReorderingIsNotSupportedYet);
-				}
-				&named.1
-			} else {
-				&$args.unnamed[$id]
-			};
-			let $name = push_description_frame(|| format!("evaluating builtin argument {}", stringify!($name)), || {
-				let value = evaluate($ctx.clone(), &$name)?;
-				$ty.check(&value)?;
-				Ok(value)
-			})?;
-			$(
-				let $name = if let $match(v) = $name {
-					v
-				} else {
-					unreachable!();
-				};
-			)?
-		)+
-		($handler as crate::Result<_>)
-	}};
-}
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -1,6 +1,10 @@
 use proc_macro2::Span;
 use quote::quote;
-use syn::{parse_macro_input, FnArg, Ident, ItemFn, Pat};
+use syn::{parse_macro_input, FnArg, Ident, ItemFn, Pat, PatType};
+
+fn is_location_arg(t: &PatType) -> bool {
+	t.attrs.iter().any(|a| a.path.is_ident("location"))
+}
 
 #[proc_macro_attribute]
 pub fn builtin(
@@ -8,14 +12,11 @@
 	item: proc_macro::TokenStream,
 ) -> proc_macro::TokenStream {
 	// syn::ItemFn::parse(input)
-	let fun: ItemFn = parse_macro_input!(item);
+	let mut fun: ItemFn = parse_macro_input!(item);
 
-	let inner_name = Ident::new("inner", Span::call_site());
-	let mut inner_fun = fun.clone();
-	inner_fun.sig.ident = inner_name.clone();
 	let result = match fun.sig.output {
 		syn::ReturnType::Default => panic!("builtin should return something"),
-		syn::ReturnType::Type(_, ty) => ty,
+		syn::ReturnType::Type(_, ref ty) => ty.clone(),
 	};
 
 	let params = fun
@@ -26,6 +27,7 @@
 			FnArg::Receiver(_) => unreachable!(),
 			FnArg::Typed(t) => t,
 		})
+		.filter(|a| !is_location_arg(a))
 		.map(|t| {
 			let ident = match &t.pat as &Pat {
 				Pat::Ident(i) => i.ident.to_string(),
@@ -39,38 +41,53 @@
 					has_default: #optional,
 				}
 			}
-		});
+		})
+		.collect::<Vec<_>>();
 
 	let args = fun
 		.sig
 		.inputs
-		.iter()
+		.iter_mut()
 		.map(|i| match i {
 			FnArg::Receiver(_) => unreachable!(),
 			FnArg::Typed(t) => t,
 		})
 		.map(|t| {
-			let ident = match &t.pat as &Pat {
-				Pat::Ident(i) => i.ident.to_string(),
-				_ => panic!("only idents supported yet"),
-			};
-			let ty = &t.ty;
-			quote! {{
-				let value = parsed.get(#ident).unwrap();
+			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;
+			if is_location {
+				quote! {{
+					loc
+				}}
+			} else {
+				let ident = match &t.pat as &Pat {
+					Pat::Ident(i) => i.ident.to_string(),
+					_ => panic!("only idents supported yet"),
+				};
+				let ty = &t.ty;
+				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();
 
 	let attrs = &fun.attrs;
 	let vis = &fun.vis;
 	let name = &fun.sig.ident;
 	(quote! {
 		#(#attrs)*
-		#vis fn #name(context: Context, _loc: &ExprLocation, args: &ArgsDesc) -> Result<Val> {
+		#vis fn #name(context: Context, loc: &ExprLocation, args: &ArgsDesc) -> Result<Val> {
 			#inner_fun
 			use jrsonnet_evaluator::function::BuiltinParam;
 			const PARAMS: &'static [BuiltinParam] = &[
modifiedcrates/jrsonnet-types/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-types/src/lib.rs
+++ b/crates/jrsonnet-types/src/lib.rs
@@ -191,9 +191,9 @@
 				write!(f, "}}")?;
 			}
 			ComplexValType::Union(v) => write_union(f, true, v.iter())?,
-			ComplexValType::UnionRef(v) => write_union(f, true, v.iter().map(|v| *v))?,
+			ComplexValType::UnionRef(v) => write_union(f, true, v.iter().copied())?,
 			ComplexValType::Sum(v) => write_union(f, false, v.iter())?,
-			ComplexValType::SumRef(v) => write_union(f, false, v.iter().map(|v| *v))?,
+			ComplexValType::SumRef(v) => write_union(f, false, v.iter().copied())?,
 		};
 		Ok(())
 	}
modifiedflake.nixdiffbeforeafterboth
--- a/flake.nix
+++ b/flake.nix
@@ -1,21 +1,59 @@
 {
-  description = "Rust jsonnet implementation";
-
-  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
-  inputs.flake-utils.url = "github:numtide/flake-utils";
-
-  outputs = { self, nixpkgs, flake-utils }:
+  description = "Dotfiles manager";
+  inputs = {
+    nixpkgs.url = "github:nixos/nixpkgs";
+    flake-utils.url = "github:numtide/flake-utils";
+    naersk.url = "github:nix-community/naersk";
+    rust-overlay.url = "github:oxalica/rust-overlay";
+    pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
+  };
+  outputs = { self, nixpkgs, flake-utils, rust-overlay, pre-commit-hooks, naersk }:
     flake-utils.lib.eachDefaultSystem (system:
       let
-        pkgs = nixpkgs.legacyPackages.${system};
-        jrsonnet = pkgs.rustPlatform.buildRustPackage rec {
-          pname = "jrsonnet";
-          version = "0.1.0";
-          src = self;
-          cargoSha256 = "sha256-cez8pJ/uwj+PHAPQwpSB4CKaxcP8Uvv8xguOrVXR2xE=";
+        pkgs = import nixpkgs
+          {
+            inherit system;
+            overlays = [ rust-overlay.overlay ];
+          };
+        rust = ((pkgs.rustChannelOf { date = "2021-11-11"; channel = "nightly"; }).default.override {
+          extensions = [ "rust-src" ];
+        });
+        naersk-lib = naersk.lib."${system}".override {
+          rustc = rust;
+          cargo = rust;
+        };
+      in
+      rec {
+        checks = {
+          pre-commit-check = pre-commit-hooks.lib.${system}.run {
+            src = ./.;
+            hooks = {
+              nixpkgs-fmt.enable = true;
+            };
+          };
+        };
+        defaultPackage = naersk-lib.buildPackage {
+          pname = "dotman";
+          root = ./.;
+          buildInputs = with pkgs; [
+            pkgs.sqlite
+          ];
         };
-      in { 
-        defaultPackage = jrsonnet;
-        devShell = pkgs.mkShell {};
-      });
+        devShell = pkgs.mkShell {
+          inherit (checks.pre-commit-check) shellHook;
+          nativeBuildInputs = with pkgs;[
+            pkgs.binutils
+            pkgs.pkgconfig
+            pkgs.clang
+            pkgs.x11
+            pkgs.alsaLib
+            pkgs.libudev
+            pkgs.sqlite
+            rust
+            cargo-edit
+            go-jsonnet
+          ];
+        };
+      }
+    );
 }