summaryrefslogtreecommitdiff
path: root/audio/src/parse/util.rs
diff options
context:
space:
mode:
Diffstat (limited to 'audio/src/parse/util.rs')
-rw-r--r--audio/src/parse/util.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/audio/src/parse/util.rs b/audio/src/parse/util.rs
new file mode 100644
index 0000000..47779c2
--- /dev/null
+++ b/audio/src/parse/util.rs
@@ -0,0 +1,42 @@
+use super::{ParserError, lexer::Token, pos::Span};
+use std::fmt;
+
+impl std::error::Error for ParserError {}
+
+impl fmt::Display for ParserError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ if let Some(file) = &self.file {
+ write!(f, "{file} | ")?;
+ }
+ write!(f, "{} | {}", self.span, self.msg)
+ }
+}
+
+pub trait SpanParserError {
+ type Output;
+ fn span_err(self, span: Span) -> Self::Output;
+}
+
+impl<T, E: std::error::Error> SpanParserError for Result<T, E> {
+ type Output = Result<T, ParserError>;
+ fn span_err(self, span: Span) -> Self::Output {
+ self.map_err(|e| ParserError {
+ span,
+ msg: e.to_string(),
+ file: None,
+ })
+ }
+}
+
+pub trait TokenError {
+ fn token_err<T>(self, msg: impl Into<String>) -> Result<T, ParserError>;
+}
+impl TokenError for Token<'_> {
+ fn token_err<T>(self, msg: impl Into<String>) -> Result<T, ParserError> {
+ Err(ParserError {
+ span: self.span,
+ msg: msg.into(),
+ file: None,
+ })
+ }
+}