summaryrefslogtreecommitdiff
path: root/chewed/src/error.rs
diff options
context:
space:
mode:
authorGreg Brown <gmb60@cam.ac.uk>2021-01-08 18:00:34 +0000
committerGreg Brown <gmb60@cam.ac.uk>2021-01-08 18:00:34 +0000
commit0a837400e0ffa7fca1a1902b34f375d0dc5b5f6b (patch)
treef6a71331efcd8e2fbdb3427d7fa0ae5289174f7b /chewed/src/error.rs
parente1452227b8bd9ad3805480f8a5a66a75fb8370dd (diff)
Add positions to chewed errors.
Diffstat (limited to 'chewed/src/error.rs')
-rw-r--r--chewed/src/error.rs24
1 files changed, 15 insertions, 9 deletions
diff --git a/chewed/src/error.rs b/chewed/src/error.rs
index 420a376..afe65c0 100644
--- a/chewed/src/error.rs
+++ b/chewed/src/error.rs
@@ -1,17 +1,19 @@
use std::{error::Error, fmt};
+use super::position::LineCol;
+
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum TakeError {
- BadBranch(char, &'static [char]),
- BadString(String, &'static str),
- EndOfStream,
+ BadBranch(LineCol, char, &'static [char]),
+ BadString(LineCol, String, &'static str),
+ EndOfStream(LineCol),
}
impl fmt::Display for TakeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
- Self::BadBranch(got, expected) => {
- write!(f, "Unexpected character {:?}. ", got)?;
+ Self::BadBranch(pos, got, expected) => {
+ write!(f, "{}: Unexpected character {:?}. ", pos, got)?;
if expected.is_empty() {
write!(f, "Expected end of input.")
@@ -28,8 +30,12 @@ impl fmt::Display for TakeError {
Ok(())
}
}
- Self::BadString(got, expected) => write!(f, "Unexpected string {:?}. Expected {:?}", got, expected),
- Self::EndOfStream => write!(f, "Unexpected end of input"),
+ Self::BadString(pos, got, expected) => write!(
+ f,
+ "{}: Unexpected string {:?}. Expected {:?}",
+ pos, got, expected
+ ),
+ Self::EndOfStream(pos) => write!(f, "{}: Unexpected end of input", pos),
}
}
}
@@ -39,14 +45,14 @@ impl Error for TakeError {}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum ParseError {
TakeError(TakeError),
- InputContinues,
+ InputContinues(LineCol),
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TakeError(e) => e.fmt(f),
- Self::InputContinues => write!(f, "Expected end of input"),
+ Self::InputContinues(pos) => write!(f, "{}: Expected end of input", pos),
}
}
}