A splicing syntax class that I have is defined as follows. The syntax class matches a sequence of two statements (first pattern), one of the statements (third and second patterns) and perhaps even none of those statements at all (last pattern).
As you can see there is quite a lot of "duplicate" code, because every pattern returns either the attributes of something captured in the pattern, or an empty thing otherwise. The problem I have is that currently the statement is never truly optional, since the last pattern must match something. In this case an empty set of brackets ()
.
The question is: how can I make the statement truly optional? As a side question - can the code be condensed by making better use of head patterns?
(define-splicing-syntax-class signal-subscriptions-and-declarations
#:description "subscriptions to signals and signal declarations"
; Match the case where both a subscription and declaration statement is present
(pattern (~seq subscribed:signal-subscriptions declared:signal-declarations)
#:with (subscription-collection ...) #'(subscribed.signal-collection ...)
#:with (subscription-signal-id ...) #'(subscribed.signal-identifier ...)
#:with (declaration-signal-id ...) #'(declared.signal-identifier ...))
; Match the case where no declaration statement is present
(pattern subscribed:signal-subscriptions
#:with (subscription-collection ...) #'(subscribed.signal-collection ...)
#:with (subscription-signal-id ...) #'(subscribed.signal-identifier ...)
#:with (declaration-signal-id ...) #'())
; Match the case where no subscription statement is present
(pattern declared:signal-declarations
#:with (subscription-collection ...) #'()
#:with (subscription-signal-id ...) #'()
#:with (declaration-signal-id ...) #'(declared.signal-identifier ...))
(pattern ()
#:with (subscription-collection ...) #'()
#:with (subscription-signal-id ...) #'()
#:with (declaration-signal-id ...) #'()))
signal-subscriptions-and-declarations
you need to name the subscriptions and declarations pattern variables, and have a#:with
clause for everything you want to provide to users of the class (from the subscriptions and declarations variables) – Bedew