On Tuesday, March 14, 2017 at 11:05:08 AM UTC-7, Stephen Chang wrote: > > 1) Use a commit pattern, which tells syntax-parse not to backtrack > past the commit point. You have to change the order of patterns for > this to work. > [...] > 2) Use the ~post pattern, which essentially tells syntax-parse to give > priority to a particular pattern. >
Both these suggestions have been helpful, in addition to these from Philip McGrath: > > This works: > #lang racket/base > > (require > (for-syntax > racket/base > syntax/parse > )) > > (define-syntax (test stx) > > (define-splicing-syntax-class maybe > #:datum-literals (:thing) > (pattern (~seq :thing n:id)) > (pattern (~seq ) #:attr n #'0)) > > (syntax-parse stx > #:datum-literals (:thing) > ((_ a:maybe > (~describe "parenthesized sequence of identifiers" > (b:id ...))) > #''(a.n b ...)))) > > (test (x y z)) > (test :thing v (x y z)) > (test :thung v (x y z)) > > You could also do this using ~optional instead of a syntax class if you want: > (define-syntax (test stx) > (syntax-parse stx > #:datum-literals (:thing) > ((_ (~optional (~seq :thing n:id) > #:defaults ([n #'0])) > (~describe "parenthesized sequence of identifiers" > (b:id ...))) > #''(n b ...)))) -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.