[racket] syntax-case and multiple ellipsis

133 views
Skip to first unread message

Spencer Florence

unread,
Jun 17, 2014, 1:48:06 PM6/17/14
to racket
Hey all,

I'm trying to write a macro with syntax-case that looks something like this:

 (syntax-case stx (in def)
    [(_ name
        (def a ...) ...
        (in clause do ...) ...)
     stuff])

But, this gives me the error "syntax-case: misplaced ellipsis in pattern (follows other ellipsis)" on the last ellipsis. Does anyone know a way around this?

--spencer

Stephen Chang

unread,
Jun 17, 2014, 2:04:31 PM6/17/14
to Spencer Florence, racket
syntax-case won't let you do that, but syntax-parse will:

(syntax-parse #'(test A (def 1 2 3) (in 4 5 6)) #:datum-literals (in def)
[(_ name
(def a ...) ...
(in clause do ...) ...)
#'(a ... ... do ... ...)])
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
>
____________________
Racket Users list:
http://lists.racket-lang.org/users

Jens Axel Søgaard

unread,
Jun 17, 2014, 2:06:32 PM6/17/14
to Spencer Florence, racket
Hi Spencer,

The problem is that syntax-case pattern doesn't allow allow
pattern of the form (x ... y ...) that is with two ellipsis on the
same level.

Luckily syntax-parse allow these:

(require syntax/parse)

(syntax-parse #'(a a a a 41 42 43)
[(c ... d ...)
#''((c ...) (d ...))])

(syntax-parse #'(a a a a 41 42 43)
[(c:id ... d ...)
#''((c ...) (d ...))])


/Jens Axel

> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
>

--
--
Jens Axel Søgaard

Spencer Florence

unread,
Jun 17, 2014, 2:09:27 PM6/17/14
to Jens Axel Søgaard, racket
Unfortunately I'm working in typed racket, which (as far as I can tell) doesn't play nice with syntax-parse. Any other ideas?

Stephen Chang

unread,
Jun 17, 2014, 2:28:19 PM6/17/14
to Spencer Florence, Jens Axel Søgaard, racket
Hmm, you might need to provide some more details about what you are
trying to define then? (This particular example seems to work with
syntax-parse in TR)

#lang typed/racket
(require (for-syntax syntax/parse))

(define-syntax (test stx)
(syntax-parse stx #:datum-literals (in def)


[(_ name
(def a ...) ...
(in clause do ...) ...)

#'(list a ... ... do ... ...)]))

(test A (def 1 2 3) (in 4 5 6))

Jens Axel Søgaard

unread,
Jun 17, 2014, 2:39:00 PM6/17/14
to Spencer Florence, racket
Here is a syntax-case only solution:

(define-syntax (foo stx)
(syntax-case stx (in def)
[(_ name . more)
#'(collect-defs () name . more)]))

(define-syntax (collect-defs stx)
(syntax-case stx (in def)
[(_ (defs-seen ...) name (def a ...) . more)
#'(collect-defs (defs-seen ... (def a ...)) name . more)]
[(_ defs name . more)
#'(collect-ins defs () name . more)]))

(define-syntax (collect-ins stx)
(syntax-case stx (in def)
[(_ defs (ins-seen ...) name (in clause ...) . more)
#'(collect-ins defs (ins-seen ... (in clause ...)) name . more)]
[(_ defs ins name . more)
#'(finish defs ins name . more)]))

(define-syntax (finish stx)
(syntax-case stx ()
[(_ defs ins name . more)
#''((definitions: defs)
(ins: ins)
(name: name)
(more: more))]))

(foo "a-name"
(def x 41)
(def y 42)
(in a)
(in b)
"yada")

/Jens Axel

Spencer Florence

unread,
Jun 17, 2014, 2:47:33 PM6/17/14
to Stephen Chang, Jens Axel Søgaard, racket
Silly me, I was trying (require syntax/parse). Thanks!

Greg Hendershott

unread,
Jun 17, 2014, 2:48:18 PM6/17/14
to Stephen Chang, Spencer Florence, racket
Although it may not be the same issue, Alex Knauth posted about a
problem using syntax-parse with Typed Racket on May 27 (I didn't see
anyone reply):

http://lists.racket-lang.org/users/archive/2014-May/062730.html

On Tue, Jun 17, 2014 at 2:26 PM, Stephen Chang <stc...@ccs.neu.edu> wrote:
> Hmm, you might need to provide some more details about what you are
> trying to define then? (This particular example seems to work with
> syntax-parse in TR)
>
> #lang typed/racket
> (require (for-syntax syntax/parse))
>
> (define-syntax (test stx)
> (syntax-parse stx #:datum-literals (in def)
> [(_ name
> (def a ...) ...
> (in clause do ...) ...)
> #'(list a ... ... do ... ...)]))
>
> (test A (def 1 2 3) (in 4 5 6))
>
> On Tue, Jun 17, 2014 at 2:08 PM, Spencer Florence <spe...@florence.io> wrote:
>> Unfortunately I'm working in typed racket, which (as far as I can tell)
>> doesn't play nice with syntax-parse. Any other ideas?

Greg Hendershott

unread,
Jun 17, 2014, 2:58:59 PM6/17/14
to Spencer Florence, Jens Axel Søgaard, racket
> Silly me, I was trying (require syntax/parse). Thanks!

Yeah, if I had a nickel for every time I've forgotten to (require
(for-syntax syntax/parse)) I would be... well, not rich, but able to
buy a fancy coffee. Which if I'd already drank, I probably wouldn't
have made the mistake.

If someone were to inflict a Microsoft Office Clippy on us, one of its
features should probably be, "Hi! It looks like you're writing a
syntax-parse macro!" and offer to fix the require.
Reply all
Reply to author
Forward
0 new messages