From 16290d0fcc36f1f0d084c4d4987524c80886506b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= Date: Mon, 19 Sep 2016 21:45:20 +0200 Subject: [PATCH] Added if-attribute --- private/try-attribute.rkt | 15 +++++++++++---- .../extensible-parser-specifications.scrbl | 6 ++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/private/try-attribute.rkt b/private/try-attribute.rkt index 81c0277..cdc3ad9 100644 --- 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 index 5008599..b41d32b 100644 --- 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