2 * Sherlock Library -- A simple XML parser
4 * (c) 2007--2008 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
12 #include "sherlock/sherlock.h"
13 #include "sherlock/xml/xml.h"
14 #include "sherlock/xml/dtd.h"
15 #include "sherlock/xml/internals.h"
16 #include "lib/fastbuf.h"
17 #include "lib/ff-unicode.h"
18 #include "lib/unicode.h"
19 #include "lib/chartype.h"
20 #include "lib/hashfunc.h"
24 /*** Basic parsing ***/
27 xml_fatal_expected(struct xml_context *ctx, uns c)
29 if (c >= 32 && c < 128)
30 xml_fatal(ctx, "Expected '%c'", c);
32 xml_fatal(ctx, "Expected U+%04x", c);
36 xml_fatal_expected_white(struct xml_context *ctx)
38 xml_fatal(ctx, "Expected a white space");
42 xml_fatal_expected_quot(struct xml_context *ctx)
44 xml_fatal(ctx, "Expected a quotation mark");
48 xml_parse_eq(struct xml_context *ctx)
50 /* Eq ::= S? '=' S? */
51 xml_parse_white(ctx, 0);
52 xml_parse_char(ctx, '=');
53 xml_parse_white(ctx, 0);
56 /*** Names and nmtokens ***/
59 xml_parse_string(struct xml_context *ctx, struct mempool *pool, uns first_cat, uns next_cat, char *err)
61 char *p = mp_start_noalign(pool, 1);
62 if (unlikely(!(xml_peek_cat(ctx) & first_cat)))
63 xml_fatal(ctx, "%s", err);
66 p = mp_spread(pool, p, 5);
67 p = utf8_32_put(p, xml_skip_char(ctx));
69 while (xml_peek_cat(ctx) & next_cat);
71 return mp_end(pool, p);
75 xml_skip_string(struct xml_context *ctx, uns first_cat, uns next_cat, char *err)
77 if (unlikely(!(xml_get_cat(ctx) & first_cat)))
78 xml_fatal(ctx, "%s", err);
79 while (xml_peek_cat(ctx) & next_cat)
84 xml_parse_name(struct xml_context *ctx, struct mempool *pool)
86 /* Name ::= NameStartChar (NameChar)* */
87 return xml_parse_string(ctx, pool, ctx->cat_sname, ctx->cat_name, "Expected a name");
91 xml_skip_name(struct xml_context *ctx)
93 xml_skip_string(ctx, ctx->cat_sname, ctx->cat_name, "Expected a name");
97 xml_parse_nmtoken(struct xml_context *ctx, struct mempool *pool)
99 /* Nmtoken ::= (NameChar)+ */
100 return xml_parse_string(ctx, pool, ctx->cat_name, ctx->cat_name, "Expected a nmtoken");
103 /*** Simple literals ***/
106 xml_parse_system_literal(struct xml_context *ctx, struct mempool *pool)
108 /* SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'") */
109 char *p = mp_start_noalign(pool, 1);
110 uns q = xml_parse_quote(ctx), c;
111 while ((c = xml_get_char(ctx)) != q)
113 p = mp_spread(pool, p, 5);
114 p = utf8_32_put(p, c);
117 return mp_end(pool, p);
121 xml_parse_pubid_literal(struct xml_context *ctx, struct mempool *pool)
123 /* PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'" */
124 char *p = mp_start_noalign(pool, 1);
125 uns q = xml_parse_quote(ctx), c;
126 while ((c = xml_get_char(ctx)) != q)
128 if (unlikely(!(xml_last_cat(ctx) & XML_CHAR_PUBID)))
129 xml_fatal(ctx, "Expected a pubid character");
130 p = mp_spread(pool, p, 2);
134 return mp_end(pool, p);
140 xml_push_comment(struct xml_context *ctx)
142 TRACE(ctx, "push_comment");
143 /* Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
144 * Already parsed: '<!-' */
145 xml_parse_char(ctx, '-');
146 struct xml_node *n = xml_push_dom(ctx, NULL);
147 n->type = XML_NODE_COMMENT;
148 char *p = mp_start_noalign(ctx->pool, 6);
151 if (xml_get_char(ctx) == '-')
152 if (xml_get_char(ctx) == '-')
156 p = utf8_32_put(p, xml_last_char(ctx));
157 p = mp_spread(ctx->pool, p, 6);
159 xml_parse_char(ctx, '>');
161 n->len = p - (char *)mp_ptr(ctx->pool);
162 n->text = mp_end(ctx->pool, p + 1);
163 if ((ctx->flags & XML_REPORT_COMMENTS) && ctx->h_comment)
168 xml_pop_comment(struct xml_context *ctx)
170 xml_pop_dom(ctx, !(ctx->flags & XML_ALLOC_COMMENTS));
172 TRACE(ctx, "pop_comment");
176 xml_skip_comment(struct xml_context *ctx)
178 TRACE(ctx, "skip_comment");
179 xml_parse_char(ctx, '-');
180 while (xml_get_char(ctx) != '-' || xml_get_char(ctx) != '-');
181 xml_parse_char(ctx, '>');
185 /*** Processing instructions ***/
188 xml_push_pi(struct xml_context *ctx)
190 TRACE(ctx, "push_pi");
191 /* Parses a PI to ctx->value and ctx->name:
192 * PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
193 * PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
194 * Already parsed: '<?' */
195 struct xml_node *n = xml_push_dom(ctx, NULL);
196 n->type = XML_NODE_PI;
197 n->name = xml_parse_name(ctx, ctx->pool);
198 if (unlikely(!strcasecmp(n->name, "xml")))
199 xml_error(ctx, "Reserved PI target");
200 char *p = mp_start_noalign(ctx->pool, 5);
201 if (!xml_parse_white(ctx, 0))
202 xml_parse_seq(ctx, "?>");
206 if (xml_get_char(ctx) == '?')
207 if (xml_peek_char(ctx) == '>')
215 p = utf8_32_put(p, xml_last_char(ctx));
216 p = mp_spread(ctx->pool, p, 5);
219 n->len = p - (char *)mp_ptr(ctx->pool);
220 n->text = mp_end(ctx->pool, p + 1);
221 if ((ctx->flags & XML_REPORT_PIS) && ctx->h_pi)
226 xml_pop_pi(struct xml_context *ctx)
228 xml_pop_dom(ctx, !(ctx->flags & XML_ALLOC_PIS));
230 TRACE(ctx, "pop_pi");
234 xml_skip_pi(struct xml_context *ctx)
236 TRACE(ctx, "skip_pi");
237 if (ctx->flags & XML_VALIDATING)
239 struct mempool_state state;
240 mp_save(ctx->stack, &state);
241 if (unlikely(!strcasecmp(xml_parse_name(ctx, ctx->stack), "xml")))
242 xml_error(ctx, "Reserved PI target");
243 mp_restore(ctx->stack, &state);
244 if (!xml_parse_white(ctx, 0))
246 xml_parse_seq(ctx, "?>");
252 if (xml_get_char(ctx) == '?')
253 if (xml_peek_char(ctx) == '>')
259 /*** Character references ***/
262 xml_parse_char_ref(struct xml_context *ctx)
264 TRACE(ctx, "parse_char_ref");
265 /* CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
266 * Already parsed: '&#' */
268 if (xml_get_char(ctx) == 'x')
270 if (!(xml_get_cat(ctx) & XML_CHAR_XDIGIT))
272 xml_error(ctx, "Expected a hexadecimal value of character reference");
277 v = (v << 4) + Cxvalue(xml_last_char(ctx));
279 while (v < 0x110000 && (xml_get_cat(ctx) & XML_CHAR_XDIGIT));
283 if (!(xml_last_cat(ctx) & XML_CHAR_DIGIT))
285 xml_error(ctx, "Expected a numeric value of character reference");
290 v = v * 10 + xml_last_char(ctx) - '0';
292 while (v < 0x110000 && (xml_get_cat(ctx) & XML_CHAR_DIGIT));
294 uns cat = xml_char_cat(v);
295 if (!(cat & ctx->cat_unrestricted))
297 xml_error(ctx, "Character reference out of range");
300 if (xml_last_char(ctx) == ';')
305 xml_error(ctx, "Expected ';'");
307 while (xml_last_char(ctx) != ';')
310 return UNI_REPLACEMENT;
313 /*** References to general entities ***/
316 xml_parse_ref(struct xml_context *ctx)
318 /* Reference ::= EntityRef | CharRef
319 * EntityRef ::= '&' Name ';'
320 * Already parsed: '&' */
321 struct fastbuf *out = &ctx->chars;
322 if (xml_peek_char(ctx) == '#')
325 bput_utf8_32(out, xml_parse_char_ref(ctx));
329 TRACE(ctx, "parse_ge_ref");
330 struct mempool_state state;
331 mp_save(ctx->stack, &state);
332 char *name = xml_parse_name(ctx, ctx->stack);
333 xml_parse_char(ctx, ';');
334 struct xml_dtd_entity *ent = xml_dtd_find_entity(ctx, name);
337 xml_error(ctx, "Unknown entity &%s;", name);
342 else if (ent->flags & XML_DTD_ENTITY_TRIVIAL)
344 TRACE(ctx, "Trivial entity &%s;", name);
345 bputs(out, ent->text);
349 TRACE(ctx, "Pushed entity &%s;", name);
350 mp_restore(ctx->stack, &state);
352 xml_push_entity(ctx, ent);
355 mp_restore(ctx->stack, &state);
360 /*** Character data ***/
363 xml_spout_chars(struct fastbuf *fb)
365 if (fb->bptr < fb->bufend)
367 struct xml_context *ctx = SKIP_BACK(struct xml_context, chars, fb);
368 struct mempool *pool = ctx->pool;
369 if (fb->bufend != fb->buffer)
371 TRACE(ctx, "growing chars");
372 uns len = fb->bufend - fb->buffer;
373 uns reported = fb->bstop - fb->buffer;
374 fb->buffer = mp_expand(pool);
375 fb->bufend = fb->buffer + mp_avail(pool);
376 fb->bptr = fb->buffer + len;
377 fb->bstop = fb->buffer + reported;
381 TRACE(ctx, "starting chars");
382 mp_save(pool, &ctx->chars_state);
383 fb->bptr = fb->buffer = fb->bstop = mp_start_noalign(pool, 2);
384 fb->bufend = fb->buffer + mp_avail(pool) - 1;
389 xml_end_chars(struct xml_context *ctx, char **out)
391 struct fastbuf *fb = &ctx->chars;
392 uns len = fb->bptr - fb->buffer;
395 TRACE(ctx, "ending chars");
397 *out = mp_end(ctx->pool, fb->bptr + 1);
398 fb->bufend = fb->bstop = fb->bptr = fb->buffer;
404 xml_report_chars(struct xml_context *ctx, char **out)
406 struct fastbuf *fb = &ctx->chars;
407 uns len = fb->bptr - fb->buffer;
412 fb->bstop = fb->bptr;
418 xml_flush_chars(struct xml_context *ctx)
421 uns len = xml_end_chars(ctx, &text), rlen;
424 if (ctx->flags & XML_NO_CHARS)
426 if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_ignorable)
427 ctx->h_ignorable(ctx, text, len);
428 mp_restore(ctx->pool, &ctx->chars_state);
431 if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_block && (rlen = xml_report_chars(ctx, &rtext)))
432 ctx->h_block(ctx, rtext, rlen);
433 if (!(ctx->flags & XML_ALLOC_CHARS) && !(ctx->flags & XML_REPORT_CHARS))
435 mp_restore(ctx->pool, &ctx->chars_state);
438 struct xml_node *n = xml_push_dom(ctx, &ctx->chars_state);
439 n->type = XML_NODE_CHARS;
442 if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_chars)
449 xml_pop_chars(struct xml_context *ctx)
451 xml_pop_dom(ctx, !(ctx->flags & XML_ALLOC_CHARS));
452 TRACE(ctx, "pop_chars");
456 xml_append_chars(struct xml_context *ctx)
458 TRACE(ctx, "append_chars");
459 struct fastbuf *out = &ctx->chars;
460 if (ctx->flags & XML_NO_CHARS)
461 while (xml_get_char(ctx) != '<')
462 if (xml_last_cat(ctx) & XML_CHAR_WHITE)
463 bput_utf8_32(out, xml_last_char(ctx));
466 xml_error(ctx, "This element must not contain character data");
467 while (xml_get_char(ctx) != '<');
471 while (xml_get_char(ctx) != '<')
472 if (xml_last_char(ctx) == '&')
478 bput_utf8_32(out, xml_last_char(ctx));
482 /*** CDATA sections ***/
485 xml_skip_cdata(struct xml_context *ctx)
487 TRACE(ctx, "skip_cdata");
488 xml_parse_seq(ctx, "CDATA[");
489 while (xml_get_char(ctx) != ']' || xml_get_char(ctx) != ']' || xml_get_char(ctx) != '>');
494 xml_append_cdata(struct xml_context *ctx)
496 /* CDSect :== '<![CDATA[' (Char* - (Char* ']]>' Char*)) ']]>'
497 * Already parsed: '<![' */
498 TRACE(ctx, "append_cdata");
499 if (ctx->flags & XML_NO_CHARS)
501 xml_error(ctx, "This element must not contain CDATA");
505 xml_parse_seq(ctx, "CDATA[");
506 struct fastbuf *out = &ctx->chars;
509 if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_block && (rlen = xml_report_chars(ctx, &rtext)))
510 ctx->h_block(ctx, rtext, rlen);
513 if (xml_get_char(ctx) == ']')
515 if (xml_get_char(ctx) == ']')
516 if (xml_get_char(ctx) == '>')
522 bput_utf8_32(out, xml_last_char(ctx));
524 if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_cdata && (rlen = xml_report_chars(ctx, &rtext)))
525 ctx->h_cdata(ctx, rtext, rlen);
529 /*** Attribute values ***/
532 xml_parse_attr_value(struct xml_context *ctx, struct xml_dtd_attr *attr UNUSED)
534 TRACE(ctx, "parse_attr_value");
535 /* AttValue ::= '"' ([^<&"] | Reference)* '"' | "'" ([^<&'] | Reference)* "'" */
536 /* FIXME: -- check value constrains / normalize leading/trailing WS and repeated WS */
537 struct mempool_state state;
538 uns quote = xml_parse_quote(ctx);
539 mp_save(ctx->stack, &state);
540 struct fastbuf *out = &ctx->chars;
541 struct xml_source *src = ctx->src;
544 uns c = xml_get_char(ctx);
550 else if (c == quote && src == ctx->src)
553 xml_error(ctx, "Attribute value must not contain '<'");
554 else if (xml_last_cat(ctx) & XML_CHAR_WHITE)
557 bput_utf8_32(out, c);
559 mp_restore(ctx->stack, &state);
561 return xml_end_chars(ctx, &text) ? text : "";
565 xml_normalize_white(struct xml_context *ctx UNUSED, char *text)
567 char *s = text, *d = text;
576 while (*++s == 0x20);
579 if (d != text && d[-1] == 0x20)
587 struct xml_attrs_table;
590 xml_attrs_hash(struct xml_attrs_table *t UNUSED, struct xml_node *e, char *n)
592 return hash_pointer(e) ^ hash_string(n);
596 xml_attrs_eq(struct xml_attrs_table *t UNUSED, struct xml_node *e1, char *n1, struct xml_node *e2, char *n2)
598 return (e1 == e2) && !strcmp(n1, n2);
602 xml_attrs_init_key(struct xml_attrs_table *t UNUSED, struct xml_attr *a, struct xml_node *e, char *name)
608 slist_add_tail(&e->attrs, &a->n);
611 #define HASH_PREFIX(x) xml_attrs_##x
612 #define HASH_NODE struct xml_attr
613 #define HASH_KEY_COMPLEX(x) x elem, x name
614 #define HASH_KEY_DECL struct xml_node *elem, char *name
615 #define HASH_TABLE_DYNAMIC
617 #define HASH_GIVE_HASHFN
618 #define HASH_GIVE_INIT_KEY
619 #define HASH_WANT_CLEANUP
620 #define HASH_WANT_REMOVE
621 #define HASH_WANT_LOOKUP
622 #define HASH_WANT_FIND
623 #define HASH_GIVE_ALLOC
625 #include "lib/hashtable.h"
628 xml_parse_attr(struct xml_context *ctx)
630 TRACE(ctx, "parse_attr");
631 /* Attribute ::= Name Eq AttValue */
632 struct xml_node *e = ctx->node;
633 char *n = xml_parse_name(ctx, ctx->pool);
634 struct xml_attr *a = xml_attrs_lookup(ctx->tab_attrs, e, n);
636 char *v = xml_parse_attr_value(ctx, NULL);
639 xml_error(ctx, "Attribute %s is not unique in element <%s>", n, e->name);
645 else if (!(a->dtd = xml_dtd_find_attr(ctx, e->dtd, a->name)))
646 xml_error(ctx, "Undefined attribute %s in element <%s>", n, e->name);
648 xml_validate_attr(ctx, a->dtd, a->val);
652 xml_attr_find(struct xml_context *ctx, struct xml_node *node, char *name)
654 return xml_attrs_find(ctx->tab_attrs, node, name);
658 xml_attr_value(struct xml_context *ctx, struct xml_node *node, char *name)
660 struct xml_attr *attr = xml_attrs_find(ctx->tab_attrs, node, name);
665 struct xml_dtd_attr *dtd = xml_dtd_find_attr(ctx, node->dtd, name);
666 return dtd ? dtd->default_value : NULL;
670 xml_attrs_table_init(struct xml_context *ctx)
672 xml_attrs_init(ctx->tab_attrs = xml_hash_new(ctx->pool, sizeof(struct xml_attrs_table)));
676 xml_attrs_table_cleanup(struct xml_context *ctx)
678 xml_attrs_cleanup(ctx->tab_attrs);
684 xml_push_element(struct xml_context *ctx)
686 TRACE(ctx, "push_element");
687 /* EmptyElemTag | STag
688 * EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
689 * STag ::= '<' Name (S Attribute)* S? '>'
690 * Already parsed: '<' */
691 struct xml_node *e = xml_push_dom(ctx, NULL);
692 clist_init(&e->sons);
693 e->type = XML_NODE_ELEM;
694 e->name = xml_parse_name(ctx, ctx->pool);
695 slist_init(&e->attrs);
699 if (ctx->doctype && strcmp(e->name, ctx->doctype))
700 xml_error(ctx, "The root element <%s> does not match the document type <%s>", e->name, ctx->doctype);
704 else if (!(e->dtd = xml_dtd_find_elem(ctx, e->name)))
705 xml_error(ctx, "Undefined element <%s>", e->name);
708 if (e->dtd->type == XML_DTD_ELEM_MIXED)
709 ctx->flags &= ~XML_NO_CHARS;
711 ctx->flags |= XML_NO_CHARS;
713 // FIXME: validate regular expressions
717 uns white = xml_parse_white(ctx, 0);
718 uns c = xml_get_char(ctx);
721 xml_parse_char(ctx, '>');
722 ctx->flags |= XML_EMPTY_ELEM_TAG;
728 xml_fatal_expected_white(ctx);
732 if ((ctx->flags & XML_REPORT_TAGS) && ctx->h_stag)
737 xml_pop_element(struct xml_context *ctx)
739 TRACE(ctx, "pop_element");
740 if ((ctx->flags & XML_REPORT_TAGS) && ctx->h_etag)
742 struct xml_node *e = ctx->node;
743 uns free = !(ctx->flags & XML_ALLOC_TAGS);
748 /* Restore hash table of attributes */
749 SLIST_FOR_EACH(struct xml_attr *, a, e->attrs)
750 xml_attrs_remove(ctx->tab_attrs, a);
752 while (n = clist_head(&e->sons))
754 if (n->type == XML_NODE_ELEM)
756 SLIST_FOR_EACH(struct xml_attr *, a, n->attrs)
757 xml_attrs_remove(ctx->tab_attrs, a);
758 clist_insert_list_after(&n->sons, &n->n);
763 xml_pop_dom(ctx, free);
768 xml_parse_etag(struct xml_context *ctx)
770 /* ETag ::= '</' Name S? '>'
771 * Already parsed: '<' */
772 struct xml_node *e = ctx->node;
778 n = utf8_32_get(n, &c);
779 if (xml_get_char(ctx) != c)
782 xml_parse_white(ctx, 0);
783 if (xml_get_char(ctx) != '>')
786 xml_error(ctx, "Invalid ETag, expected </%s>", e->name);
787 while (xml_get_char(ctx) != '>');
792 /*** Document type declaration ***/
795 xml_parse_doctype_decl(struct xml_context *ctx)
797 TRACE(ctx, "parse_doctype_decl");
798 /* doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? ('[' intSubset ']' S?)? '>'
799 * Already parsed: '<!'
800 * Terminated before '[' or '>' */
802 xml_fatal(ctx, "Multiple document types not allowed");
803 xml_parse_seq(ctx, "DOCTYPE");
804 xml_parse_white(ctx, 1);
805 ctx->doctype = xml_parse_name(ctx, ctx->pool);
806 TRACE(ctx, "doctype=%s", ctx->doctype);
808 if (xml_parse_white(ctx, 0) && ((c = xml_peek_char(ctx)) == 'S' || c == 'P'))
812 xml_parse_seq(ctx, "SYSTEM");
813 xml_parse_white(ctx, 1);
814 ctx->system_id = xml_parse_system_literal(ctx, ctx->pool);
818 xml_parse_seq(ctx, "PUBLIC");
819 xml_parse_white(ctx, 1);
820 ctx->public_id = xml_parse_pubid_literal(ctx, ctx->pool);
821 xml_parse_white(ctx, 1);
822 ctx->system_id = xml_parse_system_literal(ctx, ctx->pool);
824 xml_parse_white(ctx, 0);
825 ctx->flags |= XML_HAS_EXTERNAL_SUBSET;
827 if (xml_peek_char(ctx) == '[')
828 ctx->flags |= XML_HAS_INTERNAL_SUBSET;
829 if (ctx->h_doctype_decl)
830 ctx->h_doctype_decl(ctx);
835 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
837 /* DTD: Internal subset */
840 xml_parse_internal_subset(struct xml_context *ctx)
842 // FIXME: comments/pi have no parent
844 * intSubset :== (markupdecl | DeclSep)
845 * Already parsed: ']' */
848 xml_parse_white(ctx, 0);
849 uns c = xml_get_char(ctx);
852 if ((c = xml_get_char(ctx)) == '!')
853 switch (c = xml_get_char(ctx))
856 xml_push_comment(ctx);
857 xml_pop_comment(ctx);
860 xml_parse_seq(ctx, "OTATION");
861 xml_parse_notation_decl(ctx);
864 if ((c = xml_get_char(ctx)) == 'N')
866 xml_parse_seq(ctx, "TITY");
867 xml_parse_entity_decl(ctx);
871 xml_parse_seq(ctx, "EMENT");
872 xml_parse_element_decl(ctx);
878 xml_parse_seq(ctx, "TTLIST");
879 xml_parse_attr_list_decl(ctx);
892 xml_parse_pe_ref(ctx);
902 xml_fatal(ctx, "Invalid markup in the internal subset");
905 /*** The State Machine ***/
908 xml_next(struct xml_context *ctx)
910 /* A nasty state machine */
912 #define PULL(x) do { if (ctx->pull & XML_PULL_##x) return ctx->state = XML_STATE_##x; case XML_STATE_##x: ; } while (0)
913 #define PULL_STATE(x, s) do { if (ctx->pull & XML_PULL_##x) return ctx->state = XML_STATE_##s, XML_STATE_##x; case XML_STATE_##s: ; } while (0)
915 TRACE(ctx, "xml_next (state=%u)", ctx->state);
917 ctx->throw_buf = &throw_buf;
918 if (setjmp(throw_buf))
921 if (ctx->err_code == XML_ERR_EOF && ctx->h_fatal)
923 TRACE(ctx, "raised fatal error");
924 return ctx->state = XML_STATE_EOF;
929 case XML_STATE_START:
930 TRACE(ctx, "entering prolog");
931 ctx->flags |= XML_SRC_DOCUMENT | XML_SRC_EXPECTED_DECL;
932 if (ctx->h_document_start)
933 ctx->h_document_start(ctx);
937 ctx->h_xml_decl(ctx);
940 /* Misc* (doctypedecl Misc*)? */
943 xml_parse_white(ctx, 0);
944 xml_parse_char(ctx, '<');
946 if ((c = xml_get_char(ctx)) == '?')
947 /* Processing intruction */
948 if (!(ctx->flags & XML_REPORT_PIS))
953 PULL_STATE(PI, PROLOG_PI);
958 /* Found the root tag */
962 else if (xml_get_char(ctx) == '-')
963 if (!(ctx->flags & XML_REPORT_COMMENTS))
964 xml_skip_comment(ctx);
967 xml_push_comment(ctx);
968 PULL_STATE(COMMENT, PROLOG_COMMENT);
969 xml_pop_comment(ctx);
975 xml_parse_doctype_decl(ctx);
977 if (xml_peek_char(ctx) == '[')
981 if (ctx->flags & XML_PARSE_DTD)
984 if (ctx->h_dtd_start)
985 ctx->h_dtd_start(ctx);
986 // FIXME: pull iface?
987 xml_parse_internal_subset(ctx);
988 // FIXME: external subset
993 xml_skip_internal_subset(ctx);
995 xml_parse_white(ctx, 0);
996 xml_parse_char(ctx, '>');
1001 case XML_STATE_CHARS:
1005 if (xml_peek_char(ctx) != '<')
1008 xml_append_chars(ctx);
1016 if ((c = xml_get_char(ctx)) == '?')
1019 if (!(ctx->flags & (XML_REPORT_PIS | XML_ALLOC_PIS)))
1023 if (xml_flush_chars(ctx))
1025 PULL_STATE(CHARS, CHARS_BEFORE_PI);
1035 if ((c = xml_get_char(ctx)) == '-')
1038 if (!(ctx->flags & (XML_REPORT_COMMENTS | XML_ALLOC_COMMENTS)))
1039 xml_skip_comment(ctx);
1042 if (xml_flush_chars(ctx))
1044 PULL_STATE(CHARS, CHARS_BEFORE_COMMENT);
1047 xml_push_comment(ctx);
1049 xml_pop_comment(ctx);
1055 xml_append_cdata(ctx);
1058 xml_fatal(ctx, "Unexpected character after '<!'");
1062 /* STag | EmptyElemTag */
1063 xml_unget_char(ctx);
1064 if (xml_flush_chars(ctx))
1066 PULL_STATE(CHARS, CHARS_BEFORE_STAG);
1070 xml_push_element(ctx);
1072 if (ctx->flags & XML_EMPTY_ELEM_TAG)
1079 if (xml_flush_chars(ctx))
1081 PULL_STATE(CHARS, CHARS_BEFORE_ETAG);
1085 xml_parse_etag(ctx);
1088 xml_pop_element(ctx);
1096 TRACE(ctx, "entering epilog");
1099 /* Epilog whitespace is the only place, where a valid document can reach EOF */
1100 if (setjmp(throw_buf))
1101 if (ctx->err_code == XML_ERR_EOF)
1103 TRACE(ctx, "reached EOF");
1104 ctx->state = XML_STATE_EOF;
1105 if (ctx->h_document_end)
1106 ctx->h_document_end(ctx);
1109 ctx->err_msg = NULL;
1110 return XML_STATE_EOF;
1114 xml_parse_white(ctx, 0);
1115 if (setjmp(throw_buf))
1119 xml_parse_char(ctx, '<');
1121 if ((c = xml_get_char(ctx)) == '?')
1122 /* Processing instruction */
1123 if (!(ctx->flags & XML_REPORT_PIS))
1128 PULL_STATE(PI, EPILOG_PI);
1133 xml_parse_char(ctx, '-');
1135 if (!(ctx->flags & XML_REPORT_COMMENTS))
1136 xml_skip_comment(ctx);
1139 xml_push_comment(ctx);
1140 PULL_STATE(COMMENT, EPILOG_COMMENT);
1141 xml_pop_comment(ctx);
1145 xml_fatal(ctx, "Syntax error in the epilog");
1153 xml_next_state(struct xml_context *ctx, uns pull)
1155 uns saved = ctx->pull;
1157 uns res = xml_next(ctx);
1163 xml_skip_element(struct xml_context *ctx)
1165 ASSERT(ctx->state == XML_STATE_STAG);
1166 struct xml_node *node = ctx->node;
1167 uns saved = ctx->pull, res;
1168 ctx->pull = XML_PULL_ETAG;
1169 while ((res = xml_next(ctx)) && ctx->node != node);
1175 xml_parse(struct xml_context *ctx)
1177 /* This cycle should run only once unless the user overrides the value of ctx->pull in a SAX handler */
1182 while (xml_next(ctx));
1183 return ctx->err_code;