Hi, Racket Discourse.
It is a public holiday in RSA tomorrow, so today is Friday, haha.
I thought I would share a riff on a pattern-expander
. From the phc-toolkit
, there is a pattern-expander
called ~maybe
that looks like:
(~optional {~seq pattern ...})
I have been using this for recognizing parts of a URL in a constructor-macro, which works great for sequences that may or may not be there.
But, sometimes you'd like to know whether the pattern actually matched, or whether it fell through. In my case, because I use this to keep track of whether a pattern's result should be used to set!
a url
's field or not, when applicable.
A simple modification to this (perhaps there is an even simpler one?), to allow this check, looks like this:
(define-splicing-syntax-class
none (pattern {~seq}))
(define-syntax ~maybe
(pattern-expander
(lambda (stx)
(syntax-case stx ()
[(_ isa pat ...)
#'(~optional {~seq (~var isa none) pat ...} #:defaults ([isa #false]))]))))
Then, you can write:
(define-splicing-syntax-class
fragment
#:description
"an optional, keyword-delimited url fragment"
#:attributes
([fragment 0] [$fragment 0] [fragment? 0])
(pattern {~maybe fragment? #:focus :fragment-value}
#:attr fragment #'(~? val #false)
#:attr $fragment #'(~? arg ())))
where the val
and arg
attributes come from :fragment-value
, this case.
Mooiloop!