[regexp] only check balanced parans in presence of pipes

This commit is contained in:
Ben Greenman 2016-06-09 02:24:59 -04:00
parent e2d6d365f6
commit 4ef1c7a10b
3 changed files with 32 additions and 31 deletions

View File

@ -8,6 +8,11 @@
trivial/regexp trivial/regexp
typed/rackunit) typed/rackunit)
;; -- regexp
(let ()
;; TODO (what groups does this return? re-read regexp spec)
(check-true (and (regexp: "^(\r|\n|(\r\n))") #t)))
;; -- regexp-match: ;; -- regexp-match:
(check-equal? (check-equal?
(ann (ann
@ -308,17 +313,18 @@
'("alot")) '("alot"))
;; -- pipes = take min groups ;; -- pipes = take min groups
(check-equal? ;; 2016-06-08: currently disabled
(ann ;(check-equal?
(regexp-match: "^(a*)|(b*)$" "aaa") ; (ann
(U #f (List String (U #f String) (U #f String)))) ; (regexp-match: "^(a*)|(b*)$" "aaa")
'("aaa" "aaa" #f)) ; (U #f (List String (U #f String) (U #f String))))
; '("aaa" "aaa" #f))
(check-equal? ;(check-equal?
(ann ; (ann
(regexp-match: "^(aa*)(c*)|(b*)$" "b") ; (regexp-match: "^(aa*)(c*)|(b*)$" "b")
(U #f (List String (U #f String) (U #f String) (U #f String)))) ; (U #f (List String (U #f String) (U #f String) (U #f String))))
'("b" #f #f "b")) ; '("b" #f #f "b"))
;; -- nested gropus ;; -- nested gropus
(check-equal? (check-equal?

View File

@ -72,12 +72,14 @@
[(null? (cdr alt*)) [(null? (cdr alt*))
(parse-groups-for-alt (car alt*) #:src stx)] (parse-groups-for-alt (car alt*) #:src stx)]
[else [else
(define num-groups ;(define num-groups
(for/fold ([num-groups 0]) ; (for/fold ([num-groups 0])
([alt (in-list alt*)]) ; ([alt (in-list alt*)])
(define ng+null* (parse-groups-for-alt alt #:src stx)) ; (define ng+null* (parse-groups-for-alt alt #:src stx))
(+ num-groups (car ng+null*)))) ; (+ num-groups (car ng+null*))))
(list num-groups (range num-groups))])) (parse-groups-for-alt str #:src stx)
#f
#;(list num-groups (range num-groups))]))
;; Count the number of matched parentheses in a regexp pattern. ;; Count the number of matched parentheses in a regexp pattern.
;; Raise an exception if there are unmatched parens. ;; Raise an exception if there are unmatched parens.
@ -88,9 +90,9 @@
(if (> i last-index) (if (> i last-index)
(cond (cond
[(not (null? in-paren)) [(not (null? in-paren))
(group-error str (format "'(' at index ~a" (car in-paren)))] (group-error stx str (format "'(' at index ~a" (car in-paren)))]
[(unbox in-square?) [(unbox in-square?)
(group-error str (format "'[' at index ~a" (car in-paren)))] (group-error stx str (format "'[' at index ~a" (car in-paren)))]
[else [else
(list num-groups null-idx*)]) (list num-groups null-idx*)])
(if (unbox in-square?) (if (unbox in-square?)
@ -112,7 +114,7 @@
[(#\)) [(#\))
(cond (cond
[(null? in-paren) [(null? in-paren)
(group-error str (format "')' at index ~a" i))] (group-error stx str (format "')' at index ~a" i))]
[(eq? #f (car in-paren)) [(eq? #f (car in-paren))
;; Matched closing paren, but does not count as a group ;; Matched closing paren, but does not count as a group
(loop (+ i 1) (cdr in-paren) num-groups null-idx*)] (loop (+ i 1) (cdr in-paren) num-groups null-idx*)]
@ -129,9 +131,9 @@
(eq? #\\ (string-ref str (+ i 1)))) (eq? #\\ (string-ref str (+ i 1))))
(loop (+ i 3) in-paren num-groups null-idx*) (loop (+ i 3) in-paren num-groups null-idx*)
(loop (+ i 2) in-paren num-groups null-idx*))] (loop (+ i 2) in-paren num-groups null-idx*))]
[(#\|) ;[(#\|)
;; Nope! Can't handle pipes ; ;; Nope! Can't handle pipes
(error 'internal-error "Found '|' character in regexp string.")] ; (error 'internal-error "Found '|' character in regexp string.")]
[else [else
(loop (+ i 1) in-paren num-groups null-idx*)]))))) (loop (+ i 1) in-paren num-groups null-idx*)])))))

View File

@ -72,12 +72,8 @@
[(null? (cdr alt*)) [(null? (cdr alt*))
(parse-groups-for-alt (car alt*) #:src stx)] (parse-groups-for-alt (car alt*) #:src stx)]
[else [else
(define num-groups (parse-groups-for-alt str #:src stx)
(for/fold ([num-groups 0]) #f]))
([alt (in-list alt*)])
(define ng+null* (parse-groups-for-alt alt #:src stx))
(+ num-groups (car ng+null*))))
(list num-groups (range num-groups))]))
;; Count the number of matched parentheses in a regexp pattern. ;; Count the number of matched parentheses in a regexp pattern.
;; Raise an exception if there are unmatched parens. ;; Raise an exception if there are unmatched parens.
@ -129,9 +125,6 @@
(eq? #\\ (string-ref str (+ i 1)))) (eq? #\\ (string-ref str (+ i 1))))
(loop (+ i 3) in-paren num-groups null-idx*) (loop (+ i 3) in-paren num-groups null-idx*)
(loop (+ i 2) in-paren num-groups null-idx*))] (loop (+ i 2) in-paren num-groups null-idx*))]
[(#\|)
;; Nope! Can't handle pipes
(error 'internal-error "Found '|' character in regexp string.")]
[else [else
(loop (+ i 1) in-paren num-groups null-idx*)]))))) (loop (+ i 1) in-paren num-groups null-idx*)])))))