summaryrefslogtreecommitdiff
path: root/src/diagnostic.rs
blob: a5c5d0b863a18432ffdaa33dc147d77faaf12866 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! Diagnostics (errors / warnings) in source code.
//!
//! There are no fatal errors. The document will always compile and yield a
//! layout. However, this is a best effort process and bad things will still
//! generate errors and warnings.

use serde::Serialize;
use crate::syntax::span::SpanVec;

/// A list of spanned diagnostics.
pub type Diagnostics = SpanVec<Diagnostic>;

/// A diagnostic that arose in parsing or layouting.
#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
pub struct Diagnostic {
    /// How severe / important the diagnostic is.
    pub level: Level,
    /// A message describing the diagnostic.
    pub message: String,
}

/// How severe / important a diagnostic is.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum Level {
    Warning,
    Error,
}

impl Diagnostic {
    /// Create a new diagnostic from message and level.
    pub fn new(message: impl Into<String>, level: Level) -> Self {
        Self { message: message.into(), level }
    }
}

/// Construct a diagnostic with `Error` level.
///
/// ```
/// # use typstc::error;
/// # use typstc::syntax::span::Span;
/// # use typstc::Feedback;
/// # let span = Span::ZERO;
/// # let mut feedback = Feedback::new();
/// # let name = "";
/// // Create formatted error values.
/// let error = error!("expected {}", name);
///
/// // Create spanned errors.
/// let spanned = error!(span, "there is an error here");
///
/// // Create an error and directly add it to existing feedback.
/// error!(@feedback, span, "oh no!");
/// ```
#[macro_export]
macro_rules! error {
    ($($tts:tt)*) => {
        $crate::__impl_diagnostic!($crate::diagnostic::Level::Error; $($tts)*)
    };
}

/// Construct a diagnostic with `Warning` level.
///
/// This works exactly like `error!`. See its documentation for more
/// information.
#[macro_export]
macro_rules! warning {
    ($($tts:tt)*) => {
        $crate::__impl_diagnostic!($crate::diagnostic::Level::Warning; $($tts)*)
    };
}

/// Backs the `error!` and `warning!` macros.
#[macro_export]
#[doc(hidden)]
macro_rules! __impl_diagnostic {
    ($level:expr; @$feedback:expr, $($tts:tt)*) => {
        $feedback.diagnostics.push($crate::__impl_diagnostic!($level; $($tts)*));
    };

    ($level:expr; $fmt:literal $($tts:tt)*) => {
        $crate::diagnostic::Diagnostic::new(format!($fmt $($tts)*), $level)
    };

    ($level:expr; $span:expr, $fmt:literal $($tts:tt)*) => {
        $crate::syntax::span::Spanned::new(
            $crate::__impl_diagnostic!($level; $fmt $($tts)*),
            $span,
        )
    };
}