[plt-scheme] Re: Splicing macro containing ...

From: Michele Simionato (michele.simionato at gmail.com)
Date: Thu Apr 30 10:07:03 EDT 2009

On Apr 30, 3:47 pm, Noel Welsh <noelwe... at gmail.com> wrote:
> Hello all,
>
> Another macro question. I'm trying to splice a macro containing
> ellipses into an expression context, and I'm getting the error
>
>   syntax: no pattern variables before ellipses in template in: ...
>
> Only an ellipsis causes an error; other macros and normal expressions
> work fine. My setup is a little convoluted -- I'm defining a macro
> that defines a macro. Both macros specify imports and exports. The
> names specified in the top macro are names used in the body. The names
> specified in the internal macro specify actual imports and exports. So
> this system is something like a unit, where the internal names may
> differ to the actual names the abstraction is linked against and
> exports. Code illustrating the problem is below; hopefully this will
> be clearer than my explanation.
>
> Thanks for all help,
> N.
>
> #lang scheme/base
>
> (require
>  (for-syntax scheme/base
>              syntax/stx))
>
> (define-syntax (define-foo stx)
>   (syntax-case stx (import export)
>     [(define-foo name
>        (import import-id)
>        (export export-id)
>        body-expr ...)
>      #`(define-syntax (name stx)
>          (syntax-case stx (import export)
>            [(name
>              (import real-import-id)
>              (export real-export-id))
>             #`(begin
>                 (define import-id real-import-id)
>                 (begin
>                   body-expr ...)
>                 (define real-export-id export-id))]))]))
>
> (define-foo bar
>   (import baz)
>   (export number)
>   (define number
>     (baz 1 2 3)))
>
> (bar
>  (import +)
>  (export magic-number))
>
> magic-number
>
> (define-foo broken
>   (import baz)
>   (export number)
>   (define-syntax quux
>     (syntax-rules ()
>       [(quux a b c ...)
>        (baz a b c ...)]))
>   (define number
>     (quux 1 2 3)))
>
> (broken
>  (import +)
>  (export broken-number))
>
> broken-number
> _________________________________________________
>   For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Try this:

(define-syntax (define-foo stx)
  (syntax-case stx (import export)
    [(define-foo name
       (import import-id)
       (export export-id)
       body-expr ...)
     #`(define-syntax (name stx)
         (syntax-case stx (import export)
           [(name
             (import real-import-id)
             (export real-export-id))
            #`(begin
                (define import-id real-import-id)
                (begin
                  (... (... body-expr)) ...)
                (define real-export-id export-id))]))]))

Ellipsis are always tricky ...

    Michele Simionato


Posted on the users mailing list.