|
3 | 3 | extern crate lalrpop_util;
|
4 | 4 | use self::lalrpop_util::ParseError as InnerError;
|
5 | 5 |
|
6 |
| -use crate::lexer::{LexicalError, Location}; |
| 6 | +use crate::lexer::{LexicalError, LexicalErrorType, Location}; |
7 | 7 | use crate::token::Tok;
|
8 | 8 |
|
9 | 9 | use std::error::Error;
|
10 | 10 | use std::fmt;
|
11 | 11 |
|
12 |
| -// A token of type `Tok` was observed, with a span given by the two Location values |
13 |
| -type TokSpan = (Location, Tok, Location); |
14 |
| - |
15 | 12 | /// Represents an error during parsing
|
16 | 13 | #[derive(Debug, PartialEq)]
|
17 |
| -pub enum ParseError { |
| 14 | +pub struct ParseError { |
| 15 | + pub error: ParseErrorType, |
| 16 | + pub location: Location, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug, PartialEq)] |
| 20 | +pub enum ParseErrorType { |
18 | 21 | /// Parser encountered an unexpected end of input
|
19 |
| - EOF(Option<Location>), |
| 22 | + EOF, |
20 | 23 | /// Parser encountered an extra token
|
21 |
| - ExtraToken(TokSpan), |
| 24 | + ExtraToken(Tok), |
22 | 25 | /// Parser encountered an invalid token
|
23 |
| - InvalidToken(Location), |
| 26 | + InvalidToken, |
24 | 27 | /// Parser encountered an unexpected token
|
25 |
| - UnrecognizedToken(TokSpan, Vec<String>), |
| 28 | + UnrecognizedToken(Tok, Vec<String>), |
26 | 29 | /// Maps to `User` type from `lalrpop-util`
|
27 |
| - Other, |
| 30 | + Lexical(LexicalErrorType), |
28 | 31 | }
|
29 | 32 |
|
30 | 33 | /// Convert `lalrpop_util::ParseError` to our internal type
|
31 | 34 | impl From<InnerError<Location, Tok, LexicalError>> for ParseError {
|
32 | 35 | fn from(err: InnerError<Location, Tok, LexicalError>) -> Self {
|
33 | 36 | match err {
|
34 | 37 | // TODO: Are there cases where this isn't an EOF?
|
35 |
| - InnerError::InvalidToken { location } => ParseError::EOF(Some(location)), |
36 |
| - InnerError::ExtraToken { token } => ParseError::ExtraToken(token), |
37 |
| - // Inner field is a unit-like enum `LexicalError::StringError` with no useful info |
38 |
| - InnerError::User { .. } => ParseError::Other, |
| 38 | + InnerError::InvalidToken { location } => ParseError { |
| 39 | + error: ParseErrorType::EOF, |
| 40 | + location, |
| 41 | + }, |
| 42 | + InnerError::ExtraToken { token } => ParseError { |
| 43 | + error: ParseErrorType::ExtraToken(token.1), |
| 44 | + location: token.0, |
| 45 | + }, |
| 46 | + InnerError::User { error } => ParseError { |
| 47 | + error: ParseErrorType::Lexical(error.error), |
| 48 | + location: error.location, |
| 49 | + }, |
39 | 50 | InnerError::UnrecognizedToken { token, expected } => {
|
40 | 51 | match token {
|
41 |
| - Some(tok) => ParseError::UnrecognizedToken(tok, expected), |
| 52 | + Some(tok) => ParseError { |
| 53 | + error: ParseErrorType::UnrecognizedToken(tok.1, expected), |
| 54 | + location: tok.0, |
| 55 | + }, |
42 | 56 | // EOF was observed when it was unexpected
|
43 |
| - None => ParseError::EOF(None), |
| 57 | + None => ParseError { |
| 58 | + error: ParseErrorType::EOF, |
| 59 | + location: Default::default(), |
| 60 | + }, |
44 | 61 | }
|
45 | 62 | }
|
46 | 63 | }
|
47 | 64 | }
|
48 | 65 | }
|
49 | 66 |
|
50 | 67 | impl fmt::Display for ParseError {
|
| 68 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 69 | + write!(f, "{} at {}", self.error, self.location) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl fmt::Display for ParseErrorType { |
51 | 74 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
52 | 75 | match *self {
|
53 |
| - ParseError::EOF(ref location) => { |
54 |
| - if let Some(l) = location { |
55 |
| - write!(f, "Got unexpected EOF at: {:?}", l) |
56 |
| - } else { |
57 |
| - write!(f, "Got unexpected EOF") |
58 |
| - } |
59 |
| - } |
60 |
| - ParseError::ExtraToken(ref t_span) => { |
61 |
| - write!(f, "Got extraneous token: {:?} at: {:?}", t_span.1, t_span.0) |
62 |
| - } |
63 |
| - ParseError::InvalidToken(ref location) => { |
64 |
| - write!(f, "Got invalid token at: {:?}", location) |
65 |
| - } |
66 |
| - ParseError::UnrecognizedToken(ref t_span, _) => { |
67 |
| - write!(f, "Got unexpected token: {:?} at {:?}", t_span.1, t_span.0) |
| 76 | + ParseErrorType::EOF => write!(f, "Got unexpected EOF"), |
| 77 | + ParseErrorType::ExtraToken(ref tok) => write!(f, "Got extraneous token: {:?}", tok), |
| 78 | + ParseErrorType::InvalidToken => write!(f, "Got invalid token"), |
| 79 | + ParseErrorType::UnrecognizedToken(ref tok, _) => { |
| 80 | + write!(f, "Got unexpected token: {:?}", tok) |
68 | 81 | }
|
69 |
| - // This is user defined, it probably means a more useful error should have been given upstream. |
70 |
| - ParseError::Other => write!(f, "Got unsupported token(s)"), |
| 82 | + ParseErrorType::Lexical(ref error) => write!(f, "{}", error), |
71 | 83 | }
|
72 | 84 | }
|
73 | 85 | }
|
|
0 commit comments