1 ! Copyright (C) 2005, 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators effects
4 effects.parser kernel lexer make math memoize multiline
5 namespaces parser present sequences sequences.deep
6 sequences.generalizations strings summary unicode
7 words xml xml.data xml.state ;
12 ERROR: no-tag name word ;
15 drop "The tag-dispatching word has no method for the given tag name" ;
17 : compile-tags ( word xtable -- quot )
18 >alist swap '[ _ no-tag ] suffix '[ dup main>> _ case ] ;
20 : define-tags ( word effect -- )
21 [ dup dup "xtable" word-prop compile-tags ] dip define-declared ;
23 :: define-tag ( string word quot -- )
24 quot string word "xtable" word-prop set-at
25 word word stack-effect define-tags ;
30 scan-new-word scan-effect
31 [ drop H{ } clone "xtable" set-word-prop ]
36 scan-token scan-word parse-definition define-tag ;
39 scan-new-word scan-token '[ f swap _ <name> ] ( string -- name ) define-memoized ;
43 : each-attrs ( attrs quot -- )
44 [ values [ interpolated? ] filter ] dip each ; inline
46 : (each-interpolated) ( ... item quot: ( ... interpolated -- ... ) -- ... )
48 { [ over interpolated? ] [ call ] }
49 { [ over tag? ] [ [ attrs>> ] dip each-attrs ] }
50 { [ over attrs? ] [ each-attrs ] }
51 { [ over xml? ] [ [ body>> ] dip (each-interpolated) ] }
53 } cond ; inline recursive
55 : each-interpolated ( xml quot -- )
56 '[ _ (each-interpolated) ] deep-each ; inline
58 : has-interpolated? ( xml -- ? )
59 ! If this becomes a performance problem, it can be improved
60 f swap [ 2drop t ] each-interpolated ;
62 : when-interpolated ( xml quot -- genquot )
63 [ dup has-interpolated? ] dip [ '[ _ swap ] ] if ; inline
65 : string>chunk ( string -- chunk )
66 t interpolating? [ string>xml-chunk ] with-variable ;
68 : string>doc ( string -- xml )
69 t interpolating? [ string>xml ] with-variable ;
71 DEFER: interpolate-sequence
73 : get-interpolated ( interpolated -- quot )
74 var>> '[ [ _ of ] keep ] ;
76 : ?present ( object -- string )
77 dup [ present ] when ;
79 : interpolate-attr ( key value -- quot )
81 [ get-interpolated '[ _ swap @ [ ?present 2array ] dip ] ]
82 [ 2array '[ _ swap ] ] if ;
84 : interpolate-attrs ( attrs -- quot )
86 [ [ interpolate-attr ] { } assoc>map [ ] join ]
88 '[ @ _ swap [ narray sift-values <attrs> ] dip ]
91 : interpolate-tag ( tag -- quot )
94 [ attrs>> interpolate-attrs ]
95 [ children>> interpolate-sequence ] tri
96 '[ _ swap @ @ [ <tag> ] dip ]
99 GENERIC: push-item ( item -- )
100 M: string push-item , ;
101 M: xml-data push-item , ;
102 M: object push-item present , ;
103 M: sequence push-item
104 dup xml-data? [ , ] [ [ push-item ] each ] if ;
106 [ before>> push-item ]
108 [ after>> push-item ] tri ;
109 M: number push-item present , ;
110 M: xml-chunk push-item % ;
112 : concat-interpolate ( array -- newarray )
113 [ [ push-item ] each ] { } make ;
115 GENERIC: interpolate-item ( item -- quot )
116 M: object interpolate-item [ swap ] curry ;
117 M: tag interpolate-item interpolate-tag ;
118 M: interpolated interpolate-item get-interpolated ;
120 : interpolate-sequence ( seq -- quot )
122 [ [ interpolate-item ] map concat ]
124 '[ @ _ swap [ narray concat-interpolate ] dip ]
125 ] when-interpolated ;
127 GENERIC: [interpolate-xml] ( xml -- quot )
129 M: xml [interpolate-xml]
130 dup body>> interpolate-tag
131 '[ _ (clone) swap @ drop >>body ] ;
133 M: xml-chunk [interpolate-xml]
135 '[ @ drop <xml-chunk> ] ;
137 MACRO: interpolate-xml ( xml -- quot )
140 : number<-> ( doc -- dup )
143 over >>var [ 1 + ] dip
145 ] each-interpolated drop ;
147 : >search-hash ( seq -- hash )
148 [ dup parse-word ] H{ } map>assoc ;
150 : extract-variables ( xml -- seq )
151 [ [ var>> , ] each-interpolated ] { } make ;
153 : nenum ( ... n -- assoc )
154 narray <enumerated> ; inline
156 : collect ( accum variables -- accum ? )
158 { [ dup empty? ] [ drop f ] } ! Just a literal
159 { [ dup [ ] all? ] [ >search-hash suffix! t ] } ! locals
160 { [ dup [ not ] all? ] [ length suffix! \ nenum suffix! t ] } ! fry
161 [ drop "XML interpolation contains both fry and locals" throw ] ! mixed
164 : parse-def ( accum delimiter quot -- accum )
165 [ parse-multiline-string [ blank? ] trim ] dip call
166 [ extract-variables collect ] keep swap
167 [ number<-> suffix! ] dip
168 [ \ interpolate-xml suffix! ] when ; inline
173 "XML>" [ string>doc ] parse-def ;
176 "XML]" [ string>chunk ] parse-def ;
180 USE-WHEN-LOADED: xml.syntax.inverse { "xml.syntax" "inverse" }