factor/basis/xml/data/data.factor

230 lines
4.9 KiB
Factor
Raw Normal View History

2009-01-22 20:04:48 -05:00
! Copyright (C) 2005, 2009 Daniel Ehrenberg
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: kernel sequences sequences.private assocs arrays
2008-08-27 18:02:54 -04:00
delegate.protocols delegate vectors accessors multiline
2009-01-22 20:04:48 -05:00
macros words quotations combinators slots fry strings ;
2007-09-20 18:09:08 -04:00
IN: xml.data
2009-01-25 22:06:45 -05:00
TUPLE: interpolated var ;
C: <interpolated> interpolated
2009-01-22 20:04:48 -05:00
UNION: nullable-string string POSTPONE: f ;
TUPLE: name
{ space nullable-string }
{ main string }
{ url nullable-string } ;
2007-09-20 18:09:08 -04:00
C: <name> name
: ?= ( object/f object/f -- ? )
2dup and [ = ] [ 2drop t ] if ;
: names-match? ( name1 name2 -- ? )
2008-08-27 18:02:54 -04:00
[ [ space>> ] bi@ ?= ]
[ [ url>> ] bi@ ?= ]
[ [ main>> ] bi@ ?= ] 2tri and and ;
2007-09-20 18:09:08 -04:00
2008-08-27 18:02:54 -04:00
: <simple-name> ( string -- name )
2009-01-20 16:37:21 -05:00
"" swap f <name> ;
: <null-name> ( string -- name )
2007-09-20 18:09:08 -04:00
f swap f <name> ;
: assure-name ( string/name -- name )
2009-01-20 16:37:21 -05:00
dup name? [ <null-name> ] unless ;
2007-09-20 18:09:08 -04:00
2009-01-22 20:04:48 -05:00
TUPLE: attrs { alist sequence } ;
2007-12-23 14:57:39 -05:00
C: <attrs> attrs
2007-09-20 18:09:08 -04:00
: attr@ ( key alist -- index {key,value} )
[ assure-name ] dip alist>>
2008-01-09 17:36:30 -05:00
[ first names-match? ] with find ;
2007-09-20 18:09:08 -04:00
M: attrs at*
attr@ nip [ second t ] [ f f ] if* ;
M: attrs set-at
2dup attr@ nip [
2nip set-second
] [
[ assure-name swap 2array ] dip
2008-08-27 18:02:54 -04:00
[ alist>> ?push ] keep (>>alist)
2007-09-20 18:09:08 -04:00
] if* ;
2008-08-27 18:02:54 -04:00
M: attrs assoc-size alist>> length ;
M: attrs new-assoc drop V{ } new-sequence <attrs> ;
2008-08-27 18:02:54 -04:00
M: attrs >alist alist>> ;
2007-09-20 18:09:08 -04:00
: >attrs ( assoc -- attrs )
2007-12-17 15:29:21 -05:00
dup [
V{ } assoc-clone-like
[ [ assure-name ] dip ] assoc-map
2007-12-17 15:29:21 -05:00
] when <attrs> ;
2007-09-20 18:09:08 -04:00
M: attrs assoc-like
drop dup attrs? [ >attrs ] unless ;
M: attrs clear-assoc
2008-08-27 18:02:54 -04:00
f >>alist drop ;
2007-09-20 18:09:08 -04:00
M: attrs delete-at
2009-01-23 19:20:47 -05:00
[ nip ] [ attr@ drop ] 2bi
[ swap alist>> delete-nth ] [ drop ] if* ;
2007-12-23 14:57:39 -05:00
M: attrs clone
2008-08-27 18:02:54 -04:00
alist>> clone <attrs> ;
2007-09-20 18:09:08 -04:00
INSTANCE: attrs assoc
2009-01-22 20:04:48 -05:00
TUPLE: opener { name name } { attrs attrs } ;
C: <opener> opener
TUPLE: closer { name name } ;
C: <closer> closer
TUPLE: contained { name name } { attrs attrs } ;
C: <contained> contained
TUPLE: comment { text string } ;
C: <comment> comment
TUPLE: directive ;
TUPLE: element-decl < directive
{ name string }
{ content-spec string } ;
2009-01-22 20:04:48 -05:00
C: <element-decl> element-decl
TUPLE: attlist-decl < directive
{ name string }
{ att-defs string } ;
2009-01-22 20:04:48 -05:00
C: <attlist-decl> attlist-decl
UNION: boolean t POSTPONE: f ;
TUPLE: entity-decl < directive
{ name string }
{ def string }
{ pe? boolean } ;
C: <entity-decl> entity-decl
TUPLE: system-id { system-literal string } ;
C: <system-id> system-id
TUPLE: public-id { pubid-literal string } { system-literal string } ;
C: <public-id> public-id
UNION: id system-id public-id POSTPONE: f ;
TUPLE: dtd
{ directives sequence }
{ entities assoc }
{ parameter-entities assoc } ;
C: <dtd> dtd
UNION: dtd/f dtd POSTPONE: f ;
2009-01-22 20:04:48 -05:00
TUPLE: doctype-decl < directive
{ name string }
{ external-id id }
{ internal-subset dtd/f } ;
2009-01-22 20:04:48 -05:00
C: <doctype-decl> doctype-decl
TUPLE: notation-decl < directive
{ name string }
{ id string } ;
2009-01-22 20:04:48 -05:00
C: <notation-decl> notation-decl
TUPLE: instruction { text string } ;
C: <instruction> instruction
TUPLE: prolog
{ version string }
{ encoding string }
{ standalone boolean } ;
C: <prolog> prolog
TUPLE: tag
{ name name }
{ attrs attrs }
{ children sequence } ;
2008-08-27 18:02:54 -04:00
2007-09-20 18:09:08 -04:00
: <tag> ( name attrs children -- tag )
2008-08-27 18:02:54 -04:00
[ assure-name ] [ T{ attrs } assoc-like ] [ ] tri*
tag boa ;
2007-09-20 18:09:08 -04:00
2009-01-28 16:46:34 -05:00
: attr ( tag/xml name -- string )
swap attrs>> at ;
2009-01-28 16:46:34 -05:00
: set-attr ( tag/xml value name -- )
rot attrs>> set-at ;
2007-09-20 18:09:08 -04:00
! They also follow the sequence protocol (for children)
2008-08-27 18:02:54 -04:00
CONSULT: sequence-protocol tag children>> ;
2007-09-20 18:09:08 -04:00
INSTANCE: tag sequence
2008-08-27 18:02:54 -04:00
CONSULT: name tag name>> ;
2007-12-23 14:57:39 -05:00
M: tag like
over tag? [ drop ] [
2008-09-02 01:45:40 -04:00
[ name>> ] keep attrs>>
2007-12-23 14:57:39 -05:00
rot dup [ V{ } like ] when <tag>
] if ;
2007-12-23 14:57:39 -05:00
2008-08-27 18:02:54 -04:00
MACRO: clone-slots ( class -- tuple )
[
"slots" word-prop
[ name>> reader-word '[ _ execute clone ] ] map
'[ _ cleave ]
] [ '[ _ boa ] ] bi compose ;
2008-08-27 18:02:54 -04:00
2007-12-23 14:57:39 -05:00
M: tag clone
2008-08-27 18:02:54 -04:00
tag clone-slots ;
2007-12-23 14:57:39 -05:00
2009-01-22 20:04:48 -05:00
TUPLE: xml
{ prolog prolog }
{ before sequence }
{ body tag }
{ after sequence } ;
2008-08-27 18:02:54 -04:00
C: <xml> xml
2007-12-23 14:57:39 -05:00
2008-08-27 18:02:54 -04:00
CONSULT: sequence-protocol xml body>> ;
2007-12-23 14:57:39 -05:00
INSTANCE: xml sequence
2008-08-27 18:02:54 -04:00
CONSULT: tag xml body>> ;
CONSULT: name xml body>> ;
2007-12-23 14:57:39 -05:00
<PRIVATE
: tag>xml ( xml tag -- newxml )
[ [ prolog>> ] [ before>> ] [ after>> ] tri ] dip
2008-08-27 18:02:54 -04:00
swap <xml> ;
2007-12-23 14:57:39 -05:00
: seq>xml ( xml seq -- newxml )
2008-08-27 18:02:54 -04:00
over body>> like tag>xml ;
2007-12-23 14:57:39 -05:00
PRIVATE>
M: xml clone
2008-08-27 18:02:54 -04:00
xml clone-slots ;
2007-12-23 14:57:39 -05:00
M: xml like
2007-12-23 23:21:44 -05:00
swap dup xml? [ nip ] [
2007-12-23 14:57:39 -05:00
dup tag? [ tag>xml ] [ seq>xml ] if
2007-12-23 23:21:44 -05:00
] if ;
2007-12-23 14:57:39 -05:00
2007-09-20 18:09:08 -04:00
! tag with children=f is contained
: <contained-tag> ( name attrs -- tag )
f <tag> ;
2008-08-27 18:02:54 -04:00
PREDICATE: contained-tag < tag children>> not ;
PREDICATE: open-tag < tag children>> ;
TUPLE: unescaped string ;
C: <unescaped> unescaped
UNION: xml-data
tag comment string directive instruction unescaped ;
TUPLE: xml-chunk seq ;
C: <xml-chunk> xml-chunk
CONSULT: sequence-protocol xml-chunk seq>> ;
INSTANCE: xml-chunk sequence