redo state parser to avoid dynamic variables
parent
6b6de2b8aa
commit
a07c17598e
|
@ -1,14 +1,30 @@
|
||||||
USING: tools.test html.parser.state ascii kernel ;
|
USING: tools.test html.parser.state ascii kernel accessors ;
|
||||||
IN: html.parser.state.tests
|
IN: html.parser.state.tests
|
||||||
|
|
||||||
: take-rest ( -- string )
|
[ "hello" ]
|
||||||
[ f ] take-until ;
|
[ "hello" [ take-rest ] string-parse ] unit-test
|
||||||
|
|
||||||
: take-char ( -- string )
|
[ "hi" " how are you?" ]
|
||||||
[ get-char = ] curry take-until ;
|
[
|
||||||
|
"hi how are you?"
|
||||||
|
[ [ [ blank? ] take-until ] [ take-rest ] bi ] string-parse
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
[ "foo" ";bar" ]
|
||||||
|
[
|
||||||
|
"foo;bar" [
|
||||||
|
[ CHAR: ; take-until-char ] [ take-rest ] bi
|
||||||
|
] string-parse
|
||||||
|
] unit-test
|
||||||
|
|
||||||
[ "hello" ] [ "hello" [ take-rest ] string-parse ] unit-test
|
|
||||||
[ "hi" " how are you?" ] [ "hi how are you?" [ [ get-char blank? ] take-until take-rest ] string-parse ] unit-test
|
|
||||||
[ "foo" ";bar" ] [ "foo;bar" [ CHAR: ; take-char take-rest ] string-parse ] unit-test
|
|
||||||
[ "foo " " bar" ]
|
[ "foo " " bar" ]
|
||||||
[ "foo and bar" [ "and" take-string take-rest ] string-parse ] unit-test
|
[
|
||||||
|
"foo and bar" [
|
||||||
|
[ "and" take-until-string ] [ take-rest ] bi
|
||||||
|
] string-parse
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
[ 6 ]
|
||||||
|
[
|
||||||
|
" foo " [ skip-whitespace i>> ] string-parse
|
||||||
|
] unit-test
|
||||||
|
|
|
@ -1,41 +1,62 @@
|
||||||
! Copyright (C) 2005, 2009 Daniel Ehrenberg
|
! Copyright (C) 2005, 2009 Daniel Ehrenberg
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: namespaces math kernel sequences accessors fry circular ;
|
USING: namespaces math kernel sequences accessors fry circular
|
||||||
|
unicode.case unicode.categories locals ;
|
||||||
IN: html.parser.state
|
IN: html.parser.state
|
||||||
|
|
||||||
TUPLE: state string i ;
|
TUPLE: state-parser string i ;
|
||||||
|
|
||||||
: get-i ( -- i ) state get i>> ; inline
|
: <state-parser> ( string -- state-parser )
|
||||||
|
state-parser new
|
||||||
|
swap >>string
|
||||||
|
0 >>i ;
|
||||||
|
|
||||||
: get-char ( -- char )
|
: (get-char) ( i state -- char/f )
|
||||||
state get [ i>> ] [ string>> ] bi ?nth ; inline
|
string>> ?nth ; inline
|
||||||
|
|
||||||
: get-next ( -- char )
|
: get-char ( state -- char/f )
|
||||||
state get [ i>> 1+ ] [ string>> ] bi ?nth ; inline
|
[ i>> ] keep (get-char) ; inline
|
||||||
|
|
||||||
: next ( -- )
|
: get-next ( state -- char/f )
|
||||||
state get [ 1+ ] change-i drop ; inline
|
[ i>> 1+ ] keep (get-char) ; inline
|
||||||
|
|
||||||
|
: next ( state -- state )
|
||||||
|
[ 1+ ] change-i ; inline
|
||||||
|
|
||||||
|
: get+increment ( state -- char/f )
|
||||||
|
[ get-char ] [ next drop ] bi ; inline
|
||||||
|
|
||||||
: string-parse ( string quot -- )
|
: string-parse ( string quot -- )
|
||||||
[ 0 state boa state ] dip with-variable ; inline
|
[ <state-parser> ] dip call ; inline
|
||||||
|
|
||||||
: short* ( n seq -- n' seq )
|
:: skip-until ( state quot: ( obj -- ? ) -- )
|
||||||
over [ nip dup length swap ] unless ; inline
|
state get-char [
|
||||||
|
quot call [ state next quot skip-until ] unless
|
||||||
|
] when* ; inline recursive
|
||||||
|
|
||||||
: skip-until ( quot: ( -- ? ) -- )
|
: take-until ( state quot: ( obj -- ? ) -- string )
|
||||||
get-char [
|
[ drop i>> ]
|
||||||
[ call ] keep swap
|
[ skip-until ]
|
||||||
[ drop ] [ next skip-until ] if
|
[ drop [ i>> ] [ string>> ] bi ] 2tri subseq ; inline
|
||||||
] [ drop ] if ; inline recursive
|
|
||||||
|
|
||||||
: take-until ( quot: ( -- ? ) -- )
|
:: take-until-string ( state-parser string -- string' )
|
||||||
get-i [ skip-until ] dip get-i
|
string length <growing-circular> :> growing
|
||||||
state get string>> subseq ; inline
|
state-parser
|
||||||
|
[
|
||||||
|
growing push-growing-circular
|
||||||
|
string growing sequence=
|
||||||
|
] take-until :> found
|
||||||
|
found dup length
|
||||||
|
growing length 1- - head
|
||||||
|
state-parser next drop ;
|
||||||
|
|
||||||
: string-matches? ( string circular -- ? )
|
: skip-whitespace ( state -- state )
|
||||||
get-char over push-growing-circular sequence= ; inline
|
[ [ blank? not ] take-until drop ] keep ;
|
||||||
|
|
||||||
: take-string ( match -- string )
|
: take-rest ( state -- string )
|
||||||
dup length <growing-circular>
|
[ drop f ] take-until ; inline
|
||||||
[ 2dup string-matches? ] take-until nip
|
|
||||||
dup length rot length 1- - head next ; inline
|
: take-until-char ( state ch -- string )
|
||||||
|
'[ _ = ] take-until ;
|
||||||
|
|
||||||
|
: string-parse-end? ( state -- ? ) get-next not ;
|
||||||
|
|
Loading…
Reference in New Issue