-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrust-lexer.rkt
229 lines (212 loc) · 7.23 KB
/
rust-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
#lang racket
#| Copyright [2013] [John Clements <clements@racket-lang.org>]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
|#
(require rackunit
parser-tools/lex
(prefix-in : parser-tools/lex-sre))
(provide (all-defined-out))
;; lex a file into tokens, discard them
(define (lex-file file)
(define ip (open-input-file file))
(port-count-lines! ip)
(let loop ()
(define next-token (rust-lexer ip))
(cond [(eq? (position-token-token next-token) 'EOF)
(begin (close-input-port ip)
#t)]
[else (loop)])))
(define (lex-string string)
(define ip (open-input-string string))
(let loop ()
(define next-token (rust-lexer ip))
(cond [(eq? (position-token-token next-token) 'EOF)
empty]
[else (cons next-token (loop))])))
(define-empty-tokens empty-toks
(AS BREAK CONST COPYTOK DO DROP ELSE ENUM EXTERN FALSE FN FOR IF
IMPL LET __LOG LOOP MATCH MOD MUT ONCE PRIV PUB PURE REF
RETURN SELF STATIC STRUCT TRUE TRAIT TYPE UNSAFE USE WHILE
PLUS AND MINUS DIV REM CARET OR EQ LE LT EQEQ NE GE GT NOT
TILDE STAR AT DOT DOTDOT COMMA SEMI COLON MOD_SEP
RARROW LARROW DARROW FAT_ARROW LPAREN RPAREN LBRACKET
RBRACKET LBRACE RBRACE POUND DOLLAR EOF))
(define-tokens data
(LIT_INT LIT_FLOAT LIT_STR IDENT UNDERSCORE STATIC_LIFETIME
LIFETIME OUTER_DOC_COMMENT INNER_DOC_COMMENT
BINOPEQ))
(define-tokens hack
(LIT_FLOAT_DOTDOT LIT_FLOAT_DOT_IDENT))
(define rust-lexer
(lexer-src-pos
[(:or one-line-outer-doc-comment block-outer-doc-comment)
(token-OUTER_DOC_COMMENT lexeme)]
[(:or one-line-inner-doc-comment block-inner-doc-comment)
(token-INNER_DOC_COMMENT lexeme)]
;; Skip comments, without accumulating extra position information
[(:or rust-whitespace
one-line-comment
block-comment)
(return-without-pos (rust-lexer input-port))]
["as" 'AS ]
[ "break" 'BREAK ]
[ "const" 'CONST ]
[ "copy" 'COPYTOK ]
[ "do" 'DO ]
[ "drop" 'DROP ]
[ "else" 'ELSE ]
[ "enum" 'ENUM ]
[ "extern" 'EXTERN ]
[ "false" 'FALSE ]
[ "fn" 'FN ]
[ "for" 'FOR ]
[ "if" 'IF ]
[ "impl" 'IMPL ]
[ "let" 'LET ]
[ "__log" '__LOG ]
[ "loop" 'LOOP ]
[ "match" 'MATCH ]
[ "mod" 'MOD ]
[ "mut" 'MUT ]
[ "once" 'ONCE ]
[ "priv" 'PRIV ]
[ "pub" 'PUB ]
[ "pure" 'PURE ]
[ "ref" 'REF ]
[ "return" 'RETURN ]
[ "self" 'SELF ]
[ "static" 'STATIC ]
[ "struct" 'STRUCT ]
[ "true" 'TRUE ]
[ "trait" 'TRAIT ]
[ "type" 'TYPE ]
[ "unsafe" 'UNSAFE ]
[ "use" 'USE ]
[ "while" 'WHILE ]
[ "+" 'PLUS ]
[ "&" 'AND ]
[ "-" 'MINUS ]
[ "/" 'DIV ]
[ "%" 'REM ]
[ "^" 'CARET ]
[ "|" 'OR ]
[ "=" 'EQ ]
[ "<=" 'LE ]
[ "<"'LT ]
[ "==" 'EQEQ ]
[ "!=" 'NE ]
[ ">=" 'GE ]
[ ">" 'GT ]
[ "!" 'NOT ]
[ "~" 'TILDE ]
[ "*" 'STAR ]
[ "@" 'AT ]
[ "." 'DOT ]
[ ".." 'DOTDOT ]
[ "," 'COMMA ]
[ ";" ' SEMI ]
[ ":" 'COLON ]
[ "->" 'RARROW ]
[ "::" 'MOD_SEP ]
[ "<-" 'LARROW ]
[ "<->" 'DARROW ]
[ "(" 'LPAREN ]
[ "=>" 'FAT_ARROW ]
[ ")" 'RPAREN ]
[ "[" 'LBRACKET ]
[ "]" 'RBRACKET ]
[ "{" 'LBRACE ]
[ "}" 'RBRACE ]
[ "#" 'POUND ]
[ "$" 'DOLLAR ]
[ident (token-IDENT lexeme)]
[(:or lit-char lit-hexint lit-binint lit-decint) (token-LIT_FLOAT lexeme)]
;; to work around lack of lookahead, we make up a bogus double-token
;; here and split it apart later:
[(:: dec-digit-start (:* dec-digit-cont) "..") (token-LIT_FLOAT_DOTDOT lexeme)]
[(:: dec-digit-start (:* dec-digit-cont) "." ident)
(token-LIT_FLOAT_DOT_IDENT lexeme)]
[(:: dec-digits ".") (token-LIT_FLOAT lexeme)]
[(:: dec-digits "." dec-digits maybe-litfloat-exp maybe-litfloat-ty)
(token-LIT_FLOAT lexeme)]
[(:: dec-digits litfloat-exp maybe-litfloat-ty) (token-LIT_FLOAT lexeme)]
[(:: dec-digits litfloat-ty) (token-LIT_FLOAT lexeme)]
[(:: "\"" (:* str-char) "\"") (token-LIT_STR lexeme)]
["_" 'UNDERSCORE]
["'static" 'STATIC_LIFETIME]
["'self" (token-LIFETIME "'self")]
[(:: "'" ident) (token-LIFETIME lexeme)]
[(eof) 'EOF]))
(define-lex-abbrevs
[ident (:: xid-start (:* xid-continue))]
;; DARN! haven't committed xid-start to racket tree yet...
[xid-start (:or (:/ #\a #\z) (:/ #\A #\Z) #\_)]
[xid-continue (:or (:/ #\a #\z) (:/ #\A #\Z) (:/ #\0 #\9) #\_)]
[dec-digits (:: dec-digit-start (:* dec-digit-cont))]
[dec-digit-start (:/ #\0 #\9)]
[dec-digit-cont (:or dec-digit-start #\_)]
[upper-alpha (:/ #\A #\Z)]
[lower-alpha (:/ #\a #\z)]
[lit-hexint (:: "0x" (:+ hex-digit) maybe-intlit-ty)]
[lit-binint (:: "0b" (:+ bin-digit) maybe-intlit-ty)]
[lit-decint (:: dec-digits maybe-intlit-ty)]
;; different flavors of comments
[one-line-comment (:: #\/ #\/ to-end-of-line)]
[one-line-outer-doc-comment
(:or
(:: "///" (:* "/") non-slash-or-ws to-end-of-line)
(:: "///" (:* "/") (:or #\space #\tab) non-ws-char to-end-of-line)
(:: "///" (:* (:or #\space #\tab)) #\newline))]
[one-line-inner-doc-comment
(:: "//!" to-end-of-line)]
[block-comment
(:: "/*" block-comment-continue block-comment-end)]
;; do a funny dance here to prevent "/******/"
;; I wonder if minus would be as fast?
[block-outer-doc-comment
(:: "/**" block-comment-continue
non-star block-comment-continue block-comment-end)]
[block-inner-doc-comment
(:: "/*!" block-comment-continue block-comment-end)]
[block-comment-continue
(:* (:or (:~ "*") (:: (:+ "*") (:~ "*" "/"))))]
[block-comment-end
(:: (:+ "*") "/")]
#|
SHEBANG_LINE : {at_beginning_of_file()}? '#!' ~[\n]* '\n' -> skip ;
|#
[bin-digit (:or #\0 #\1 #\_)]
[dec-digit (:or #\_ (:/ #\0 #\9))]
[hex-digit (:or dec-digit (:/ #\a #\f) (:/ #\A #\F) #\_)]
[maybe-intlit-ty (:? (:: (:or "u" "i") (:? (:or "8" "16" "32" "64"))))]
[maybe-litfloat-exp (:? litfloat-exp)]
[litfloat-exp (:: (:or "e" "E") (:? (:or #\+ #\-)) (:+ dec-digit-cont))]
[maybe-litfloat-ty (:? litfloat-ty)]
[litfloat-ty (:: "f" (:? (:or "32" "64")))]
[escaped-char
(:or "n" "r" "t" #\\ #\' #\"
(:: "x" hex-digit hex-digit)
(:: "u" hex-digit hex-digit hex-digit hex-digit)
(:: "U" hex-digit hex-digit hex-digit hex-digit
hex-digit hex-digit hex-digit hex-digit))]
[lit-char (:or (:: "'\\" escaped-char "'")
(:: "'" (:~) "'"))]
[str-char (:or (:~ #\\ #\") (:: #\\ str-escape))]
[str-escape (:or #\newline escaped-char)]
[non-slash-or-ws (:~ #\space #\tab #\return #\newline #\/)]
[ws-char (:or #\newline #\return #\tab #\space)]
[non-ws-char (:~ #\newline #\return #\tab #\space)]
[non-star (:~ #\*)]
[to-end-of-line (:: (:* (:~ #\newline)) #\newline)]
[rust-whitespace (:+ ws-char)]
[comment (:or line-comment
#;inline-comment)]
)