]> gitweb.factorcode.org Git - factor.git/commitdiff
add take-sequence to state parser
authorDoug Coleman <erg@jobim.local>
Wed, 1 Apr 2009 17:44:06 +0000 (12:44 -0500)
committerDoug Coleman <erg@jobim.local>
Wed, 1 Apr 2009 17:44:06 +0000 (12:44 -0500)
extra/html/parser/state/state-tests.factor
extra/html/parser/state/state.factor

index 835b54d0d38e3b643b9864ba0450284f9e3cfb8a..6766cfddc246c1c49801f505ff4080fc863fe7fe 100644 (file)
@@ -34,3 +34,21 @@ IN: html.parser.state.tests
 
 [ { 1 2 } ]
 [ { 1 2 3 4 } <state-parser> { 3 4 } take-until-sequence ] unit-test
+
+[ "ab" ]
+[ "abcd" <state-parser> "ab" take-sequence ] unit-test
+
+[ f ]
+[ "abcd" <state-parser> "lol" take-sequence ] unit-test
+
+[ "ab" ]
+[
+    "abcd" <state-parser>
+    [ "lol" take-sequence drop ] [ "ab" take-sequence ] bi
+] unit-test
+
+[ "" ]
+[ "abcd" <state-parser> "" take-sequence ] unit-test
+
+[ "cd" ]
+[ "abcd" <state-parser> [ "ab" take-sequence drop ] [ "cd" take-sequence ] bi ] unit-test
index 3f899446c0f1ba8e6afdf0f179af2952abdf24bf..85b0b0fbb97490796e79ccaa630c116a6c7741f1 100644 (file)
@@ -12,32 +12,32 @@ TUPLE: state-parser sequence n ;
         swap >>sequence
         0 >>n ;
 
-: state-parser-nth ( n state -- char/f )
+: state-parser-nth ( n state-parser -- char/f )
     sequence>> ?nth ; inline
 
-: current ( state -- char/f )
+: current ( state-parser -- char/f )
     [ n>> ] keep state-parser-nth ; inline
 
-: previous ( state -- char/f )
+: previous ( state-parser -- char/f )
     [ n>> 1 - ] keep state-parser-nth ; inline
 
-: peek-next ( state -- char/f )
+: peek-next ( state-parser -- char/f )
     [ n>> 1 + ] keep state-parser-nth ; inline
 
-: next ( state -- state )
+: next ( state-parser -- state-parser )
     [ 1 + ] change-n ; inline
 
-: get+increment ( state -- char/f )
+: get+increment ( state-parser -- char/f )
     [ current ] [ next drop ] bi ; inline
 
-:: skip-until ( state quot: ( obj -- ? ) -- )
-    state current [
-        state quot call [ state next quot skip-until ] unless
+:: skip-until ( state-parser quot: ( obj -- ? ) -- )
+    state-parser current [
+        state-parser quot call [ state-parser next quot skip-until ] unless
     ] when ; inline recursive
 
-: state-parse-end? ( state -- ? ) peek-next not ;
+: state-parse-end? ( state-parser -- ? ) peek-next not ;
 
-: take-until ( state quot: ( obj -- ? ) -- sequence/f )
+: take-until ( state-parser quot: ( obj -- ? ) -- sequence/f )
     over state-parse-end? [
         2drop f
     ] [
@@ -46,9 +46,18 @@ TUPLE: state-parser sequence n ;
         [ drop [ n>> ] [ sequence>> ] bi ] 2tri subseq
     ] if ; inline
 
-: take-while ( state quot: ( obj -- ? ) -- sequence/f )
+: take-while ( state-parser quot: ( obj -- ? ) -- sequence/f )
     [ not ] compose take-until ; inline
 
+:: take-sequence ( state-parser sequence -- obj/f )
+    state-parser [ n>> dup sequence length + ] [ sequence>> ] bi <slice>
+    sequence sequence= [
+        sequence
+        state-parser [ sequence length + ] change-n drop
+    ] [
+        f
+    ] if ;
+
 :: take-until-sequence ( state-parser sequence -- sequence' )
     sequence length <growing-circular> :> growing
     state-parser
@@ -60,13 +69,13 @@ TUPLE: state-parser sequence n ;
     growing length 1- - head
     state-parser next drop ;
     
-: skip-whitespace ( state -- state )
+: skip-whitespace ( state-parser -- state-parser )
     [ [ current blank? not ] take-until drop ] keep ;
 
-: take-rest ( state -- sequence )
+: take-rest ( state-parser -- sequence )
     [ drop f ] take-until ; inline
 
-: take-until-object ( state obj -- sequence )
+: take-until-object ( state-parser obj -- sequence )
     '[ current _ = ] take-until ;
 
 : state-parse ( sequence quot -- )