[racket] Many datums in syntax-parse

From: Greg Hendershott (greghendershott at gmail.com)
Date: Mon Jan 21 18:33:24 EST 2013

Have you tried using define-syntax-class to define some of the
"intermediate chunks"?

For example would it help if you were to define a syntax class for
old-date, another for time-of-day, and compose old-timestamp from
those?

On Mon, Jan 21, 2013 at 3:04 PM, Lorenz Köhl <rainbowtwigs at gmail.com> wrote:
> How can I match datums more concisely with syntax-parse?
>
> I get syntax objects like this from ragg:
>
> #'(old-timestamp (old-date 2 23) (time-of-day 2 ":" 23 ":" 42))
>
> The parts I care about are the numbers. I write patterns like
>
> ((~datum old-timestamp)
>       ((~datum old-date) month day)
>       ((~datum time-of-day) hour ":" minute ":" second))
>
> to extract them. But with longer syntax the patterns gets messy. I tried #:literals (foo ..) but I don't know what binding I should give to the ids and how (and why).
>
> Is there an equivalent to #:datums (old-timestamp time-of-day …) in syntax-parse?
>
> Longer example:
>
> ;; timestamp-stx->list : Syntax -> (Listof Number)
> (define (timestamp-stx->list stx)
>   (define-syntax-rule (! s)
>     (syntax->datum s))
>
>   (syntax-parse stx
>     ;#:literals (old-timestamp old-date time-of-day new-timestamp new-date)
>     [((~datum old-timestamp)
>       ((~datum old-date) month day)
>       ((~datum time-of-day) hour ":" minute ":" second))
>
>      (list (! #'second) (! #'minute) (! #'hour) (! #'day) (month->num (! #'month)))]
>
>     [((~datum new-timestamp)
>       ((~datum new-date) year "-" month "-" day "T")
>       ((~datum time-of-day) hour ":" minute ":" second))
>      1]))
>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users


Posted on the users mailing list.