[plt-scheme] Getting the expansion location

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue May 13 11:02:13 EDT 2003

At Tue, 13 May 2003 07:46:08 -0700 (PDT), Noel Welsh wrote:
> (define-syntax name
>   (lambda (stx)
>     (syntax-case stx ()
>       ((name param ...)
>        (syntax/loc stx 
>          (if (begin expr ...)
>              #t
>              (fail-assertion reported-name
>                              (quote-syntax stx)
>                              ""
> (current-continuation-marks)
>                              param ...))))))) 
> I get a syntax object but the location is always the
> file (assert.ss) where I define this macro, not the
> location where this macro is expanded.  How can I get
> the expansion location?

When you wrote

  (quote-syntax stx)

you probably meant for the `stx' to be replaced by the value of the
`stx' argument. But `stx' isn't bound as syntax, so the expansion of a
`name' use includes literally

  (quote-syntax stx)

I think you want something like this:

 (define-syntax name
   (lambda (stx)
     ((name param ...)
      (with-syntax ([loc-holder (datum->syntax-object #f 'loc-holder stx)])
         (fail-assertion reported-name
                         (quote-syntax loc-holder)

In the above example, I threw away the structre of stx and just
attached its source information to a `loc-holder' identifier. If you
want to keep the full structure of stx, just replace the
`datum->syntax-object' call with `stx'.


Posted on the users mailing list.