Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bugs in lookbehind matching #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions pregexp-test.scm
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,22 @@
((27 . 31))

(pregexp-match-positions "(?<=grey)hound"
"the hound in the picture is not a greyhound")
((38 . 43))

"\nthe hound in the picture is not a greyhound")
((39 . 44))

(pregexp-match-positions "(?<!grey)hound"
"\nthe greyhound in the picture is not a hound")
((39 . 44))

(pregexp-match-positions "(?<=grey)hound"
"greyhound"
4)
#f

(pregexp-match-positions "(?<!grey)hound"
"the greyhound in the picture is not a hound")
((38 . 43))
"greyhound"
4)
((4 . 9))

)

Expand Down
39 changes: 20 additions & 19 deletions pregexp.scm
Original file line number Diff line number Diff line change
Expand Up @@ -439,18 +439,15 @@
'()))))

(define pregexp-match-positions-aux
(lambda (re s sn start n i)
(lambda (re s start n i)
(let ((identity (lambda (x) x))
(backrefs (pregexp-make-backref-list re))
(case-sensitive? #t))
(let sub ((re re) (i i) (sk identity) (fk (lambda () #f)))
;(printf "sub ~s ~s\n" i re)
(cond ((eqv? re ':bos)
;(if (= i 0) (sk i) (fk))
(if (= i start) (sk i) (fk))
)
((eqv? re ':eos)
;(if (>= i sn) (sk i) (fk))
(if (>= i n) (sk i) (fk))
)
((eqv? re ':empty)
Expand All @@ -464,7 +461,6 @@
(fk)
(sk i)))
((and (char? re) (< i n))
;(printf "bingo\n")
(if ((if case-sensitive? char=? char-ci=?)
(string-ref s i) re)
(sk (+ i 1)) (fk)))
Expand Down Expand Up @@ -539,22 +535,28 @@
identity (lambda () #f))))
(if found-it? (fk) (sk i))))
((:lookbehind)
(let ((n-actual n) (sn-actual sn))
(set! n i) (set! sn i)
(let ((n-actual n) (inner (cadr re)))
(set! n i)
(let ((found-it?
(sub (list ':seq '(:between #f 0 #f :any)
(cadr re) ':eos) 0
identity (lambda () #f))))
(set! n n-actual) (set! sn sn-actual)
(let loup-behind ((i i))
(sub inner i
(lambda (i) (= i n))
(lambda ()
(and (> i start)
(loup-behind (- i 1))))))))
(set! n n-actual)
(if found-it? (sk i) (fk)))))
((:neg-lookbehind)
(let ((n-actual n) (sn-actual sn))
(set! n i) (set! sn i)
(let ((n-actual n) (inner (cadr re)))
(set! n i)
(let ((found-it?
(sub (list ':seq '(:between #f 0 #f :any)
(cadr re) ':eos) 0
identity (lambda () #f))))
(set! n n-actual) (set! sn sn-actual)
(let loup-behind ((i i))
(sub inner i
(lambda (i) (= i n))
(lambda ()
(and (> i start)
(loup-behind (- i 1))))))))
(set! n n-actual)
(if found-it? (fk) (sk i)))))
((:no-backtrack)
(let ((found-it? (sub (cadr re) i
Expand Down Expand Up @@ -614,7 +616,6 @@
(else (pregexp-error 'pregexp-match-positions-aux))))
((>= i n) (fk))
(else (pregexp-error 'pregexp-match-positions-aux))))
;(printf "done\n")
(let ((backrefs (map cdr backrefs)))
(and (car backrefs) backrefs)))))

Expand Down Expand Up @@ -666,7 +667,7 @@
(let loop ((i start))
(and (<= i end)
(or (pregexp-match-positions-aux
pat str str-len start end i)
pat str start end i)
(loop (+ i 1))))))))

(define pregexp-match
Expand Down