1+ use std:: io;
2+ use std:: io:: Write ;
3+ use std:: process;
14use itertools:: Itertools ;
25use super :: source:: Source ;
36use super :: token:: { Token , TokenCache } ;
@@ -33,15 +36,18 @@ impl<'a> Lexer<'a> {
3336 break ;
3437 }
3538 Ok ( token) => self . token_cache . push ( token) ,
36- Err ( e) => panic ! ( e)
39+ Err ( error) => {
40+ writeln ! ( io:: stderr( ) , "{}" , error) . unwrap ( ) ;
41+ process:: exit ( 1 ) ;
42+ }
3743 }
3844 }
3945 }
4046
4147 fn number ( & mut self ) -> Result < i32 , String > {
4248 let start_int: String = match self . source . current_char ( ) {
4349 Some ( c) if c. is_digit ( 10 ) => Ok ( c) ,
44- _ => Err ( "Internal Lexer Error, expected number" )
50+ _ => Err ( "Internal Lexer Error: Expected number" )
4551 } ?. to_string ( ) ;
4652
4753 let final_int = self . source . by_ref ( )
@@ -51,7 +57,7 @@ impl<'a> Lexer<'a> {
5157 return acc;
5258 } )
5359 . parse :: < i32 > ( )
54- . or ( Err ( "Internal Lexer Error, failed to parse integer" ) ) ?;
60+ . or ( Err ( "Internal Lexer Error: Failed to parse integer" ) ) ?;
5561
5662 return Ok ( final_int) ;
5763 }
@@ -64,8 +70,8 @@ impl<'a> Lexer<'a> {
6470
6571 let decimal = match self . source . next ( ) {
6672 Some ( c) if c. is_digit ( 10 ) => self . number ( ) ,
67- Some ( c) => Err ( String :: from ( format ! ( "Lexing Error: Expected floating point number at: {}.{}" , integer, c) ) ) ,
68- None => Err ( String :: from ( format ! ( "Lexing Error: Expected floating point number at: {}." , integer) ) )
73+ Some ( c) => Err ( String :: from ( format ! ( "Lexer Error: Expected floating point number at: {}.{}" , integer, c) ) ) ,
74+ None => Err ( String :: from ( format ! ( "Lexer Error: Expected floating point number at: {}." , integer) ) )
6975 } ?;
7076
7177 let mut string_real: String = integer. to_string ( ) ;
@@ -83,7 +89,7 @@ impl<'a> Lexer<'a> {
8389 fn id ( & mut self ) -> Result < Token , String > {
8490 let start_id: String = match self . source . current_char ( ) {
8591 Some ( c) if c. is_alphabetic ( ) => Ok ( c) ,
86- _ => Err ( "Internal Lexer Error, expected alphabetic character" )
92+ _ => Err ( "Internal Lexer Error: Expected alphabetic character" )
8793 } ?. to_string ( ) ;
8894 let final_id: String = self . source . by_ref ( )
8995 . peeking_take_while ( | c : & char | c. is_alphanumeric ( ) || c == & '_' )
@@ -119,7 +125,7 @@ impl<'a> Lexer<'a> {
119125 fn string ( & mut self ) -> Result < Token , String > {
120126 match self . source . current_char ( ) {
121127 Some ( '\'' ) => Ok ( ( ) ) ,
122- _ => Err ( "Internal Lexer Error, expected '\' ' character" )
128+ _ => Err ( "Internal Lexer Error: Expected '\' ' character" )
123129 } ?;
124130
125131 let final_string: String = self . source . by_ref ( )
@@ -131,7 +137,7 @@ impl<'a> Lexer<'a> {
131137
132138 match self . source . next ( ) {
133139 Some ( '\'' ) => Ok ( ( ) ) ,
134- _ => Err ( "Internal Lexer Error, expected '\' ' character" )
140+ _ => Err ( "Internal Lexer Error: Expected '\' ' character" )
135141 } ?;
136142
137143 return Ok ( Token :: STRING_LITERAL ( final_string) ) ;
@@ -140,7 +146,7 @@ impl<'a> Lexer<'a> {
140146 fn assign ( & mut self ) -> Result < Token , String > {
141147 return match ( self . source . current_char ( ) , self . source . next ( ) ) {
142148 ( Some ( ':' ) , Some ( '=' ) ) => Ok ( Token :: ASSIGN ) ,
143- _ => Err ( String :: from ( "Internal Lexer Error: expected ':=' characters" ) )
149+ _ => Err ( String :: from ( "Internal Lexer Error: Expected ':=' characters" ) )
144150 } ;
145151 }
146152
@@ -165,7 +171,7 @@ impl<'a> Lexer<'a> {
165171 _ => Ok ( Token :: GREATER_THAN )
166172 } ,
167173 Some ( '=' ) => Ok ( Token :: EQUAL ) ,
168- _ => Err ( String :: from ( "Internal Lexer Error: expected comparison character" ) )
174+ _ => Err ( String :: from ( "Internal Lexer Error: Expected comparison character" ) )
169175 } ;
170176 }
171177
@@ -192,7 +198,7 @@ impl<'a> Lexer<'a> {
192198 Some ( '(' ) => Ok ( Token :: LPAREN ) ,
193199 Some ( ')' ) => Ok ( Token :: RPAREN ) ,
194200 None => Ok ( Token :: EOF ) ,
195- Some ( character) => Err ( format ! ( "Unknown Token: '{}'" , character) ) ,
201+ Some ( character) => Err ( format ! ( "Lexer Error: Unknown Token '{}'" , character) ) ,
196202 }
197203 }
198204}
0 commit comments