[racket] Many datums in syntax-parse
2013/1/22 Danny Yoo <dyoo at hashcollision.org>:
>> Is there an equivalent to #:datums (old-timestamp time-of-day …)
>> in syntax-parse?
A nice puzzle. Now Ryan added #literal-datums that's of course
the way to go, but here is a solution.
First define a syntax class that matches a literal datum.
(define-syntax-class <drawing> (pattern (~datum drawing)))
Then make the convention that drawing has the syntax class <drawing>.
(define-conventions drawing-datum
[#rx"^drawing$" <drawing>]
Finally use
(syntax-parse stx
#:conventions (drawing-datum)
...)
The complete examples is as follows:
#lang racket/base
(require ragg/examples/simple-line-drawing/lexer
ragg/examples/simple-line-drawing/grammar
syntax/parse
(for-syntax syntax/parse)
(for-syntax racket/base))
(define-syntax (define-datums stx)
(syntax-parse stx
[(_ name ...)
#'(begin
(define-syntax-class name (pattern (~datum name)))
...)]))
(define-datums drawing row repeat chunk)
(define-conventions simple-line-datums
[#rx"^drawing$" drawing]
[#rx"^row$" row]
[#rx"^repeat$" repeat]
[#rx"^hunk$" chunk])
(define an-stx
(parse (tokenize (open-input-string "3 9 X;
6 3 b 3 X 3 b;
3 9 X;"))))
(syntax-parse an-stx
#:conventions (simple-line-datums)
[(drawing r ...)
(printf "I see: ~s\n" #'(r ...))])
--
Jens Axel Søgaard