[let-regexp] working now
This commit is contained in:
parent
1f114d8fc5
commit
d768e59ca2
17
regexp.rkt
17
regexp.rkt
|
@ -29,6 +29,7 @@
|
||||||
(only-in racket/syntax format-id)
|
(only-in racket/syntax format-id)
|
||||||
syntax/id-table
|
syntax/id-table
|
||||||
syntax/parse
|
syntax/parse
|
||||||
|
syntax/stx
|
||||||
trivial/private/common
|
trivial/private/common
|
||||||
))
|
))
|
||||||
|
|
||||||
|
@ -67,13 +68,15 @@
|
||||||
;; For lets, (let-regexp: ([id val]) ...)
|
;; For lets, (let-regexp: ([id val]) ...)
|
||||||
(define-syntax let-f:
|
(define-syntax let-f:
|
||||||
(syntax-parser
|
(syntax-parser
|
||||||
[(_ ([name:id pat-stx]) e* (... ...))
|
[(_ ([name*:id pat-stx*] (... ...)) e* (... ...))
|
||||||
#:with pat-stx+ (expand-expr #'pat-stx)
|
#:with (pat-stx+* (... ...)) (stx-map expand-expr #'(pat-stx* (... ...)))
|
||||||
#:with (num-groups . T) (count-groups #'pat-stx+)
|
#:with ((num-groups* . T*) (... ...)) (stx-map count-groups #'(pat-stx+* (... ...)))
|
||||||
(free-id-table-set! id+num-groups
|
#'(let ([name* pat-stx+*] (... ...))
|
||||||
#'name
|
(let-syntax ([name* (make-rename-transformer
|
||||||
(cons (syntax-e #'num-groups) #'T))
|
(syntax-property #'name*
|
||||||
#'(let ([name pat-stx+]) e* (... ...))]
|
num-groups-key
|
||||||
|
(cons 'num-groups* #'T*)))] (... ...))
|
||||||
|
e* (... ...)))]
|
||||||
[(_ arg* (... ...))
|
[(_ arg* (... ...))
|
||||||
#'(let arg* (... ...))]))
|
#'(let arg* (... ...))]))
|
||||||
;; For definitions, (define-regexp: id val)
|
;; For definitions, (define-regexp: id val)
|
||||||
|
|
|
@ -126,6 +126,36 @@
|
||||||
byte-pregexp:)))
|
byte-pregexp:)))
|
||||||
|
|
||||||
;; -- let-regexp:
|
;; -- 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?
|
(check-equal?
|
||||||
(ann
|
(ann
|
||||||
(let-regexp: ([rx "\\(\\)he(l*)(o*)"])
|
(let-regexp: ([rx "\\(\\)he(l*)(o*)"])
|
||||||
|
@ -178,7 +208,7 @@
|
||||||
(ann
|
(ann
|
||||||
(let-pregexp: ([rx #px"he(l*)(o*)"])
|
(let-pregexp: ([rx #px"he(l*)(o*)"])
|
||||||
(regexp-match: rx "helllooo"))
|
(regexp-match: rx "helllooo"))
|
||||||
(u #f (list string string string)))
|
(U #f (List String String String)))
|
||||||
'("helllooo" "lll" "ooo"))
|
'("helllooo" "lll" "ooo"))
|
||||||
|
|
||||||
;; -- define-pregexp:
|
;; -- define-pregexp:
|
||||||
|
@ -187,7 +217,7 @@
|
||||||
(let ()
|
(let ()
|
||||||
(define-pregexp: rx #px"he(l*)(o*)")
|
(define-pregexp: rx #px"he(l*)(o*)")
|
||||||
(regexp-match: rx "helllooo"))
|
(regexp-match: rx "helllooo"))
|
||||||
(u #f (list string string string)))
|
(U #f (List String String String)))
|
||||||
'("helllooo" "lll" "ooo"))
|
'("helllooo" "lll" "ooo"))
|
||||||
|
|
||||||
(check-equal?
|
(check-equal?
|
||||||
|
|
Loading…
Reference in New Issue
Block a user