|
|
@ -1,5 +1,5 @@
|
|
|
|
#lang racket/base
|
|
|
|
#lang racket/base
|
|
|
|
(require (for-syntax racket/base) syntax/strip-context)
|
|
|
|
(require (for-syntax racket/base racket/syntax) syntax/strip-context)
|
|
|
|
(provide define-read-and-read-syntax)
|
|
|
|
(provide define-read-and-read-syntax)
|
|
|
|
|
|
|
|
|
|
|
|
;; `define-read-functions` simplifies support for the standard reading API,
|
|
|
|
;; `define-read-functions` simplifies support for the standard reading API,
|
|
|
@ -9,28 +9,35 @@
|
|
|
|
(define-syntax (define-read-and-read-syntax calling-site-stx)
|
|
|
|
(define-syntax (define-read-and-read-syntax calling-site-stx)
|
|
|
|
(syntax-case calling-site-stx ()
|
|
|
|
(syntax-case calling-site-stx ()
|
|
|
|
[(_ (PATH PORT) BODY ...)
|
|
|
|
[(_ (PATH PORT) BODY ...)
|
|
|
|
(with-syntax ([READ (datum->syntax calling-site-stx 'read)]
|
|
|
|
(let ([internal-prefix (gensym)])
|
|
|
|
[READ-SYNTAX (datum->syntax calling-site-stx 'read-syntax)])
|
|
|
|
(with-syntax ([READ (datum->syntax calling-site-stx 'read)]
|
|
|
|
#'(begin
|
|
|
|
[READ-SYNTAX (datum->syntax calling-site-stx 'read-syntax)]
|
|
|
|
(provide READ READ-SYNTAX)
|
|
|
|
;; use prefixed names to prevent namespace collisions with possibly existing `read` & `read-syntax`
|
|
|
|
(define (calling-site-function PATH PORT)
|
|
|
|
[INTERNAL-READ (format-id #'here "~a-~a" internal-prefix 'read)]
|
|
|
|
BODY ...) ; don't care whether this produces datum or syntax
|
|
|
|
[INTERNAL-READ-SYNTAX (format-id #'here "~a-~a" internal-prefix 'read-syntax)])
|
|
|
|
|
|
|
|
#'(begin
|
|
|
|
|
|
|
|
(provide (rename-out [INTERNAL-READ READ]
|
|
|
|
|
|
|
|
[INTERNAL-READ-SYNTAX READ-SYNTAX]))
|
|
|
|
|
|
|
|
(define (calling-site-function PATH PORT)
|
|
|
|
|
|
|
|
BODY ...) ; don't care whether this produces datum or syntax
|
|
|
|
|
|
|
|
|
|
|
|
(define (READ-SYNTAX path port)
|
|
|
|
(define INTERNAL-READ-SYNTAX
|
|
|
|
;; because `read-syntax` must produce syntax
|
|
|
|
(procedure-rename (λ (path port) ; rename proc so it looks right in the REPL (otherwise retains internal prefix name)
|
|
|
|
;; coerce a datum result to syntax if needed (à la `with-syntax`)
|
|
|
|
;; because `read-syntax` must produce syntax
|
|
|
|
(define result-syntax (let ([output (calling-site-function path port)])
|
|
|
|
;; coerce a datum result to syntax if needed (à la `with-syntax`)
|
|
|
|
(if (syntax? output)
|
|
|
|
(define result-syntax (let ([output (calling-site-function path port)])
|
|
|
|
output
|
|
|
|
(if (syntax? output)
|
|
|
|
(datum->syntax #f output))))
|
|
|
|
output
|
|
|
|
;; because `read-syntax` must produce syntax without context
|
|
|
|
(datum->syntax #f output))))
|
|
|
|
;; see http://docs.racket-lang.org/guide/hash-lang_reader.html
|
|
|
|
;; because `read-syntax` must produce syntax without context
|
|
|
|
;; "a `read-syntax` function should return a syntax object with no lexical context"
|
|
|
|
;; see http://docs.racket-lang.org/guide/hash-lang_reader.html
|
|
|
|
(strip-context result-syntax))
|
|
|
|
;; "a `read-syntax` function should return a syntax object with no lexical context"
|
|
|
|
|
|
|
|
(strip-context result-syntax)) 'READ-SYNTAX))
|
|
|
|
|
|
|
|
|
|
|
|
(define (READ port)
|
|
|
|
(define INTERNAL-READ
|
|
|
|
; because `read` must produce a datum
|
|
|
|
(procedure-rename (λ (port)
|
|
|
|
(let ([output (calling-site-function #f port)])
|
|
|
|
; because `read` must produce a datum
|
|
|
|
(if (syntax? output)
|
|
|
|
(let ([output (calling-site-function #f port)])
|
|
|
|
(syntax->datum output)
|
|
|
|
(if (syntax? output)
|
|
|
|
output)))))]))
|
|
|
|
(syntax->datum output)
|
|
|
|
|
|
|
|
output))) 'READ)))))]))
|