varyk_syntax/error.rs
1//! Diagnostic codes and the syntax errors the lexer and parser produce.
2//!
3//! This crate defines exactly the codes used by its own passes plus the
4//! ones reserved for the parser: `V0001` through `V0003`
5//! and `V0010` through `V0012`. A code, once assigned, is never reused for
6//! a different meaning (spec section 3).
7
8use crate::span::Span;
9
10/// Unsupported construct, naming it.
11pub const V0001: &str = "V0001";
12/// Unexpected token or malformed syntax.
13pub const V0002: &str = "V0002";
14/// Bad string escape, or an unterminated string.
15pub const V0003: &str = "V0003";
16/// `&x` or `&mut x` written at a call site.
17pub const V0010: &str = "V0010";
18/// `&T` or `&mut T` written in a parameter type.
19pub const V0011: &str = "V0011";
20/// Lifetime syntax, such as `<'a>` or `&'a T`.
21pub const V0012: &str = "V0012";
22
23/// A suggested edit attached to a [`SyntaxError`]: replace the text at
24/// `span` with `replacement`.
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct FixIt {
27 pub span: Span,
28 pub replacement: String,
29}
30
31/// A lexical or syntactic error, carrying a span, a message, and one of
32/// the diagnostic codes defined above.
33#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct SyntaxError {
35 pub span: Span,
36 pub message: String,
37 pub code: &'static str,
38 pub fix_it: Option<FixIt>,
39}
40
41impl SyntaxError {
42 pub fn new(span: Span, code: &'static str, message: impl Into<String>) -> Self {
43 Self {
44 span,
45 message: message.into(),
46 code,
47 fix_it: None,
48 }
49 }
50
51 pub fn with_fix_it(mut self, fix_it: FixIt) -> Self {
52 self.fix_it = Some(fix_it);
53 self
54 }
55}