Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add source to diagnostics #204

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions components/dada-ir/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ pub struct ErrorReported;
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub struct Diagnostic {
/// This is only used for testing the reference grammar.
/// If it's true it means that the reference grammar should also emit an error.
pub is_grammar_error: bool,
pub severity: Severity,
pub span: FileSpan,
pub message: String,
Expand Down Expand Up @@ -54,6 +57,15 @@ macro_rules! error {
}
}

/// Convenience macro for avoiding `format!`
/// Use this when the error should also be emitted by the reference grammar
#[macro_export]
macro_rules! grammar_error {
($span:expr, $($message:tt)*) => {
$crate::diagnostic::Diagnostic::builder($crate::diagnostic::Severity::Error, $span, format!($($message)*)).is_grammar_error()
}
}

/// Convenience macro for avoiding `format!`
#[macro_export]
macro_rules! warning {
Expand Down Expand Up @@ -119,6 +131,8 @@ pub struct DiagnosticBuilder {
/// label ("here") when the diagnostic is emitted. Set to false
/// if user adds an explicit primary label or calls [`Self::skip_primary_label`].
add_primary_label: bool,

is_grammar_error: bool,
}

impl DiagnosticBuilder {
Expand All @@ -130,9 +144,16 @@ impl DiagnosticBuilder {
labels: vec![],
children: vec![],
add_primary_label: true,
is_grammar_error: false,
}
}

#[must_use = "you have not emitted the diagnostic"]
pub fn is_grammar_error(mut self) -> Self {
self.is_grammar_error = true;
self
}

/// Replaces the "primary label", which is always placed on the source
/// of the diagnostic. The default primary label, if nothing else is given,
/// is just "here".
Expand Down Expand Up @@ -186,6 +207,7 @@ impl DiagnosticBuilder {
}

Diagnostic {
is_grammar_error: self.is_grammar_error,
severity: self.severity,
span: self.span,
message: self.message,
Expand Down
2 changes: 1 addition & 1 deletion components/dada-lex/src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ where
.map(|pair| pair.0)
.unwrap_or(self.file_len),
);
dada_ir::error!(
dada_ir::grammar_error!(
Span {
start: ch_offset,
end,
Expand Down
2 changes: 1 addition & 1 deletion components/dada-parse/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl<'me> Parser<'me> {
}

fn error(&self, span: Span, message: impl ToString) -> DiagnosticBuilder {
dada_ir::error!(span.in_file(self.input_file), "{}", message.to_string())
dada_ir::grammar_error!(span.in_file(self.input_file), "{}", message.to_string())
}
}

Expand Down
3 changes: 2 additions & 1 deletion components/dada-parse/src/parser/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ impl<'db> Parser<'db> {
} else {
let span = self.tokens.last_span();
self.tokens.consume();
dada_ir::error!(span.in_file(self.input_file), "unexpected token").emit(self.db);
dada_ir::grammar_error!(span.in_file(self.input_file), "unexpected token")
.emit(self.db);
}
}

Expand Down