0

我正在创建一种基于 Racket 的新语言,并且我不希望某些#x宏起作用,例如 syntax-quote #'。如何删除它以便#'不执行语法引用,而是执行未绑定调度宏字符的任何操作?

我可以通过做单字符宏来做到这一点

(make-readtable (current-readtable)
                #\' #\a #f) ; set ' to be the same as a normal character

但我不知道如何为调度宏执行此操作。

4

1 回答 1

0

假设您希望#'被视为'

提供一个reader-proc简单地调用 normal 的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.


更新

假设您要#'导致读取错误,则"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)))))
于 2014-03-12T14:02:24.877 回答