-
Notifications
You must be signed in to change notification settings - Fork 3
/
lexer.rkt
424 lines (398 loc) · 14.1 KB
/
lexer.rkt
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
#lang racket/base
(provide dssl2-empty-tokens dssl2-tokens new-dssl2-lexer
natural float hexadecimal octal binary
comment identifier
sq-str-char dq-str-char
lsq-str-contents ldq-str-contents)
(require parser-tools/lex
(prefix-in : parser-tools/lex-sre)
racket/list
racket/match
syntax/readerr
(for-syntax racket/base))
(define-empty-tokens dssl2-empty-tokens
(EOF
INDENT
DEDENT
NEWLINE
LPAREN
RPAREN
LBRACK
RBRACK
LBRACE
RBRACE
COMMA
PERIOD
COLON
SEMICOLON
EQUALS
IS
NOT
NOT-IN ; ∉
PLUS ; two different precedences
MINUS ; two different precedences
ARROW
LAMBDA
ASSERT
ASSERT-ERROR
LET
IF
ELIF
ELSE
WHILE
FOR
IN
BREAK
CONTINUE
IMPORT
RETURN
PASS
TEST
TIME
DEF
STRUCT
INTERFACE
CLASS))
(define-tokens dssl2-tokens
(IDENT
OP0 ; or
OP1 ; and
OP2 ; not
OP3 ; == != ≠ <= ≤ >= ≥ > (< is "is not")
OP4 ; (|)
OP5 ; ^
OP6 ; &
OP7 ; << >>
OP8 ; (+ -)
OP9 ; * / // %
OP10 ; (unary) ~ (+ -)
OP11 ; **
OP-LESS
LITERAL
STRING-LITERAL))
(define-lex-abbrevs
[line-break (:: (:? #\return) #\newline)]
[space (:or #\space #\uA0)]
[not-eol (:- any-char (:or #\return #\newline))]
[natural (:+ numeric)]
[exponent (:: (:or #\e #\E) (:? #\-) natural)]
[pointfloat (:or (:: natural #\. (:* numeric))
(:: (:* numeric) #\. natural))]
[float (:or (:: pointfloat (:? exponent))
(:: natural exponent))]
[hexdigit (:or numeric (char-range #\a #\f) (char-range #\A #\F))]
[hexadecimal (:: (:? #\-) (:or "0x" "0X") (:+ hexdigit))]
[octdigit (char-range #\0 #\7)]
[octal (:: (:? #\-) (:or "0o" "0O") (:+ octdigit))]
[binary (:: (:? #\-) (:or "0b" "0B") (:+ (:or #\0 #\1)))]
[comment (:: #\# (:* not-eol))]
[sq-str-char (:or (:- not-eol (:or #\\ #\'))
(:: #\\ (:or not-eol line-break)))]
[dq-str-char (:or (:- not-eol (:or #\\ #\"))
(:: #\\ (:or not-eol line-break)))]
[lsq-str-contents
(:- (:* (:or (:- any-char #\\)
(:: #\\ any-char)))
(:: (:* any-char)
#\' #\' #\'
(:* any-char))
(:: (:* any-char)
#\'))]
[ldq-str-contents
(:- (:* (:or (:- any-char #\\)
(:: #\\ any-char)))
(:: (:* any-char)
#\" #\" #\"
(:* any-char))
(:: (:* any-char)
#\"))]
[identifier (:: (:or alphabetic #\_)
(:* (:or alphabetic numeric #\_))
(:? (:or #\! #\?)))])
; new-dssl2-lexer : string? input-port? boolean? -> [ -> Token]
(define (new-dssl2-lexer src port interactive?)
(define stack '(0))
(define queue '())
(define (push item)
(set! stack (cons item stack)))
(define (pop)
(define result (first stack))
(set! stack (rest stack))
result)
(define (enq item start end)
(set! queue (append queue (list (position-token item start end)))))
(define (deq)
(define result (first queue))
(set! queue (rest queue))
result)
(define (lexical-error pos msg . args)
(define offset (position-offset pos))
(raise-read-error (apply format msg args)
src
(position-line pos)
(position-col pos)
offset
(and offset (max 1 (- (file-position port) offset)))))
(define (lexical-unexpected pos descr culprit)
(cond
[culprit (lexical-error pos "Unexpected ~a ‘~a’" descr culprit)]
[else (lexical-error pos "Unexpected ~a" descr)]))
(define (closing closer token pos)
(cond
[(number? (first stack))
(lexical-unexpected pos "closing delimeter" closer)]
[(eq? (first stack) closer)
(pop)
(enq token pos pos)
(deq)]
[else
(lexical-error pos "Expected ‘~a’ but got ‘~a’"
(first stack) closer)]))
(define (on-eof start-pos end-pos)
(when (not (eqv? 0 (first stack)))
(enq (token-NEWLINE) start-pos end-pos))
(let loop []
(when (not (eqv? 0 (first stack)))
(enq (token-DEDENT) start-pos end-pos)
(pop)
(loop)))
(enq (token-NEWLINE) start-pos end-pos)
(enq (token-EOF) start-pos end-pos)
(deq))
(define (on-indent indent start-pos end-pos)
(cond
[(number? (first stack))
(enq (token-NEWLINE) start-pos end-pos)
(cond
[(> indent (first stack))
(push indent)
(enq (token-INDENT) start-pos end-pos)]
[(= indent (first stack))
(void)]
[else
(let loop []
(cond
[(< indent (first stack))
(enq (token-DEDENT) start-pos end-pos)
(pop)
(loop)]
[(= indent (first stack))
(void)]
[else
(lexical-error start-pos "Inconsistent dedent")]))])
(deq)]
[else (the-lexer port)]))
(define the-lexer
(lexer-src-pos
[(eof) (return-without-pos
(on-eof start-pos end-pos))]
[#\(
(begin
(push #\))
(token-LPAREN))]
[#\[
(begin
(push #\])
(token-LBRACK))]
[#\{ (begin
(push #\})
(token-LBRACE))]
[#\) (return-without-pos
(closing #\) (token-RPAREN) start-pos))]
[#\] (return-without-pos
(closing #\] (token-RBRACK) start-pos))]
[#\} (return-without-pos
(closing #\} (token-RBRACE) start-pos))]
[#\, (token-COMMA)]
[#\. (token-PERIOD)]
[#\: (token-COLON)]
[#\; (token-SEMICOLON)]
[#\= (token-EQUALS)]
["is" (token-IS)]
["not" (token-NOT)]
[#\∉ (token-NOT-IN)]
[#\+ (token-PLUS)]
[#\- (token-MINUS)]
["->" (token-ARROW)]
["let" (token-LET)]
["assert" (token-ASSERT)]
["assert_error" (token-ASSERT-ERROR)]
["if" (token-IF)]
["elif" (token-ELIF)]
["else" (token-ELSE)]
["while" (token-WHILE)]
["for" (token-FOR)]
[(:or "in" "∈") (token-IN)]
["break" (token-BREAK)]
["continue" (token-CONTINUE)]
["import" (token-IMPORT)]
["return" (token-RETURN)]
["pass" (token-PASS)]
["lambda" (token-LAMBDA)]
[#\λ (token-LAMBDA)]
["True" (token-LITERAL #t)]
["False" (token-LITERAL #f)]
["None" (token-LITERAL (void))]
["def" (token-DEF)]
["struct" (token-STRUCT)]
["interface" (token-INTERFACE)]
["class" (token-CLASS)]
["test" (token-TEST)]
["time" (token-TIME)]
["or" (token-OP0 (string->symbol lexeme))]
["and" (token-OP1 (string->symbol lexeme))]
[(:or "==" "<=" #\≤ #\> ">=" #\≥ "!=" #\≠)
(token-OP3 (string->symbol lexeme))]
[#\< (token-OP-LESS (string->symbol lexeme))]
[#\| (token-OP4 (string->symbol lexeme))]
[#\^ (token-OP5 (string->symbol lexeme))]
[#\& (token-OP6 (string->symbol lexeme))]
[(:or "<<" ">>") (token-OP7 (string->symbol lexeme))]
[(:or #\* #\/ "//" #\%) (token-OP9 (string->symbol lexeme))]
[#\~ (token-OP10 (string->symbol lexeme))]
["**" (token-OP11 (string->symbol lexeme))]
[(:: "'''" lsq-str-contents "'''")
(token-STRING-LITERAL
(interpret-string (remove-first-and-last 3 lexeme)))]
[(:: "\"\"\"" ldq-str-contents "\"\"\"")
(token-STRING-LITERAL
(interpret-string (remove-first-and-last 3 lexeme)))]
[(:: "'''" lsq-str-contents)
(lexical-error start-pos "Unterminated long string")]
[(:: "\"\"\"" ldq-str-contents)
(lexical-error start-pos "Unterminated long string")]
[(:: #\" (:* dq-str-char) #\")
(token-STRING-LITERAL
(interpret-string (remove-first-and-last 1 lexeme)))]
[(:: #\' (:* sq-str-char) #\')
(token-STRING-LITERAL
(interpret-string (remove-first-and-last 1 lexeme)))]
[(:: #\" (:* dq-str-char))
(lexical-error start-pos "Unterminated string")]
[(:: #\' (:* sq-str-char))
(lexical-error start-pos "Unterminated string")]
[natural (token-LITERAL (read-string lexeme))]
[float (token-LITERAL (read-string lexeme))]
[hexadecimal (token-LITERAL (interpret-non-dec lexeme))]
[octal (token-LITERAL (interpret-non-dec lexeme))]
[binary (token-LITERAL (interpret-non-dec lexeme))]
["inf" (token-LITERAL +inf.0)]
["nan" (token-LITERAL +nan.0)]
[identifier (token-IDENT (string->symbol lexeme))]
[space
(return-without-pos (the-lexer port))]
[comment
(return-without-pos (the-lexer port))]
[(:: #\\ (:* space) (:? comment) line-break)
(return-without-pos (the-lexer port))]
[(:: (:* (:: line-break (:* space) (:? comment)))
line-break (:* space))
(return-without-pos
(on-indent (last-spaces lexeme) start-pos end-pos))]
[#\tab
(lexical-error start-pos "Tabs are not allowed in DSSL2")]
[any-char
(lexical-error start-pos "Unexpected character ‘~a’ (~a)"
lexeme (char->integer (string-ref lexeme 0)))]
[(special)
(lexical-unexpected start-pos "special" lexeme)]
[(special-comment)
(lexical-unexpected start-pos "special comment" lexeme)]))
(port-count-lines! port)
(λ ()
(define result
(cond
[(cons? queue) (deq)]
[else (the-lexer port)]))
; (displayln (format "Token: ~a" result))
result))
; string? -> string?
; Removes the first `n` and last `n` characters of a string.
(define (remove-first-and-last n str)
(substring str n (- (string-length str) n)))
; Expands to a match pattern that matches an octal digit.
(define-match-expander digit/8
(λ (stx)
(syntax-case stx ()
[(_ pat ...)
#'(and (or #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7)
pat
...)])))
; Expands to a match pattern that matches a hex digit.
(define-match-expander digit/16
(λ (stx)
(syntax-case stx ()
[(_ pat ...)
#'(and (or #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9
#\A #\B #\C #\D #\E #\F
#\a #\b #\c #\d #\e #\f)
pat
...)])))
; string? -> string?
; Interprets the escapes in a string literal.
(define (interpret-string lit)
(define (unescape chars)
(match chars
; C-style escapes
[(cons #\a rst) (values rst #\007)]
[(cons #\b rst) (values rst #\backspace)]
[(cons #\f rst) (values rst #\page)]
[(cons #\n rst) (values rst #\newline)]
[(cons #\r rst) (values rst #\return)]
[(cons #\t rst) (values rst #\tab)]
[(cons #\v rst) (values rst #\vtab)]
; hexadecimal character escapes
[(list* #\x (digit/16 c1) (digit/16 c2) rst)
(values rst (hex->char c1 c2))]
[(list* #\x (digit/16 c1) rst)
(values rst (hex->char c1))]
; octal character escapes
[(list* (digit/8 c1) (digit/8 c2) (digit/8 c3) rst)
(values rst (oct->char c1 c2 c3))]
[(list* (digit/8 c1) (digit/8 c2) rst)
(values rst (oct->char c1 c2))]
[(list* (digit/8 c1) rst)
(values rst (oct->char c1))]
; escaped line breaks
[(or (list* #\return #\newline rst)
(list* #\newline rst))
(values rst #f)]
; otherwise
[(cons c1 rst)
(values rst c1)]))
(define (loop chars acc)
(match chars
['()
(reverse acc)]
[(cons #\\ escaped)
(define-values (rst c) (unescape escaped))
(if (eq? c #false)
(loop rst acc)
(loop rst (cons c acc)))]
[(list* #\return #\newline rst)
(loop rst (cons #\newline acc))]
[(cons c1 rst)
(loop rst (cons c1 acc))]))
(list->string (loop (string->list lit) '())))
; char? ... -> char?
; Converts two hex digits to the represented character.
(define (hex->char . digits)
(integer->char
(read-string (list->string (list* #\# #\x digits)))))
; char? ... -> char?
; Converts octal digits to the represented character.
(define (oct->char . digits)
(integer->char
(read-string (list->string (list* #\# #\o digits)))))
; string? -> number?
; Interprets a Python non-decimal number.
(define (interpret-non-dec str)
(read-string (string-append "#" (substring str 1))))
; string? -> number?
; Counts the spaces on the last line of the string.
(define (last-spaces str)
(string-length (regexp-replace #rx".*\n" str "")))
; string? -> any?
; Reads from a string
(define (read-string str)
(read (open-input-string str)))