 M: reverse-regexp compile-regexp ( regexp -- regexp )
     t backwards? [ do-compile-regexp ] with-variable ;
 
-GENERIC: compile-next-match ( regexp -- regexp )
-
-: next-initial-word ( i string regexp -- i slice/f )
+DEFER: compile-next-match
+
+: next-initial-word ( i string regexp -- i start end string )
     compile-next-match do-next-match ;
 
-M: regexp compile-next-match ( regexp -- regexp )
+: compile-next-match ( regexp -- regexp )
     dup '[
         dup \ next-initial-word = [
             drop _ [ compile-regexp dfa>> ] [ reverse-regexp? ] bi
             '[ _ '[ _ _ execute ] _ next-match ]
-            (( i string regexp -- i match/f )) simple-define-temp
+            (( i string regexp -- i start end string )) simple-define-temp
         ] when
     ] change-next-match ;
-
-! Write M: reverse-regexp compile-next-match
 
 PRIVATE>
 


<PRIVATE

: make-slice ( i j seq -- slice )
    [ 2dup > [ swap [ 1+ ] bi@ ] when ] dip <slice> ; inline

: match-slice ( i string quot -- slice/f )
    [ 2dup ] dip call
    [ swap make-slice ] [ 2drop f ] if* ; inline

: search-range ( i string reverse? -- seq )
    [ drop 0 [a,b] ] [ length [a,b) ] if ; inline

:: next-match ( i string quot reverse? -- i slice/f )
    i string reverse? search-range
    [ string quot match-slice ] map-find drop
    [ dup [ reverse? [ from>> ] [ to>> ] if ] when ] keep ; inline

: do-next-match ( i string regexp -- i match/f )
    dup next-match>> execute( i string regexp -- i match/f ) ; inline

PRIVATE>



: iterate ( iterator -- iterator'/f )
    dup
    [ i>> ] [ string>> ] [ regexp>> ] tri next-slice
    [ [ [ string>> ] [ regexp>> ] bi ] 2dip match-iterator boa ]
    [ 2drop f ] if* ;

