[racket] Many datums in syntax-parse

From: Lorenz Köhl (rainbowtwigs at gmail.com)
Date: Mon Jan 21 15:04:17 EST 2013

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]))



Posted on the users mailing list.