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
before · crates/jrsonnet-evaluator/src/error.rs
1use std::{cmp::Ordering, convert::Infallible, fmt};23use jrsonnet_gcmodule::{Acyclic, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_ir::{BinaryOpType, Source, SourcePath, Span, Spanned, UnaryOpType};6use jrsonnet_types::ValType;7use thiserror::Error;89use crate::{10	function::{CallLocation, FunctionSignature, ParamName},11	stdlib::format::FormatError,12	typed::TypeLocError,13	val::ConvertNumValueError,14	ObjValue, ResolvePathOwned,15};1617pub(crate) fn format_found(list: &[IStr], what: &str) -> String {18	if list.is_empty() {19		return String::new();20	}21	let mut out = String::new();22	out.push_str("\nThere ");23	if list.len() > 1 {24		out.push_str("are ");25	} else {26		out.push_str("is a ");27	}28	out.push_str(what);29	if list.len() > 1 {30		out.push('s');31	}32	out.push_str(" with similar name");33	if list.len() > 1 {34		out.push('s');35	}36	out.push_str(" present: ");37	for (i, v) in list.iter().enumerate() {38		if i != 0 {39			out.push_str(", ");40		}41		out.push_str(v as &str);42	}43	out44}4546const fn format_empty_str(str: &str) -> &str {47	if str.is_empty() {48		"\"\" (empty string)"49	} else {50		str51	}52}5354pub(crate) fn suggest_object_fields(v: &ObjValue, key: IStr) -> Vec<IStr> {55	let mut heap = Vec::new();56	for field in v.fields_ex(57		true,58		#[cfg(feature = "exp-preserve-order")]59		false,60	) {61		let conf = strsim::jaro_winkler(field.as_str(), key.as_str());62		if conf < 0.8 {63			continue;64		}65		assert!(field.as_str() != key.as_str(), "looks like string pooling failure, please write any info regarding this crash to https://github.com/CertainLach/jrsonnet/issues/113, thanks!");6667		heap.push((conf, field));68	}69	heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));70	heap.into_iter().map(|v| v.1).collect()71}7273/// Possible errors74#[allow(missing_docs)]75#[derive(Error, Debug, Clone, Trace)]76#[non_exhaustive]77pub enum ErrorKind {78	#[error("intrinsic not found: {0}")]79	IntrinsicNotFound(IStr),8081	#[error("operator {0} does not operate on type {1}")]82	UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),83	#[error("binary operation {1} {0} {2} is not implemented")]84	BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),8586	#[error("self/super/$ are only usable inside objects")]87	CantUseSelfSupOutsideOfObject,88	#[error("no super found")]89	NoSuperFound,9091	#[error("for loop can only iterate over arrays")]92	InComprehensionCanOnlyIterateOverArray,9394	#[error("array out of bounds: {0} is not within [0,{1})")]95	ArrayBoundsError(isize, usize),96	#[error("string out of bounds: {0} is not within [0,{1})")]97	StringBoundsError(usize, usize),9899	#[error("assert failed: {}", format_empty_str(.0))]100	AssertionFailed(IStr),101102	#[error("local is not defined: {0}{found}", found = format_found(.1, "local"))]103	VariableIsNotDefined(IStr, Vec<IStr>),104	#[error("duplicate local var: {0}")]105	DuplicateLocalVar(IStr),106107	#[error("type mismatch: expected {expected}, got {2} {0}", expected = .1.iter().map(|e| format!("{e}")).collect::<Vec<_>>().join(", "))]108	TypeMismatch(&'static str, Vec<ValType>, ValType),109	#[error("no such field: {}{}", format_empty_str(.0), format_found(.1, "field"))]110	NoSuchField(IStr, Vec<IStr>),111112	#[error("only functions can be called, got {0}")]113	OnlyFunctionsCanBeCalledGot(ValType),114	#[error("parameter {0} is not defined")]115	UnknownFunctionParameter(IStr),116	#[error("argument {0} is already bound")]117	BindingParameterASecondTime(IStr),118	#[error("too many args, function has {0}\nFunction has the following signature: {1}")]119	TooManyArgsFunctionHas(usize, FunctionSignature),120	#[error("function argument is not passed: {0}\nFunction has the following signature: {1}")]121	FunctionParameterNotBoundInCall(ParamName, FunctionSignature),122123	#[error("external variable is not defined: {0}")]124	UndefinedExternalVariable(IStr),125126	#[error("field name should be string, got {0}")]127	FieldMustBeStringGot(ValType),128	#[error("duplicate field name: {}", format_empty_str(.0))]129	DuplicateFieldName(IStr),130131	#[error("attempted to index array with string {}", format_empty_str(.0))]132	AttemptedIndexAnArrayWithString(IStr),133	#[error("{0} index type should be {1}, got {2}")]134	ValueIndexMustBeTypeGot(ValType, ValType, ValType),135	#[error("cant index into {0}")]136	CantIndexInto(ValType),137	#[error("{0} is not indexable")]138	ValueIsNotIndexable(ValType),139140	#[error("super can't be used standalone")]141	StandaloneSuper,142143	#[error("can't resolve {1} from {0}")]144	ImportFileNotFound(SourcePath, ResolvePathOwned),145	#[error("resolved file not found: {:?}", .0)]146	ResolvedFileNotFound(SourcePath),147	#[error("can't import {0}: is a directory")]148	ImportIsADirectory(SourcePath),149	#[error("imported file is not valid utf-8: {0:?}")]150	ImportBadFileUtf8(SourcePath),151	#[error("import io error: {0}")]152	ImportIo(String),153	#[error("tried to import {1} from {0}, but imports are not supported")]154	ImportNotSupported(SourcePath, ResolvePathOwned),155	#[error("can't import from virtual file")]156	CantImportFromVirtualFile,157	#[cfg(not(feature = "ir-parser"))]158	#[error(159		"syntax error: {}",160		// Peg has no fancier way to handle critical parsing errors https://github.com/kevinmehall/rust-peg/issues/225161		{.error.expected.tokens().find(|t| t.starts_with("!!!")).map_or_else(|| {162			format!(163				"expected {}, got {:?}",164				.error.expected,165				.path.code().chars().nth(error.location.offset)166				.map_or_else(|| "EOF".into(), |c| c.to_string())167			)168		}, |v| v[3..].into())}169	)]170	ImportSyntaxError {171		path: Source,172		#[trace(skip)]173		error: Box<jrsonnet_peg_parser::ParseError>,174	},175176	#[cfg(feature = "ir-parser")]177	#[error("syntax error: {error}")]178	ImportSyntaxError {179		path: Source,180		#[trace(skip)]181		error: Box<jrsonnet_ir_parser::ParseError>,182	},183184	#[error("runtime error: {}", format_empty_str(.0))]185	RuntimeError(IStr),186	#[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]187	StackOverflow,188	#[error("infinite recursion detected")]189	InfiniteRecursionDetected,190	#[error("tried to index by fractional value")]191	FractionalIndex,192	#[error("attempted to divide by zero")]193	DivisionByZero,194195	#[error("string manifest output is not an string")]196	StringManifestOutputIsNotAString,197	#[error("stream manifest output is not an array")]198	StreamManifestOutputIsNotAArray,199	#[error("multi manifest output is not an object")]200	MultiManifestOutputIsNotAObject,201202	#[error("cant recurse stream manifest")]203	StreamManifestOutputCannotBeRecursed,204	#[error("stream manifest output cannot consist of raw strings")]205	StreamManifestCannotNestString,206207	#[error("{}", format_empty_str(.0))]208	ImportCallbackError(String),209	#[error("invalid unicode codepoint: {0}")]210	InvalidUnicodeCodepointGot(u32),211212	#[error("convert num value: {0}")]213	ConvertNumValue(#[from] ConvertNumValueError),214215	#[error("format error: {0}")]216	Format(#[from] FormatError),217	#[error("type error: {0}")]218	TypeError(TypeLocError),219220	#[cfg(feature = "anyhow-error")]221	#[error(transparent)]222	Other(#[trace(skip)] std::rc::Rc<anyhow::Error>),223}224225#[cfg(feature = "anyhow-error")]226impl From<anyhow::Error> for Error {227	fn from(e: anyhow::Error) -> Self {228		Self::new(ErrorKind::Other(std::rc::Rc::new(e)))229	}230}231232impl From<ErrorKind> for Error {233	fn from(e: ErrorKind) -> Self {234		Self::new(e)235	}236}237238impl From<Infallible> for Error {239	fn from(_value: Infallible) -> Self {240		unreachable!()241	}242}243244/// Single stack trace frame245#[derive(Clone, Debug, Trace)]246pub struct StackTraceElement {247	/// Source of this frame248	/// Some frames only act as description, without attached source249	pub location: Option<Span>,250	/// Frame description251	pub desc: String,252}253#[derive(Debug, Clone, Trace)]254pub struct StackTrace(pub Vec<StackTraceElement>);255256#[derive(Clone, Trace)]257pub struct Error(Box<(ErrorKind, StackTrace)>);258impl Error {259	pub fn new(e: ErrorKind) -> Self {260		Self(Box::new((e, StackTrace(vec![]))))261	}262263	pub const fn error(&self) -> &ErrorKind {264		&(self.0).0265	}266	pub fn error_mut(&mut self) -> &mut ErrorKind {267		&mut (self.0).0268	}269	pub const fn trace(&self) -> &StackTrace {270		&(self.0).1271	}272	pub fn trace_mut(&mut self) -> &mut StackTrace {273		&mut (self.0).1274	}275}276impl fmt::Display for Error {277	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {278		writeln!(f, "{}", self.0 .0)?;279		for el in &self.0 .1 .0 {280			write!(f, "\t{}", el.desc)?;281			if let Some(loc) = &el.location {282				write!(f, "at {}", loc.0 .0 .0)?;283				loc.0.map_source_locations(&[loc.1, loc.2]);284			}285			writeln!(f)?;286		}287		Ok(())288	}289}290impl fmt::Debug for Error {291	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {292		f.debug_tuple("LocError").field(&self.0).finish()293	}294}295impl std::error::Error for Error {}296297pub trait ErrorSource {298	fn to_location(self) -> Option<Span>;299}300impl<T: Acyclic> ErrorSource for &Spanned<T> {301	fn to_location(self) -> Option<Span> {302		Some(self.span.clone())303	}304}305impl ErrorSource for &Span {306	fn to_location(self) -> Option<Span> {307		Some(self.clone())308	}309}310impl ErrorSource for CallLocation<'_> {311	fn to_location(self) -> Option<Span> {312		self.0.cloned()313	}314}315316pub type Result<V, E = Error> = std::result::Result<V, E>;317pub trait ResultExt: Sized {318	#[must_use]319	fn with_description<O: Into<String>>(self, msg: impl FnOnce() -> O) -> Self;320	#[must_use]321	fn description(self, msg: &str) -> Self {322		self.with_description(|| msg)323	}324325	#[must_use]326	fn with_description_src<O: Into<String>>(327		self,328		src: impl ErrorSource,329		msg: impl FnOnce() -> O,330	) -> Self;331	#[must_use]332	fn description_src(self, src: impl ErrorSource, msg: &str) -> Self {333		self.with_description_src(src, || msg)334	}335}336impl<T> ResultExt for Result<T, Error> {337	fn with_description<O: Into<String>>(mut self, msg: impl FnOnce() -> O) -> Self {338		if let Err(e) = &mut self {339			let trace = e.trace_mut();340			trace.0.push(StackTraceElement {341				location: None,342				desc: msg().into(),343			});344		}345		self346	}347348	fn with_description_src<O: Into<String>>(349		mut self,350		src: impl ErrorSource,351		msg: impl FnOnce() -> O,352	) -> Self {353		if let Err(e) = &mut self {354			let trace = e.trace_mut();355			trace.0.push(StackTraceElement {356				location: src.to_location(),357				desc: msg().into(),358			});359		}360		self361	}362}363364#[macro_export]365macro_rules! bail {366	($w:ident$(::$i:ident)*$(($($tt:tt)*))?) => {367		return Err($w$(::$i)*$(($($tt)*))?.into())368	};369	($w:ident$(::$i:ident)*$({$($tt:tt)*})?) => {370		return Err($w$(::$i)*$({$($tt)*})?.into())371	};372	($l:literal$(, $($tt:tt)*)?) => {373		return Err($crate::error::ErrorKind::RuntimeError($crate::jrsonnet_macros::format_istr!($l$(, $($tt)*)?)).into())374	};375}376377#[macro_export]378macro_rules! runtime_error {379	($l:literal$(, $($tt:tt)*)?) => {380		$crate::error::Error::from($crate::error::ErrorKind::RuntimeError($crate::jrsonnet_macros::format_istr!($l$(, $($tt)*)?)))381	};382}
after · crates/jrsonnet-evaluator/src/error.rs
1use std::{cmp::Ordering, convert::Infallible, fmt};23use jrsonnet_gcmodule::{Acyclic, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_ir::{BinaryOpType, Source, SourcePath, Span, Spanned, UnaryOpType};6use jrsonnet_types::ValType;7use thiserror::Error;89use crate::{10	function::{CallLocation, FunctionSignature, ParamName},11	stdlib::format::FormatError,12	typed::TypeLocError,13	val::ConvertNumValueError,14	ObjValue, ResolvePathOwned,15};1617#[derive(Debug, Clone)]18pub struct SyntaxErrorLocation {19	pub offset: usize,20}2122#[derive(Debug, Clone)]23pub struct SyntaxError {24	pub message: String,25	pub location: SyntaxErrorLocation,26}27impl fmt::Display for SyntaxError {28	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {29		write!(f, "{}", self.message)30	}31}3233pub(crate) fn format_found(list: &[IStr], what: &str) -> String {34	if list.is_empty() {35		return String::new();36	}37	let mut out = String::new();38	out.push_str("\nThere ");39	if list.len() > 1 {40		out.push_str("are ");41	} else {42		out.push_str("is a ");43	}44	out.push_str(what);45	if list.len() > 1 {46		out.push('s');47	}48	out.push_str(" with similar name");49	if list.len() > 1 {50		out.push('s');51	}52	out.push_str(" present: ");53	for (i, v) in list.iter().enumerate() {54		if i != 0 {55			out.push_str(", ");56		}57		out.push_str(v as &str);58	}59	out60}6162const fn format_empty_str(str: &str) -> &str {63	if str.is_empty() {64		"\"\" (empty string)"65	} else {66		str67	}68}6970pub(crate) fn suggest_object_fields(v: &ObjValue, key: IStr) -> Vec<IStr> {71	let mut heap = Vec::new();72	for field in v.fields_ex(73		true,74		#[cfg(feature = "exp-preserve-order")]75		false,76	) {77		let conf = strsim::jaro_winkler(field.as_str(), key.as_str());78		if conf < 0.8 {79			continue;80		}81		assert!(field.as_str() != key.as_str(), "looks like string pooling failure, please write any info regarding this crash to https://github.com/CertainLach/jrsonnet/issues/113, thanks!");8283		heap.push((conf, field));84	}85	heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));86	heap.into_iter().map(|v| v.1).collect()87}8889/// Possible errors90#[allow(missing_docs)]91#[derive(Error, Debug, Clone, Trace)]92#[non_exhaustive]93pub enum ErrorKind {94	#[error("intrinsic not found: {0}")]95	IntrinsicNotFound(IStr),9697	#[error("operator {0} does not operate on type {1}")]98	UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),99	#[error("binary operation {1} {0} {2} is not implemented")]100	BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),101102	#[error("self/super/$ are only usable inside objects")]103	CantUseSelfSupOutsideOfObject,104	#[error("no super found")]105	NoSuperFound,106107	#[error("for loop can only iterate over arrays")]108	InComprehensionCanOnlyIterateOverArray,109110	#[error("array out of bounds: {0} is not within [0,{1})")]111	ArrayBoundsError(isize, usize),112	#[error("string out of bounds: {0} is not within [0,{1})")]113	StringBoundsError(usize, usize),114115	#[error("assert failed: {}", format_empty_str(.0))]116	AssertionFailed(IStr),117118	#[error("local is not defined: {0}{found}", found = format_found(.1, "local"))]119	VariableIsNotDefined(IStr, Vec<IStr>),120	#[error("duplicate local var: {0}")]121	DuplicateLocalVar(IStr),122123	#[error("type mismatch: expected {expected}, got {2} {0}", expected = .1.iter().map(|e| format!("{e}")).collect::<Vec<_>>().join(", "))]124	TypeMismatch(&'static str, Vec<ValType>, ValType),125	#[error("no such field: {}{}", format_empty_str(.0), format_found(.1, "field"))]126	NoSuchField(IStr, Vec<IStr>),127128	#[error("only functions can be called, got {0}")]129	OnlyFunctionsCanBeCalledGot(ValType),130	#[error("parameter {0} is not defined")]131	UnknownFunctionParameter(IStr),132	#[error("argument {0} is already bound")]133	BindingParameterASecondTime(IStr),134	#[error("too many args, function has {0}\nFunction has the following signature: {1}")]135	TooManyArgsFunctionHas(usize, FunctionSignature),136	#[error("function argument is not passed: {0}\nFunction has the following signature: {1}")]137	FunctionParameterNotBoundInCall(ParamName, FunctionSignature),138139	#[error("external variable is not defined: {0}")]140	UndefinedExternalVariable(IStr),141142	#[error("field name should be string, got {0}")]143	FieldMustBeStringGot(ValType),144	#[error("duplicate field name: {}", format_empty_str(.0))]145	DuplicateFieldName(IStr),146147	#[error("attempted to index array with string {}", format_empty_str(.0))]148	AttemptedIndexAnArrayWithString(IStr),149	#[error("{0} index type should be {1}, got {2}")]150	ValueIndexMustBeTypeGot(ValType, ValType, ValType),151	#[error("cant index into {0}")]152	CantIndexInto(ValType),153	#[error("{0} is not indexable")]154	ValueIsNotIndexable(ValType),155156	#[error("super can't be used standalone")]157	StandaloneSuper,158159	#[error("can't resolve {1} from {0}")]160	ImportFileNotFound(SourcePath, ResolvePathOwned),161	#[error("resolved file not found: {:?}", .0)]162	ResolvedFileNotFound(SourcePath),163	#[error("can't import {0}: is a directory")]164	ImportIsADirectory(SourcePath),165	#[error("imported file is not valid utf-8: {0:?}")]166	ImportBadFileUtf8(SourcePath),167	#[error("import io error: {0}")]168	ImportIo(String),169	#[error("tried to import {1} from {0}, but imports are not supported")]170	ImportNotSupported(SourcePath, ResolvePathOwned),171	#[error("can't import from virtual file")]172	CantImportFromVirtualFile,173	#[error("syntax error: {error}")]174	ImportSyntaxError {175		path: Source,176		#[trace(skip)]177		error: Box<SyntaxError>,178	},179180	#[error("runtime error: {}", format_empty_str(.0))]181	RuntimeError(IStr),182	#[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]183	StackOverflow,184	#[error("infinite recursion detected")]185	InfiniteRecursionDetected,186	#[error("tried to index by fractional value")]187	FractionalIndex,188	#[error("attempted to divide by zero")]189	DivisionByZero,190191	#[error("string manifest output is not an string")]192	StringManifestOutputIsNotAString,193	#[error("stream manifest output is not an array")]194	StreamManifestOutputIsNotAArray,195	#[error("multi manifest output is not an object")]196	MultiManifestOutputIsNotAObject,197198	#[error("cant recurse stream manifest")]199	StreamManifestOutputCannotBeRecursed,200	#[error("stream manifest output cannot consist of raw strings")]201	StreamManifestCannotNestString,202203	#[error("{}", format_empty_str(.0))]204	ImportCallbackError(String),205	#[error("invalid unicode codepoint: {0}")]206	InvalidUnicodeCodepointGot(u32),207208	#[error("convert num value: {0}")]209	ConvertNumValue(#[from] ConvertNumValueError),210211	#[error("format error: {0}")]212	Format(#[from] FormatError),213	#[error("type error: {0}")]214	TypeError(TypeLocError),215216	#[cfg(feature = "anyhow-error")]217	#[error(transparent)]218	Other(#[trace(skip)] std::rc::Rc<anyhow::Error>),219}220221#[cfg(feature = "anyhow-error")]222impl From<anyhow::Error> for Error {223	fn from(e: anyhow::Error) -> Self {224		Self::new(ErrorKind::Other(std::rc::Rc::new(e)))225	}226}227228impl From<ErrorKind> for Error {229	fn from(e: ErrorKind) -> Self {230		Self::new(e)231	}232}233234impl From<Infallible> for Error {235	fn from(_value: Infallible) -> Self {236		unreachable!()237	}238}239240/// Single stack trace frame241#[derive(Clone, Debug, Trace)]242pub struct StackTraceElement {243	/// Source of this frame244	/// Some frames only act as description, without attached source245	pub location: Option<Span>,246	/// Frame description247	pub desc: String,248}249#[derive(Debug, Clone, Trace)]250pub struct StackTrace(pub Vec<StackTraceElement>);251252#[derive(Clone, Trace)]253pub struct Error(Box<(ErrorKind, StackTrace)>);254impl Error {255	pub fn new(e: ErrorKind) -> Self {256		Self(Box::new((e, StackTrace(vec![]))))257	}258259	pub const fn error(&self) -> &ErrorKind {260		&(self.0).0261	}262	pub fn error_mut(&mut self) -> &mut ErrorKind {263		&mut (self.0).0264	}265	pub const fn trace(&self) -> &StackTrace {266		&(self.0).1267	}268	pub fn trace_mut(&mut self) -> &mut StackTrace {269		&mut (self.0).1270	}271}272impl fmt::Display for Error {273	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {274		writeln!(f, "{}", self.0 .0)?;275		for el in &self.0 .1 .0 {276			write!(f, "\t{}", el.desc)?;277			if let Some(loc) = &el.location {278				write!(f, "at {}", loc.0 .0 .0)?;279				loc.0.map_source_locations(&[loc.1, loc.2]);280			}281			writeln!(f)?;282		}283		Ok(())284	}285}286impl fmt::Debug for Error {287	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {288		f.debug_tuple("LocError").field(&self.0).finish()289	}290}291impl std::error::Error for Error {}292293pub trait ErrorSource {294	fn to_location(self) -> Option<Span>;295}296impl<T: Acyclic> ErrorSource for &Spanned<T> {297	fn to_location(self) -> Option<Span> {298		Some(self.span.clone())299	}300}301impl ErrorSource for &Span {302	fn to_location(self) -> Option<Span> {303		Some(self.clone())304	}305}306impl ErrorSource for CallLocation<'_> {307	fn to_location(self) -> Option<Span> {308		self.0.cloned()309	}310}311312pub type Result<V, E = Error> = std::result::Result<V, E>;313pub trait ResultExt: Sized {314	#[must_use]315	fn with_description<O: Into<String>>(self, msg: impl FnOnce() -> O) -> Self;316	#[must_use]317	fn description(self, msg: &str) -> Self {318		self.with_description(|| msg)319	}320321	#[must_use]322	fn with_description_src<O: Into<String>>(323		self,324		src: impl ErrorSource,325		msg: impl FnOnce() -> O,326	) -> Self;327	#[must_use]328	fn description_src(self, src: impl ErrorSource, msg: &str) -> Self {329		self.with_description_src(src, || msg)330	}331}332impl<T> ResultExt for Result<T, Error> {333	fn with_description<O: Into<String>>(mut self, msg: impl FnOnce() -> O) -> Self {334		if let Err(e) = &mut self {335			let trace = e.trace_mut();336			trace.0.push(StackTraceElement {337				location: None,338				desc: msg().into(),339			});340		}341		self342	}343344	fn with_description_src<O: Into<String>>(345		mut self,346		src: impl ErrorSource,347		msg: impl FnOnce() -> O,348	) -> Self {349		if let Err(e) = &mut self {350			let trace = e.trace_mut();351			trace.0.push(StackTraceElement {352				location: src.to_location(),353				desc: msg().into(),354			});355		}356		self357	}358}359360#[macro_export]361macro_rules! bail {362	($w:ident$(::$i:ident)*$(($($tt:tt)*))?) => {363		return Err($w$(::$i)*$(($($tt)*))?.into())364	};365	($w:ident$(::$i:ident)*$({$($tt:tt)*})?) => {366		return Err($w$(::$i)*$({$($tt)*})?.into())367	};368	($l:literal$(, $($tt:tt)*)?) => {369		return Err($crate::error::ErrorKind::RuntimeError($crate::jrsonnet_macros::format_istr!($l$(, $($tt)*)?)).into())370	};371}372373#[macro_export]374macro_rules! runtime_error {375	($l:literal$(, $($tt:tt)*)?) => {376		$crate::error::Error::from($crate::error::ErrorKind::RuntimeError($crate::jrsonnet_macros::format_istr!($l$(, $($tt)*)?)))377	};378}
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -46,10 +46,11 @@
 use jrsonnet_ir::{Expr, Source, SourcePath};
 #[doc(hidden)]
 pub use jrsonnet_macros;
-#[cfg(feature = "ir-parser")]
-use jrsonnet_ir_parser::ParserSettings;
-#[cfg(not(feature = "ir-parser"))]
-use jrsonnet_peg_parser::ParserSettings;
+
+#[cfg(not(any(feature = "ir-parser", feature = "peg-parser")))]
+compile_error!("at least one of `ir-parser` or `peg-parser` features must be enabled");
+
+pub use error::{SyntaxError, SyntaxErrorLocation};
 pub use obj::*;
 pub use rustc_hash;
 use rustc_hash::FxHashMap;
@@ -59,20 +60,64 @@
 
 use crate::gc::WithCapacityExt as _;
 
+pub(crate) fn parse_jsonnet(code: &str, source: Source) -> Result<Expr, SyntaxError> {
+	#[cfg(all(feature = "ir-parser", feature = "peg-parser"))]
+	{
+		if std::env::var_os("JRSONNET_LEGACY_PARSER").is_some() {
+			return parse_peg(code, source);
+		}
+		return parse_ir(code, source);
+	}
+	#[cfg(all(feature = "ir-parser", not(feature = "peg-parser")))]
+	{
+		return parse_ir(code, source);
+	}
+	#[cfg(all(feature = "peg-parser", not(feature = "ir-parser")))]
+	{
+		return parse_peg(code, source);
+	}
+}
+
 #[cfg(feature = "ir-parser")]
-pub(crate) fn parse_jsonnet(
-	code: &str,
-	settings: &ParserSettings,
-) -> Result<Expr, jrsonnet_ir_parser::ParseError> {
-	jrsonnet_ir_parser::parse(code, settings)
+fn parse_ir(code: &str, source: Source) -> Result<Expr, 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(not(feature = "ir-parser"))]
-pub(crate) fn parse_jsonnet(
-	code: &str,
-	settings: &ParserSettings,
-) -> Result<Expr, jrsonnet_peg_parser::ParseError> {
-	jrsonnet_peg_parser::parse(code, settings)
+#[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,
+				},
+			}
+		},
+	)
 }
 
 cc_dyn!(
@@ -364,12 +409,7 @@
 		let file_name = Source::new(path.clone(), code.clone());
 		if file.parsed.is_none() {
 			file.parsed = Some(
-				parse_jsonnet(
-					&code,
-					&ParserSettings {
-						source: file_name.clone(),
-					},
-				)
+				parse_jsonnet(&code, file_name.clone())
 				.map(Rc::new)
 				.map_err(|e| ImportSyntaxError {
 					path: file_name.clone(),
@@ -480,12 +520,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,
-			&ParserSettings {
-				source: source.clone(),
-			},
-		)
+		let parsed = parse_jsonnet(&code, source.clone())
 		.map_err(|e| ImportSyntaxError {
 			path: source.clone(),
 			error: Box::new(e),
@@ -501,12 +536,7 @@
 	) -> Result<Val> {
 		let code = code.into();
 		let source = Source::new_virtual(name.into(), code.clone());
-		let parsed = parse_jsonnet(
-			&code,
-			&ParserSettings {
-				source: source.clone(),
-			},
-		)
+		let parsed = parse_jsonnet(&code, source.clone())
 		.map_err(|e| ImportSyntaxError {
 			path: source.clone(),
 			error: Box::new(e),