factor/basis/xml/data/data.factor

153 lines
3.3 KiB
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
! Copyright (C) 2005, 2006 Daniel Ehrenberg
! 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
macros words quotations combinators ;
2007-09-20 18:09:08 -04:00
IN: xml.data
2008-08-27 18:02:54 -04:00
TUPLE: name space main url ;
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 )
2007-09-20 18:09:08 -04:00
f swap f <name> ;
: assure-name ( string/name -- name )
2008-08-27 18:02:54 -04:00
dup name? [ <simple-name> ] unless ;
2007-09-20 18:09:08 -04:00
TUPLE: opener name attrs ;
C: <opener> opener
TUPLE: closer name ;
C: <closer> closer
TUPLE: contained name attrs ;
C: <contained> contained
TUPLE: comment text ;
C: <comment> comment
TUPLE: directive text ;
C: <directive> directive
TUPLE: instruction text ;
C: <instruction> instruction
TUPLE: prolog version encoding standalone ;
C: <prolog> prolog
2007-12-23 14:57:39 -05:00
TUPLE: attrs alist ;
C: <attrs> attrs
2007-09-20 18:09:08 -04:00
: attr@ ( key alist -- index {key,value} )
2008-08-27 18:02:54 -04:00
>r assure-name r> 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
] [
2007-12-23 14:57:39 -05:00
>r assure-name swap 2array r>
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
[ >r assure-name r> ] assoc-map
] 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
2008-08-27 18:02:54 -04:00
tuck attr@ drop [ 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
2008-08-27 18:02:54 -04:00
TUPLE: tag name attrs children ;
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
! For convenience, tags follow the assoc protocol too (for attrs)
CONSULT: assoc-protocol tag tag-attrs ;
2007-09-20 18:09:08 -04:00
INSTANCE: tag assoc
! 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-08-27 18:02:54 -04:00
[ name>> ] keep tag-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
[ reader>> 1quotation [ clone ] compose ] map
[ cleave ] curry
] [ [ boa ] curry ] bi compose ;
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
2008-08-27 18:02:54 -04:00
TUPLE: xml prolog before body after ;
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: assoc-protocol xml body>> ;
2007-12-23 14:57:39 -05:00
INSTANCE: xml assoc
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 )
2008-08-27 18:02:54 -04:00
>r [ prolog>> ] [ before>> ] [ after>> ] tri r>
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>> ;