git.delta.rocks / jrsonnet / refs/commits / 8885f2169203

difftreelog

feat show full error range instead of just start

sonopmszLach2026-04-02parent: #75beb61.patch.diff
in: master

4 files changed

modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -15,14 +15,9 @@
 };
 
 #[derive(Debug, Clone)]
-pub struct SyntaxErrorLocation {
-	pub offset: usize,
-}
-
-#[derive(Debug, Clone)]
 pub struct SyntaxError {
 	pub message: String,
-	pub location: SyntaxErrorLocation,
+	pub location: (u32, u32),
 }
 impl fmt::Display for SyntaxError {
 	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/lib.rs
1//! jsonnet interpreter implementation2#![cfg_attr(nightly, feature(thread_local, type_alias_impl_trait))]34// For jrsonnet-macros5extern crate self as jrsonnet_evaluator;67mod arr;8pub mod async_import;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13pub mod function;14pub mod gc;15mod import;16mod integrations;17pub mod manifest;18mod map;19mod obj;20pub mod stack;21pub mod stdlib;22pub mod tla;23pub mod trace;24pub mod typed;25pub mod val;2627use std::{28	any::Any,29	cell::{RefCell, RefMut},30	clone::Clone,31	collections::hash_map::Entry,32	fmt::{self, Debug},33	marker::PhantomData,34	rc::Rc,35};3637pub use ctx::*;38pub use dynamic::*;39pub use error::{Error, ErrorKind::*, Result, ResultExt};40pub use evaluate::*;41use function::CallLocation;42pub use import::*;43use jrsonnet_gcmodule::{Cc, Trace, cc_dyn};44pub use jrsonnet_interner::{IBytes, IStr};45pub use jrsonnet_ir as parser;46use jrsonnet_ir::{Expr, Source, SourcePath};47#[doc(hidden)]48pub use jrsonnet_macros;4950#[cfg(not(any(feature = "ir-parser", feature = "peg-parser")))]51compile_error!("at least one of `ir-parser` or `peg-parser` features must be enabled");5253pub use error::{SyntaxError, SyntaxErrorLocation};54pub use obj::*;55pub use rustc_hash;56use rustc_hash::FxHashMap;57use stack::check_depth;58pub use tla::apply_tla;59pub use val::{Thunk, Val};6061use crate::gc::WithCapacityExt as _;6263#[allow(clippy::needless_return)]64pub(crate) fn parse_jsonnet(code: &str, source: Source) -> Result<Expr, SyntaxError> {65	#[cfg(feature = "peg-parser")]66	{67		use std::sync::LazyLock;68		static USE_LEGACY_PARSER: LazyLock<bool> =69			LazyLock::new(|| std::env::var_os("JRSONNET_LEGACY_PARSER").is_some());7071		if *USE_LEGACY_PARSER {72			return parse_peg(code, source);73		}74	}75	#[cfg(feature = "ir-parser")]76	{77		return parse_ir(code, source);78	}79	#[cfg(feature = "peg-parser")]80	{81		return parse_peg(code, source);82	}83}8485#[cfg(feature = "ir-parser")]86fn parse_ir(code: &str, source: Source) -> Result<Expr, SyntaxError> {87	jrsonnet_ir_parser::parse(code, &jrsonnet_ir_parser::ParserSettings { source }).map_err(|e| {88		SyntaxError {89			message: e.message,90			location: SyntaxErrorLocation {91				offset: e.location.offset,92			},93		}94	})95}9697#[cfg(feature = "peg-parser")]98fn parse_peg(code: &str, source: Source) -> Result<Expr, SyntaxError> {99	jrsonnet_peg_parser::parse(code, &jrsonnet_peg_parser::ParserSettings { source }).map_err(|e| {100		let message = e101			.expected102			.tokens()103			.find(|t| t.starts_with("!!!"))104			.map_or_else(105				|| {106					format!(107						"expected {}, got {:?}",108						e.expected,109						code.chars()110							.nth(e.location.offset)111							.map_or_else(|| "EOF".into(), |c: char| c.to_string())112					)113				},114				|v| v[3..].into(),115			);116		SyntaxError {117			message,118			location: SyntaxErrorLocation {119				offset: e.location.offset,120			},121		}122	})123}124125cc_dyn!(126	#[derive(Clone)]127	CcUnbound<V>,128	Unbound<Bound = V>129);130131/// Thunk without bound `super`/`this`132/// object inheritance may be overriden multiple times, and will be fixed only on field read133pub trait Unbound: Trace {134	/// Type of value after object context is bound135	type Bound;136	/// Create value bound to specified object context137	fn bind(&self, sup_this: SupThis) -> Result<Self::Bound>;138}139140/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code141/// Standard jsonnet fields are always unbound142#[derive(Clone, Trace)]143pub enum MaybeUnbound {144	/// Value needs to be bound to `this`/`super`145	Unbound(CcUnbound<Val>),146	/// Value is object-independent147	Bound(Thunk<Val>),148}149150impl Debug for MaybeUnbound {151	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {152		write!(f, "MaybeUnbound")153	}154}155impl MaybeUnbound {156	/// Attach object context to value, if required157	pub fn evaluate(&self, sup_this: SupThis) -> Result<Val> {158		match self {159			Self::Unbound(v) => v.0.bind(sup_this),160			Self::Bound(v) => Ok(v.evaluate()?),161		}162	}163}164165cc_dyn!(CcContextInitializer, ContextInitializer);166167/// During import, this trait will be called to create initial context for file.168/// It may initialize global variables, stdlib for example.169pub trait ContextInitializer: Trace {170	/// For which size the builder should be preallocated171	fn reserve_vars(&self) -> usize {172		0173	}174	/// Initialize default file context.175	/// Has default implementation, which calls `populate`.176	/// Prefer to always implement `populate` instead.177	fn initialize(&self, for_file: Source) -> Context {178		let mut builder = ContextBuilder::with_capacity(self.reserve_vars());179		self.populate(for_file, &mut builder);180		builder.build()181	}182	/// For composability: extend builder. May panic if this initialization is not supported,183	/// and the context may only be created via `initialize`.184	fn populate(&self, for_file: Source, builder: &mut ContextBuilder);185	/// Allows upcasting from abstract to concrete context initializer.186	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.187	fn as_any(&self) -> &dyn Any;188}189190/// Context initializer which adds nothing.191impl ContextInitializer for () {192	fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}193	fn as_any(&self) -> &dyn Any {194		self195	}196}197198impl<T> ContextInitializer for Option<T>199where200	T: ContextInitializer,201{202	fn initialize(&self, for_file: Source) -> Context {203		if let Some(ctx) = self {204			ctx.initialize(for_file)205		} else {206			().initialize(for_file)207		}208	}209210	fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {211		if let Some(ctx) = self {212			ctx.populate(for_file, builder);213		}214	}215216	fn as_any(&self) -> &dyn Any {217		self218	}219}220221macro_rules! impl_context_initializer {222	($($gen:ident)*) => {223		#[allow(non_snake_case)]224		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {225			fn reserve_vars(&self) -> usize {226				let mut out = 0;227				let ($($gen,)*) = self;228				$(out += $gen.reserve_vars();)*229				out230			}231			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {232				let ($($gen,)*) = self;233				$($gen.populate(for_file.clone(), builder);)*234			}235			fn as_any(&self) -> &dyn Any {236				self237			}238		}239	};240	($($cur:ident)* @ $c:ident $($rest:ident)*) => {241		impl_context_initializer!($($cur)*);242		impl_context_initializer!($($cur)* $c @ $($rest)*);243	};244	($($cur:ident)* @) => {245		impl_context_initializer!($($cur)*);246	}247}248impl_context_initializer! {249	A @ B C D E F G250}251252#[derive(Trace)]253struct FileData {254	string: Option<IStr>,255	bytes: Option<IBytes>,256	parsed: Option<Rc<Expr>>,257	evaluated: Option<Val>,258259	evaluating: bool,260}261impl FileData {262	fn new_string(data: IStr) -> Self {263		Self {264			string: Some(data),265			bytes: None,266			parsed: None,267			evaluated: None,268			evaluating: false,269		}270	}271	fn new_bytes(data: IBytes) -> Self {272		Self {273			string: None,274			bytes: Some(data),275			parsed: None,276			evaluated: None,277			evaluating: false,278		}279	}280	pub(crate) fn get_string(&mut self) -> Option<IStr> {281		if self.string.is_none() {282			self.string = Some(283				self.bytes284					.as_ref()285					.expect("either string or bytes should be set")286					.clone()287					.cast_str()?,288			);289		}290		Some(self.string.clone().expect("just set"))291	}292}293294#[derive(Trace)]295pub struct EvaluationStateInternals {296	/// Internal state297	file_cache: RefCell<FxHashMap<SourcePath, FileData>>,298	/// Context initializer, which will be used for imports and everything299	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`300	context_initializer: CcContextInitializer,301	/// Used to resolve file locations/contents302	import_resolver: Rc<dyn ImportResolver>,303}304305/// Maintains stack trace and import resolution306#[derive(Clone, Trace)]307pub struct State(Cc<EvaluationStateInternals>);308309thread_local! {310	pub static DEFAULT_STATE: State = State::builder().build();311	pub static STATE: RefCell<Option<State>> = const {RefCell::new(None)};312}313pub struct StateEnterGuard(PhantomData<()>);314impl Drop for StateEnterGuard {315	fn drop(&mut self) {316		STATE.with_borrow_mut(|v| *v = None);317	}318}319320pub fn with_state<V>(v: impl FnOnce(State) -> V) -> V {321	if let Some(state) = STATE.with_borrow(Clone::clone) {322		v(state)323	} else {324		let s = DEFAULT_STATE.with(Clone::clone);325		v(s)326	}327}328329impl State {330	pub fn enter(&self) -> StateEnterGuard {331		self.try_enter().expect("entered state already exists")332	}333	pub fn try_enter(&self) -> Option<StateEnterGuard> {334		STATE.with_borrow_mut(|v| {335			if v.is_none() {336				*v = Some(self.clone());337				Some(StateEnterGuard(PhantomData))338			} else {339				None340			}341		})342	}343	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise344	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {345		let mut file_cache = self.file_cache();346		let mut file = file_cache.entry(path.clone());347348		let file = match file {349			Entry::Occupied(ref mut d) => d.get_mut(),350			Entry::Vacant(v) => {351				let data = self.import_resolver().load_file_contents(&path)?;352				v.insert(FileData::new_string(353					std::str::from_utf8(&data)354						.map_err(|_| ImportBadFileUtf8(path.clone()))?355						.into(),356				))357			}358		};359		Ok(file360			.get_string()361			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)362	}363	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise364	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {365		let mut file_cache = self.file_cache();366		let mut file = file_cache.entry(path.clone());367368		let file = match file {369			Entry::Occupied(ref mut d) => d.get_mut(),370			Entry::Vacant(v) => {371				let data = self.import_resolver().load_file_contents(&path)?;372				v.insert(FileData::new_bytes(data.as_slice().into()))373			}374		};375		if let Some(str) = &file.bytes {376			return Ok(str.clone());377		}378		if file.bytes.is_none() {379			file.bytes = Some(380				file.string381					.as_ref()382					.expect("either string or bytes should be set")383					.clone()384					.cast_bytes(),385			);386		}387		Ok(file.bytes.as_ref().expect("just set").clone())388	}389	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise390	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {391		let mut file_cache = self.file_cache();392		let mut file = file_cache.entry(path.clone());393394		let file = match file {395			Entry::Occupied(ref mut d) => d.get_mut(),396			Entry::Vacant(v) => {397				let data = self.import_resolver().load_file_contents(&path)?;398				v.insert(FileData::new_string(399					std::str::from_utf8(&data)400						.map_err(|_| ImportBadFileUtf8(path.clone()))?401						.into(),402				))403			}404		};405		if let Some(val) = &file.evaluated {406			return Ok(val.clone());407		}408		let code = file409			.get_string()410			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;411		let file_name = Source::new(path.clone(), code.clone());412		if file.parsed.is_none() {413			file.parsed = Some(414				parse_jsonnet(&code, file_name.clone())415					.map(Rc::new)416					.map_err(|e| ImportSyntaxError {417						path: file_name.clone(),418						error: Box::new(e),419					})?,420			);421		}422		let parsed = file.parsed.as_ref().expect("just set").clone();423		if file.evaluating {424			bail!(InfiniteRecursionDetected)425		}426		file.evaluating = true;427		// Dropping file cache guard here, as evaluation may use this map too428		drop(file_cache);429		let res = evaluate(self.create_default_context(file_name), &parsed);430431		let mut file_cache = self.file_cache();432		let mut file = file_cache.entry(path);433434		let Entry::Occupied(file) = &mut file else {435			unreachable!("this file was just here")436		};437		let file = file.get_mut();438		file.evaluating = false;439		match res {440			Ok(v) => {441				file.evaluated = Some(v.clone());442				Ok(v)443			}444			Err(e) => Err(e),445		}446	}447448	/// Has same semantics as `import 'path'` called from `from` file449	pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {450		let resolved = self.resolve_from(from, &path)?;451		self.import_resolved(resolved)452	}453	pub fn import(&self, path: impl AsPathLike) -> Result<Val> {454		let resolved = self.resolve_from_default(&path)?;455		self.import_resolved(resolved)456	}457458	/// Creates context with all passed global variables459	pub fn create_default_context(&self, source: Source) -> Context {460		self.context_initializer().initialize(source)461	}462463	/// Creates context with all passed global variables, calling custom modifier464	pub fn create_default_context_with(465		&self,466		source: Source,467		context_initializer: impl ContextInitializer,468	) -> Context {469		let default_initializer = self.context_initializer();470		let mut builder = ContextBuilder::with_capacity(471			default_initializer.reserve_vars() + context_initializer.reserve_vars(),472		);473		default_initializer.populate(source.clone(), &mut builder);474		context_initializer.populate(source, &mut builder);475476		builder.build()477	}478}479480/// Internals481impl State {482	fn file_cache(&self) -> RefMut<'_, FxHashMap<SourcePath, FileData>> {483		self.0.file_cache.borrow_mut()484	}485}486/// Executes code creating a new stack frame, to be replaced with try{}487pub fn in_frame<T>(488	e: CallLocation<'_>,489	frame_desc: impl FnOnce() -> String,490	f: impl FnOnce() -> Result<T>,491) -> Result<T> {492	let _guard = check_depth()?;493494	f().with_description_src(e, frame_desc)495}496497/// Executes code creating a new stack frame, to be replaced with try{}498pub fn in_description_frame<T>(499	frame_desc: impl FnOnce() -> String,500	f: impl FnOnce() -> Result<T>,501) -> Result<T> {502	let _guard = check_depth()?;503504	f().with_description(frame_desc)505}506507#[derive(Trace)]508pub struct InitialUnderscore(pub Thunk<Val>);509impl ContextInitializer for InitialUnderscore {510	fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {511		builder.bind("_", self.0.clone());512	}513514	fn as_any(&self) -> &dyn Any {515		self516	}517}518519/// Raw methods evaluate passed values but don't perform TLA execution520impl State {521	/// Parses and evaluates the given snippet522	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {523		let code = code.into();524		let source = Source::new_virtual(name.into(), code.clone());525		let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {526			path: source.clone(),527			error: Box::new(e),528		})?;529		evaluate(self.create_default_context(source), &parsed)530	}531	/// Parses and evaluates the given snippet with custom context modifier532	pub fn evaluate_snippet_with(533		&self,534		name: impl Into<IStr>,535		code: impl Into<IStr>,536		context_initializer: impl ContextInitializer,537	) -> Result<Val> {538		let code = code.into();539		let source = Source::new_virtual(name.into(), code.clone());540		let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {541			path: source.clone(),542			error: Box::new(e),543		})?;544		evaluate(545			self.create_default_context_with(source, context_initializer),546			&parsed,547		)548	}549}550551/// Settings utilities552impl State {553	// Only panics in case of [`ImportResolver`] contract violation554	#[allow(clippy::missing_panics_doc)]555	pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {556		self.import_resolver().resolve_from(from, path)557	}558	#[allow(clippy::missing_panics_doc)]559	pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {560		self.import_resolver().resolve_from_default(path)561	}562	pub fn import_resolver(&self) -> &dyn ImportResolver {563		&*self.0.import_resolver564	}565	pub fn context_initializer(&self) -> &dyn ContextInitializer {566		&*self.0.context_initializer.0567	}568}569570impl State {571	pub fn builder() -> StateBuilder {572		StateBuilder::default()573	}574}575576impl Default for State {577	fn default() -> Self {578		Self::builder().build()579	}580}581582#[derive(Default)]583pub struct StateBuilder {584	import_resolver: Option<Rc<dyn ImportResolver>>,585	context_initializer: Option<CcContextInitializer>,586}587impl StateBuilder {588	pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {589		let _ = self.import_resolver.insert(Rc::new(import_resolver));590		self591	}592	pub fn context_initializer(593		&mut self,594		context_initializer: impl ContextInitializer,595	) -> &mut Self {596		let _ = self597			.context_initializer598			.insert(CcContextInitializer::new(context_initializer));599		self600	}601	pub fn build(mut self) -> State {602		State(Cc::new(EvaluationStateInternals {603			file_cache: RefCell::new(FxHashMap::new()),604			context_initializer: self605				.context_initializer606				.take()607				.unwrap_or_else(|| CcContextInitializer::new(())),608			import_resolver: self609				.import_resolver610				.take()611				.unwrap_or_else(|| Rc::new(DummyImportResolver)),612		}))613	}614}
after · crates/jrsonnet-evaluator/src/lib.rs
1//! jsonnet interpreter implementation2#![cfg_attr(nightly, feature(thread_local, type_alias_impl_trait))]34// For jrsonnet-macros5extern crate self as jrsonnet_evaluator;67mod arr;8pub mod async_import;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13pub mod function;14pub mod gc;15mod import;16mod integrations;17pub mod manifest;18mod map;19mod obj;20pub mod stack;21pub mod stdlib;22pub mod tla;23pub mod trace;24pub mod typed;25pub mod val;2627use std::{28	any::Any,29	cell::{RefCell, RefMut},30	clone::Clone,31	collections::hash_map::Entry,32	fmt::{self, Debug},33	marker::PhantomData,34	rc::Rc,35};3637pub use ctx::*;38pub use dynamic::*;39pub use error::{Error, ErrorKind::*, Result, ResultExt};40pub use evaluate::*;41use function::CallLocation;42pub use import::*;43use jrsonnet_gcmodule::{Cc, Trace, cc_dyn};44pub use jrsonnet_interner::{IBytes, IStr};45pub use jrsonnet_ir as parser;46use jrsonnet_ir::{Expr, Source, SourcePath};47#[doc(hidden)]48pub use jrsonnet_macros;4950#[cfg(not(any(feature = "ir-parser", feature = "peg-parser")))]51compile_error!("at least one of `ir-parser` or `peg-parser` features must be enabled");5253pub use error::SyntaxError;54pub use obj::*;55pub use rustc_hash;56use rustc_hash::FxHashMap;57use stack::check_depth;58pub use tla::apply_tla;59pub use val::{Thunk, Val};6061use crate::gc::WithCapacityExt as _;6263#[allow(clippy::needless_return)]64pub(crate) fn parse_jsonnet(code: &str, source: Source) -> Result<Expr, SyntaxError> {65	#[cfg(feature = "peg-parser")]66	{67		use std::sync::LazyLock;68		static USE_LEGACY_PARSER: LazyLock<bool> =69			LazyLock::new(|| std::env::var_os("JRSONNET_LEGACY_PARSER").is_some());7071		if *USE_LEGACY_PARSER {72			return parse_peg(code, source);73		}74	}75	#[cfg(feature = "ir-parser")]76	{77		return parse_ir(code, source);78	}79	#[cfg(feature = "peg-parser")]80	{81		return parse_peg(code, source);82	}83}8485#[cfg(feature = "ir-parser")]86fn parse_ir(code: &str, source: Source) -> Result<Expr, SyntaxError> {87	jrsonnet_ir_parser::parse(code, &jrsonnet_ir_parser::ParserSettings { source }).map_err(|e| {88		SyntaxError {89			message: e.message,90			location: (e.location.0, e.location.1),91		}92	})93}9495#[cfg(feature = "peg-parser")]96fn parse_peg(code: &str, source: Source) -> Result<Expr, SyntaxError> {97	jrsonnet_peg_parser::parse(code, &jrsonnet_peg_parser::ParserSettings { source }).map_err(|e| {98		let message = e99			.expected100			.tokens()101			.find(|t| t.starts_with("!!!"))102			.map_or_else(103				|| {104					format!(105						"expected {}, got {:?}",106						e.expected,107						code.chars()108							.nth(e.location.0)109							.map_or_else(|| "EOF".into(), |c: char| c.to_string())110					)111				},112				|v| v[3..].into(),113			);114		SyntaxError {115			message,116			location: e.location,117		}118	})119}120121cc_dyn!(122	#[derive(Clone)]123	CcUnbound<V>,124	Unbound<Bound = V>125);126127/// Thunk without bound `super`/`this`128/// object inheritance may be overriden multiple times, and will be fixed only on field read129pub trait Unbound: Trace {130	/// Type of value after object context is bound131	type Bound;132	/// Create value bound to specified object context133	fn bind(&self, sup_this: SupThis) -> Result<Self::Bound>;134}135136/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code137/// Standard jsonnet fields are always unbound138#[derive(Clone, Trace)]139pub enum MaybeUnbound {140	/// Value needs to be bound to `this`/`super`141	Unbound(CcUnbound<Val>),142	/// Value is object-independent143	Bound(Thunk<Val>),144}145146impl Debug for MaybeUnbound {147	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {148		write!(f, "MaybeUnbound")149	}150}151impl MaybeUnbound {152	/// Attach object context to value, if required153	pub fn evaluate(&self, sup_this: SupThis) -> Result<Val> {154		match self {155			Self::Unbound(v) => v.0.bind(sup_this),156			Self::Bound(v) => Ok(v.evaluate()?),157		}158	}159}160161cc_dyn!(CcContextInitializer, ContextInitializer);162163/// During import, this trait will be called to create initial context for file.164/// It may initialize global variables, stdlib for example.165pub trait ContextInitializer: Trace {166	/// For which size the builder should be preallocated167	fn reserve_vars(&self) -> usize {168		0169	}170	/// Initialize default file context.171	/// Has default implementation, which calls `populate`.172	/// Prefer to always implement `populate` instead.173	fn initialize(&self, for_file: Source) -> Context {174		let mut builder = ContextBuilder::with_capacity(self.reserve_vars());175		self.populate(for_file, &mut builder);176		builder.build()177	}178	/// For composability: extend builder. May panic if this initialization is not supported,179	/// and the context may only be created via `initialize`.180	fn populate(&self, for_file: Source, builder: &mut ContextBuilder);181	/// Allows upcasting from abstract to concrete context initializer.182	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.183	fn as_any(&self) -> &dyn Any;184}185186/// Context initializer which adds nothing.187impl ContextInitializer for () {188	fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}189	fn as_any(&self) -> &dyn Any {190		self191	}192}193194impl<T> ContextInitializer for Option<T>195where196	T: ContextInitializer,197{198	fn initialize(&self, for_file: Source) -> Context {199		if let Some(ctx) = self {200			ctx.initialize(for_file)201		} else {202			().initialize(for_file)203		}204	}205206	fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {207		if let Some(ctx) = self {208			ctx.populate(for_file, builder);209		}210	}211212	fn as_any(&self) -> &dyn Any {213		self214	}215}216217macro_rules! impl_context_initializer {218	($($gen:ident)*) => {219		#[allow(non_snake_case)]220		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {221			fn reserve_vars(&self) -> usize {222				let mut out = 0;223				let ($($gen,)*) = self;224				$(out += $gen.reserve_vars();)*225				out226			}227			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {228				let ($($gen,)*) = self;229				$($gen.populate(for_file.clone(), builder);)*230			}231			fn as_any(&self) -> &dyn Any {232				self233			}234		}235	};236	($($cur:ident)* @ $c:ident $($rest:ident)*) => {237		impl_context_initializer!($($cur)*);238		impl_context_initializer!($($cur)* $c @ $($rest)*);239	};240	($($cur:ident)* @) => {241		impl_context_initializer!($($cur)*);242	}243}244impl_context_initializer! {245	A @ B C D E F G246}247248#[derive(Trace)]249struct FileData {250	string: Option<IStr>,251	bytes: Option<IBytes>,252	parsed: Option<Rc<Expr>>,253	evaluated: Option<Val>,254255	evaluating: bool,256}257impl FileData {258	fn new_string(data: IStr) -> Self {259		Self {260			string: Some(data),261			bytes: None,262			parsed: None,263			evaluated: None,264			evaluating: false,265		}266	}267	fn new_bytes(data: IBytes) -> Self {268		Self {269			string: None,270			bytes: Some(data),271			parsed: None,272			evaluated: None,273			evaluating: false,274		}275	}276	pub(crate) fn get_string(&mut self) -> Option<IStr> {277		if self.string.is_none() {278			self.string = Some(279				self.bytes280					.as_ref()281					.expect("either string or bytes should be set")282					.clone()283					.cast_str()?,284			);285		}286		Some(self.string.clone().expect("just set"))287	}288}289290#[derive(Trace)]291pub struct EvaluationStateInternals {292	/// Internal state293	file_cache: RefCell<FxHashMap<SourcePath, FileData>>,294	/// Context initializer, which will be used for imports and everything295	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`296	context_initializer: CcContextInitializer,297	/// Used to resolve file locations/contents298	import_resolver: Rc<dyn ImportResolver>,299}300301/// Maintains stack trace and import resolution302#[derive(Clone, Trace)]303pub struct State(Cc<EvaluationStateInternals>);304305thread_local! {306	pub static DEFAULT_STATE: State = State::builder().build();307	pub static STATE: RefCell<Option<State>> = const {RefCell::new(None)};308}309pub struct StateEnterGuard(PhantomData<()>);310impl Drop for StateEnterGuard {311	fn drop(&mut self) {312		STATE.with_borrow_mut(|v| *v = None);313	}314}315316pub fn with_state<V>(v: impl FnOnce(State) -> V) -> V {317	if let Some(state) = STATE.with_borrow(Clone::clone) {318		v(state)319	} else {320		let s = DEFAULT_STATE.with(Clone::clone);321		v(s)322	}323}324325impl State {326	pub fn enter(&self) -> StateEnterGuard {327		self.try_enter().expect("entered state already exists")328	}329	pub fn try_enter(&self) -> Option<StateEnterGuard> {330		STATE.with_borrow_mut(|v| {331			if v.is_none() {332				*v = Some(self.clone());333				Some(StateEnterGuard(PhantomData))334			} else {335				None336			}337		})338	}339	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise340	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {341		let mut file_cache = self.file_cache();342		let mut file = file_cache.entry(path.clone());343344		let file = match file {345			Entry::Occupied(ref mut d) => d.get_mut(),346			Entry::Vacant(v) => {347				let data = self.import_resolver().load_file_contents(&path)?;348				v.insert(FileData::new_string(349					std::str::from_utf8(&data)350						.map_err(|_| ImportBadFileUtf8(path.clone()))?351						.into(),352				))353			}354		};355		Ok(file356			.get_string()357			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)358	}359	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise360	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {361		let mut file_cache = self.file_cache();362		let mut file = file_cache.entry(path.clone());363364		let file = match file {365			Entry::Occupied(ref mut d) => d.get_mut(),366			Entry::Vacant(v) => {367				let data = self.import_resolver().load_file_contents(&path)?;368				v.insert(FileData::new_bytes(data.as_slice().into()))369			}370		};371		if let Some(str) = &file.bytes {372			return Ok(str.clone());373		}374		if file.bytes.is_none() {375			file.bytes = Some(376				file.string377					.as_ref()378					.expect("either string or bytes should be set")379					.clone()380					.cast_bytes(),381			);382		}383		Ok(file.bytes.as_ref().expect("just set").clone())384	}385	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise386	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {387		let mut file_cache = self.file_cache();388		let mut file = file_cache.entry(path.clone());389390		let file = match file {391			Entry::Occupied(ref mut d) => d.get_mut(),392			Entry::Vacant(v) => {393				let data = self.import_resolver().load_file_contents(&path)?;394				v.insert(FileData::new_string(395					std::str::from_utf8(&data)396						.map_err(|_| ImportBadFileUtf8(path.clone()))?397						.into(),398				))399			}400		};401		if let Some(val) = &file.evaluated {402			return Ok(val.clone());403		}404		let code = file405			.get_string()406			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;407		let file_name = Source::new(path.clone(), code.clone());408		if file.parsed.is_none() {409			file.parsed = Some(410				parse_jsonnet(&code, file_name.clone())411					.map(Rc::new)412					.map_err(|e| ImportSyntaxError {413						path: file_name.clone(),414						error: Box::new(e),415					})?,416			);417		}418		let parsed = file.parsed.as_ref().expect("just set").clone();419		if file.evaluating {420			bail!(InfiniteRecursionDetected)421		}422		file.evaluating = true;423		// Dropping file cache guard here, as evaluation may use this map too424		drop(file_cache);425		let res = evaluate(self.create_default_context(file_name), &parsed);426427		let mut file_cache = self.file_cache();428		let mut file = file_cache.entry(path);429430		let Entry::Occupied(file) = &mut file else {431			unreachable!("this file was just here")432		};433		let file = file.get_mut();434		file.evaluating = false;435		match res {436			Ok(v) => {437				file.evaluated = Some(v.clone());438				Ok(v)439			}440			Err(e) => Err(e),441		}442	}443444	/// Has same semantics as `import 'path'` called from `from` file445	pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {446		let resolved = self.resolve_from(from, &path)?;447		self.import_resolved(resolved)448	}449	pub fn import(&self, path: impl AsPathLike) -> Result<Val> {450		let resolved = self.resolve_from_default(&path)?;451		self.import_resolved(resolved)452	}453454	/// Creates context with all passed global variables455	pub fn create_default_context(&self, source: Source) -> Context {456		self.context_initializer().initialize(source)457	}458459	/// Creates context with all passed global variables, calling custom modifier460	pub fn create_default_context_with(461		&self,462		source: Source,463		context_initializer: impl ContextInitializer,464	) -> Context {465		let default_initializer = self.context_initializer();466		let mut builder = ContextBuilder::with_capacity(467			default_initializer.reserve_vars() + context_initializer.reserve_vars(),468		);469		default_initializer.populate(source.clone(), &mut builder);470		context_initializer.populate(source, &mut builder);471472		builder.build()473	}474}475476/// Internals477impl State {478	fn file_cache(&self) -> RefMut<'_, FxHashMap<SourcePath, FileData>> {479		self.0.file_cache.borrow_mut()480	}481}482/// Executes code creating a new stack frame, to be replaced with try{}483pub fn in_frame<T>(484	e: CallLocation<'_>,485	frame_desc: impl FnOnce() -> String,486	f: impl FnOnce() -> Result<T>,487) -> Result<T> {488	let _guard = check_depth()?;489490	f().with_description_src(e, frame_desc)491}492493/// Executes code creating a new stack frame, to be replaced with try{}494pub fn in_description_frame<T>(495	frame_desc: impl FnOnce() -> String,496	f: impl FnOnce() -> Result<T>,497) -> Result<T> {498	let _guard = check_depth()?;499500	f().with_description(frame_desc)501}502503#[derive(Trace)]504pub struct InitialUnderscore(pub Thunk<Val>);505impl ContextInitializer for InitialUnderscore {506	fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {507		builder.bind("_", self.0.clone());508	}509510	fn as_any(&self) -> &dyn Any {511		self512	}513}514515/// Raw methods evaluate passed values but don't perform TLA execution516impl State {517	/// Parses and evaluates the given snippet518	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {519		let code = code.into();520		let source = Source::new_virtual(name.into(), code.clone());521		let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {522			path: source.clone(),523			error: Box::new(e),524		})?;525		evaluate(self.create_default_context(source), &parsed)526	}527	/// Parses and evaluates the given snippet with custom context modifier528	pub fn evaluate_snippet_with(529		&self,530		name: impl Into<IStr>,531		code: impl Into<IStr>,532		context_initializer: impl ContextInitializer,533	) -> Result<Val> {534		let code = code.into();535		let source = Source::new_virtual(name.into(), code.clone());536		let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {537			path: source.clone(),538			error: Box::new(e),539		})?;540		evaluate(541			self.create_default_context_with(source, context_initializer),542			&parsed,543		)544	}545}546547/// Settings utilities548impl State {549	// Only panics in case of [`ImportResolver`] contract violation550	#[allow(clippy::missing_panics_doc)]551	pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {552		self.import_resolver().resolve_from(from, path)553	}554	#[allow(clippy::missing_panics_doc)]555	pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {556		self.import_resolver().resolve_from_default(path)557	}558	pub fn import_resolver(&self) -> &dyn ImportResolver {559		&*self.0.import_resolver560	}561	pub fn context_initializer(&self) -> &dyn ContextInitializer {562		&*self.0.context_initializer.0563	}564}565566impl State {567	pub fn builder() -> StateBuilder {568		StateBuilder::default()569	}570}571572impl Default for State {573	fn default() -> Self {574		Self::builder().build()575	}576}577578#[derive(Default)]579pub struct StateBuilder {580	import_resolver: Option<Rc<dyn ImportResolver>>,581	context_initializer: Option<CcContextInitializer>,582}583impl StateBuilder {584	pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {585		let _ = self.import_resolver.insert(Rc::new(import_resolver));586		self587	}588	pub fn context_initializer(589		&mut self,590		context_initializer: impl ContextInitializer,591	) -> &mut Self {592		let _ = self593			.context_initializer594			.insert(CcContextInitializer::new(context_initializer));595		self596	}597	pub fn build(mut self) -> State {598		State(Cc::new(EvaluationStateInternals {599			file_cache: RefCell::new(FxHashMap::new()),600			context_initializer: self601				.context_initializer602				.take()603				.unwrap_or_else(|| CcContextInitializer::new(())),604			import_resolver: self605				.import_resolver606				.take()607				.unwrap_or_else(|| Rc::new(DummyImportResolver)),608		}))609	}610}
modifiedcrates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -122,7 +122,7 @@
 				|| path.source_path().to_string(),
 				|r| self.resolver.resolve(r),
 			);
