[racket] Macros With Multiple Ellipsis

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Wed Jan 8 15:32:43 EST 2014

Hi Scott,

I suspect this is a bug in syntax-case. The work-around
is luckily very simple. Just use syntax-parse.

(require (for-syntax syntax/parse))
(define-syntax (define/test stx)
  (syntax-parse stx
[(_ (id arg ...) body ... #:test test-expr ...)
#'(define (id arg ...) body ... test-expr ...)]))

(define/test (my-fn a b c)
  (print a)
  (print b)
  (print c)
  #:test
  (print 'test))

/Jens Axel


2014/1/8 Scott Klarenbach <scott at pointyhat.ca>:
> I have a macro that modifies define to perform some additional processing.
>
> (define-syntax (define/test stx)
>   (syntax-case stx ()
> [(_ (id arg ...) body ... #:test test-expr)
> #'(define (id arg ...) body ... test-expr)]))
>
> (define/test (my-fn a b c)
>   (print a)
>   (print b)
>   (print c)
>   #:test
>   (print 'test))
>
> This works, but only if test-expr is one expression or wrapped in a begin or
> something.  I'd like the test-expr to be the same as the define body.  I
> wish I could write:
>
> (define-syntax (define/test stx)
>   (syntax-case stx ()
> [(_ (id arg ...) body ... #:test test-expr ...)
>  #'(define (id arg ...) body ... test-expr ...)]))
>
> But I get a "misplaced ellipsis in pattern" error.
>
> I could change the pattern to something like (body ...) #:test (test-body
> ...), but now the syntax of the macro has changed to include the extra
> parenthesis.
>
> Am I making a simple mistake?  Or am I resigned to manually parsing out the
> single test-expr into multiple forms in the template?
>
> Thanks.
> --
> Talk to you soon,
>
> Scott Klarenbach
>
> PointyHat Software Corp.
> www.pointyhat.ca
> p 604-568-4280
> e scott at pointyhat.ca
> 200-1575 W. Georgia
> Vancouver, BC V6G2V3
>
> _______________________________________
> To iterate is human; to recur, divine
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>



-- 
--
Jens Axel Søgaard


Posted on the users mailing list.