[racket] Nested macros to refer to user's lexical context?

From: Sean Kanaley (skanaley at gmail.com)
Date: Fri Jan 24 12:18:19 EST 2014

A macro like

(define-syntax (mac stx)
  (... generate user scope function by passing stx to format-id ...))

doesn't work (function is unbound in phase 0) if I use optional parameters
and telescoping cases:

(syntax-case ...
  [(_ param ...) #'(mac some-default-thing param ...)]
  [(_ optional-thing param ...) #'the-main-expansion]

This is presumably because the second call to "mac" is now in a different
context.  The solution I have is to thread stx through recursive calls:

(define-syntax (mac stx)
  (define (aux original-stx stx)
    ( ... cases here and pass original-stx to format-id ... )
  (syntax-case stx ()
    [(orig rest ...) (aux #'orig #'(rest ...))]))

I was hoping there was something that stores the context of the original
macro call for just this kind of thing, so it could be written like the
first version but with permanent access to its original call's context
through nested expansion code, or perhaps I'm going about it wrong.  Also,
in the above example, orig != stx in the sense that passing stx to
format-id results in the undefined in phase 0 error:

(define-syntax (mac stx)
  (define (aux aux-stx)
    (... cases here and pass stx to format-id ...)
  (aux stx))

What makes that fail?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140124/f4f2df82/attachment.html>

Posted on the users mailing list.