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