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

From: Alexander D. Knauth (alexander at knauth.org)
Date: Thu Jun 26 23:55:56 EDT 2014

Well, I made my own modified version of syntax/parse that lets you define ~separated as a pattern-expander that let’s you do this:
#lang racket
(require (for-syntax syntax-parse-with-pattern-expanders
                     (for-syntax racket/base)))

(begin-for-syntax
  (define-splicing-syntax-class binding #:literals (=)
    [pattern (~seq name:id = expr:expr)])
  
  (define-syntax ~separated
    (pattern-expander
     (lambda (stx)
       (syntax-case stx ()
         [(separated sep pat)
          (with-syntax ([ooo '...])
            #'((~seq pat (~or (~peek-not _)
                              (~seq sep (~peek _))))
               ooo))]))))
  
  (define-splicing-syntax-class bindings
    [pattern (~separated (~datum /) b:binding)
             #:with (name ...) #'(b.name ...)
             #:with (expr ...) #'(b.expr ...)]))

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

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

It’s on github here:
https://github.com/AlexKnauth/syntax-parse-with-pattern-expanders

I haven’t tested it on anything other than this example though.  

On Jun 26, 2014, at 3:44 PM, Jens Axel Søgaard <jensaxel at soegaard.net> wrote:

> 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
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140626/a8df164a/attachment.html>

Posted on the users mailing list.