2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
|
|
|
;;; Copyright (C) 2006,2007 Abdulaziz Ghuloum
|
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2006-11-23 19:48:14 -05:00
|
|
|
|
2007-05-05 20:47:31 -04:00
|
|
|
(library (ikarus reader)
|
2007-11-22 14:43:39 -05:00
|
|
|
(export read read-initial read-token comment-handler get-datum)
|
2007-05-05 20:47:31 -04:00
|
|
|
(import
|
2007-05-06 18:43:04 -04:00
|
|
|
(ikarus system $chars)
|
2007-05-15 23:57:35 -04:00
|
|
|
(ikarus system $fx)
|
|
|
|
(ikarus system $pairs)
|
|
|
|
(ikarus system $bytevectors)
|
2007-06-17 10:20:19 -04:00
|
|
|
(only (ikarus unicode-data) unicode-printable-char?)
|
2007-12-13 05:57:15 -05:00
|
|
|
(except (ikarus) read-char read read-token comment-handler get-datum))
|
|
|
|
|
|
|
|
(define-syntax read-char
|
|
|
|
(syntax-rules ()
|
|
|
|
[(_ p) (get-char p)]))
|
2007-04-29 22:29:42 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define delimiter?
|
|
|
|
(lambda (c)
|
|
|
|
(or (char-whitespace? c)
|
2007-12-01 03:18:28 -05:00
|
|
|
(memq c '(#\( #\) #\[ #\] #\" #\# #\;)))))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define digit?
|
|
|
|
(lambda (c)
|
2006-11-23 19:48:14 -05:00
|
|
|
(and ($char<= #\0 c) ($char<= c #\9))))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define char->num
|
|
|
|
(lambda (c)
|
2006-11-23 19:38:26 -05:00
|
|
|
(fx- ($char->fixnum c) ($char->fixnum #\0))))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define initial?
|
|
|
|
(lambda (c)
|
2007-06-01 22:17:22 -04:00
|
|
|
(cond
|
|
|
|
[($char<= c ($fixnum->char 127))
|
|
|
|
(or (letter? c) (special-initial? c))]
|
|
|
|
[else (unicode-printable-char? c)])))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define letter?
|
|
|
|
(lambda (c)
|
2006-11-23 19:48:14 -05:00
|
|
|
(or (and ($char<= #\a c) ($char<= c #\z))
|
|
|
|
(and ($char<= #\A c) ($char<= c #\Z)))))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define af?
|
|
|
|
(lambda (c)
|
2006-11-23 19:48:14 -05:00
|
|
|
(or (and ($char<= #\a c) ($char<= c #\f))
|
|
|
|
(and ($char<= #\A c) ($char<= c #\F)))))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define af->num
|
|
|
|
(lambda (c)
|
2006-11-23 19:48:14 -05:00
|
|
|
(if (and ($char<= #\a c) ($char<= c #\f))
|
2006-11-23 19:38:26 -05:00
|
|
|
(fx+ 10 (fx- ($char->fixnum c) ($char->fixnum #\a)))
|
|
|
|
(fx+ 10 (fx- ($char->fixnum c) ($char->fixnum #\A))))))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define special-initial?
|
|
|
|
(lambda (c)
|
|
|
|
(memq c '(#\! #\$ #\% #\& #\* #\/ #\: #\< #\= #\> #\? #\^ #\_ #\~))))
|
|
|
|
(define subsequent?
|
|
|
|
(lambda (c)
|
|
|
|
(or (initial? c) (digit? c) (special-subsequent? c))))
|
|
|
|
(define special-subsequent?
|
|
|
|
(lambda (c)
|
|
|
|
(memq c '(#\+ #\- #\. #\@))))
|
|
|
|
(define tokenize-identifier
|
|
|
|
(lambda (ls p)
|
2007-12-05 05:01:56 -05:00
|
|
|
(let ([c (peek-char p)])
|
2006-11-23 19:33:45 -05:00
|
|
|
(cond
|
|
|
|
[(eof-object? c) ls]
|
|
|
|
[(subsequent? c)
|
2007-12-05 05:01:56 -05:00
|
|
|
(tokenize-identifier (cons (read-char p) ls) p)]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(delimiter? c)
|
|
|
|
ls]
|
2007-12-05 05:01:56 -05:00
|
|
|
[(char=? c #\\)
|
|
|
|
(read-char p)
|
|
|
|
(tokenize-backslash ls p)]
|
2006-11-23 19:33:45 -05:00
|
|
|
[else
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'tokenize "invalid identifier syntax"
|
2006-11-23 19:33:45 -05:00
|
|
|
(list->string (reverse (cons c ls))))]))))
|
2007-12-02 06:58:33 -05:00
|
|
|
(define (tokenize-string ls p)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "invalid eof inside string")]
|
|
|
|
[else (tokenize-string-char ls p c)])))
|
|
|
|
(define (tokenize-string-char ls p c)
|
|
|
|
(define (intraline-whitespace? c)
|
|
|
|
(or (eqv? c #\x9)
|
|
|
|
(eq? (char-general-category c) 'Zs)))
|
|
|
|
(define (tokenize-string-continue ls p c)
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "invalid eof inside string")]
|
|
|
|
[(intraline-whitespace? c)
|
|
|
|
(let f ()
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "invalid eof inside string")]
|
|
|
|
[(intraline-whitespace? c) (f)]
|
|
|
|
[else (tokenize-string-char ls p c)])))]
|
|
|
|
[else (tokenize-string-char ls p c)]))
|
|
|
|
(cond
|
|
|
|
[($char= #\" c) ls]
|
|
|
|
[($char= #\\ c)
|
2006-11-23 19:33:45 -05:00
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
2007-12-02 06:58:33 -05:00
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "invalid eof after string escape")]
|
|
|
|
[($char= #\a c) (tokenize-string (cons #\x7 ls) p)]
|
|
|
|
[($char= #\b c) (tokenize-string (cons #\x8 ls) p)]
|
|
|
|
[($char= #\t c) (tokenize-string (cons #\x9 ls) p)]
|
|
|
|
[($char= #\n c) (tokenize-string (cons #\xA ls) p)]
|
|
|
|
[($char= #\v c) (tokenize-string (cons #\xB ls) p)]
|
|
|
|
[($char= #\f c) (tokenize-string (cons #\xC ls) p)]
|
|
|
|
[($char= #\r c) (tokenize-string (cons #\xD ls) p)]
|
|
|
|
[($char= #\" c) (tokenize-string (cons #\x22 ls) p)]
|
|
|
|
[($char= #\\ c) (tokenize-string (cons #\x5C ls) p)]
|
|
|
|
[($char= #\x c) ;;; unicode escape \xXXX;
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "invalid eof inside string")]
|
|
|
|
[(hex c) =>
|
|
|
|
(lambda (n)
|
|
|
|
(let f ([n n])
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? n)
|
|
|
|
(error 'tokenize "invalid eof inside string")]
|
|
|
|
[(hex c) =>
|
|
|
|
(lambda (v) (f (+ (* n 16) v)))]
|
|
|
|
[($char= c #\;)
|
|
|
|
(tokenize-string
|
|
|
|
(cons (integer->char n) ls) p)]
|
|
|
|
[else
|
|
|
|
(error 'tokenize
|
|
|
|
"invalid char in escape sequence"
|
|
|
|
c)]))))]
|
|
|
|
[else
|
|
|
|
(error 'tokenize
|
|
|
|
"invalid char in escape sequence" c)]))]
|
|
|
|
[(intraline-whitespace? c)
|
|
|
|
(let f ()
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "invalid eof inside string")]
|
|
|
|
[(intraline-whitespace? c) (f)]
|
|
|
|
[(memv c '(#\xA #\x85 #\x2028))
|
|
|
|
(tokenize-string-continue ls p (read-char p))]
|
|
|
|
[(memv c '(#\xD))
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(memv c '(#\xA #\x85))
|
|
|
|
(tokenize-string-continue ls p (read-char p))]
|
|
|
|
[else
|
|
|
|
(tokenize-string-continue ls p c)]))]
|
|
|
|
[else
|
|
|
|
(error 'tokenize
|
|
|
|
"non-whitespace character after escape")])))]
|
|
|
|
[(memv c '(#\xA #\x85 #\x2028))
|
|
|
|
(tokenize-string-continue ls p (read-char p))]
|
|
|
|
[(memv c '(#\xD))
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(memv c '(#\xA #\x85))
|
|
|
|
(tokenize-string-continue ls p (read-char p))]
|
|
|
|
[else
|
|
|
|
(tokenize-string-continue ls p c)]))]
|
|
|
|
[else (error 'tokenize "invalid string escape" c)]))]
|
|
|
|
[(memv c '(#\xA #\x85 #\x2028))
|
|
|
|
(tokenize-string (cons #\linefeed ls) p)]
|
|
|
|
[(memv c '(#\xD))
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(when (memv c '(#\xA #\x85))
|
|
|
|
(read-char p))
|
|
|
|
(tokenize-string (cons #\linefeed ls) p))]
|
|
|
|
[else
|
|
|
|
(tokenize-string (cons c ls) p)]))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define skip-comment
|
|
|
|
(lambda (p)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(unless (eof-object? c)
|
2006-11-23 19:38:26 -05:00
|
|
|
(let ([i ($char->fixnum c)])
|
2006-11-23 19:33:45 -05:00
|
|
|
(unless (or (fx= i 10) (fx= i 13))
|
|
|
|
(skip-comment p)))))))
|
|
|
|
(define tokenize-dot
|
|
|
|
(lambda (p)
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) 'dot]
|
|
|
|
[(delimiter? c) 'dot]
|
2006-11-23 19:48:14 -05:00
|
|
|
[($char= c #\.) ; this is second dot
|
2006-11-23 19:33:45 -05:00
|
|
|
(read-char p)
|
2007-12-05 05:01:56 -05:00
|
|
|
(let ([c (peek-char p)])
|
2006-11-23 19:33:45 -05:00
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "invalid syntax .. near end of file")]
|
2006-11-23 19:48:14 -05:00
|
|
|
[($char= c #\.) ; this is the third
|
2007-12-05 05:01:56 -05:00
|
|
|
(read-char p)
|
2006-11-23 19:33:45 -05:00
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
2007-01-20 19:26:17 -05:00
|
|
|
[(eof-object? c) '(datum . ...)]
|
|
|
|
[(delimiter? c) '(datum . ...)]
|
|
|
|
[else
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'tokenize "invalid syntax"
|
|
|
|
(string-append "..." (string c)))]))]
|
2006-11-23 19:33:45 -05:00
|
|
|
[else
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'tokenize "invalid syntax"
|
|
|
|
(string-append ".." (string c)))]))]
|
2007-06-14 11:56:47 -04:00
|
|
|
[else
|
|
|
|
(cons 'datum
|
|
|
|
(tokenize-decimal-no-digits p '(#\.) #f))]))))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define tokenize-char*
|
|
|
|
(lambda (i str p d)
|
|
|
|
(cond
|
|
|
|
[(fx= i (string-length str))
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) d]
|
|
|
|
[(delimiter? c) d]
|
2007-10-25 14:32:26 -04:00
|
|
|
[else (error 'tokenize "invalid character after sequence"
|
|
|
|
(string-append (string c) str))]))]
|
2006-11-23 19:33:45 -05:00
|
|
|
[else
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'tokenize "invalid eof in the middle of expected sequence" str)]
|
2006-11-23 19:48:14 -05:00
|
|
|
[($char= c (string-ref str i))
|
2006-11-23 19:33:45 -05:00
|
|
|
(tokenize-char* (fxadd1 i) str p d)]
|
|
|
|
[else
|
|
|
|
(error 'tokenize
|
2007-10-25 14:32:26 -04:00
|
|
|
"invalid char while scanning string"
|
|
|
|
c str)]))])))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define tokenize-char-seq
|
|
|
|
(lambda (p str d)
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (cons 'datum (string-ref str 0))]
|
|
|
|
[(delimiter? c) (cons 'datum (string-ref str 0))]
|
2006-11-23 19:48:14 -05:00
|
|
|
[($char= (string-ref str 1) c)
|
2006-11-23 19:33:45 -05:00
|
|
|
(read-char p)
|
|
|
|
(tokenize-char* 2 str p d)]
|
2007-10-25 14:32:26 -04:00
|
|
|
[else (error 'tokenize "invalid syntax"
|
2006-11-23 19:33:45 -05:00
|
|
|
(string-ref str 0) c)]))))
|
|
|
|
(define tokenize-char
|
|
|
|
(lambda (p)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "invalid #\\ near end of file")]
|
2007-11-07 11:24:18 -05:00
|
|
|
[(eqv? #\n c)
|
2007-11-07 11:41:28 -05:00
|
|
|
(let ([c (peek-char p)])
|
2007-11-07 11:24:18 -05:00
|
|
|
(cond
|
2007-11-07 11:41:28 -05:00
|
|
|
[(eof-object? c)
|
|
|
|
(read-char p)
|
|
|
|
'(datum . #\n)]
|
2007-11-07 11:24:18 -05:00
|
|
|
[(eqv? #\u c)
|
2007-11-07 11:41:28 -05:00
|
|
|
(read-char p)
|
|
|
|
(tokenize-char-seq p "ul" '(datum . #\x0))]
|
2007-11-07 11:24:18 -05:00
|
|
|
[(eqv? #\e c)
|
2007-11-07 11:41:28 -05:00
|
|
|
(read-char p)
|
|
|
|
(tokenize-char-seq p "ewline" '(datum . #\xA))]
|
|
|
|
[(delimiter? c)
|
|
|
|
'(datum . #\n)]
|
2007-11-07 11:24:18 -05:00
|
|
|
[else
|
2007-11-07 11:41:28 -05:00
|
|
|
(error 'tokenize "invalid syntax"
|
2007-11-07 11:24:18 -05:00
|
|
|
(string #\# #\\ #\n c))]))]
|
|
|
|
[(eqv? #\a c)
|
2007-11-07 11:41:28 -05:00
|
|
|
(tokenize-char-seq p "alarm" '(datum . #\x7))]
|
2007-11-07 11:24:18 -05:00
|
|
|
[(eqv? #\b c)
|
2007-11-07 11:41:28 -05:00
|
|
|
(tokenize-char-seq p "backspace" '(datum . #\x8))]
|
2007-11-07 11:24:18 -05:00
|
|
|
[(eqv? #\t c)
|
2007-11-07 11:41:28 -05:00
|
|
|
(tokenize-char-seq p "tab" '(datum . #\x9))]
|
2007-11-07 11:24:18 -05:00
|
|
|
[(eqv? #\l c)
|
2007-11-07 11:41:28 -05:00
|
|
|
(tokenize-char-seq p "linefeed" '(datum . #\xA))]
|
2007-11-07 11:24:18 -05:00
|
|
|
[(eqv? #\v c)
|
2007-11-07 11:41:28 -05:00
|
|
|
(tokenize-char-seq p "vtab" '(datum . #\xB))]
|
2007-11-07 11:24:18 -05:00
|
|
|
[(eqv? #\p c)
|
2007-11-07 11:41:28 -05:00
|
|
|
(tokenize-char-seq p "page" '(datum . #\xC))]
|
2007-11-07 11:24:18 -05:00
|
|
|
[(eqv? #\r c)
|
2007-11-07 11:41:28 -05:00
|
|
|
(tokenize-char-seq p "return" '(datum . #\xD))]
|
2007-11-07 11:24:18 -05:00
|
|
|
[(eqv? #\e c)
|
2007-11-07 11:41:28 -05:00
|
|
|
(tokenize-char-seq p "esc" '(datum . #\x1B))]
|
2007-11-07 11:24:18 -05:00
|
|
|
[(eqv? #\s c)
|
2007-11-07 11:41:28 -05:00
|
|
|
(tokenize-char-seq p "space" '(datum . #\x20))]
|
2007-11-07 11:24:18 -05:00
|
|
|
[(eqv? #\d c)
|
2007-11-07 11:41:28 -05:00
|
|
|
(tokenize-char-seq p "delete" '(datum . #\x7F))]
|
2007-11-07 11:24:18 -05:00
|
|
|
[(eqv? #\x c)
|
2007-06-01 22:17:22 -04:00
|
|
|
(let ([n (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(or (eof-object? n) (delimiter? n))
|
|
|
|
'(datum . #\x)]
|
|
|
|
[(hex n) =>
|
|
|
|
(lambda (v)
|
|
|
|
(read-char p)
|
|
|
|
(let f ([v v])
|
2007-12-05 05:01:56 -05:00
|
|
|
(let ([c (peek-char p)])
|
2007-06-01 22:17:22 -04:00
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(cons 'datum (integer->char v))]
|
|
|
|
[(delimiter? c)
|
|
|
|
(cons 'datum (integer->char v))]
|
|
|
|
[(hex c) =>
|
|
|
|
(lambda (v0)
|
2007-12-05 05:01:56 -05:00
|
|
|
(read-char p)
|
2007-06-01 22:17:22 -04:00
|
|
|
(f (+ (* v 16) v0)))]
|
|
|
|
[else
|
|
|
|
(error 'tokenize "invalid character sequence")]))))]
|
|
|
|
[else
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'tokenize "invalid character sequence"
|
|
|
|
(string-append "#\\" (string n)))]))]
|
2006-11-23 19:33:45 -05:00
|
|
|
[else
|
|
|
|
(let ([n (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? n) (cons 'datum c)]
|
|
|
|
[(delimiter? n) (cons 'datum c)]
|
|
|
|
[else
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'tokenize "invalid syntax"
|
|
|
|
(string-append "#\\" (string c n)))]))]))))
|
2007-06-01 22:17:22 -04:00
|
|
|
(define (hex x)
|
|
|
|
(cond
|
|
|
|
[(and ($char<= #\0 x) ($char<= x #\9))
|
|
|
|
($fx- ($char->fixnum x) ($char->fixnum #\0))]
|
|
|
|
[(and ($char<= #\a x) ($char<= x #\z))
|
|
|
|
($fx- ($char->fixnum x)
|
|
|
|
($fx- ($char->fixnum #\a) 10))]
|
|
|
|
[(and ($char<= #\A x) ($char<= x #\Z))
|
|
|
|
($fx- ($char->fixnum x)
|
|
|
|
($fx- ($char->fixnum #\A) 10))]
|
|
|
|
[else #f]))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define multiline-error
|
|
|
|
(lambda ()
|
|
|
|
(error 'tokenize
|
|
|
|
"end of file encountered while inside a #|-style comment")))
|
2006-12-02 10:11:57 -05:00
|
|
|
(define apprev
|
|
|
|
(lambda (str i ac)
|
|
|
|
(cond
|
|
|
|
[(fx= i (string-length str)) ac]
|
|
|
|
[else
|
|
|
|
(apprev str (fx+ i 1) (cons (string-ref str i) ac))])))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define multiline-comment
|
|
|
|
(lambda (p)
|
2006-12-02 10:11:57 -05:00
|
|
|
(define f
|
|
|
|
(lambda (p ac)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (multiline-error)]
|
|
|
|
[($char= #\| c)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (multiline-error)]
|
|
|
|
[($char= #\# c) ac]
|
|
|
|
[else (f p (cons c ac))]))]
|
|
|
|
[($char= #\# c)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (multiline-error)]
|
|
|
|
[($char= #\| c)
|
|
|
|
(let ([v (multiline-comment p)])
|
|
|
|
(if (string? v)
|
|
|
|
(f p (apprev v 0 ac))
|
|
|
|
(f p ac)))]
|
|
|
|
[else
|
|
|
|
(f p (cons c (cons #\# ac)))]))]
|
|
|
|
[else (f p (cons c ac))]))))
|
|
|
|
(let ([ac (f p '())])
|
|
|
|
((comment-handler)
|
|
|
|
(list->string (reverse ac))))))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define tokenize-hash
|
|
|
|
(lambda (p)
|
2006-12-02 05:02:05 -05:00
|
|
|
(tokenize-hash/c (read-char p) p)))
|
2006-12-25 03:18:37 -05:00
|
|
|
(define (skip-whitespace p caller)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'tokenize "invalid eof inside" caller)]
|
2006-12-25 03:18:37 -05:00
|
|
|
[(char-whitespace? c)
|
|
|
|
(skip-whitespace p caller)]
|
|
|
|
[else c])))
|
2006-12-02 05:02:05 -05:00
|
|
|
(define tokenize-hash/c
|
|
|
|
(lambda (c p)
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (error 'tokenize "invalid # near end of file")]
|
2007-05-20 22:16:57 -04:00
|
|
|
[(memq c '(#\t #\T))
|
2006-12-02 05:02:05 -05:00
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) '(datum . #t)]
|
|
|
|
[(delimiter? c) '(datum . #t)]
|
2007-10-25 14:32:26 -04:00
|
|
|
[else (error 'tokenize
|
|
|
|
(format "invalid syntax near #~a" c))]))]
|
2007-05-20 22:16:57 -04:00
|
|
|
[(memq c '(#\f #\F))
|
2006-12-02 05:02:05 -05:00
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) '(datum . #f)]
|
|
|
|
[(delimiter? c) '(datum . #f)]
|
2007-10-25 14:32:26 -04:00
|
|
|
[else (error 'tokenize
|
|
|
|
(format "invalid syntax near #~a" c))]))]
|
2006-12-02 05:02:05 -05:00
|
|
|
[($char= #\\ c) (tokenize-char p)]
|
|
|
|
[($char= #\( c) 'vparen]
|
|
|
|
[($char= #\' c) '(macro . syntax)]
|
2007-08-30 20:17:23 -04:00
|
|
|
[($char= #\` c) '(macro . quasisyntax)]
|
|
|
|
[($char= #\, c)
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eqv? c #\@) (read-char p)
|
|
|
|
'(macro . unsyntax-splicing)]
|
|
|
|
[else '(macro . unsyntax)]))]
|
2006-12-02 05:02:05 -05:00
|
|
|
[($char= #\; c) 'hash-semi]
|
|
|
|
[($char= #\% c) '(macro . |#primitive|)]
|
|
|
|
[($char= #\| c) (multiline-comment p) (tokenize p)]
|
|
|
|
[($char= #\! c)
|
|
|
|
(let ([e (read-char p)])
|
|
|
|
(when (eof-object? e)
|
|
|
|
(error 'tokenize "invalid eof near #!"))
|
2007-11-18 19:53:32 -05:00
|
|
|
(case e
|
|
|
|
[(#\e)
|
2007-11-25 16:23:39 -05:00
|
|
|
(when (eq? (port-mode p) 'r6rs-mode)
|
|
|
|
(error 'tokenize "invalid syntax: #!e"))
|
2007-11-18 19:53:32 -05:00
|
|
|
(read-char* p '(#\e) "of" "eof sequence" #f #f)
|
|
|
|
(cons 'datum (eof-object))]
|
|
|
|
[(#\r)
|
|
|
|
(read-char* p '(#\r) "6rs" "#!r6rs comment" #f #f)
|
|
|
|
(set-port-mode! p 'r6rs-mode)
|
|
|
|
(tokenize p)]
|
|
|
|
[(#\i)
|
|
|
|
(read-char* p '(#\i) "karus" "#!ikarus comment" #f #f)
|
|
|
|
(set-port-mode! p 'ikarus-mode)
|
|
|
|
(tokenize p)]
|
|
|
|
[else
|
|
|
|
(error 'tokenize
|
|
|
|
(format "invalid syntax near #!~a" e))]))]
|
2006-12-02 05:02:05 -05:00
|
|
|
[(digit? c)
|
2007-11-18 19:53:32 -05:00
|
|
|
(when (eq? (port-mode p) 'r6rs-mode)
|
|
|
|
(error 'tokenize "graph syntax is invalid in #!r6rs mode"
|
|
|
|
(format "#~a" c)))
|
2006-12-02 05:02:05 -05:00
|
|
|
(tokenize-hashnum p (char->num c))]
|
2006-12-25 03:33:03 -05:00
|
|
|
[($char= #\: c)
|
2007-11-18 19:53:32 -05:00
|
|
|
(when (eq? (port-mode p) 'r6rs-mode)
|
|
|
|
(error 'tokenize "gensym syntax is invalid in #!r6rs mode"
|
|
|
|
(format "#~a" c)))
|
2006-12-25 03:33:03 -05:00
|
|
|
(let* ([c (skip-whitespace p "gensym")]
|
|
|
|
[id0
|
|
|
|
(cond
|
|
|
|
[(initial? c)
|
|
|
|
(list->string
|
|
|
|
(reverse (tokenize-identifier (cons c '()) p)))]
|
|
|
|
[($char= #\| c)
|
|
|
|
(list->string
|
|
|
|
(reverse (tokenize-bar p '())))]
|
|
|
|
[else
|
|
|
|
(error 'tokenize
|
2007-10-25 14:32:26 -04:00
|
|
|
"invalid char inside gensym" c)])])
|
2006-12-25 03:33:03 -05:00
|
|
|
(cons 'datum (gensym id0)))]
|
2006-12-25 03:18:37 -05:00
|
|
|
[($char= #\{ c)
|
2007-11-18 19:53:32 -05:00
|
|
|
(when (eq? (port-mode p) 'r6rs-mode)
|
|
|
|
(error 'tokenize "gensym syntax is invalid in #!r6rs mode"
|
|
|
|
(format "#~a" c)))
|
2006-12-25 03:18:37 -05:00
|
|
|
(let* ([c (skip-whitespace p "gensym")]
|
|
|
|
[id0
|
|
|
|
(cond
|
|
|
|
[(initial? c)
|
|
|
|
(list->string
|
|
|
|
(reverse (tokenize-identifier (cons c '()) p)))]
|
|
|
|
[($char= #\| c)
|
|
|
|
(list->string
|
|
|
|
(reverse (tokenize-bar p '())))]
|
|
|
|
[else
|
|
|
|
(error 'tokenize
|
2007-10-25 14:32:26 -04:00
|
|
|
"invalid char inside gensym" c)])]
|
2006-12-25 03:18:37 -05:00
|
|
|
[c (skip-whitespace p "gensym")])
|
|
|
|
(cond
|
|
|
|
[($char= #\} c)
|
2007-10-30 17:45:08 -04:00
|
|
|
;(cons 'datum (gensym id0))]
|
|
|
|
(cons 'datum
|
|
|
|
(foreign-call "ikrt_strings_to_gensym" #f id0))]
|
2006-12-25 03:18:37 -05:00
|
|
|
[else
|
|
|
|
(let ([id1
|
|
|
|
(cond
|
|
|
|
[(initial? c)
|
|
|
|
(list->string
|
|
|
|
(reverse
|
|
|
|
(tokenize-identifier
|
|
|
|
(cons c '()) p)))]
|
|
|
|
[($char= #\| c)
|
|
|
|
(list->string
|
|
|
|
(reverse (tokenize-bar p '())))]
|
|
|
|
[else
|
|
|
|
(error 'tokenize
|
2007-10-25 14:32:26 -04:00
|
|
|
"invalid char inside gensym" c)])])
|
2006-12-25 03:18:37 -05:00
|
|
|
(let ([c (skip-whitespace p "gensym")])
|
|
|
|
(cond
|
|
|
|
[($char= #\} c)
|
|
|
|
(cons 'datum
|
|
|
|
(foreign-call "ikrt_strings_to_gensym"
|
|
|
|
id0 id1))]
|
|
|
|
[else
|
|
|
|
(error 'tokenize
|
2007-10-25 14:32:26 -04:00
|
|
|
"invalid char inside gensym" c)])))]))]
|
2007-05-15 23:57:35 -04:00
|
|
|
[($char= #\v c)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[($char= #\u c)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[($char= c #\8)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[($char= c #\() 'vu8]
|
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "invalid eof object after #vu8")]
|
2007-10-25 14:32:26 -04:00
|
|
|
[else (error 'tokenize
|
|
|
|
(format "invalid sequence #vu8~a" c))]))]
|
2007-05-15 23:57:35 -04:00
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "invalid eof object after #vu")]
|
2007-10-25 14:32:26 -04:00
|
|
|
[else (error 'tokenize
|
|
|
|
(format "invalid sequence #vu~a" c))]))]
|
2007-05-15 23:57:35 -04:00
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "invalid eof object after #v")]
|
2007-10-25 14:32:26 -04:00
|
|
|
[else (error 'tokenize
|
|
|
|
(format "invalid sequence #v~a" c))]))]
|
2007-06-14 11:56:47 -04:00
|
|
|
[(memq c '(#\e #\E))
|
|
|
|
(cons 'datum (tokenize-exactness-mark p (list c #\#) 'e))]
|
|
|
|
[(memq c '(#\i #\I))
|
|
|
|
(cons 'datum (tokenize-exactness-mark p (list c #\#) 'i))]
|
|
|
|
[(memq c '(#\b #\B))
|
|
|
|
(cons 'datum (tokenize-radix-mark p (list c #\#) 2))]
|
|
|
|
[(memq c '(#\x #\X))
|
|
|
|
(cons 'datum (tokenize-radix-mark p (list c #\#) 16))]
|
|
|
|
[(memq c '(#\o #\O))
|
|
|
|
(cons 'datum (tokenize-radix-mark p (list c #\#) 8))]
|
|
|
|
[(memq c '(#\d #\D))
|
|
|
|
(cons 'datum (tokenize-radix-mark p (list c #\#) 10))]
|
2006-12-25 22:21:07 -05:00
|
|
|
[($char= #\@ c)
|
2007-11-18 19:53:32 -05:00
|
|
|
(when (eq? (port-mode p) 'r6rs-mode)
|
|
|
|
(error 'tokenize "fasl syntax is invalid in #!r6rs mode"
|
|
|
|
(format "#~a" c)))
|
2007-04-29 22:29:42 -04:00
|
|
|
(error 'read "FIXME: fasl read disabled")
|
|
|
|
'(cons 'datum ($fasl-read p))]
|
2006-12-02 05:02:05 -05:00
|
|
|
[else
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'tokenize
|
|
|
|
(format "invalid syntax #~a" c))])))
|
2007-06-14 11:56:47 -04:00
|
|
|
(define (tokenize-exactness-mark p ls exact?)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (num-error "eof object" ls)]
|
|
|
|
[(radix-digit c 10) =>
|
|
|
|
(lambda (d)
|
|
|
|
(tokenize-integer p (cons c ls) exact? 10 d))]
|
|
|
|
[(char=? c #\.)
|
|
|
|
(tokenize-decimal-no-digits p (cons c ls) exact?)]
|
|
|
|
[(char=? c #\-)
|
|
|
|
(- (tokenize-integer-no-digits p (cons c ls) exact? 10))]
|
|
|
|
[(char=? c #\+)
|
|
|
|
(tokenize-integer-no-digits p (cons c ls) exact? 10)]
|
|
|
|
[(char=? c #\#)
|
|
|
|
(let ([c1 (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c1)
|
|
|
|
(num-error "eof object" (cons c ls))]
|
|
|
|
[(memv c1 '(#\b #\B))
|
2007-09-09 23:41:12 -04:00
|
|
|
(tokenize-radix/exactness-marks p (cons* c1 c ls) exact? 2)]
|
2007-06-14 11:56:47 -04:00
|
|
|
[(memv c1 '(#\x #\X))
|
2007-09-09 23:41:12 -04:00
|
|
|
(tokenize-radix/exactness-marks p (cons* c1 c ls) exact? 16)]
|
2007-06-14 11:56:47 -04:00
|
|
|
[(memv c1 '(#\o #\O))
|
2007-09-09 23:41:12 -04:00
|
|
|
(tokenize-radix/exactness-marks p (cons* c1 c ls) exact? 8)]
|
2007-06-14 11:56:47 -04:00
|
|
|
[(memv c1 '(#\d #\D))
|
2007-09-09 23:41:12 -04:00
|
|
|
(tokenize-radix/exactness-marks p (cons* c1 c ls) exact? 10)]
|
|
|
|
[else (num-error "invalid sequence" (cons* c1 c ls))]))]
|
2007-06-14 11:56:47 -04:00
|
|
|
[else (num-error "invalid sequence" (cons c ls))])))
|
|
|
|
(define (tokenize-radix-mark p ls radix)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (num-error "eof object" ls)]
|
|
|
|
[(radix-digit c radix) =>
|
|
|
|
(lambda (d)
|
|
|
|
(tokenize-integer p (cons c ls) #f radix d))]
|
|
|
|
[(char=? c #\.)
|
|
|
|
(unless (= radix 10)
|
|
|
|
(num-error "invalid decimal" (cons c ls)))
|
|
|
|
(tokenize-decimal-no-digits p (cons c ls) #f)]
|
|
|
|
[(char=? c #\-)
|
|
|
|
(- (tokenize-integer-no-digits p (cons c ls) #f radix))]
|
|
|
|
[(char=? c #\+)
|
|
|
|
(tokenize-integer-no-digits p (cons c ls) #f radix)]
|
|
|
|
[(char=? c #\#)
|
|
|
|
(let ([c1 (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c1)
|
|
|
|
(num-error "eof object" (cons c ls))]
|
|
|
|
[(memv c1 '(#\e #\E))
|
|
|
|
(tokenize-radix/exactness-marks p (cons c1 (cons c ls))
|
|
|
|
'e radix)]
|
|
|
|
[(memv c1 '(#\i #\I))
|
|
|
|
(tokenize-radix/exactness-marks p (cons c1 (cons c ls))
|
|
|
|
'i radix)]
|
2007-09-09 23:41:12 -04:00
|
|
|
[else (num-error "invalid sequence" (cons* c1 c ls))]))]
|
2007-06-14 11:56:47 -04:00
|
|
|
[else (num-error "invalid sequence" (cons c ls))])))
|
|
|
|
(define (tokenize-radix/exactness-marks p ls exact? radix)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (num-error "eof object" ls)]
|
|
|
|
[(radix-digit c radix) =>
|
|
|
|
(lambda (d)
|
|
|
|
(tokenize-integer p (cons c ls) exact? radix d))]
|
|
|
|
[(char=? c #\.)
|
|
|
|
(unless (= radix 10)
|
|
|
|
(num-error "invalid decimal" (cons c ls)))
|
|
|
|
(tokenize-decimal-no-digits p (cons c ls) exact?)]
|
|
|
|
[(char=? c #\-)
|
|
|
|
(- (tokenize-integer-no-digits p (cons c ls) exact? radix))]
|
|
|
|
[(char=? c #\+)
|
|
|
|
(tokenize-integer-no-digits p (cons c ls) exact? radix)]
|
|
|
|
[else (num-error "invalid sequence" (cons c ls))])))
|
|
|
|
(define (tokenize-integer p ls exact? radix ac)
|
2007-06-14 12:21:26 -04:00
|
|
|
(define (tokenize-denom-start p ls exact? radix num)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (num-error "eof object" ls)]
|
|
|
|
[(radix-digit c radix) =>
|
|
|
|
(lambda (d)
|
|
|
|
(tokenize-denom p (cons c ls) exact? radix num d))]
|
|
|
|
[(char=? c #\-)
|
|
|
|
(tokenize-denom-no-digits p (cons c ls) exact? radix (- num))]
|
|
|
|
[(char=? c #\+)
|
|
|
|
(tokenize-denom-no-digits p (cons c ls) exact? radix num)]
|
|
|
|
[else (num-error "invalid sequence" (cons c ls))])))
|
|
|
|
(define (tokenize-denom-no-digits p ls exact? radix num)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (num-error "eof object" ls)]
|
|
|
|
[(radix-digit c radix) =>
|
|
|
|
(lambda (d)
|
|
|
|
(tokenize-denom p (cons c ls) exact? radix num d))]
|
|
|
|
[else (num-error "invalid sequence" (cons c ls))])))
|
|
|
|
(define (tokenize-denom p ls exact? radix num ac)
|
2007-12-05 05:01:56 -05:00
|
|
|
(let ([c (peek-char p)])
|
2007-06-14 12:21:26 -04:00
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
2007-12-05 05:01:56 -05:00
|
|
|
(read-char p)
|
2007-06-14 12:21:26 -04:00
|
|
|
(if (= ac 0)
|
|
|
|
(num-error "zero denominator" ls)
|
|
|
|
(convert/exact exact? (/ num ac)))]
|
|
|
|
[(radix-digit c radix) =>
|
|
|
|
(lambda (d)
|
2007-12-05 05:01:56 -05:00
|
|
|
(read-char p)
|
2007-06-14 12:21:26 -04:00
|
|
|
(tokenize-denom p (cons c ls) exact? radix num
|
|
|
|
(+ (* radix ac) d)))]
|
|
|
|
[(delimiter? c)
|
2007-12-05 05:01:56 -05:00
|
|
|
(if (= ac 0)
|
2007-06-14 12:21:26 -04:00
|
|
|
(num-error "zero denominator" ls)
|
|
|
|
(convert/exact exact? (/ num ac)))]
|
|
|
|
[else (num-error "invalid sequence" (cons c ls))])))
|
2007-12-05 05:01:56 -05:00
|
|
|
(let ([c (peek-char p)])
|
2007-06-14 11:56:47 -04:00
|
|
|
(cond
|
|
|
|
[(eof-object? c) (convert/exact exact? ac)]
|
|
|
|
[(radix-digit c radix) =>
|
|
|
|
(lambda (d)
|
2007-12-05 05:01:56 -05:00
|
|
|
(read-char p)
|
2007-06-14 11:56:47 -04:00
|
|
|
(tokenize-integer p (cons c ls) exact? radix
|
|
|
|
(+ (* ac radix) d)))]
|
|
|
|
[(char=? c #\.)
|
|
|
|
(unless (= radix 10)
|
|
|
|
(num-error "invalid decimal" (cons c ls)))
|
2007-12-05 05:01:56 -05:00
|
|
|
(read-char p)
|
2007-06-14 11:56:47 -04:00
|
|
|
(tokenize-decimal p (cons c ls) exact? ac 0)]
|
|
|
|
[(char=? c #\/)
|
2007-12-05 05:01:56 -05:00
|
|
|
(read-char p)
|
2007-06-14 12:21:26 -04:00
|
|
|
(tokenize-denom-start p (cons #\/ ls) exact? radix ac)]
|
2007-06-14 11:56:47 -04:00
|
|
|
[(memv c '(#\e #\E)) ; exponent
|
2007-12-05 05:01:56 -05:00
|
|
|
(read-char p)
|
2007-06-14 11:56:47 -04:00
|
|
|
(unless (= radix 10)
|
|
|
|
(num-error "invalid decimal" (cons c ls)))
|
|
|
|
(let ([ex (tokenize-exponent-start p (cons c ls))])
|
2007-11-13 03:00:57 -05:00
|
|
|
(convert/exact (or exact? 'i)
|
2007-06-14 11:56:47 -04:00
|
|
|
(* ac (expt radix ex))))]
|
|
|
|
[(delimiter? c)
|
|
|
|
(convert/exact exact? ac)]
|
|
|
|
[else (num-error "invalid sequence" (cons c ls))])))
|
|
|
|
(define (tokenize-exponent-start p ls)
|
|
|
|
(define (tokenize-exponent-no-digits p ls)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (num-error "eof object" ls)]
|
|
|
|
[(radix-digit c 10) =>
|
|
|
|
(lambda (d)
|
|
|
|
(tokenize-exponent p (cons c ls) d))]
|
|
|
|
[else (num-error "invalid sequence" (cons c ls))])))
|
|
|
|
(define (tokenize-exponent p ls ac)
|
2007-12-05 05:01:56 -05:00
|
|
|
(let ([c (peek-char p)])
|
2007-06-14 11:56:47 -04:00
|
|
|
(cond
|
|
|
|
[(eof-object? c) ac]
|
|
|
|
[(radix-digit c 10) =>
|
|
|
|
(lambda (d)
|
2007-12-05 05:01:56 -05:00
|
|
|
(read-char p)
|
2007-06-14 11:56:47 -04:00
|
|
|
(tokenize-exponent p (cons c ls)
|
|
|
|
(+ (* ac 10) d)))]
|
2007-12-05 05:01:56 -05:00
|
|
|
[(delimiter? c) ac]
|
2007-06-14 11:56:47 -04:00
|
|
|
[else (num-error "invalid sequence" (cons c ls))])))
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (num-error "eof object" ls)]
|
|
|
|
[(radix-digit c 10) =>
|
|
|
|
(lambda (d)
|
|
|
|
(tokenize-exponent p (cons c ls) d))]
|
|
|
|
[(char=? c #\-)
|
|
|
|
(- (tokenize-exponent-no-digits p (cons c ls)))]
|
|
|
|
[(char=? c #\+)
|
|
|
|
(tokenize-exponent-no-digits p (cons c ls))]
|
|
|
|
[else (num-error "invalid sequence" (cons c ls))])))
|
|
|
|
(define (tokenize-decimal p ls exact? ac exp)
|
2007-12-05 05:01:56 -05:00
|
|
|
(let ([c (peek-char p)])
|
2007-06-14 11:56:47 -04:00
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(let ([ac (* ac (expt 10 exp))])
|
|
|
|
(convert/exact (or exact? 'i) ac))]
|
|
|
|
[(radix-digit c 10) =>
|
|
|
|
(lambda (d)
|
2007-12-05 05:01:56 -05:00
|
|
|
(read-char p)
|
2007-06-14 11:56:47 -04:00
|
|
|
(tokenize-decimal p (cons c ls) exact?
|
|
|
|
(+ (* ac 10) d) (- exp 1)))]
|
|
|
|
[(memv c '(#\e #\E))
|
2007-12-05 05:01:56 -05:00
|
|
|
(read-char p)
|
2007-06-14 11:56:47 -04:00
|
|
|
(let ([ex (tokenize-exponent-start p (cons c ls))])
|
|
|
|
(let ([ac (* ac (expt 10 (+ exp ex)))])
|
|
|
|
(convert/exact (or exact? 'i) ac)))]
|
|
|
|
[(delimiter? c)
|
|
|
|
(let ([ac (* ac (expt 10 exp))])
|
|
|
|
(convert/exact (or exact? 'i) ac))]
|
|
|
|
[else (num-error "invalid sequence" (cons c ls))])))
|
|
|
|
(define (tokenize-decimal-no-digits p ls exact?)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (num-error "eof object" ls)]
|
|
|
|
[(radix-digit c 10) =>
|
|
|
|
(lambda (d)
|
|
|
|
(tokenize-decimal p (cons c ls) exact? d -1))]
|
|
|
|
[else (num-error "invalid sequence" (cons c ls))])))
|
|
|
|
(define (convert/exact exact? n)
|
|
|
|
(if (eq? exact? 'i)
|
|
|
|
(exact->inexact n)
|
|
|
|
n))
|
|
|
|
(define (radix-digit c radix)
|
|
|
|
(case radix
|
|
|
|
[(10)
|
|
|
|
(cond
|
|
|
|
[(char<=? #\0 c #\9)
|
|
|
|
(fx- (char->integer c) (char->integer #\0))]
|
|
|
|
[else #f])]
|
|
|
|
[(16)
|
|
|
|
(cond
|
|
|
|
[(char<=? #\0 c #\9)
|
|
|
|
(fx- (char->integer c) (char->integer #\0))]
|
|
|
|
[(char<=? #\a c #\f)
|
|
|
|
(fx- (char->integer c) (fx- (char->integer #\a) 10))]
|
|
|
|
[(char<=? #\A c #\F)
|
|
|
|
(fx- (char->integer c) (fx- (char->integer #\A) 10))]
|
|
|
|
[else #f])]
|
|
|
|
[(8)
|
|
|
|
(cond
|
|
|
|
[(char<=? #\0 c #\7)
|
|
|
|
(fx- (char->integer c) (char->integer #\0))]
|
|
|
|
[else #f])]
|
|
|
|
[(2)
|
|
|
|
(case c
|
|
|
|
[(#\0) 0]
|
|
|
|
[(#\1) 1]
|
|
|
|
[else #f])]
|
2007-10-25 14:32:26 -04:00
|
|
|
[else (error 'radix-digit "invalid radix" radix)]))
|
2007-11-18 19:53:32 -05:00
|
|
|
(define (read-char* p ls str who ci? delimited?)
|
|
|
|
(let f ([i 0] [ls ls])
|
|
|
|
(cond
|
|
|
|
[(fx= i (string-length str))
|
|
|
|
(when delimited?
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(when (and (not (eof-object? c)) (not (delimiter? c)))
|
|
|
|
(error 'tokenize
|
|
|
|
(format "invalid ~a: ~s" who
|
|
|
|
(list->string (reverse (cons c ls))))))))]
|
|
|
|
[else
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
2007-12-05 05:01:56 -05:00
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize
|
|
|
|
(format "invalid eof inside ~a" who))]
|
|
|
|
[(or (and (not ci?) (char=? c (string-ref str i)))
|
|
|
|
(and ci? (char=? (char-downcase c) (string-ref str i))))
|
|
|
|
(f (add1 i) (cons c ls))]
|
|
|
|
[else
|
|
|
|
(error 'tokenize
|
|
|
|
(format "invalid ~a: ~s" who
|
|
|
|
(list->string (reverse (cons c ls)))))]))])))
|
2007-09-11 03:06:35 -04:00
|
|
|
(define (tokenize-integer/nan/inf-no-digits p ls)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (num-error "invalid eof" ls)]
|
|
|
|
[(radix-digit c 10) =>
|
|
|
|
(lambda (d)
|
|
|
|
(tokenize-integer p (cons c ls) #f 10 d))]
|
|
|
|
[(char=? c #\.)
|
|
|
|
(tokenize-decimal-no-digits p (cons c ls) #f)]
|
2007-11-12 14:12:01 -05:00
|
|
|
[(memv c '(#\i #\I))
|
2007-11-18 19:53:32 -05:00
|
|
|
(read-char* p (cons #\i ls) "nf.0" "number sequence" #t #t)
|
2007-09-11 03:13:28 -04:00
|
|
|
+inf.0]
|
2007-11-12 14:12:01 -05:00
|
|
|
[(memv c '(#\n #\N))
|
2007-11-18 19:53:32 -05:00
|
|
|
(read-char* p (cons #\i ls) "an.0" "number sequence" #t #t)
|
2007-09-11 03:13:28 -04:00
|
|
|
+nan.0]
|
2007-09-11 03:06:35 -04:00
|
|
|
[else (num-error "invalid sequence" (cons c ls))])))
|
2007-06-14 11:56:47 -04:00
|
|
|
(define (tokenize-integer-no-digits p ls exact? radix?)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (num-error "invalid eof" ls)]
|
|
|
|
[(radix-digit c (or radix? 10)) =>
|
|
|
|
(lambda (d)
|
|
|
|
(tokenize-integer p (cons c ls) exact? (or radix? 10) d))]
|
|
|
|
[(char=? c #\.)
|
|
|
|
(when (and radix? (not (= radix? 10)))
|
|
|
|
(num-error "invalid decimal" (cons c ls)))
|
|
|
|
(tokenize-decimal-no-digits p (cons c ls) exact?)]
|
|
|
|
[else (num-error "invalid sequence" (cons c ls))])))
|
|
|
|
(define (num-error str ls)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'read "invalid numeric sequence"
|
2007-06-14 11:56:47 -04:00
|
|
|
(list->string (reverse ls))))
|
2006-11-23 19:48:14 -05:00
|
|
|
(define (tokenize-hashnum p n)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "invalid eof inside #n mark/ref")]
|
|
|
|
[($char= #\= c) (cons 'mark n)]
|
|
|
|
[($char= #\# c) (cons 'ref n)]
|
|
|
|
[(digit? c)
|
|
|
|
(tokenize-hashnum p (fx+ (fx* n 10) (char->num c)))]
|
|
|
|
[else
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'tokenize "invalid char while inside a #n mark/ref" c)])))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define tokenize-bar
|
|
|
|
(lambda (p ac)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "unexpected eof while reading symbol")]
|
2006-11-23 19:48:14 -05:00
|
|
|
[($char= #\\ c)
|
2006-11-23 19:33:45 -05:00
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "unexpected eof while reading symbol")]
|
|
|
|
[else (tokenize-bar p (cons c ac))]))]
|
2006-11-23 19:48:14 -05:00
|
|
|
[($char= #\| c) ac]
|
2006-11-23 19:33:45 -05:00
|
|
|
[else (tokenize-bar p (cons c ac))]))))
|
2007-11-19 02:00:26 -05:00
|
|
|
(define (tokenize-backslash main-ac p)
|
2007-06-01 22:17:22 -04:00
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
2007-11-19 02:00:26 -05:00
|
|
|
(error 'tokenize "invalid eof after symbol escape")]
|
2007-06-01 22:17:22 -04:00
|
|
|
[($char= #\x c)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "invalid eof after \\x")]
|
|
|
|
[(hex c) =>
|
|
|
|
(lambda (v)
|
|
|
|
(let f ([v v] [ac `(,c #\x #\\)])
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'tokenize
|
|
|
|
(format "invalid eof after ~a"
|
|
|
|
(list->string (reverse ac))))]
|
2007-06-01 22:17:22 -04:00
|
|
|
[($char= #\; c)
|
2007-11-19 02:00:26 -05:00
|
|
|
(tokenize-identifier (cons (integer->char v) main-ac) p)]
|
2007-06-01 22:17:22 -04:00
|
|
|
[(hex c) =>
|
|
|
|
(lambda (v0)
|
|
|
|
(f (+ (* v 16) v0) (cons c ac)))]
|
|
|
|
[else
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'tokenize "invalid sequence"
|
2007-06-01 22:17:22 -04:00
|
|
|
(list->string (cons c (reverse ac))))]))))]
|
|
|
|
[else
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'tokenize
|
|
|
|
(format "invalid sequence \\x~a" c))]))]
|
2007-06-01 22:17:22 -04:00
|
|
|
[else
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'tokenize
|
|
|
|
(format "invalid sequence \\~a" c))])))
|
2006-12-02 05:02:05 -05:00
|
|
|
(define tokenize/c
|
|
|
|
(lambda (c p)
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (eof-object)]
|
|
|
|
[(char-whitespace? c) (tokenize p)]
|
|
|
|
[($char= #\( c) 'lparen]
|
|
|
|
[($char= #\) c) 'rparen]
|
|
|
|
[($char= #\[ c) 'lbrack]
|
|
|
|
[($char= #\] c) 'rbrack]
|
|
|
|
[($char= #\' c) '(macro . quote)]
|
|
|
|
[($char= #\` c) '(macro . quasiquote)]
|
|
|
|
[($char= #\, c)
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) '(macro . unquote)]
|
2007-05-20 13:11:33 -04:00
|
|
|
[($char= c #\@)
|
2006-12-02 05:02:05 -05:00
|
|
|
(read-char p)
|
|
|
|
'(macro . unquote-splicing)]
|
|
|
|
[else '(macro . unquote)]))]
|
|
|
|
[($char= #\# c) (tokenize-hash p)]
|
2007-06-14 11:56:47 -04:00
|
|
|
[(radix-digit c 10) =>
|
|
|
|
(lambda (d)
|
|
|
|
(cons 'datum
|
|
|
|
(tokenize-integer p (list c) #f 10 d)))]
|
2007-12-02 20:06:00 -05:00
|
|
|
[(initial? c) ;;; HERE
|
2006-12-02 05:02:05 -05:00
|
|
|
(let ([ls (reverse (tokenize-identifier (cons c '()) p))])
|
|
|
|
(cons 'datum (string->symbol (list->string ls))))]
|
2007-05-20 13:11:33 -04:00
|
|
|
[($char= #\" c)
|
2006-12-02 05:02:05 -05:00
|
|
|
(let ([ls (tokenize-string '() p)])
|
|
|
|
(cons 'datum (list->string (reverse ls))))]
|
|
|
|
[($char= #\; c)
|
|
|
|
(skip-comment p)
|
|
|
|
(tokenize p)]
|
2007-05-20 22:16:57 -04:00
|
|
|
[(memq c '(#\+))
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) '(datum . +)]
|
|
|
|
[(delimiter? c) '(datum . +)]
|
2007-06-14 11:56:47 -04:00
|
|
|
[else
|
|
|
|
(cons 'datum
|
2007-09-11 03:06:35 -04:00
|
|
|
(tokenize-integer/nan/inf-no-digits p '(#\+)))]))]
|
2007-05-20 22:16:57 -04:00
|
|
|
[(memq c '(#\-))
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) '(datum . -)]
|
|
|
|
[(delimiter? c) '(datum . -)]
|
|
|
|
[($char= c #\>)
|
|
|
|
(read-char p)
|
|
|
|
(let ([ls (tokenize-identifier '() p)])
|
2007-09-09 23:41:12 -04:00
|
|
|
(let ([str (list->string (cons* #\- #\> (reverse ls)))])
|
2007-05-20 22:16:57 -04:00
|
|
|
(cons 'datum (string->symbol str))))]
|
2007-06-14 11:56:47 -04:00
|
|
|
[else
|
|
|
|
(cons 'datum
|
2007-09-11 03:06:35 -04:00
|
|
|
(- (tokenize-integer/nan/inf-no-digits p '(#\-))))]))]
|
2006-12-02 05:02:05 -05:00
|
|
|
[($char= #\. c)
|
|
|
|
(tokenize-dot p)]
|
|
|
|
[($char= #\| c)
|
2007-11-19 01:47:46 -05:00
|
|
|
(when (eq? (port-mode p) 'r6rs-mode)
|
|
|
|
(error 'tokenize "|symbol| syntax is invalid in #!r6rs mode"))
|
2006-12-02 05:02:05 -05:00
|
|
|
(let ([ls (reverse (tokenize-bar p '()))])
|
|
|
|
(cons 'datum (string->symbol (list->string ls))))]
|
2007-06-01 22:17:22 -04:00
|
|
|
[($char= #\\ c)
|
2007-11-19 02:00:26 -05:00
|
|
|
(cons 'datum
|
|
|
|
(string->symbol
|
|
|
|
(list->string
|
|
|
|
(reverse (tokenize-backslash '() p)))))]
|
2006-12-02 05:02:05 -05:00
|
|
|
[else
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'tokenize "invalid syntax" c)])))
|
2006-12-02 05:02:05 -05:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define tokenize
|
2006-12-02 05:02:05 -05:00
|
|
|
(lambda (p)
|
|
|
|
(tokenize/c (read-char p) p)))
|
|
|
|
|
|
|
|
(define tokenize-initial
|
2006-11-23 19:33:45 -05:00
|
|
|
(lambda (p)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
2006-12-02 05:02:05 -05:00
|
|
|
[(eof-object? c) c]
|
|
|
|
[($char= #\# c)
|
|
|
|
(let ([c (read-char p)])
|
2006-11-23 19:33:45 -05:00
|
|
|
(cond
|
2006-12-02 05:02:05 -05:00
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize "invalid eof after #")]
|
|
|
|
[($char= #\! c)
|
|
|
|
(skip-comment p)
|
|
|
|
(tokenize p)]
|
|
|
|
[else
|
|
|
|
(tokenize-hash/c c p)]))]
|
|
|
|
[else (tokenize/c c p)]))))
|
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define read-list-rest
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (p locs k end mis)
|
2007-05-05 20:47:31 -04:00
|
|
|
(let ([t (tokenize p)])
|
2006-11-23 19:33:45 -05:00
|
|
|
(cond
|
|
|
|
[(eof-object? t)
|
|
|
|
(error 'read "end of file encountered while reading list")]
|
2006-11-23 19:48:14 -05:00
|
|
|
[(eq? t end) (values '() locs k)]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(eq? t mis)
|
|
|
|
(error 'read "paren mismatch")]
|
|
|
|
[(eq? t 'dot)
|
2006-11-23 19:48:14 -05:00
|
|
|
(let-values ([(d locs k) (read-expr p locs k)])
|
2007-05-05 20:47:31 -04:00
|
|
|
(let ([t (tokenize p)])
|
2006-11-23 19:33:45 -05:00
|
|
|
(cond
|
2006-11-23 19:48:14 -05:00
|
|
|
[(eq? t end) (values d locs k)]
|
|
|
|
[(eq? t mis)
|
2006-11-23 19:33:45 -05:00
|
|
|
(error 'read "paren mismatch")]
|
|
|
|
[(eq? t 'dot)
|
|
|
|
(error 'read "cannot have two dots in a list")]
|
|
|
|
[else
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'read
|
|
|
|
(format "expecting ~a, got ~a" end t))])))]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(eq? t 'hash-semi)
|
2006-11-23 19:48:14 -05:00
|
|
|
(let-values ([(ignored locs k) (read-expr p locs k)])
|
|
|
|
(read-list-rest p locs k end mis))]
|
2006-11-23 19:33:45 -05:00
|
|
|
[else
|
2006-11-23 19:48:14 -05:00
|
|
|
(let-values ([(a locs k) (parse-token p locs k t)])
|
|
|
|
(let-values ([(d locs k) (read-list-rest p locs k end mis)])
|
|
|
|
(let ([x (cons a d)])
|
|
|
|
(values x locs
|
|
|
|
(if (or (loc? a) (loc? d))
|
|
|
|
(extend-k-pair x k)
|
|
|
|
k)))))]))))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define read-list-init
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (p locs k end mis)
|
2007-05-05 20:47:31 -04:00
|
|
|
(let ([t (tokenize p)])
|
2006-11-23 19:33:45 -05:00
|
|
|
(cond
|
|
|
|
[(eof-object? t)
|
|
|
|
(error 'read "end of file encountered while reading list")]
|
2006-11-23 19:48:14 -05:00
|
|
|
[(eq? t end) (values '() locs k)]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(eq? t mis)
|
|
|
|
(error 'read "paren mismatch")]
|
|
|
|
[(eq? t 'dot)
|
|
|
|
(error 'read "invalid dot while reading list")]
|
|
|
|
[(eq? t 'hash-semi)
|
2006-11-23 19:48:14 -05:00
|
|
|
(let-values ([(ignored locs k) (read-expr p locs k)])
|
|
|
|
(read-list-init p locs k end mis))]
|
2006-11-23 19:33:45 -05:00
|
|
|
[else
|
2006-11-23 19:48:14 -05:00
|
|
|
(let-values ([(a locs k) (parse-token p locs k t)])
|
|
|
|
(let-values ([(d locs k) (read-list-rest p locs k end mis)])
|
|
|
|
(let ([x (cons a d)])
|
|
|
|
(values x locs
|
|
|
|
(if (or (loc? a) (loc? d))
|
|
|
|
(extend-k-pair x k)
|
|
|
|
k)))))]))))
|
|
|
|
(define extend-k-pair
|
|
|
|
(lambda (x k)
|
|
|
|
(lambda ()
|
|
|
|
(let ([a (car x)])
|
|
|
|
(when (loc? a)
|
|
|
|
(set-car! x (loc-value a))))
|
|
|
|
(let ([d (cdr x)])
|
|
|
|
(when (loc? d)
|
|
|
|
(set-cdr! x (loc-value d))))
|
|
|
|
(k))))
|
|
|
|
(define vector-put
|
|
|
|
(lambda (v k i ls)
|
2006-11-23 19:33:45 -05:00
|
|
|
(cond
|
2006-11-23 19:48:14 -05:00
|
|
|
[(null? ls) k]
|
2006-11-23 19:33:45 -05:00
|
|
|
[else
|
2006-11-23 19:48:14 -05:00
|
|
|
(let ([a (car ls)])
|
|
|
|
(vector-set! v i a)
|
|
|
|
(vector-put v
|
|
|
|
(if (loc? a)
|
|
|
|
(lambda ()
|
|
|
|
(vector-set! v i (loc-value (vector-ref v i)))
|
|
|
|
(k))
|
|
|
|
k)
|
|
|
|
(fxsub1 i) (cdr ls)))])))
|
2007-05-15 23:57:35 -04:00
|
|
|
(define bytevector-put
|
|
|
|
(lambda (v k i ls)
|
|
|
|
(cond
|
|
|
|
[(null? ls) k]
|
|
|
|
[else
|
|
|
|
(let ([a (car ls)])
|
|
|
|
(cond
|
|
|
|
[(fixnum? a)
|
|
|
|
(unless (and (fx<= 0 a) (fx<= a 255))
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'read
|
|
|
|
(format "invalid value ~s in a bytevector" a)))
|
2007-05-15 23:57:35 -04:00
|
|
|
($bytevector-set! v i a)
|
|
|
|
(bytevector-put v k ($fxsub1 i) ($cdr ls))]
|
2007-10-25 14:32:26 -04:00
|
|
|
[else (error 'read "invalid value inside a bytevector" a)]))])))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define read-vector
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (p locs k count ls)
|
2007-05-05 20:47:31 -04:00
|
|
|
(let ([t (tokenize p)])
|
2006-11-23 19:33:45 -05:00
|
|
|
(cond
|
2006-11-23 19:48:14 -05:00
|
|
|
[(eof-object? t)
|
|
|
|
(error 'read "end of file encountered while reading a vector")]
|
|
|
|
[(eq? t 'rparen)
|
|
|
|
(let ([v (make-vector count)])
|
|
|
|
(let ([k (vector-put v k (fxsub1 count) ls)])
|
|
|
|
(values v locs k)))]
|
|
|
|
[(eq? t 'rbrack)
|
|
|
|
(error 'read "unexpected ] while reading a vector")]
|
|
|
|
[(eq? t 'dot)
|
|
|
|
(error 'read "unexpected . while reading a vector")]
|
|
|
|
[(eq? t 'hash-semi)
|
|
|
|
(let-values ([(ignored locs k) (read-expr p locs k)])
|
|
|
|
(read-vector p locs k count ls))]
|
|
|
|
[else
|
|
|
|
(let-values ([(a locs k) (parse-token p locs k t)])
|
|
|
|
(read-vector p locs k (fxadd1 count) (cons a ls)))]))))
|
2007-05-15 23:57:35 -04:00
|
|
|
(define read-bytevector
|
|
|
|
(lambda (p locs k count ls)
|
|
|
|
(let ([t (tokenize p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? t)
|
|
|
|
(error 'read "end of file encountered while reading a bytevector")]
|
|
|
|
[(eq? t 'rparen)
|
|
|
|
(let ([v ($make-bytevector count)])
|
|
|
|
(let ([k (bytevector-put v k (fxsub1 count) ls)])
|
|
|
|
(values v locs k)))]
|
|
|
|
[(eq? t 'rbrack)
|
|
|
|
(error 'read "unexpected ] while reading a bytevector")]
|
|
|
|
[(eq? t 'dot)
|
|
|
|
(error 'read "unexpected . while reading a bytevector")]
|
|
|
|
[(eq? t 'hash-semi)
|
|
|
|
(let-values ([(ignored locs k) (read-expr p locs k)])
|
|
|
|
(read-bytevector p locs k count ls))]
|
|
|
|
[else
|
|
|
|
(let-values ([(a locs k) (parse-token p locs k t)])
|
|
|
|
(read-bytevector p locs k (fxadd1 count) (cons a ls)))]))))
|
2007-10-12 02:59:27 -04:00
|
|
|
(define-struct loc (value set?))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define parse-token
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (p locs k t)
|
2006-11-23 19:33:45 -05:00
|
|
|
(cond
|
2006-11-23 19:48:14 -05:00
|
|
|
[(eof-object? t) (values (eof-object) locs k)]
|
|
|
|
[(eq? t 'lparen) (read-list-init p locs k 'rparen 'rbrack)]
|
|
|
|
[(eq? t 'lbrack) (read-list-init p locs k 'rbrack 'rparen)]
|
|
|
|
[(eq? t 'vparen) (read-vector p locs k 0 '())]
|
|
|
|
[(eq? t 'hash-semi)
|
|
|
|
(let-values ([(ignored locs k) (read-expr p locs k)])
|
|
|
|
(read-expr p locs k))]
|
2007-05-15 23:57:35 -04:00
|
|
|
[(eq? t 'vu8) (read-bytevector p locs k 0 '())]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(pair? t)
|
|
|
|
(cond
|
2006-11-23 19:48:14 -05:00
|
|
|
[(eq? (car t) 'datum) (values (cdr t) locs k)]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(eq? (car t) 'macro)
|
2006-11-23 19:48:14 -05:00
|
|
|
(let-values ([(expr locs k) (read-expr p locs k)])
|
2007-12-01 04:31:48 -05:00
|
|
|
(when (eof-object? expr)
|
|
|
|
(error 'read
|
|
|
|
(format "invalid eof after ~a read macro"
|
|
|
|
(cdr t))))
|
2006-11-23 19:48:14 -05:00
|
|
|
(let ([x (list expr)])
|
|
|
|
(values (cons (cdr t) x) locs
|
|
|
|
(if (loc? expr)
|
|
|
|
(lambda ()
|
|
|
|
(set-car! x (loc-value expr))
|
|
|
|
(k))
|
|
|
|
k))))]
|
|
|
|
[(eq? (car t) 'mark)
|
|
|
|
(let ([n (cdr t)])
|
|
|
|
(let-values ([(expr locs k) (read-expr p locs k)])
|
|
|
|
(cond
|
|
|
|
[(assq n locs) =>
|
|
|
|
(lambda (x)
|
|
|
|
(let ([loc (cdr x)])
|
|
|
|
(when (loc-set? loc)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'read "duplicate mark" n))
|
2006-11-23 19:48:14 -05:00
|
|
|
(set-loc-value! loc expr)
|
|
|
|
(set-loc-set?! loc #t)
|
|
|
|
(values expr locs k)))]
|
|
|
|
[else
|
|
|
|
(let ([loc (make-loc expr #t)])
|
|
|
|
(let ([locs (cons (cons n loc) locs)])
|
|
|
|
(values expr locs k)))])))]
|
|
|
|
[(eq? (car t) 'ref)
|
|
|
|
(let ([n (cdr t)])
|
|
|
|
(cond
|
|
|
|
[(assq n locs) =>
|
|
|
|
(lambda (x)
|
|
|
|
(values (cdr x) locs k))]
|
|
|
|
[else
|
|
|
|
(let ([loc (make-loc #f #f)])
|
|
|
|
(let ([locs (cons (cons n loc) locs)])
|
|
|
|
(values loc locs k)))]))]
|
2007-10-25 14:32:26 -04:00
|
|
|
[else (error 'read "invalid token" t)])]
|
2006-11-23 19:33:45 -05:00
|
|
|
[else
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'read
|
|
|
|
(format "unexpected ~s found" t))])))
|
2006-12-02 05:02:05 -05:00
|
|
|
|
2006-11-23 19:48:14 -05:00
|
|
|
(define read-expr
|
|
|
|
(lambda (p locs k)
|
2007-05-05 20:47:31 -04:00
|
|
|
(parse-token p locs k (tokenize p))))
|
2006-12-02 05:02:05 -05:00
|
|
|
|
|
|
|
(define read-expr-initial
|
|
|
|
(lambda (p locs k)
|
|
|
|
(parse-token p locs k (tokenize-initial p))))
|
|
|
|
|
2006-11-23 19:48:14 -05:00
|
|
|
(define reduce-loc!
|
|
|
|
(lambda (x)
|
|
|
|
(let ([loc (cdr x)])
|
|
|
|
(unless (loc-set? loc)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'read "referenced mark is not set" (car x)))
|
2006-11-23 19:48:14 -05:00
|
|
|
(when (loc? (loc-value loc))
|
|
|
|
(let f ([h loc] [t loc])
|
|
|
|
(if (loc? h)
|
|
|
|
(let ([h1 (loc-value h)])
|
|
|
|
(if (loc? h1)
|
|
|
|
(begin
|
|
|
|
(when (eq? h1 t)
|
|
|
|
(error 'read "circular marks"))
|
|
|
|
(let ([v (f (loc-value h1) (loc-value t))])
|
|
|
|
(set-loc-value! h1 v)
|
|
|
|
(set-loc-value! h v)
|
|
|
|
v))
|
|
|
|
(begin
|
|
|
|
(set-loc-value! h h1)
|
|
|
|
h1)))
|
|
|
|
h))))))
|
|
|
|
|
2007-04-29 22:29:42 -04:00
|
|
|
(define my-read
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (p)
|
|
|
|
(let-values ([(expr locs k) (read-expr p '() void)])
|
|
|
|
(cond
|
|
|
|
[(null? locs) expr]
|
|
|
|
[else
|
|
|
|
(for-each reduce-loc! locs)
|
|
|
|
(k)
|
|
|
|
(if (loc? expr)
|
|
|
|
(loc-value expr)
|
|
|
|
expr)]))))
|
|
|
|
|
2006-12-02 05:02:05 -05:00
|
|
|
(define read-initial
|
|
|
|
(lambda (p)
|
|
|
|
(let-values ([(expr locs k) (read-expr-initial p '() void)])
|
|
|
|
(cond
|
|
|
|
[(null? locs) expr]
|
|
|
|
[else
|
|
|
|
(for-each reduce-loc! locs)
|
|
|
|
(k)
|
|
|
|
(if (loc? expr)
|
|
|
|
(loc-value expr)
|
|
|
|
expr)]))))
|
2006-11-23 19:48:14 -05:00
|
|
|
|
2007-05-05 20:47:31 -04:00
|
|
|
(define read-token
|
2006-11-23 19:48:14 -05:00
|
|
|
(case-lambda
|
|
|
|
[() (tokenize (current-input-port))]
|
|
|
|
[(p)
|
|
|
|
(if (input-port? p)
|
|
|
|
(tokenize p)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'read-token "not an input port" p))]))
|
2007-05-05 20:47:31 -04:00
|
|
|
|
|
|
|
(define read
|
2006-11-23 19:48:14 -05:00
|
|
|
(case-lambda
|
2007-04-29 22:29:42 -04:00
|
|
|
[() (my-read (current-input-port))]
|
2006-11-23 19:48:14 -05:00
|
|
|
[(p)
|
|
|
|
(if (input-port? p)
|
2007-04-29 22:29:42 -04:00
|
|
|
(my-read p)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'read "not an input port" p))]))
|
2007-05-05 20:47:31 -04:00
|
|
|
|
2007-11-22 14:43:39 -05:00
|
|
|
(define (get-datum p)
|
|
|
|
(unless (input-port? p)
|
|
|
|
(error 'get-datum "not an input port"))
|
|
|
|
(my-read p))
|
|
|
|
|
2007-05-05 20:47:31 -04:00
|
|
|
(define comment-handler
|
2006-12-02 10:11:57 -05:00
|
|
|
(make-parameter
|
|
|
|
(lambda (x) (void))
|
|
|
|
(lambda (x)
|
|
|
|
(unless (procedure? x)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'comment-handler "not a procedure" x))
|
2006-12-02 10:11:57 -05:00
|
|
|
x)))
|
2007-05-05 20:47:31 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
)
|
|
|
|
|