| 1 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 2 |
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; This program is free software: you can redistribute it and/or modify |
| 5 |
;;; it under the terms of the GNU General Public License version 3 as |
| 6 |
;;; published by the Free Software Foundation. |
| 7 |
;;; |
| 8 |
;;; This program is distributed in the hope that it will be useful, but |
| 9 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;;; General Public License for more details. |
| 12 |
;;; |
| 13 |
;;; You should have received a copy of the GNU General Public License |
| 14 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
|
| 17 |
(library (ikarus.reader) |
| 18 |
(export read read-initial read-token comment-handler get-datum |
| 19 |
read-annotated read-script-annotated annotation? |
| 20 |
annotation-expression annotation-source |
| 21 |
annotation-stripped) |
| 22 |
(import |
| 23 |
(only (ikarus.string-to-number) define-string->number-parser) |
| 24 |
(ikarus system $chars) |
| 25 |
(ikarus system $fx) |
| 26 |
(ikarus system $pairs) |
| 27 |
(ikarus system $bytevectors) |
| 28 |
(only (ikarus.io) input-port-byte-position |
| 29 |
input-port-column-number) |
| 30 |
(except (ikarus) read-char read read-token comment-handler get-datum |
| 31 |
read-annotated read-script-annotated annotation? |
| 32 |
annotation-expression annotation-source annotation-stripped |
| 33 |
input-port-column-number)) |
| 34 |
|
| 35 |
(define (die/lex id pos who msg arg*) |
| 36 |
(raise |
| 37 |
(condition |
| 38 |
(make-lexical-violation) |
| 39 |
(make-message-condition msg) |
| 40 |
(if (null? arg*) |
| 41 |
(condition) |
| 42 |
(make-irritants-condition arg*)) |
| 43 |
(make-source-position-condition |
| 44 |
id pos)))) |
| 45 |
(define (die/pos p off who msg arg*) |
| 46 |
(die/lex (port-id p) |
| 47 |
(let ([pos (input-port-byte-position p)]) |
| 48 |
(and pos (+ pos off))) |
| 49 |
who msg arg*)) |
| 50 |
(define (die/p p who msg . arg*) |
| 51 |
(die/pos p 0 who msg arg*)) |
| 52 |
(define (die/p-1 p who msg . arg*) |
| 53 |
(die/pos p -1 who msg arg*)) |
| 54 |
(define (die/ann ann who msg . arg*) |
| 55 |
(let ([src (annotation-source ann)]) |
| 56 |
(die/lex (car src) (cdr src) who msg arg*))) |
| 57 |
|
| 58 |
|
| 59 |
(define (checked-integer->char n ac p) |
| 60 |
(define (valid-integer-char? n) |
| 61 |
(cond |
| 62 |
[(<= n #xD7FF) #t] |
| 63 |
[(< n #xE000) #f] |
| 64 |
[(<= n #x10FFFF) #t] |
| 65 |
[else #f])) |
| 66 |
(if (valid-integer-char? n) |
| 67 |
($fixnum->char n) |
| 68 |
(die/p p 'tokenize |
| 69 |
"invalid numeric value for character" |
| 70 |
(list->string (reverse ac))))) |
| 71 |
|
| 72 |
(define-syntax read-char |
| 73 |
(syntax-rules () |
| 74 |
[(_ p) (get-char p)])) |
| 75 |
|
| 76 |
(define delimiter? |
| 77 |
(lambda (c) |
| 78 |
(or (char-whitespace? c) |
| 79 |
(memq c '(#\( #\) #\[ #\] #\" #\# #\; #\{ #\} #\|))))) |
| 80 |
(define digit? |
| 81 |
(lambda (c) |
| 82 |
(and ($char<= #\0 c) ($char<= c #\9)))) |
| 83 |
(define char->num |
| 84 |
(lambda (c) |
| 85 |
(fx- ($char->fixnum c) ($char->fixnum #\0)))) |
| 86 |
(define initial? |
| 87 |
(lambda (c) |
| 88 |
(cond |
| 89 |
[($char<= c ($fixnum->char 127)) |
| 90 |
(or (letter? c) (special-initial? c))] |
| 91 |
[else (unicode-printable-char? c)]))) |
| 92 |
(define letter? |
| 93 |
(lambda (c) |
| 94 |
(or (and ($char<= #\a c) ($char<= c #\z)) |
| 95 |
(and ($char<= #\A c) ($char<= c #\Z))))) |
| 96 |
(define special-initial? |
| 97 |
(lambda (c) |
| 98 |
(memq c '(#\! #\$ #\% #\& #\* #\/ #\: #\< #\= #\> #\? #\^ #\_ #\~)))) |
| 99 |
(define special-subsequent? |
| 100 |
(lambda (c) |
| 101 |
(memq c '(#\+ #\- #\. #\@)))) |
| 102 |
(define subsequent? |
| 103 |
(lambda (c) |
| 104 |
(cond |
| 105 |
[($char<= c ($fixnum->char 127)) |
| 106 |
(or (letter? c) |
| 107 |
(digit? c) |
| 108 |
(special-initial? c) |
| 109 |
(special-subsequent? c))] |
| 110 |
[else |
| 111 |
(or (unicode-printable-char? c) |
| 112 |
(memq (char-general-category c) '(Nd Mc Me)))]))) |
| 113 |
(define tokenize-identifier |
| 114 |
(lambda (ls p) |
| 115 |
(let ([c (peek-char p)]) |
| 116 |
(cond |
| 117 |
[(eof-object? c) ls] |
| 118 |
[(subsequent? c) |
| 119 |
(read-char p) |
| 120 |
(tokenize-identifier (cons c ls) p)] |
| 121 |
[(delimiter? c) |
| 122 |
ls] |
| 123 |
[(char=? c #\\) |
| 124 |
(read-char p) |
| 125 |
(tokenize-backslash ls p)] |
| 126 |
[(eq? (port-mode p) 'r6rs-mode) |
| 127 |
(die/p p 'tokenize "invalid identifier syntax" |
| 128 |
(list->string (reverse (cons c ls))))] |
| 129 |
[else ls])))) |
| 130 |
(define (tokenize-string ls p) |
| 131 |
(let ([c (read-char p)]) |
| 132 |
(cond |
| 133 |
[(eof-object? c) |
| 134 |
(die/p p 'tokenize "invalid eof inside string")] |
| 135 |
[else (tokenize-string-char ls p c)]))) |
| 136 |
(define LF1 '(#\xA #\x85 #\x2028)) ;;; these are considered newlines |
| 137 |
(define LF2 '(#\xA #\x85)) ;;; these are not newlines if they |
| 138 |
;;; appear after CR |
| 139 |
(define (tokenize-string-char ls p c) |
| 140 |
(define (intraline-whitespace? c) |
| 141 |
(or (eqv? c #\x9) |
| 142 |
(eq? (char-general-category c) 'Zs))) |
| 143 |
(define (tokenize-string-continue ls p c) |
| 144 |
(cond |
| 145 |
[(eof-object? c) |
| 146 |
(die/p p 'tokenize "invalid eof inside string")] |
| 147 |
[(intraline-whitespace? c) |
| 148 |
(let f () |
| 149 |
(let ([c (read-char p)]) |
| 150 |
(cond |
| 151 |
[(eof-object? c) |
| 152 |
(die/p p 'tokenize "invalid eof inside string")] |
| 153 |
[(intraline-whitespace? c) (f)] |
| 154 |
[else (tokenize-string-char ls p c)])))] |
| 155 |
[else (tokenize-string-char ls p c)])) |
| 156 |
(cond |
| 157 |
[($char= #\" c) ls] |
| 158 |
[($char= #\\ c) |
| 159 |
(let ([c (read-char p)]) |
| 160 |
(cond |
| 161 |
[(eof-object? c) |
| 162 |
(die/p p 'tokenize "invalid eof after string escape")] |
| 163 |
[($char= #\a c) (tokenize-string (cons #\x7 ls) p)] |
| 164 |
[($char= #\b c) (tokenize-string (cons #\x8 ls) p)] |
| 165 |
[($char= #\t c) (tokenize-string (cons #\x9 ls) p)] |
| 166 |
[($char= #\n c) (tokenize-string (cons #\xA ls) p)] |
| 167 |
[($char= #\v c) (tokenize-string (cons #\xB ls) p)] |
| 168 |
[($char= #\f c) (tokenize-string (cons #\xC ls) p)] |
| 169 |
[($char= #\r c) (tokenize-string (cons #\xD ls) p)] |
| 170 |
[($char= #\" c) (tokenize-string (cons #\x22 ls) p)] |
| 171 |
[($char= #\\ c) (tokenize-string (cons #\x5C ls) p)] |
| 172 |
[($char= #\x c) ;;; unicode escape \xXXX; |
| 173 |
(let ([c (read-char p)]) |
| 174 |
(cond |
| 175 |
[(eof-object? c) |
| 176 |
(die/p p 'tokenize "invalid eof inside string")] |
| 177 |
[(hex c) => |
| 178 |
(lambda (n) |
| 179 |
(let f ([n n] [ac (cons c '(#\x))]) |
| 180 |
(let ([c (read-char p)]) |
| 181 |
(cond |
| 182 |
[(eof-object? n) |
| 183 |
(die/p p 'tokenize "invalid eof inside string")] |
| 184 |
[(hex c) => |
| 185 |
(lambda (v) (f (+ (* n 16) v) (cons c ac)))] |
| 186 |
[($char= c #\;) |
| 187 |
(tokenize-string |
| 188 |
(cons (checked-integer->char n ac p) ls) p)] |
| 189 |
[else |
| 190 |
(die/p-1 p 'tokenize |
| 191 |
"invalid char in escape sequence" |
| 192 |
(list->string (reverse (cons c ac))))]))))] |
| 193 |
[else |
| 194 |
(die/p-1 p 'tokenize |
| 195 |
"invalid char in escape sequence" c)]))] |
| 196 |
[(intraline-whitespace? c) |
| 197 |
(let f () |
| 198 |
(let ([c (read-char p)]) |
| 199 |
(cond |
| 200 |
[(eof-object? c) |
| 201 |
(die/p p 'tokenize "invalid eof inside string")] |
| 202 |
[(intraline-whitespace? c) (f)] |
| 203 |
[(memv c LF1) |
| 204 |
(tokenize-string-continue ls p (read-char p))] |
| 205 |
[(eqv? c #\return) |
| 206 |
(let ([c (read-char p)]) |
| 207 |
(cond |
| 208 |
[(memv c LF2) |
| 209 |
(tokenize-string-continue ls p (read-char p))] |
| 210 |
[else |
| 211 |
(tokenize-string-continue ls p c)]))] |
| 212 |
[else |
| 213 |
(die/p-1 p 'tokenize |
| 214 |
"non-whitespace character after escape")])))] |
| 215 |
[(memv c LF1) |
| 216 |
(tokenize-string-continue ls p (read-char p))] |
| 217 |
[(eqv? c #\return) |
| 218 |
(let ([c (read-char p)]) |
| 219 |
(cond |
| 220 |
[(memv c LF2) |
| 221 |
(tokenize-string-continue ls p (read-char p))] |
| 222 |
[else |
| 223 |
(tokenize-string-continue ls p c)]))] |
| 224 |
[else (die/p-1 p 'tokenize "invalid string escape" c)]))] |
| 225 |
[(memv c LF1) |
| 226 |
(tokenize-string (cons #\linefeed ls) p)] |
| 227 |
[(eqv? c #\return) |
| 228 |
(let ([c (peek-char p)]) |
| 229 |
(when (memv c LF2) (read-char p)) |
| 230 |
(tokenize-string (cons #\linefeed ls) p))] |
| 231 |
[else |
| 232 |
(tokenize-string (cons c ls) p)])) |
| 233 |
(define skip-comment |
| 234 |
(lambda (p) |
| 235 |
(let ([c (read-char p)]) |
| 236 |
(unless (or (eof-object? c) (memv c LF1) (eqv? c #\return)) |
| 237 |
(skip-comment p))))) |
| 238 |
(define tokenize-dot |
| 239 |
(lambda (p) |
| 240 |
(let ([c (peek-char p)]) |
| 241 |
(cond |
| 242 |
[(eof-object? c) 'dot] |
| 243 |
[(delimiter? c) 'dot] |
| 244 |
[($char= c #\.) ; this is second dot |
| 245 |
(read-char p) |
| 246 |
(let ([c (peek-char p)]) |
| 247 |
(cond |
| 248 |
[(eof-object? c) |
| 249 |
(die/p p 'tokenize "invalid syntax .. near end of file")] |
| 250 |
[($char= c #\.) ; this is the third |
| 251 |
(read-char p) |
| 252 |
(let ([c (peek-char p)]) |
| 253 |
(cond |
| 254 |
[(eof-object? c) '(datum . ...)] |
| 255 |
[(delimiter? c) '(datum . ...)] |
| 256 |
[else |
| 257 |
(die/p p 'tokenize "invalid syntax" |
| 258 |
(string-append "..." (string c)))]))] |
| 259 |
[else |
| 260 |
(die/p p 'tokenize "invalid syntax" |
| 261 |
(string-append ".." (string c)))]))] |
| 262 |
[else |
| 263 |
(cons 'datum |
| 264 |
(u:dot p '(#\.) 10 #f #f +1))])))) |
| 265 |
(define tokenize-char* |
| 266 |
(lambda (i str p d) |
| 267 |
(cond |
| 268 |
[(fx= i (string-length str)) |
| 269 |
(let ([c (peek-char p)]) |
| 270 |
(cond |
| 271 |
[(eof-object? c) d] |
| 272 |
[(delimiter? c) d] |
| 273 |
[else |
| 274 |
(die/p p 'tokenize "invalid character after sequence" |
| 275 |
(string-append str (string c)))]))] |
| 276 |
[else |
| 277 |
(let ([c (read-char p)]) |
| 278 |
(cond |
| 279 |
[(eof-object? c) |
| 280 |
(die/p p 'tokenize |
| 281 |
"invalid eof in the middle of expected sequence" str)] |
| 282 |
[($char= c (string-ref str i)) |
| 283 |
(tokenize-char* (fxadd1 i) str p d)] |
| 284 |
[else |
| 285 |
(die/p-1 p 'tokenize |
| 286 |
"invalid char while scanning string" |
| 287 |
c str)]))]))) |
| 288 |
(define tokenize-char-seq |
| 289 |
(lambda (p str d) |
| 290 |
(let ([c (peek-char p)]) |
| 291 |
(cond |
| 292 |
[(eof-object? c) (cons 'datum (string-ref str 0))] |
| 293 |
[(delimiter? c) (cons 'datum (string-ref str 0))] |
| 294 |
[($char= (string-ref str 1) c) |
| 295 |
(read-char p) |
| 296 |
(tokenize-char* 2 str p d)] |
| 297 |
[else |
| 298 |
(die/p p 'tokenize "invalid syntax" |
| 299 |
(string-ref str 0) c)])))) |
| 300 |
(define tokenize-char |
| 301 |
(lambda (p) |
| 302 |
(let ([c (read-char p)]) |
| 303 |
(cond |
| 304 |
[(eof-object? c) |
| 305 |
(die/p p 'tokenize "invalid #\\ near end of file")] |
| 306 |
[(eqv? #\n c) |
| 307 |
(let ([c (peek-char p)]) |
| 308 |
(cond |
| 309 |
[(eof-object? c) |
| 310 |
(read-char p) |
| 311 |
'(datum . #\n)] |
| 312 |
[(eqv? #\u c) |
| 313 |
(read-char p) |
| 314 |
(tokenize-char-seq p "ul" '(datum . #\x0))] |
| 315 |
[(eqv? #\e c) |
| 316 |
(read-char p) |
| 317 |
(tokenize-char-seq p "ewline" '(datum . #\xA))] |
| 318 |
[(delimiter? c) |
| 319 |
'(datum . #\n)] |
| 320 |
[else |
| 321 |
(die/p p 'tokenize "invalid syntax" |
| 322 |
(string #\# #\\ #\n c))]))] |
| 323 |
[(eqv? #\a c) |
| 324 |
(tokenize-char-seq p "alarm" '(datum . #\x7))] |
| 325 |
[(eqv? #\b c) |
| 326 |
(tokenize-char-seq p "backspace" '(datum . #\x8))] |
| 327 |
[(eqv? #\t c) |
| 328 |
(tokenize-char-seq p "tab" '(datum . #\x9))] |
| 329 |
[(eqv? #\l c) |
| 330 |
(tokenize-char-seq p "linefeed" '(datum . #\xA))] |
| 331 |
[(eqv? #\v c) |
| 332 |
(tokenize-char-seq p "vtab" '(datum . #\xB))] |
| 333 |
[(eqv? #\p c) |
| 334 |
(tokenize-char-seq p "page" '(datum . #\xC))] |
| 335 |
[(eqv? #\r c) |
| 336 |
(tokenize-char-seq p "return" '(datum . #\xD))] |
| 337 |
[(eqv? #\e c) |
| 338 |
(tokenize-char-seq p "esc" '(datum . #\x1B))] |
| 339 |
[(eqv? #\s c) |
| 340 |
(tokenize-char-seq p "space" '(datum . #\x20))] |
| 341 |
[(eqv? #\d c) |
| 342 |
(tokenize-char-seq p "delete" '(datum . #\x7F))] |
| 343 |
[(eqv? #\x c) |
| 344 |
(let ([n (peek-char p)]) |
| 345 |
(cond |
| 346 |
[(or (eof-object? n) (delimiter? n)) |
| 347 |
'(datum . #\x)] |
| 348 |
[(hex n) => |
| 349 |
(lambda (v) |
| 350 |
(read-char p) |
| 351 |
(let f ([v v] [ac (cons n '(#\x))]) |
| 352 |
(let ([c (peek-char p)]) |
| 353 |
(cond |
| 354 |
[(eof-object? c) |
| 355 |
(cons 'datum (checked-integer->char v ac p))] |
| 356 |
[(delimiter? c) |
| 357 |
(cons 'datum (checked-integer->char v ac p))] |
| 358 |
[(hex c) => |
| 359 |
(lambda (v0) |
| 360 |
(read-char p) |
| 361 |
(f (+ (* v 16) v0) (cons c ac)))] |
| 362 |
[else |
| 363 |
(die/p p 'tokenize |
| 364 |
"invalid character sequence" |
| 365 |
(list->string (reverse (cons c ac))))]))))] |
| 366 |
[else |
| 367 |
(die/p p 'tokenize "invalid character sequence" |
| 368 |
(string-append "#\\" (string n)))]))] |
| 369 |
[else |
| 370 |
(let ([n (peek-char p)]) |
| 371 |
(cond |
| 372 |
[(eof-object? n) (cons 'datum c)] |
| 373 |
[(delimiter? n) (cons 'datum c)] |
| 374 |
[else |
| 375 |
(die/p p 'tokenize "invalid syntax" |
| 376 |
(string-append "#\\" (string c n)))]))])))) |
| 377 |
(define (hex x) |
| 378 |
(cond |
| 379 |
[(and ($char<= #\0 x) ($char<= x #\9)) |
| 380 |
($fx- ($char->fixnum x) ($char->fixnum #\0))] |
| 381 |
[(and ($char<= #\a x) ($char<= x #\f)) |
| 382 |
($fx- ($char->fixnum x) |
| 383 |
($fx- ($char->fixnum #\a) 10))] |
| 384 |
[(and ($char<= #\A x) ($char<= x #\F)) |
| 385 |
($fx- ($char->fixnum x) |
| 386 |
($fx- ($char->fixnum #\A) 10))] |
| 387 |
[else #f])) |
| 388 |
(define multiline-error |
| 389 |
(lambda (p) |
| 390 |
(die/p p 'tokenize |
| 391 |
"end of file encountered while inside a #|-style comment"))) |
| 392 |
(define apprev |
| 393 |
(lambda (str i ac) |
| 394 |
(cond |
| 395 |
[(fx= i (string-length str)) ac] |
| 396 |
[else |
| 397 |
(apprev str (fx+ i 1) (cons (string-ref str i) ac))]))) |
| 398 |
(define multiline-comment |
| 399 |
(lambda (p) |
| 400 |
(define f |
| 401 |
(lambda (p ac) |
| 402 |
(let ([c (read-char p)]) |
| 403 |
(cond |
| 404 |
[(eof-object? c) (multiline-error p)] |
| 405 |
[($char= #\| c) |
| 406 |
(let g ([c (read-char p)] [ac ac]) |
| 407 |
(cond |
| 408 |
[(eof-object? c) (multiline-error p)] |
| 409 |
[($char= #\# c) ac] |
| 410 |
[($char= #\| c) |
| 411 |
(g (read-char p) (cons c ac))] |
| 412 |
[else (f p (cons c ac))]))] |
| 413 |
[($char= #\# c) |
| 414 |
(let ([c (read-char p)]) |
| 415 |
(cond |
| 416 |
[(eof-object? c) (multiline-error p)] |
| 417 |
[($char= #\| c) |
| 418 |
(let ([v (multiline-comment p)]) |
| 419 |
(if (string? v) |
| 420 |
(f p (apprev v 0 ac)) |
| 421 |
(f p ac)))] |
| 422 |
[else |
| 423 |
(f p (cons c (cons #\# ac)))]))] |
| 424 |
[else (f p (cons c ac))])))) |
| 425 |
(let ([ac (f p '())]) |
| 426 |
((comment-handler) |
| 427 |
(list->string (reverse ac)))))) |
| 428 |
(define tokenize-hash |
| 429 |
(lambda (p) |
| 430 |
(tokenize-hash/c (read-char p) p))) |
| 431 |
(define (skip-whitespace p caller) |
| 432 |
(let ([c (read-char p)]) |
| 433 |
(cond |
| 434 |
[(eof-object? c) |
| 435 |
(die/p p 'tokenize "invalid eof inside" caller)] |
| 436 |
[(char-whitespace? c) |
| 437 |
(skip-whitespace p caller)] |
| 438 |
[else c]))) |
| 439 |
(define tokenize-hash/c |
| 440 |
(lambda (c p) |
| 441 |
(cond |
| 442 |
[(eof-object? c) (die/p p 'tokenize "invalid # near end of file")] |
| 443 |
[(memq c '(#\t #\T)) |
| 444 |
(let ([c1 (peek-char p)]) |
| 445 |
(cond |
| 446 |
[(eof-object? c1) '(datum . #t)] |
| 447 |
[(delimiter? c1) '(datum . #t)] |
| 448 |
[else (die/p p 'tokenize |
| 449 |
(format "invalid syntax near #~a~a" c c1))]))] |
| 450 |
[(memq c '(#\f #\F)) |
| 451 |
(let ([c1 (peek-char p)]) |
| 452 |
(cond |
| 453 |
[(eof-object? c1) '(datum . #f)] |
| 454 |
[(delimiter? c1) '(datum . #f)] |
| 455 |
[else (die/p p 'tokenize |
| 456 |
(format "invalid syntax near #~a~a" c c1))]))] |
| 457 |
[($char= #\\ c) (tokenize-char p)] |
| 458 |
[($char= #\( c) 'vparen] |
| 459 |
[($char= #\' c) '(macro . syntax)] |
| 460 |
[($char= #\` c) '(macro . quasisyntax)] |
| 461 |
[($char= #\, c) |
| 462 |
(let ([c (peek-char p)]) |
| 463 |
(cond |
| 464 |
[(eqv? c #\@) (read-char p) |
| 465 |
'(macro . unsyntax-splicing)] |
| 466 |
[else '(macro . unsyntax)]))] |
| 467 |
[($char= #\! c) |
| 468 |
(let ([e (read-char p)]) |
| 469 |
(when (eof-object? e) |
| 470 |
(die/p p 'tokenize "invalid eof near #!")) |
| 471 |
(case e |
| 472 |
[(#\e) |
| 473 |
(when (eq? (port-mode p) 'r6rs-mode) |
| 474 |
(die/p-1 p 'tokenize "invalid syntax: #!e")) |
| 475 |
(read-char* p '(#\e) "of" "eof sequence" #f #f) |
| 476 |
(cons 'datum (eof-object))] |
| 477 |
[(#\r) |
| 478 |
(read-char* p '(#\r) "6rs" "#!r6rs comment" #f #f) |
| 479 |
(set-port-mode! p 'r6rs-mode) |
| 480 |
(tokenize/1 p)] |
| 481 |
[(#\i) |
| 482 |
(read-char* p '(#\i) "karus" "#!ikarus comment" #f #f) |
| 483 |
(set-port-mode! p 'ikarus-mode) |
| 484 |
(tokenize/1 p)] |
| 485 |
[else |
| 486 |
(die/p-1 p 'tokenize |
| 487 |
(format "invalid syntax near #!~a" e))]))] |
| 488 |
[(digit? c) |
| 489 |
(when (eq? (port-mode p) 'r6rs-mode) |
| 490 |
(die/p-1 p 'tokenize "graph syntax is invalid in #!r6rs mode" |
| 491 |
(format "#~a" c))) |
| 492 |
(tokenize-hashnum p (char->num c))] |
| 493 |
[($char= #\: c) |
| 494 |
(when (eq? (port-mode p) 'r6rs-mode) |
| 495 |
(die/p-1 p 'tokenize "gensym syntax is invalid in #!r6rs mode" |
| 496 |
(format "#~a" c))) |
| 497 |
(let* ([c (skip-whitespace p "gensym")] |
| 498 |
[id0 |
| 499 |
(cond |
| 500 |
[(initial? c) |
| 501 |
(list->string |
| 502 |
(reverse (tokenize-identifier (cons c '()) p)))] |
| 503 |
[($char= #\| c) |
| 504 |
(list->string |
| 505 |
(reverse (tokenize-bar p '())))] |
| 506 |
[else |
| 507 |
(die/p-1 p 'tokenize |
| 508 |
"invalid char inside gensym" c)])]) |
| 509 |
(cons 'datum (gensym id0)))] |
| 510 |
[($char= #\{ c) |
| 511 |
(when (eq? (port-mode p) 'r6rs-mode) |
| 512 |
(die/p-1 p 'tokenize "gensym syntax is invalid in #!r6rs mode" |
| 513 |
(format "#~a" c))) |
| 514 |
(let* ([c (skip-whitespace p "gensym")] |
| 515 |
[id0 |
| 516 |
(cond |
| 517 |
[(initial? c) |
| 518 |
(list->string |
| 519 |
(reverse (tokenize-identifier (cons c '()) p)))] |
| 520 |
[($char= #\| c) |
| 521 |
(list->string |
| 522 |
(reverse (tokenize-bar p '())))] |
| 523 |
[else |
| 524 |
(die/p-1 p 'tokenize |
| 525 |
"invalid char inside gensym" c)])] |
| 526 |
[c (skip-whitespace p "gensym")]) |
| 527 |
(cond |
| 528 |
[($char= #\} c) |
| 529 |
(cons 'datum |
| 530 |
(foreign-call "ikrt_strings_to_gensym" #f id0))] |
| 531 |
[else |
| 532 |
(let ([id1 |
| 533 |
(cond |
| 534 |
[(initial? c) |
| 535 |
(list->string |
| 536 |
(reverse |
| 537 |
(tokenize-identifier |
| 538 |
(cons c '()) p)))] |
| 539 |
[($char= #\| c) |
| 540 |
(list->string |
| 541 |
(reverse (tokenize-bar p '())))] |
| 542 |
[else |
| 543 |
(die/p-1 p 'tokenize |
| 544 |
"invalid char inside gensym" c)])]) |
| 545 |
(let ([c (skip-whitespace p "gensym")]) |
| 546 |
(cond |
| 547 |
[($char= #\} c) |
| 548 |
(cons 'datum |
| 549 |
(foreign-call "ikrt_strings_to_gensym" |
| 550 |
id0 id1))] |
| 551 |
[else |
| 552 |
(die/p-1 p 'tokenize |
| 553 |
"invalid char inside gensym" c)])))]))] |
| 554 |
[($char= #\v c) |
| 555 |
(let ([c (read-char p)]) |
| 556 |
(cond |
| 557 |
[($char= #\u c) |
| 558 |
(let ([c (read-char p)]) |
| 559 |
(cond |
| 560 |
[($char= c #\8) |
| 561 |
(let ([c (read-char p)]) |
| 562 |
(cond |
| 563 |
[($char= c #\() 'vu8] |
| 564 |
[(eof-object? c) |
| 565 |
(die/p p 'tokenize "invalid eof object after #vu8")] |
| 566 |
[else (die/p-1 p 'tokenize |
| 567 |
(format "invalid sequence #vu8~a" c))]))] |
| 568 |
[(eof-object? c) |
| 569 |
(die/p p 'tokenize "invalid eof object after #vu")] |
| 570 |
[else (die/p-1 p 'tokenize |
| 571 |
(format "invalid sequence #vu~a" c))]))] |
| 572 |
[(eof-object? c) |
| 573 |
(die/p p 'tokenize "invalid eof object after #v")] |
| 574 |
[else (die/p p 'tokenize |
| 575 |
(format "invalid sequence #v~a" c))]))] |
| 576 |
[(memq c '(#\e #\E)) |
| 577 |
(cons 'datum (parse-string p (list c #\#) 10 #f 'e))] |
| 578 |
[(memq c '(#\i #\I)) |
| 579 |
(cons 'datum (parse-string p (list c #\#) 10 #f 'i))] |
| 580 |
[(memq c '(#\b #\B)) |
| 581 |
(cons 'datum (parse-string p (list c #\#) 2 2 #f))] |
| 582 |
[(memq c '(#\x #\X)) |
| 583 |
(cons 'datum (parse-string p (list c #\#) 16 16 #f))] |
| 584 |
[(memq c '(#\o #\O)) |
| 585 |
(cons 'datum (parse-string p (list c #\#) 8 8 #f))] |
| 586 |
[(memq c '(#\d #\D)) |
| 587 |
(cons 'datum (parse-string p (list c #\#) 10 10 #f))] |
| 588 |
;[($char= #\@ c) DEAD: Unfixable due to port encoding |
| 589 |
; that does not allow mixing binary and |
| 590 |
; textual data in the same port. |
| 591 |
; Left here for historical value |
| 592 |
; (when (eq? (port-mode p) 'r6rs-mode) |
| 593 |
; (die/p-1 p 'tokenize "fasl syntax is invalid in #!r6rs mode" |
| 594 |
; (format "#~a" c))) |
| 595 |
; (die/p-1 p 'read "FIXME: fasl read disabled") |
| 596 |
; '(cons 'datum ($fasl-read p))] |
| 597 |
[else |
| 598 |
(die/p-1 p 'tokenize |
| 599 |
(format "invalid syntax #~a" c))]))) |
| 600 |
|
| 601 |
(define (num-error p str ls) |
| 602 |
(die/p-1 p 'read str |
| 603 |
(list->string (reverse ls)))) |
| 604 |
|
| 605 |
(define-syntax port-config |
| 606 |
(syntax-rules (GEN-TEST GEN-ARGS FAIL EOF-ERROR GEN-DELIM-TEST) |
| 607 |
[(_ GEN-ARGS k . rest) (k (p ac) . rest)] |
| 608 |
[(_ FAIL (p ac)) |
| 609 |
(num-error p "invalid numeric sequence" ac)] |
| 610 |
[(_ FAIL (p ac) c) |
| 611 |
(num-error p "invalid numeric sequence" (cons c ac))] |
| 612 |
[(_ EOF-ERROR (p ac)) |
| 613 |
(num-error p "invalid eof while reading number" ac)] |
| 614 |
[(_ GEN-DELIM-TEST c sk fk) |
| 615 |
(if (delimiter? c) sk fk)] |
| 616 |
[(_ GEN-TEST var next fail (p ac) eof-case char-case) |
| 617 |
(let ([c (peek-char p)]) |
| 618 |
(if (eof-object? c) |
| 619 |
(let () |
| 620 |
(define-syntax fail |
| 621 |
(syntax-rules () |
| 622 |
[(_) (num-error p "invalid numeric sequence" ac)])) |
| 623 |
eof-case) |
| 624 |
(let ([var c]) |
| 625 |
(define-syntax fail |
| 626 |
(syntax-rules () |
| 627 |
[(_) |
| 628 |
(num-error p "invalid numeric sequence" |
| 629 |
(cons var ac))])) |
| 630 |
(define-syntax next |
| 631 |
(syntax-rules () |
| 632 |
[(_ who args (... ...)) |
| 633 |
(who p (cons (get-char p) ac) args (... ...))])) |
| 634 |
char-case)))])) |
| 635 |
|
| 636 |
(define-string->number-parser port-config |
| 637 |
(parse-string u:digit+ u:sign u:dot)) |
| 638 |
|
| 639 |
(define (read-char* p ls str who ci? delimited?) |
| 640 |
(let f ([i 0] [ls ls]) |
| 641 |
(cond |
| 642 |
[(fx= i (string-length str)) |
| 643 |
(when delimited? |
| 644 |
(let ([c (peek-char p)]) |
| 645 |
(when (and (not (eof-object? c)) (not (delimiter? c))) |
| 646 |
(die/p p 'tokenize |
| 647 |
(format "invalid ~a: ~s" who |
| 648 |
(list->string (reverse (cons c ls))))))))] |
| 649 |
[else |
| 650 |
(let ([c (read-char p)]) |
| 651 |
(cond |
| 652 |
[(eof-object? c) |
| 653 |
(die/p p 'tokenize |
| 654 |
(format "invalid eof inside ~a" who))] |
| 655 |
[(or (and (not ci?) (char=? c (string-ref str i))) |
| 656 |
(and ci? (char=? (char-downcase c) (string-ref str i)))) |
| 657 |
(f (add1 i) (cons c ls))] |
| 658 |
[else |
| 659 |
(die/p-1 p 'tokenize |
| 660 |
(format "invalid ~a: ~s" who |
| 661 |
(list->string (reverse (cons c ls)))))]))]))) |
| 662 |
(define (tokenize-hashnum p n) |
| 663 |
(let ([c (read-char p)]) |
| 664 |
(cond |
| 665 |
[(eof-object? c) |
| 666 |
(die/p p 'tokenize "invalid eof inside #n mark/ref")] |
| 667 |
[($char= #\= c) (cons 'mark n)] |
| 668 |
[($char= #\# c) (cons 'ref n)] |
| 669 |
[(digit? c) |
| 670 |
(tokenize-hashnum p (fx+ (fx* n 10) (char->num c)))] |
| 671 |
[else |
| 672 |
(die/p-1 p 'tokenize "invalid char while inside a #n mark/ref" c)]))) |
| 673 |
(define tokenize-bar |
| 674 |
(lambda (p ac) |
| 675 |
(let ([c (read-char p)]) |
| 676 |
(cond |
| 677 |
[(eof-object? c) |
| 678 |
(die/p p 'tokenize "unexpected eof while reading symbol")] |
| 679 |
[($char= #\\ c) |
| 680 |
(let ([c (read-char p)]) |
| 681 |
(cond |
| 682 |
[(eof-object? c) |
| 683 |
(die/p p 'tokenize "unexpected eof while reading symbol")] |
| 684 |
[else (tokenize-bar p (cons c ac))]))] |
| 685 |
[($char= #\| c) ac] |
| 686 |
[else (tokenize-bar p (cons c ac))])))) |
| 687 |
(define (tokenize-backslash main-ac p) |
| 688 |
(let ([c (read-char p)]) |
| 689 |
(cond |
| 690 |
[(eof-object? c) |
| 691 |
(die/p p 'tokenize "invalid eof after symbol escape")] |
| 692 |
[($char= #\x c) |
| 693 |
(let ([c (read-char p)]) |
| 694 |
(cond |
| 695 |
[(eof-object? c) |
| 696 |
(die/p p 'tokenize "invalid eof after \\x")] |
| 697 |
[(hex c) => |
| 698 |
(lambda (v) |
| 699 |
(let f ([v v] [ac `(,c #\x #\\)]) |
| 700 |
(let ([c (read-char p)]) |
| 701 |
(cond |
| 702 |
[(eof-object? c) |
| 703 |
(die/p p 'tokenize |
| 704 |
(format "invalid eof after ~a" |
| 705 |
(list->string (reverse ac))))] |
| 706 |
[($char= #\; c) |
| 707 |
(tokenize-identifier |
| 708 |
(cons (checked-integer->char v ac p) main-ac) |
| 709 |
p)] |
| 710 |
[(hex c) => |
| 711 |
(lambda (v0) |
| 712 |
(f (+ (* v 16) v0) (cons c ac)))] |
| 713 |
[else |
| 714 |
(die/p-1 p 'tokenize "invalid sequence" |
| 715 |
(list->string (cons c (reverse ac))))]))))] |
| 716 |
[else |
| 717 |
(die/p-1 p 'tokenize |
| 718 |
(format "invalid sequence \\x~a" c))]))] |
| 719 |
[else |
| 720 |
(die/p-1 p 'tokenize |
| 721 |
(format "invalid sequence \\~a" c))]))) |
| 722 |
(define tokenize/c |
| 723 |
(lambda (c p) |
| 724 |
(cond |
| 725 |
[(eof-object? c) |
| 726 |
(error 'tokenize/c "hmmmm eof") |
| 727 |
(eof-object)] |
| 728 |
[($char= #\( c) 'lparen] |
| 729 |
[($char= #\) c) 'rparen] |
| 730 |
[($char= #\[ c) 'lbrack] |
| 731 |
[($char= #\] c) 'rbrack] |
| 732 |
[($char= #\' c) '(macro . quote)] |
| 733 |
[($char= #\` c) '(macro . quasiquote)] |
| 734 |
[($char= #\, c) |
| 735 |
(let ([c (peek-char p)]) |
| 736 |
(cond |
| 737 |
[(eof-object? c) '(macro . unquote)] |
| 738 |
[($char= c #\@) |
| 739 |
(read-char p) |
| 740 |
'(macro . unquote-splicing)] |
| 741 |
[else '(macro . unquote)]))] |
| 742 |
[($char= #\# c) (tokenize-hash p)] |
| 743 |
[(char<=? #\0 c #\9) |
| 744 |
(let ([d (fx- (char->integer c) (char->integer #\0))]) |
| 745 |
(cons 'datum |
| 746 |
(u:digit+ p (list c) 10 #f #f +1 d)))] |
| 747 |
[(initial? c) |
| 748 |
(let ([ls (reverse (tokenize-identifier (cons c '()) p))]) |
| 749 |
(cons 'datum (string->symbol (list->string ls))))] |
| 750 |
[($char= #\" c) |
| 751 |
(let ([ls (tokenize-string '() p)]) |
| 752 |
(cons 'datum (list->string (reverse ls))))] |
| 753 |
[(memq c '(#\+)) |
| 754 |
(let ([c (peek-char p)]) |
| 755 |
(cond |
| 756 |
[(eof-object? c) '(datum . +)] |
| 757 |
[(delimiter? c) '(datum . +)] |
| 758 |
[else |
| 759 |
(cons 'datum |
| 760 |
(u:sign p '(#\+) 10 #f #f +1))]))] |
| 761 |
[(memq c '(#\-)) |
| 762 |
(let ([c (peek-char p)]) |
| 763 |
(cond |
| 764 |
[(eof-object? c) '(datum . -)] |
| 765 |
[(delimiter? c) '(datum . -)] |
| 766 |
[($char= c #\>) |
| 767 |
(read-char p) |
| 768 |
(let ([ls (tokenize-identifier '() p)]) |
| 769 |
(let ([str (list->string (cons* #\- #\> (reverse ls)))]) |
| 770 |
(cons 'datum (string->symbol str))))] |
| 771 |
[else |
| 772 |
(cons 'datum |
| 773 |
(u:sign p '(#\-) 10 #f #f -1))]))] |
| 774 |
[($char= #\. c) |
| 775 |
(tokenize-dot p)] |
| 776 |
[($char= #\| c) |
| 777 |
(when (eq? (port-mode p) 'r6rs-mode) |
| 778 |
(die/p p 'tokenize "|symbol| syntax is invalid in #!r6rs mode")) |
| 779 |
(let ([ls (reverse (tokenize-bar p '()))]) |
| 780 |
(cons 'datum (string->symbol (list->string ls))))] |
| 781 |
[($char= #\\ c) |
| 782 |
(cons 'datum |
| 783 |
(string->symbol |
| 784 |
(list->string |
| 785 |
(reverse (tokenize-backslash '() p)))))] |
| 786 |
;[($char= #\{ c) 'lbrace] |
| 787 |
[($char= #\@ c) |
| 788 |
(when (eq? (port-mode p) 'r6rs-mode) |
| 789 |
(die/p p 'tokenize "@-expr syntax is invalid in #!r6rs mode")) |
| 790 |
'at-expr] |
| 791 |
[else |
| 792 |
(die/p-1 p 'tokenize "invalid syntax" c)]))) |
| 793 |
|
| 794 |
(define tokenize/1 |
| 795 |
(lambda (p) |
| 796 |
(let ([c (read-char p)]) |
| 797 |
(cond |
| 798 |
[(eof-object? c) (eof-object)] |
| 799 |
[(eqv? c #\;) |
| 800 |
(skip-comment p) |
| 801 |
(tokenize/1 p)] |
| 802 |
[(eqv? c #\#) |
| 803 |
(let ([c (read-char p)]) |
| 804 |
(cond |
| 805 |
[(eof-object? c) |
| 806 |
(die/p p 'tokenize "invalid eof after #")] |
| 807 |
[(eqv? c #\;) |
| 808 |
(read-as-comment p) |
| 809 |
(tokenize/1 p)] |
| 810 |
[(eqv? c #\|) |
| 811 |
(multiline-comment p) |
| 812 |
(tokenize/1 p)] |
| 813 |
[else |
| 814 |
(tokenize-hash/c c p)]))] |
| 815 |
[(char-whitespace? c) (tokenize/1 p)] |
| 816 |
[else (tokenize/c c p)])))) |
| 817 |
|
| 818 |
(define tokenize/1+pos |
| 819 |
(lambda (p) |
| 820 |
(let ([pos (input-port-byte-position p)]) |
| 821 |
(let ([c (read-char p)]) |
| 822 |
(cond |
| 823 |
[(eof-object? c) (values (eof-object) pos)] |
| 824 |
[(eqv? c #\;) |
| 825 |
(skip-comment p) |
| 826 |
(tokenize/1+pos p)] |
| 827 |
[(eqv? c #\#) |
| 828 |
(let ([pos (input-port-byte-position p)]) |
| 829 |
(let ([c (read-char p)]) |
| 830 |
(cond |
| 831 |
[(eof-object? c) |
| 832 |
(die/p p 'tokenize "invalid eof after #")] |
| 833 |
[(eqv? c #\;) |
| 834 |
(read-as-comment p) |
| 835 |
(tokenize/1+pos p)] |
| 836 |
[(eqv? c #\|) |
| 837 |
(multiline-comment p) |
| 838 |
(tokenize/1+pos p)] |
| 839 |
[else |
| 840 |
(values (tokenize-hash/c c p) pos)])))] |
| 841 |
[(char-whitespace? c) (tokenize/1+pos p)] |
| 842 |
[else |
| 843 |
(values (tokenize/c c p) pos)]))))) |
| 844 |
|
| 845 |
(define tokenize-script-initial |
| 846 |
(lambda (p) |
| 847 |
(let ([c (read-char p)]) |
| 848 |
(cond |
| 849 |
[(eof-object? c) c] |
| 850 |
[(eqv? c #\;) |
| 851 |
(skip-comment p) |
| 852 |
(tokenize/1 p)] |
| 853 |
[(eqv? c #\#) |
| 854 |
(let ([c (read-char p)]) |
| 855 |
(cond |
| 856 |
[(eof-object? c) |
| 857 |
(die/p p 'tokenize "invalid eof after #")] |
| 858 |
[(eqv? c #\!) |
| 859 |
(skip-comment p) |
| 860 |
(tokenize/1 p)] |
| 861 |
[(eqv? c #\;) |
| 862 |
(read-as-comment p) |
| 863 |
(tokenize/1 p)] |
| 864 |
[(eqv? c #\|) |
| 865 |
(multiline-comment p) |
| 866 |
(tokenize/1 p)] |
| 867 |
[else |
| 868 |
(tokenize-hash/c c p)]))] |
| 869 |
[(char-whitespace? c) (tokenize/1 p)] |
| 870 |
[else (tokenize/c c p)])))) |
| 871 |
|
| 872 |
(define tokenize-script-initial+pos |
| 873 |
(lambda (p) |
| 874 |
(let ([pos (input-port-byte-position p)]) |
| 875 |
(let ([c (read-char p)]) |
| 876 |
(cond |
| 877 |
[(eof-object? c) (values (eof-object) pos)] |
| 878 |
[(eqv? c #\;) |
| 879 |
(skip-comment p) |
| 880 |
(tokenize/1+pos p)] |
| 881 |
[(eqv? c #\#) |
| 882 |
(let ([pos (input-port-byte-position p)]) |
| 883 |
(let ([c (read-char p)]) |
| 884 |
(cond |
| 885 |
[(eof-object? c) |
| 886 |
(die/p p 'tokenize "invalid eof after #")] |
| 887 |
[(eqv? c #\!) |
| 888 |
(skip-comment p) |
| 889 |
(tokenize/1+pos p)] |
| 890 |
[(eqv? c #\;) |
| 891 |
(read-as-comment p) |
| 892 |
(tokenize/1+pos p)] |
| 893 |
[(eqv? c #\|) |
| 894 |
(multiline-comment p) |
| 895 |
(tokenize/1+pos p)] |
| 896 |
[else |
| 897 |
(values (tokenize-hash/c c p) pos)])))] |
| 898 |
[(char-whitespace? c) (tokenize/1+pos p)] |
| 899 |
[else (values (tokenize/c c p) pos)]))))) |
| 900 |
|
| 901 |
(define-struct loc (value value^ set?)) |
| 902 |
|
| 903 |
;;; this is reverse engineered from psyntax.ss |
| 904 |
(define-struct annotation (expression source stripped)) |
| 905 |
;;; - source is a pair of file-name x char-position |
| 906 |
;;; - stripped is an s-expression with no annotations |
| 907 |
;;; - expression is a list/vector/id/whathaveyou that |
| 908 |
;;; may contain further annotations. |
| 909 |
|
| 910 |
|
| 911 |
(module (read-expr read-expr-script-initial) |
| 912 |
(define-syntax tokenize/1 syntax-error) |
| 913 |
(define (annotate-simple datum pos p) |
| 914 |
(make-annotation datum (cons (port-id p) pos) datum)) |
| 915 |
(define (annotate stripped expression pos p) |
| 916 |
(make-annotation expression (cons (port-id p) pos) stripped)) |
| 917 |
(define read-list |
| 918 |
(lambda (p locs k end mis init?) |
| 919 |
(let-values ([(t pos) (tokenize/1+pos p)]) |
| 920 |
(cond |
| 921 |
[(eof-object? t) |
| 922 |
(die/p p 'read "end of file encountered while reading list")] |
| 923 |
[(eq? t end) (values '() '() locs k)] |
| 924 |
[(eq? t mis) |
| 925 |
(die/p-1 p 'read "paren mismatch")] |
| 926 |
[(eq? t 'dot) |
| 927 |
(when init? |
| 928 |
(die/p-1 p 'read "invalid dot while reading list")) |
| 929 |
(let-values ([(d d^ locs k) (read-expr p locs k)]) |
| 930 |
(let-values ([(t pos^) (tokenize/1+pos p)]) |
| 931 |
(cond |
| 932 |
[(eq? t end) (values d d^ locs k)] |
| 933 |
[(eq? t mis) |
| 934 |
(die/p-1 p 'read "paren mismatch")] |
| 935 |
[(eq? t 'dot) |
| 936 |
(die/p-1 p 'read "cannot have two dots in a list")] |
| 937 |
[else |
| 938 |
(die/p-1 p 'read |
| 939 |
(format "expecting ~a, got ~a" end t))])))] |
| 940 |
[else |
| 941 |
(let-values ([(a a^ locs k) (parse-token p locs k t pos)]) |
| 942 |
(let-values ([(d d^ locs k) (read-list p locs k end mis #f)]) |
| 943 |
(let ([x (cons a d)] [x^ (cons a^ d^)]) |
| 944 |
(values x x^ locs (extend-k-pair x x^ a d k)))))])))) |
| 945 |
(define extend-k-pair |
| 946 |
(lambda (x x^ a d k) |
| 947 |
(cond |
| 948 |
[(or (loc? a) (loc? d)) |
| 949 |
(lambda () |
| 950 |
(let ([a (car x)]) |
| 951 |
(when (loc? a) |
| 952 |
(set-car! x (loc-value a)) |
| 953 |
(set-car! x^ (loc-value^ a)))) |
| 954 |
(let ([d (cdr x)]) |
| 955 |
(when (loc? d) |
| 956 |
(set-cdr! x (loc-value d)) |
| 957 |
(set-cdr! x^ (loc-value^ d)))) |
| 958 |
(k))] |
| 959 |
[else k]))) |
| 960 |
(define vector-put |
| 961 |
(lambda (v v^ k i ls ls^) |
| 962 |
(cond |
| 963 |
[(null? ls) k] |
| 964 |
[else |
| 965 |
(let ([a (car ls)]) |
| 966 |
(vector-set! v i a) |
| 967 |
(vector-set! v^ i (car ls^)) |
| 968 |
(vector-put v v^ |
| 969 |
(if (loc? a) |
| 970 |
(lambda () |
| 971 |
(vector-set! v i (loc-value a)) |
| 972 |
(vector-set! v^ i (loc-value^ a)) |
| 973 |
(k)) |
| 974 |
k) |
| 975 |
(fxsub1 i) |
| 976 |
(cdr ls) |
| 977 |
(cdr ls^)))]))) |
| 978 |
(define read-vector |
| 979 |
(lambda (p locs k count ls ls^) |
| 980 |
(let-values ([(t pos) (tokenize/1+pos p)]) |
| 981 |
(cond |
| 982 |
[(eof-object? t) |
| 983 |
(die/p p 'read "end of file encountered while reading a vector")] |
| 984 |
[(eq? t 'rparen) |
| 985 |
(let ([v (make-vector count)] [v^ (make-vector count)]) |
| 986 |
(let ([k (vector-put v v^ k (fxsub1 count) ls ls^)]) |
| 987 |
(values v v^ locs k)))] |
| 988 |
[(eq? t 'rbrack) |
| 989 |
(die/p-1 p 'read "unexpected ] while reading a vector")] |
| 990 |
[(eq? t 'dot) |
| 991 |
(die/p-1 p 'read "unexpected . while reading a vector")] |
| 992 |
[else |
| 993 |
(let-values ([(a a^ locs k) (parse-token p locs k t pos)]) |
| 994 |
(read-vector p locs k (fxadd1 count) |
| 995 |
(cons a ls) (cons a^ ls^)))])))) |
| 996 |
(define read-bytevector |
| 997 |
(lambda (p locs k count ls) |
| 998 |
(let-values ([(t pos) (tokenize/1+pos p)]) |
| 999 |
(cond |
| 1000 |
[(eof-object? t) |
| 1001 |
(die/p p 'read "end of file encountered while reading a bytevector")] |
| 1002 |
[(eq? t 'rparen) |
| 1003 |
(let ([v (u8-list->bytevector (reverse ls))]) |
| 1004 |
(values v v locs k))] |
| 1005 |
[(eq? t 'rbrack) |
| 1006 |
(die/p-1 p 'read "unexpected ] while reading a bytevector")] |
| 1007 |
[(eq? t 'dot) |
| 1008 |
(die/p-1 p 'read "unexpected . while reading a bytevector")] |
| 1009 |
[else |
| 1010 |
(let-values ([(a a^ locs k) (parse-token p locs k t pos)]) |
| 1011 |
(unless (and (fixnum? a) (fx<= 0 a) (fx<= a 255)) |
| 1012 |
(die/ann a^ 'read |
| 1013 |
"invalid value in a bytevector" a)) |
| 1014 |
(read-bytevector p locs k (fxadd1 count) |
| 1015 |
(cons a ls)))])))) |
| 1016 |
(define read-at-expr |
| 1017 |
(lambda (p locs k at-pos) |
| 1018 |
(define-struct nested (a a^)) |
| 1019 |
(define-struct nested* (a* a*^)) |
| 1020 |
(define (get-chars chars pos p a* a*^) |
| 1021 |
(if (null? chars) |
| 1022 |
(values a* a*^) |
| 1023 |
(let ([str (list->string chars)]) |
| 1024 |
(let ([str^ (annotate-simple str pos p)]) |
| 1025 |
(values (cons str a*) (cons str^ a*^)))))) |
| 1026 |
(define (return start-pos start-col c*** p) |
| 1027 |
(let ([indent |
| 1028 |
(apply min start-col |
| 1029 |
(map |
| 1030 |
(lambda (c**) |
| 1031 |
(define (st00 c* c** n) |
| 1032 |
(if (null? c*) |
| 1033 |
(st0 c** n) |
| 1034 |
(if (char=? (car c*) #\space) |
| 1035 |
(st00 (cdr c*) c** (+ n 1)) |
| 1036 |
n))) |
| 1037 |
(define (st0 c** n) |
| 1038 |
(if (null? c**) |
| 1039 |
start-col |
| 1040 |
(let ([c* (car c**)]) |
| 1041 |
(if (or (nested? c*) (nested*? c*)) |
| 1042 |
start-col |
| 1043 |
(st00 (car c*) (cdr c**) n))))) |
| 1044 |
(st0 c** 0)) |
| 1045 |
(cdr c***)))]) |
| 1046 |
(define (convert c*) |
| 1047 |
(if (or (nested? c*) (nested*? c*)) |
| 1048 |
c* |
| 1049 |
(let ([str (list->string (car c*))]) |
| 1050 |
(let ([str^ (annotate-simple str (cdr c*) p)]) |
| 1051 |
(make-nested str str^))))) |
| 1052 |
(define (trim/convert c**) |
| 1053 |
(define (mk n pos) |
| 1054 |
(let ([str (make-string (- n indent) #\space)]) |
| 1055 |
(let ([str^ (annotate-simple str pos p)]) |
| 1056 |
(make-nested str str^)))) |
| 1057 |
(define (s1 c* pos c** n) |
| 1058 |
(if (null? c*) |
| 1059 |
(let ([c* (car c**)]) |
| 1060 |
(if (or (nested? c*) (nested*? c*)) |
| 1061 |
(cons (mk n pos) (map convert c**)) |
| 1062 |
(s1 c* pos (cdr c**) n))) |
| 1063 |
(if (char=? (car c*) #\space) |
| 1064 |
(s1 (cdr c*) pos c** (+ n 1)) |
| 1065 |
(cons* |
| 1066 |
(mk n pos) |
| 1067 |
(map convert (cons (cons c* pos) c**)))))) |
| 1068 |
(define (s00 c* pos c** n) |
| 1069 |
(if (null? c*) |
| 1070 |
(s0 c** n) |
| 1071 |
(if (char=? #\space (car c*)) |
| 1072 |
(if (< n indent) |
| 1073 |
(s00 (cdr c*) pos c** (+ n 1)) |
| 1074 |
(s1 (cdr c*) pos c** (+ n 1))) |
| 1075 |
(map convert (cons (cons c* pos) c**))))) |
| 1076 |
(define (s0 c** n) |
| 1077 |
(if (null? c**) |
| 1078 |
'() |
| 1079 |
(let ([c* (car c**)]) |
| 1080 |
(if (or (nested? c*) (nested*? c*)) |
| 1081 |
(map convert c**) |
| 1082 |
(s00 (car c*) (cdr c*) (cdr c**) n))))) |
| 1083 |
(s0 c** 0)) |
| 1084 |
(define (cons-initial c** c***) |
| 1085 |
(define (all-white? c**) |
| 1086 |
(andmap (lambda (c*) |
| 1087 |
(and (not (nested? c*)) |
| 1088 |
(not (nested*? c*)) |
| 1089 |
(andmap |
| 1090 |
(lambda (c) (char=? c #\space)) |
| 1091 |
(car c*)))) |
| 1092 |
c**)) |
| 1093 |
(define (nl) |
| 1094 |
(let ([str "\n"]) |
| 1095 |
(list (make-nested str str)))) |
| 1096 |
(define (S1 c*** n) |
| 1097 |
(if (null? c***) |
| 1098 |
(make-list n (nl)) |
| 1099 |
(let ([c** (car c***)] [c*** (cdr c***)]) |
| 1100 |
(if (all-white? c**) |
| 1101 |
(S1 c*** (+ n 1)) |
| 1102 |
(append |
| 1103 |
(make-list n (nl)) |
| 1104 |
(cons (trim/convert c**) |
| 1105 |
(S2 c*** 0 0))))))) |
| 1106 |
(define (S2 c*** n m) |
| 1107 |
(if (null? c***) |
| 1108 |
(make-list (+ n m) (nl)) |
| 1109 |
(let ([c** (car c***)] [c*** (cdr c***)]) |
| 1110 |
(if (all-white? c**) |
| 1111 |
(S2 c*** (+ n 1) -1) |
| 1112 |
(append |
| 1113 |
(make-list (+ n 1) (nl)) |
| 1114 |
(cons (trim/convert c**) |
| 1115 |
(S2 c*** 0 0))))))) |
| 1116 |
(define (S0 c** c***) |
| 1117 |
(if (all-white? c**) |
| 1118 |
(S1 c*** 0) |
| 1119 |
(cons |
| 1120 |
(map convert c**) |
| 1121 |
(S2 c*** 0 0)))) |
| 1122 |
(S0 c** c***)) |
| 1123 |
(let ([c** (cons-initial (car c***) (cdr c***))]) |
| 1124 |
(let ([n* (apply append c**)]) |
| 1125 |
(define (extract p p* ls) |
| 1126 |
(let f ([ls ls]) |
| 1127 |
(cond |
| 1128 |
[(null? ls) '()] |
| 1129 |
[(nested? (car ls)) (cons (p (car ls)) (f (cdr ls)))] |
| 1130 |
[else (append (p* (car ls)) (f (cdr ls)))]))) |
| 1131 |
(let ([c* (extract nested-a nested*-a* n*)] |
| 1132 |
[c*^ (extract nested-a^ nested*-a*^ n*)]) |
| 1133 |
(values c* (annotate c* c*^ start-pos p) locs k)))))) |
| 1134 |
(define (read-text p locs k pref*) |
| 1135 |
(let ([start-pos (port-position p)] |
| 1136 |
[start-col (input-port-column-number p)]) |
| 1137 |
(let f ([c* '()] [pos start-pos] |
| 1138 |
[c** '()] [c*** '()] |
| 1139 |
[depth 0] [locs locs] [k k]) |
| 1140 |
(define (match-prefix c* pref*) |
| 1141 |
(cond |
| 1142 |
[(and (pair? c*) (pair? pref*)) |
| 1143 |
(and (char=? (car c*) (car pref*)) |
| 1144 |
(match-prefix (cdr c*) (cdr pref*)))] |
| 1145 |
[else (and (null? pref*) c*)])) |
| 1146 |
(let ([c (read-char p)]) |
| 1147 |
(cond |
| 1148 |
[(eof-object? c) |
| 1149 |
(die/p p 'read "end of file while reading @-expr text")] |
| 1150 |
[(char=? c #\}) |
| 1151 |
(let g ([x* (cons #\} c*)] [p* pref*]) |
| 1152 |
(if (null? p*) |
| 1153 |
(if (= depth 0) |
| 1154 |
(let ([c** |
| 1155 |
(reverse |
| 1156 |
(if (null? c*) |
| 1157 |
c** |
| 1158 |
(cons (cons (reverse c*) pos) c**)))]) |
| 1159 |
(let ([c*** (reverse (cons c** c***))]) |
| 1160 |
(return start-pos start-col c*** p))) |
| 1161 |
(f x* pos c** c*** (- depth 1) locs k)) |
| 1162 |
(let ([c (peek-char p)]) |
| 1163 |
(cond |
| 1164 |
[(eof-object? c) |
| 1165 |
(die/p p 'read "invalid eof inside @-expression")] |
| 1166 |
[(char=? c (rev-punc (car p*))) |
| 1167 |
(read-char p) |
| 1168 |
(g (cons c x*) (cdr p*))] |
| 1169 |
[else |
| 1170 |
(f x* pos c** c*** depth locs k)]))))] |
| 1171 |
[(char=? c #\{) |
| 1172 |
(f (cons c c*) pos c** c*** |
| 1173 |
(if (match-prefix c* pref*) (+ depth 1) depth) |
| 1174 |
locs k)] |
| 1175 |
[(char=? c #\newline) |
| 1176 |
(f '() |
| 1177 |
(port-position p) |
| 1178 |
'() |
| 1179 |
(cons (reverse |
| 1180 |
(if (null? c*) |
| 1181 |
c** |
| 1182 |
(cons (cons (reverse c*) pos) c**))) |
| 1183 |
c***) |
| 1184 |
depth locs k)] |
| 1185 |
[(and (char=? c #\@) (match-prefix c* pref*)) => |
| 1186 |
(lambda (c*) |
| 1187 |
(let ([c (peek-char p)]) |
| 1188 |
(cond |
| 1189 |
[(eof-object? c) |
| 1190 |
(die/p p 'read "invalid eof inside nested @-expr")] |
| 1191 |
[(char=? c #\") |
| 1192 |
(read-char p) |
| 1193 |
(let ([c* (tokenize-string c* p)]) |
| 1194 |
(f c* pos c** c*** depth locs k))] |
| 1195 |
[else |
| 1196 |
(let-values ([(a* a*^ locs k) |
| 1197 |
(read-at-text-mode p locs k)]) |
| 1198 |
(f '() |
| 1199 |
(port-position p) |
| 1200 |
(cons (make-nested* a* a*^) |
| 1201 |
(if (null? c*) |
| 1202 |
c** |
| 1203 |
(cons (cons (reverse c*) pos) c**))) |
| 1204 |
c*** depth locs k))])))] |
| 1205 |
[else |
| 1206 |
(f (cons c c*) pos c** c*** depth locs k)]))))) |
| 1207 |
(define (read-brackets p locs k) |
| 1208 |
(let-values ([(a* a*^ locs k) |
| 1209 |
(read-list p locs k 'rbrack 'rparen #t)]) |
| 1210 |
(unless (list? a*) |
| 1211 |
(die/ann a*^ 'read "not a proper list")) |
| 1212 |
(let ([c (peek-char p)]) |
| 1213 |
(cond |
| 1214 |
[(eof-object? c) ;;; @<cmd>[...] |
| 1215 |
(values a* a*^ locs k)] |
| 1216 |
[(char=? c #\{) |
| 1217 |
(read-char p) |
| 1218 |
(let-values ([(b* b*^ locs k) |
| 1219 |
(read-text p locs k '())]) |
| 1220 |
(values (append a* b*) |
| 1221 |
(append a*^ b*^) |
| 1222 |
locs k))] |
| 1223 |
[(char=? c #\|) |
| 1224 |
(read-char p) |
| 1225 |
(let-values ([(b* b*^ locs k) |
| 1226 |
(read-at-bar p locs k #t)]) |
| 1227 |
(values (append a* b*) |
| 1228 |
(append a*^ b*^) |
| 1229 |
locs k))] |
| 1230 |
[else (values a* a*^ locs k)])))) |
| 1231 |
(define (left-punc? c) |
| 1232 |
(define chars "([<!?~$%^&*-_+=:") |
| 1233 |
(let f ([i 0]) |
| 1234 |
(cond |
| 1235 |
[(= i (string-length chars)) #f] |
| 1236 |
[(char=? c (string-ref chars i)) #t] |
| 1237 |
[else (f (+ i 1))]))) |
| 1238 |
(define (rev-punc c) |
| 1239 |
(cond |
| 1240 |
[(char=? c #\() #\)] |
| 1241 |
[(char=? c #\[) #\]] |
| 1242 |
[(char=? c #\<) #\>] |
| 1243 |
[else c])) |
| 1244 |
(define (read-at-bar p locs k text-mode?) |
| 1245 |
(let ([c (peek-char p)]) |
| 1246 |
(cond |
| 1247 |
[(eof-object? c) |
| 1248 |
(die/p p 'read "eof inside @|-expression")] |
| 1249 |
[(and (char=? c #\|) text-mode?) ;;; @|| |
| 1250 |
(read-char p) |
| 1251 |
(values '() '() locs k)] |
| 1252 |
[(char=? c #\{) ;;; @|{ |
| 1253 |
(read-char p) |
| 1254 |
(read-text p locs k '(#\|))] |
| 1255 |
[(left-punc? c) ;;; @|<({ |
| 1256 |
(read-char p) |
| 1257 |
(let ([pos (port-position p)]) |
| 1258 |
(let f ([ls (list c)]) |
| 1259 |
(let ([c (peek-char p)]) |
| 1260 |
(cond |
| 1261 |
[(eof-object? c) |
| 1262 |
(die/p p 'read "eof inside @|< mode")] |
| 1263 |
[(left-punc? c) |
| 1264 |
(read-char p) |
| 1265 |
(f (cons c ls))] |
| 1266 |
[(char=? c #\{) |
| 1267 |
(read-char p) |
| 1268 |
(read-text p locs k (append ls '(#\|)))] |
| 1269 |
[else |
| 1270 |
(read-at-bar-others ls p locs k)]))))] |
| 1271 |
[text-mode? ;;; @|5 6 7| |
| 1272 |
(read-at-bar-datum p locs k)] |
| 1273 |
[else |
| 1274 |
(die/p p 'read "invalid char in @| mode" c)]))) |
| 1275 |
(define (read-at-bar-others ls p locs k) |
| 1276 |
(define (split ls) |
| 1277 |
(cond |
| 1278 |
[(null? ls) (values '() '())] |
| 1279 |
[(initial? (car ls)) |
| 1280 |
(let-values ([(a d) (split (cdr ls))]) |
| 1281 |
(values (cons (car ls) a) d))] |
| 1282 |
[else |
| 1283 |
(values '() ls)])) |
| 1284 |
(define (mksymbol ls) |
| 1285 |
(let ([s (string->symbol |
| 1286 |
(list->string |
| 1287 |
(reverse ls)))]) |
| 1288 |
(values s s))) |
| 1289 |
(let-values ([(inits rest) (split ls)]) |
| 1290 |
(let ([ls (tokenize-identifier inits p)]) |
| 1291 |
(let-values ([(s s^) (mksymbol ls)]) |
| 1292 |
(let g ([rest rest] |
| 1293 |
[a* (list s)] |
| 1294 |
[a*^ (list s^)] |
| 1295 |
[locs locs] |
| 1296 |
[k k]) |
| 1297 |
(if (null? rest) |
| 1298 |
(let-values ([(b* b*^ locs k) |
| 1299 |
(read-at-bar-datum p locs k)]) |
| 1300 |
(values (append a* b*) (append a*^ b*^) locs k)) |
| 1301 |
(let ([x (car rest)]) |
| 1302 |
(case x |
| 1303 |
[(#\() #\) ;;; vim paren-matching sucks |
| 1304 |
(let-values ([(b* b*^ locs k) |
| 1305 |
(read-list p locs k 'rparen 'rbrack #t)]) |
| 1306 |
(g (cdr rest) |
| 1307 |
(list (append a* b*)) |
| 1308 |
(list (append a*^ b*^)) |
| 1309 |
locs k))] |
| 1310 |
[(#\[) #\] ;;; vim paren-matching sucks |
| 1311 |
(let-values ([(b* b*^ locs k) |
| 1312 |
(read-list p locs k 'rbrack 'rparen #t)]) |
| 1313 |
(g (cdr rest) |
| 1314 |
(list (append a* b*)) |
| 1315 |
(list (append a*^ b*^)) |
| 1316 |
locs k))] |
| 1317 |
[else |
| 1318 |
(let-values ([(inits rest) (split rest)]) |
| 1319 |
(let-values ([(s s^) (mksymbol inits)]) |
| 1320 |
(g rest |
| 1321 |
(cons s a*) |
| 1322 |
(cons s^ a*^) |
| 1323 |
locs k)))])))))))) |
| 1324 |
(define (read-at-bar-datum p locs k) |
| 1325 |
(let ([c (peek-char p)]) |
| 1326 |
(cond |
| 1327 |
[(eof-object? c) (die/p p 'read "eof inside @|datum mode")] |
| 1328 |
[(char-whitespace? c) |
| 1329 |
(read-char p) |
| 1330 |
(read-at-bar-datum p locs k)] |
| 1331 |
[(char=? c #\|) |
| 1332 |
(read-char p) |
| 1333 |
(values '() '() locs k)] |
| 1334 |
[else |
| 1335 |
(let-values ([(a a^ locs k) (read-expr p locs k)]) |
| 1336 |
(let-values ([(a* a*^ locs k) (read-at-bar-datum p locs k)]) |
| 1337 |
(values (cons a a*) (cons a^ a*^) locs k)))]))) |
| 1338 |
(define (read-at-text-mode p locs k) |
| 1339 |
(let ([c (peek-char p)]) |
| 1340 |
(cond |
| 1341 |
[(eof-object? c) |
| 1342 |
(die/p p 'read "eof encountered inside @-expression")] |
| 1343 |
[(char=? c #\|) |
| 1344 |
(read-char p) |
| 1345 |
(read-at-bar p locs k #t)] |
| 1346 |
[else |
| 1347 |
(let-values ([(a a^ locs k) |
| 1348 |
(read-at-sexpr-mode p locs k)]) |
| 1349 |
(values (list a) (list a^) locs k))]))) |
| 1350 |
(define (read-at-sexpr-mode p locs k) |
| 1351 |
(let ([c (peek-char p)]) |
| 1352 |
(cond |
| 1353 |
[(eof-object? c) |
| 1354 |
(die/p p 'read "eof encountered inside @-expression")] |
| 1355 |
[(eqv? c '#\[) ;;; @[ ... |
| 1356 |
(read-char p) |
| 1357 |
(read-brackets p locs k)] |
| 1358 |
[(eqv? c #\{) ;;; @{ ... |
| 1359 |
(read-char p) |
| 1360 |
(read-text p locs k '())] |
| 1361 |
[(char=? c #\|) |
| 1362 |
(read-char p) |
| 1363 |
(read-at-bar p locs k #f)] |
| 1364 |
[else ;;; @<cmd> ... |
| 1365 |
(let-values ([(a a^ locs k) (read-expr p locs k)]) |
| 1366 |
(let ([c (peek-char p)]) |
| 1367 |
(cond |
| 1368 |
[(eof-object? c) ;;; @<cmd><eof> |
| 1369 |
(values a a^ locs k)] |
| 1370 |
[(eqv? c #\[) |
| 1371 |
(read-char p) |
| 1372 |
(let-values ([(a* a*^ locs k) |
| 1373 |
(read-brackets p locs k)]) |
| 1374 |
(let ([v (cons a a*)] [v^ (cons a^ a*^)]) |
| 1375 |
(values v (annotate v v^ at-pos p) locs k)))] |
| 1376 |
[(eqv? c #\{) ;;; @<cmd>{ ... |
| 1377 |
(read-char p) |
| 1378 |
(let-values ([(a* a*^ locs k) |
| 1379 |
(read-text p locs k '())]) |
| 1380 |
(let ([v (cons a a*)] [v^ (cons a^ a*^)]) |
| 1381 |
(values v (annotate v v^ at-pos p) locs k)))] |
| 1382 |
[(eqv? c #\|) ;;; @<cmd>| ... |
| 1383 |
(read-char p) |
| 1384 |
(let-values ([(a* a*^ locs k) |
| 1385 |
(read-at-bar p locs k #f)]) |
| 1386 |
(let ([v (cons a a*)] [v^ (cons a^ a*^)]) |
| 1387 |
(values v (annotate v v^ at-pos p) locs k)))] |
| 1388 |
[else |
| 1389 |
(values a a^ locs k)])))]))) |
| 1390 |
(read-at-sexpr-mode p locs k))) |
| 1391 |
(define parse-token |
| 1392 |
(lambda (p locs k t pos) |
| 1393 |
(cond |
| 1394 |
[(eof-object? t) |
| 1395 |
(values (eof-object) |
| 1396 |
(annotate-simple (eof-object) pos p) locs k)] |
| 1397 |
[(eq? t 'lparen) |
| 1398 |
(let-values ([(ls ls^ locs k) |
| 1399 |
(read-list p locs k 'rparen 'rbrack #t)]) |
| 1400 |
(values ls (annotate ls ls^ pos p) locs k))] |
| 1401 |
[(eq? t 'lbrack) |
| 1402 |
(let-values ([(ls ls^ locs k) |
| 1403 |
(read-list p locs k 'rbrack 'rparen #t)]) |
| 1404 |
(values ls (annotate ls ls^ pos p) locs k))] |
| 1405 |
[(eq? t 'vparen) |
| 1406 |
(let-values ([(v v^ locs k) |
| 1407 |
(read-vector p locs k 0 '() '())]) |
| 1408 |
(values v (annotate v v^ pos p) locs k))] |
| 1409 |
[(eq? t 'vu8) |
| 1410 |
(let-values ([(v v^ locs k) |
| 1411 |
(read-bytevector p locs k 0 '())]) |
| 1412 |
(values v (annotate v v^ pos p) locs k))] |
| 1413 |
[(eq? t 'at-expr) |
| 1414 |
(read-at-expr p locs k pos)] |
| 1415 |
[(pair? t) |
| 1416 |
(cond |
| 1417 |
[(eq? (car t) 'datum) |
| 1418 |
(values (cdr t) |
| 1419 |
(annotate-simple (cdr t) pos p) locs k)] |
| 1420 |
[(eq? (car t) 'macro) |
| 1421 |
(let ([macro (cdr t)]) |
| 1422 |
(define (read-macro) |
| 1423 |
(let-values ([(t pos) (tokenize/1+pos p)]) |
| 1424 |
(cond |
| 1425 |
[(eof-object? t) |
| 1426 |
(die/p p 'read |
| 1427 |
(format "invalid eof after ~a read macro" |
| 1428 |
macro))] |
| 1429 |
[else (parse-token p locs k t pos)]))) |
| 1430 |
(let-values ([(expr expr^ locs k) (read-macro)]) |
| 1431 |
(let ([d (list expr)] [d^ (list expr^)]) |
| 1432 |
(let ([x (cons macro d)] |
| 1433 |
[x^ (cons (annotate-simple macro pos p) d^)]) |
| 1434 |
(values x (annotate x x^ pos p) locs |
| 1435 |
(extend-k-pair d d^ expr '() k))))))] |
| 1436 |
[(eq? (car t) 'mark) |
| 1437 |
(let ([n (cdr t)]) |
| 1438 |
(let-values ([(expr expr^ locs k) |
| 1439 |
(read-expr p locs k)]) |
| 1440 |
(cond |
| 1441 |
[(assq n locs) => |
| 1442 |
(lambda (x) |
| 1443 |
(let ([loc (cdr x)]) |
| 1444 |
(when (loc-set? loc) ;;; FIXME: pos |
| 1445 |
(die/p p 'read "duplicate mark" n)) |
| 1446 |
(set-loc-value! loc expr) |
| 1447 |
(set-loc-value^! loc expr^) |
| 1448 |
(set-loc-set?! loc #t) |
| 1449 |
(values expr expr^ locs k)))] |
| 1450 |
[else |
| 1451 |
(let ([loc (make-loc expr 'unused #t)]) |
| 1452 |
(let ([locs (cons (cons n loc) locs)]) |
| 1453 |
(values expr expr^ locs k)))])))] |
| 1454 |
[(eq? (car t) 'ref) |
| 1455 |
(let ([n (cdr t)]) |
| 1456 |
(cond |
| 1457 |
[(assq n locs) => |
| 1458 |
(lambda (x) |
| 1459 |
(values (cdr x) 'unused locs k))] |
| 1460 |
[else |
| 1461 |
(let ([loc (make-loc #f 'unused #f)]) |
| 1462 |
(let ([locs (cons (cons n loc) locs)]) |
| 1463 |
(values loc 'unused locs k)))]))] |
| 1464 |
[else (die/p p 'read "invalid token" t)])] |
| 1465 |
[else |
| 1466 |
(die/p-1 p 'read |
| 1467 |
(format "unexpected ~s found" t))]))) |
| 1468 |
(define read-expr |
| 1469 |
(lambda (p locs k) |
| 1470 |
(let-values ([(t pos) (tokenize/1+pos p)]) |
| 1471 |
(parse-token p locs k t pos)))) |
| 1472 |
(define read-expr-script-initial |
| 1473 |
(lambda (p locs k) |
| 1474 |
(let-values ([(t pos) (tokenize-script-initial+pos p)]) |
| 1475 |
(parse-token p locs k t pos))))) |
| 1476 |
|
| 1477 |
|
| 1478 |
(define (reduce-loc! p) |
| 1479 |
(lambda (x) |
| 1480 |
(let ([loc (cdr x)]) |
| 1481 |
(unless (loc-set? loc) |
| 1482 |
(die/p p 'read "referenced mark is not set" (car x))) |
| 1483 |
(when (loc? (loc-value loc)) |
| 1484 |
(let f ([h loc] [t loc]) |
| 1485 |
(if (loc? h) |
| 1486 |
(let ([h1 (loc-value h)]) |
| 1487 |
(if (loc? h1) |
| 1488 |
(begin |
| 1489 |
(when (eq? h1 t) |
| 1490 |
(die/p p 'read "circular marks")) |
| 1491 |
(let ([v (f (loc-value h1) (loc-value t))]) |
| 1492 |
(set-loc-value! h1 v) |
| 1493 |
(set-loc-value! h v) |
| 1494 |
v)) |
| 1495 |
(begin |
| 1496 |
(set-loc-value! h h1) |
| 1497 |
h1))) |
| 1498 |
h)))))) |
| 1499 |
|
| 1500 |
(define (read-as-comment p) |
| 1501 |
(begin (read-expr p '() void) (void))) |
| 1502 |
|
| 1503 |
(define (return-annotated x) |
| 1504 |
(cond |
| 1505 |
[(and (annotation? x) (eof-object? (annotation-expression x))) |
| 1506 |
(eof-object)] |
| 1507 |
[else x])) |
| 1508 |
|
| 1509 |
(define my-read |
| 1510 |
(lambda (p) |
| 1511 |
(let-values ([(expr expr^ locs k) (read-expr p '() void)]) |
| 1512 |
(cond |
| 1513 |
[(null? locs) expr] |
| 1514 |
[else |
| 1515 |
(for-each (reduce-loc! p) locs) |
| 1516 |
(k) |
| 1517 |
(if (loc? expr) |
| 1518 |
(loc-value expr) |
| 1519 |
expr)])))) |
| 1520 |
|
| 1521 |
(define read-initial |
| 1522 |
(lambda (p) |
| 1523 |
(let-values ([(expr expr^ locs k) (read-expr-script-initial p '() void)]) |
| 1524 |
(cond |
| 1525 |
[(null? locs) expr] |
| 1526 |
[else |
| 1527 |
(for-each (reduce-loc! p) locs) |
| 1528 |
(k) |
| 1529 |
(if (loc? expr) |
| 1530 |
(loc-value expr) |
| 1531 |
expr)])))) |
| 1532 |
|
| 1533 |
(define read-annotated |
| 1534 |
(case-lambda |
| 1535 |
[(p) |
| 1536 |
(unless (input-port? p) |
| 1537 |
(error 'read-annotated "not an input port" p)) |
| 1538 |
(let-values ([(expr expr^ locs k) (read-expr p '() void)]) |
| 1539 |
(cond |
| 1540 |
[(null? locs) (return-annotated expr^)] |
| 1541 |
[else |
| 1542 |
(for-each (reduce-loc! p) locs) |
| 1543 |
(k) |
| 1544 |
(if (loc? expr) |
| 1545 |
(loc-value^ expr) |
| 1546 |
(return-annotated expr^))]))] |
| 1547 |
[() (read-annotated (current-input-port))])) |
| 1548 |
|
| 1549 |
(define read-script-annotated |
| 1550 |
(lambda (p) |
| 1551 |
(let-values ([(expr expr^ locs k) (read-expr-script-initial p '() void)]) |
| 1552 |
(cond |
| 1553 |
[(null? locs) (return-annotated expr^)] |
| 1554 |
[else |
| 1555 |
(for-each (reduce-loc! p) locs) |
| 1556 |
(k) |
| 1557 |
(if (loc? expr) |
| 1558 |
(loc-value^ expr) |
| 1559 |
(return-annotated expr^))])))) |
| 1560 |
|
| 1561 |
(define read-token |
| 1562 |
(case-lambda |
| 1563 |
[() (tokenize/1 (current-input-port))] |
| 1564 |
[(p) |
| 1565 |
(if (input-port? p) |
| 1566 |
(tokenize/1 p) |
| 1567 |
(die 'read-token "not an input port" p))])) |
| 1568 |
|
| 1569 |
(define read |
| 1570 |
(case-lambda |
| 1571 |
[() (my-read (current-input-port))] |
| 1572 |
[(p) |
| 1573 |
(if (input-port? p) |
| 1574 |
(my-read p) |
| 1575 |
(die 'read "not an input port" p))])) |
| 1576 |
|
| 1577 |
(define (get-datum p) |
| 1578 |
(unless (input-port? p) |
| 1579 |
(die 'get-datum "not an input port")) |
| 1580 |
(my-read p)) |
| 1581 |
|
| 1582 |
(define comment-handler |
| 1583 |
;;; this is stale, maybe delete |
| 1584 |
(make-parameter |
| 1585 |
(lambda (x) (void)) |
| 1586 |
(lambda (x) |
| 1587 |
(unless (procedure? x) |
| 1588 |
(die 'comment-handler "not a procedure" x)) |
| 1589 |
x))) |
| 1590 |
|
| 1591 |
) |
| 1592 |
|
| 1593 |
|
| 1594 |
|