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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use crate::parser::context::ContextType;
use crate::token::TerminalToken;
use crate::{display_error, TokenItem};
use std::ops::Range;

/// Type of [`ParseError`].
///
/// Implements [`std::fmt::Display`] to write a useful error message.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ParseErrorType {
    /// Expected a specific terminal token but got something else.
    ///
    /// # Example
    /// ```text
    /// function MyFunc { }
    ///                 ^ error
    /// ```
    ExpectedTerminal(TerminalToken),

    /// Expected a specific compound terminal but got something else.
    ExpectedCompound2(TerminalToken, TerminalToken),

    /// Expected a specific compound terminal but got something else.
    ExpectedCompound3(TerminalToken, TerminalToken, TerminalToken),

    /// Expected an identifier but got something else.
    ///
    /// # Example
    /// ```text
    /// global var !?!?! = "guh??"
    ///            ^ error
    /// ```
    ExpectedIdentifier,

    /// Expected a literal but got something else.
    ExpectedLiteral,

    /// Expected a token that starts an expression but got something else.
    ///
    /// # Example
    /// ```text
    /// int What = globalize_all_functions
    ///            ^ error
    /// ```
    ExpectedExpression,

    /// Expected an operator but got something else.
    ExpectedOperator,

    /// Expected a prefix operator but got something else.
    ExpectedPrefixOperator,

    /// Expected a postfix operator but got something else.
    ExpectedPostfixOperator,

    /// Expected a binary operator but got something else.
    ExpectedBinaryOperator,

    /// Expected a type but got something else.
    ///
    /// # Example
    /// ```text
    /// typedef Five 5
    ///              ^ error
    /// ```
    ExpectedType,

    /// Expected a type modifier but got something else.
    ///
    /// # Example
    /// ```text
    /// typedef help table&-
    ///                    ^ error
    /// ```
    ExpectedTypeModifier,

    /// Expected a token that starts a table slot but got something else.
    ///
    /// # Example
    /// ```text
    /// my_table = {
    ///     class MyTableClass {}
    ///     ^ error
    /// }
    /// ```
    ExpectedTableSlot,

    /// Expected a token that starts a class member but got something else.
    ///
    /// # Example
    /// ```text
    /// class MyClass {
    ///     globalize_all_functions
    ///     ^ error
    /// }
    /// ```
    ExpectedClassMember,

    /// Expected a token that starts a statement but got something else.
    ///
    /// # Example
    /// ```text
    /// > hey
    /// ^ error
    /// ```
    ExpectedStatement,

    /// Expected a newline or semicolon to end a statement but got something else.
    ///
    /// # Example
    /// ```text
    /// { 1 } + 2
    ///       ^ error
    /// ```
    ExpectedEndOfStatement,

    /// Expected a token that starts a global definition but got something else.
    ///
    /// # Example
    /// ```text
    /// global if ()
    ///        ^ error
    /// ```
    ExpectedGlobalDefinition,

    /// Found a linebreak in a place where one is not allowed.
    IllegalLineBreak,

    /// An expression was not allowed due to precedence rules.
    Precedence,

    /// Expected a slot in a class or table.
    ExpectedSlot,

    /// Expected a string literal.
    ExpectedStringLiteral,
}

/// An error emitted while trying to parse a token list.
///
/// Each error has a type with more information, the token where the error occurred, and possibly
/// some contextual information.
#[derive(Debug, Clone)]
pub struct ParseError {
    /// The type of error.
    pub ty: ParseErrorType,

    /// The index of the token where the error occurred.
    pub token_index: usize,

    /// Contextual information if available.
    pub context: Option<ParseErrorContext>,

    pub(crate) is_fatal: bool,
}

/// Context attached to a [`ParseError`].
///
/// This is generally attached to an error when the parser knows the context of what it is parsing
/// with confidence.
///
/// # Example
/// In this code, the parser knows that it is parsing the RHS of an expression when the error
/// occurs.
/// ```text
/// 1 + function
///     ^ error
/// ```
/// So it will attach a context to the error with an [`Expression`] context.
///
/// [`Expression`]: ContextType::Expression
#[derive(Debug, Clone)]
pub struct ParseErrorContext {
    /// The range of tokens that this context applies.
    ///
    /// For example, if the context is a [`FunctionDefinitionStatement`], the range will include
    /// the entire function.
    ///
    /// In some cases this will end at the token where the error is encountered, however in many
    /// cases the parser can match delimiters like `{` and `}` to provide more context.
    ///
    /// [`FunctionDefinitionStatement`]: crate::ast::FunctionDefinitionStatement
    pub token_range: Range<usize>,

    /// The type of context.
    pub ty: ContextType,
}

impl ParseError {
    /// Creates a new `ParseError`.
    pub fn new(ty: ParseErrorType, token_index: usize) -> Self {
        ParseError {
            ty,
            token_index,
            context: None,
            is_fatal: false,
        }
    }

