-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.rs
More file actions
56 lines (49 loc) · 1.52 KB
/
lexer.rs
File metadata and controls
56 lines (49 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#[derive(Debug)]
pub struct Lexer<'a> {
content: &'a [char],
}
impl<'a> Lexer<'a> {
pub fn new(content: &'a [char]) -> Self {
Self { content }
}
fn trim_left(&mut self) {
while self.content.len() > 0 && (self.content[0].is_whitespace()) {
self.content = &self.content[1..];
}
}
fn next_token(&mut self) -> Option<&'a [char]> {
self.trim_left();
if self.content.is_empty() {
return None;
}
if self.content[0].is_numeric() {
while self.content.len() > 0 && self.content[0].is_alphanumeric() {
let mut len = 0;
while len < self.content.len() && self.content[len].is_alphanumeric() {
len += 1;
}
let tok = &self.content[0..len];
self.content = &self.content[len..];
Some(tok);
}
}
if self.content[0].is_alphabetic() {
while self.content.len() > 0 && self.content[0].is_alphanumeric() {
let mut len = 0;
while len < self.content.len() && self.content[len].is_alphanumeric() {
len += 1;
}
let tok = &self.content[0..len];
self.content = &self.content[len..];
Some(tok);
}
}
todo!()
}
}
impl<'a> Iterator for Lexer<'a> {
type Item = &'a [char];
fn next(&mut self) -> Option<Self::Item> {
self.next_token()
}
}