-			let mut offset = error.location.offset;
+			let mut offset = error.location.0 as usize;
 			let is_eof = if offset >= path.code().len() {
 				offset = path.code().len().saturating_sub(1);
 				true
@@ -263,11 +263,15 @@
 		write!(out, "{}", error.error())?;
 		if let ErrorKind::ImportSyntaxError { path, error } = error.error() {
 			writeln!(out)?;
-			let offset = error.location.offset;
+			let mut offset = error.location;
+			// To inclusive range
+			if offset.1 > offset.0 {
+				offset.1 -= 1;
+			}
 			let mut builder = SnippetBuilder::new(path.code());
 			builder
 				.error(Text::fragment("syntax error", Formatting::default()))
-				.range(offset..=offset)
+				.range(offset.0 as usize..=offset.1 as usize)
 				.build();
 			let source = builder.build();
 			let ansi = source_to_ansi(&source);
modifiedcrates/jrsonnet-ir-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-ir-parser/src/lib.rs
+++ b/crates/jrsonnet-ir-parser/src/lib.rs
@@ -7,21 +7,16 @@
 	ImportKind, IndexPart, LiteralType, Member, ObjBody, ObjComp, ObjMembers, Slice, SliceDesc,
 	Source, Span, Spanned, UnaryOpType, Visibility, unescape,
 };
-use jrsonnet_lexer::{Lexeme, Lexer, SyntaxKind, T, collect_lexed_str_block};
+use jrsonnet_lexer::{Lexeme, Lexer, Span as LexSpan, SyntaxKind, T, collect_lexed_str_block};
 
 pub struct ParserSettings {
 	pub source: Source,
-}
-
-#[derive(Debug, Clone)]
-pub struct ParseErrorLocation {
-	pub offset: usize,
 }
 
 #[derive(Debug, Clone)]
 pub struct ParseError {
 	pub message: String,
-	pub location: ParseErrorLocation,
+	pub location: LexSpan,
 }
 
 impl std::fmt::Display for ParseError {
@@ -131,9 +126,7 @@
 
 	fn error(&self, message: String) -> ParseError {
 		ParseError {
-			location: ParseErrorLocation {
-				offset: self.span_start() as usize,
-			},
+			location: self.lexemes[self.offset].range,
 			message,
 		}
 	}
@@ -143,9 +136,6 @@
 			return Err(self.error(format!("expected identifier, got {}", self.current_desc())));
 		}
 		let text = self.text();
-		if is_reserved(text) {
-			return Err(self.error(format!("expected identifier, got reserved word '{text}'")));
-		}
 		let s: IStr = text.into();
 		self.eat_any();
 		Ok(s)
@@ -156,23 +146,6 @@
 	}
 }
 
-fn is_reserved(s: &str) -> bool {
-	matches!(
-		s,
-		"assert"
-			| "else" | "error"
-			| "false" | "for"
-			| "function"
-			| "if" | "import"
-			| "importstr"
-			| "importbin"
-			| "in" | "local"
-			| "null" | "tailstrict"
-			| "then" | "self"
-			| "super" | "true"
-	)
-}
-
 fn spanned<T: Acyclic>(
 	p: &mut Parser<'_>,
 	cb: impl FnOnce(&mut Parser<'_>) -> Result<T>,
@@ -1040,9 +1013,7 @@
 		if let Some(desc) = lexeme.kind.error_description() {
 			return Err(ParseError {
 				message: desc.to_owned(),
-				location: ParseErrorLocation {
-					offset: lexeme.range.0 as usize,
-				},
+				location: lexeme.range,
 			});
 		}
 	}