Refine posistion display.

This commit is contained in:
Stephen Chung 2021-04-23 14:24:53 +08:00
parent 1fbbb2a40d
commit fc782c5563
5 changed files with 79 additions and 115 deletions

View File

@ -780,12 +780,8 @@ pub struct Ident {
impl fmt::Debug for Ident { impl fmt::Debug for Ident {
#[inline(always)] #[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[cfg(not(feature = "no_position"))]
write!(f, "{:?} @ {:?}", self.name, self.pos)?;
#[cfg(feature = "no_position")]
write!(f, "{:?}", self.name)?; write!(f, "{:?}", self.name)?;
self.pos.debug_print(f)
Ok(())
} }
} }
@ -879,10 +875,7 @@ impl fmt::Debug for StmtBlock {
#[inline(always)] #[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)?; fmt::Debug::fmt(&self.0, f)?;
if !self.1.is_none() { self.1.debug_print(f)
write!(f, " @ {:?}", self.1)?;
}
Ok(())
} }
} }
@ -1691,60 +1684,33 @@ impl Default for Expr {
impl fmt::Debug for Expr { impl fmt::Debug for Expr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { let mut display_pos = self.position();
#[cfg(not(feature = "no_position"))]
Self::DynamicConstant(value, pos) => write!(f, "{:?} @ {:?}", value, pos),
#[cfg(not(feature = "no_position"))]
Self::BoolConstant(value, pos) => write!(f, "{:?} @ {:?}", value, pos),
#[cfg(not(feature = "no_position"))]
Self::IntegerConstant(value, pos) => write!(f, "{:?} @ {:?}", value, pos),
#[cfg(not(feature = "no_float"))]
#[cfg(not(feature = "no_position"))]
Self::FloatConstant(value, pos) => write!(f, "{:?} @ {:?}", value, pos),
#[cfg(not(feature = "no_position"))]
Self::CharConstant(value, pos) => write!(f, "{:?} @ {:?}", value, pos),
#[cfg(not(feature = "no_position"))]
Self::StringConstant(value, pos) => write!(f, "{:?} @ {:?}", value, pos),
#[cfg(not(feature = "no_position"))]
Self::Unit(pos) => write!(f, "() @ {:?}", pos),
#[cfg(feature = "no_position")] match self {
Self::DynamicConstant(value, _) => write!(f, "{:?}", value), Self::DynamicConstant(value, _) => write!(f, "{:?}", value),
#[cfg(feature = "no_position")]
Self::BoolConstant(value, _) => write!(f, "{:?}", value), Self::BoolConstant(value, _) => write!(f, "{:?}", value),
#[cfg(feature = "no_position")]
Self::IntegerConstant(value, _) => write!(f, "{:?}", value), Self::IntegerConstant(value, _) => write!(f, "{:?}", value),
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
#[cfg(feature = "no_position")]
Self::FloatConstant(value, _) => write!(f, "{:?}", value), Self::FloatConstant(value, _) => write!(f, "{:?}", value),
#[cfg(feature = "no_position")]
Self::CharConstant(value, _) => write!(f, "{:?}", value), Self::CharConstant(value, _) => write!(f, "{:?}", value),
#[cfg(feature = "no_position")]
Self::StringConstant(value, _) => write!(f, "{:?}", value), Self::StringConstant(value, _) => write!(f, "{:?}", value),
#[cfg(feature = "no_position")]
Self::Unit(_) => f.write_str("()"), Self::Unit(_) => f.write_str("()"),
Self::InterpolatedString(x) => { Self::InterpolatedString(x) => {
f.write_str("InterpolatedString")?; f.write_str("InterpolatedString")?;
return f.debug_list().entries(x.iter()).finish();
}
Self::Array(x, _) => {
f.write_str("Array")?;
f.debug_list().entries(x.iter()).finish() f.debug_list().entries(x.iter()).finish()
} }
Self::Array(x, _pos) => { Self::Map(x, _) => {
f.write_str("Array")?;
f.debug_list().entries(x.iter()).finish()?;
#[cfg(not(feature = "no_position"))]
write!(f, " @ {:?}", _pos)?;
Ok(())
}
Self::Map(x, _pos) => {
f.write_str("Map")?; f.write_str("Map")?;
f.debug_map() f.debug_map()
.entries(x.0.iter().map(|(k, v)| (k, v))) .entries(x.0.iter().map(|(k, v)| (k, v)))
.finish()?; .finish()
#[cfg(not(feature = "no_position"))]
write!(f, " @ {:?}", _pos)?;
Ok(())
} }
Self::Variable(i, _pos, x) => { Self::Variable(i, _, x) => {
f.write_str("Variable(")?; f.write_str("Variable(")?;
match x.1 { match x.1 {
Some((_, ref namespace)) => write!(f, "{}", namespace)?, Some((_, ref namespace)) => write!(f, "{}", namespace)?,
@ -1755,23 +1721,14 @@ impl fmt::Debug for Expr {
Some(n) => write!(f, ", {}", n)?, Some(n) => write!(f, ", {}", n)?,
_ => (), _ => (),
} }
f.write_str(")")?; f.write_str(")")
#[cfg(not(feature = "no_position"))]
write!(f, " @ {:?}", _pos)?;
Ok(())
} }
#[cfg(not(feature = "no_position"))] Self::Property(x) => write!(f, "Property({})", x.2.name),
Self::Property(x) => write!(f, "Property({:?} @ {:?})", x.2.name, x.2.pos),
#[cfg(feature = "no_position")]
Self::Property(x) => write!(f, "Property({:?})", x.2.name),
Self::Stmt(x) => { Self::Stmt(x) => {
f.write_str("Stmt")?; f.write_str("Stmt")?;
f.debug_list().entries(x.0.iter()).finish()?; f.debug_list().entries(x.0.iter()).finish()
#[cfg(not(feature = "no_position"))]
write!(f, " @ {:?}", x.1)?;
Ok(())
} }
Self::FnCall(x, _pos) => { Self::FnCall(x, _) => {
let mut ff = f.debug_struct("FnCall"); let mut ff = f.debug_struct("FnCall");
if let Some(ref ns) = x.namespace { if let Some(ref ns) = x.namespace {
ff.field("namespace", ns); ff.field("namespace", ns);
@ -1785,12 +1742,9 @@ impl fmt::Debug for Expr {
if x.capture { if x.capture {
ff.field("capture", &x.capture); ff.field("capture", &x.capture);
} }
ff.finish()?; ff.finish()
#[cfg(not(feature = "no_position"))]
write!(f, " @ {:?}", _pos)?;
Ok(())
} }
Self::Dot(x, _pos) | Self::Index(x, _pos) | Self::And(x, _pos) | Self::Or(x, _pos) => { Self::Dot(x, pos) | Self::Index(x, pos) | Self::And(x, pos) | Self::Or(x, pos) => {
let op_name = match self { let op_name = match self {
Self::Dot(_, _) => "Dot", Self::Dot(_, _) => "Dot",
Self::Index(_, _) => "Index", Self::Index(_, _) => "Index",
@ -1799,21 +1753,17 @@ impl fmt::Debug for Expr {
_ => unreachable!(), _ => unreachable!(),
}; };
display_pos = *pos;
f.debug_struct(op_name) f.debug_struct(op_name)
.field("lhs", &x.lhs) .field("lhs", &x.lhs)
.field("rhs", &x.rhs) .field("rhs", &x.rhs)
.finish()?; .finish()
#[cfg(not(feature = "no_position"))]
write!(f, " @ {:?}", _pos)?;
Ok(())
}
Self::Custom(x, _pos) => {
f.debug_tuple("Custom").field(x).finish()?;
#[cfg(not(feature = "no_position"))]
write!(f, " @ {:?}", _pos)?;
Ok(())
}
} }
Self::Custom(x, _) => f.debug_tuple("Custom").field(x).finish(),
}?;
display_pos.debug_print(f)
} }
} }
@ -1875,26 +1825,26 @@ impl Expr {
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
Self::FloatConstant(_, pos) => *pos, Self::FloatConstant(_, pos) => *pos,
Self::DynamicConstant(_, pos) => *pos, Self::DynamicConstant(_, pos)
Self::BoolConstant(_, pos) => *pos, | Self::BoolConstant(_, pos)
Self::IntegerConstant(_, pos) => *pos, | Self::IntegerConstant(_, pos)
Self::CharConstant(_, pos) => *pos, | Self::CharConstant(_, pos)
Self::StringConstant(_, pos) => *pos, | Self::Unit(pos)
| Self::StringConstant(_, pos)
| Self::Array(_, pos)
| Self::Map(_, pos)
| Self::Variable(_, pos, _)
| Self::FnCall(_, pos)
| Self::Custom(_, pos) => *pos,
Self::InterpolatedString(x) => x.first().unwrap().position(), Self::InterpolatedString(x) => x.first().unwrap().position(),
Self::Array(_, pos) => *pos,
Self::Map(_, pos) => *pos,
Self::Property(x) => (x.2).pos, Self::Property(x) => (x.2).pos,
Self::Stmt(x) => x.1, Self::Stmt(x) => x.1,
Self::Variable(_, pos, _) => *pos,
Self::FnCall(_, pos) => *pos,
Self::And(x, _) | Self::Or(x, _) => x.lhs.position(), Self::And(x, _) | Self::Or(x, _) | Self::Dot(x, _) | Self::Index(x, _) => {
x.lhs.position()
Self::Unit(pos) => *pos, }
Self::Dot(x, _) | Self::Index(x, _) => x.lhs.position(),
Self::Custom(_, pos) => *pos,
} }
} }
/// Override the [position][Position] of the expression. /// Override the [position][Position] of the expression.
@ -1904,24 +1854,28 @@ impl Expr {
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
Self::FloatConstant(_, pos) => *pos = new_pos, Self::FloatConstant(_, pos) => *pos = new_pos,
Self::DynamicConstant(_, pos) => *pos = new_pos, Self::DynamicConstant(_, pos)
Self::BoolConstant(_, pos) => *pos = new_pos, | Self::BoolConstant(_, pos)
Self::IntegerConstant(_, pos) => *pos = new_pos, | Self::IntegerConstant(_, pos)
Self::CharConstant(_, pos) => *pos = new_pos, | Self::CharConstant(_, pos)
Self::StringConstant(_, pos) => *pos = new_pos, | Self::Unit(pos)
| Self::StringConstant(_, pos)
| Self::Array(_, pos)
| Self::Map(_, pos)
| Self::And(_, pos)
| Self::Or(_, pos)
| Self::Dot(_, pos)
| Self::Index(_, pos)
| Self::Variable(_, pos, _)
| Self::FnCall(_, pos)
| Self::Custom(_, pos) => *pos = new_pos,
Self::InterpolatedString(x) => { Self::InterpolatedString(x) => {
x.first_mut().unwrap().set_position(new_pos); x.first_mut().unwrap().set_position(new_pos);
} }
Self::Array(_, pos) => *pos = new_pos,
Self::Map(_, pos) => *pos = new_pos,
Self::Variable(_, pos, _) => *pos = new_pos,
Self::Property(x) => (x.2).pos = new_pos, Self::Property(x) => (x.2).pos = new_pos,
Self::Stmt(x) => x.1 = new_pos, Self::Stmt(x) => x.1 = new_pos,
Self::FnCall(_, pos) => *pos = new_pos,
Self::And(_, pos) | Self::Or(_, pos) => *pos = new_pos,
Self::Unit(pos) => *pos = new_pos,
Self::Dot(_, pos) | Self::Index(_, pos) => *pos = new_pos,
Self::Custom(_, pos) => *pos = new_pos,
} }
self self

