refactor state parser some more, add a word to parse escaped strings
parent
d52535b63a
commit
1e4eebda3a
|
@ -52,3 +52,17 @@ IN: html.parser.state.tests
|
||||||
|
|
||||||
[ "cd" ]
|
[ "cd" ]
|
||||||
[ "abcd" <state-parser> [ "ab" take-sequence drop ] [ "cd" take-sequence ] bi ] unit-test
|
[ "abcd" <state-parser> [ "ab" take-sequence drop ] [ "cd" take-sequence ] bi ] unit-test
|
||||||
|
|
||||||
|
|
||||||
|
[ f ]
|
||||||
|
[
|
||||||
|
"\"abc\" asdf" <state-parser>
|
||||||
|
[ CHAR: \ CHAR: " take-quoted-string drop ] [ "asdf" take-sequence ] bi
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
[ "asdf" ]
|
||||||
|
[
|
||||||
|
"\"abc\" asdf" <state-parser>
|
||||||
|
[ CHAR: \ CHAR: " take-quoted-string drop ]
|
||||||
|
[ skip-whitespace "asdf" take-sequence ] bi
|
||||||
|
] unit-test
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
! 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 ;
|
unicode.case unicode.categories locals combinators.short-circuit ;
|
||||||
|
|
||||||
IN: html.parser.state
|
IN: html.parser.state
|
||||||
|
|
||||||
|
@ -12,21 +12,22 @@ TUPLE: state-parser sequence n ;
|
||||||
swap >>sequence
|
swap >>sequence
|
||||||
0 >>n ;
|
0 >>n ;
|
||||||
|
|
||||||
: state-parser-nth ( n state-parser -- char/f )
|
: offset ( state-parser offset -- char/f )
|
||||||
sequence>> ?nth ; inline
|
swap
|
||||||
|
[ n>> + ] [ sequence>> ?nth ] bi ; inline
|
||||||
|
|
||||||
: current ( state-parser -- char/f )
|
: current ( state-parser -- char/f ) 0 offset ; inline
|
||||||
[ n>> ] keep state-parser-nth ; inline
|
|
||||||
|
|
||||||
: previous ( state-parser -- char/f )
|
: previous ( state-parser -- char/f ) -1 offset ; inline
|
||||||
[ n>> 1 - ] keep state-parser-nth ; inline
|
|
||||||
|
|
||||||
: peek-next ( state-parser -- char/f )
|
: peek-next ( state-parser -- char/f ) 1 offset ; inline
|
||||||
[ n>> 1 + ] keep state-parser-nth ; inline
|
|
||||||
|
|
||||||
: advance ( state-parser -- state-parser )
|
: advance ( state-parser -- state-parser )
|
||||||
[ 1 + ] change-n ; inline
|
[ 1 + ] change-n ; inline
|
||||||
|
|
||||||
|
: advance* ( state-parser -- )
|
||||||
|
advance drop ; inline
|
||||||
|
|
||||||
: get+increment ( state-parser -- char/f )
|
: get+increment ( state-parser -- char/f )
|
||||||
[ current ] [ advance drop ] bi ; inline
|
[ current ] [ advance drop ] bi ; inline
|
||||||
|
|
||||||
|
@ -80,3 +81,13 @@ TUPLE: state-parser sequence n ;
|
||||||
|
|
||||||
: state-parse ( sequence quot -- )
|
: state-parse ( sequence quot -- )
|
||||||
[ <state-parser> ] dip call ; inline
|
[ <state-parser> ] dip call ; inline
|
||||||
|
|
||||||
|
:: take-quoted-string ( state-parser escape-char quote-char -- string )
|
||||||
|
state-parser advance
|
||||||
|
[
|
||||||
|
{
|
||||||
|
[ { [ previous quote-char = ] [ current quote-char = ] } 1&& ]
|
||||||
|
[ current quote-char = not ]
|
||||||
|
} 1||
|
||||||
|
] take-while
|
||||||
|
state-parser advance* ;
|
||||||
|
|
Loading…
Reference in New Issue