summaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-02-04 11:22:00 +0100
committerLaurenz <laurmaedje@gmail.com>2020-02-04 11:22:00 +0100
commit5c11aa72239ecbdd9577f027bdc7e9468d68414e (patch)
tree66d846fb58f38e564eca385a6f86ee8154c503ce /src/error.rs
parentf655656fb8cb6135b26e7960ce0b7adf96d6f567 (diff)
Adapt for tonty and fix a few bugs 🚧
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
index 1eb48deb..3a095eef 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -35,3 +35,38 @@ impl Error {
Error { message: message.into(), severity }
}
}
+
+/// Construct an error with formatted message and optionally severity and / or
+/// span.
+///
+/// # Examples
+/// ```
+/// # use typstc::err;
+/// # use typstc::syntax::span::Span;
+/// # let span = Span::ZERO;
+/// # let value = 0;
+///
+/// // With span and default severity `Error`.
+/// err!(span; "the wrong {}", value);
+///
+/// // With no span and severity `Warning`.
+/// err!(@Warning: span; "non-fatal!");
+///
+/// // Without span and default severity.
+/// err!("no spans here ...");
+/// ```
+#[macro_export]
+macro_rules! err {
+ (@$severity:ident: $span:expr; $($args:tt)*) => {
+ $crate::syntax::span::Spanned { v: err!(@$severity: $($args)*), span: $span }
+ };
+
+ (@$severity:ident: $($args:tt)*) => {
+ $crate::error::Error {
+ message: format!($($args)*),
+ severity: $crate::error::Severity::$severity,
+ }
+ };
+
+ ($($tts:tt)*) => { err!(@Error: $($tts)*) };
+}