use std::fmt;
use crate::grammar::Span;
use crate::render::visible;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Severity {
Error,
Warning,
}
impl fmt::Display for Severity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Severity::Error => "error",
Severity::Warning => "warning",
})
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Diagnostic {
pub code: DiagnosticCode,
pub message: String,
pub span: Span,
}
impl Diagnostic {
pub fn severity(&self) -> Severity {
self.code.severity()
}
}
impl fmt::Display for Diagnostic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (severity, span) = (self.severity(), self.span);
write!(
f,
"{severity}[{}]: {} at {}..{}",
self.code,
visible(&self.message),
span.start,
span.end
)
}
}
impl std::error::Error for Diagnostic {}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DiagnosticCode {
Syntax,
Escape,
Property,
Number,
UnknownRule,
UnknownFlag,
UnknownAttribute,
Duplicate,
Name,
Arguments,
BuilderField,
BuilderFunction,
RuleInTerminal,
RuleForm,
Precedence,
TransparentField,
FieldItems,
EmptyLiteral,
Class,
Bounds,
BuilderPlace,
BuilderArguments,
TooDeep,
TooLarge,
NotYet,
UnknownModule,
ModuleCycle,
ImportClash,
UnknownImport,
Inherited,
Internal,
UnreachableAlternative,
ShadowedLiteral,
ShadowedAlternative,
EmptyRepetition,
LeftAndRightRecursion,
UnsteadyShape,
UnusedBuilder,
UnusedFlag,
UnusedParameter,
UnusedRecovery,
}
impl DiagnosticCode {
pub const ALL: [DiagnosticCode; 41] = [
DiagnosticCode::Syntax,
DiagnosticCode::Escape,
DiagnosticCode::Property,
DiagnosticCode::Number,
DiagnosticCode::UnknownRule,
DiagnosticCode::UnknownFlag,
DiagnosticCode::UnknownAttribute,
DiagnosticCode::Duplicate,
DiagnosticCode::Name,
DiagnosticCode::Arguments,
DiagnosticCode::BuilderField,
DiagnosticCode::BuilderFunction,
DiagnosticCode::RuleInTerminal,
DiagnosticCode::RuleForm,
DiagnosticCode::Precedence,
DiagnosticCode::TransparentField,
DiagnosticCode::FieldItems,
DiagnosticCode::EmptyLiteral,
DiagnosticCode::Class,
DiagnosticCode::Bounds,
DiagnosticCode::BuilderPlace,
DiagnosticCode::BuilderArguments,
DiagnosticCode::TooDeep,
DiagnosticCode::TooLarge,
DiagnosticCode::NotYet,
DiagnosticCode::UnknownModule,
DiagnosticCode::ModuleCycle,
DiagnosticCode::ImportClash,
DiagnosticCode::UnknownImport,
DiagnosticCode::Inherited,
DiagnosticCode::Internal,
DiagnosticCode::UnreachableAlternative,
DiagnosticCode::ShadowedLiteral,
DiagnosticCode::ShadowedAlternative,
DiagnosticCode::EmptyRepetition,
DiagnosticCode::LeftAndRightRecursion,
DiagnosticCode::UnsteadyShape,
DiagnosticCode::UnusedBuilder,
DiagnosticCode::UnusedFlag,
DiagnosticCode::UnusedParameter,
DiagnosticCode::UnusedRecovery,
];
pub fn code(self) -> &'static str {
match self {
DiagnosticCode::Syntax => "E101",
DiagnosticCode::Escape => "E102",
DiagnosticCode::Property => "E103",
DiagnosticCode::Number => "E104",
DiagnosticCode::UnknownRule => "E201",
DiagnosticCode::UnknownFlag => "E202",
DiagnosticCode::UnknownAttribute => "E203",
DiagnosticCode::Duplicate => "E204",
DiagnosticCode::Name => "E205",
DiagnosticCode::Arguments => "E206",
DiagnosticCode::BuilderField => "E207",
DiagnosticCode::BuilderFunction => "E208",
DiagnosticCode::RuleInTerminal => "E301",
DiagnosticCode::RuleForm => "E302",
DiagnosticCode::Precedence => "E303",
DiagnosticCode::TransparentField => "E304",
DiagnosticCode::FieldItems => "E305",
DiagnosticCode::EmptyLiteral => "E306",
DiagnosticCode::Class => "E307",
DiagnosticCode::Bounds => "E308",
DiagnosticCode::BuilderPlace => "E309",
DiagnosticCode::BuilderArguments => "E310",
DiagnosticCode::TooDeep => "E401",
DiagnosticCode::TooLarge => "E402",
DiagnosticCode::NotYet => "E501",
DiagnosticCode::UnknownModule => "E601",
DiagnosticCode::ModuleCycle => "E602",
DiagnosticCode::ImportClash => "E603",
DiagnosticCode::UnknownImport => "E604",
DiagnosticCode::Inherited => "E605",
DiagnosticCode::Internal => "E901",
DiagnosticCode::UnreachableAlternative => "W101",
DiagnosticCode::ShadowedLiteral => "W102",
DiagnosticCode::ShadowedAlternative => "W103",
DiagnosticCode::EmptyRepetition => "W201",
DiagnosticCode::LeftAndRightRecursion => "W202",
DiagnosticCode::UnsteadyShape => "W301",
DiagnosticCode::UnusedBuilder => "W302",
DiagnosticCode::UnusedFlag => "W401",
DiagnosticCode::UnusedParameter => "W402",
DiagnosticCode::UnusedRecovery => "W403",
}
}
pub fn severity(self) -> Severity {
match self.code().starts_with('W') {
true => Severity::Warning,
false => Severity::Error,
}
}
}
impl fmt::Display for DiagnosticCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.code())
}
}