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

difftreelog

chore rename executable to jrsonnet

Лач2020-06-06parent: #bcd9b11.patch.diff
in: master

6 files changed

addedcmds/jrsonnet/Cargo.tomldiffbeforeafterboth
--- /dev/null
+++ b/cmds/jrsonnet/Cargo.toml
@@ -0,0 +1,17 @@
+[package]
+name = "jrsonnet"
+version = "0.1.0"
+authors = ["Лач <iam@lach.pw>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+jsonnet-evaluator = { path = "../../crates/jsonnet-evaluator" }
+jsonnet-parser = { path = "../../crates/jsonnet-parser" }
+annotate-snippets = "0.8.0"
+
+[dependencies.clap]
+version = "3.0.0-beta.1"
+default-features = false
+features = ["std", "derive"]
addedcmds/jrsonnet/src/location.rsdiffbeforeafterboth
--- /dev/null
+++ b/cmds/jrsonnet/src/location.rs
@@ -0,0 +1,99 @@
+#[derive(Clone, PartialEq, Debug)]
+pub struct CodeLocation {
+	pub line: usize,
+	pub column: usize,
+
+	pub line_start_offset: usize,
+	pub line_end_offset: usize,
+}
+
+pub fn offset_to_location(file: &str, offsets: &[usize]) -> Vec<CodeLocation> {
+	if offsets.is_empty() {
+		return vec![];
+	}
+	let mut line = 1;
+	let mut column = 0;
+	let max_offset = *offsets.iter().max().unwrap();
+
+	let mut offset_map = offsets
+		.iter()
+		.enumerate()
+		.map(|(pos, offset)| (*offset, pos))
+		.collect::<Vec<_>>();
+	offset_map.sort_by_key(|v| v.0);
+	offset_map.reverse();
+
+	let mut out = vec![
+		CodeLocation {
+			column: 0,
+			line: 0,
+			line_start_offset: 0,
+			line_end_offset: 0
+		};
+		offsets.len()
+	];
+	let mut with_no_known_line_ending = vec![];
+	let mut this_line_offset = 0;
+	for (pos, ch) in file.chars().enumerate() {
+		column += 1;
+		match offset_map.last() {
+			Some(x) if x.0 == pos => {
+				let out_idx = x.1;
+				with_no_known_line_ending.push(out_idx);
+				out[out_idx].line = line;
+				out[out_idx].column = column;
+				out[out_idx].line_start_offset = this_line_offset + 1;
+				offset_map.pop();
+			}
+			_ => {}
+		}
+		if ch == '\n' {
+			line += 1;
+			column = 0;
+
+			for idx in with_no_known_line_ending.drain(..) {
+				out[idx].line_end_offset = pos;
+			}
+			this_line_offset = pos;
+
+			if pos == max_offset + 1 {
+				break;
+			}
+		}
+	}
+	let file_end = file.chars().count();
+	for idx in with_no_known_line_ending {
+		out[idx].line_end_offset = file_end;
+	}
+
+	out
+}
+
+#[cfg(test)]
+pub mod tests {
+	use super::{offset_to_location, CodeLocation};
+
+	#[test]
+	fn test() {
+		assert_eq!(
+			offset_to_location(
+				"hello world\n_______________________________________________________",
+				&[0, 14]
+			),
+			vec![
+				CodeLocation {
+					line: 1,
+					column: 1,
+					line_start_offset: 0,
+					line_end_offset: 11
+				},
+				CodeLocation {
+					line: 2,
+					column: 3,
+					line_start_offset: 11,
+					line_end_offset: 67
+				}
+			]
+		)
+	}
+}
addedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- /dev/null
+++ b/cmds/jrsonnet/src/main.rs
@@ -0,0 +1,269 @@
+pub mod location;
+
+use clap::Clap;
+use jsonnet_evaluator::{EvaluationState, LocError, StackTrace, Val};
+use location::{offset_to_location, CodeLocation};
+use std::env::current_dir;
+use std::{path::PathBuf, str::FromStr};
+
+enum Format {
+	None,
+	Json,
+	Yaml,
+}
+
+impl FromStr for Format {
+	type Err = &'static str;
+	fn from_str(s: &str) -> Result<Self, Self::Err> {
+		Ok(match s {
+			"none" => Format::None,
+			"json" => Format::Json,
+			"yaml" => Format::Yaml,
+			_ => return Err("no such format"),
+		})
+	}
+}
+
+#[derive(PartialEq)]
+enum TraceFormat {
+	CppJsonnet,
+	GoJsonnet,
+	Custom,
+}
+impl FromStr for TraceFormat {
+	type Err = &'static str;
+	fn from_str(s: &str) -> Result<Self, Self::Err> {
+		Ok(match s {
+			"cpp" => TraceFormat::CppJsonnet,
+			"go" => TraceFormat::GoJsonnet,
+			"default" => TraceFormat::Custom,
+			_ => return Err("no such format"),
+		})
+	}
+}
+
+#[derive(Clap)]
+#[clap(version = "0.1.0", author = "Lach <iam@lach.pw>")]
+struct Opts {
+	#[clap(long, about = "Disable global std variable")]
+	no_stdlib: bool,
+	#[clap(long, about = "Add external string")]
+	ext_str: Option<Vec<String>>,
+	#[clap(long, about = "Add external string from code")]
+	ext_code: Option<Vec<String>>,
+	#[clap(long, about = "Add TLA")]
+	tla_str: Option<Vec<String>>,
+	#[clap(long, about = "Add TLA from code")]
+	tla_code: Option<Vec<String>>,
+	#[clap(long, short = "f", default_value = "json", possible_values = &["none", "json", "yaml"], about = "Output format, wraps resulting value to corresponding std.manifest call")]
+	format: Format,
+	#[clap(long, default_value = "default", possible_values = &["cpp", "go", "default"], about = "Emulated needed stacktrace display")]
+	trace_format: TraceFormat,
+
+	#[clap(
+		long,
+		short = "s",
+		default_value = "200",
+		about = "Number of allowed stack frames"
+	)]
+	max_stack: usize,
+	#[clap(
+		long,
+		short = "t",
+		default_value = "20",
+		about = "Max length of stack trace before cropping"
+	)]
+	max_trace: usize,
+
+	#[clap(about = "File to compile", index = 1)]
+	input: String,
+}
+
+fn main() {
+	let opts: Opts = Opts::parse();
+	let evaluator = jsonnet_evaluator::EvaluationState::default();
+	if !opts.no_stdlib {
+		evaluator.add_stdlib();
+	}
+	let mut input = current_dir().unwrap();
+	input.push(opts.input.clone());
+	let code_string = String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap();
+	if let Err(e) = evaluator.add_file(input.clone(), code_string.clone()) {
+		print_syntax_error(e, &input, &code_string);
+		std::process::exit(2);
+	}
+	let result = evaluator.evaluate_file(&input);
+	match result {
+		Ok(v) => {
+			let v = match opts.format {
+				Format::Json => {
+					if opts.no_stdlib {
+						evaluator.add_stdlib();
+					}
+					evaluator.add_global("__tmp__to_json__".to_owned(), v);
+					let v = evaluator
+						.parse_evaluate_raw("std.manifestJsonEx(__tmp__to_json__, \"  \")");
+					match v {
+						Ok(v) => v,
+						Err(err) => {
+							print_error(&err, evaluator, &opts);
+							std::process::exit(2);
+						}
+					}
+				}
+				Format::Yaml => {
+					if opts.no_stdlib {
+						evaluator.add_stdlib();
+					}
+					evaluator.add_global("__tmp__to_yaml__".to_owned(), v);
+					let v = evaluator
+						.parse_evaluate_raw("std.manifestYamlDoc(__tmp__to_yaml__, \"  \")");
+					match v {
+						Ok(v) => v,
+						Err(err) => {
+							print_error(&err, evaluator, &opts);
+							std::process::exit(2);
+						}
+					}
+				}
+				_ => v,
+			};
+			match v {
+				Val::Str(s) => println!("{}", s),
+				Val::Num(n) => println!("{}", n),
+				_v => eprintln!(
+					"jsonnet output is not a string.\nDid you forgot to set --format, or wrap your data with std.manifestJson?"
+				),
+			}
+		}
+		Err(err) => {
+			print_error(&err, evaluator, &opts);
+		}
+	}
+}
+
+fn print_error(err: &LocError, evaluator: EvaluationState, opts: &Opts) {
+	println!("Error: {:?}", err.0);
+	print_trace(&(err.1), evaluator, &opts);
+}
+
+fn print_syntax_error(error: jsonnet_parser::ParseError, file: &PathBuf, code: &str) {
+	use annotate_snippets::{
+		display_list::{DisplayList, FormatOptions},
+		snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
+	};
+	//&("Expected: ".to_owned() + error.expected)
+	let origin = file.to_str().unwrap();
+	let error_message = format!("Expected: {}", error.expected);
+	let snippet = Snippet {
+		opt: FormatOptions {
+			color: true,
+			..Default::default()
+		},
+		title: Some(Annotation {
+			label: Some(&error_message),
+			id: None,
+			annotation_type: AnnotationType::Error,
+		}),
+		footer: vec![],
+		slices: vec![Slice {
+			source: &code,
+			line_start: 1,
+			origin: Some(origin),
+			fold: false,
+			annotations: vec![SourceAnnotation {
+				label: "At this position",
+				annotation_type: AnnotationType::Error,
+				range: (error.location.offset, error.location.offset + 1),
+			}],
+		}],
+	};
+
+	let dl = DisplayList::from(snippet);
+	println!("{}", dl);
+}
+
+fn print_trace(trace: &StackTrace, evaluator: EvaluationState, opts: &Opts) {
+	use annotate_snippets::{
+		display_list::{DisplayList, FormatOptions},
+		snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
+	};
+	for item in trace.0.iter() {
+		let desc = &item.1;
+		if (item.0).1.is_none() {
+			continue;
+		}
+		let source = (item.0).1.clone().unwrap();
+		let code = evaluator.get_source(&source.0);
+		if code.is_none() {
+			continue;
+		}
+		let code = code.unwrap();
+		let start_end = offset_to_location(&code, &[source.1, source.2]);
+		if opts.trace_format == TraceFormat::Custom {
+			let source_fragment: String = code
+				.chars()
+				.skip(start_end[0].line_start_offset)
+				.take(start_end[1].line_end_offset - start_end[0].line_start_offset)
+				.collect();
+			let snippet = Snippet {
+				opt: FormatOptions {
+					color: true,
+					..Default::default()
+				},
+				title: Some(Annotation {
+					label: Some(&item.1),
+					id: None,
+					annotation_type: AnnotationType::Error,
+				}),
+				footer: vec![],
+				slices: vec![Slice {
+					source: &source_fragment,
+					line_start: start_end[0].line,
+					origin: Some(&source.0.to_str().unwrap()),
+					fold: false,
+					annotations: vec![SourceAnnotation {
+						label: desc,
+						annotation_type: AnnotationType::Error,
+						range: (
+							source.1 - start_end[0].line_start_offset,
+							source.2 - start_end[0].line_start_offset,
+						),
+					}],
+				}],
+			};
+
+			let dl = DisplayList::from(snippet);
+			println!("{}", dl);
+		} else {
+			print_jsonnet_pair(
+				source.0.to_str().unwrap(),
+				&start_end[0],
+				&start_end[1],
+				opts.trace_format == TraceFormat::GoJsonnet,
+			);
+		}
+	}
+}
+
+fn print_jsonnet_pair(file: &str, start: &CodeLocation, end: &CodeLocation, is_go: bool) {
+	if is_go {
+		print!("        ");
+	} else {
+		print!("  ");
+	}
+	print!("{}:", file);
+	if start.line == end.line {
+		// IDK why, but this is the behavior original jsonnet cpp impl shows
+		if start.column == end.column || !is_go && start.column + 1 == end.column {
+			println!("{}:{}", start.line, end.column)
+		} else {
+			println!("{}:{}-{}", start.line, start.column, end.column);
+		}
+	} else {
+		println!(
+			"({}:{})-({}:{})",
+			start.line, end.column, start.line, end.column
+		);
+	}
+}
deletedcmds/jsonnet/Cargo.tomldiffbeforeafterboth
--- a/cmds/jsonnet/Cargo.toml
+++ /dev/null
@@ -1,17 +0,0 @@
-[package]
-name = "jsonnet"
-version = "0.1.0"
-authors = ["Лач <iam@lach.pw>"]
-edition = "2018"
-
-# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
-
-[dependencies]
-jsonnet-evaluator = { path = "../../crates/jsonnet-evaluator" }
-jsonnet-parser = { path = "../../crates/jsonnet-parser" }
-annotate-snippets = "0.8.0"
-
-[dependencies.clap]
-version = "3.0.0-beta.1"
-default-features = false
-features = ["std", "derive"]
deletedcmds/jsonnet/src/location.rsdiffbeforeafterboth
--- a/cmds/jsonnet/src/location.rs
+++ /dev/null
@@ -1,99 +0,0 @@
-#[derive(Clone, PartialEq, Debug)]
-pub struct CodeLocation {
-	pub line: usize,
-	pub column: usize,
-
-	pub line_start_offset: usize,
-	pub line_end_offset: usize,
-}
-
-pub fn offset_to_location(file: &str, offsets: &[usize]) -> Vec<CodeLocation> {
-	if offsets.is_empty() {
-		return vec![];
-	}
-	let mut line = 1;
-	let mut column = 0;
-	let max_offset = *offsets.iter().max().unwrap();
-
-	let mut offset_map = offsets
-		.iter()
-		.enumerate()
-		.map(|(pos, offset)| (*offset, pos))
-		.collect::<Vec<_>>();
-	offset_map.sort_by_key(|v| v.0);
-	offset_map.reverse();
-
-	let mut out = vec![
-		CodeLocation {
-			column: 0,
-			line: 0,
-			line_start_offset: 0,
-			line_end_offset: 0
-		};
-		offsets.len()
-	];
-	let mut with_no_known_line_ending = vec![];
-	let mut this_line_offset = 0;
-	for (pos, ch) in file.chars().enumerate() {
-		column += 1;
-		match offset_map.last() {
-			Some(x) if x.0 == pos => {
-				let out_idx = x.1;
-				with_no_known_line_ending.push(out_idx);
-				out[out_idx].line = line;
-				out[out_idx].column = column;
-				out[out_idx].line_start_offset = this_line_offset + 1;
-				offset_map.pop();
-			}
-			_ => {}
-		}
-		if ch == '\n' {
-			line += 1;
-			column = 0;
-
-			for idx in with_no_known_line_ending.drain(..) {
-				out[idx].line_end_offset = pos;
-			}
-			this_line_offset = pos;
-
-			if pos == max_offset + 1 {
-				break;
-			}
-		}
-	}
-	let file_end = file.chars().count();
-	for idx in with_no_known_line_ending {
-		out[idx].line_end_offset = file_end;
-	}
-
-	out
-}
-
-#[cfg(test)]
-pub mod tests {
-	use super::{offset_to_location, CodeLocation};
-
-	#[test]
-	fn test() {
-		assert_eq!(
-			offset_to_location(
-				"hello world\n_______________________________________________________",
-				&[0, 14]
-			),
-			vec![
-				CodeLocation {
-					line: 1,
-					column: 1,
-					line_start_offset: 0,
-					line_end_offset: 11
-				},
-				CodeLocation {
-					line: 2,
-					column: 3,
-					line_start_offset: 11,
-					line_end_offset: 67
-				}
-			]
-		)
-	}
-}
deletedcmds/jsonnet/src/main.rsdiffbeforeafterboth
before · cmds/jsonnet/src/main.rs
1pub mod location;23use clap::Clap;4use jsonnet_evaluator::{EvaluationState, LocError, StackTrace, Val};5use location::{offset_to_location, CodeLocation};6use std::env::current_dir;7use std::{path::PathBuf, str::FromStr};89enum Format {10	None,11	Json,12	Yaml,13}1415impl FromStr for Format {16	type Err = &'static str;17	fn from_str(s: &str) -> Result<Self, Self::Err> {18		Ok(match s {19			"none" => Format::None,20			"json" => Format::Json,21			"yaml" => Format::Yaml,22			_ => return Err("no such format"),23		})24	}25}2627#[derive(PartialEq)]28enum TraceFormat {29	CppJsonnet,30	GoJsonnet,31	Custom,32}33impl FromStr for TraceFormat {34	type Err = &'static str;35	fn from_str(s: &str) -> Result<Self, Self::Err> {36		Ok(match s {37			"cpp" => TraceFormat::CppJsonnet,38			"go" => TraceFormat::GoJsonnet,39			"default" => TraceFormat::Custom,40			_ => return Err("no such format"),41		})42	}43}4445#[derive(Clap)]46#[clap(version = "0.1.0", author = "Lach <iam@lach.pw>")]47struct Opts {48	#[clap(long, about = "Disable global std variable")]49	no_stdlib: bool,50	#[clap(long, about = "Add external string")]51	ext_str: Option<Vec<String>>,52	#[clap(long, about = "Add external string from code")]53	ext_code: Option<Vec<String>>,54	#[clap(long, about = "Add TLA")]55	tla_str: Option<Vec<String>>,56	#[clap(long, about = "Add TLA from code")]57	tla_code: Option<Vec<String>>,58	#[clap(long, short = "f", default_value = "json", possible_values = &["none", "json", "yaml"], about = "Output format, wraps resulting value to corresponding std.manifest call")]59	format: Format,60	#[clap(long, default_value = "default", possible_values = &["cpp", "go", "default"], about = "Emulated needed stacktrace display")]61	trace_format: TraceFormat,6263	#[clap(64		long,65		short = "s",66		default_value = "200",67		about = "Number of allowed stack frames"68	)]69	max_stack: usize,70	#[clap(71		long,72		short = "t",73		default_value = "20",74		about = "Max length of stack trace before cropping"75	)]76	max_trace: usize,7778	#[clap(about = "File to compile", index = 1)]79	input: String,80}8182fn main() {83	let opts: Opts = Opts::parse();84	let evaluator = jsonnet_evaluator::EvaluationState::default();85	if !opts.no_stdlib {86		evaluator.add_stdlib();87	}88	let mut input = current_dir().unwrap();89	input.push(opts.input.clone());90	let code_string = String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap();91	if let Err(e) = evaluator.add_file(input.clone(), code_string.clone()) {92		print_syntax_error(e, &input, &code_string);93		std::process::exit(2);94	}95	let result = evaluator.evaluate_file(&input);96	match result {97		Ok(v) => {98			let v = match opts.format {99				Format::Json => {100					if opts.no_stdlib {101						evaluator.add_stdlib();102					}103					evaluator.add_global("__tmp__to_json__".to_owned(), v);104					let v = evaluator105						.parse_evaluate_raw("std.manifestJsonEx(__tmp__to_json__, \"  \")");106					match v {107						Ok(v) => v,108						Err(err) => {109							print_error(&err, evaluator, &opts);110							std::process::exit(2);111						}112					}113				}114				Format::Yaml => {115					if opts.no_stdlib {116						evaluator.add_stdlib();117					}118					evaluator.add_global("__tmp__to_yaml__".to_owned(), v);119					let v = evaluator120						.parse_evaluate_raw("std.manifestYamlDoc(__tmp__to_yaml__, \"  \")");121					match v {122						Ok(v) => v,123						Err(err) => {124							print_error(&err, evaluator, &opts);125							std::process::exit(2);126						}127					}128				}129				_ => v,130			};131			match v {132				Val::Str(s) => println!("{}", s),133				Val::Num(n) => println!("{}", n),134				_v => eprintln!(135					"jsonnet output is not a string.\nDid you forgot to set --format, or wrap your data with std.manifestJson?"136				),137			}138		}139		Err(err) => {140			print_error(&err, evaluator, &opts);141		}142	}143}144145fn print_error(err: &LocError, evaluator: EvaluationState, opts: &Opts) {146	println!("Error: {:?}", err.0);147	print_trace(&(err.1), evaluator, &opts);148}149150fn print_syntax_error(error: jsonnet_parser::ParseError, file: &PathBuf, code: &str) {151	use annotate_snippets::{152		display_list::{DisplayList, FormatOptions},153		snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},154	};155	//&("Expected: ".to_owned() + error.expected)156	let origin = file.to_str().unwrap();157	let error_message = format!("Expected: {}", error.expected);158	let snippet = Snippet {159		opt: FormatOptions {160			color: true,161			..Default::default()162		},163		title: Some(Annotation {164			label: Some(&error_message),165			id: None,166			annotation_type: AnnotationType::Error,167		}),168		footer: vec![],169		slices: vec![Slice {170			source: &code,171			line_start: 1,172			origin: Some(origin),173			fold: false,174			annotations: vec![SourceAnnotation {175				label: "At this position",176				annotation_type: AnnotationType::Error,177				range: (error.location.offset, error.location.offset + 1),178			}],179		}],180	};181182	let dl = DisplayList::from(snippet);183	println!("{}", dl);184}185186fn print_trace(trace: &StackTrace, evaluator: EvaluationState, opts: &Opts) {187	use annotate_snippets::{188		display_list::{DisplayList, FormatOptions},189		snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},190	};191	for item in trace.0.iter() {192		let desc = &item.1;193		if (item.0).1.is_none() {194			continue;195		}196		let source = (item.0).1.clone().unwrap();197		let code = evaluator.get_source(&source.0);198		if code.is_none() {199			continue;200		}201		let code = code.unwrap();202		let start_end = offset_to_location(&code, &[source.1, source.2]);203		if opts.trace_format == TraceFormat::Custom {204			let source_fragment: String = code205				.chars()206				.skip(start_end[0].line_start_offset)207				.take(start_end[1].line_end_offset - start_end[0].line_start_offset)208				.collect();209			let snippet = Snippet {210				opt: FormatOptions {211					color: true,212					..Default::default()213				},214				title: Some(Annotation {215					label: Some(&item.1),216					id: None,217					annotation_type: AnnotationType::Error,218				}),219				footer: vec![],220				slices: vec![Slice {221					source: &source_fragment,222					line_start: start_end[0].line,223					origin: Some(&source.0.to_str().unwrap()),224					fold: false,225					annotations: vec![SourceAnnotation {226						label: desc,227						annotation_type: AnnotationType::Error,228						range: (229							source.1 - start_end[0].line_start_offset,230							source.2 - start_end[0].line_start_offset,231						),232					}],233				}],234			};235236			let dl = DisplayList::from(snippet);237			println!("{}", dl);238		} else {239			print_jsonnet_pair(240				source.0.to_str().unwrap(),241				&start_end[0],242				&start_end[1],243				opts.trace_format == TraceFormat::GoJsonnet,244			);245		}246	}247}248249fn print_jsonnet_pair(file: &str, start: &CodeLocation, end: &CodeLocation, is_go: bool) {250	if is_go {251		print!("        ");252	} else {253		print!("  ");254	}255	print!("{}:", file);256	if start.line == end.line {257		// IDK why, but this is the behavior original jsonnet cpp impl shows258		if start.column == end.column || !is_go && start.column + 1 == end.column {259			println!("{}:{}", start.line, end.column)260		} else {261			println!("{}:{}-{}", start.line, start.column, end.column);262		}263	} else {264		println!(265			"({}:{})-({}:{})",266			start.line, end.column, start.line, end.column267		);268	}269}