View File

@ -838,7 +838,7 @@ fn default_debug(_s: &str, _source: Option<&str>, _pos: Position) {
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
if let Some(source) = _source { if let Some(source) = _source {
println!("{} @ {:?} | {}", source, _pos, _s); println!("{}{:?} | {}", source, _pos, _s);
} else if _pos.is_none() { } else if _pos.is_none() {
println!("{}", _s); println!("{}", _s);
} else { } else {

View File

@ -1661,6 +1661,16 @@ impl fmt::Debug for NamespaceRef {
} }
} }
impl fmt::Display for NamespaceRef {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for Ident { name, .. } in self.path.iter() {
write!(f, "{}{}", name, Token::DoubleColon.syntax())?;
}
Ok(())
}
}
impl Deref for NamespaceRef { impl Deref for NamespaceRef {
type Target = StaticVec<Ident>; type Target = StaticVec<Ident>;
@ -1677,16 +1687,6 @@ impl DerefMut for NamespaceRef {
} }
} }
impl fmt::Display for NamespaceRef {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for Ident { name, .. } in self.path.iter() {
write!(f, "{}{}", name, Token::DoubleColon.syntax())?;
}
Ok(())
}
}
impl From<StaticVec<Ident>> for NamespaceRef { impl From<StaticVec<Ident>> for NamespaceRef {
#[inline(always)] #[inline(always)]
fn from(path: StaticVec<Ident>) -> Self { fn from(path: StaticVec<Ident>) -> Self {

View File

@ -185,6 +185,16 @@ impl Position {
#[cfg(feature = "no_position")] #[cfg(feature = "no_position")]
return true; return true;
} }
/// Print this [`Position`] for debug purposes.
#[inline(always)]
pub(crate) fn debug_print(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[cfg(not(feature = "no_position"))]
if !self.is_none() {
write!(f, " @ {:?}", self)?;
}
Ok(())
}
} }
impl Default for Position { impl Default for Position {

View File

@ -205,7 +205,7 @@ fn test_map_json() -> Result<(), Box<EvalAltResult>> {
assert!(matches!( assert!(matches!(
*engine.parse_json(" 123", true).expect_err("should error"), *engine.parse_json(" 123", true).expect_err("should error"),
EvalAltResult::ErrorParsing(ParseErrorType::MissingToken(token, _), pos) EvalAltResult::ErrorParsing(ParseErrorType::MissingToken(token, _), _)
if token == "{" if token == "{"
)); ));