add lang-reader-module-paths to factor out copy-pasted code (#1347)

* add lang-reader-module-paths to syntax/module-reader

to be used as the third argument to make-meta-reader in lang-extensions
like at-exp

* document lang-reader-module-paths

* use lang-reader-module-paths in at-exp
This commit is contained in:
Alex Knauth 2016-08-19 09:42:59 -04:00 committed by Matthew Flatt
parent 9df2c3292e
commit 42dcc525b1
3 changed files with 29 additions and 11 deletions

View File

@ -16,15 +16,7 @@
(make-meta-reader
'at-exp
"language path"
(lambda (bstr)
(let* ([str (bytes->string/latin-1 bstr)]
[sym (string->symbol str)])
(and (module-path? sym)
(vector
;; try submod first:
`(submod ,sym reader)
;; fall back to /lang/reader:
(string->symbol (string-append str "/lang/reader"))))))
lang-reader-module-paths
wrap-reader
(lambda (orig-read-syntax)
(define read-syntax (wrap-reader orig-read-syntax))

View File

@ -409,7 +409,8 @@ a vector of module paths, they are tried in order using
@racket[#f], a reader exception is raised in the same way as when
@racket[read-spec] produces a @racket[#f]. The @racketmodname[planet]
languages supply a @racket[module-path-parser] that converts a byte
string to a module path.
string to a module path. Lang-extensions like @racketmodname[at-exp]
use @racket[lang-reader-module-paths] as this argument.
If loading the module produced by @racket[module-path-parser]
succeeds, then the loaded module's @racketidfont{read},
@ -426,6 +427,14 @@ The procedures generated by @racket[make-meta-reader] are not meant
for use with the @racketmodname[syntax/module-reader] language; they
are meant to be exported directly.}
@defproc[(lang-reader-module-paths [bstr bytes?]) (or/c #f (vectorof module-path?))]{
To be used as the third argument to @racket[make-meta-reader] in
lang-extensions like @racketmodname[at-exp]. On success, it returns a
vector of module paths, one of which should point to the reader module
for the @hash-lang[] @racket[bstr] language. These paths are
@racket[(submod base-path reader)] and @racket[base-path/lang/reader].
}
@defproc[(wrap-read-all [mod-path module-path?]
[in input-port?]

View File

@ -5,6 +5,7 @@
(provide (rename-out [provide-module-reader #%module-begin]
[wrap wrap-read-all])
make-meta-reader
lang-reader-module-paths
(except-out (all-from-out racket/private/base) #%module-begin))
(define ar? procedure-arity-includes?)
@ -296,4 +297,20 @@
(read-fn inp 'read-syntax (list src) src mod line col pos
convert-read-syntax))
(values -read -read-syntax -get-info)))
(values -read -read-syntax -get-info))
;; lang-reader-module-paths : Byte-String -> (U False (Vectorof Module-Path))
;; To be used as the third argument to make-meta-reader in lang-extensions
;; like at-exp. On success, returns a vector of module paths, one of which
;; should point to the reader module for the #lang bstr language.
(define (lang-reader-module-paths bstr)
(let* ([str (bytes->string/latin-1 bstr)]
[sym (string->symbol str)])
(and (module-path? sym)
(vector
;; try submod first:
`(submod ,sym reader)
;; fall back to /lang/reader:
(string->symbol (string-append str "/lang/reader"))))))
)