@@ -3,10 +3,8 @@ use std::str::Chars;
3
3
4
4
use crate :: lexical_analysis:: model:: lexical_error:: LexicalError ;
5
5
use crate :: lexical_analysis:: model:: token:: Token ;
6
- use crate :: lexical_analysis:: utilities:: * ;
7
6
8
7
pub ( crate ) mod model;
9
- mod utilities;
10
8
11
9
pub ( crate ) struct LexicalAnalysis < ' a > {
12
10
source_code : Peekable < Chars < ' a > > ,
@@ -153,3 +151,46 @@ impl<'a> LexicalAnalysis<'a> {
153
151
Ok ( string)
154
152
}
155
153
}
154
+
155
+ fn parse_keyword ( parsing : & str ) -> Option < Token > {
156
+ match parsing. to_lowercase ( ) . as_str ( ) {
157
+ "fn" => Some ( Token :: Function ) ,
158
+ "let" => Some ( Token :: Let ) ,
159
+ "true" => Some ( Token :: True ) ,
160
+ "false" => Some ( Token :: False ) ,
161
+ "if" => Some ( Token :: If ) ,
162
+ "else" => Some ( Token :: Else ) ,
163
+ "return" => Some ( Token :: Return ) ,
164
+ _ => None ,
165
+ }
166
+ }
167
+
168
+ fn parse_integer ( parsing : & str ) -> Option < Token > {
169
+ match parsing. parse ( ) {
170
+ Ok ( integer) => Some ( Token :: Integer { literal : integer } ) ,
171
+ Err ( _) => None ,
172
+ }
173
+ }
174
+
175
+ fn parse_identifier ( parsing : & str ) -> Option < Token > {
176
+ if is_valid_identifier ( parsing) {
177
+ Some ( Token :: Identifier {
178
+ literal : parsing. to_string ( ) ,
179
+ } )
180
+ } else {
181
+ None
182
+ }
183
+ }
184
+
185
+ fn is_valid_identifier ( verifying : & str ) -> bool {
186
+ verifying
187
+ . chars ( )
188
+ . map ( is_valid_identifier_character)
189
+ . filter ( |results| !( * results) )
190
+ . count ( )
191
+ == 0
192
+ }
193
+
194
+ fn is_valid_identifier_character ( character : char ) -> bool {
195
+ character. is_alphabetic ( ) || character == '_'
196
+ }
0 commit comments