Skip to content

Commit f09911d

Browse files
refactor: inlining lexical analysis utilities (#29)
1 parent f657e8f commit f09911d

File tree

2 files changed

+43
-46
lines changed

2 files changed

+43
-46
lines changed

src/lexical_analysis/mod.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ use std::str::Chars;
33

44
use crate::lexical_analysis::model::lexical_error::LexicalError;
55
use crate::lexical_analysis::model::token::Token;
6-
use crate::lexical_analysis::utilities::*;
76

87
pub(crate) mod model;
9-
mod utilities;
108

119
pub(crate) struct LexicalAnalysis<'a> {
1210
source_code: Peekable<Chars<'a>>,
@@ -153,3 +151,46 @@ impl<'a> LexicalAnalysis<'a> {
153151
Ok(string)
154152
}
155153
}
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+
}

src/lexical_analysis/utilities/mod.rs

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)