Allow a whole ~seq's contents in define-syntax/parse+simple and define/syntax-parse+simple, not just a single (splicing) syntax class.

This commit is contained in:
Georges Dupéron 2016-08-30 02:06:58 +02:00
parent 783641abd8
commit a7dd0c0759
2 changed files with 29 additions and 12 deletions

View File

@ -16,17 +16,33 @@
phc-toolkit/untyped phc-toolkit/untyped
(prefix-in syntax/parse: syntax/parse/private/residual-ct))) (prefix-in syntax/parse: syntax/parse/private/residual-ct)))
(define-simple-macro (define-syntax/parse+simple [name stxclass] . body) (define-simple-macro (define-syntax/parse+simple [name . args] . body)
#:with name-forward (format-id #'name "~a-forward-attributes" #'name) #:with name-forward (format-id #'name "~a-forward-attributes" #'name)
#:with tmp-forward (format-id #'tmp "~a-forward-attributes" #'tmp) #:with tmp-forward (format-id #'tmp "~a-forward-attributes" #'tmp)
(begin (begin
(begin-for-syntax (begin-for-syntax
(define/syntax-parse+simple [tmp stxclass] . body) (define/syntax-parse+simple [tmp . args] . body)
(define-syntax name-forward (make-rename-transformer #'tmp-forward))) (define-syntax name-forward (make-rename-transformer #'tmp-forward)))
(define-syntax name tmp))) (define-syntax name tmp)))
(begin-for-syntax (begin-for-syntax
(define-syntax define/syntax-parse+simple (define-syntax (define/syntax-parse+simple stx)
(syntax-case stx ()
[(_ [name . args] . body)
(let ()
(define introducer (make-syntax-introducer))
(define/with-syntax args-stxclass
(introducer (datum->syntax #'args 'args-stxclass) 'add))
(define/with-syntax body-introduced
(introducer #'body 'add))
#'(begin
(define-syntax-class args-stxclass
#:auto-nested-attributes
(pattern args))
(define/syntax-parse+simple/stxclass [name args-stxclass]
. body-introduced)))]))
(define-syntax define/syntax-parse+simple/stxclass
(syntax-parser (syntax-parser
[(_ [name (~var cls (static syntax/parse:stxclass? "a syntax class"))] [(_ [name (~var cls (static syntax/parse:stxclass? "a syntax class"))]
. body) . body)
@ -47,7 +63,7 @@
(define (name stx2) (define (name stx2)
(syntax-parameterize ([stx (make-rename-transformer #'stx2)]) (syntax-parameterize ([stx (make-rename-transformer #'stx2)])
(syntax-parse stx2 (syntax-parse stx2
[(name colon-stxclass) . body]))) [(name . colon-stxclass) . body])))
(define (private-simple-api stx/arg attr-name/arg ) (define (private-simple-api stx/arg attr-name/arg )
(syntax-parameterize ([stx (make-rename-transformer #'stx/arg)]) (syntax-parameterize ([stx (make-rename-transformer #'stx/arg)])
(syntax-parse #'nothing (syntax-parse #'nothing

View File

@ -6,20 +6,21 @@
"test-structure-options.rkt" "test-structure-options.rkt"
syntax/parse)) syntax/parse))
(define-syntax/parse+simple [foo structure-kws] (define-syntax/parse+simple [foo foo-a :structure-kws]
#''(field ...)) #''(foo-a field ...))
(check-equal? (foo [f tf] [g tg]) (check-equal? (foo #:first-case [f tf] [g tg])
'(f g)) '(#:first-case f g))
(begin-for-syntax (begin-for-syntax
(define-splicing-syntax-class structure-xyz-kws (define-splicing-syntax-class structure-xyz-kws
(pattern {~seq-no-order {~optional {~seq #:xyz xyz:id}} (pattern {~seq-no-order {~optional {~seq #:xyz xyz:id}}
{structure-kw-all-mixin}}))) {structure-kw-all-mixin}})))
(define-syntax/parse [bar :structure-xyz-kws] (define-syntax/parse [bar foo-a :structure-xyz-kws]
#`'[(xyz field ...) #`'[(xyz foo-a field ...)
#,(foo-forward-attributes)]) #,(foo-forward-attributes)])
(check-equal? (bar #:xyz zyx [f tf] [g tg]) (check-equal? (bar #:second-case #:xyz zyx [f tf] [g tg])
'((zyx f g) (quote (f g)))) '((zyx #:second-case f g)
(quote (#:second-case f g))))