[plt-scheme] Extending macro pattern syntax

From: Jens Axel Soegaard (jensaxel at soegaard.net)
Date: Sat Jan 31 09:30:45 EST 2009

James Coglan wrote:

> Following on from some macro pattern stuff I've brought up before, I was 
> wondering whether it's possible to extend the syntax of macro patterns 
> using Scheme macros? Specifically, is it possible to create your own 
> macro syntax that would allow ellipses in places other than tail position?

It is in possible to define a macro define-my-syntax that expands into
ordinary uses of define-syntax, so the answer is yes.

> For example, there's no reason the pattern (_ (name value) ... expr) 
> could not be made to work, since 'expr' is a less specific matcher than 
> '(name value)'. '(name value) ...' would consume input expressions until 
> it hit a non-list expression, then 'expr' would take over. So firstly, 
> are there counterexamples to prove this wouldn't work, and if it would 
> work, can you implement it using Scheme macros themselves? I'd also like 
> to do something similar with keywords, so that for example (_ expr ... 
> stop stmt ...) would work if 'stop' is a keyword for the syntax.

And in fact it already works in PLT Scheme:

#lang scheme

(define-syntax foo
   (syntax-rules ()
     [(foo a b ... c)
      '(a b ... c)]))

(foo 1 2 3) ; =: (1 2 3)

(define-syntax bar
   (syntax-rules ()
     [(foo a b ... ignore)
      '(a b ...)]))

(bar 1 2 3) ; => (1 2)

Jens Axel Søgaard

Posted on the users mailing list.