better code layout (easier to add new options)

svn: r11956
This commit is contained in:
Eli Barzilay 2008-10-07 07:53:50 +00:00
parent d2bd4c050e
commit da389b03a9

View File

@ -7,47 +7,51 @@
(require (for-syntax scheme/base))
(define-syntax (provide-module-reader stx)
(syntax-case stx ()
[(_ lib body ...)
(let ([key-args '()])
(define (construct-reader lib body)
(define (err str [sub #f])
(raise-syntax-error 'syntax/module-reader str sub))
(define-syntax-rule (keywords -body [kwd var default] ... [checks ...])
(begin
(define var #f) ...
(define -body
(let loop ([body (syntax->list #'(body ...))])
(let loop ([body body])
(if (not (and (pair? body)
(pair? (cdr body))
(keyword? (syntax-e (car body)))))
(datum->syntax stx body stx)
(let* ([k (car body)] [k* (syntax-e k)] [v (cadr body)])
(cond
[(assq k* key-args) (err (format "got two ~s keywords" k*) k)]
[(not (memq k* '(#:read #:read-syntax #:wrapper1 #:wrapper2
#:whole-body-readers?)))
(err "got an unknown keyword" (car body))]
[else (set! key-args (cons (cons k* v) key-args))
(loop (cddr body))])))))
(define (get kwd [dflt #f])
(cond [(assq kwd key-args) => cdr] [else dflt]))
(unless (equal? (and (assq '#:read key-args) #t)
(and (assq '#:read-syntax key-args) #t))
(case k*
[(kwd) (if var
(err (format "got two ~s keywords" k*) k)
(begin (set! var v) (loop (cddr body))))]
...
[else (err "got an unknown keyword" (car body))])))))
checks ...
(set! var (or var default)) ...))
(keywords -body
[#:read ~read #'read]
[#:read-syntax ~read-syntax #'read-syntax]
[#:wrapper1 ~wrapper1 #'#f]
[#:wrapper2 ~wrapper2 #'#f]
[#:whole-body-readers? ~whole-body-readers? #'#f]
[(unless (equal? (and ~read #t) (and ~read-syntax #t))
(err "must specify either both #:read and #:read-syntax, or none"))
(when (and (assq '#:whole-body-readers? key-args)
(not (assq '#:read key-args)))
(err "got a #:whole-body-readers? without #:read and #:read-syntax"))
(when (and ~whole-body-readers? (not (and ~read ~read-syntax)))
(err "got a #:whole-body-readers? without #:read and #:read-syntax"))])
(quasisyntax/loc stx
(#%module-begin
#,@-body
(#%provide (rename *read read) (rename *read-syntax read-syntax))
(define-values (*read *read-syntax)
(let* ([rd #,(get '#:read #'read)]
[rds #,(get '#:read-syntax #'read-syntax)]
[w1 #,(get '#:wrapper1 #'#f)]
[w2 #,(get '#:wrapper2 #'#f)]
(let* ([rd #,~read]
[rds #,~read-syntax]
[w1 #,~wrapper1]
[w2 #,~wrapper2]
[w2 (cond [(not w2) (lambda (in r _) (r in))]
[(procedure-arity-includes? w2 3) w2]
[else (lambda (in r _) (w2 in r))])]
[base 'lib]
[whole? #,(get '#:whole-body-readers? #'#f)])
[base '#,lib]
[whole? #,~whole-body-readers?])
(values
(lambda (in modpath line col pos)
(w2 in
@ -58,10 +62,11 @@
(lambda (src in modpath line col pos)
(w2 in
(lambda (in)
(wrap-internal
base in (lambda (in) (rds src in)) whole?
(wrap-internal base in (lambda (in) (rds src in)) whole?
w1 #t modpath src line col pos))
#t))))))))]))
#t))))))))
(syntax-case stx ()
[(_ lib body ...) (construct-reader #'lib (syntax->list #'(body ...)))]))
(define (wrap-internal lib port read whole? wrapper stx?
modpath src line col pos)