[racket] Improvement of a syntax-parse syntax class definition

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Thu Jun 26 15:44:52 EDT 2014

Hi All,

The example program below defines a macro, my-let, that
enables you to write let-bindings with / as a separator.

  (my-let (x = 1 / y = 2 / z = 3)
          (+ x y z))

I'd like to improve the definition of the syntax class called bindings.
Ideally I'd like to replace its definition with:

(begin-for-syntax
  (define-splicing-syntax-class bindings
    [pattern (~separated / b:binding)
             #:with names #'(b.name ...)
             #:with expressions #'(b.expression ...)]))

But how can I define the fictitious ~separated ?

/Jens Axel


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

(begin-for-syntax
  (define-splicing-syntax-class binding #:literals (=)
    [pattern (~seq name:id = expression:expr)])

  (define-splicing-syntax-class bindings
    [pattern ()
             #:with names #'()
             #:with expressions #'()]
    [pattern (b:binding)
             #:with names #'(b.name)
             #:with expressions #'(b.expression)]
    [pattern (b:binding (~seq / b*:binding) ...)
             #:with names #'(b.name b*.name ...)
             #:with expressions #'(b.expression b*.expression ...)]))

(define-syntax (my-let stx)
  (syntax-parse stx
    [(_ bs:bindings body)
     (with-syntax ([(name ...) #'bs.names]
                   [(expr ...) #'bs.expressions])
       #'(let ([name expr] ...)
           body))]))

(my-let (x = 1 / y = 2 / z = 3)
        (+ x y z))

--
Jens Axel Søgaard


Posted on the users mailing list.