updating xml code

cvs
Daniel Ehrenberg 2005-09-27 20:24:05 +00:00
parent 95ca37cb43
commit 7cd809b32e
1 changed files with 44 additions and 24 deletions

View File

@ -20,7 +20,7 @@ SYMBOL: column
code get length spot get = not ; code get length spot get = not ;
: char ( -- char/f ) : char ( -- char/f )
more? [ spot get code get nth ] [ f ] ifte ; more? [ spot get code get nth ] [ f ] if ;
: incr-spot ( -- ) : incr-spot ( -- )
#! Increment spot. #! Increment spot.
@ -30,14 +30,14 @@ SYMBOL: column
line line
] [ ] [
column column
] ifte [ 1 + ] change ; ] if [ 1 + ] change ;
: skip-until ( quot -- | quot: char -- ? ) : skip-until ( quot -- | quot: char -- ? )
more? [ more? [
char swap [ call ] keep swap [ drop ] [ char swap [ call ] keep swap [ drop ] [
incr-spot skip-until incr-spot skip-until
] ifte ] if
] [ drop ] ifte ; inline ] [ drop ] if ; inline
: take-until ( quot -- string | quot: char -- ? ) : take-until ( quot -- string | quot: char -- ? )
#! Take the substring of a string starting at spot #! Take the substring of a string starting at spot
@ -60,8 +60,8 @@ DEFER: <xml-string-error>
drop spot get drop spot get
] [ ] [
incr-spot (take-until-string) incr-spot (take-until-string)
] ifte ] if
] [ "Missing closing token" <xml-string-error> throw ] ifte ; ] [ "Missing closing token" <xml-string-error> throw ] if ;
: take-until-string ( string -- string ) : take-until-string ( string -- string )
[ >r spot get r> (take-until-string) code get subseq ] keep [ >r spot get r> (take-until-string) code get subseq ] keep
@ -116,12 +116,12 @@ M: xml-string-error error.
: expect ( ch -- ) : expect ( ch -- )
char 2dup = [ 2drop ] [ char 2dup = [ 2drop ] [
>r ch>string r> ch>string <expected> throw >r ch>string r> ch>string <expected> throw
] ifte incr-spot ; ] if incr-spot ;
: expect-string ( string -- ) : expect-string ( string -- )
>r spot get r> t over [ char incr-spot = and ] each [ 2drop ] [ >r spot get r> t over [ char incr-spot = and ] each [ 2drop ] [
swap spot get code get subseq <expected> throw swap spot get code get subseq <expected> throw
] ifte ; ] if ;
: entities : entities
{{ {{
@ -137,8 +137,8 @@ M: xml-string-error error.
dup first CHAR: # = [ dup first CHAR: # = [
1 swap tail "x" ?head 16 10 ? base> 1 swap tail "x" ?head 16 10 ? base>
] [ ] [
dup entities hash [ nip ] [ <no-entity> throw ] ifte* dup entities hash [ nip ] [ <no-entity> throw ] if*
] ifte ; ] if ;
: (parse-text) ( vector -- vector ) : (parse-text) ( vector -- vector )
[ CHAR: & = ] take-until over push [ CHAR: & = ] take-until over push
@ -176,7 +176,7 @@ M: xml-string-error error.
incr-spot ch>string [ name-char? not ] take-until append incr-spot ch>string [ name-char? not ] take-until append
] [ ] [
"Malformed name" <xml-string-error> throw "Malformed name" <xml-string-error> throw
] ifte ; ] if ;
: parse-quot ( ch -- str ) : parse-quot ( ch -- str )
incr-spot [ dupd = ] take-until parse-text nip incr-spot ; incr-spot [ dupd = ] take-until parse-text nip incr-spot ;
@ -186,7 +186,7 @@ M: xml-string-error error.
parse-quot parse-quot
] [ ] [
"Attribute lacks quote" <xml-string-error> throw "Attribute lacks quote" <xml-string-error> throw
] ifte ; ] if ;
: parse-prop ( -- [[ name value ]] ) : parse-prop ( -- [[ name value ]] )
parse-name pass-blank CHAR: = expect pass-blank parse-name pass-blank CHAR: = expect pass-blank
@ -213,7 +213,7 @@ TUPLE: comment text ;
<contained> incr-spot <contained> incr-spot
] [ ] [
<opener> <opener>
] ifte ; ] if ;
: skip-comment ( -- comment ) : skip-comment ( -- comment )
"--" expect-string "--" take-until-string <comment> CHAR: > expect ; "--" expect-string "--" take-until-string <comment> CHAR: > expect ;
@ -222,7 +222,7 @@ TUPLE: comment text ;
"[CDATA[" expect-string "]]>" take-until-string ; "[CDATA[" expect-string "]]>" take-until-string ;
: cdata/comment ( -- object ) : cdata/comment ( -- object )
incr-spot char CHAR: - = [ skip-comment ] [ cdata ] ifte ; incr-spot char CHAR: - = [ skip-comment ] [ cdata ] if ;
: make-tag ( -- tag/f ) : make-tag ( -- tag/f )
CHAR: < expect CHAR: < expect
@ -233,8 +233,8 @@ TUPLE: comment text ;
<closer> <closer>
] [ ] [
middle-tag end-tag middle-tag end-tag
] ifte pass-blank CHAR: > expect ] if pass-blank CHAR: > expect
] ifte ; ] if ;
! -- Overall ! -- Overall
@ -252,7 +252,7 @@ TUPLE: comment text ;
get-text swap [ dip-ns ] keep get-text swap [ dip-ns ] keep
more? [ more? [
make-tag [ swap [ dip-ns ] keep ] when* (xml-each) make-tag [ swap [ dip-ns ] keep ] when* (xml-each)
] [ drop ] ifte ; inline ] [ drop ] if ; inline
: xml-each ( string quot -- | quot: node -- ) : xml-each ( string quot -- | quot: node -- )
#! Quotation is called with each node: an opener, closer, contained, #! Quotation is called with each node: an opener, closer, contained,
@ -271,6 +271,21 @@ TUPLE: tag name props children ;
SYMBOL: xml-stack SYMBOL: xml-stack
TUPLE: mismatched open close ;
M: mismatched error.
"Mismatched tags" print
"Opening tag: <" write dup mismatched-open write ">" print
"Closing tag: </" write mismatched-close write ">" print ;
TUPLE: unclosed tags ;
C: unclosed ( -- unclosed )
1 xml-stack get tail-slice [ car opener-name ] map
swap [ set-unclosed-tags ] keep ;
M: unclosed error.
"Unclosed tags" print
"Tags: " print
unclosed-tags [ " <" write write ">" print ] each ;
: seq-last ( seq -- last ) : seq-last ( seq -- last )
[ length 1 - ] keep nth ; [ length 1 - ] keep nth ;
@ -292,16 +307,21 @@ M: opener process
M: closer process M: closer process
closer-name xml-stack get pop uncons closer-name xml-stack get pop uncons
>r [ >r [
opener-name [ = [ "Mismatched tags" throw ] unless ] keep opener-name [
2dup = [ 2drop ] [ swap <mismatched> throw ] if
] keep
] keep opener-props r> <tag> push-datum ; ] keep opener-props r> <tag> push-datum ;
: initialize-xml-stack ( -- )
f 10 <vector> cons unit >vector xml-stack set ;
: xml ( string -- vector ) : xml ( string -- vector )
#! Produces a tree of XML nodes #! Produces a tree of XML nodes
[ [
f 10 <vector> cons 1vector xml-stack set initialize-xml-stack
[ process ] xml-each [ process ] xml-each
xml-stack get xml-stack get
dup length 1 = [ "Unclosed tags!" throw ] unless dup length 1 = [ <unclosed> throw ] unless
first cdr first cdr
] with-scope ; ] with-scope ;
@ -327,7 +347,7 @@ M: string (xml>string)
CHAR: & , % CHAR: ; , CHAR: & , % CHAR: ; ,
] [ ] [
, ,
] ?ifte ] ?if
] each ; ] each ;
: print-open/close ( tag -- ) : print-open/close ( tag -- )
@ -345,7 +365,7 @@ M: tag (xml>string)
drop "/>" % drop "/>" %
] [ ] [
print-open/close print-open/close
] ifte ; ] if ;
M: comment (xml>string) M: comment (xml>string)
"<!--" % "<!--" %
@ -358,7 +378,7 @@ M: comment (xml>string)
: xml-reprint ( string -- string ) : xml-reprint ( string -- string )
xml xml>string ; xml xml>string ;
! * Easy XML generation ! * Easy XML generation for more literal things
: text ( string -- ) : text ( string -- )
chars>entities push-datum ; chars>entities push-datum ;
@ -374,7 +394,7 @@ M: comment (xml>string)
: make-xml ( quot -- vector ) : make-xml ( quot -- vector )
#! Produces a tree of XML from a quotation to generate it #! Produces a tree of XML from a quotation to generate it
[ [
f 10 <vector> cons 1vector xml-stack set initialize-xml-stack
call call
xml-stack get xml-stack get
first cdr first cdr