1
+ use std:: io;
2
+ use std:: io:: Write ;
3
+ use std:: process;
1
4
use itertools:: Itertools ;
2
5
use super :: source:: Source ;
3
6
use super :: token:: { Token , TokenCache } ;
@@ -33,15 +36,18 @@ impl<'a> Lexer<'a> {
33
36
break ;
34
37
}
35
38
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
+ }
37
43
}
38
44
}
39
45
}
40
46
41
47
fn number ( & mut self ) -> Result < i32 , String > {
42
48
let start_int: String = match self . source . current_char ( ) {
43
49
Some ( c) if c. is_digit ( 10 ) => Ok ( c) ,
44
- _ => Err ( "Internal Lexer Error, expected number" )
50
+ _ => Err ( "Internal Lexer Error: Expected number" )
45
51
} ?. to_string ( ) ;
46
52
47
53
let final_int = self . source . by_ref ( )
@@ -51,7 +57,7 @@ impl<'a> Lexer<'a> {
51
57
return acc;
52
58
} )
53
59
. parse :: < i32 > ( )
54
- . or ( Err ( "Internal Lexer Error, failed to parse integer" ) ) ?;
60
+ . or ( Err ( "Internal Lexer Error: Failed to parse integer" ) ) ?;
55
61
56
62
return Ok ( final_int) ;
57
63
}
@@ -64,8 +70,8 @@ impl<'a> Lexer<'a> {
64
70
65
71
let decimal = match self . source . next ( ) {
66
72
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) ) )
69
75
} ?;
70
76
71
77
let mut string_real: String = integer. to_string ( ) ;
@@ -83,7 +89,7 @@ impl<'a> Lexer<'a> {
83
89
fn id ( & mut self ) -> Result < Token , String > {
84
90
let start_id: String = match self . source . current_char ( ) {
85
91
Some ( c) if c. is_alphabetic ( ) => Ok ( c) ,
86
- _ => Err ( "Internal Lexer Error, expected alphabetic character" )
92
+ _ => Err ( "Internal Lexer Error: Expected alphabetic character" )
87
93
} ?. to_string ( ) ;
88
94
let final_id: String = self . source . by_ref ( )
89
95
. peeking_take_while ( | c : & char | c. is_alphanumeric ( ) || c == & '_' )
@@ -119,7 +125,7 @@ impl<'a> Lexer<'a> {
119
125
fn string ( & mut self ) -> Result < Token , String > {
120
126
match self . source . current_char ( ) {
121
127
Some ( '\'' ) => Ok ( ( ) ) ,
122
- _ => Err ( "Internal Lexer Error, expected '\' ' character" )
128
+ _ => Err ( "Internal Lexer Error: Expected '\' ' character" )
123
129
} ?;
124
130
125
131
let final_string: String = self . source . by_ref ( )
@@ -131,7 +137,7 @@ impl<'a> Lexer<'a> {
131
137
132
138
match self . source . next ( ) {
133
139
Some ( '\'' ) => Ok ( ( ) ) ,
134
- _ => Err ( "Internal Lexer Error, expected '\' ' character" )
140
+ _ => Err ( "Internal Lexer Error: Expected '\' ' character" )
135
141
} ?;
136
142
137
143
return Ok ( Token :: STRING_LITERAL ( final_string) ) ;
@@ -140,7 +146,7 @@ impl<'a> Lexer<'a> {
140
146
fn assign ( & mut self ) -> Result < Token , String > {
141
147
return match ( self . source . current_char ( ) , self . source . next ( ) ) {
142
148
( Some ( ':' ) , Some ( '=' ) ) => Ok ( Token :: ASSIGN ) ,
143
- _ => Err ( String :: from ( "Internal Lexer Error: expected ':=' characters" ) )
149
+ _ => Err ( String :: from ( "Internal Lexer Error: Expected ':=' characters" ) )
144
150
} ;
145
151
}
146
152
@@ -165,7 +171,7 @@ impl<'a> Lexer<'a> {
165
171
_ => Ok ( Token :: GREATER_THAN )
166
172
} ,
167
173
Some ( '=' ) => Ok ( Token :: EQUAL ) ,
168
- _ => Err ( String :: from ( "Internal Lexer Error: expected comparison character" ) )
174
+ _ => Err ( String :: from ( "Internal Lexer Error: Expected comparison character" ) )
169
175
} ;
170
176
}
171
177
@@ -192,7 +198,7 @@ impl<'a> Lexer<'a> {
192
198
Some ( '(' ) => Ok ( Token :: LPAREN ) ,
193
199
Some ( ')' ) => Ok ( Token :: RPAREN ) ,
194
200
None => Ok ( Token :: EOF ) ,
195
- Some ( character) => Err ( format ! ( "Unknown Token: '{}'" , character) ) ,
201
+ Some ( character) => Err ( format ! ( "Lexer Error: Unknown Token '{}'" , character) ) ,
196
202
}
197
203
}
198
204
}
0 commit comments