]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/regexp/compiler/compiler.factor
regexp: don't use execute so the generated code is easier to read
[factor.git] / basis / regexp / compiler / compiler.factor
index 6c7896dccac3a8c87c58d488d5e3103093c1109c..9102836d68798eeb2ba16af6e9c23c143fe4a60f 100644 (file)
@@ -1,10 +1,9 @@
 ! Copyright (C) 2009 Daniel Ehrenberg.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: regexp.classes kernel sequences regexp.negation
-quotations assocs fry math locals combinators
-accessors words compiler.units kernel.private strings
-sequences.private arrays namespaces unicode.breaks
-regexp.transition-tables combinators.short-circuit ;
+USING: accessors assocs combinators combinators.short-circuit
+kernel kernel.private math namespaces quotations regexp.classes
+regexp.transition-tables sequences sequences.private sets
+strings unicode words ;
 IN: regexp.compiler
 
 GENERIC: question>quot ( question -- quot )
@@ -31,17 +30,17 @@ M: end-of-file question>quot
         } 2&&
     ] ;
 
-M: $ question>quot
+M: $crlf question>quot
     drop [ { [ length = ] [ ?nth "\r\n" member? ] } 2|| ] ;
 
-M: ^ question>quot
-    drop [ { [ drop zero? ] [ [ 1- ] dip ?nth "\r\n" member? ] } 2|| ] ;
+M: ^crlf question>quot
+    drop [ { [ drop zero? ] [ [ 1 - ] dip ?nth "\r\n" member? ] } 2|| ] ;
 
 M: $unix question>quot
     drop [ { [ length = ] [ ?nth CHAR: \n = ] } 2|| ] ;
 
 M: ^unix question>quot
-    drop [ { [ drop zero? ] [ [ 1- ] dip ?nth CHAR: \n = ] } 2|| ] ;
+    drop [ { [ drop zero? ] [ [ 1 - ] dip ?nth CHAR: \n = ] } 2|| ] ;
 
 M: word-break question>quot
     drop [ word-break-at? ] ;
@@ -52,7 +51,7 @@ M: word-break question>quot
         [ question>> question>quot ] [ yes>> ] [ no>> ] tri
         [ (execution-quot) ] bi@
         '[ 2dup @ _ _ if ]
-    ] [ '[ _ execute ] ] if ;
+    ] [ 1quotation ] if ;
 
 : execution-quot ( next-state -- quot )
     dup sequence? [ first ] when
@@ -84,32 +83,35 @@ C: <box> box
     { } assoc-like [ first integer? ] partition
     [ [ literals>cases ] keep ] dip non-literals>dispatch ;
 
-:: step ( last-match index str quot final? direction -- last-index/f )
+: advance ( index backwards? -- index+/-1 )
+    -1 1 ? + >fixnum ; inline
+
+: check ( index string backwards? -- in-bounds? )
+    [ drop -1 eq? not ] [ length < ] if ; inline
+
+:: step ( last-match index str quot final? backwards? -- last-index/f )
     final? index last-match ?
-    index str bounds-check? [
-        index direction + str
+    index str backwards? check [
+        index backwards? advance str
         index str nth-unsafe
         quot call
     ] when ; inline
 
-: direction ( -- n )
-    backwards? get -1 1 ? ;
-
 : transitions>quot ( transitions final-state? -- quot )
     dup shortest? get and [ 2drop [ drop nip ] ] [
-        [ split-literals swap case>quot ] dip direction
-        '[ { array-capacity string } declare _ _ _ step ]
+        [ split-literals swap case>quot ] dip backwards? get
+        '[ { fixnum string } declare _ _ _ step ]
     ] if ;
 
 : word>quot ( word dfa -- quot )
     [ transitions>> at ]
-    [ final-states>> key? ] 2bi
+    [ final-states>> in? ] 2bi
     transitions>quot ;
 
 : states>code ( words dfa -- )
     '[
         dup _ word>quot
-        (( last-match index string -- ? ))
+        ( last-match index string -- ? )
         define-declared
     ] each ;
 
@@ -117,16 +119,19 @@ C: <box> box
     dup transitions>> keys [ gensym ] H{ } map>assoc
     [ transitions-at ]
     [ values ]
-    bi swap ; 
+    bi swap ;
 
 : dfa>main-word ( dfa -- word )
     states>words [ states>code ] keep start-state>> ;
 
+: word-template ( quot -- quot' )
+    '[ drop [ f ] 2dip over array-capacity? _ [ 2drop ] if ] ;
+
 PRIVATE>
 
 : dfa>word ( dfa -- quot )
-    dfa>main-word execution-quot '[ drop [ f ] 2dip @ ]
-    (( start-index string regexp -- i/f )) define-temp ;
+    dfa>main-word execution-quot word-template
+    ( start-index string regexp -- i/f ) define-temp ;
 
 : dfa>shortest-word ( dfa -- word )
     t shortest? [ dfa>word ] with-variable ;