From 4ef1c7a10b7ae4a053995700fdff1a55d2166d8c Mon Sep 17 00:00:00 2001 From: Ben Greenman Date: Thu, 9 Jun 2016 02:24:59 -0400 Subject: [PATCH] [regexp] only check balanced parans in presence of pipes --- test/regexp-pass.rkt | 26 ++++++++++++++++---------- trivial/private/regexp.rkt | 26 ++++++++++++++------------ trivial/untyped/regexp.rkt | 11 ++--------- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/test/regexp-pass.rkt b/test/regexp-pass.rkt index d8c2acb..78a85ac 100644 --- a/test/regexp-pass.rkt +++ b/test/regexp-pass.rkt @@ -8,6 +8,11 @@ trivial/regexp 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: (check-equal? (ann @@ -308,17 +313,18 @@ '("alot")) ;; -- pipes = take min groups - (check-equal? - (ann - (regexp-match: "^(a*)|(b*)$" "aaa") - (U #f (List String (U #f String) (U #f String)))) - '("aaa" "aaa" #f)) + ;; 2016-06-08: currently disabled + ;(check-equal? + ; (ann + ; (regexp-match: "^(a*)|(b*)$" "aaa") + ; (U #f (List String (U #f String) (U #f String)))) + ; '("aaa" "aaa" #f)) - (check-equal? - (ann - (regexp-match: "^(aa*)(c*)|(b*)$" "b") - (U #f (List String (U #f String) (U #f String) (U #f String)))) - '("b" #f #f "b")) + ;(check-equal? + ; (ann + ; (regexp-match: "^(aa*)(c*)|(b*)$" "b") + ; (U #f (List String (U #f String) (U #f String) (U #f String)))) + ; '("b" #f #f "b")) ;; -- nested gropus (check-equal? diff --git a/trivial/private/regexp.rkt b/trivial/private/regexp.rkt index 40e2af3..e6f392e 100644 --- a/trivial/private/regexp.rkt +++ b/trivial/private/regexp.rkt @@ -72,12 +72,14 @@ [(null? (cdr alt*)) (parse-groups-for-alt (car alt*) #:src stx)] [else - (define num-groups - (for/fold ([num-groups 0]) - ([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))])) + ;(define num-groups + ; (for/fold ([num-groups 0]) + ; ([alt (in-list alt*)]) + ; (define ng+null* (parse-groups-for-alt alt #:src stx)) + ; (+ num-groups (car ng+null*)))) + (parse-groups-for-alt str #:src stx) + #f + #;(list num-groups (range num-groups))])) ;; Count the number of matched parentheses in a regexp pattern. ;; Raise an exception if there are unmatched parens. @@ -88,9 +90,9 @@ (if (> i last-index) (cond [(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?) - (group-error str (format "'[' at index ~a" (car in-paren)))] + (group-error stx str (format "'[' at index ~a" (car in-paren)))] [else (list num-groups null-idx*)]) (if (unbox in-square?) @@ -112,7 +114,7 @@ [(#\)) (cond [(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)) ;; Matched closing paren, but does not count as a group (loop (+ i 1) (cdr in-paren) num-groups null-idx*)] @@ -129,9 +131,9 @@ (eq? #\\ (string-ref str (+ i 1)))) (loop (+ i 3) 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.")] + ;[(#\|) + ; ;; Nope! Can't handle pipes + ; (error 'internal-error "Found '|' character in regexp string.")] [else (loop (+ i 1) in-paren num-groups null-idx*)]))))) diff --git a/trivial/untyped/regexp.rkt b/trivial/untyped/regexp.rkt index b851d20..43dfc0b 100644 --- a/trivial/untyped/regexp.rkt +++ b/trivial/untyped/regexp.rkt @@ -72,12 +72,8 @@ [(null? (cdr alt*)) (parse-groups-for-alt (car alt*) #:src stx)] [else - (define num-groups - (for/fold ([num-groups 0]) - ([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))])) + (parse-groups-for-alt str #:src stx) + #f])) ;; Count the number of matched parentheses in a regexp pattern. ;; Raise an exception if there are unmatched parens. @@ -129,9 +125,6 @@ (eq? #\\ (string-ref str (+ i 1)))) (loop (+ i 3) 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 (loop (+ i 1) in-paren num-groups null-idx*)])))))