[plt-scheme] syntax/cx+loc

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Mar 22 11:52:32 EST 2005

At Tue, 22 Mar 2005 11:19:37 -0500, Doug Orleans wrote:
> (module m2 mzscheme
>   (provide helper2)
>   (define (helper2 stx)
>     (syntax-case stx ()
>       ((a . b)
>        (syntax (list . b))))))
> 
> (module m3 mzscheme
>   (require-for-syntax m2)
>   (provide macro2)
>   (define-syntax (macro2 stx)
>     (helper2 stx)))
> 
> (require m3)
> (macro2 1 2 3) ; => stdin::324: compile: bad syntax; function application is 
> not allowed, because no #%app syntax transformer is bound in: (list 1 2 3)

Probably the best solution to this problem is to add 

  (require-for-template mzscheme)

to `m2'.

> So instead of using `syntax' (or `syntax/loc', which copies the source
> location information), now I use `syntax/cx+loc', which copies both
> the lexical context and source location information:
> 
>   (define-syntax syntax/cx+loc
>     (syntax-rules ()
>       ((_ source-stx-expr template-expr)
>        (let ((source-stx source-stx-expr))
> 	 (datum->syntax-object
> 	  source-stx (syntax-e #'template-expr) source-stx)))))
> 
> This seems to work fine, so I thought I'd share it.

I think this is probably not want you want. First, it makes your macro
slightly non-hygienic. Second, if you use `syntax/cx+loc' instead of
`syntax' in `m2' above, you end up with an `#%app' binding in your
example, but not a `list' binding.

Matthew



Posted on the users mailing list.