improve template->pattern, used in syntax-lens

This commit is contained in:
Alex Knauth 2016-07-24 20:55:42 -04:00 committed by AlexKnauth
parent 7274791b7d
commit 27806613c0

View File

@ -53,14 +53,15 @@
[(a ...) (ormap (target-stx target-id) (syntax->list #'(a ...)))]
[a (and (bound-identifier=? target-id #'a) #'a)]))
(define template->pattern
(syntax-parser #:literals (_)
[(a ...) #`(#,@(stx-map template->pattern #'(a ...)))]
[_ (generate-temporary)]
(define (template->pattern stx)
(syntax-parse stx
;; preserve lexical context, source location, and properties
[(a ...) (datum->syntax stx (stx-map template->pattern #'(a ...)) stx stx)]
[(~literal _) (generate-temporary)]
[a #'a]))
(define ((template-rebuilder target-id) parse-pattern)
(with-syntax ([pat parse-pattern])
#`(lambda (stx)
(with-syntax ([#,target-id stx])
#'pat)))))
#'pat)))))