-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.rkt
42 lines (40 loc) · 1.06 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
#lang racket/base
(provide lex-simple)
(require (prefix-in : parser-tools/lex-sre)
parser-tools/lex)
(define-tokens simple
[IDENTIFIER
NUMBER
STRING])
(define-empty-tokens simple*
[COMMA
ASSIGN
OPEN-PAREN CLOSE-PAREN
+ - * /])
(define-empty-tokens keyword*
[LET])
(define simple-lexer
(lexer-src-pos
[#\( (token-OPEN-PAREN)]
[#\) (token-CLOSE-PAREN)]
[#\, (token-COMMA)]
[#\= (token-ASSIGN)]
[#\+ (token-+)]
[#\- (token--)]
[#\* (token-*)]
[#\/ (token-/)]
["let" (token-LET)]
[(:seq #\" (:+ (:~ #\")) #\")
(token-STRING lexeme)]
[(:+ (:or (:/ #\a #\z) (:/ #\A #\Z) #\-))
(token-IDENTIFIER (string->symbol lexeme))]
[(:+ (:/ #\0 #\9))
(token-NUMBER (string->number lexeme))]
[(:or whitespace blank iso-control) (void)]
[(eof) eof]))
(define (lex-simple in)
(port-count-lines! in)
(let loop ([v (simple-lexer in)])
(cond [(void? (position-token-token v)) (loop (simple-lexer in))]
[(eof-object? (position-token-token v)) '()]
[else (cons v (loop (simple-lexer in)))])))