-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.rkt
75 lines (71 loc) · 2.15 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
#lang racket
(require parser-tools/lex)
(require (prefix-in : parser-tools/lex-sre))
(define-lex-abbrev name (:: (:or alphabetic #\_) (:* (:or alphabetic numeric #\_))))
(define-lex-abbrev string
(:: "\""
(:* (:or "\\\"" (complement (:: any-string "\"" any-string))))
"\""))
(define (string-decode s)
(define p (open-input-string (~a s)))
(read p))
(define-tokens value-tokens (id num string bool sym tyvar typo))
(define-empty-tokens tokens
(begin eof colon semicolon comma lparen rparen lbracket rbracket pipe mapsto
cat cons dot eq gt lt ge le ne plus minus times div mod assign
and case else end if in lambda let of or then type wildcard))
(define lex
(lexer-src-pos
[(eof) (token-eof)]
[whitespace
(return-without-pos (lex input-port))]
[(:: "(*" (complement (:: any-string "*)" any-string)) "*)")
(return-without-pos (lex input-port))]
[":" (token-colon)]
["::" (token-cons)]
[";" (token-semicolon)]
["," (token-comma)]
["." (token-dot)]
["^" (token-cat)]
["=" (token-eq)]
[">" (token-gt)]
["<" (token-lt)]
[">=" (token-ge)]
["<=" (token-le)]
["<>" (token-ne)]
["(" (token-lparen)]
[")" (token-rparen)]
["[" (token-lbracket)]
["]" (token-rbracket)]
["|" (token-pipe)]
["+" (token-plus)]
["-" (token-minus)]
["*" (token-times)]
["/" (token-div)]
["mod" (token-mod)]
["->" (token-mapsto)]
[":=" (token-assign)]
["_" (token-wildcard)]
["and" (token-and)]
["begin" (token-begin)]
["case" (token-case)]
["else" (token-else)]
["end" (token-end)]
["false" (token-bool #f)]
["if" (token-if)]
["in" (token-in)]
["lambda" (token-lambda)]
["let" (token-let)]
["of" (token-of)]
["or" (token-or)]
["then" (token-then)]
["true" (token-bool #t)]
["type" (token-type)]
[name (token-id (string->symbol lexeme))]
[(:: "#" name) (token-sym (string->symbol (substring lexeme 1)))]
[(:: name "?") (token-tyvar (string->symbol lexeme))]
[(:* numeric) (token-num (string->number lexeme))]
[string (token-string (string-decode lexeme))]
[any-char (token-typo lexeme)]
))
(provide lex tokens value-tokens position-line position-col)