@@ -10,6 +10,7 @@ use num_traits::Num;
1010use serde:: { Deserialize , Serialize } ;
1111use std:: cmp:: Ordering ;
1212use std:: collections:: HashMap ;
13+ use std:: fmt;
1314use std:: str:: FromStr ;
1415use unic_emoji_char:: is_emoji_presentation;
1516use unicode_xid:: UnicodeXID ;
@@ -59,13 +60,13 @@ pub struct Lexer<T: Iterator<Item = char>> {
5960 keywords : HashMap < String , Tok > ,
6061}
6162
62- #[ derive( Debug ) ]
63+ #[ derive( Debug , PartialEq ) ]
6364pub struct LexicalError {
6465 pub error : LexicalErrorType ,
6566 pub location : Location ,
6667}
6768
68- #[ derive( Debug ) ]
69+ #[ derive( Debug , PartialEq ) ]
6970pub enum LexicalErrorType {
7071 StringError ,
7172 UnicodeError ,
@@ -74,6 +75,26 @@ pub enum LexicalErrorType {
7475 OtherError ( String ) ,
7576}
7677
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+
7798#[ derive( Clone , Debug , Default , PartialEq , Serialize , Deserialize ) ]
7899pub struct Location {
79100 row : usize ,
0 commit comments