diff --git a/regexp.rkt b/regexp.rkt index ce41936..dce53c0 100644 --- a/regexp.rkt +++ b/regexp.rkt @@ -29,6 +29,7 @@ (only-in racket/syntax format-id) syntax/id-table syntax/parse + syntax/stx trivial/private/common )) @@ -67,13 +68,15 @@ ;; For lets, (let-regexp: ([id val]) ...) (define-syntax let-f: (syntax-parser - [(_ ([name:id pat-stx]) e* (... ...)) - #:with pat-stx+ (expand-expr #'pat-stx) - #:with (num-groups . T) (count-groups #'pat-stx+) - (free-id-table-set! id+num-groups - #'name - (cons (syntax-e #'num-groups) #'T)) - #'(let ([name pat-stx+]) e* (... ...))] + [(_ ([name*:id pat-stx*] (... ...)) e* (... ...)) + #:with (pat-stx+* (... ...)) (stx-map expand-expr #'(pat-stx* (... ...))) + #:with ((num-groups* . T*) (... ...)) (stx-map count-groups #'(pat-stx+* (... ...))) + #'(let ([name* pat-stx+*] (... ...)) + (let-syntax ([name* (make-rename-transformer + (syntax-property #'name* + num-groups-key + (cons 'num-groups* #'T*)))] (... ...)) + e* (... ...)))] [(_ arg* (... ...)) #'(let arg* (... ...))])) ;; For definitions, (define-regexp: id val) diff --git a/test/regexp-pass.rkt b/test/regexp-pass.rkt index 7545077..a8004ad 100644 --- a/test/regexp-pass.rkt +++ b/test/regexp-pass.rkt @@ -126,6 +126,36 @@ byte-pregexp:))) ;; -- let-regexp: + (check-equal? + (ann + (let-regexp: ([rx #rx"^y(o+)lo$"]) + (cond + [(regexp-match: rx "yolo") + => (lambda ([x* : (List String String)]) + (cadr x*))] + ;=> cadr] + [else + (raise-user-error 'nope)])) + String) + "o") + + (check-equal? + (ann + (let-regexp: ([rx1 #rx"^y(o+)lo$"] + [rx2 #rx"^w(e+)pa$"] + [rx3 #rx"^y(e+)rrr$"]) + (cond + [(regexp-match: rx1 "wepa") + => cadr] + [(regexp-match: rx2 "yolo") + => cadr] + [(regexp-match: rx3 "yeeeeeerrr") + => cadr] + [else + (raise-user-error 'nope)])) + String) + "eeeeee") + (check-equal? (ann (let-regexp: ([rx "\\(\\)he(l*)(o*)"]) @@ -178,7 +208,7 @@ (ann (let-pregexp: ([rx #px"he(l*)(o*)"]) (regexp-match: rx "helllooo")) - (u #f (list string string string))) + (U #f (List String String String))) '("helllooo" "lll" "ooo")) ;; -- define-pregexp: @@ -187,7 +217,7 @@ (let () (define-pregexp: rx #px"he(l*)(o*)") (regexp-match: rx "helllooo")) - (u #f (list string string string))) + (U #f (List String String String))) '("helllooo" "lll" "ooo")) (check-equal?