Skip to main content

lisette_syntax/parse/
error.rs

1use crate::ast::Span;
2
3#[derive(Debug, Clone)]
4pub struct ParseError {
5    pub message: String,
6    pub labels: Vec<(Span, String)>,
7    pub help: Option<String>,
8    pub note: Option<String>,
9    pub code: String,
10}
11
12impl ParseError {
13    pub fn new(message: impl Into<String>, span: Span, label: impl Into<String>) -> Self {
14        Self {
15            message: message.into(),
16            labels: vec![(span, label.into())],
17            help: None,
18            note: None,
19            code: String::new(),
20        }
21    }
22
23    pub fn with_span_label(mut self, span: Span, label: impl Into<String>) -> Self {
24        self.labels.push((span, label.into()));
25        self
26    }
27
28    pub fn with_help(mut self, help: impl Into<String>) -> Self {
29        self.help = Some(help.into());
30        self
31    }
32
33    pub fn with_note(mut self, note: impl Into<String>) -> Self {
34        self.note = Some(note.into());
35        self
36    }
37
38    pub fn with_lex_code(mut self, code: &str) -> Self {
39        self.code = format!("lex.{}", code);
40        self
41    }
42
43    pub fn with_parse_code(mut self, code: &str) -> Self {
44        self.code = format!("parse.{}", code);
45        self
46    }
47
48    pub fn with_lint_code(mut self, code: &str) -> Self {
49        self.code = format!("lint.{}", code);
50        self
51    }
52}