Add filename information to the builder error messages.

This commit is contained in:
Jeremy Wall 2018-05-29 20:49:33 -05:00
parent e86186b709
commit e926bdd733
2 changed files with 45 additions and 25 deletions

View File

@ -45,7 +45,10 @@ impl MacroDef {
// macro call error. // macro call error.
if args.len() > self.argdefs.len() { if args.len() > self.argdefs.len() {
return Err(Box::new(error::Error::new( return Err(Box::new(error::Error::new(
"Macro called with too many args", format!(
"Macro called with too many args in file: {}",
root.to_string_lossy()
),
error::ErrorType::BadArgLen, error::ErrorType::BadArgLen,
self.pos.clone(), self.pos.clone(),
))); )));
@ -117,7 +120,12 @@ impl Val {
) )
} }
pub fn equal(&self, target: &Self, pos: Position) -> Result<bool, error::Error> { pub fn equal(
&self,
target: &Self,
file_name: &str,
pos: Position,
) -> Result<bool, error::Error> {
// first we do a type equality comparison // first we do a type equality comparison
match (self, target) { match (self, target) {
// Empty values are always equal. // Empty values are always equal.
@ -131,7 +139,7 @@ impl Val {
Ok(false) Ok(false)
} else { } else {
for (i, v) in ldef.iter().enumerate() { for (i, v) in ldef.iter().enumerate() {
try!(v.equal(lldef[i].as_ref(), pos.clone())); try!(v.equal(lldef[i].as_ref(), file_name, pos.clone()));
} }
Ok(true) Ok(true)
} }
@ -147,7 +155,10 @@ impl Val {
return Ok(false); return Ok(false);
} else { } else {
// field value equality. // field value equality.
if !try!(v.1.equal(field_target.1.as_ref(), v.0.pos.clone())) { if !try!(
v.1
.equal(field_target.1.as_ref(), file_name, v.0.pos.clone())
) {
return Ok(false); return Ok(false);
} }
} }
@ -156,12 +167,12 @@ impl Val {
} }
} }
(&Val::Macro(_), &Val::Macro(_)) => Err(error::Error::new( (&Val::Macro(_), &Val::Macro(_)) => Err(error::Error::new(
"Macros are not comparable", format!("Macros are not comparable in file: {}", file_name),
error::ErrorType::TypeFail, error::ErrorType::TypeFail,
pos, pos,
)), )),
(me, tgt) => Err(error::Error::new( (me, tgt) => Err(error::Error::new(
format!("Types differ for {}, {}", me, tgt), format!("Types differ for {}, {} in file: {}", me, tgt, file_name),
error::ErrorType::TypeFail, error::ErrorType::TypeFail,
pos, pos,
)), )),
@ -335,7 +346,11 @@ impl Builder {
&Value::String(ref s) => Ok(Rc::new(Val::String(s.val.to_string()))), &Value::String(ref s) => Ok(Rc::new(Val::String(s.val.to_string()))),
&Value::Symbol(ref s) => self.lookup_sym(&(s.into())).ok_or(Box::new( &Value::Symbol(ref s) => self.lookup_sym(&(s.into())).ok_or(Box::new(
error::Error::new( error::Error::new(
format!("Unable to find {}", s.val), format!(
"Unable to find {} in file: {}",
s.val,
self.root.to_string_lossy()
),
error::ErrorType::NoSuchSymbol, error::ErrorType::NoSuchSymbol,
v.pos().clone(), v.pos().clone(),
), ),
@ -405,7 +420,11 @@ impl Builder {
Some(val) => Ok(val), Some(val) => Ok(val),
} }
} }
Err(err) => Err(Box::new(err)), Err(err) => Err(Box::new(error::Error::new_with_cause(
format!("Error while parsing file: {}", self.root.to_string_lossy()),
error::ErrorType::ParseError,
err,
))),
} }
} }
@ -447,7 +466,10 @@ impl Builder {
None => { None => {
// some kind of error here I think. // some kind of error here I think.
Err(Box::new(error::Error::new( Err(Box::new(error::Error::new(
"Unknown Error processing import", format!(
"Unknown Error processing import in file: {}",
self.root.to_string_lossy()
),
error::ErrorType::Unsupported, error::ErrorType::Unsupported,
def.name.pos.clone(), def.name.pos.clone(),
))) )))
@ -466,8 +488,9 @@ impl Builder {
format!( format!(
"Let binding \ "Let binding \
for {:?} already \ for {:?} already \
exists", exists in file: {}",
e.key() e.key(),
self.root.to_string_lossy(),
), ),
error::ErrorType::DuplicateBinding, error::ErrorType::DuplicateBinding,
def.name.pos.clone(), def.name.pos.clone(),
@ -526,8 +549,9 @@ impl Builder {
format!( format!(
"Unable to \ "Unable to \
match selector \ match selector \
path {:?}", path {:?} in file: {}",
sl sl,
self.root.to_string_lossy(),
), ),
error::ErrorType::NoSuchSymbol, error::ErrorType::NoSuchSymbol,
next.0.clone(), next.0.clone(),
@ -551,8 +575,9 @@ impl Builder {
format!( format!(
"Unable to \ "Unable to \
match selector \ match selector \
path {:?}", path {:?} in file: {}",
sl sl,
self.root.to_string_lossy(),
), ),
error::ErrorType::NoSuchSymbol, error::ErrorType::NoSuchSymbol,
next.0.clone(), next.0.clone(),
@ -757,7 +782,7 @@ impl Builder {
right: Rc<Val>, right: Rc<Val>,
) -> Result<Rc<Val>, Box<Error>> { ) -> Result<Rc<Val>, Box<Error>> {
Ok(Rc::new(Val::Boolean(try!( Ok(Rc::new(Val::Boolean(try!(
left.equal(right.as_ref(), pos.clone()) left.equal(right.as_ref(), &self.root.to_string_lossy(), pos.clone())
)))) ))))
} }
@ -768,7 +793,7 @@ impl Builder {
right: Rc<Val>, right: Rc<Val>,
) -> Result<Rc<Val>, Box<Error>> { ) -> Result<Rc<Val>, Box<Error>> {
Ok(Rc::new(Val::Boolean(!try!( Ok(Rc::new(Val::Boolean(!try!(
left.equal(right.as_ref(), pos.clone()) left.equal(right.as_ref(), &self.root.to_string_lossy(), pos.clone())
)))) ))))
} }

View File

@ -75,13 +75,8 @@ impl Error {
} }
} }
pub fn new_with_cause<S: Into<String>>( pub fn new_with_cause<S: Into<String>>(msg: S, t: ErrorType, cause: Error) -> Self {
msg: S, let mut e = Self::new(msg, t, cause.pos.clone());
t: ErrorType,
pos: Position,
cause: Error,
) -> Self {
let mut e = Self::new(msg, t, pos);
e.cause = Some(Box::new(cause)); e.cause = Some(Box::new(cause));
return e; return e;
} }
@ -93,7 +88,7 @@ impl Error {
cause: nom::ErrorKind<Error>, cause: nom::ErrorKind<Error>,
) -> Self { ) -> Self {
match cause { match cause {
nom::ErrorKind::Custom(e) => Self::new_with_cause(msg, t, pos, e), nom::ErrorKind::Custom(e) => Self::new_with_cause(msg, t, e),
_ => Self::new(msg, t, pos), _ => Self::new(msg, t, pos),
} }
} }