1 ! Copyright (C) 2005, 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: namespaces xml.state kernel sequences accessors
4 xml.char-classes xml.errors math io sbufs fry strings ascii
5 xml.entities assocs splitting math.parser
6 locals combinators arrays hints ;
9 ! * Basic utility words
11 : assure-good-char ( spot ch -- )
14 [ version-1.0?>> over text? not ]
17 [ [ 1 + ] change-column drop ] dip
22 HINTS: assure-good-char { spot fixnum } ;
24 : record ( spot char -- spot )
27 [ [ 1 + ] change-line -1 ] [ dup column>> 1 + ] if
31 HINTS: record { spot fixnum } ;
33 :: (next) ( spot -- spot char )
34 spot next>> :> old-next
35 spot stream>> stream-read1 :> new-next
36 old-next CHAR: \r eq? [
39 [ spot stream>> stream-read1 >>next ]
40 [ new-next >>next ] if
41 ] [ spot old-next >>char new-next >>next ] if
45 dup char>> [ unexpected-end ] unless
46 (next) [ record ] keep assure-good-char ;
48 HINTS: next* { spot } ;
55 input-stream get >>stream
59 : with-state ( stream quot -- )
60 ! with-input-stream implicitly creates a new scope which we use
61 swap [ init-parser call ] with-input-stream ; inline
63 :: (skip-until) ( ... quot: ( ... char -- ... ? ) spot -- ... )
66 spot next* quot spot (skip-until)
68 ] when* ; inline recursive
70 : skip-until ( ... quot: ( ... char -- ... ? ) -- ... )
71 spot get (skip-until) ; inline
73 : take-until ( ... quot: ( ... char -- ... ? ) -- ... string )
74 #! Take the substring of a string starting at spot
75 #! from code until the quotation given is true and
76 #! advance spot to after the substring.
78 '[ _ keep over [ drop ] [ _ push ] if ] skip-until
79 ] keep >string ; inline
81 : take-to ( seq -- string )
82 '[ _ member? ] take-until ;
85 #! Advance code past any whitespace, including newlines
86 [ blank? not ] skip-until ;
88 : next-matching ( pos ch str -- pos' )
89 [ over ] dip nth eq? [ 1 + ] [ drop 0 ] if ;
91 : string-matcher ( str -- quot: ( pos char -- pos ? ) )
92 dup length 1 - '[ _ next-matching dup _ > ] ; inline
94 : take-string ( match -- string )
95 [ 0 swap string-matcher take-until nip ] keep
96 dupd [ length ] bi@ 1 - - head
97 get-char [ missing-close ] unless next ;
99 : expect ( string -- )
100 dup length spot get '[ _ [ char>> ] keep next* ] "" replicate-as
101 2dup = [ 2drop ] [ expected ] if ;
103 ! Suddenly XML-specific
105 : parse-named-entity ( accum string -- )
106 dup entities at [ swap push ] [
107 dup extra-entities get at
108 [ swap push-all ] [ no-entity ] ?if
111 : take-; ( -- string )
112 next ";" take-to next ;
114 : parse-entity ( accum -- )
116 "x" ?head 16 10 ? base> swap push
117 ] [ parse-named-entity ] if ;
119 : parse-pe ( accum -- )
120 take-; dup pe-table get at
121 [ swap push-all ] [ no-entity ] ?if ;
123 :: (parse-char) ( quot: ( ch -- ? ) accum spot -- )
127 { [ char quot call ] [ spot next* ] }
128 { [ char CHAR: & eq? ] [
130 quot accum spot (parse-char)
132 { [ char CHAR: % eq? in-dtd? get and ] [
134 quot accum spot (parse-char)
139 quot accum spot (parse-char)
141 } cond ; inline recursive
143 : parse-char ( quot: ( ch -- ? ) -- seq )
144 1024 <sbuf> [ spot get (parse-char) ] keep >string ; inline
146 : assure-no-]]> ( pos char -- pos' )
147 "]]>" next-matching dup 2 > [ text-w/]]> ] when ;
149 :: parse-text ( -- string )
151 depth get zero? :> no-text
153 pos char assure-no-]]> pos!
155 char blank? char CHAR: < eq? or [
156 char 1string t pre/post-content
163 pass-blank ">" expect ;
165 : normalize-quote ( str -- str )
166 [ dup "\t\r\n" member? [ drop CHAR: \s ] when ] map ;
168 : (parse-quote) ( <-disallowed? ch -- string )
171 [ CHAR: < eq? _ and [ attr-w/< ] [ f ] if ] if
172 ] parse-char normalize-quote get-char
173 [ unclosed-quote ] unless ; inline
175 : parse-quote* ( <-disallowed? -- seq )
176 pass-blank get-char dup "'\"" member?
177 [ next (parse-quote) ] [ quoteless-attr ] if ; inline
179 : parse-quote ( -- seq )