git.delta.rocks / jrsonnet / refs/commits / 0a23a80af017

difftreelog

feat make both parsers available at the same time

sklkzktrYaroslav Bolyukin2026-03-23parent: #c02b270.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -60,19 +60,22 @@
 
 use crate::gc::WithCapacityExt as _;
 
+#[allow(clippy::needless_return)]
 pub(crate) fn parse_jsonnet(code: &str, source: Source) -> Result<Expr, SyntaxError> {
-	#[cfg(all(feature = "ir-parser", feature = "peg-parser"))]
+	#[cfg(feature = "peg-parser")]
 	{
-		if std::env::var_os("JRSONNET_LEGACY_PARSER").is_some() {
+		static USE_LEGACY_PARSER: LazyLock<bool> =
+			LazyLock::new(|| std::env::var_os("JRSONNET_LEGACY_PARSER").is_some());
+
+		if USE_LEGACY_PARSER {
 			return parse_peg(code, source);
 		}
-		return parse_ir(code, source);
 	}
-	#[cfg(all(feature = "ir-parser", not(feature = "peg-parser")))]
+	#[cfg(feature = "ir-parser")]
 	{
 		return parse_ir(code, source);
 	}
-	#[cfg(all(feature = "peg-parser", not(feature = "ir-parser")))]
+	#[cfg(feature = "peg-parser")]
 	{
 		return parse_peg(code, source);
 	}
@@ -80,44 +83,42 @@
 
 #[cfg(feature = "ir-parser")]
 fn parse_ir(code: &str, source: Source) -> Result<Expr, SyntaxError> {
-	jrsonnet_ir_parser::parse(code, &jrsonnet_ir_parser::ParserSettings { source }).map_err(
-		|e| SyntaxError {
+	jrsonnet_ir_parser::parse(code, &jrsonnet_ir_parser::ParserSettings { source }).map_err(|e| {
+		SyntaxError {
 			message: e.message,
 			location: SyntaxErrorLocation {
 				offset: e.location.offset,
 			},
-		},
-	)
+		}
+	})
 }
 
 #[cfg(feature = "peg-parser")]
 fn parse_peg(code: &str, source: Source) -> Result<Expr, SyntaxError> {
-	jrsonnet_peg_parser::parse(code, &jrsonnet_peg_parser::ParserSettings { source }).map_err(
-		|e| {
-			let message = e
-				.expected
-				.tokens()
-				.find(|t| t.starts_with("!!!"))
-				.map_or_else(
-					|| {
-						format!(
-							"expected {}, got {:?}",
-							e.expected,
-							code.chars()
-								.nth(e.location.offset)
-								.map_or_else(|| "EOF".into(), |c: char| c.to_string())
-						)
-					},
-					|v| v[3..].into(),
-				);
-			SyntaxError {
-				message,
-				location: SyntaxErrorLocation {
-					offset: e.location.offset,
+	jrsonnet_peg_parser::parse(code, &jrsonnet_peg_parser::ParserSettings { source }).map_err(|e| {
+		let message = e
+			.expected
+			.tokens()
+			.find(|t| t.starts_with("!!!"))
+			.map_or_else(
+				|| {
+					format!(
+						"expected {}, got {:?}",
+						e.expected,
+						code.chars()
+							.nth(e.location.offset)
+							.map_or_else(|| "EOF".into(), |c: char| c.to_string())
+					)
 				},
-			}
-		},
-	)
+				|v| v[3..].into(),
+			);
+		SyntaxError {
+			message,
+			location: SyntaxErrorLocation {
+				offset: e.location.offset,
+			},
+		}
+	})
 }
 
 cc_dyn!(
@@ -410,11 +411,11 @@
 		if file.parsed.is_none() {
 			file.parsed = Some(
 				parse_jsonnet(&code, file_name.clone())
-				.map(Rc::new)
-				.map_err(|e| ImportSyntaxError {
-					path: file_name.clone(),
-					error: Box::new(e),
-				})?,
+					.map(Rc::new)
+					.map_err(|e| ImportSyntaxError {
+						path: file_name.clone(),
+						error: Box::new(e),
+					})?,
 			);
 		}
 		let parsed = file.parsed.as_ref().expect("just set").clone();
@@ -520,8 +521,7 @@
 	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {
 		let code = code.into();
 		let source = Source::new_virtual(name.into(), code.clone());
-		let parsed = parse_jsonnet(&code, source.clone())
-		.map_err(|e| ImportSyntaxError {
+		let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {
 			path: source.clone(),
 			error: Box::new(e),
 		})?;
@@ -536,8 +536,7 @@
 	) -> Result<Val> {
 		let code = code.into();
 		let source = Source::new_virtual(name.into(), code.clone());
-		let parsed = parse_jsonnet(&code, source.clone())
-		.map_err(|e| ImportSyntaxError {
+		let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {
 			path: source.clone(),
 			error: Box::new(e),
 		})?;
modifiedtests/Cargo.tomldiffbeforeafterboth
before · tests/Cargo.toml
1[package]2name = "tests"3version = "0.1.0"4edition = "2024"5publish = false67[features]8default = ["ir-parser"]9ir-parser = ["jrsonnet-evaluator/ir-parser"]10exp-null-coaelse = ["jrsonnet-evaluator/exp-null-coaelse"]1112[lints]13workspace = true1415[dependencies]16jrsonnet-evaluator.workspace = true17jrsonnet-gcmodule.workspace = true18jrsonnet-stdlib.workspace = true19serde.workspace = true20json-structural-diff.workspace = true21serde_json.workspace = true2223[dev-dependencies]24insta.workspace = true
after · tests/Cargo.toml
1[package]2name = "tests"3version = "0.1.0"4edition = "2024"5publish = false67[features]8default = ["ir-parser"]9ir-parser = ["jrsonnet-evaluator/ir-parser"]10peg-parser = ["jrsonnet-evaluator/peg-parser"]11exp-destruct = ["jrsonnet-evaluator/exp-destruct"]12exp-null-coaelse = ["jrsonnet-evaluator/exp-null-coaelse"]1314[lints]15workspace = true1617[dependencies]18jrsonnet-evaluator.workspace = true19jrsonnet-gcmodule.workspace = true20jrsonnet-stdlib.workspace = true21serde.workspace = true22json-structural-diff.workspace = true23serde_json.workspace = true2425[dev-dependencies]26insta.workspace = true