Added if-attribute

This commit is contained in:
Georges Dupéron 2016-09-19 21:45:20 +02:00
parent 9a8a6f83ff
commit 16290d0fcc
2 changed files with 17 additions and 4 deletions

View File

@ -2,11 +2,18 @@
(require (for-syntax racket/base)) (require (for-syntax racket/base))
(provide try-attribute) (provide try-attribute if-attribute)
(define-syntax (if-attribute stx)
(syntax-case stx ()
[(_ name if-branch else-branch)
(if (syntax-pattern-variable? (syntax-local-value #'name (λ () #f)))
#'if-branch
#'else-branch)]))
(define-syntax (try-attribute stx) (define-syntax (try-attribute stx)
(syntax-case stx () (syntax-case stx ()
[(_ name fallback)
#'(if-attribute (attribute name) fallback)]
[(_ name) [(_ name)
(if (syntax-pattern-variable? (syntax-local-value #'name (λ () #f))) #'(if-attribute (attribute name) #f)]))
#'(attribute name)
#'#f)]))

View File

@ -352,6 +352,12 @@ appear in the matched syntax (aside from issues related to greediness).
declared, while using the operation's neutral element to not alter the final declared, while using the operation's neutral element to not alter the final
result.} result.}
@defform[(if-attribute #,tribute-name if-branch else-branch)]{
This macro expands to @racket[if-branch] if @racket[#,tribute-name] is bound as
a syntax pattern variable, and to @racket[else-branch] otherwise.
The same caveats as for @racket[try-attribute] apply.}
@defform[(~named-seq #,tribute-name #,ntax-pattern ...)]{ @defform[(~named-seq #,tribute-name #,ntax-pattern ...)]{
Equivalent to @racket[{~seq #,ntax-pattern ...}], but also binds the Equivalent to @racket[{~seq #,ntax-pattern ...}], but also binds the
@racket[#,tribute-name] to the whole sequence. If the sequence appears inside @racket[#,tribute-name] to the whole sequence. If the sequence appears inside