Files
endorphin-aster-vm/crates/frontend/src/diagnostic.rs
T
codex-bot 0b3a480555
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 7m30s
Add token-based PEG frontend and CLI source parsing
2026-10-19 02:48:23 +08:00

89 lines
2.2 KiB
Rust

use std::error::Error;
use std::fmt;
/// Half-open UTF-8 byte range in the original source, including trivia between
/// the first and last syntax token. EOF has an empty range at source.len().
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Span {
pub start: usize,
pub end: usize,
}
impl Span {
pub const fn new(start: usize, end: usize) -> Self {
Self { start, end }
}
pub(crate) fn through(self, last: Self) -> Self {
Self::new(self.start, last.end)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiagnosticKind {
Lexical,
Syntax,
}
/// Lines and Unicode scalar columns are one-based. CRLF counts as one newline.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SourceLocation {
pub line: usize,
pub column: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Diagnostic {
pub kind: DiagnosticKind,
pub span: Span,
pub message: String,
pub expected: Vec<String>,
}
impl Diagnostic {
pub(crate) fn lexical(span: Span, message: impl Into<String>) -> Self {
Self {
kind: DiagnosticKind::Lexical,
span,
message: message.into(),
expected: Vec::new(),
}
}
pub fn location(&self, source: &str) -> SourceLocation {
let mut location = SourceLocation { line: 1, column: 1 };
let mut previous_cr = false;
for (offset, character) in source.char_indices() {
if offset >= self.span.start {
break;
}
match character {
'\r' => {
location.line += 1;
location.column = 1;
}
'\n' if previous_cr => {}
'\n' => {
location.line += 1;
location.column = 1;
}
_ => location.column += 1,
}
previous_cr = character == '\r';
}
location
}
}
impl fmt::Display for Diagnostic {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"{} at bytes {}..{}",
self.message, self.span.start, self.span.end
)
}
}
impl Error for Diagnostic {}