mago_type_syntax/
error.rs1use serde::Serialize;
2
3use mago_span::HasPosition;
4use mago_span::HasSpan;
5use mago_span::Position;
6use mago_span::Span;
7
8use crate::token::TypeTokenKind;
9
10#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize)]
11pub enum SyntaxError {
12 UnexpectedToken(u8, Position),
13 UnrecognizedToken(u8, Position),
14 UnexpectedEndOfFile(Position),
15}
16
17#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
18pub enum ParseError {
19 SyntaxError(SyntaxError),
20 UnexpectedEndOfFile(Vec<TypeTokenKind>, Position),
21 UnexpectedToken(Vec<TypeTokenKind>, TypeTokenKind, Span),
22 UnclosedLiteralString(Span),
23}
24
25impl ParseError {
26 pub fn note(&self) -> String {
28 match self {
29 ParseError::SyntaxError(SyntaxError::UnrecognizedToken(_, _)) => {
30 "An invalid character was found that is not part of any valid type syntax.".to_string()
31 }
32 ParseError::SyntaxError(_) => {
33 "A low-level syntax error occurred while parsing the type string.".to_string()
34 }
35 ParseError::UnexpectedEndOfFile(expected, _) => {
36 if expected.is_empty() {
37 "The type declaration ended prematurely.".to_string()
38 } else {
39 let expected_str = expected.iter().map(|t| format!("`{t}`")).collect::<Vec<_>>().join(" or ");
40 format!("The parser reached the end of the input but expected one of: {expected_str}.")
41 }
42 }
43 ParseError::UnexpectedToken(expected, _, _) => {
44 if expected.is_empty() {
45 "The parser encountered a token that was not expected at this position.".to_string()
46 } else {
47 let expected_str = expected.iter().map(|t| format!("`{t}`")).collect::<Vec<_>>().join(" or ");
48 format!("The parser expected one of the following here: {expected_str}.")
49 }
50 }
51 ParseError::UnclosedLiteralString(_) => {
52 "String literals within type declarations must be closed with a matching quote.".to_string()
53 }
54 }
55 }
56
57 pub fn help(&self) -> String {
59 match self {
60 ParseError::SyntaxError(SyntaxError::UnrecognizedToken(_, _)) => {
61 "Remove or replace the invalid character.".to_string()
62 }
63 ParseError::SyntaxError(_) => "Review the syntax of the type declaration for errors.".to_string(),
64 ParseError::UnexpectedEndOfFile(_, _) => {
65 "Complete the type declaration. Check for unclosed parentheses `()`, angle brackets `<>`, or curly braces `{}`.".to_string()
66 }
67 ParseError::UnexpectedToken(_, _, _) => {
68 "Review the type syntax near the unexpected token.".to_string()
69 }
70 ParseError::UnclosedLiteralString(_) => {
71 "Add a closing quote (`'` or `\"`) to complete the string literal.".to_string()
72 }
73 }
74 }
75}
76
77impl HasPosition for SyntaxError {
78 fn position(&self) -> Position {
79 match self {
80 SyntaxError::UnexpectedToken(_, position)
81 | SyntaxError::UnrecognizedToken(_, position)
82 | SyntaxError::UnexpectedEndOfFile(position) => *position,
83 }
84 }
85}
86
87impl HasSpan for ParseError {
88 fn span(&self) -> Span {
89 match self {
90 ParseError::SyntaxError(error) => {
91 let position = error.position();
92 Span::new(position, position)
93 }
94 ParseError::UnexpectedEndOfFile(_, position) => Span::new(*position, *position),
95 ParseError::UnexpectedToken(_, _, span) => *span,
96 ParseError::UnclosedLiteralString(span) => *span,
97 }
98 }
99}
100
101impl std::fmt::Display for SyntaxError {
102 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
103 match self {
104 SyntaxError::UnexpectedToken(token, _) => {
105 write!(f, "Unexpected character '{}'", *token as char)
106 }
107 SyntaxError::UnrecognizedToken(token, _) => {
108 write!(f, "Unrecognized character '{}'", *token as char)
109 }
110 SyntaxError::UnexpectedEndOfFile(_) => {
111 write!(f, "Unexpected end of input")
112 }
113 }
114 }
115}
116
117impl std::fmt::Display for ParseError {
118 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119 match self {
120 ParseError::SyntaxError(err) => write!(f, "{err}"),
121 ParseError::UnexpectedEndOfFile(_, _) => {
122 write!(f, "Unexpected end of type declaration")
123 }
124 ParseError::UnexpectedToken(_, token, _) => {
125 write!(f, "Unexpected token `{token}`")
126 }
127 ParseError::UnclosedLiteralString(_) => {
128 write!(f, "Unclosed string literal in type")
129 }
130 }
131 }
132}
133
134impl std::error::Error for SyntaxError {
135 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
136 None
137 }
138}
139
140impl std::error::Error for ParseError {
141 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
142 match self {
143 ParseError::SyntaxError(err) => Some(err),
144 _ => None,
145 }
146 }
147}
148
149impl From<SyntaxError> for ParseError {
150 fn from(error: SyntaxError) -> Self {
151 ParseError::SyntaxError(error)
152 }
153}