1 ! Copyright (C) 2005, 2006 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences sequences.private assocs arrays delegate ;
6 TUPLE: name space tag url ;
9 : ?= ( object/f object/f -- ? )
10 2dup and [ = ] [ 2drop t ] if ;
12 : names-match? ( name1 name2 -- ? )
13 [ name-space swap name-space ?= ] 2keep
14 [ name-url swap name-url ?= ] 2keep
15 name-tag swap name-tag ?= and and ;
17 : <name-tag> ( string -- name )
20 : assure-name ( string/name -- name )
21 dup name? [ <name-tag> ] unless ;
23 TUPLE: opener name attrs ;
29 TUPLE: contained name attrs ;
30 C: <contained> contained
35 TUPLE: directive text ;
36 C: <directive> directive
38 TUPLE: instruction text ;
39 C: <instruction> instruction
41 TUPLE: prolog version encoding standalone ;
44 TUPLE: xml prolog before after ;
45 : <xml> ( prolog before main after -- xml )
46 { set-xml-prolog set-xml-before set-delegate set-xml-after }
50 : <attrs> ( alist -- attrs )
51 attrs construct-delegate ;
53 : attr@ ( key alist -- index {key,value} )
55 [ first names-match? ] curry* find ;
58 attr@ nip [ second t ] [ f f ] if* ;
63 >r assure-name swap 2array r> push
66 M: attrs assoc-size length ;
67 M: attrs new-assoc drop V{ } new <attrs> ;
68 M: attrs assoc-find >r delegate r> assoc-find ;
69 M: attrs >alist delegate >alist ;
71 : >attrs ( assoc -- attrs )
73 [ >r assure-name r> ] assoc-map
76 drop dup attrs? [ >attrs ] unless ;
81 tuck attr@ drop [ swap delete-nth ] [ drop ] if* ;
85 TUPLE: tag attrs children ;
86 : <tag> ( name attrs children -- tag )
87 >r >r assure-name r> T{ attrs } assoc-like r>
88 { set-delegate set-tag-attrs set-tag-children }
91 ! For convenience, tags follow the assoc protocol too (for attrs)
92 CONSULT: assoc-protocol tag tag-attrs ;
95 ! They also follow the sequence protocol (for children)
96 CONSULT: sequence-protocol tag tag-children ;
97 INSTANCE: tag sequence
99 ! tag with children=f is contained
100 : <contained-tag> ( name attrs -- tag )
103 PREDICATE: tag contained-tag tag-children not ;
104 PREDICATE: tag open-tag tag-children ;