2006-01-20 01:26:50 -05:00
|
|
|
USING: kernel math parser namespaces sequences strings
|
|
|
|
prettyprint errors lists hashtables vectors io generic
|
|
|
|
words ;
|
2005-09-09 21:29:28 -04:00
|
|
|
IN: xml
|
|
|
|
|
2005-09-09 23:52:12 -04:00
|
|
|
! * Simple SAX-ish parser
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
! -- Basic utility words
|
|
|
|
|
|
|
|
SYMBOL: code #! Source code
|
|
|
|
SYMBOL: spot #! Current index of string
|
|
|
|
SYMBOL: version
|
|
|
|
SYMBOL: line
|
|
|
|
SYMBOL: column
|
|
|
|
|
|
|
|
: set-code ( string -- ) ! for debugging
|
|
|
|
code set [ spot line column ] [ 0 swap set ] each ;
|
|
|
|
|
|
|
|
: more? ( -- ? )
|
|
|
|
#! Return t if spot is not at the end of code
|
|
|
|
code get length spot get = not ;
|
|
|
|
|
|
|
|
: char ( -- char/f )
|
2005-09-27 16:24:05 -04:00
|
|
|
more? [ spot get code get nth ] [ f ] if ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: incr-spot ( -- )
|
|
|
|
#! Increment spot.
|
|
|
|
spot [ 1 + ] change
|
|
|
|
char "\n\r" member? [
|
|
|
|
0 column set
|
2005-09-09 23:52:12 -04:00
|
|
|
line
|
2005-09-09 21:29:28 -04:00
|
|
|
] [
|
2005-09-09 23:52:12 -04:00
|
|
|
column
|
2005-09-27 16:24:05 -04:00
|
|
|
] if [ 1 + ] change ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: skip-until ( quot -- | quot: char -- ? )
|
|
|
|
more? [
|
|
|
|
char swap [ call ] keep swap [ drop ] [
|
|
|
|
incr-spot skip-until
|
2005-09-27 16:24:05 -04:00
|
|
|
] if
|
|
|
|
] [ drop ] if ; inline
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: take-until ( quot -- string | quot: char -- ? )
|
|
|
|
#! Take the substring of a string starting at spot
|
|
|
|
#! from code until the quotation given is true and
|
|
|
|
#! advance spot to after the substring.
|
|
|
|
spot get >r skip-until r>
|
|
|
|
spot get code get subseq ; inline
|
|
|
|
|
|
|
|
: pass-blank ( -- )
|
|
|
|
#! Advance code past any whitespace, including newlines
|
|
|
|
[ blank? not ] skip-until ;
|
|
|
|
|
|
|
|
: string-matches? ( string -- ? )
|
|
|
|
spot get dup pick length + code get subseq = ;
|
|
|
|
|
2005-09-09 23:52:12 -04:00
|
|
|
DEFER: <xml-string-error>
|
2005-09-09 21:29:28 -04:00
|
|
|
: (take-until-string) ( string -- n )
|
|
|
|
more? [
|
|
|
|
dup string-matches? [
|
|
|
|
drop spot get
|
|
|
|
] [
|
|
|
|
incr-spot (take-until-string)
|
2005-09-27 16:24:05 -04:00
|
|
|
] if
|
|
|
|
] [ "Missing closing token" <xml-string-error> throw ] if ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: take-until-string ( string -- string )
|
|
|
|
[ >r spot get r> (take-until-string) code get subseq ] keep
|
|
|
|
length spot [ + ] change ;
|
|
|
|
|
|
|
|
: in-range-seq? ( number { [[ min max ]] ... } -- ? )
|
|
|
|
[ uncons between? not ] all-with? not ;
|
|
|
|
|
|
|
|
! -- Error reporting
|
|
|
|
|
|
|
|
TUPLE: xml-error line column ;
|
|
|
|
C: xml-error ( -- xml-error )
|
|
|
|
[ line get swap set-xml-error-line ] keep
|
|
|
|
[ column get swap set-xml-error-column ] keep ;
|
|
|
|
|
|
|
|
: xml-error. ( xml-error -- )
|
|
|
|
"XML error" print
|
|
|
|
"Line: " write dup xml-error-line .
|
|
|
|
"Column: " write xml-error-column . ;
|
|
|
|
|
|
|
|
TUPLE: expected should-be was ;
|
|
|
|
C: expected ( should-be was -- error )
|
|
|
|
[ <xml-error> swap set-delegate ] keep
|
|
|
|
[ set-expected-was ] keep
|
|
|
|
[ set-expected-should-be ] keep ;
|
|
|
|
|
|
|
|
M: expected error.
|
2005-09-09 23:52:12 -04:00
|
|
|
dup xml-error.
|
2005-09-09 21:29:28 -04:00
|
|
|
"Token expected: " write dup expected-should-be print
|
|
|
|
"Token present: " write expected-was print ;
|
|
|
|
|
|
|
|
TUPLE: no-entity thing ;
|
|
|
|
C: no-entity ( string -- entitiy )
|
|
|
|
[ <xml-error> swap set-delegate ] keep
|
|
|
|
[ set-no-entity-thing ] keep ;
|
|
|
|
|
|
|
|
M: no-entity error.
|
2005-09-09 23:52:12 -04:00
|
|
|
dup xml-error.
|
2005-09-09 21:29:28 -04:00
|
|
|
"Entity does not exist: &" write no-entity-thing write ";" print ;
|
|
|
|
|
2005-09-09 23:52:12 -04:00
|
|
|
TUPLE: xml-string-error string ;
|
|
|
|
C: xml-string-error ( -- xml-string-error )
|
|
|
|
[ set-xml-string-error-string ] keep
|
2005-09-09 21:29:28 -04:00
|
|
|
[ <xml-error> swap set-delegate ] keep ;
|
|
|
|
|
2005-09-09 23:52:12 -04:00
|
|
|
M: xml-string-error error.
|
|
|
|
dup xml-error.
|
|
|
|
xml-string-error-string print ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
! -- Parsing strings
|
|
|
|
|
|
|
|
: expect ( ch -- )
|
|
|
|
char 2dup = [ 2drop ] [
|
|
|
|
>r ch>string r> ch>string <expected> throw
|
2005-09-27 16:24:05 -04:00
|
|
|
] if incr-spot ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: expect-string ( string -- )
|
|
|
|
>r spot get r> t over [ char incr-spot = and ] each [ 2drop ] [
|
|
|
|
swap spot get code get subseq <expected> throw
|
2005-09-27 16:24:05 -04:00
|
|
|
] if ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: entities
|
2006-01-20 01:26:50 -05:00
|
|
|
#! We have both directions here as a shortcut.
|
2005-10-29 23:25:38 -04:00
|
|
|
H{
|
2006-01-20 01:26:50 -05:00
|
|
|
{ "lt" CHAR: < }
|
|
|
|
{ "gt" CHAR: > }
|
|
|
|
{ "amp" CHAR: & }
|
|
|
|
{ "apos" CHAR: ' }
|
|
|
|
{ "quot" CHAR: " }
|
|
|
|
{ CHAR: < "<" }
|
|
|
|
{ CHAR: > ">" }
|
|
|
|
{ CHAR: & "&" }
|
|
|
|
{ CHAR: ' "'" }
|
|
|
|
{ CHAR: " """ }
|
2005-10-29 23:25:38 -04:00
|
|
|
} ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: parse-entity ( -- ch )
|
|
|
|
incr-spot [ CHAR: ; = ] take-until incr-spot
|
|
|
|
dup first CHAR: # = [
|
|
|
|
1 swap tail "x" ?head 16 10 ? base>
|
|
|
|
] [
|
2005-09-27 16:24:05 -04:00
|
|
|
dup entities hash [ nip ] [ <no-entity> throw ] if*
|
|
|
|
] if ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: (parse-text) ( vector -- vector )
|
|
|
|
[ CHAR: & = ] take-until over push
|
|
|
|
char CHAR: & = [
|
|
|
|
parse-entity ch>string over push (parse-text)
|
|
|
|
] when ;
|
|
|
|
|
|
|
|
: parse-text ( string -- string )
|
|
|
|
[
|
|
|
|
code set 0 spot set
|
|
|
|
100 <vector> (parse-text) concat
|
|
|
|
] with-scope ;
|
|
|
|
|
|
|
|
: get-text ( -- string )
|
|
|
|
[ CHAR: < = ] take-until parse-text ;
|
|
|
|
|
|
|
|
! -- Parsing tags
|
|
|
|
|
|
|
|
: name-start-char? ( ch -- ? )
|
|
|
|
dup ":_" member? swap {
|
|
|
|
[[ CHAR: A CHAR: Z ]] [[ CHAR: a CHAR: z ]] [[ HEX: C0 HEX: D6 ]]
|
|
|
|
[[ HEX: D8 HEX: F6 ]] [[ HEX: F8 HEX: 2FF ]] [[ HEX: 370 HEX: 37D ]]
|
|
|
|
[[ HEX: 37F HEX: 1FFF ]] [[ HEX: 200C HEX: 200D ]] [[ HEX: 2070 HEX: 218F ]]
|
|
|
|
[[ HEX: 2C00 HEX: 2FEF ]] [[ HEX: 3001 HEX: D7FF ]] [[ HEX: F900 HEX: FDCF ]]
|
|
|
|
[[ HEX: FDF0 HEX: FFFD ]] [[ HEX: 10000 HEX: EFFFF ]]
|
|
|
|
} in-range-seq? or ;
|
|
|
|
|
|
|
|
: name-char? ( ch -- ? )
|
|
|
|
dup name-start-char? over "-." member? or over HEX: B7 = or swap
|
|
|
|
{ [[ CHAR: 0 CHAR: 9 ]] [[ HEX: 300 HEX: 36F ]] [[ HEX: 203F HEX: 2040 ]] }
|
|
|
|
in-range-seq? or ;
|
|
|
|
|
|
|
|
: parse-name ( -- name )
|
|
|
|
char dup name-start-char? [
|
|
|
|
incr-spot ch>string [ name-char? not ] take-until append
|
|
|
|
] [
|
2005-09-09 23:52:12 -04:00
|
|
|
"Malformed name" <xml-string-error> throw
|
2005-09-27 16:24:05 -04:00
|
|
|
] if ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: parse-quot ( ch -- str )
|
|
|
|
incr-spot [ dupd = ] take-until parse-text nip incr-spot ;
|
|
|
|
|
|
|
|
: parse-prop-value ( -- str )
|
|
|
|
char dup "'\"" member? [
|
|
|
|
parse-quot
|
|
|
|
] [
|
2005-09-09 23:52:12 -04:00
|
|
|
"Attribute lacks quote" <xml-string-error> throw
|
2005-09-27 16:24:05 -04:00
|
|
|
] if ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: parse-prop ( -- [[ name value ]] )
|
|
|
|
parse-name pass-blank CHAR: = expect pass-blank
|
|
|
|
parse-prop-value cons pass-blank ;
|
|
|
|
|
|
|
|
TUPLE: opener name props ;
|
|
|
|
TUPLE: closer name ;
|
|
|
|
TUPLE: contained name props ;
|
|
|
|
TUPLE: comment text ;
|
|
|
|
|
|
|
|
: start-tag ( -- string ? )
|
|
|
|
#! Outputs the name and whether this is a closing tag
|
|
|
|
char CHAR: / = dup [ incr-spot ] when
|
|
|
|
parse-name swap ;
|
|
|
|
|
|
|
|
: (middle-tag) ( list -- list )
|
|
|
|
pass-blank char name-char? [ parse-prop swons (middle-tag) ] when ;
|
|
|
|
|
|
|
|
: middle-tag ( -- hash )
|
|
|
|
f (middle-tag) alist>hash ;
|
|
|
|
|
|
|
|
: end-tag ( string hash -- tag )
|
|
|
|
pass-blank char CHAR: / = [
|
|
|
|
<contained> incr-spot
|
|
|
|
] [
|
|
|
|
<opener>
|
2005-09-27 16:24:05 -04:00
|
|
|
] if ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: skip-comment ( -- comment )
|
|
|
|
"--" expect-string "--" take-until-string <comment> CHAR: > expect ;
|
|
|
|
|
|
|
|
: cdata ( -- string )
|
|
|
|
"[CDATA[" expect-string "]]>" take-until-string ;
|
|
|
|
|
|
|
|
: cdata/comment ( -- object )
|
2005-09-27 16:24:05 -04:00
|
|
|
incr-spot char CHAR: - = [ skip-comment ] [ cdata ] if ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: make-tag ( -- tag/f )
|
|
|
|
CHAR: < expect
|
|
|
|
char CHAR: ! = [
|
|
|
|
cdata/comment
|
|
|
|
] [
|
|
|
|
start-tag [
|
|
|
|
<closer>
|
|
|
|
] [
|
|
|
|
middle-tag end-tag
|
2005-09-27 16:24:05 -04:00
|
|
|
] if pass-blank CHAR: > expect
|
|
|
|
] if ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
! -- Overall
|
|
|
|
|
|
|
|
: get-version ( -- )
|
|
|
|
"<?" string-matches? [
|
|
|
|
"<?xml" expect-string
|
|
|
|
pass-blank middle-tag "?>" expect-string
|
|
|
|
"version" swap hash [ version set ] when*
|
|
|
|
] when ;
|
|
|
|
|
|
|
|
: dip-ns ( quot -- )
|
|
|
|
n> slip >n ; inline
|
|
|
|
|
|
|
|
: (xml-each) ( quot -- )
|
|
|
|
get-text swap [ dip-ns ] keep
|
|
|
|
more? [
|
|
|
|
make-tag [ swap [ dip-ns ] keep ] when* (xml-each)
|
2005-09-27 16:24:05 -04:00
|
|
|
] [ drop ] if ; inline
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: xml-each ( string quot -- | quot: node -- )
|
|
|
|
#! Quotation is called with each node: an opener, closer, contained,
|
|
|
|
#! comment, or string
|
|
|
|
#! Somewhat like SAX but vastly simplified.
|
|
|
|
[
|
|
|
|
swap code set
|
|
|
|
[ spot line column ] [ 0 swap set ] each
|
|
|
|
"1.0" version set
|
|
|
|
get-version (xml-each)
|
|
|
|
] with-scope ; inline
|
|
|
|
|
|
|
|
! * Data tree
|
|
|
|
|
|
|
|
TUPLE: tag name props children ;
|
|
|
|
|
|
|
|
SYMBOL: xml-stack
|
|
|
|
|
2005-09-27 16:24:05 -04:00
|
|
|
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 ;
|
|
|
|
|
2005-09-09 21:29:28 -04:00
|
|
|
: push-datum ( object -- )
|
2006-01-21 02:37:39 -05:00
|
|
|
xml-stack get peek cdr push ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
GENERIC: process ( object -- )
|
|
|
|
|
|
|
|
M: string process push-datum ;
|
|
|
|
M: comment process push-datum ;
|
|
|
|
|
|
|
|
M: contained process
|
|
|
|
[ contained-name ] keep contained-props 0 <vector> <tag> push-datum ;
|
|
|
|
|
|
|
|
M: opener process
|
2005-10-29 23:25:38 -04:00
|
|
|
V{ } clone cons
|
2005-09-09 21:29:28 -04:00
|
|
|
xml-stack get push ;
|
|
|
|
|
|
|
|
M: closer process
|
|
|
|
closer-name xml-stack get pop uncons
|
|
|
|
>r [
|
2005-09-27 16:24:05 -04:00
|
|
|
opener-name [
|
2006-01-21 02:37:39 -05:00
|
|
|
2dup = [ 2drop ] [ swap <mismatched> throw ] if
|
|
|
|
] keep
|
2005-09-09 21:29:28 -04:00
|
|
|
] keep opener-props r> <tag> push-datum ;
|
|
|
|
|
2005-09-27 16:24:05 -04:00
|
|
|
: initialize-xml-stack ( -- )
|
2005-10-29 23:25:38 -04:00
|
|
|
f V{ } clone cons unit >vector xml-stack set ;
|
2005-09-27 16:24:05 -04:00
|
|
|
|
2005-12-04 18:39:27 -05:00
|
|
|
: xml ( string -- tag )
|
2005-09-09 21:29:28 -04:00
|
|
|
#! Produces a tree of XML nodes
|
|
|
|
[
|
2006-01-21 02:37:39 -05:00
|
|
|
initialize-xml-stack
|
2005-09-09 21:29:28 -04:00
|
|
|
[ process ] xml-each
|
|
|
|
xml-stack get
|
2005-09-27 16:24:05 -04:00
|
|
|
dup length 1 = [ <unclosed> throw ] unless
|
2005-10-27 17:13:14 -04:00
|
|
|
first cdr second
|
2005-09-09 21:29:28 -04:00
|
|
|
] with-scope ;
|
|
|
|
|
|
|
|
! * Printer
|
|
|
|
|
|
|
|
: print-props ( hash -- )
|
|
|
|
[
|
2005-11-29 23:49:59 -05:00
|
|
|
" " % swap % "=\"" % % "\"" %
|
2005-09-09 21:29:28 -04:00
|
|
|
] hash-each ;
|
|
|
|
|
|
|
|
GENERIC: (xml>string) ( object -- )
|
|
|
|
|
2006-01-20 01:26:50 -05:00
|
|
|
: chars>entities ( str -- str )
|
|
|
|
#! Convert <, >, &, ' and " to HTML entities.
|
2005-09-09 21:29:28 -04:00
|
|
|
[
|
2006-01-20 01:26:50 -05:00
|
|
|
[ dup entities hash [ % ] [ , ] ?if ] each
|
|
|
|
] "" make ;
|
|
|
|
|
|
|
|
M: string (xml>string) chars>entities % ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: print-open/close ( tag -- )
|
|
|
|
CHAR: > ,
|
|
|
|
dup tag-children [ (xml>string) ] each
|
|
|
|
"</" %
|
|
|
|
tag-name %
|
|
|
|
CHAR: > , ;
|
|
|
|
|
|
|
|
M: tag (xml>string)
|
|
|
|
CHAR: < ,
|
|
|
|
dup tag-name %
|
|
|
|
dup tag-props print-props
|
|
|
|
dup tag-children [ "" = not ] subset empty? [
|
|
|
|
drop "/>" %
|
|
|
|
] [
|
|
|
|
print-open/close
|
2005-09-27 16:24:05 -04:00
|
|
|
] if ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
M: comment (xml>string)
|
|
|
|
"<!--" %
|
|
|
|
comment-text %
|
|
|
|
"-->" % ;
|
|
|
|
|
|
|
|
: xml>string ( xml -- string )
|
2005-10-27 17:13:14 -04:00
|
|
|
[ (xml>string) ] "" make ;
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: xml-reprint ( string -- string )
|
|
|
|
xml xml>string ;
|
|
|
|
|
2005-09-27 16:24:05 -04:00
|
|
|
! * Easy XML generation for more literal things
|
2005-10-27 17:13:14 -04:00
|
|
|
! should this be rewritten?
|
2005-09-09 21:29:28 -04:00
|
|
|
|
|
|
|
: text ( string -- )
|
|
|
|
chars>entities push-datum ;
|
|
|
|
|
2005-09-27 19:52:30 -04:00
|
|
|
: tag ( string attr-quot contents-quot -- )
|
|
|
|
>r swap >r make-hash r> swap r>
|
2005-09-09 21:29:28 -04:00
|
|
|
-rot dupd <opener> process
|
|
|
|
slip
|
|
|
|
<closer> process ; inline
|
|
|
|
|
|
|
|
: comment ( string -- )
|
|
|
|
<comment> push-datum ;
|
|
|
|
|
|
|
|
: make-xml ( quot -- vector )
|
|
|
|
#! Produces a tree of XML from a quotation to generate it
|
|
|
|
[
|
2005-09-27 16:24:05 -04:00
|
|
|
initialize-xml-stack
|
2005-09-09 21:29:28 -04:00
|
|
|
call
|
|
|
|
xml-stack get
|
2005-10-27 17:13:14 -04:00
|
|
|
first cdr second
|
2005-09-09 21:29:28 -04:00
|
|
|
] with-scope ; inline
|
2005-10-27 17:13:14 -04:00
|
|
|
|
|
|
|
! * System for words specialized on tag names
|
|
|
|
|
2005-12-04 18:39:27 -05:00
|
|
|
TUPLE: process-missing process tag ;
|
|
|
|
M: process-missing error.
|
|
|
|
"Tag <" write
|
|
|
|
process-missing-tag tag-name write
|
2006-01-21 02:37:39 -05:00
|
|
|
"> not implemented on process " write
|
2005-12-04 18:39:27 -05:00
|
|
|
dup process-missing-process word-name print ;
|
|
|
|
|
|
|
|
: run-process ( tag word -- )
|
|
|
|
2dup "xtable" word-prop
|
|
|
|
>r dup tag-name r> hash* [ 2nip call ] [
|
|
|
|
drop <process-missing> throw
|
|
|
|
] if ;
|
|
|
|
|
2005-10-27 17:13:14 -04:00
|
|
|
: PROCESS:
|
|
|
|
CREATE
|
2005-10-29 23:25:38 -04:00
|
|
|
dup H{ } clone "xtable" set-word-prop
|
2005-12-04 18:39:27 -05:00
|
|
|
dup literalize [ run-process ] cons define-compound ; parsing
|
2005-10-27 17:13:14 -04:00
|
|
|
|
|
|
|
: TAG:
|
|
|
|
scan scan-word [
|
|
|
|
swap "xtable" word-prop
|
|
|
|
rot "/" split [ >r 2dup r> swap set-hash ] each 2drop
|
|
|
|
] f ; parsing
|