git.delta.rocks / jrsonnet / refs/commits / 7eb771ff363d

difftreelog

feat allow both parsers at the same time

uwkkuzmuYaroslav Bolyukin2026-03-23parent: #b6f9e83.patch.diff
in: master

4 files changed

modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -18,24 +18,26 @@
 explaining-traces = ["annotate-snippets", "hi-doc"]
 # Allows library authors to throw custom errors
 anyhow-error = ["anyhow"]
-# Use hand-written recursive descent parser instead of PEG parser
+# Use hand-written recursive descent parser
 ir-parser = ["dep:jrsonnet-ir-parser"]
+# Use PEG parser
+peg-parser = ["dep:jrsonnet-peg-parser"]
 
 # Allows to preserve field order in objects
 exp-preserve-order = []
 # Implements field destructuring
-exp-destruct = ["jrsonnet-peg-parser/exp-destruct"]
+exp-destruct = ["jrsonnet-peg-parser?/exp-destruct", "jrsonnet-ir-parser?/exp-destruct"]
 # Iteration over objects yields [key, value] elements
 exp-object-iteration = []
 # Bigint type
 exp-bigint = ["num-bigint", "jrsonnet-types/exp-bigint"]
 # obj?.field, obj?.['field']
-exp-null-coaelse = ["jrsonnet-peg-parser/exp-null-coaelse", "jrsonnet-ir-parser?/exp-null-coaelse"]
+exp-null-coaelse = ["jrsonnet-peg-parser?/exp-null-coaelse", "jrsonnet-ir-parser?/exp-null-coaelse"]
 
 [dependencies]
 jrsonnet-interner.workspace = true
 jrsonnet-ir.workspace = true
-jrsonnet-peg-parser.workspace = true
+jrsonnet-peg-parser = { workspace = true, optional = true }
 jrsonnet-ir-parser = { workspace = true, optional = true }
 jrsonnet-types.workspace = true
 jrsonnet-macros.workspace = true
modifiedcrates/jrsonnet-evaluator/src/async_import.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/async_import.rs
+++ b/crates/jrsonnet-evaluator/src/async_import.rs
@@ -7,10 +7,6 @@
 	FieldMember, FieldName, ForSpecData, IfElse, IfSpecData, ImportKind, ObjBody, Slice, SliceDesc,
 	Source, SourcePath, Spanned,
 };
-#[cfg(feature = "ir-parser")]
-use jrsonnet_ir_parser::ParserSettings;
-#[cfg(not(feature = "ir-parser"))]
-use jrsonnet_peg_parser::ParserSettings;
 use rustc_hash::FxHashMap;
 
 use crate::{AsPathLike, FileData, ImportResolver, ResolvePathOwned, State};
@@ -326,7 +322,7 @@
 						};
 						let source = Source::new(path.clone(), code.clone());
 						// If failed - then skip import
-						file.parsed = crate::parse_jsonnet(&code, &ParserSettings { source })
+						file.parsed = crate::parse_jsonnet(&code, source)
 							.map(Rc::new)
 							.ok();
 						if let Some(parsed) = &file.parsed {
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -14,6 +14,22 @@
 	ObjValue, ResolvePathOwned,
 };
 
