@@ -10,6 +10,7 @@ use num_traits::Num;
10
10
use serde:: { Deserialize , Serialize } ;
11
11
use std:: cmp:: Ordering ;
12
12
use std:: collections:: HashMap ;
13
+ use std:: fmt;
13
14
use std:: str:: FromStr ;
14
15
use unic_emoji_char:: is_emoji_presentation;
15
16
use unicode_xid:: UnicodeXID ;
@@ -59,13 +60,13 @@ pub struct Lexer<T: Iterator<Item = char>> {
59
60
keywords : HashMap < String , Tok > ,
60
61
}
61
62
62
- #[ derive( Debug ) ]
63
+ #[ derive( Debug , PartialEq ) ]
63
64
pub struct LexicalError {
64
65
pub error : LexicalErrorType ,
65
66
pub location : Location ,
66
67
}
67
68
68
- #[ derive( Debug ) ]
69
+ #[ derive( Debug , PartialEq ) ]
69
70
pub enum LexicalErrorType {
70
71
StringError ,
71
72
UnicodeError ,
@@ -74,6 +75,26 @@ pub enum LexicalErrorType {
74
75
OtherError ( String ) ,
75
76
}
76
77
78
+ impl fmt:: Display for LexicalError {
79
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
80
+ match self . error {
81
+ LexicalErrorType :: StringError => write ! ( f, "Got unexpected string" ) ,
82
+ LexicalErrorType :: UnicodeError => write ! ( f, "Got unexpected unicode" ) ,
83
+ LexicalErrorType :: NestingError => write ! ( f, "Got unexpected nesting" ) ,
84
+ LexicalErrorType :: UnrecognizedToken { tok } => {
85
+ write ! ( f, "Got unexpected token {}" , tok)
86
+ }
87
+ LexicalErrorType :: OtherError ( ref msg) => write ! ( f, "{}" , msg) ,
88
+ } ?;
89
+ write ! (
90
+ f,
91
+ " at line {} column {}" ,
92
+ self . location. row( ) ,
93
+ self . location. column( )
94
+ )
95
+ }
96
+ }
97
+
77
98
#[ derive( Clone , Debug , Default , PartialEq , Serialize , Deserialize ) ]
78
99
pub struct Location {
79
100
row : usize ,
0 commit comments