-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlexer.rs
144 lines (135 loc) · 4.41 KB
/
lexer.rs
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use regex::Regex;
#[derive(PartialEq, Clone, Debug)]
#[allow(dead_code)]
pub enum Token {
KeyWord(KeyWords), // language Keywords
Ident(String), // variable names
IntNumber(isize),
FloatNumber(f64),
Plus, // +
Hyphen, // -
Asterisk, // *
Slash, // /
Dot, // .
QuestionMark, // ?
And, // &&
Or, // ||
Assign, // =
LessThan, // <
GreaterThan, // >
Equal, // ==
NotEqual, // !=
LessThanEqual, // <=
GreaterThanEqual, // >=
Colon, // :
Tilde, // ~
Comma, // ,
Delimiter, // ;
OpenBracket, // (
CloseBracket, // )
OpenCurly, // {
CLoseCurly, // }
}
impl Token {
pub fn get_string(self) -> String {
match self {
Token::Ident(name) => name,
_ => unimplemented!(),
}
}
}
#[derive(PartialEq, Clone, Debug)]
#[allow(non_camel_case_types)]
pub enum KeyWords {
r#number,
r#return,
r#true,
r#false,
r#fn,
r#print,
r#start,
r#extern,
}
use self::Token::*;
pub fn tokenize(input: &str) -> Vec<Token> {
let mut result = Vec::new();
let comments = Regex::new(r"(?m)#.*\n").unwrap();
let no_comments_input = comments.replace_all(input, "\n");
let tokens_to_match = Regex::new(concat!(
r"(?P<ident>\p{Alphabetic}\w*)|",
r"(?P<separator>,|;)|",
r"(?P<logic>[|&]{2})|",
r"(?P<bracket>[()}{])|",
r"(?P<decimal>\d+\.\d+)|",
r"(?P<number>\d+)|",
r"(?P<inequality><=|==|=|>=|!=|<|>)|",
r"(?P<operator>\S)"
))
.unwrap();
for capture in tokens_to_match.captures_iter(no_comments_input.as_ref()) {
let token = if capture.name("ident").is_some() {
match capture.name("ident").unwrap().as_str() {
"number" => KeyWord(KeyWords::r#number),
"return" => KeyWord(KeyWords::r#return),
"true" => KeyWord(KeyWords::r#true),
"false" => KeyWord(KeyWords::r#false),
"fn" => KeyWord(KeyWords::r#fn),
"print" => KeyWord(KeyWords::r#print),
"start" => KeyWord(KeyWords::r#start),
"extern" => KeyWord(KeyWords::r#extern),
ident => Ident(ident.to_string()),
}
} else if capture.name("separator").is_some() {
match capture.name("separator").unwrap().as_str() {
";" => Delimiter,
"," => Comma,
_ => unimplemented!(),
}
} else if capture.name("logic").is_some() {
match capture.name("logic").unwrap().as_str() {
"&&" => And,
"||" => Or,
_ => unimplemented!(),
}
} else if capture.name("decimal").is_some() {
match capture.name("decimal").unwrap().as_str().parse() {
Ok(decimal) => FloatNumber(decimal),
Err(e) => panic!("Lexer failed trying to parse number : {:?}", e),
}
} else if capture.name("bracket").is_some() {
match capture.name("bracket").unwrap().as_str() {
")" => CloseBracket,
"(" => OpenBracket,
"{" => OpenCurly,
"}" => CLoseCurly,
_ => unimplemented!(),
}
} else if capture.name("number").is_some() {
match capture.name("number").unwrap().as_str().parse() {
Ok(number) => IntNumber(number),
Err(e) => panic!("Lexer failed trying to parse number : {:?}", e),
}
} else if capture.name("inequality").is_some() {
match capture.name("inequality").unwrap().as_str() {
"=" => Assign,
"==" => Equal,
"<" => LessThan,
">" => GreaterThan,
"!=" => NotEqual,
"<=" => LessThanEqual,
">=" => GreaterThanEqual,
_ => unreachable!(),
}
} else {
match capture.name("operator").unwrap().as_str() {
"+" => Plus,
"-" => Hyphen,
"*" => Asterisk,
"/" => Slash,
_ => unreachable!(),
}
};
result.push(token);
}
result
}