[racket] Splicing Syntax Class Error?
There's a typo in the keyword in the second form of the macro on that
page, the one with the splicing syntax class: "though" instead of
"through". Mea culpa.
There's another lesson here, though, related to the fact that
syntax-parse (disappointingly) doesn't give you any help diagnosing this
typo. The reason is that the macro doesn't do any validation of the
"clause ..." terms. This kind of incomplete validation should probably
be considered bad macro design. Mea culpa again.
Here's a better version of the macro:
(define-syntax (mycond stx)
(define-splicing-syntax-class maybe-fallthrough-option
#:description #f
(pattern (~seq #:error-on-fallthrough who:expr)
#:with error? #'#t)
(pattern (~seq)
#:with error? #'#f
#:with who #'#f))
(define-syntax-class clause
(pattern (question:expr answer:expr)))
(syntax-parse stx
[(mycond fo:maybe-fallthrough-option c:clause ...)
#'(mycond* fo.error? fo.who c ...)]))
The "#:description #f" line is there to keep the errors simple enough
that syntax-parse will show you all of it. That's due to a limitation of
the way syntax-parse displays errors.
I'll update the docs with this point. Thanks for the question!
Ryan
On 06/09/2014 03:48 PM, Kevin Forchione wrote:
> Hi guys,
> In Syntax: Meta-Programming Helpers 1.2.2 Optional Keyword Arguments, the splicing syntax class example is returning an error for the keyword case:
>
> #lang racket
>
> (require (for-syntax syntax/parse))
>
> (define-syntax (mycond stx)
>
> (define-splicing-syntax-class maybe-fallthrough-option
> (pattern (~seq #:error-on-fallthough who:expr)
> #:with error? #'#t)
> (pattern (~seq)
> #:with error? #'#f
> #:with who #'#f))
>
> (syntax-parse stx
> [(mycond fo:maybe-fallthrough-option clause ...)
> #'(mycond* fo.error? fo.who clause ...)]))
>
> (define-syntax mycond*
> (syntax-rules ()
> [(mycond error? who [question answer] . clauses)
> (if question answer (mycond* error? who . clauses))]
> [(mycond #t who)
> (error who "no clauses matched")]
> [(mycond #f _)
> (void)]))
>
>> (mycond [(even? 13) 'blue]
> [(odd? 4) 'red])
>
>> (mycond #:error-on-fallthrough 'myfun
> [(even? 13) 'blue]
> [(odd? 4) 'red])
>
>
> mycond*: bad syntax in: (mycond* #f #f #:error-on-fallthrough (quote myfun) ((even? 13) (quote blue)) ((odd? 4) (quote red)))
>
> Am I missing something? Thanks!
>
> —Kevin
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
>