1 ! Copyright (C) 2005, 2006 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: xml.data xml.writer kernel generic io prettyprint math
4 debugger sequences xml.state accessors summary
5 namespaces io.streams.string ;
8 TUPLE: parsing-error line column ;
10 : parsing-error ( class -- obj )
14 M: parsing-error summary ( obj -- str )
17 "Line: " write dup line>> .
18 "Column: " write column>> .
19 ] with-string-writer ;
21 TUPLE: expected < parsing-error should-be was ;
22 : expected ( should-be was -- * )
23 \ expected parsing-error
25 swap >>should-be throw ;
26 M: expected summary ( obj -- str )
28 dup call-next-method write
29 "Token expected: " write dup should-be>> print
30 "Token present: " write was>> print
31 ] with-string-writer ;
33 TUPLE: unexpected-end < parsing-error ;
34 : unexpected-end ( -- * ) \ unexpected-end parsing-error throw ;
35 M: unexpected-end summary ( obj -- str )
37 call-next-method write
38 "File unexpectedly ended." print
39 ] with-string-writer ;
41 TUPLE: missing-close < parsing-error ;
42 : missing-close ( -- * ) \ missing-close parsing-error throw ;
43 M: missing-close summary ( obj -- str )
45 call-next-method write
46 "Missing closing token." print
47 ] with-string-writer ;
49 TUPLE: disallowed-char < parsing-error char ;
51 : disallowed-char ( char -- * )
52 \ disallowed-char parsing-error swap >>char throw ;
54 M: disallowed-char summary
56 [ char>> "Disallowed character in XML document: " swap suffix ] bi
61 M: multitags summary ( obj -- str )
62 drop "XML document contains multiple main tags" ;
64 ERROR: pre/post-content string pre? ;
66 M: pre/post-content summary ( obj -- str )
68 "The text string:" print
71 pre?>> "before" "after" ? write
72 " the main tag." print
73 ] with-string-writer ;
75 TUPLE: no-entity < parsing-error thing ;
77 : no-entity ( string -- * )
78 \ no-entity parsing-error swap >>thing throw ;
80 M: no-entity summary ( obj -- str )
82 dup call-next-method write
83 "Entity does not exist: &" write thing>> write ";" print
84 ] with-string-writer ;
86 TUPLE: mismatched < parsing-error open close ;
88 : mismatched ( open close -- * )
89 \ mismatched parsing-error swap >>close swap >>open throw ;
91 M: mismatched summary ( obj -- str )
93 dup call-next-method write
94 "Mismatched tags" print
95 "Opening tag: <" write dup open>> print-name ">" print
96 "Closing tag: </" write close>> print-name ">" print
97 ] with-string-writer ;
99 TUPLE: unclosed < parsing-error tags ;
102 \ unclosed parsing-error
103 xml-stack get rest-slice [ first name>> ] map >>tags
106 M: unclosed summary ( obj -- str )
108 dup call-next-method write
109 "Unclosed tags" print
111 tags>> [ " <" write print-name ">" print ] each
112 ] with-string-writer ;
114 TUPLE: bad-uri < parsing-error string ;
116 : bad-uri ( string -- * )
117 \ bad-uri parsing-error swap >>string throw ;
119 M: bad-uri summary ( obj -- str )
121 dup call-next-method write
122 "Bad URI:" print string>> .
123 ] with-string-writer ;
125 TUPLE: nonexist-ns < parsing-error name ;
127 : nonexist-ns ( name-string -- * )
128 \ nonexist-ns parsing-error swap >>name throw ;
130 M: nonexist-ns summary ( obj -- str )
132 dup call-next-method write
133 "Namespace " write name>> write " has not been declared" print
134 ] with-string-writer ;
136 TUPLE: unopened < parsing-error ; ! this should give which tag was unopened
139 \ unopened parsing-error throw ;
141 M: unopened summary ( obj -- str )
143 call-next-method write
144 "Closed an unopened tag" print
145 ] with-string-writer ;
147 TUPLE: not-yes/no < parsing-error text ;
149 : not-yes/no ( text -- * )
150 \ not-yes/no parsing-error swap >>text throw ;
152 M: not-yes/no summary ( obj -- str )
154 dup call-next-method write
155 "standalone must be either yes or no, not \"" write
156 text>> write "\"." print
157 ] with-string-writer ;
159 ! this should actually print the names
160 TUPLE: extra-attrs < parsing-error attrs ;
162 : extra-attrs ( attrs -- * )
163 \ extra-attrs parsing-error swap >>attrs throw ;
165 M: extra-attrs summary ( obj -- str )
167 dup call-next-method write
168 "Extra attributes included in xml version declaration:" print
170 ] with-string-writer ;
172 TUPLE: bad-version < parsing-error num ;
174 : bad-version ( num -- * )
175 \ bad-version parsing-error swap >>num throw ;
177 M: bad-version summary ( obj -- str )
179 "XML version must be \"1.0\" or \"1.1\". Version here was " write
181 ] with-string-writer ;
185 M: notags summary ( obj -- str )
186 drop "XML document lacks a main tag" ;
188 TUPLE: bad-prolog < parsing-error prolog ;
190 : bad-prolog ( prolog -- * )
191 \ bad-prolog parsing-error swap >>prolog throw ;
193 M: bad-prolog summary ( obj -- str )
195 dup call-next-method write
196 "Misplaced XML prolog" print
197 prolog>> write-prolog nl
198 ] with-string-writer ;
200 TUPLE: capitalized-prolog < parsing-error name ;
202 : capitalized-prolog ( name -- capitalized-prolog )
203 \ capitalized-prolog parsing-error swap >>name throw ;
205 M: capitalized-prolog summary ( obj -- str )
207 dup call-next-method write
208 "XML prolog name was partially or totally capitalized, using" print
209 "<?" write name>> write "...?>" write
210 " instead of <?xml...?>" print
211 ] with-string-writer ;
213 TUPLE: versionless-prolog < parsing-error ;
215 : versionless-prolog ( -- * )
216 \ versionless-prolog parsing-error throw ;
218 M: versionless-prolog summary ( obj -- str )
220 call-next-method write
221 "XML prolog lacks a version declaration" print
222 ] with-string-writer ;
224 TUPLE: bad-directive < parsing-error dir ;
226 : bad-directive ( directive -- * )
227 \ bad-directive parsing-error swap >>dir throw ;
229 M: bad-directive summary ( obj -- str )
231 dup call-next-method write
232 "Unknown directive:" print
234 ] with-string-writer ;
236 TUPLE: bad-decl < parsing-error ;
239 \ bad-decl parsing-error throw ;
241 M: bad-decl summary ( obj -- str )
242 call-next-method "\nExtra content in directive" append ;
244 TUPLE: bad-external-id < parsing-error ;
246 : bad-external-id ( -- * )
247 \ bad-external-id parsing-error throw ;
249 M: bad-external-id summary ( obj -- str )
250 call-next-method "\nBad external ID" append ;
252 TUPLE: misplaced-directive < parsing-error dir ;
254 : misplaced-directive ( directive -- * )
255 \ misplaced-directive parsing-error swap >>dir throw ;
257 M: misplaced-directive summary ( obj -- str )
259 dup call-next-method write
260 "Misplaced directive:" print
261 dir>> write-xml-chunk nl
262 ] with-string-writer ;
264 TUPLE: bad-name < parsing-error name ;
266 : bad-name ( name -- * )
267 \ bad-name parsing-error swap >>name throw ;
269 M: bad-name summary ( obj -- str )
271 [ "Invalid name: " swap name>> "\n" 3append ]
274 TUPLE: unclosed-quote < parsing-error ;
276 : unclosed-quote ( -- * )
277 \ unclosed-quote parsing-error throw ;
279 M: unclosed-quote summary
281 "XML document ends with quote still open\n" append ;
283 TUPLE: quoteless-attr < parsing-error ;
285 : quoteless-attr ( -- * )
286 \ quoteless-attr parsing-error throw ;
288 M: quoteless-attr summary
289 call-next-method "Attribute lacks quotes around value\n" append ;
291 TUPLE: attr-w/< < parsing-error ;
293 : attr-w/< ( value -- * )
294 \ attr-w/< parsing-error throw ;
298 "Attribute value contains literal <" append ;
300 TUPLE: text-w/]]> < parsing-error ;
302 : text-w/]]> ( text -- * )
303 \ text-w/]]> parsing-error throw ;
305 M: text-w/]]> summary
307 "Text node contains ']]>'" append ;
309 TUPLE: duplicate-attr < parsing-error key values ;
311 : duplicate-attr ( key values -- * )
312 \ duplicate-attr parsing-error
313 swap >>values swap >>key throw ;
315 M: duplicate-attr summary
316 call-next-method "\nDuplicate attribute" append ;
318 TUPLE: bad-cdata < parsing-error ;
321 \ bad-cdata parsing-error throw ;
324 call-next-method "\nCDATA occurs before or after main tag" append ;
326 TUPLE: not-enough-characters < parsing-error ;
327 : not-enough-characters ( -- * )
328 \ not-enough-characters parsing-error throw ;
329 M: not-enough-characters summary ( obj -- str )
331 call-next-method write
332 "Not enough characters" print
333 ] with-string-writer ;
335 TUPLE: bad-doctype < parsing-error contents ;
336 : bad-doctype ( contents -- * )
337 \ bad-doctype parsing-error swap >>contents throw ;
338 M: bad-doctype summary
339 call-next-method "\nDTD contains invalid object" append ;
341 UNION: xml-parse-error
342 multitags notags extra-attrs nonexist-ns bad-decl
343 not-yes/no unclosed mismatched expected no-entity
344 bad-prolog versionless-prolog capitalized-prolog
345 bad-directive bad-name unclosed-quote quoteless-attr
346 attr-w/< text-w/]]> duplicate-attr ;