feat: add diagnostic system and improve CLI with check command

Implement comprehensive diagnostic reporting system:
- Add Diagnostic struct with severity levels and span-based error tracking
- Add diagnostic rendering with source context and caret positioning
- Replace ParseError with diagnostic collection in lexer and parser
- Add LexResult and ParseResult types to carry diagnostics

Enhance driver crate with frontend output:
- Replace CompileResult with FrontendOutput containing diagnostics
- Add has_errors() and render_diagnostics() methods
- Add AstSummary for
This commit is contained in:
2026-04-06 17:07:50 +02:00
parent 0da224325a
commit dfd2f10234
12 changed files with 1315 additions and 502 deletions

View File

@@ -4,22 +4,48 @@ use std::fmt;
pub struct Span {
pub start: usize,
pub end: usize,
pub line: usize,
pub column: usize,
pub start_line: usize,
pub start_column: usize,
pub end_line: usize,
pub end_column: usize,
}
impl Span {
pub fn new(start: usize, end: usize, line: usize, column: usize) -> Self {
pub fn new(
start: usize,
end: usize,
start_line: usize,
start_column: usize,
end_line: usize,
end_column: usize,
) -> Self {
Self {
start,
end,
line,
column,
start_line,
start_column,
end_line,
end_column,
}
}
pub fn single(position: usize, line: usize, column: usize) -> Self {
Self::new(position, position, line, column, line, column)
}
pub fn merge(self, other: Self) -> Self {
Self {
start: self.start.min(other.start),
end: self.end.max(other.end),
start_line: self.start_line,
start_column: self.start_column,
end_line: other.end_line,
end_column: other.end_column,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq)]
pub struct Token {
pub kind: TokenKind,
pub span: Span,
@@ -31,23 +57,19 @@ impl Token {
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq)]
pub enum TokenKind {
Identifier(String),
Integer(String),
Float(String),
String(String),
Bool(bool),
Keyword(Keyword),
LeftParen,
RightParen,
LeftBrace,
RightBrace,
LeftBracket,
RightBracket,
Comma,
Dot,
Colon,
Arrow,
FatArrow,
Plus,
Minus,
Star,
@@ -61,52 +83,41 @@ pub enum TokenKind {
LessEqual,
Greater,
GreaterEqual,
Question,
AndAnd,
OrOr,
Newline,
Indent,
Dedent,
Eof,
}
impl TokenKind {
pub fn same_variant(&self, other: &Self) -> bool {
std::mem::discriminant(self) == std::mem::discriminant(other)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Keyword {
Async,
Else,
Fn,
For,
If,
Impl,
Import,
In,
Let,
Match,
Pub,
Return,
Struct,
Use,
Var,
While,
}
impl Keyword {
pub fn from_ident(value: &str) -> Option<Self> {
match value {
"async" => Some(Self::Async),
"else" => Some(Self::Else),
"fn" => Some(Self::Fn),
"for" => Some(Self::For),
"if" => Some(Self::If),
"impl" => Some(Self::Impl),
"import" => Some(Self::Import),
"in" => Some(Self::In),
"let" => Some(Self::Let),
"match" => Some(Self::Match),
"pub" => Some(Self::Pub),
"return" => Some(Self::Return),
"struct" => Some(Self::Struct),
"use" => Some(Self::Use),
"var" => Some(Self::Var),
"while" => Some(Self::While),
_ => None,
}
}
@@ -117,7 +128,9 @@ impl fmt::Display for TokenKind {
match self {
TokenKind::Identifier(name) => write!(f, "identifier({name})"),
TokenKind::Integer(value) => write!(f, "integer({value})"),
TokenKind::Float(value) => write!(f, "float({value})"),
TokenKind::String(value) => write!(f, "string({value})"),
TokenKind::Bool(value) => write!(f, "bool({value})"),
TokenKind::Keyword(keyword) => write!(f, "keyword({keyword:?})"),
other => write!(f, "{other:?}"),
}