|
3 | 3 | externcrate lalrpop_util; |
4 | 4 | useself::lalrpop_util::ParseErrorasInnerError; |
5 | 5 |
|
6 | | -usecrate::lexer::{LexicalError,Location}; |
| 6 | +usecrate::lexer::{LexicalError,LexicalErrorType,Location}; |
7 | 7 | usecrate::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 | | -typeTokSpan =(Location,Tok,Location); |
14 | | - |
15 | 12 | /// Represents an error during parsing |
16 | 13 | #[derive(Debug,PartialEq)] |
17 | | -pubenumParseError{ |
| 14 | +pubstructParseError{ |
| 15 | +puberror:ParseErrorType, |
| 16 | +publocation:Location, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug,PartialEq)] |
| 20 | +pubenumParseErrorType{ |
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(LexicalError), |
| 30 | +Lexical(LexicalErrorType), |
28 | 31 | } |
29 | 32 |
|
30 | 33 | /// Convert `lalrpop_util::ParseError` to our internal type |
31 | 34 | implFrom<InnerError<Location,Tok,LexicalError>>forParseError{ |
32 | 35 | fnfrom(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 | | -InnerError::User{ error} =>ParseError::Other(error), |
| 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 | +}, |
38 | 50 | InnerError::UnrecognizedToken{ token, expected} =>{ |
39 | 51 | match token{ |
40 | | -Some(tok) =>ParseError::UnrecognizedToken(tok, expected), |
| 52 | +Some(tok) =>ParseError{ |
| 53 | +error:ParseErrorType::UnrecognizedToken(tok.1, expected), |
| 54 | +location: tok.0, |
| 55 | +}, |
41 | 56 | // EOF was observed when it was unexpected |
42 | | -None =>ParseError::EOF(None), |
| 57 | +None =>ParseError{ |
| 58 | +error:ParseErrorType::EOF, |
| 59 | +location:Default::default(), |
| 60 | +}, |
43 | 61 | } |
44 | 62 | } |
45 | 63 | } |
46 | 64 | } |
47 | 65 | } |
48 | 66 |
|
49 | 67 | impl fmt::DisplayforParseError{ |
| 68 | +fnfmt(&self,f:&mut fmt::Formatter) -> fmt::Result{ |
| 69 | +write!(f,"{} at {}",self.error,self.location) |
| 70 | +} |
| 71 | +} |
| 72 | + |
| 73 | +impl fmt::DisplayforParseErrorType{ |
50 | 74 | fnfmt(&self,f:&mut fmt::Formatter) -> fmt::Result{ |
51 | 75 | match*self{ |
52 | | -ParseError::EOF(ref location) =>{ |
53 | | -ifletSome(l) = location{ |
54 | | -write!(f,"Got unexpected EOF at: {:?}", l) |
55 | | -}else{ |
56 | | -write!(f,"Got unexpected EOF") |
57 | | -} |
58 | | -} |
59 | | -ParseError::ExtraToken(ref t_span) =>{ |
60 | | -write!(f,"Got extraneous token: {:?} at: {:?}", t_span.1, t_span.0) |
61 | | -} |
62 | | -ParseError::InvalidToken(ref location) =>{ |
63 | | -write!(f,"Got invalid token at: {:?}", location) |
64 | | -} |
65 | | -ParseError::UnrecognizedToken(ref t_span, _) =>{ |
66 | | -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) |
67 | 81 | } |
68 | | -ParseError::Other(ref error) =>write!(f,"{}", error), |
| 82 | +ParseErrorType::Lexical(ref error) =>write!(f,"{}", error), |
69 | 83 | } |
70 | 84 | } |
71 | 85 | } |
|