我正在创建一种基于球拍的新语言,我不希望某些#x
宏工作,例如语法引用#'
。我如何删除它,使#'
不做语法引用,但做任何未绑定调度宏字符?
我可以通过
处理单字符宏(make-readtable (current-readtable)
#' #a #f) ; set ' to be the same as a normal character
但是我不知道如何为调度宏做这个
假设您希望将#'
视为'
:
提供一个reader-proc
,它只调用正常的read-syntax
:
#lang racket/base
(define (reader-proc ch in src line col pos)
(read-syntax src in))
(define our-readtable (make-readtable (current-readtable)
#'
'dispatch-macro
reader-proc))
;; A `#:wrapper1` for `syntax/module-reader`, i.e. to use in your
;; lang/reader.rkt
(define (wrapper1 thk)
(parameterize ([current-readtable our-readtable])
(thk)))
(provide wrapper1)
;; tests
(module+ test
(require rackunit
racket/port)
(parameterize ([current-readtable our-readtable])
(check-equal? (with-input-from-string "#'foo" read)
'foo)
(check-equal? (with-input-from-string "#'(foo)" read)
'(foo))
(check-equal? (with-input-from-string "#'(foo #'(bar))" read)
'(foo (bar)))))
使用'dispatch-macro
的一个稍微复杂的例子是我最近添加到#lang rackjure
的lambda读取器文字支持。
假设您希望#'
导致读错误,"bad syntax: #'"
:
#lang racket/base
(require syntax/readerr)
(define (reader-proc ch in src line col pos)
(raise-read-error (format "bad syntax: #~a" ch)
src line col pos 2))
(define our-readtable (make-readtable (current-readtable)
#'
'dispatch-macro
reader-proc))
;; A `#:wrapper1` for `syntax/module-reader`, i.e. to use in your
;; lang/reader.rkt
(define (wrapper1 thk)
(parameterize ([current-readtable our-readtable])
(thk)))
(provide wrapper1)
;; tests
(module+ test
(require rackunit
racket/port)
(parameterize ([current-readtable our-readtable])
(check-exn exn:fail? (λ () (with-input-from-string "#'foo" read)))))