[racket] Getting context info from ellipses pattern vars when calling DATUM->SYNTAX

From: Sam Tobin-Hochstadt (samth at ccs.neu.edu)
Date: Sun Nov 14 21:52:04 EST 2010

On Sat, Nov 13, 2010 at 5:09 PM, D Marshall <dmarshall207 at gmail.com> wrote:
>
> I understand that DATUM->SYNTAX gets it's context
> information from it's first argument.  I have no problems
> when using simple (non-ellipses) pattern variables, however,
> I haven't been unable to get a strictly "ellipses pattern"
> to work in this context (no pun intended).

Here are two options:

This is my preference - use the context of the macro application
    (define-syntax test-this
      (lambda (x)
        (syntax-case x ()
          [(_ a ...)
             (with-syntax
              ([form    (datum->syntax x
                           `(+ ,@(syntax->datum (syntax (a ...)))))])
              (syntax form))])))

This is also common, but can break in some cases:
    (define-syntax test-this
      (lambda (x)
        (syntax-case x ()
          [(kw a ...)
             (with-syntax
              ([form    (datum->syntax (syntax kw)
                           `(+ ,@(syntax->datum (syntax (a ...)))))])
              (syntax form))])))

The reason what you tried to do didn't work is that you need a context
that is from the context of the macro use.  The rest of the list isn't
something the programmer originally wrote, but something that was
generated by your macro, so it doesn't have any useful context.
-- 
sam th
samth at ccs.neu.edu


Posted on the users mailing list.