]> gitweb.factorcode.org Git - factor.git/commitdiff
fix negative lookbehind
authorDoug Coleman <doug.coleman@gmail.com>
Mon, 22 Sep 2008 19:55:17 +0000 (14:55 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Mon, 22 Sep 2008 19:55:17 +0000 (14:55 -0500)
basis/regexp/dfa/dfa.factor
basis/regexp/regexp-tests.factor
basis/regexp/traversal/traversal.factor

index cd6dab6a064172819591b2bc18a4cbb48fbfb438..ef985258fd2cc66706d4a39402ebedc9a1c09c3f 100644 (file)
@@ -25,7 +25,7 @@ IN: regexp.dfa
 
 : find-transitions ( seq1 regexp -- seq2 )
     nfa-table>> transitions>>
-    [ at keys ] curry map concat
+    [ at keys ] curry gather
     eps swap remove ;
 
 : add-todo-state ( state regexp -- )
index 4d25a58c39317030071fd75a5426fbf57e3bbbbe..be19c065f5235f9d35c29d760c20685c6620f64a 100644 (file)
@@ -299,3 +299,10 @@ IN: regexp-tests
 ! clear "a$" <regexp> "a\n" over match
 ! clear "a$" <regexp> "a\r" over match
 ! clear "a$" <regexp> "a\r\n" over match
+
+! "(az)(?<=b)" <regexp> "baz" over first-match
+! "a(?<=b*)" <regexp> "cbaz" over first-match
+! "a(?<=b)" <regexp> "baz" over first-match
+
+! "a(?<!b)" <regexp> "baz" over first-match
+! "a(?<!b)" <regexp> "caz" over first-match
index d4b43c37f013ac823d59e2d613f226254734de8c..d82e9941a28fdf2224a378496b1b0232f8fb0fa6 100644 (file)
@@ -38,7 +38,11 @@ TUPLE: dfa-traverser
     key? ;
 
 : text-finished? ( dfa-traverser -- ? )
-    [ current-index>> ] [ text>> length ] bi >= ;
+    {
+        [ current-state>> empty? ]
+        [ [ current-index>> ] [ text>> length ] bi >= ]
+        ! [ current-index>> 0 < ]
+    } 1|| ;
 
 : save-final-state ( dfa-straverser -- )
     [ current-index>> ] [ matches>> ] bi push ;
@@ -62,24 +66,26 @@ M: lookahead-off flag-action ( dfa-traverser flag -- )
 M: lookbehind-on flag-action ( dfa-traverser flag -- )
     drop
     f >>traverse-forward
+    [ 2 - ] change-current-index
     lookbehind-counters>> 0 swap push ;
 
 M: lookbehind-off flag-action ( dfa-traverser flag -- )
     drop
     t >>traverse-forward
     dup lookbehind-counters>>
-    [ drop ] [ pop '[ _ + ] change-current-index drop ] if-empty ;
+    [ drop ] [ pop '[ _ + 2 + ] change-current-index drop ] if-empty ;
 
 : process-flags ( dfa-traverser -- )
     [ [ 1+ ] map ] change-lookahead-counters
+    [ [ 1+ ] map ] change-lookbehind-counters
+    ! dup current-state>> .
     dup [ current-state>> ] [ traversal-flags>> ] bi
     at [ dup . flag-action ] with each ;
 
 : increment-state ( dfa-traverser state -- dfa-traverser )
     [
         dup traverse-forward>>
-        [ [ 1+ ] change-current-index ]
-        [ [ 1- ] change-current-index ] if
+        [ 1+ ] [ 1- ] ? change-current-index
         dup current-state>> >>last-state
     ] dip
     first >>current-state ;