Skip to main content

patch_prolog_frontend/
parse_error.rs

1//! Structured parse error: a message plus the source `Span` it points at.
2//!
3//! Replaces the old `"... at line N col M"` string trailer. Consumers render
4//! `file:line:col` from `span` via [`crate::source_map::SourceMap`] — the
5//! position is no longer baked into the message text.
6
7use crate::tokenizer::Token;
8use plg_shared::Span;
9
10#[derive(Debug, Clone, PartialEq)]
11pub struct ParseError {
12    pub message: String,
13    pub span: Span,
14}
15
16impl ParseError {
17    pub fn new(message: impl Into<String>, span: Span) -> Self {
18        Self {
19            message: message.into(),
20            span,
21        }
22    }
23
24    /// Error pointing at `tok`'s lexeme (single-buffer: file `0`).
25    pub fn at(message: impl Into<String>, tok: &Token) -> Self {
26        Self::new(message, Span::new(0, tok.lo, tok.hi))
27    }
28}
29
30impl std::fmt::Display for ParseError {
31    /// Message only — position is rendered separately from the span, so this
32    /// stays free of the old trailer.
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        f.write_str(&self.message)
35    }
36}