1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//! Constructor methods for ParseError.
use super::suggestions::suggest_function;
use super::types::{ParseError, ParseErrorKind, Span};
impl ParseError {
/// Creates a new parse error.
///
/// # Arguments
///
/// * `kind` - The kind of parsing error
/// * `span` - Optional span indicating where the error occurred
///
/// # Example
///
/// ```
/// use mathlex::error::{ParseError, ParseErrorKind};
///
/// let error = ParseError::new(
/// ParseErrorKind::EmptyExpression,
/// None,
/// );
/// ```
pub fn new(kind: ParseErrorKind, span: Option<Span>) -> Self {
Self {
kind,
span,
context: None,
suggestion: None,
}
}
/// Adds context to this error.
///
/// # Example
///
/// ```
/// use mathlex::error::{ParseError, ParseErrorKind};
///
/// let error = ParseError::new(ParseErrorKind::EmptyExpression, None)
/// .with_context("while parsing function arguments");
/// ```
pub fn with_context<S: Into<String>>(mut self, context: S) -> Self {
self.context = Some(context.into());
self
}
/// Adds a suggestion to this error.
///
/// # Example
///
/// ```
/// use mathlex::error::{ParseError, ParseErrorKind};
///
/// let error = ParseError::new(ParseErrorKind::EmptyExpression, None)
/// .with_suggestion("Did you mean 'sin'?");
/// ```
pub fn with_suggestion<S: Into<String>>(mut self, suggestion: S) -> Self {
self.suggestion = Some(suggestion.into());
self
}
/// Creates an unexpected token error.
///
/// # Example
///
/// ```
/// use mathlex::error::ParseError;
///
/// let error = ParseError::unexpected_token(
/// vec!["number"],
/// "+",
/// None,
/// );
/// ```
pub fn unexpected_token<S1, S2>(expected: Vec<S1>, found: S2, span: Option<Span>) -> Self
where
S1: Into<String>,
S2: Into<String>,
{
Self::new(
ParseErrorKind::UnexpectedToken {
expected: expected.into_iter().map(|s| s.into()).collect(),
found: found.into(),
},
span,
)
}
/// Creates an unexpected end of input error.
///
/// # Example
///
/// ```
/// use mathlex::error::ParseError;
///
/// let error = ParseError::unexpected_eof(
/// vec!["closing parenthesis"],
/// None,
/// );
/// ```
pub fn unexpected_eof<S>(expected: Vec<S>, span: Option<Span>) -> Self
where
S: Into<String>,
{
Self::new(
ParseErrorKind::UnexpectedEof {
expected: expected.into_iter().map(|s| s.into()).collect(),
},
span,
)
}
/// Creates an unmatched delimiter error.
///
/// # Example
///
/// ```
/// use mathlex::error::{Position, ParseError};
///
/// let error = ParseError::unmatched_delimiter(
/// '(',
/// Position::new(1, 1, 0),
/// None,
/// );
/// ```
pub fn unmatched_delimiter(
opening: char,
position: super::types::Position,
span: Option<Span>,
) -> Self {
Self::new(
ParseErrorKind::UnmatchedDelimiter { opening, position },
span,
)
}
/// Creates an invalid number error.
///
/// # Example
///
/// ```
/// use mathlex::error::ParseError;
///
/// let error = ParseError::invalid_number(
/// "123.45.67",
/// "multiple decimal points",
/// None,
/// );
/// ```
pub fn invalid_number<S1, S2>(value: S1, reason: S2, span: Option<Span>) -> Self
where
S1: Into<String>,
S2: Into<String>,
{
Self::new(
ParseErrorKind::InvalidNumber {
value: value.into(),
reason: reason.into(),
},
span,
)
}
/// Creates an invalid LaTeX command error.
///
/// # Example
///
/// ```
/// use mathlex::error::ParseError;
///
/// let error = ParseError::invalid_latex_command(r"\unknowncommand", None);
/// ```
pub fn invalid_latex_command<S>(command: S, span: Option<Span>) -> Self
where
S: Into<String>,
{
Self::new(
ParseErrorKind::InvalidLatexCommand {
command: command.into(),
},
span,
)
}
/// Creates an unknown function error.
///
/// Automatically adds a suggestion if a similar known function is found.
///
/// # Example
///
/// ```
/// use mathlex::error::ParseError;
///
/// let error = ParseError::unknown_function("unknownfunc", None);
/// ```
pub fn unknown_function<S>(name: S, span: Option<Span>) -> Self
where
S: Into<String>,
{
let name_str = name.into();
let suggestion = suggest_function(&name_str);
let mut error = Self::new(ParseErrorKind::UnknownFunction { name: name_str }, span);
error.suggestion = suggestion;
error
}
/// Creates an invalid subscript error.
///
/// # Example
///
/// ```
/// use mathlex::error::ParseError;
///
/// let error = ParseError::invalid_subscript("missing expression", None);
/// ```
pub fn invalid_subscript<S>(reason: S, span: Option<Span>) -> Self
where
S: Into<String>,
{
Self::new(
ParseErrorKind::InvalidSubscript {
reason: reason.into(),
},
span,
)
}
/// Creates an invalid superscript error.
///
/// # Example
///
/// ```
/// use mathlex::error::ParseError;
///
/// let error = ParseError::invalid_superscript("missing expression", None);
/// ```
pub fn invalid_superscript<S>(reason: S, span: Option<Span>) -> Self
where
S: Into<String>,
{
Self::new(
ParseErrorKind::InvalidSuperscript {
reason: reason.into(),
},
span,
)
}
/// Creates a malformed matrix error.
///
/// # Example
///
/// ```
/// use mathlex::error::ParseError;
///
/// let error = ParseError::malformed_matrix("inconsistent row lengths", None);
/// ```
pub fn malformed_matrix<S>(reason: S, span: Option<Span>) -> Self
where
S: Into<String>,
{
Self::new(
ParseErrorKind::MalformedMatrix {
reason: reason.into(),
},
span,
)
}
/// Creates an empty expression error.
///
/// # Example
///
/// ```
/// use mathlex::error::ParseError;
///
/// let error = ParseError::empty_expression(None);
/// ```
pub fn empty_expression(span: Option<Span>) -> Self {
Self::new(ParseErrorKind::EmptyExpression, span)
}
/// Creates a custom error.
///
/// # Example
///
/// ```
/// use mathlex::error::ParseError;
///
/// let error = ParseError::custom("something went wrong", None);
/// ```
pub fn custom<S>(message: S, span: Option<Span>) -> Self
where
S: Into<String>,
{
Self::new(ParseErrorKind::Custom(message.into()), span)
}
}