    /// Attaches some context to the error.
    pub fn with_context(self, ty: ContextType, token_range: Range<usize>) -> Self {
        self.replace_context(ContextType::Span, ty, token_range)
    }

    pub fn replace_context(
        mut self,
        from_ty: ContextType,
        to_ty: ContextType,
        token_range: Range<usize>,
    ) -> Self {
        // Sanity check, ensure the range includes the actual token.
        let token_range = (self.token_index.min(token_range.start))
            ..((self.token_index + 1).max(token_range.end));

        match &mut self.context {
            // Set a new context if there isn't one already.
            None => {
                self.context = Some(ParseErrorContext {
                    token_range,
                    ty: to_ty,
                });
            }

            // Replace the existing context if it matches the replace type.
            Some(context) if context.ty == from_ty => {
                // Ensure the range contains both, allowing an inner context to expand the outer context.
                let token_range = (token_range.start.min(context.token_range.start))
                    ..(token_range.end.max(context.token_range.end));
                *context = ParseErrorContext {
                    token_range,
                    ty: to_ty,
                };
            }

            // Otherwise, leave the existing context intact.
            _ => {}
        }

        self
    }

    /// Returns an implementation of [`std::fmt::Display`] that pretty-prints the error and context
    /// using [`display_error`].
    pub fn display<'s>(
        &'s self,
        source: &'s str,
        tokens: &'s [TokenItem<'s>],
    ) -> impl std::fmt::Display + 's {
        Display {
            error: self,
            source,
            tokens,
        }
    }

    pub(crate) fn into_fatal(mut self) -> Self {
        self.is_fatal = true;
        self
    }

    pub(crate) fn into_non_fatal(mut self) -> Self {
        self.is_fatal = false;
        self
    }
}

impl std::fmt::Display for ParseErrorType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ParseErrorType::ExpectedTerminal(terminal) => {
                write!(f, "expected `{}`", terminal.as_str())
            }
            ParseErrorType::ExpectedCompound2(token1, token2) => {
                write!(f, "expected `{}{}`", token1.as_str(), token2.as_str())
            }
            ParseErrorType::ExpectedCompound3(token1, token2, token3) => write!(
                f,
                "expected `{}{}{}`",
                token1.as_str(),
                token2.as_str(),
                token3.as_str()
            ),
            ParseErrorType::ExpectedIdentifier => write!(f, "expected an identifier"),
            ParseErrorType::ExpectedLiteral => write!(f, "expected a literal"),
            ParseErrorType::ExpectedExpression => write!(f, "expected an expression"),
            ParseErrorType::ExpectedOperator => write!(f, "expected an operator"),
            ParseErrorType::ExpectedPrefixOperator => write!(f, "expected a prefix operator"),
            ParseErrorType::ExpectedPostfixOperator => write!(f, "expected a postfix operator"),
            ParseErrorType::ExpectedBinaryOperator => write!(f, "expected a binary operator"),
            ParseErrorType::ExpectedType => write!(f, "expected a type"),
            ParseErrorType::ExpectedTypeModifier => write!(f, "expected a type modifier"),
            ParseErrorType::ExpectedTableSlot => write!(f, "expected a table slot"),
            ParseErrorType::ExpectedClassMember => write!(f, "expected a class member"),
            ParseErrorType::ExpectedStatement => write!(f, "expected a statement"),
            ParseErrorType::ExpectedEndOfStatement => {
                write!(f, "expected a newline or `;` to end the statement")
            }
            ParseErrorType::ExpectedGlobalDefinition => write!(f, "expected a global definition"),
            ParseErrorType::IllegalLineBreak => {
                write!(f, "expected anything but `\\n`; got it anyway")
            }
            ParseErrorType::Precedence => write!(f, "not allowed due to precedence rules"),
            ParseErrorType::ExpectedSlot => write!(f, "expected a slot"),
            ParseErrorType::ExpectedStringLiteral => write!(f, "expected a string literal"),
        }
    }
}

struct Display<'s> {
    error: &'s ParseError,
    source: &'s str,
    tokens: &'s [TokenItem<'s>],
}

impl std::fmt::Display for Display<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let src_range = token_src_range(self.error.token_index, self.tokens, self.source);
        write!(
            f,
            "{} {}",
            display_error(src_range, self.source),
            self.error.ty
        )?;

        if let Some(context) = &self.error.context {
            let start_range = token_src_range(context.token_range.start, self.tokens, self.source);
            let end_range = token_src_range(context.token_range.end - 1, self.tokens, self.source);
            writeln!(f)?;
            writeln!(f)?;
            write!(
                f,
                "{} in this {}",
                display_error(start_range.start..end_range.end, self.source),
                context.ty,
            )?;
        }

        Ok(())
    }
}

fn token_src_range(token_index: usize, tokens: &[TokenItem], src: &str) -> Range<usize> {
    match tokens.get(token_index) {
        Some(item) => item.token.range.clone(),
        None => src.len()..src.len(),
    }
}