Capturing a variable number of arguments via an ellipsis in a nested macro; Missing pattern variable error

[亡魂溺海] 提交于 2019-12-05 10:13:06

To “escape” ellipses in syntax templates, you can use the syntax (... <form>), where <form> is a syntax template where ... sequences are treated literally. Therefore, you can wrap a piece of syntax to include literal ellipses:

> #'(... (syntax-rules ()
           [(x ...) (list x ...)]))
#<syntax:4:9 (syntax-rules () ((x ...) (li...>

You can use this to surround your inner macro definition to escape the inner ellipses:

(define-syntax (outer-macro stx)
  (syntax-parse stx
    [(_ body:expr ...+)
     #'(syntax-parameterize
        ([inner-macro
          (lambda (stx)
            (... (syntax-case stx ()
                   [(_ value ...)
                    (printf "values are: ~a~n" (list value ...))])))])
        body ...)]))

However, this is actually not quite right, because your syntax-case body is wrong—it does not return a syntax object. You are just missing a #' before the (printf ...) (or you could use syntax-rules), so the correct implementation should be the following:

(define-syntax (outer-macro stx)
  (syntax-parse stx
    [(_ body:expr ...+)
     #'(syntax-parameterize
        ([inner-macro
          (lambda (stx)
            (... (syntax-case stx ()
                   [(_ value ...)
                    #'(printf "values are: ~a~n" (list value ...))])))])
        body ...)]))

This should work as intended.

Alexis King's answer is good. However another way to do it, which I find simpler to think about, is to use a #:with pattern (or a with-syntax), to define something like ooo as a literal ellipsis.

You can create a literal ellipsis with quote-syntax, so the #:with clause looks like #:with ooo (quote-syntax ...). Then you use ooo whenever you want to generate an ellipsis in the output of the macro.

(define-syntax (outer-macro stx)
  (syntax-parse stx
    [(_ body:expr ...+)
     #:with ooo (quote-syntax ...)
     #'(syntax-parameterize
        ([inner-macro
          (lambda (stx)
            (syntax-case stx ()
              [(_ value ooo)
               #'(printf "values are: ~a~n" (list value ooo))]))])
    body ...)]))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!