-
Notifications
You must be signed in to change notification settings - Fork 6
/
lexer.gleam
612 lines (522 loc) · 14.3 KB
/
lexer.gleam
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// IMPORTS ---------------------------------------------------------------------
import gleam/float
import gleam/function
import gleam/int
import gleam/list
import gleam/regex
import gleam/result
import gleam/set.{Set}
import gleam/string
// TYPES -----------------------------------------------------------------------
///
///
pub opaque type Matcher(a, mode) {
Matcher(run: fn(mode, String, String) -> Match(a, mode))
}
pub type Match(a, mode) {
Keep(a, mode)
Skip
Drop(mode)
NoMatch
}
///
///
pub type Token(a) {
Token(span: Span, lexeme: String, value: a)
}
///
///
pub type Span {
Span(row_start: Int, col_start: Int, row_end: Int, col_end: Int)
}
///
///
pub type Error {
NoMatchFound(row: Int, col: Int, lexeme: String)
}
///
///
pub opaque type Lexer(a, mode) {
Lexer(matchers: fn(mode) -> List(Matcher(a, mode)))
}
type State(a) {
State(
source: List(String),
tokens: List(Token(a)),
current: #(Int, Int, String),
row: Int,
col: Int,
)
}
// LEXER CONSTRUCTORS ----------------------------------------------------------
///
///
pub fn simple(matchers: List(Matcher(a, Nil))) -> Lexer(a, Nil) {
Lexer(fn(_) { matchers })
}
///
///
pub fn advanced(matchers: fn(mode) -> List(Matcher(a, mode))) -> Lexer(a, mode) {
Lexer(fn(mode) { matchers(mode) })
}
// MATCHER CONSTRUCTORS --------------------------------------------------------
///
///
pub fn keep(f: fn(String, String) -> Result(a, Nil)) -> Matcher(a, mode) {
use mode, lexeme, lookahead <- Matcher
f(lexeme, lookahead)
|> result.map(Keep(_, mode))
|> result.unwrap(NoMatch)
}
///
///
pub fn drop(f: fn(String, String) -> Bool) -> Matcher(a, mode) {
use mode, lexeme, lookahead <- Matcher
case f(lexeme, lookahead) {
True -> Drop(mode)
False -> NoMatch
}
}
///
///
pub fn skip(matcher: Matcher(a, mode)) -> Matcher(b, mode) {
use mode, lexeme, lookahead <- Matcher
case matcher.run(mode, lexeme, lookahead) {
Keep(_, _) -> Skip
Skip -> Skip
Drop(_) -> Skip
NoMatch -> NoMatch
}
}
///
///
pub fn custom(f: fn(mode, String, String) -> Match(a, mode)) -> Matcher(a, mode) {
Matcher(f)
}
///
///
pub fn map(matcher: Matcher(a, mode), f: fn(a) -> b) -> Matcher(b, mode) {
use mode, lexeme, lookahead <- Matcher
case matcher.run(mode, lexeme, lookahead) {
Keep(value, mode) -> Keep(f(value), mode)
Skip -> Skip
Drop(mode) -> Drop(mode)
NoMatch -> NoMatch
}
}
///
///
pub fn then(
matcher: Matcher(a, mode),
f: fn(a) -> Match(b, mode),
) -> Matcher(b, mode) {
use mode, lexeme, lookahead <- Matcher
case matcher.run(mode, lexeme, lookahead) {
Keep(value, _) -> f(value)
Skip -> Skip
Drop(mode) -> Drop(mode)
NoMatch -> NoMatch
}
}
///
///
pub fn into(matcher: Matcher(a, mode), f: fn(mode) -> mode) -> Matcher(a, mode) {
use mode, lexeme, lookahead <- Matcher
case matcher.run(mode, lexeme, lookahead) {
Keep(value, mode) -> Keep(value, f(mode))
Skip -> Skip
Drop(mode) -> Drop(f(mode))
NoMatch -> NoMatch
}
}
///
///
pub fn ignore(matcher: Matcher(a, mode)) -> Matcher(b, mode) {
use mode, lexeme, lookahead <- Matcher
case matcher.run(mode, lexeme, lookahead) {
Keep(_, mode) -> Drop(mode)
Skip -> Skip
Drop(mode) -> Drop(mode)
NoMatch -> NoMatch
}
}
// COMMON MATCHERS -------------------------------------------------------------
///
///
pub fn token(str: String, value: a) -> Matcher(a, mode) {
use mode, lexeme, _ <- Matcher
case lexeme == str {
True -> Keep(value, mode)
False -> NoMatch
}
}
///
///
pub fn symbol(str: String, breaker: String, value: a) -> Matcher(a, mode) {
let assert Ok(break) = regex.from_string(breaker)
use mode, lexeme, lookahead <- Matcher
case lexeme == str && { lookahead == "" || regex.check(break, lookahead) } {
True -> Keep(value, mode)
False -> NoMatch
}
}
///
///
pub fn keyword(str: String, breaker: String, value: a) -> Matcher(a, mode) {
let assert Ok(break) = regex.from_string(breaker)
use mode, lexeme, lookahead <- Matcher
case lexeme == str && { lookahead == "" || regex.check(break, lookahead) } {
True -> Keep(value, mode)
False -> NoMatch
}
}
///
///
pub fn int(to_value: fn(Int) -> a) -> Matcher(a, mode) {
int_with_separator("", to_value)
}
///
///
pub fn int_with_separator(
separator: String,
to_value: fn(Int) -> a,
) -> Matcher(a, mode) {
let assert Ok(digit) = regex.from_string("[0-9" <> separator <> "]")
let assert Ok(integer) = regex.from_string("^-*[0-9" <> separator <> "]+$")
use mode, lexeme, lookahead <- Matcher
case !regex.check(digit, lookahead) && regex.check(integer, lexeme) {
False -> NoMatch
True -> {
let assert Ok(num) =
lexeme
|> string.replace(separator, "")
|> int.parse
Keep(to_value(num), mode)
}
}
}
///
///
pub fn float(to_value: fn(Float) -> a) -> Matcher(a, mode) {
float_with_separator("", to_value)
}
///
///
pub fn float_with_separator(
separator: String,
to_value: fn(Float) -> a,
) -> Matcher(a, mode) {
let assert Ok(digit) = regex.from_string("[0-9" <> separator <> "]")
let assert Ok(integer) = regex.from_string("^-*[0-9" <> separator <> "]+$")
let assert Ok(number) =
regex.from_string(
"^-*[0-9" <> separator <> "]+\\.[0-9" <> separator <> "]+$",
)
use mode, lexeme, lookahead <- Matcher
let is_int = !regex.check(digit, lookahead) && regex.check(integer, lexeme)
let is_float = !regex.check(digit, lookahead) && regex.check(number, lexeme)
case lexeme {
"." if is_int -> NoMatch
_ if is_float -> {
let assert Ok(num) =
lexeme
|> string.replace(separator, "")
|> float.parse
Keep(to_value(num), mode)
}
_ -> NoMatch
}
}
pub fn number(
from_int: fn(Int) -> a,
from_float: fn(Float) -> a,
) -> Matcher(a, mode) {
number_with_separator("", from_int, from_float)
}
pub fn number_with_separator(
separator: String,
from_int: fn(Int) -> a,
from_float: fn(Float) -> a,
) -> Matcher(a, mode) {
let assert Ok(digit) = regex.from_string("[0-9" <> separator <> "]")
let assert Ok(integer) = regex.from_string("^-*[0-9" <> separator <> "]+$")
let assert Ok(number) =
regex.from_string(
"^-*[0-9" <> separator <> "]+\\.[0-9" <> separator <> "]+$",
)
use mode, lexeme, lookahead <- Matcher
let is_int = !regex.check(digit, lookahead) && regex.check(integer, lexeme)
let is_float = !regex.check(digit, lookahead) && regex.check(number, lexeme)
case lexeme, lookahead {
".", _ if is_int -> NoMatch
_, "." if is_int -> NoMatch
_, _ if is_int -> {
let assert Ok(num) =
lexeme
|> string.replace(separator, "")
|> int.parse
Keep(from_int(num), mode)
}
_, _ if is_float -> {
let assert Ok(num) =
lexeme
|> string.replace(separator, "")
|> float.parse
Keep(from_float(num), mode)
}
_, _ -> NoMatch
}
}
///
///
pub fn string(char: String, to_value: fn(String) -> a) -> Matcher(a, mode) {
let assert Ok(is_string) =
regex.from_string(
"^" <> char <> "([^" <> char <> "\\\\]|\\\\[\\s\\S])*" <> char <> "$",
)
use mode, lexeme, _ <- Matcher
case regex.check(is_string, lexeme) {
True ->
lexeme
|> string.drop_left(1)
|> string.drop_right(1)
|> to_value
|> Keep(mode)
False -> NoMatch
}
}
///
///
pub fn identifier(
start: String,
inner: String,
reserved: Set(String),
to_value: fn(String) -> a,
) -> Matcher(a, mode) {
let assert Ok(ident) = regex.from_string("^" <> start <> inner <> "*$")
let assert Ok(inner) = regex.from_string(inner)
use mode, lexeme, lookahead <- Matcher
case regex.check(inner, lookahead), regex.check(ident, lexeme) {
True, True -> Skip
False, True ->
case set.contains(reserved, lexeme) {
True -> NoMatch
False -> Keep(to_value(lexeme), mode)
}
_, _ -> NoMatch
}
}
///
///
pub fn try_identifier(
start: String,
inner: String,
reserved: Set(String),
to_value: fn(String) -> a,
) -> Result(Matcher(a, mode), regex.CompileError) {
use ident <- result.then(regex.from_string("^" <> start <> inner <> "*$"))
use inner <- result.map(regex.from_string(inner))
use mode, lexeme, lookahead <- Matcher
case regex.check(inner, lookahead), regex.check(ident, lexeme) {
True, True -> Skip
False, True ->
case set.contains(reserved, lexeme) {
True -> NoMatch
False -> Keep(to_value(lexeme), mode)
}
_, _ -> NoMatch
}
}
///
///
pub fn variable(
reserved: Set(String),
to_value: fn(String) -> a,
) -> Matcher(a, mode) {
identifier("[a-z]", "[a-zA-Z0-9_]", reserved, to_value)
}
///
///
pub fn spaces(token: a) -> Matcher(a, mode) {
spaces_(function.constant(token))
}
///
///
pub fn spaces_(to_value: fn(String) -> a) -> Matcher(a, mode) {
let assert Ok(spaces) = regex.from_string("^[ \\t]+")
use mode, lexeme, _ <- Matcher
case regex.check(spaces, lexeme) {
True -> Keep(to_value(lexeme), mode)
False -> NoMatch
}
}
///
///
pub fn whitespace(token: a) -> Matcher(a, mode) {
let assert Ok(whitespace) = regex.from_string("^\\s+$")
use mode, lexeme, _ <- Matcher
case regex.check(whitespace, lexeme) {
True -> Keep(token, mode)
False -> NoMatch
}
}
///
pub fn comment(start: String, to_value: fn(String) -> a) -> Matcher(a, mode) {
let drop_length = string.length(start)
use mode, lexeme, lookahead <- Matcher
case string.starts_with(lexeme, start), lookahead {
True, "\n" ->
lexeme
|> string.drop_left(drop_length)
|> to_value
|> Keep(mode)
True, _ -> Skip
False, _ -> NoMatch
}
}
// RUNNING A LEXER -------------------------------------------------------------
///
///
pub fn run(
source: String,
lexer: Lexer(a, Nil),
) -> Result(List(Token(a)), Error) {
string.to_graphemes(source)
|> State([], #(1, 1, ""), 1, 1)
|> do_run(lexer, Nil, _)
}
///
///
pub fn run_advanced(
source: String,
mode: mode,
lexer: Lexer(a, mode),
) -> Result(List(Token(a)), Error) {
do_run(lexer, mode, State(string.to_graphemes(source), [], #(1, 1, ""), 1, 1))
}
fn do_run(
lexer: Lexer(a, mode),
mode: mode,
state: State(a),
) -> Result(List(Token(a)), Error) {
let matchers = lexer.matchers(mode)
case state.source, state.current {
// If we're at the end of the source and there's no lexeme left to match,
// we're done!
//
// We have to remember to reverse the list of tokens because we've been building
// it backwards using `[token, ..state.tokens]`. This is much quicker than
// trying to prepend to the list as we go.
[], #(_, _, "") -> Ok(list.reverse(state.tokens))
// If we're at the end of the source but there's still a lexeme left to match,
// we'll run the final `do_match` and return the result. If we get a `NoMatch`
// at this point something went wrong.
[], #(start_row, start_col, lexeme) ->
case do_match(mode, lexeme, "", matchers) {
NoMatch -> Error(NoMatchFound(start_row, start_col, lexeme))
Skip -> Error(NoMatchFound(start_row, start_col, lexeme))
Drop(_) -> Ok(list.reverse(state.tokens))
Keep(value, _) -> {
let span = Span(start_row, start_col, state.row, state.col)
let token = Token(span, lexeme, value)
Ok(list.reverse([token, ..state.tokens]))
}
}
// When lexing we include a one-grapheme lookahead to help us with things like
// matching identifiers or other mode-aware tokens. This just takes the
// skip grapheme from the source (we call it `lookahead` here) and calls the
// `do_match` function with it and some other bits.
[lookahead, ..rest], #(start_row, start_col, lexeme) -> {
let row = next_row(state.row, lookahead)
let col = next_col(state.col, lookahead)
case do_match(mode, lexeme, lookahead, matchers) {
Keep(value, mode) -> {
let span = Span(start_row, start_col, state.row, state.col)
let token = Token(span, lexeme, value)
do_run(
lexer,
mode,
State(
source: rest,
tokens: [token, ..state.tokens],
current: #(state.row, state.col, lookahead),
row: row,
col: col,
),
)
}
// A skip says that a matcher has matched the lexeme but still wants to
// consume more input. This is mostly useful for things like identifiers
// where the current lexeme is in the set of reserved words but we can
// see the lookahead and know that it's not a reserved word.
Skip ->
do_run(
lexer,
mode,
State(
source: rest,
tokens: state.tokens,
current: #(start_row, start_col, lexeme <> lookahead),
row: row,
col: col,
),
)
// A drop says that we've matched the lexeme but we don't want to emit a
// token. This is mostly useful for things like comments or whitespace that
// users may not want to appear in the final token stream but do want to
// handle in the lexer.
Drop(mode) ->
do_run(
lexer,
mode,
State(
source: rest,
tokens: state.tokens,
current: #(state.row, state.col, lookahead),
row: row,
col: col,
),
)
NoMatch ->
do_run(
lexer,
mode,
State(
source: rest,
tokens: state.tokens,
current: #(start_row, start_col, lexeme <> lookahead),
row: row,
col: col,
),
)
}
}
}
}
fn do_match(
mode: mode,
str: String,
lookahead: String,
matchers: List(Matcher(a, mode)),
) -> Match(a, mode) {
use _, matcher <- list.fold_until(matchers, NoMatch)
case matcher.run(mode, str, lookahead) {
Keep(_, _) as match -> list.Stop(match)
Skip -> list.Stop(Skip)
Drop(_) as match -> list.Stop(match)
NoMatch -> list.Continue(NoMatch)
}
}
// UTILS -----------------------------------------------------------------------
fn next_col(col: Int, str: String) -> Int {
case str {
"\n" -> 1
_ -> col + 1
}
}
fn next_row(row: Int, str: String) -> Int {
case str {
"\n" -> row + 1
_ -> row
}
}