This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
forked from ocen-lang/ocen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.oc
338 lines (298 loc) · 9.63 KB
/
lexer.oc
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//* The lexer
import std::vector::Vector
import std::span::{Span, Location}
import std::buffer::Buffer
import @errors::Error
import @tokens::{Token, TokenType}
def is_hex_digit(c: char): bool {
if c.is_digit() return true
if 'a' <= c <= 'f' return true
if 'A' <= c <= 'F' return true
return false
}
struct Lexer {
source: str
source_len: u32
i: u32
loc: Location
seen_newline: bool
tokens: &Vector<&Token>
errors: &Vector<&Error>
in_comment: bool
comment: Buffer
comment_start: Location
}
def Lexer::make(source: str, filename: str): Lexer {
let start_loc = Location(filename, 1, 1, 0)
return Lexer(
source,
source_len: source.len(),
i: 0,
loc: start_loc,
seen_newline: false,
tokens: Vector<&Token>::new(),
errors: Vector<&Error>::new(),
in_comment: false,
comment: Buffer::make(),
comment_start: start_loc,
)
}
def Lexer::push(&this, token: &Token) {
token.seen_newline = .seen_newline
if .comment.size > 0 {
token.comment = .comment.new_str()
token.comment_loc = .comment_start
}
.comment.clear()
.tokens.push(token)
.seen_newline = false
.in_comment = false
}
def Lexer::push_type(&this, type: TokenType, len: u32) {
let start_loc = .loc
for let i = 0; i < len; i += 1 {
.inc()
}
.push(Token::from_type(type, Span(start_loc, .loc)))
}
def Lexer::cur(&this): char => .source[.i]
def Lexer::inc(&this) {
match .cur() {
'\n' => {
.loc.line += 1
.loc.col = 1
.seen_newline = true
}
else => .loc.col += 1
}
.i += 1
.loc.index += 1
}
def Lexer::peek(&this, offset: i32): char {
if .cur() == '\0' {
return .cur()
}
return .source[.i + 1]
}
def Lexer::lex_char_literal(&this) {
let start_loc = .loc
let start = .i + 1
.inc()
if .cur() == '\\' {
.inc()
}
.inc()
if .cur() != '\'' {
.errors.push(Error::new(Span(.loc, .loc), "Expected ' after character literal"))
}
let len = .i - start
let text = .source.substring(start, len)
.inc()
.push(Token::new(TokenType::CharLiteral, Span(start_loc, .loc), text))
}
// Format strings can be specified JS-style with backticks, or Python-style with f"..."
// Backticks can be inferred in here directly, but for `f"..."` we need to the lexer to tell
// us whether we saw an `f` right before the string literal or not.
def Lexer::lex_string_literal(&this, has_seen_f: bool) {
let start_loc = .loc
let end_char = .cur()
let start = .i + 1
.inc()
while .i < .source_len and .cur() != end_char {
if .cur() == '\\' {
.inc()
}
.inc()
}
let len = .i - start
let text = .source.substring(start, len)
.inc()
if .i >= .source_len {
.errors.push(Error::new(Span(.loc, .loc), "Unterminated string literal"))
}
if end_char == '`' or has_seen_f {
.push(Token::new(TokenType::FormatStringLiteral, Span(start_loc, .loc), text))
} else {
.push(Token::new(TokenType::StringLiteral, Span(start_loc, .loc), text))
}
}
def Lexer::lex_int_literal_different_base(&this): &Token {
let start_loc = .loc
let start = .i
.inc()
match .cur() {
'x' => {
.inc()
while .i < .source_len and is_hex_digit(.cur()) {
.inc()
}
}
'b' => {
.inc()
while .i < .source_len and .cur() == '0' or .cur() == '1' {
.inc()
}
}
else => assert false, "Invalid base for int literal"
}
let len = .i - start
let text = .source.substring(start, len)
return Token::new(TokenType::IntLiteral, Span(start_loc, .loc), text)
}
def Lexer::lex_numeric_literal_helper(&this): &Token {
let start_loc = .loc
if .cur() == '0' {
match .peek(1) {
'x' | 'b' => {
return .lex_int_literal_different_base()
}
// Do nothing, fall through
else => {}
}
}
let start = .i
let token_type: TokenType
while .cur().is_digit() {
.inc()
}
if .cur() == '.' {
.inc()
while .cur().is_digit() {
.inc()
}
token_type = TokenType::FloatLiteral
} else {
token_type = TokenType::IntLiteral
}
let len = .i - start
let text = .source.substring(start, len)
return Token::new(token_type, Span(start_loc, .loc), text)
}
def Lexer::lex_numeric_literal(&this) {
let token = .lex_numeric_literal_helper()
if .cur() == 'u' or .cur() == 'i' or .cur() == 'f' {
let start_loc = .loc
let start = .i
.inc()
while .i < .source_len and .cur().is_digit() {
.inc()
}
let len = .i - start
let suffix = .source.substring(start, len)
token.suffix = Token::from_ident(suffix, Span(start_loc, .loc))
}
.push(token)
}
def Lexer::lex_comment(&this) {
// Skip leading slashes
while .cur() == '/' { .inc() }
// We only save comments that have a leading asterisk or dot
let save_comment = false
if .cur() == '*' or .cur() == '.' {
.inc()
save_comment = true
if .comment.size == 0 {
.comment_start = .loc
}
}
// Skip whitespace
if .cur() == ' ' or .cur() == '\t' { .inc() }
// Read the comment and store it into the buffer
while .i < .source_len and .cur() != '\n' {
if save_comment then .comment.putc(.cur())
.inc()
}
if save_comment then .comment.putc('\n')
}
def Lexer::lex(&this): &Vector<&Token> {
while .i < .source_len {
let c = .cur()
match c {
' ' | '\t' | '\v' | '\r' | '\b'| '\n' => {
.inc()
}
';' => .push_type(TokenType::Semicolon, len: 1)
',' => .push_type(TokenType::Comma, len: 1)
'.' => .push_type(TokenType::Dot, len: 1)
'(' => .push_type(TokenType::OpenParen, len: 1)
')' => .push_type(TokenType::CloseParen, len: 1)
'[' => .push_type(TokenType::OpenSquare, len: 1)
']' => .push_type(TokenType::CloseSquare, len: 1)
'{' => .push_type(TokenType::OpenCurly, len: 1)
'}' => .push_type(TokenType::CloseCurly, len: 1)
'@' => .push_type(TokenType::AtSign, len: 1)
'%' => .push_type(TokenType::Percent, len: 1)
'^' => .push_type(TokenType::Caret, len: 1)
'&' => .push_type(TokenType::Ampersand, len: 1)
'|' => .push_type(TokenType::Line, len: 1)
'?' => .push_type(TokenType::Question, len: 1)
'~' => .push_type(TokenType::Tilde, len: 1)
'!' => match .peek(1) {
'=' => .push_type(TokenType::NotEquals, len: 2)
else => .push_type(TokenType::Exclamation, len: 1)
}
':' => match .peek(1) {
':' => .push_type(TokenType::ColonColon, len: 2)
else => .push_type(TokenType::Colon, len: 1)
}
'=' => match .peek(1) {
'=' => .push_type(TokenType::EqualEquals, len: 2)
'>' => .push_type(TokenType::FatArrow, len: 2)
else => .push_type(TokenType::Equals, len: 1)
}
'*' => match .peek(1) {
'=' => .push_type(TokenType::StarEquals, len: 2)
else => .push_type(TokenType::Star, len: 1)
}
'+' => match .peek(1) {
'=' => .push_type(TokenType::PlusEquals, len: 2)
'+' => .push_type(TokenType::PlusPlus, len: 2)
else => .push_type(TokenType::Plus, len: 1)
}
'-' => match .peek(1) {
'=' => .push_type(TokenType::MinusEquals, len: 2)
'-' => .push_type(TokenType::MinusMinus, len: 2)
else => .push_type(TokenType::Minus, len: 1)
}
'<' => match .peek(1) {
'=' => .push_type(TokenType::LessThanEquals, len: 2)
else => .push_type(TokenType::LessThan, len: 1)
}
'>' => match .peek(1) {
'=' => .push_type(TokenType::GreaterThanEquals, len: 2)
else => .push_type(TokenType::GreaterThan, len: 1)
}
'/' => match .peek(1) {
'/' => .lex_comment()
'=' => .push_type(TokenType::SlashEquals, len: 2)
else => .push_type(TokenType::Slash, len: 1)
}
'\'' => .lex_char_literal()
'"' | '`' => .lex_string_literal(has_seen_f: false)
else => {
let start_loc = .loc
if c == 'f' and .peek(1) == '"' {
.inc()
.lex_string_literal(has_seen_f: true)
} else if c.is_digit() {
.lex_numeric_literal()
} else if c.is_alpha() or c == '_' {
let start = .i
while .cur().is_alnum() or .cur() == '_' {
.inc()
}
let len = .i - start
let text = .source.substring(start, len)
.push(Token::from_ident(text, Span(start_loc, .loc)))
} else {
.errors.push(Error::new(Span(.loc, .loc), `Unrecognized char in lexer: '{c}'`))
.inc()
}
}
}
}
// We can assume EOF acts like a newline
.seen_newline = true
.push_type(TokenType::EOF, len: 0)
return .tokens
}