2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
2008-01-29 00:34:34 -05:00
|
|
|
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
|
2007-10-25 16:27:34 -04:00
|
|
|
;;;
|
|
|
|
;;; 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
|
|
|
|
2008-05-21 02:21:37 -04:00
|
|
|
(library (ikarus.reader)
|
2007-12-19 00:40:25 -05:00
|
|
|
(export read read-initial read-token comment-handler get-datum
|
|
|
|
read-annotated read-script-annotated annotation?
|
|
|
|
annotation-expression annotation-source
|
2008-05-21 02:21:37 -04:00
|
|
|
annotation-stripped)
|
2007-05-05 20:47:31 -04:00
|
|
|
(import
|
2008-06-02 03:01:59 -04:00
|
|
|
(only (ikarus.string-to-number) define-string->number-parser)
|
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)
|
2009-07-29 12:07:03 -04:00
|
|
|
(only (ikarus.io) input-port-byte-position
|
|
|
|
input-port-column-number)
|
2007-12-19 00:40:25 -05:00
|
|
|
(except (ikarus) read-char read read-token comment-handler get-datum
|
|
|
|
read-annotated read-script-annotated annotation?
|
2009-08-03 03:36:18 -04:00
|
|
|
annotation-expression annotation-source annotation-stripped
|
|
|
|
input-port-column-number))
|
2007-12-13 05:57:15 -05:00
|
|
|
|
2008-11-14 03:12:18 -05:00
|
|
|
(define (die/lex id pos who msg arg*)
|
2007-12-18 19:52:15 -05:00
|
|
|
(raise
|
|
|
|
(condition
|
|
|
|
(make-lexical-violation)
|
|
|
|
(make-message-condition msg)
|
|
|
|
(if (null? arg*)
|
|
|
|
(condition)
|
|
|
|
(make-irritants-condition arg*))
|
2008-12-27 00:36:13 -05:00
|
|
|
(make-source-position-condition
|
2008-11-14 03:12:18 -05:00
|
|
|
id pos))))
|
|
|
|
(define (die/pos p off who msg arg*)
|
|
|
|
(die/lex (port-id p)
|
|
|
|
(let ([pos (input-port-byte-position p)])
|
|
|
|
(and pos (+ pos off)))
|
|
|
|
who msg arg*))
|
2007-12-18 19:52:15 -05:00
|
|
|
(define (die/p p who msg . arg*)
|
|
|
|
(die/pos p 0 who msg arg*))
|
2007-12-19 19:33:05 -05:00
|
|
|
(define (die/p-1 p who msg . arg*)
|
|
|
|
(die/pos p -1 who msg arg*))
|
2008-11-14 03:12:18 -05:00
|
|
|
(define (die/ann ann who msg . arg*)
|
|
|
|
(let ([src (annotation-source ann)])
|
|
|
|
(die/lex (car src) (cdr src) who msg arg*)))
|
2007-12-18 19:52:15 -05:00
|
|
|
|
|
|
|
|
2008-04-28 15:01:45 -04:00
|
|
|
(define (checked-integer->char n ac p)
|
|
|
|
(define (valid-integer-char? n)
|
|
|
|
(cond
|
|
|
|
[(<= n #xD7FF) #t]
|
|
|
|
[(< n #xE000) #f]
|
|
|
|
[(<= n #x10FFFF) #t]
|
|
|
|
[else #f]))
|
|
|
|
(if (valid-integer-char? n)
|
|
|
|
($fixnum->char n)
|
|
|
|
(die/p p 'tokenize
|
|
|
|
"invalid numeric value for character"
|
|
|
|
(list->string (reverse ac)))))
|
|
|
|
|
2007-12-13 05:57:15 -05:00
|
|
|
(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)
|
2009-07-29 12:07:03 -04: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 special-initial?
|
|
|
|
(lambda (c)
|
|
|
|
(memq c '(#\! #\$ #\% #\& #\* #\/ #\: #\< #\= #\> #\? #\^ #\_ #\~))))
|
|
|
|
(define special-subsequent?
|
|
|
|
(lambda (c)
|
|
|
|
(memq c '(#\+ #\- #\. #\@))))
|
2008-08-07 18:02:53 -04:00
|
|
|
(define subsequent?
|
|
|
|
(lambda (c)
|
|
|
|
(cond
|
|
|
|
[($char<= c ($fixnum->char 127))
|
|
|
|
(or (letter? c)
|
|
|
|
(digit? c)
|
|
|
|
(special-initial? c)
|
|
|
|
(special-subsequent? c))]
|
|
|
|
[else
|
|
|
|
(or (unicode-printable-char? c)
|
|
|
|
(memq (char-general-category c) '(Nd Mc Me)))])))
|
2006-11-23 19:33:45 -05:00
|
|
|
(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)
|
2008-11-07 21:53:55 -05:00
|
|
|
(read-char p)
|
|
|
|
(tokenize-identifier (cons c 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)]
|
2009-07-29 12:07:03 -04:00
|
|
|
[(eq? (port-mode p) 'r6rs-mode)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid identifier syntax"
|
2009-07-29 12:07:03 -04:00
|
|
|
(list->string (reverse (cons c ls))))]
|
|
|
|
[else ls]))))
|
2007-12-02 06:58:33 -05:00
|
|
|
(define (tokenize-string ls p)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid eof inside string")]
|
2007-12-02 06:58:33 -05:00
|
|
|
[else (tokenize-string-char ls p c)])))
|
2009-06-26 03:07:26 -04:00
|
|
|
(define LF1 '(#\xA #\x85 #\x2028)) ;;; these are considered newlines
|
|
|
|
(define LF2 '(#\xA #\x85)) ;;; these are not newlines if they
|
|
|
|
;;; appear after CR
|
2007-12-02 06:58:33 -05:00
|
|
|
(define (tokenize-string-char ls p c)
|
|
|
|
(define (intraline-whitespace? c)
|
2009-06-26 03:07:26 -04:00
|
|
|
(or (eqv? c #\x9)
|
2007-12-02 06:58:33 -05:00
|
|
|
(eq? (char-general-category c) 'Zs)))
|
|
|
|
(define (tokenize-string-continue ls p c)
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid eof inside string")]
|
2007-12-02 06:58:33 -05:00
|
|
|
[(intraline-whitespace? c)
|
|
|
|
(let f ()
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid eof inside string")]
|
2007-12-02 06:58:33 -05:00
|
|
|
[(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)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid eof after string escape")]
|
2007-12-02 06:58:33 -05:00
|
|
|
[($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)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid eof inside string")]
|
2007-12-02 06:58:33 -05:00
|
|
|
[(hex c) =>
|
|
|
|
(lambda (n)
|
2008-04-28 15:01:45 -04:00
|
|
|
(let f ([n n] [ac (cons c '(#\x))])
|
2007-12-02 06:58:33 -05:00
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? n)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid eof inside string")]
|
2007-12-02 06:58:33 -05:00
|
|
|
[(hex c) =>
|
2008-04-28 15:01:45 -04:00
|
|
|
(lambda (v) (f (+ (* n 16) v) (cons c ac)))]
|
2007-12-02 06:58:33 -05:00
|
|
|
[($char= c #\;)
|
|
|
|
(tokenize-string
|
2008-04-28 15:01:45 -04:00
|
|
|
(cons (checked-integer->char n ac p) ls) p)]
|
2007-12-02 06:58:33 -05:00
|
|
|
[else
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p 'tokenize
|
2007-12-02 06:58:33 -05:00
|
|
|
"invalid char in escape sequence"
|
2008-04-28 15:01:45 -04:00
|
|
|
(list->string (reverse (cons c ac))))]))))]
|
2007-12-02 06:58:33 -05:00
|
|
|
[else
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p 'tokenize
|
2007-12-02 06:58:33 -05:00
|
|
|
"invalid char in escape sequence" c)]))]
|
|
|
|
[(intraline-whitespace? c)
|
|
|
|
(let f ()
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid eof inside string")]
|
2007-12-02 06:58:33 -05:00
|
|
|
[(intraline-whitespace? c) (f)]
|
2009-06-26 03:07:26 -04:00
|
|
|
[(memv c LF1)
|
2007-12-02 06:58:33 -05:00
|
|
|
(tokenize-string-continue ls p (read-char p))]
|
2009-06-26 03:07:26 -04:00
|
|
|
[(eqv? c #\return)
|
2007-12-02 06:58:33 -05:00
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
2009-06-26 03:07:26 -04:00
|
|
|
[(memv c LF2)
|
2007-12-02 06:58:33 -05:00
|
|
|
(tokenize-string-continue ls p (read-char p))]
|
|
|
|
[else
|
|
|
|
(tokenize-string-continue ls p c)]))]
|
|
|
|
[else
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p 'tokenize
|
2007-12-02 06:58:33 -05:00
|
|
|
"non-whitespace character after escape")])))]
|
2009-06-26 03:07:26 -04:00
|
|
|
[(memv c LF1)
|
2007-12-02 06:58:33 -05:00
|
|
|
(tokenize-string-continue ls p (read-char p))]
|
2009-06-26 03:07:26 -04:00
|
|
|
[(eqv? c #\return)
|
2007-12-02 06:58:33 -05:00
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
2009-06-26 03:07:26 -04:00
|
|
|
[(memv c LF2)
|
2007-12-02 06:58:33 -05:00
|
|
|
(tokenize-string-continue ls p (read-char p))]
|
2009-06-26 03:07:26 -04:00
|
|
|
[else
|
2007-12-02 06:58:33 -05:00
|
|
|
(tokenize-string-continue ls p c)]))]
|
2007-12-18 19:52:15 -05:00
|
|
|
[else (die/p-1 p 'tokenize "invalid string escape" c)]))]
|
2009-06-26 03:07:26 -04:00
|
|
|
[(memv c LF1)
|
2007-12-02 06:58:33 -05:00
|
|
|
(tokenize-string (cons #\linefeed ls) p)]
|
2009-06-26 03:07:26 -04:00
|
|
|
[(eqv? c #\return)
|
2007-12-02 06:58:33 -05:00
|
|
|
(let ([c (peek-char p)])
|
2009-06-26 03:07:26 -04:00
|
|
|
(when (memv c LF2) (read-char p))
|
2007-12-02 06:58:33 -05:00
|
|
|
(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)])
|
2009-06-26 03:07:26 -04:00
|
|
|
(unless (or (eof-object? c) (memv c LF1) (eqv? c #\return))
|
|
|
|
(skip-comment p)))))
|
2006-11-23 19:33:45 -05:00
|
|
|
(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)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p '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-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid syntax"
|
|
|
|
(string-append "..." (string c)))]))]
|
2006-11-23 19:33:45 -05:00
|
|
|
[else
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid syntax"
|
|
|
|
(string-append ".." (string c)))]))]
|
2007-06-14 11:56:47 -04:00
|
|
|
[else
|
2008-06-02 03:01:59 -04:00
|
|
|
(cons 'datum
|
2009-08-02 03:47:39 -04:00
|
|
|
(u:dot p '(#\.) 10 #f #f +1))]))))
|
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]
|
2009-06-26 03:07:26 -04:00
|
|
|
[else
|
|
|
|
(die/p p 'tokenize "invalid character after sequence"
|
|
|
|
(string-append str (string c)))]))]
|
2006-11-23 19:33:45 -05:00
|
|
|
[else
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
2009-06-26 03:07:26 -04:00
|
|
|
(die/p p '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
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p 'tokenize
|
|
|
|
"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)]
|
2009-06-26 03:07:26 -04:00
|
|
|
[else
|
|
|
|
(die/p p 'tokenize "invalid syntax"
|
|
|
|
(string-ref str 0) c)]))))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define tokenize-char
|
|
|
|
(lambda (p)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid #\\ near end of file")]
|
2009-06-26 03:07:26 -04: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-12-18 19:52:15 -05:00
|
|
|
(die/p p '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) =>
|
2008-04-28 15:01:45 -04:00
|
|
|
(lambda (v)
|
2007-06-01 22:17:22 -04:00
|
|
|
(read-char p)
|
2008-04-28 15:01:45 -04:00
|
|
|
(let f ([v v] [ac (cons n '(#\x))])
|
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)
|
2008-04-28 15:01:45 -04:00
|
|
|
(cons 'datum (checked-integer->char v ac p))]
|
2007-06-01 22:17:22 -04:00
|
|
|
[(delimiter? c)
|
2008-04-28 15:01:45 -04:00
|
|
|
(cons 'datum (checked-integer->char v ac p))]
|
2007-06-01 22:17:22 -04:00
|
|
|
[(hex c) =>
|
|
|
|
(lambda (v0)
|
2007-12-05 05:01:56 -05:00
|
|
|
(read-char p)
|
2008-04-28 15:01:45 -04:00
|
|
|
(f (+ (* v 16) v0) (cons c ac)))]
|
2007-06-01 22:17:22 -04:00
|
|
|
[else
|
2008-04-28 15:01:45 -04:00
|
|
|
(die/p p 'tokenize
|
|
|
|
"invalid character sequence"
|
|
|
|
(list->string (reverse (cons c ac))))]))))]
|
2007-06-01 22:17:22 -04:00
|
|
|
[else
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid character sequence"
|
2007-10-25 14:32:26 -04:00
|
|
|
(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-12-18 19:52:15 -05:00
|
|
|
(die/p p '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))]
|
2008-04-28 15:01:45 -04:00
|
|
|
[(and ($char<= #\a x) ($char<= x #\f))
|
2007-06-01 22:17:22 -04:00
|
|
|
($fx- ($char->fixnum x)
|
|
|
|
($fx- ($char->fixnum #\a) 10))]
|
2008-04-28 15:01:45 -04:00
|
|
|
[(and ($char<= #\A x) ($char<= x #\F))
|
|
|
|
($fx- ($char->fixnum x)
|
2007-06-01 22:17:22 -04:00
|
|
|
($fx- ($char->fixnum #\A) 10))]
|
|
|
|
[else #f]))
|
2006-11-23 19:33:45 -05:00
|
|
|
(define multiline-error
|
2007-12-18 19:52:15 -05:00
|
|
|
(lambda (p)
|
|
|
|
(die/p p '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
|
2007-12-18 19:52:15 -05:00
|
|
|
[(eof-object? c) (multiline-error p)]
|
2006-12-02 10:11:57 -05:00
|
|
|
[($char= #\| c)
|
2008-03-15 21:12:43 -04:00
|
|
|
(let g ([c (read-char p)] [ac ac])
|
2006-12-02 10:11:57 -05:00
|
|
|
(cond
|
2007-12-18 19:52:15 -05:00
|
|
|
[(eof-object? c) (multiline-error p)]
|
2006-12-02 10:11:57 -05:00
|
|
|
[($char= #\# c) ac]
|
2008-03-15 21:12:43 -04:00
|
|
|
[($char= #\| c)
|
|
|
|
(g (read-char p) (cons c ac))]
|
2006-12-02 10:11:57 -05:00
|
|
|
[else (f p (cons c ac))]))]
|
|
|
|
[($char= #\# c)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
2007-12-18 19:52:15 -05:00
|
|
|
[(eof-object? c) (multiline-error p)]
|
2006-12-02 10:11:57 -05:00
|
|
|
[($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-12-18 19:52:15 -05:00
|
|
|
(die/p p '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
|
2007-12-18 19:52:15 -05:00
|
|
|
[(eof-object? c) (die/p p 'tokenize "invalid # near end of file")]
|
2007-05-20 22:16:57 -04:00
|
|
|
[(memq c '(#\t #\T))
|
2008-12-08 06:28:41 -05:00
|
|
|
(let ([c1 (peek-char p)])
|
2006-12-02 05:02:05 -05:00
|
|
|
(cond
|
2008-12-08 06:28:41 -05:00
|
|
|
[(eof-object? c1) '(datum . #t)]
|
|
|
|
[(delimiter? c1) '(datum . #t)]
|
2007-12-18 19:52:15 -05:00
|
|
|
[else (die/p p 'tokenize
|
2008-12-08 06:28:41 -05:00
|
|
|
(format "invalid syntax near #~a~a" c c1))]))]
|
2007-05-20 22:16:57 -04:00
|
|
|
[(memq c '(#\f #\F))
|
2008-12-08 06:28:41 -05:00
|
|
|
(let ([c1 (peek-char p)])
|
2006-12-02 05:02:05 -05:00
|
|
|
(cond
|
2008-12-08 06:28:41 -05:00
|
|
|
[(eof-object? c1) '(datum . #f)]
|
|
|
|
[(delimiter? c1) '(datum . #f)]
|
2007-12-18 19:52:15 -05:00
|
|
|
[else (die/p p 'tokenize
|
2008-12-08 06:28:41 -05:00
|
|
|
(format "invalid syntax near #~a~a" c c1))]))]
|
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)
|
|
|
|
(let ([e (read-char p)])
|
|
|
|
(when (eof-object? e)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p '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)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p '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)
|
2007-12-18 22:06:58 -05:00
|
|
|
(tokenize/1 p)]
|
2007-11-18 19:53:32 -05:00
|
|
|
[(#\i)
|
|
|
|
(read-char* p '(#\i) "karus" "#!ikarus comment" #f #f)
|
|
|
|
(set-port-mode! p 'ikarus-mode)
|
2007-12-18 22:06:58 -05:00
|
|
|
(tokenize/1 p)]
|
2007-11-18 19:53:32 -05:00
|
|
|
[else
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p 'tokenize
|
2007-11-18 19:53:32 -05:00
|
|
|
(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)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p 'tokenize "graph syntax is invalid in #!r6rs mode"
|
2007-11-18 19:53:32 -05:00
|
|
|
(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)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p 'tokenize "gensym syntax is invalid in #!r6rs mode"
|
2007-11-18 19:53:32 -05:00
|
|
|
(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
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p '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)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p 'tokenize "gensym syntax is invalid in #!r6rs mode"
|
2007-11-18 19:53:32 -05:00
|
|
|
(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
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p '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
|
|
|
|
(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
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p '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
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p '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)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid eof object after #vu8")]
|
|
|
|
[else (die/p-1 p 'tokenize
|
2007-10-25 14:32:26 -04:00
|
|
|
(format "invalid sequence #vu8~a" c))]))]
|
2007-05-15 23:57:35 -04:00
|
|
|
[(eof-object? c)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid eof object after #vu")]
|
|
|
|
[else (die/p-1 p 'tokenize
|
2007-10-25 14:32:26 -04:00
|
|
|
(format "invalid sequence #vu~a" c))]))]
|
2007-05-15 23:57:35 -04:00
|
|
|
[(eof-object? c)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid eof object after #v")]
|
|
|
|
[else (die/p p 'tokenize
|
2007-10-25 14:32:26 -04:00
|
|
|
(format "invalid sequence #v~a" c))]))]
|
2007-06-14 11:56:47 -04:00
|
|
|
[(memq c '(#\e #\E))
|
2008-06-02 03:01:59 -04:00
|
|
|
(cons 'datum (parse-string p (list c #\#) 10 #f 'e))]
|
2007-06-14 11:56:47 -04:00
|
|
|
[(memq c '(#\i #\I))
|
2008-06-02 03:01:59 -04:00
|
|
|
(cons 'datum (parse-string p (list c #\#) 10 #f 'i))]
|
2007-06-14 11:56:47 -04:00
|
|
|
[(memq c '(#\b #\B))
|
2008-06-02 03:01:59 -04:00
|
|
|
(cons 'datum (parse-string p (list c #\#) 2 2 #f))]
|
2007-06-14 11:56:47 -04:00
|
|
|
[(memq c '(#\x #\X))
|
2008-06-02 03:01:59 -04:00
|
|
|
(cons 'datum (parse-string p (list c #\#) 16 16 #f))]
|
2007-06-14 11:56:47 -04:00
|
|
|
[(memq c '(#\o #\O))
|
2008-06-02 03:01:59 -04:00
|
|
|
(cons 'datum (parse-string p (list c #\#) 8 8 #f))]
|
2007-06-14 11:56:47 -04:00
|
|
|
[(memq c '(#\d #\D))
|
2008-06-02 03:01:59 -04:00
|
|
|
(cons 'datum (parse-string p (list c #\#) 10 10 #f))]
|
2009-06-26 03:07:26 -04:00
|
|
|
;[($char= #\@ c) DEAD: Unfixable due to port encoding
|
|
|
|
; that does not allow mixing binary and
|
|
|
|
; textual data in the same port.
|
|
|
|
; Left here for historical value
|
|
|
|
; (when (eq? (port-mode p) 'r6rs-mode)
|
|
|
|
; (die/p-1 p 'tokenize "fasl syntax is invalid in #!r6rs mode"
|
|
|
|
; (format "#~a" c)))
|
|
|
|
; (die/p-1 p 'read "FIXME: fasl read disabled")
|
|
|
|
; '(cons 'datum ($fasl-read p))]
|
2006-12-02 05:02:05 -05:00
|
|
|
[else
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p 'tokenize
|
2007-10-25 14:32:26 -04:00
|
|
|
(format "invalid syntax #~a" c))])))
|
2008-06-02 03:01:59 -04:00
|
|
|
|
|
|
|
(define (num-error p str ls)
|
2008-06-04 01:27:33 -04:00
|
|
|
(die/p-1 p 'read str
|
2008-06-02 03:01:59 -04:00
|
|
|
(list->string (reverse ls))))
|
|
|
|
|
|
|
|
(define-syntax port-config
|
2008-06-04 01:27:33 -04:00
|
|
|
(syntax-rules (GEN-TEST GEN-ARGS FAIL EOF-ERROR GEN-DELIM-TEST)
|
2008-06-02 03:01:59 -04:00
|
|
|
[(_ GEN-ARGS k . rest) (k (p ac) . rest)]
|
|
|
|
[(_ FAIL (p ac))
|
|
|
|
(num-error p "invalid numeric sequence" ac)]
|
2008-06-04 01:27:33 -04:00
|
|
|
[(_ FAIL (p ac) c)
|
|
|
|
(num-error p "invalid numeric sequence" (cons c ac))]
|
|
|
|
[(_ EOF-ERROR (p ac))
|
|
|
|
(num-error p "invalid eof while reading number" ac)]
|
|
|
|
[(_ GEN-DELIM-TEST c sk fk)
|
|
|
|
(if (delimiter? c) sk fk)]
|
|
|
|
[(_ GEN-TEST var next fail (p ac) eof-case char-case)
|
2008-06-02 03:01:59 -04:00
|
|
|
(let ([c (peek-char p)])
|
2008-06-04 01:27:33 -04:00
|
|
|
(if (eof-object? c)
|
|
|
|
(let ()
|
|
|
|
(define-syntax fail
|
|
|
|
(syntax-rules ()
|
|
|
|
[(_) (num-error p "invalid numeric sequence" ac)]))
|
|
|
|
eof-case)
|
2008-06-02 03:01:59 -04:00
|
|
|
(let ([var c])
|
2008-06-04 01:27:33 -04:00
|
|
|
(define-syntax fail
|
|
|
|
(syntax-rules ()
|
|
|
|
[(_)
|
|
|
|
(num-error p "invalid numeric sequence"
|
|
|
|
(cons var ac))]))
|
2008-06-02 03:01:59 -04:00
|
|
|
(define-syntax next
|
|
|
|
(syntax-rules ()
|
|
|
|
[(_ who args (... ...))
|
|
|
|
(who p (cons (get-char p) ac) args (... ...))]))
|
|
|
|
char-case)))]))
|
|
|
|
|
|
|
|
(define-string->number-parser port-config
|
2009-08-02 03:47:39 -04:00
|
|
|
(parse-string u:digit+ u:sign u:dot))
|
2008-06-02 03:01:59 -04:00
|
|
|
|
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)))
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize
|
2007-11-18 19:53:32 -05:00
|
|
|
(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)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize
|
2007-12-05 05:01:56 -05:00
|
|
|
(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
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p 'tokenize
|
2007-12-05 05:01:56 -05:00
|
|
|
(format "invalid ~a: ~s" who
|
|
|
|
(list->string (reverse (cons c ls)))))]))])))
|
2006-11-23 19:48:14 -05:00
|
|
|
(define (tokenize-hashnum p n)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid eof inside #n mark/ref")]
|
2006-11-23 19:48:14 -05:00
|
|
|
[($char= #\= c) (cons 'mark n)]
|
|
|
|
[($char= #\# c) (cons 'ref n)]
|
|
|
|
[(digit? c)
|
|
|
|
(tokenize-hashnum p (fx+ (fx* n 10) (char->num c)))]
|
|
|
|
[else
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p '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)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p '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)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "unexpected eof while reading symbol")]
|
2006-11-23 19:33:45 -05:00
|
|
|
[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-12-18 19:52:15 -05:00
|
|
|
(die/p p '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)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid eof after \\x")]
|
2007-06-01 22:17:22 -04:00
|
|
|
[(hex c) =>
|
|
|
|
(lambda (v)
|
|
|
|
(let f ([v v] [ac `(,c #\x #\\)])
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize
|
2007-10-25 14:32:26 -04:00
|
|
|
(format "invalid eof after ~a"
|
|
|
|
(list->string (reverse ac))))]
|
2007-06-01 22:17:22 -04:00
|
|
|
[($char= #\; c)
|
2008-04-28 15:01:45 -04:00
|
|
|
(tokenize-identifier
|
|
|
|
(cons (checked-integer->char v ac p) main-ac)
|
|
|
|
p)]
|
2007-06-01 22:17:22 -04:00
|
|
|
[(hex c) =>
|
|
|
|
(lambda (v0)
|
|
|
|
(f (+ (* v 16) v0) (cons c ac)))]
|
|
|
|
[else
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p 'tokenize "invalid sequence"
|
2007-06-01 22:17:22 -04:00
|
|
|
(list->string (cons c (reverse ac))))]))))]
|
|
|
|
[else
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p 'tokenize
|
2007-10-25 14:32:26 -04:00
|
|
|
(format "invalid sequence \\x~a" c))]))]
|
2007-06-01 22:17:22 -04:00
|
|
|
[else
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p 'tokenize
|
2007-10-25 14:32:26 -04:00
|
|
|
(format "invalid sequence \\~a" c))])))
|
2006-12-02 05:02:05 -05:00
|
|
|
(define tokenize/c
|
|
|
|
(lambda (c p)
|
|
|
|
(cond
|
2007-12-18 22:28:27 -05:00
|
|
|
[(eof-object? c)
|
|
|
|
(error 'tokenize/c "hmmmm eof")
|
|
|
|
(eof-object)]
|
2006-12-02 05:02:05 -05:00
|
|
|
[($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)]
|
2008-06-02 03:01:59 -04:00
|
|
|
[(char<=? #\0 c #\9)
|
|
|
|
(let ([d (fx- (char->integer c) (char->integer #\0))])
|
2007-06-14 11:56:47 -04:00
|
|
|
(cons 'datum
|
2009-08-02 03:47:39 -04:00
|
|
|
(u:digit+ p (list c) 10 #f #f +1 d)))]
|
2007-12-18 19:52:15 -05:00
|
|
|
[(initial? c)
|
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))))]
|
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
|
2009-08-02 03:47:39 -04:00
|
|
|
(u:sign p '(#\+) 10 #f #f +1))]))]
|
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
|
2009-08-02 03:47:39 -04:00
|
|
|
(u:sign p '(#\-) 10 #f #f -1))]))]
|
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)
|
2009-07-30 14:46:11 -04:00
|
|
|
(die/p p '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)))))]
|
2009-07-29 12:07:03 -04:00
|
|
|
;[($char= #\{ c) 'lbrace]
|
|
|
|
[($char= #\@ c)
|
|
|
|
(when (eq? (port-mode p) 'r6rs-mode)
|
2009-07-30 14:46:11 -04:00
|
|
|
(die/p p 'tokenize "@-expr syntax is invalid in #!r6rs mode"))
|
2009-07-29 12:07:03 -04:00
|
|
|
'at-expr]
|
2006-12-02 05:02:05 -05:00
|
|
|
[else
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p-1 p 'tokenize "invalid syntax" c)])))
|
2006-12-02 05:02:05 -05:00
|
|
|
|
2007-12-18 22:06:58 -05:00
|
|
|
(define tokenize/1
|
2006-12-02 05:02:05 -05:00
|
|
|
(lambda (p)
|
2007-12-18 22:06:58 -05:00
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (eof-object)]
|
2007-12-18 22:28:27 -05:00
|
|
|
[(eqv? c #\;)
|
2007-12-18 22:06:58 -05:00
|
|
|
(skip-comment p)
|
|
|
|
(tokenize/1 p)]
|
|
|
|
[(eqv? c #\#)
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(die/p p 'tokenize "invalid eof after #")]
|
|
|
|
[(eqv? c #\;)
|
2007-12-19 00:40:25 -05:00
|
|
|
(read-as-comment p)
|
2007-12-18 22:06:58 -05:00
|
|
|
(tokenize/1 p)]
|
|
|
|
[(eqv? c #\|)
|
|
|
|
(multiline-comment p)
|
|
|
|
(tokenize/1 p)]
|
|
|
|
[else
|
|
|
|
(tokenize-hash/c c p)]))]
|
2007-12-18 22:28:27 -05:00
|
|
|
[(char-whitespace? c) (tokenize/1 p)]
|
2007-12-18 22:06:58 -05:00
|
|
|
[else (tokenize/c c p)]))))
|
|
|
|
|
2007-12-18 23:07:57 -05:00
|
|
|
(define tokenize/1+pos
|
|
|
|
(lambda (p)
|
|
|
|
(let ([pos (input-port-byte-position p)])
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
2007-12-19 00:40:25 -05:00
|
|
|
[(eof-object? c) (values (eof-object) pos)]
|
2007-12-18 23:07:57 -05:00
|
|
|
[(eqv? c #\;)
|
|
|
|
(skip-comment p)
|
|
|
|
(tokenize/1+pos p)]
|
|
|
|
[(eqv? c #\#)
|
|
|
|
(let ([pos (input-port-byte-position p)])
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(die/p p 'tokenize "invalid eof after #")]
|
|
|
|
[(eqv? c #\;)
|
2007-12-19 00:40:25 -05:00
|
|
|
(read-as-comment p)
|
2007-12-18 23:07:57 -05:00
|
|
|
(tokenize/1+pos p)]
|
|
|
|
[(eqv? c #\|)
|
|
|
|
(multiline-comment p)
|
|
|
|
(tokenize/1+pos p)]
|
|
|
|
[else
|
|
|
|
(values (tokenize-hash/c c p) pos)])))]
|
|
|
|
[(char-whitespace? c) (tokenize/1+pos p)]
|
|
|
|
[else
|
|
|
|
(values (tokenize/c c p) pos)])))))
|
|
|
|
|
2007-12-18 22:06:58 -05:00
|
|
|
(define tokenize-script-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]
|
2007-12-18 22:28:27 -05:00
|
|
|
[(eqv? c #\;)
|
|
|
|
(skip-comment p)
|
|
|
|
(tokenize/1 p)]
|
|
|
|
[(eqv? c #\#)
|
2006-12-02 05:02:05 -05:00
|
|
|
(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)
|
2007-12-18 19:52:15 -05:00
|
|
|
(die/p p 'tokenize "invalid eof after #")]
|
2007-12-18 22:28:27 -05:00
|
|
|
[(eqv? c #\!)
|
2006-12-02 05:02:05 -05:00
|
|
|
(skip-comment p)
|
2007-12-18 22:06:58 -05:00
|
|
|
(tokenize/1 p)]
|
2007-12-18 22:28:27 -05:00
|
|
|
[(eqv? c #\;)
|
2007-12-19 00:40:25 -05:00
|
|
|
(read-as-comment p)
|
2007-12-18 22:28:27 -05:00
|
|
|
(tokenize/1 p)]
|
|
|
|
[(eqv? c #\|)
|
|
|
|
(multiline-comment p)
|
|
|
|
(tokenize/1 p)]
|
2006-12-02 05:02:05 -05:00
|
|
|
[else
|
|
|
|
(tokenize-hash/c c p)]))]
|
2007-12-18 22:28:27 -05:00
|
|
|
[(char-whitespace? c) (tokenize/1 p)]
|
2006-12-02 05:02:05 -05:00
|
|
|
[else (tokenize/c c p)]))))
|
|
|
|
|
2007-12-18 23:07:57 -05:00
|
|
|
(define tokenize-script-initial+pos
|
|
|
|
(lambda (p)
|
|
|
|
(let ([pos (input-port-byte-position p)])
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
2007-12-19 00:40:25 -05:00
|
|
|
[(eof-object? c) (values (eof-object) pos)]
|
2007-12-18 23:07:57 -05:00
|
|
|
[(eqv? c #\;)
|
|
|
|
(skip-comment p)
|
|
|
|
(tokenize/1+pos p)]
|
|
|
|
[(eqv? c #\#)
|
|
|
|
(let ([pos (input-port-byte-position p)])
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(die/p p 'tokenize "invalid eof after #")]
|
|
|
|
[(eqv? c #\!)
|
|
|
|
(skip-comment p)
|
|
|
|
(tokenize/1+pos p)]
|
|
|
|
[(eqv? c #\;)
|
2007-12-19 00:40:25 -05:00
|
|
|
(read-as-comment p)
|
2007-12-18 23:07:57 -05:00
|
|
|
(tokenize/1+pos p)]
|
|
|
|
[(eqv? c #\|)
|
|
|
|
(multiline-comment p)
|
|
|
|
(tokenize/1+pos p)]
|
|
|
|
[else
|
|
|
|
(values (tokenize-hash/c c p) pos)])))]
|
|
|
|
[(char-whitespace? c) (tokenize/1+pos p)]
|
|
|
|
[else (values (tokenize/c c p) pos)])))))
|
|
|
|
|
2007-12-19 00:40:25 -05:00
|
|
|
(define-struct loc (value value^ set?))
|
|
|
|
|
|
|
|
;;; this is reverse engineered from psyntax.ss
|
|
|
|
(define-struct annotation (expression source stripped))
|
|
|
|
;;; - source is a pair of file-name x char-position
|
|
|
|
;;; - stripped is an s-expression with no annotations
|
|
|
|
;;; - expression is a list/vector/id/whathaveyou that
|
|
|
|
;;; may contain further annotations.
|
|
|
|
|
|
|
|
|
2007-12-18 22:06:58 -05:00
|
|
|
(module (read-expr read-expr-script-initial)
|
2007-12-19 00:40:25 -05:00
|
|
|
(define-syntax tokenize/1 syntax-error)
|
|
|
|
(define (annotate-simple datum pos p)
|
|
|
|
(make-annotation datum (cons (port-id p) pos) datum))
|
|
|
|
(define (annotate stripped expression pos p)
|
|
|
|
(make-annotation expression (cons (port-id p) pos) stripped))
|
2007-12-18 23:07:57 -05:00
|
|
|
(define read-list
|
|
|
|
(lambda (p locs k end mis init?)
|
2007-12-19 00:40:25 -05:00
|
|
|
(let-values ([(t pos) (tokenize/1+pos p)])
|
2007-12-18 19:52:15 -05:00
|
|
|
(cond
|
2007-12-19 00:40:25 -05:00
|
|
|
[(eof-object? t)
|
|
|
|
(die/p p 'read "end of file encountered while reading list")]
|
|
|
|
[(eq? t end) (values '() '() locs k)]
|
|
|
|
[(eq? t mis)
|
|
|
|
(die/p-1 p 'read "paren mismatch")]
|
|
|
|
[(eq? t 'dot)
|
|
|
|
(when init?
|
|
|
|
(die/p-1 p 'read "invalid dot while reading list"))
|
|
|
|
(let-values ([(d d^ locs k) (read-expr p locs k)])
|
|
|
|
(let-values ([(t pos^) (tokenize/1+pos p)])
|
|
|
|
(cond
|
|
|
|
[(eq? t end) (values d d^ locs k)]
|
|
|
|
[(eq? t mis)
|
|
|
|
(die/p-1 p 'read "paren mismatch")]
|
|
|
|
[(eq? t 'dot)
|
|
|
|
(die/p-1 p 'read "cannot have two dots in a list")]
|
|
|
|
[else
|
|
|
|
(die/p-1 p 'read
|
|
|
|
(format "expecting ~a, got ~a" end t))])))]
|
|
|
|
[else
|
|
|
|
(let-values ([(a a^ locs k) (parse-token p locs k t pos)])
|
|
|
|
(let-values ([(d d^ locs k) (read-list p locs k end mis #f)])
|
|
|
|
(let ([x (cons a d)] [x^ (cons a^ d^)])
|
|
|
|
(values x x^ locs (extend-k-pair x x^ a d k)))))]))))
|
2007-12-18 19:52:15 -05:00
|
|
|
(define extend-k-pair
|
2007-12-19 00:40:25 -05:00
|
|
|
(lambda (x x^ a d k)
|
|
|
|
(cond
|
|
|
|
[(or (loc? a) (loc? d))
|
|
|
|
(lambda ()
|
|
|
|
(let ([a (car x)])
|
|
|
|
(when (loc? a)
|
|
|
|
(set-car! x (loc-value a))
|
|
|
|
(set-car! x^ (loc-value^ a))))
|
|
|
|
(let ([d (cdr x)])
|
|
|
|
(when (loc? d)
|
|
|
|
(set-cdr! x (loc-value d))
|
|
|
|
(set-cdr! x^ (loc-value^ d))))
|
|
|
|
(k))]
|
|
|
|
[else k])))
|
2007-12-18 19:52:15 -05:00
|
|
|
(define vector-put
|
2007-12-19 00:40:25 -05:00
|
|
|
(lambda (v v^ k i ls ls^)
|
2006-11-23 19:33:45 -05:00
|
|
|
(cond
|
2007-12-18 19:52:15 -05:00
|
|
|
[(null? ls) k]
|
|
|
|
[else
|
|
|
|
(let ([a (car ls)])
|
|
|
|
(vector-set! v i a)
|
2007-12-19 00:40:25 -05:00
|
|
|
(vector-set! v^ i (car ls^))
|
|
|
|
(vector-put v v^
|
2007-12-18 19:52:15 -05:00
|
|
|
(if (loc? a)
|
|
|
|
(lambda ()
|
2007-12-19 00:40:25 -05:00
|
|
|
(vector-set! v i (loc-value a))
|
|
|
|
(vector-set! v^ i (loc-value^ a))
|
2007-12-18 19:52:15 -05:00
|
|
|
(k))
|
|
|
|
k)
|
2007-12-19 00:40:25 -05:00
|
|
|
(fxsub1 i)
|
|
|
|
(cdr ls)
|
|
|
|
(cdr ls^)))])))
|
2007-12-18 19:52:15 -05:00
|
|
|
(define read-vector
|
2007-12-19 00:40:25 -05:00
|
|
|
(lambda (p locs k count ls ls^)
|
|
|
|
(let-values ([(t pos) (tokenize/1+pos p)])
|
2007-12-18 19:52:15 -05:00
|
|
|
(cond
|
|
|
|
[(eof-object? t)
|
|
|
|
(die/p p 'read "end of file encountered while reading a vector")]
|
|
|
|
[(eq? t 'rparen)
|
2007-12-19 00:40:25 -05:00
|
|
|
(let ([v (make-vector count)] [v^ (make-vector count)])
|
|
|
|
(let ([k (vector-put v v^ k (fxsub1 count) ls ls^)])
|
|
|
|
(values v v^ locs k)))]
|
2007-12-18 19:52:15 -05:00
|
|
|
[(eq? t 'rbrack)
|
|
|
|
(die/p-1 p 'read "unexpected ] while reading a vector")]
|
|
|
|
[(eq? t 'dot)
|
|
|
|
(die/p-1 p 'read "unexpected . while reading a vector")]
|
|
|
|
[else
|
2007-12-19 00:40:25 -05:00
|
|
|
(let-values ([(a a^ locs k) (parse-token p locs k t pos)])
|
|
|
|
(read-vector p locs k (fxadd1 count)
|
|
|
|
(cons a ls) (cons a^ ls^)))]))))
|
2007-12-18 19:52:15 -05:00
|
|
|
(define read-bytevector
|
2008-11-14 03:12:18 -05:00
|
|
|
(lambda (p locs k count ls)
|
2007-12-19 00:40:25 -05:00
|
|
|
(let-values ([(t pos) (tokenize/1+pos p)])
|
2007-12-18 19:52:15 -05:00
|
|
|
(cond
|
|
|
|
[(eof-object? t)
|
|
|
|
(die/p p 'read "end of file encountered while reading a bytevector")]
|
|
|
|
[(eq? t 'rparen)
|
2008-11-14 03:12:18 -05:00
|
|
|
(let ([v (u8-list->bytevector (reverse ls))])
|
|
|
|
(values v v locs k))]
|
2007-12-18 19:52:15 -05:00
|
|
|
[(eq? t 'rbrack)
|
|
|
|
(die/p-1 p 'read "unexpected ] while reading a bytevector")]
|
|
|
|
[(eq? t 'dot)
|
|
|
|
(die/p-1 p 'read "unexpected . while reading a bytevector")]
|
|
|
|
[else
|
2007-12-19 00:40:25 -05:00
|
|
|
(let-values ([(a a^ locs k) (parse-token p locs k t pos)])
|
2008-11-14 03:12:18 -05:00
|
|
|
(unless (and (fixnum? a) (fx<= 0 a) (fx<= a 255))
|
|
|
|
(die/ann a^ 'read
|
|
|
|
"invalid value in a bytevector" a))
|
2007-12-19 00:40:25 -05:00
|
|
|
(read-bytevector p locs k (fxadd1 count)
|
2008-11-14 03:12:18 -05:00
|
|
|
(cons a ls)))]))))
|
2009-07-29 12:07:03 -04:00
|
|
|
(define read-at-expr
|
|
|
|
(lambda (p locs k at-pos)
|
|
|
|
(define-struct nested (a a^))
|
|
|
|
(define-struct nested* (a* a*^))
|
|
|
|
(define (get-chars chars pos p a* a*^)
|
|
|
|
(if (null? chars)
|
|
|
|
(values a* a*^)
|
|
|
|
(let ([str (list->string chars)])
|
|
|
|
(let ([str^ (annotate-simple str pos p)])
|
|
|
|
(values (cons str a*) (cons str^ a*^))))))
|
|
|
|
(define (return start-pos start-col c*** p)
|
|
|
|
(let ([indent
|
|
|
|
(apply min start-col
|
|
|
|
(map
|
|
|
|
(lambda (c**)
|
|
|
|
(define (st00 c* c** n)
|
|
|
|
(if (null? c*)
|
|
|
|
(st0 c** n)
|
|
|
|
(if (char=? (car c*) #\space)
|
|
|
|
(st00 (cdr c*) c** (+ n 1))
|
|
|
|
n)))
|
|
|
|
(define (st0 c** n)
|
|
|
|
(if (null? c**)
|
|
|
|
start-col
|
|
|
|
(let ([c* (car c**)])
|
|
|
|
(if (or (nested? c*) (nested*? c*))
|
|
|
|
start-col
|
|
|
|
(st00 (car c*) (cdr c**) n)))))
|
|
|
|
(st0 c** 0))
|
|
|
|
(cdr c***)))])
|
|
|
|
(define (convert c*)
|
|
|
|
(if (or (nested? c*) (nested*? c*))
|
|
|
|
c*
|
|
|
|
(let ([str (list->string (car c*))])
|
|
|
|
(let ([str^ (annotate-simple str (cdr c*) p)])
|
|
|
|
(make-nested str str^)))))
|
|
|
|
(define (trim/convert c**)
|
|
|
|
(define (mk n pos)
|
|
|
|
(let ([str (make-string (- n indent) #\space)])
|
|
|
|
(let ([str^ (annotate-simple str pos p)])
|
|
|
|
(make-nested str str^))))
|
|
|
|
(define (s1 c* pos c** n)
|
|
|
|
(if (null? c*)
|
|
|
|
(let ([c* (car c**)])
|
|
|
|
(if (or (nested? c*) (nested*? c*))
|
|
|
|
(cons (mk n pos) (map convert c**))
|
|
|
|
(s1 c* pos (cdr c**) n)))
|
|
|
|
(if (char=? (car c*) #\space)
|
|
|
|
(s1 (cdr c*) pos c** (+ n 1))
|
|
|
|
(cons*
|
|
|
|
(mk n pos)
|
|
|
|
(map convert (cons (cons c* pos) c**))))))
|
|
|
|
(define (s00 c* pos c** n)
|
|
|
|
(if (null? c*)
|
|
|
|
(s0 c** n)
|
|
|
|
(if (char=? #\space (car c*))
|
|
|
|
(if (< n indent)
|
|
|
|
(s00 (cdr c*) pos c** (+ n 1))
|
|
|
|
(s1 (cdr c*) pos c** (+ n 1)))
|
|
|
|
(map convert (cons (cons c* pos) c**)))))
|
|
|
|
(define (s0 c** n)
|
|
|
|
(if (null? c**)
|
|
|
|
'()
|
|
|
|
(let ([c* (car c**)])
|
|
|
|
(if (or (nested? c*) (nested*? c*))
|
|
|
|
(map convert c**)
|
|
|
|
(s00 (car c*) (cdr c*) (cdr c**) n)))))
|
|
|
|
(s0 c** 0))
|
|
|
|
(define (cons-initial c** c***)
|
|
|
|
(define (all-white? c**)
|
|
|
|
(andmap (lambda (c*)
|
|
|
|
(and (not (nested? c*))
|
|
|
|
(not (nested*? c*))
|
|
|
|
(andmap
|
|
|
|
(lambda (c) (char=? c #\space))
|
|
|
|
(car c*))))
|
|
|
|
c**))
|
|
|
|
(define (nl)
|
|
|
|
(let ([str "\n"])
|
|
|
|
(list (make-nested str str))))
|
|
|
|
(define (S1 c*** n)
|
|
|
|
(if (null? c***)
|
|
|
|
(make-list n (nl))
|
|
|
|
(let ([c** (car c***)] [c*** (cdr c***)])
|
|
|
|
(if (all-white? c**)
|
|
|
|
(S1 c*** (+ n 1))
|
|
|
|
(append
|
|
|
|
(make-list n (nl))
|
|
|
|
(cons (trim/convert c**)
|
|
|
|
(S2 c*** 0 0)))))))
|
|
|
|
(define (S2 c*** n m)
|
|
|
|
(if (null? c***)
|
|
|
|
(make-list (+ n m) (nl))
|
|
|
|
(let ([c** (car c***)] [c*** (cdr c***)])
|
|
|
|
(if (all-white? c**)
|
|
|
|
(S2 c*** (+ n 1) -1)
|
|
|
|
(append
|
|
|
|
(make-list (+ n 1) (nl))
|
|
|
|
(cons (trim/convert c**)
|
|
|
|
(S2 c*** 0 0)))))))
|
|
|
|
(define (S0 c** c***)
|
|
|
|
(if (all-white? c**)
|
|
|
|
(S1 c*** 0)
|
|
|
|
(cons
|
|
|
|
(map convert c**)
|
|
|
|
(S2 c*** 0 0))))
|
|
|
|
(S0 c** c***))
|
|
|
|
(let ([c** (cons-initial (car c***) (cdr c***))])
|
|
|
|
(let ([n* (apply append c**)])
|
|
|
|
(define (extract p p* ls)
|
|
|
|
(let f ([ls ls])
|
|
|
|
(cond
|
|
|
|
[(null? ls) '()]
|
|
|
|
[(nested? (car ls)) (cons (p (car ls)) (f (cdr ls)))]
|
|
|
|
[else (append (p* (car ls)) (f (cdr ls)))])))
|
|
|
|
(let ([c* (extract nested-a nested*-a* n*)]
|
|
|
|
[c*^ (extract nested-a^ nested*-a*^ n*)])
|
|
|
|
(values c* (annotate c* c*^ start-pos p) locs k))))))
|
|
|
|
(define (read-text p locs k pref*)
|
|
|
|
(let ([start-pos (port-position p)]
|
|
|
|
[start-col (input-port-column-number p)])
|
|
|
|
(let f ([c* '()] [pos start-pos]
|
|
|
|
[c** '()] [c*** '()]
|
|
|
|
[depth 0] [locs locs] [k k])
|
|
|
|
(define (match-prefix c* pref*)
|
|
|
|
(cond
|
|
|
|
[(and (pair? c*) (pair? pref*))
|
|
|
|
(and (char=? (car c*) (car pref*))
|
|
|
|
(match-prefix (cdr c*) (cdr pref*)))]
|
|
|
|
[else (and (null? pref*) c*)]))
|
|
|
|
(let ([c (read-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(die/p p 'read "end of file while reading @-expr text")]
|
|
|
|
[(char=? c #\})
|
|
|
|
(let g ([x* (cons #\} c*)] [p* pref*])
|
|
|
|
(if (null? p*)
|
|
|
|
(if (= depth 0)
|
|
|
|
(let ([c**
|
|
|
|
(reverse
|
|
|
|
(if (null? c*)
|
|
|
|
c**
|
|
|
|
(cons (cons (reverse c*) pos) c**)))])
|
|
|
|
(let ([c*** (reverse (cons c** c***))])
|
|
|
|
(return start-pos start-col c*** p)))
|
|
|
|
(f x* pos c** c*** (- depth 1) locs k))
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(die/p p 'read "invalid eof inside @-expression")]
|
|
|
|
[(char=? c (rev-punc (car p*)))
|
|
|
|
(read-char p)
|
|
|
|
(g (cons c x*) (cdr p*))]
|
|
|
|
[else
|
|
|
|
(f x* pos c** c*** depth locs k)]))))]
|
|
|
|
[(char=? c #\{)
|
|
|
|
(f (cons c c*) pos c** c***
|
|
|
|
(if (match-prefix c* pref*) (+ depth 1) depth)
|
|
|
|
locs k)]
|
|
|
|
[(char=? c #\newline)
|
|
|
|
(f '()
|
|
|
|
(port-position p)
|
|
|
|
'()
|
|
|
|
(cons (reverse
|
|
|
|
(if (null? c*)
|
|
|
|
c**
|
|
|
|
(cons (cons (reverse c*) pos) c**)))
|
|
|
|
c***)
|
|
|
|
depth locs k)]
|
|
|
|
[(and (char=? c #\@) (match-prefix c* pref*)) =>
|
|
|
|
(lambda (c*)
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(die/p p 'read "invalid eof inside nested @-expr")]
|
|
|
|
[(char=? c #\")
|
|
|
|
(read-char p)
|
|
|
|
(let ([c* (tokenize-string c* p)])
|
|
|
|
(f c* pos c** c*** depth locs k))]
|
|
|
|
[else
|
|
|
|
(let-values ([(a* a*^ locs k)
|
|
|
|
(read-at-text-mode p locs k)])
|
|
|
|
(f '()
|
|
|
|
(port-position p)
|
|
|
|
(cons (make-nested* a* a*^)
|
|
|
|
(if (null? c*)
|
|
|
|
c**
|
|
|
|
(cons (cons (reverse c*) pos) c**)))
|
|
|
|
c*** depth locs k))])))]
|
|
|
|
[else
|
|
|
|
(f (cons c c*) pos c** c*** depth locs k)])))))
|
|
|
|
(define (read-brackets p locs k)
|
|
|
|
(let-values ([(a* a*^ locs k)
|
|
|
|
(read-list p locs k 'rbrack 'rparen #t)])
|
|
|
|
(unless (list? a*)
|
|
|
|
(die/ann a*^ 'read "not a proper list"))
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) ;;; @<cmd>[...]
|
|
|
|
(values a* a*^ locs k)]
|
|
|
|
[(char=? c #\{)
|
|
|
|
(read-char p)
|
|
|
|
(let-values ([(b* b*^ locs k)
|
|
|
|
(read-text p locs k '())])
|
|
|
|
(values (append a* b*)
|
|
|
|
(append a*^ b*^)
|
|
|
|
locs k))]
|
|
|
|
[(char=? c #\|)
|
|
|
|
(read-char p)
|
|
|
|
(let-values ([(b* b*^ locs k)
|
|
|
|
(read-at-bar p locs k #t)])
|
|
|
|
(values (append a* b*)
|
|
|
|
(append a*^ b*^)
|
|
|
|
locs k))]
|
|
|
|
[else (values a* a*^ locs k)]))))
|
|
|
|
(define (left-punc? c)
|
2009-07-29 12:43:49 -04:00
|
|
|
(define chars "([<!?~$%^&*-_+=:")
|
|
|
|
(let f ([i 0])
|
|
|
|
(cond
|
|
|
|
[(= i (string-length chars)) #f]
|
|
|
|
[(char=? c (string-ref chars i)) #t]
|
|
|
|
[else (f (+ i 1))])))
|
|
|
|
(define (rev-punc c)
|
|
|
|
(cond
|
|
|
|
[(char=? c #\() #\)]
|
|
|
|
[(char=? c #\[) #\]]
|
|
|
|
[(char=? c #\<) #\>]
|
|
|
|
[else c]))
|
2009-07-29 12:07:03 -04:00
|
|
|
(define (read-at-bar p locs k text-mode?)
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(die/p p 'read "eof inside @|-expression")]
|
|
|
|
[(and (char=? c #\|) text-mode?) ;;; @||
|
|
|
|
(read-char p)
|
|
|
|
(values '() '() locs k)]
|
|
|
|
[(char=? c #\{) ;;; @|{
|
|
|
|
(read-char p)
|
|
|
|
(read-text p locs k '(#\|))]
|
|
|
|
[(left-punc? c) ;;; @|<({
|
|
|
|
(read-char p)
|
|
|
|
(let ([pos (port-position p)])
|
|
|
|
(let f ([ls (list c)])
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(die/p p 'read "eof inside @|< mode")]
|
|
|
|
[(left-punc? c)
|
|
|
|
(read-char p)
|
|
|
|
(f (cons c ls))]
|
|
|
|
[(char=? c #\{)
|
|
|
|
(read-char p)
|
|
|
|
(read-text p locs k (append ls '(#\|)))]
|
|
|
|
[else
|
|
|
|
(read-at-bar-others ls p locs k)]))))]
|
|
|
|
[text-mode? ;;; @|5 6 7|
|
|
|
|
(read-at-bar-datum p locs k)]
|
|
|
|
[else
|
|
|
|
(die/p p 'read "invalid char in @| mode" c)])))
|
|
|
|
(define (read-at-bar-others ls p locs k)
|
|
|
|
(define (split ls)
|
|
|
|
(cond
|
|
|
|
[(null? ls) (values '() '())]
|
|
|
|
[(initial? (car ls))
|
|
|
|
(let-values ([(a d) (split (cdr ls))])
|
|
|
|
(values (cons (car ls) a) d))]
|
|
|
|
[else
|
|
|
|
(values '() ls)]))
|
|
|
|
(define (mksymbol ls)
|
|
|
|
(let ([s (string->symbol
|
|
|
|
(list->string
|
|
|
|
(reverse ls)))])
|
|
|
|
(values s s)))
|
|
|
|
(let-values ([(inits rest) (split ls)])
|
|
|
|
(let ([ls (tokenize-identifier inits p)])
|
|
|
|
(let-values ([(s s^) (mksymbol ls)])
|
|
|
|
(let g ([rest rest]
|
|
|
|
[a* (list s)]
|
|
|
|
[a*^ (list s^)]
|
|
|
|
[locs locs]
|
|
|
|
[k k])
|
|
|
|
(if (null? rest)
|
|
|
|
(let-values ([(b* b*^ locs k)
|
|
|
|
(read-at-bar-datum p locs k)])
|
|
|
|
(values (append a* b*) (append a*^ b*^) locs k))
|
|
|
|
(let ([x (car rest)])
|
|
|
|
(case x
|
|
|
|
[(#\() #\) ;;; vim paren-matching sucks
|
|
|
|
(let-values ([(b* b*^ locs k)
|
|
|
|
(read-list p locs k 'rparen 'rbrack #t)])
|
|
|
|
(g (cdr rest)
|
|
|
|
(list (append a* b*))
|
|
|
|
(list (append a*^ b*^))
|
|
|
|
locs k))]
|
|
|
|
[(#\[) #\] ;;; vim paren-matching sucks
|
|
|
|
(let-values ([(b* b*^ locs k)
|
|
|
|
(read-list p locs k 'rbrack 'rparen #t)])
|
|
|
|
(g (cdr rest)
|
|
|
|
(list (append a* b*))
|
|
|
|
(list (append a*^ b*^))
|
|
|
|
locs k))]
|
|
|
|
[else
|
|
|
|
(let-values ([(inits rest) (split rest)])
|
|
|
|
(let-values ([(s s^) (mksymbol inits)])
|
|
|
|
(g rest
|
|
|
|
(cons s a*)
|
|
|
|
(cons s^ a*^)
|
|
|
|
locs k)))]))))))))
|
|
|
|
(define (read-at-bar-datum p locs k)
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) (die/p p 'read "eof inside @|datum mode")]
|
|
|
|
[(char-whitespace? c)
|
|
|
|
(read-char p)
|
|
|
|
(read-at-bar-datum p locs k)]
|
|
|
|
[(char=? c #\|)
|
|
|
|
(read-char p)
|
|
|
|
(values '() '() locs k)]
|
|
|
|
[else
|
|
|
|
(let-values ([(a a^ locs k) (read-expr p locs k)])
|
|
|
|
(let-values ([(a* a*^ locs k) (read-at-bar-datum p locs k)])
|
|
|
|
(values (cons a a*) (cons a^ a*^) locs k)))])))
|
|
|
|
(define (read-at-text-mode p locs k)
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(die/p p 'read "eof encountered inside @-expression")]
|
|
|
|
[(char=? c #\|)
|
|
|
|
(read-char p)
|
|
|
|
(read-at-bar p locs k #t)]
|
|
|
|
[else
|
|
|
|
(let-values ([(a a^ locs k)
|
|
|
|
(read-at-sexpr-mode p locs k)])
|
|
|
|
(values (list a) (list a^) locs k))])))
|
|
|
|
(define (read-at-sexpr-mode p locs k)
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c)
|
|
|
|
(die/p p 'read "eof encountered inside @-expression")]
|
|
|
|
[(eqv? c '#\[) ;;; @[ ...
|
|
|
|
(read-char p)
|
|
|
|
(read-brackets p locs k)]
|
|
|
|
[(eqv? c #\{) ;;; @{ ...
|
|
|
|
(read-char p)
|
|
|
|
(read-text p locs k '())]
|
|
|
|
[(char=? c #\|)
|
|
|
|
(read-char p)
|
|
|
|
(read-at-bar p locs k #f)]
|
|
|
|
[else ;;; @<cmd> ...
|
|
|
|
(let-values ([(a a^ locs k) (read-expr p locs k)])
|
|
|
|
(let ([c (peek-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? c) ;;; @<cmd><eof>
|
|
|
|
(values a a^ locs k)]
|
|
|
|
[(eqv? c #\[)
|
|
|
|
(read-char p)
|
|
|
|
(let-values ([(a* a*^ locs k)
|
|
|
|
(read-brackets p locs k)])
|
|
|
|
(let ([v (cons a a*)] [v^ (cons a^ a*^)])
|
|
|
|
(values v (annotate v v^ at-pos p) locs k)))]
|
|
|
|
[(eqv? c #\{) ;;; @<cmd>{ ...
|
|
|
|
(read-char p)
|
|
|
|
(let-values ([(a* a*^ locs k)
|
|
|
|
(read-text p locs k '())])
|
|
|
|
(let ([v (cons a a*)] [v^ (cons a^ a*^)])
|
|
|
|
(values v (annotate v v^ at-pos p) locs k)))]
|
|
|
|
[(eqv? c #\|) ;;; @<cmd>| ...
|
|
|
|
(read-char p)
|
|
|
|
(let-values ([(a* a*^ locs k)
|
|
|
|
(read-at-bar p locs k #f)])
|
|
|
|
(let ([v (cons a a*)] [v^ (cons a^ a*^)])
|
|
|
|
(values v (annotate v v^ at-pos p) locs k)))]
|
|
|
|
[else
|
|
|
|
(values a a^ locs k)])))])))
|
|
|
|
(read-at-sexpr-mode p locs k)))
|
2007-12-18 19:52:15 -05:00
|
|
|
(define parse-token
|
2007-12-19 00:40:25 -05:00
|
|
|
(lambda (p locs k t pos)
|
2007-05-15 23:57:35 -04:00
|
|
|
(cond
|
2007-12-19 00:40:25 -05:00
|
|
|
[(eof-object? t)
|
|
|
|
(values (eof-object)
|
|
|
|
(annotate-simple (eof-object) pos p) locs k)]
|
|
|
|
[(eq? t 'lparen)
|
|
|
|
(let-values ([(ls ls^ locs k)
|
|
|
|
(read-list p locs k 'rparen 'rbrack #t)])
|
|
|
|
(values ls (annotate ls ls^ pos p) locs k))]
|
|
|
|
[(eq? t 'lbrack)
|
|
|
|
(let-values ([(ls ls^ locs k)
|
|
|
|
(read-list p locs k 'rbrack 'rparen #t)])
|
|
|
|
(values ls (annotate ls ls^ pos p) locs k))]
|
|
|
|
[(eq? t 'vparen)
|
|
|
|
(let-values ([(v v^ locs k)
|
|
|
|
(read-vector p locs k 0 '() '())])
|
|
|
|
(values v (annotate v v^ pos p) locs k))]
|
|
|
|
[(eq? t 'vu8)
|
|
|
|
(let-values ([(v v^ locs k)
|
2008-11-14 03:12:18 -05:00
|
|
|
(read-bytevector p locs k 0 '())])
|
2007-12-19 00:40:25 -05:00
|
|
|
(values v (annotate v v^ pos p) locs k))]
|
2009-07-29 12:07:03 -04:00
|
|
|
[(eq? t 'at-expr)
|
|
|
|
(read-at-expr p locs k pos)]
|
2007-12-18 19:52:15 -05:00
|
|
|
[(pair? t)
|
|
|
|
(cond
|
2007-12-19 00:40:25 -05:00
|
|
|
[(eq? (car t) 'datum)
|
|
|
|
(values (cdr t)
|
|
|
|
(annotate-simple (cdr t) pos p) locs k)]
|
2007-12-18 19:52:15 -05:00
|
|
|
[(eq? (car t) 'macro)
|
2008-04-14 22:18:56 -04:00
|
|
|
(let ([macro (cdr t)])
|
|
|
|
(define (read-macro)
|
|
|
|
(let-values ([(t pos) (tokenize/1+pos p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? t)
|
|
|
|
(die/p p 'read
|
|
|
|
(format "invalid eof after ~a read macro"
|
|
|
|
macro))]
|
|
|
|
[else (parse-token p locs k t pos)])))
|
|
|
|
(let-values ([(expr expr^ locs k) (read-macro)])
|
2008-05-05 03:52:08 -04:00
|
|
|
(let ([d (list expr)] [d^ (list expr^)])
|
|
|
|
(let ([x (cons macro d)]
|
|
|
|
[x^ (cons (annotate-simple macro pos p) d^)])
|
|
|
|
(values x (annotate x x^ pos p) locs
|
|
|
|
(extend-k-pair d d^ expr '() k))))))]
|
2007-12-18 19:52:15 -05:00
|
|
|
[(eq? (car t) 'mark)
|
|
|
|
(let ([n (cdr t)])
|
2007-12-19 00:40:25 -05:00
|
|
|
(let-values ([(expr expr^ locs k)
|
|
|
|
(read-expr p locs k)])
|
2007-12-18 19:52:15 -05:00
|
|
|
(cond
|
|
|
|
[(assq n locs) =>
|
|
|
|
(lambda (x)
|
|
|
|
(let ([loc (cdr x)])
|
|
|
|
(when (loc-set? loc) ;;; FIXME: pos
|
2009-07-30 14:46:11 -04:00
|
|
|
(die/p p 'read "duplicate mark" n))
|
2007-12-18 19:52:15 -05:00
|
|
|
(set-loc-value! loc expr)
|
2007-12-19 00:40:25 -05:00
|
|
|
(set-loc-value^! loc expr^)
|
2007-12-18 19:52:15 -05:00
|
|
|
(set-loc-set?! loc #t)
|
2007-12-19 00:40:25 -05:00
|
|
|
(values expr expr^ locs k)))]
|
2007-12-18 19:52:15 -05:00
|
|
|
[else
|
2007-12-19 00:40:25 -05:00
|
|
|
(let ([loc (make-loc expr 'unused #t)])
|
2007-12-18 19:52:15 -05:00
|
|
|
(let ([locs (cons (cons n loc) locs)])
|
2007-12-19 00:40:25 -05:00
|
|
|
(values expr expr^ locs k)))])))]
|
2007-12-18 19:52:15 -05:00
|
|
|
[(eq? (car t) 'ref)
|
|
|
|
(let ([n (cdr t)])
|
2006-11-23 19:48:14 -05:00
|
|
|
(cond
|
|
|
|
[(assq n locs) =>
|
|
|
|
(lambda (x)
|
2007-12-19 00:40:25 -05:00
|
|
|
(values (cdr x) 'unused locs k))]
|
2006-11-23 19:48:14 -05:00
|
|
|
[else
|
2007-12-19 00:40:25 -05:00
|
|
|
(let ([loc (make-loc #f 'unused #f)])
|
2006-11-23 19:48:14 -05:00
|
|
|
(let ([locs (cons (cons n loc) locs)])
|
2007-12-19 00:40:25 -05:00
|
|
|
(values loc 'unused locs k)))]))]
|
2009-07-30 14:46:11 -04:00
|
|
|
[else (die/p p 'read "invalid token" t)])]
|
2007-12-18 19:52:15 -05:00
|
|
|
[else
|
|
|
|
(die/p-1 p 'read
|
|
|
|
(format "unexpected ~s found" t))])))
|
|
|
|
(define read-expr
|
|
|
|
(lambda (p locs k)
|
2007-12-19 00:40:25 -05:00
|
|
|
(let-values ([(t pos) (tokenize/1+pos p)])
|
|
|
|
(parse-token p locs k t pos))))
|
2007-12-18 22:06:58 -05:00
|
|
|
(define read-expr-script-initial
|
2007-12-18 19:52:15 -05:00
|
|
|
(lambda (p locs k)
|
2007-12-19 00:40:25 -05:00
|
|
|
(let-values ([(t pos) (tokenize-script-initial+pos p)])
|
|
|
|
(parse-token p locs k t pos)))))
|
2007-12-18 23:07:57 -05:00
|
|
|
|
|
|
|
|
2009-07-30 14:46:11 -04:00
|
|
|
(define (reduce-loc! p)
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (x)
|
|
|
|
(let ([loc (cdr x)])
|
|
|
|
(unless (loc-set? loc)
|
2009-07-30 14:46:11 -04:00
|
|
|
(die/p p '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)
|
2009-07-30 14:46:11 -04:00
|
|
|
(die/p p 'read "circular marks"))
|
2006-11-23 19:48:14 -05:00
|
|
|
(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-12-19 00:40:25 -05:00
|
|
|
|
|
|
|
(define (read-as-comment p)
|
|
|
|
(begin (read-expr p '() void) (void)))
|
|
|
|
|
2007-12-19 19:05:23 -05:00
|
|
|
(define (return-annotated x)
|
|
|
|
(cond
|
|
|
|
[(and (annotation? x) (eof-object? (annotation-expression x)))
|
|
|
|
(eof-object)]
|
|
|
|
[else x]))
|
|
|
|
|
2007-04-29 22:29:42 -04:00
|
|
|
(define my-read
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (p)
|
2007-12-19 00:40:25 -05:00
|
|
|
(let-values ([(expr expr^ locs k) (read-expr p '() void)])
|
2006-11-23 19:48:14 -05:00
|
|
|
(cond
|
|
|
|
[(null? locs) expr]
|
|
|
|
[else
|
2009-07-30 14:46:11 -04:00
|
|
|
(for-each (reduce-loc! p) locs)
|
2006-11-23 19:48:14 -05:00
|
|
|
(k)
|
|
|
|
(if (loc? expr)
|
|
|
|
(loc-value expr)
|
|
|
|
expr)]))))
|
|
|
|
|
2006-12-02 05:02:05 -05:00
|
|
|
(define read-initial
|
|
|
|
(lambda (p)
|
2007-12-19 00:40:25 -05:00
|
|
|
(let-values ([(expr expr^ locs k) (read-expr-script-initial p '() void)])
|
2006-12-02 05:02:05 -05:00
|
|
|
(cond
|
|
|
|
[(null? locs) expr]
|
|
|
|
[else
|
2009-07-30 14:46:11 -04:00
|
|
|
(for-each (reduce-loc! p) locs)
|
2006-12-02 05:02:05 -05:00
|
|
|
(k)
|
|
|
|
(if (loc? expr)
|
|
|
|
(loc-value expr)
|
|
|
|
expr)]))))
|
2006-11-23 19:48:14 -05:00
|
|
|
|
2007-12-19 00:40:25 -05:00
|
|
|
(define read-annotated
|
2007-12-19 19:05:23 -05:00
|
|
|
(case-lambda
|
|
|
|
[(p)
|
|
|
|
(unless (input-port? p)
|
|
|
|
(error 'read-annotated "not an input port" p))
|
|
|
|
(let-values ([(expr expr^ locs k) (read-expr p '() void)])
|
|
|
|
(cond
|
|
|
|
[(null? locs) (return-annotated expr^)]
|
|
|
|
[else
|
2009-07-30 14:46:11 -04:00
|
|
|
(for-each (reduce-loc! p) locs)
|
2007-12-19 19:05:23 -05:00
|
|
|
(k)
|
|
|
|
(if (loc? expr)
|
|
|
|
(loc-value^ expr)
|
|
|
|
(return-annotated expr^))]))]
|
|
|
|
[() (read-annotated (current-input-port))]))
|
2007-12-19 00:40:25 -05:00
|
|
|
|
|
|
|
(define read-script-annotated
|
|
|
|
(lambda (p)
|
|
|
|
(let-values ([(expr expr^ locs k) (read-expr-script-initial p '() void)])
|
|
|
|
(cond
|
2007-12-19 19:05:23 -05:00
|
|
|
[(null? locs) (return-annotated expr^)]
|
2007-12-19 00:40:25 -05:00
|
|
|
[else
|
2009-07-30 14:46:11 -04:00
|
|
|
(for-each (reduce-loc! p) locs)
|
2007-12-19 00:40:25 -05:00
|
|
|
(k)
|
|
|
|
(if (loc? expr)
|
|
|
|
(loc-value^ expr)
|
2007-12-19 19:05:23 -05:00
|
|
|
(return-annotated expr^))]))))
|
2007-12-19 00:40:25 -05:00
|
|
|
|
2007-05-05 20:47:31 -04:00
|
|
|
(define read-token
|
2006-11-23 19:48:14 -05:00
|
|
|
(case-lambda
|
2007-12-18 22:06:58 -05:00
|
|
|
[() (tokenize/1 (current-input-port))]
|
2006-11-23 19:48:14 -05:00
|
|
|
[(p)
|
|
|
|
(if (input-port? p)
|
2007-12-18 22:06:58 -05:00
|
|
|
(tokenize/1 p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die '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-12-15 08:22:49 -05:00
|
|
|
(die '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)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-datum "not an input port"))
|
2007-11-22 14:43:39 -05:00
|
|
|
(my-read p))
|
|
|
|
|
2007-05-05 20:47:31 -04:00
|
|
|
(define comment-handler
|
2007-12-18 19:52:15 -05:00
|
|
|
;;; this is stale, maybe delete
|
2006-12-02 10:11:57 -05:00
|
|
|
(make-parameter
|
|
|
|
(lambda (x) (void))
|
|
|
|
(lambda (x)
|
|
|
|
(unless (procedure? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'comment-handler "not a procedure" x))
|
2006-12-02 10:11:57 -05:00
|
|
|
x)))
|
2007-05-05 20:47:31 -04:00
|
|
|
|
2008-05-21 02:21:37 -04:00
|
|
|
)
|
2008-05-06 15:38:05 -04:00
|
|
|
|
2008-05-21 02:21:37 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
|