commit 16290d0fcc36f1f0d084c4d4987524c80886506b
parent 9a8a6f83ffe5c13f4b8501c21f0be9063cae37d0
Author: Georges Dupéron <georges.duperon@gmail.com>
Date: Mon, 19 Sep 2016 21:45:20 +0200
Added if-attribute
Diffstat:
2 files changed, 18 insertions(+), 5 deletions(-)
diff --git a/private/try-attribute.rkt b/private/try-attribute.rkt
@@ -2,11 +2,18 @@
(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)
(syntax-case stx ()
+ [(_ name fallback)
+ #'(if-attribute (attribute name) fallback)]
[(_ name)
- (if (syntax-pattern-variable? (syntax-local-value #'name (λ () #f)))
- #'(attribute name)
- #'#f)]))
-\ No newline at end of file
+ #'(if-attribute (attribute name) #f)]))
+\ No newline at end of file
diff --git a/scribblings/extensible-parser-specifications.scrbl b/scribblings/extensible-parser-specifications.scrbl
@@ -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
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 ...)]{
Equivalent to @racket[{~seq #,ntax-pattern ...}], but also binds the
@racket[#,tribute-name] to the whole sequence. If the sequence appears inside