+#[derive(Debug, Clone)]
+pub struct SyntaxErrorLocation {
+	pub offset: usize,
+}
+
+#[derive(Debug, Clone)]
+pub struct SyntaxError {
+	pub message: String,
+	pub location: SyntaxErrorLocation,
+}
+impl fmt::Display for SyntaxError {
+	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+		write!(f, "{}", self.message)
+	}
+}
+
 pub(crate) fn format_found(list: &[IStr], what: &str) -> String {
 	if list.is_empty() {
 		return String::new();
@@ -154,31 +170,11 @@
 	ImportNotSupported(SourcePath, ResolvePathOwned),
 	#[error("can't import from virtual file")]
 	CantImportFromVirtualFile,
-	#[cfg(not(feature = "ir-parser"))]
-	#[error(
-		"syntax error: {}",
-		// Peg has no fancier way to handle critical parsing errors https://github.com/kevinmehall/rust-peg/issues/225
-		{.error.expected.tokens().find(|t| t.starts_with("!!!")).map_or_else(|| {
-			format!(
-				"expected {}, got {:?}",
-				.error.expected,
-				.path.code().chars().nth(error.location.offset)
-				.map_or_else(|| "EOF".into(), |c| c.to_string())
-			)
-		}, |v| v[3..].into())}
-	)]
-	ImportSyntaxError {
-		path: Source,
-		#[trace(skip)]
-		error: Box<jrsonnet_peg_parser::ParseError>,
-	},
-
-	#[cfg(feature = "ir-parser")]
 	#[error("syntax error: {error}")]
 	ImportSyntaxError {
 		path: Source,
 		#[trace(skip)]
-		error: Box<jrsonnet_ir_parser::ParseError>,
+		error: Box<SyntaxError>,
 	},
 
 	#[error("runtime error: {}", format_empty_str(.0))]
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
47#[doc(hidden)]47#[doc(hidden)]
48pub use jrsonnet_macros;48pub use jrsonnet_macros;
49
49#[cfg(feature = "ir-parser")]50#[cfg(not(any(feature = "ir-parser", feature = "peg-parser")))]
50use jrsonnet_ir_parser::ParserSettings;51compile_error!("at least one of `ir-parser` or `peg-parser` features must be enabled");
51#[cfg(not(feature = "ir-parser"))]52
52use jrsonnet_peg_parser::ParserSettings;53pub use error::{SyntaxError, SyntaxErrorLocation};
53pub use obj::*;54pub use obj::*;
54pub use rustc_hash;55pub use rustc_hash;
55use rustc_hash::FxHashMap;56use rustc_hash::FxHashMap;
5960
60use crate::gc::WithCapacityExt as _;61use crate::gc::WithCapacityExt as _;
62
63pub(crate) fn parse_jsonnet(code: &str, source: Source) -> Result<Expr, SyntaxError> {
64 #[cfg(all(feature = "ir-parser", feature = "peg-parser"))]
65 {
66 if std::env::var_os("JRSONNET_LEGACY_PARSER").is_some() {
67 return parse_peg(code, source);
68 }
69 return parse_ir(code, source);
70 }
71 #[cfg(all(feature = "ir-parser", not(feature = "peg-parser")))]
72 {
73 return parse_ir(code, source);
74 }
75 #[cfg(all(feature = "peg-parser", not(feature = "ir-parser")))]
76 {
77 return parse_peg(code, source);
78 }
79}
6180
62#[cfg(feature = "ir-parser")]81#[cfg(feature = "ir-parser")]
63pub(crate) fn parse_jsonnet(82fn parse_ir(code: &str, source: Source) -> Result<Expr, SyntaxError> {
64 code: &str,
65 settings: &ParserSettings,
66) -> Result<Expr, jrsonnet_ir_parser::ParseError> {
67 jrsonnet_ir_parser::parse(code, settings)83 jrsonnet_ir_parser::parse(code, &jrsonnet_ir_parser::ParserSettings { source }).map_err(
84 |e| SyntaxError {
85 message: e.message,
86 location: SyntaxErrorLocation {
87 offset: e.location.offset,
88 },
89 },
90 )
68}91}
6992
70#[cfg(not(feature = "ir-parser"))]93#[cfg(feature = "peg-parser")]
71pub(crate) fn parse_jsonnet(94fn parse_peg(code: &str, source: Source) -> Result<Expr, SyntaxError> {
72 code: &str,
73 settings: &ParserSettings,
74) -> Result<Expr, jrsonnet_peg_parser::ParseError> {
75 jrsonnet_peg_parser::parse(code, settings)95 jrsonnet_peg_parser::parse(code, &jrsonnet_peg_parser::ParserSettings { source }).map_err(
96 |e| {
97 let message = e
98 .expected
99 .tokens()
100 .find(|t| t.starts_with("!!!"))
101 .map_or_else(
102 || {
103 format!(
104 "expected {}, got {:?}",
105 e.expected,
106 code.chars()
107 .nth(e.location.offset)
108 .map_or_else(|| "EOF".into(), |c: char| c.to_string())
109 )
110 },
111 |v| v[3..].into(),
112 );
113 SyntaxError {
114 message,
115 location: SyntaxErrorLocation {
116 offset: e.location.offset,
117 },
118 }
119 },
120 )
76}121}
77122
78cc_dyn!(123cc_dyn!(
366 file.parsed = Some(411 file.parsed = Some(
367 parse_jsonnet(412 parse_jsonnet(&code, file_name.clone())
368 &code,
369 &ParserSettings {
370 source: file_name.clone(),
371 },
372 )
373 .map(Rc::new)413 .map(Rc::new)
374 .map_err(|e| ImportSyntaxError {414 .map_err(|e| ImportSyntaxError {
482 let source = Source::new_virtual(name.into(), code.clone());522 let source = Source::new_virtual(name.into(), code.clone());
483 let parsed = parse_jsonnet(523 let parsed = parse_jsonnet(&code, source.clone())
484 &code,
485 &ParserSettings {
486 source: source.clone(),
487 },
488 )
489 .map_err(|e| ImportSyntaxError {524 .map_err(|e| ImportSyntaxError {
490 path: source.clone(),525 path: source.clone(),
503 let source = Source::new_virtual(name.into(), code.clone());538 let source = Source::new_virtual(name.into(), code.clone());
504 let parsed = parse_jsonnet(539 let parsed = parse_jsonnet(&code, source.clone())
505 &code,
506 &ParserSettings {
507 source: source.clone(),
508 },
509 )
510 .map_err(|e| ImportSyntaxError {540 .map_err(|e| ImportSyntaxError {
511 path: source.clone(),541 path: source.clone(),