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

difftreelog

feat(evaluator) track object field source location

Лач2020-07-01parent: #7d9fe5c.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -276,6 +276,7 @@
 								})?))
 							}),
 						)),
+						location: value.1.clone(),
 					},
 				);
 			}
@@ -305,6 +306,7 @@
 								)))
 							}),
 						)),
+						location: value.1.clone(),
 					},
 				);
 			}
@@ -368,6 +370,7 @@
 								add: false,
 								visibility: Visibility::Normal,
 								invoke: v,
+								location: obj.value.1.clone(),
 							},
 						);
 					}
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/obj.rs
1use crate::{evaluate_add_op, LazyBinding, Result, Val};2use indexmap::IndexMap;3use jrsonnet_parser::{ExprLocation, Visibility};4use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};56#[derive(Debug)]7pub struct ObjMember {8	pub add: bool,9	pub visibility: Visibility,10	pub invoke: LazyBinding,11}1213#[derive(Debug)]14pub struct ObjValueInternals {15	super_obj: Option<ObjValue>,16	this_entries: Rc<HashMap<Rc<str>, ObjMember>>,17	value_cache: RefCell<HashMap<Rc<str>, Val>>,18}19#[derive(Clone)]20pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);21impl Debug for ObjValue {22	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {23		if let Some(super_obj) = self.0.super_obj.as_ref() {24			if f.alternate() {25				write!(f, "{:#?}", super_obj)?;26			} else {27				write!(f, "{:?}", super_obj)?;28			}29			write!(f, " + ")?;30		}31		let mut debug = f.debug_struct("ObjValue");32		for (name, member) in self.0.this_entries.iter() {33			debug.field(name, member);34		}35		debug.finish_non_exhaustive()36	}37}3839impl ObjValue {40	pub fn new(41		super_obj: Option<ObjValue>,42		this_entries: Rc<HashMap<Rc<str>, ObjMember>>,43	) -> ObjValue {44		ObjValue(Rc::new(ObjValueInternals {45			super_obj,46			this_entries,47			value_cache: RefCell::new(HashMap::new()),48		}))49	}50	pub fn new_empty() -> ObjValue {51		Self::new(None, Rc::new(HashMap::new()))52	}53	pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {54		match &self.0.super_obj {55			None => ObjValue::new(Some(super_obj), self.0.this_entries.clone()),56			Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),57		}58	}59	pub fn enum_fields(&self, handler: &impl Fn(&Rc<str>, &Visibility)) {60		if let Some(s) = &self.0.super_obj {61			s.enum_fields(handler);62		}63		for (name, member) in self.0.this_entries.iter() {64			handler(&name, &member.visibility);65		}66	}67	pub fn fields_visibility(&self) -> IndexMap<Rc<str>, bool> {68		let out = Rc::new(RefCell::new(IndexMap::new()));69		self.enum_fields(&|name, visibility| {70			let mut out = out.borrow_mut();71			match visibility {72				Visibility::Normal => {73					if !out.contains_key(name) {74						out.insert(name.to_owned(), true);75					}76				}77				Visibility::Hidden => {78					out.insert(name.to_owned(), false);79				}80				Visibility::Unhide => {81					out.insert(name.to_owned(), true);82				}83			};84		});85		Rc::try_unwrap(out).unwrap().into_inner()86	}87	pub fn visible_fields(&self) -> Vec<Rc<str>> {88		self.fields_visibility()89			.into_iter()90			.filter(|(_k, v)| *v)91			.map(|(k, _)| k)92			.collect()93	}94	pub fn get(&self, key: Rc<str>) -> Result<Option<Val>> {95		if let Some(v) = self.0.value_cache.borrow().get(&key) {96			return Ok(Some(v.clone()));97		}98		if let Some(v) = self.get_raw(&key, self)? {99			let v = v.unwrap_if_lazy()?;100			self.0.value_cache.borrow_mut().insert(key, v.clone());101			Ok(Some(v))102		} else {103			Ok(None)104		}105	}106	pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {107		match (self.0.this_entries.get(key), &self.0.super_obj) {108			(Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),109			(Some(k), Some(s)) => {110				let our = self.evaluate_this(k, real_this)?;111				if k.add {112					s.get_raw(key, real_this)?113						.map_or(Ok(Some(our.clone())), |v| {114							Ok(Some(evaluate_add_op(&v, &our)?))115						})116				} else {117					Ok(Some(our))118				}119			}120			(None, Some(s)) => s.get_raw(key, real_this),121			(None, None) => Ok(None),122		}123	}124	fn evaluate_this(&self, v: &ObjMember, real_this: &ObjValue) -> Result<Val> {125		Ok(v.invoke126			.evaluate(Some(real_this.clone()), self.0.super_obj.clone())?127			.evaluate()?)128	}129}130impl PartialEq for ObjValue {131	fn eq(&self, other: &Self) -> bool {132		Rc::ptr_eq(&self.0, &other.0)133	}134}
after · crates/jrsonnet-evaluator/src/obj.rs
1use crate::{evaluate_add_op, LazyBinding, Result, Val};2use indexmap::IndexMap;3use jrsonnet_parser::{ExprLocation, Visibility};4use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};56#[derive(Debug)]7pub struct ObjMember {8	pub add: bool,9	pub visibility: Visibility,10	pub invoke: LazyBinding,11	pub location: Option<ExprLocation>,12}1314#[derive(Debug)]15pub struct ObjValueInternals {16	super_obj: Option<ObjValue>,17	this_entries: Rc<HashMap<Rc<str>, ObjMember>>,18	value_cache: RefCell<HashMap<Rc<str>, Val>>,19}20#[derive(Clone)]21pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);22impl Debug for ObjValue {23	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {24		if let Some(super_obj) = self.0.super_obj.as_ref() {25			if f.alternate() {26				write!(f, "{:#?}", super_obj)?;27			} else {28				write!(f, "{:?}", super_obj)?;29			}30			write!(f, " + ")?;31		}32		let mut debug = f.debug_struct("ObjValue");33		for (name, member) in self.0.this_entries.iter() {34			debug.field(name, member);35		}36		debug.finish_non_exhaustive()37	}38}3940impl ObjValue {41	pub fn new(42		super_obj: Option<ObjValue>,43		this_entries: Rc<HashMap<Rc<str>, ObjMember>>,44	) -> ObjValue {45		ObjValue(Rc::new(ObjValueInternals {46			super_obj,47			this_entries,48			value_cache: RefCell::new(HashMap::new()),49		}))50	}51	pub fn new_empty() -> ObjValue {52		Self::new(None, Rc::new(HashMap::new()))53	}54	pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {55		match &self.0.super_obj {56			None => ObjValue::new(Some(super_obj), self.0.this_entries.clone()),57			Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),58		}59	}60	pub fn enum_fields(&self, handler: &impl Fn(&Rc<str>, &Visibility)) {61		if let Some(s) = &self.0.super_obj {62			s.enum_fields(handler);63		}64		for (name, member) in self.0.this_entries.iter() {65			handler(&name, &member.visibility);66		}67	}68	pub fn fields_visibility(&self) -> IndexMap<Rc<str>, bool> {69		let out = Rc::new(RefCell::new(IndexMap::new()));70		self.enum_fields(&|name, visibility| {71			let mut out = out.borrow_mut();72			match visibility {73				Visibility::Normal => {74					if !out.contains_key(name) {75						out.insert(name.to_owned(), true);76					}77				}78				Visibility::Hidden => {79					out.insert(name.to_owned(), false);80				}81				Visibility::Unhide => {82					out.insert(name.to_owned(), true);83				}84			};85		});86		Rc::try_unwrap(out).unwrap().into_inner()87	}88	pub fn visible_fields(&self) -> Vec<Rc<str>> {89		self.fields_visibility()90			.into_iter()91			.filter(|(_k, v)| *v)92			.map(|(k, _)| k)93			.collect()94	}95	pub fn get(&self, key: Rc<str>) -> Result<Option<Val>> {96		if let Some(v) = self.0.value_cache.borrow().get(&key) {97			return Ok(Some(v.clone()));98		}99		if let Some(v) = self.get_raw(&key, self)? {100			let v = v.unwrap_if_lazy()?;101			self.0.value_cache.borrow_mut().insert(key, v.clone());102			Ok(Some(v))103		} else {104			Ok(None)105		}106	}107	pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {108		match (self.0.this_entries.get(key), &self.0.super_obj) {109			(Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),110			(Some(k), Some(s)) => {111				let our = self.evaluate_this(k, real_this)?;112				if k.add {113					s.get_raw(key, real_this)?114						.map_or(Ok(Some(our.clone())), |v| {115							Ok(Some(evaluate_add_op(&v, &our)?))116						})117				} else {118					Ok(Some(our))119				}120			}121			(None, Some(s)) => s.get_raw(key, real_this),122			(None, None) => Ok(None),123		}124	}125	fn evaluate_this(&self, v: &ObjMember, real_this: &ObjValue) -> Result<Val> {126		Ok(v.invoke127			.evaluate(Some(real_this.clone()), self.0.super_obj.clone())?128			.evaluate()?)129	}130}131impl PartialEq for ObjValue {132	fn eq(&self, other: &Self) -> bool {133		Rc::ptr_eq(&self.0, &other.0)134	}135}