2 * Sherlock Library -- A simple XML parser
4 * (c) 2007 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/common.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);
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);
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 data ***/
262 xml_flush_chars(struct xml_context *ctx)
264 struct fastbuf *fb = &ctx->chars;
265 if (fb->bufend == fb->buffer)
267 TRACE(ctx, "flush_chars");
268 struct xml_node *n = ctx->node;
269 n->text = xml_end_chars(ctx, &n->len);
270 n->len = fb->bufend - fb->buffer;
271 if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_chars)
277 xml_pop_chars(struct xml_context *ctx)
279 xml_pop_dom(ctx, !(ctx->flags & XML_ALLOC_CHARS));
280 TRACE(ctx, "pop_chars");
284 xml_append_chars(struct xml_context *ctx)
286 TRACE(ctx, "append_chars");
287 struct fastbuf *out = &ctx->chars;
288 while (xml_get_char(ctx) != '<')
289 if (xml_last_char(ctx) == '&')
295 bput_utf8_32(out, xml_last_char(ctx));
299 /*** CDATA sections ***/
302 xml_push_cdata(struct xml_context *ctx)
304 TRACE(ctx, "push_cdata");
305 /* CDSect :== '<![CDATA[' (Char* - (Char* ']]>' Char*)) ']]>'
306 * Already parsed: '<![' */
307 xml_parse_seq(ctx, "CDATA[");
308 struct xml_node *n = xml_push_dom(ctx);
309 n->type = XML_NODE_CHARS;
310 char *p = mp_start_noalign(ctx->pool, 7);
313 if (xml_get_char(ctx) == ']')
315 if (xml_get_char(ctx) == ']')
316 if (xml_get_char(ctx) == '>')
322 p = utf8_32_put(p, xml_last_char(ctx));
323 p = mp_spread(ctx->pool, p, 7);
326 n->len = p - (char *)mp_ptr(ctx->pool);
327 n->text = mp_end(ctx->pool, p + 1);
328 if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_cdata)
333 xml_pop_cdata(struct xml_context *ctx)
335 xml_pop_dom(ctx, !(ctx->flags & XML_ALLOC_CHARS));
337 TRACE(ctx, "pop_cdata");
341 xml_append_cdata(struct xml_context *ctx)
343 TRACE(ctx, "append_cdata");
344 xml_parse_seq(ctx, "CDATA[");
345 struct fastbuf *out = &ctx->chars;
348 if (xml_get_char(ctx) == ']')
350 if (xml_get_char(ctx) == ']')
351 if (xml_get_char(ctx) == '>')
357 bput_utf8_32(out, xml_last_char(ctx));
363 xml_skip_cdata(struct xml_context *ctx)
365 TRACE(ctx, "skip_cdata");
366 xml_parse_seq(ctx, "CDATA[");
367 while (xml_get_char(ctx) != ']' || xml_get_char(ctx) != ']' || xml_get_char(ctx) != '>');
371 /*** Character references ***/
374 xml_parse_char_ref(struct xml_context *ctx)
376 TRACE(ctx, "parse_char_ref");
377 /* CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
378 * Already parsed: '&#' */
380 if (xml_get_char(ctx) == 'x')
382 if (!(xml_get_cat(ctx) & XML_CHAR_XDIGIT))
384 xml_error(ctx, "Expected a hexadecimal value of character reference");
389 v = (v << 4) + Cxvalue(xml_last_char(ctx));
391 while (v < 0x110000 && (xml_get_cat(ctx) & XML_CHAR_XDIGIT));
395 if (!(xml_last_cat(ctx) & XML_CHAR_DIGIT))
397 xml_error(ctx, "Expected a numeric value of character reference");
402 v = v * 10 + xml_last_char(ctx) - '0';
404 while (v < 0x110000 && (xml_get_cat(ctx) & XML_CHAR_DIGIT));
406 uns cat = xml_char_cat(v);
407 if (!(cat & ctx->cat_unrestricted))
409 xml_error(ctx, "Character reference out of range");
412 if (xml_last_char(ctx) == ';')
417 xml_error(ctx, "Expected ';'");
419 while (xml_last_char(ctx) != ';')
422 return UNI_REPLACEMENT;
425 /*** References to general entities ***/
428 xml_parse_ref(struct xml_context *ctx)
430 /* Reference ::= EntityRef | CharRef
431 * EntityRef ::= '&' Name ';'
432 * Already parsed: '&' */
433 struct fastbuf *out = &ctx->chars;
434 if (xml_peek_char(ctx) == '#')
437 bput_utf8_32(out, xml_parse_char_ref(ctx));
441 TRACE(ctx, "parse_ge_ref");
442 struct mempool_state state;
443 mp_save(ctx->stack, &state);
444 char *name = xml_parse_name(ctx, ctx->stack);
445 xml_parse_char(ctx, ';');
446 struct xml_dtd_ent *ent = xml_dtd_find_ent(ctx, name);
449 xml_error(ctx, "Unknown entity &%s;", name);
454 else if (ent->flags & XML_DTD_ENT_TRIVIAL_UNI)
456 TRACE(ctx, "Trivial entity &%s;", name);
457 bput_utf8_32(out, ent->uni);
459 else if (ent->flags & XML_DTD_ENT_TRIVIAL_STR)
461 TRACE(ctx, "Trivial entity &%s;", name);
462 bwrite(out, ent->text, ent->len);
466 TRACE(ctx, "Pushed entity &%s;", name);
467 mp_restore(ctx->stack, &state);
469 xml_push_entity(ctx, ent);
472 mp_restore(ctx->stack, &state);
477 /*** Attribute values ***/
480 xml_parse_attr_value(struct xml_context *ctx, struct xml_dtd_attr *attr UNUSED)
482 TRACE(ctx, "parse_attr_value");
483 /* AttValue ::= '"' ([^<&"] | Reference)* '"' | "'" ([^<&'] | Reference)* "'" */
485 * -- copying from ctx->chars to ctx->pool is not necessary, we could directly write to ctx->pool
486 * -- berare quotes inside parased entities
487 * -- check value constrains / normalize value */
488 struct mempool_state state;
489 uns quote = xml_parse_quote(ctx);
490 mp_save(ctx->stack, &state);
491 xml_start_chars(ctx);
492 struct fastbuf *out = &ctx->chars;
495 uns c = xml_get_char(ctx);
501 else if (c == quote) // FIXME: beware quotes inside parsed entities
504 xml_error(ctx, "Attribute value must not contain '<'");
505 else if (xml_last_cat(ctx) & XML_CHAR_WHITE)
508 bput_utf8_32(out, c);
510 mp_restore(ctx->stack, &state);
512 return xml_end_chars(ctx, &len);
517 struct xml_attrs_table;
520 xml_attrs_hash(struct xml_attrs_table *t UNUSED, struct xml_node *e, char *n)
522 return hash_pointer(e) ^ hash_string(n);
526 xml_attrs_eq(struct xml_attrs_table *t UNUSED, struct xml_node *e1, char *n1, struct xml_node *e2, char *n2)
528 return (e1 == e2) && !strcmp(n1, n2);
532 xml_attrs_init_key(struct xml_attrs_table *t UNUSED, struct xml_attr *a, struct xml_node *e, char *name)
538 slist_add_tail(&e->attrs, &a->n);
541 #define HASH_PREFIX(x) xml_attrs_##x
542 #define HASH_NODE struct xml_attr
543 #define HASH_KEY_COMPLEX(x) x elem, x name
544 #define HASH_KEY_DECL struct xml_node *elem, char *name
545 #define HASH_TABLE_DYNAMIC
547 #define HASH_GIVE_HASHFN
548 #define HASH_GIVE_INIT_KEY
549 #define HASH_WANT_CLEANUP
550 #define HASH_WANT_REMOVE
551 #define HASH_WANT_LOOKUP
552 #define HASH_WANT_FIND
553 #define HASH_GIVE_ALLOC
555 #include "lib/hashtable.h"
558 xml_parse_attr(struct xml_context *ctx)
560 TRACE(ctx, "parse_attr");
561 /* Attribute ::= Name Eq AttValue */
563 * -- memory management
565 struct xml_node *e = ctx->node;
566 char *n = xml_parse_name(ctx, ctx->pool);
567 struct xml_attr *a = xml_attrs_lookup(ctx->tab_attrs, e, n);
569 char *v = xml_parse_attr_value(ctx, NULL);
571 xml_error(ctx, "Attribute %s is not unique", n);
577 xml_attr_find(struct xml_context *ctx, struct xml_node *node, char *name)
579 return xml_attrs_find(ctx->tab_attrs, node, name);
583 xml_attrs_table_init(struct xml_context *ctx)
585 xml_attrs_init(ctx->tab_attrs = xml_hash_new(ctx->pool, sizeof(struct xml_attrs_table)));
589 xml_attrs_table_cleanup(struct xml_context *ctx)
591 xml_attrs_cleanup(ctx->tab_attrs);
597 xml_push_element(struct xml_context *ctx)
599 TRACE(ctx, "push_element");
600 /* EmptyElemTag | STag
601 * EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
602 * STag ::= '<' Name (S Attribute)* S? '>'
603 * Already parsed: '<' */
604 struct xml_node *e = xml_push_dom(ctx);
605 clist_init(&e->sons);
606 e->type = XML_NODE_ELEM;
607 e->name = xml_parse_name(ctx, ctx->pool);
608 slist_init(&e->attrs);
612 if (ctx->doctype && strcmp(e->name, ctx->doctype))
613 xml_error(ctx, "The root element %s does not match the document type %s", e->name, ctx->doctype);
617 uns white = xml_parse_white(ctx, 0);
618 uns c = xml_get_char(ctx);
621 xml_parse_char(ctx, '>');
622 ctx->flags |= XML_EMPTY_ELEM_TAG;
628 xml_fatal_expected_white(ctx);
632 if ((ctx->flags & XML_REPORT_TAGS) && ctx->h_stag)
637 xml_pop_element(struct xml_context *ctx)
639 TRACE(ctx, "pop_element");
640 if ((ctx->flags & XML_REPORT_TAGS) && ctx->h_etag)
642 struct xml_node *e = ctx->node;
643 uns free = !(ctx->flags & XML_ALLOC_TAGS);
648 /* Restore hash table of attributes */
649 SLIST_FOR_EACH(struct xml_attr *, a, e->attrs)
650 xml_attrs_remove(ctx->tab_attrs, a);
652 while (n = clist_head(&e->sons))
654 if (n->type == XML_NODE_ELEM)
656 SLIST_FOR_EACH(struct xml_attr *, a, n->attrs)
657 xml_attrs_remove(ctx->tab_attrs, a);
658 clist_insert_list_after(&n->sons, &n->n);
663 xml_pop_dom(ctx, free);
668 xml_parse_etag(struct xml_context *ctx)
670 /* ETag ::= '</' Name S? '>'
671 * Already parsed: '<' */
672 struct xml_node *e = ctx->node;
678 n = utf8_32_get(n, &c);
679 if (xml_get_char(ctx) != c)
682 xml_parse_white(ctx, 0);
683 if (xml_get_char(ctx) != '>')
686 xml_error(ctx, "Invalid ETag, expected </%s>", e->name);
687 while (xml_get_char(ctx) != '>');
692 /*** Document type declaration ***/
695 xml_parse_doctype_decl(struct xml_context *ctx)
697 TRACE(ctx, "parse_doctype_decl");
698 /* doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? ('[' intSubset ']' S?)? '>'
699 * Already parsed: '<!'
700 * Terminated before '[' or '>' */
702 xml_fatal(ctx, "Multiple document types not allowed");
703 xml_parse_seq(ctx, "DOCTYPE");
704 xml_parse_white(ctx, 1);
705 ctx->doctype = xml_parse_name(ctx, ctx->pool);
706 TRACE(ctx, "doctype=%s", ctx->doctype);
708 if (xml_parse_white(ctx, 0) && ((c = xml_peek_char(ctx)) == 'S' || c == 'P'))
712 xml_parse_seq(ctx, "SYSTEM");
713 xml_parse_white(ctx, 1);
714 ctx->system_id = xml_parse_system_literal(ctx, ctx->pool);
718 xml_parse_seq(ctx, "PUBLIC");
719 xml_parse_white(ctx, 1);
720 ctx->public_id = xml_parse_pubid_literal(ctx, ctx->pool);
721 xml_parse_white(ctx, 1);
722 ctx->system_id = xml_parse_system_literal(ctx, ctx->pool);
724 xml_parse_white(ctx, 0);
725 ctx->flags |= XML_HAS_EXTERNAL_SUBSET;
727 if (xml_peek_char(ctx) == '[')
728 ctx->flags |= XML_HAS_INTERNAL_SUBSET;
729 if (ctx->h_doctype_decl)
730 ctx->h_doctype_decl(ctx);
735 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
737 /* DTD: Internal subset */
740 xml_parse_internal_subset(struct xml_context *ctx)
742 // FIXME: comments/pi have no parent
744 * intSubset :== (markupdecl | DeclSep)
745 * Already parsed: ']' */
748 xml_parse_white(ctx, 0);
749 uns c = xml_get_char(ctx);
752 if ((c = xml_get_char(ctx)) == '!')
753 switch (c = xml_get_char(ctx))
756 xml_push_comment(ctx);
757 xml_pop_comment(ctx);
760 xml_parse_seq(ctx, "OTATION");
761 xml_parse_notation_decl(ctx);
764 if ((c = xml_get_char(ctx)) == 'N')
766 xml_parse_seq(ctx, "TITY");
767 xml_parse_entity_decl(ctx);
771 xml_parse_seq(ctx, "EMENT");
772 xml_parse_element_decl(ctx);
778 xml_parse_seq(ctx, "TTLIST");
779 xml_parse_attr_list_decl(ctx);
792 xml_parse_pe_ref(ctx);
802 xml_fatal(ctx, "Invalid markup in the internal subset");
805 /*** The State Machine ***/
808 xml_next(struct xml_context *ctx)
810 /* A nasty state machine */
812 #define PULL(x) do { if (ctx->pull & XML_PULL_##x) return ctx->state = XML_STATE_##x; case XML_STATE_##x: ; } while (0)
813 #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)
815 TRACE(ctx, "xml_next (state=%u)", ctx->state);
817 ctx->throw_buf = &throw_buf;
818 if (setjmp(throw_buf))
821 if (ctx->err_code == XML_ERR_EOF && ctx->h_fatal)
823 TRACE(ctx, "raised fatal error");
824 return ctx->state = XML_STATE_EOF;
829 case XML_STATE_START:
830 TRACE(ctx, "entering prolog");
831 if (ctx->h_document_start)
832 ctx->h_document_start(ctx);
836 ctx->h_xml_decl(ctx);
839 /* Misc* (doctypedecl Misc*)? */
842 xml_parse_white(ctx, 0);
843 xml_parse_char(ctx, '<');
845 if ((c = xml_get_char(ctx)) == '?')
846 /* Processing intruction */
847 if (!(ctx->flags & XML_REPORT_PIS))
852 PULL_STATE(PI, PROLOG_PI);
857 /* Found the root tag */
861 else if (xml_get_char(ctx) == '-')
862 if (!(ctx->flags & XML_REPORT_COMMENTS))
863 xml_skip_comment(ctx);
866 xml_push_comment(ctx);
867 PULL_STATE(COMMENT, PROLOG_COMMENT);
868 xml_pop_comment(ctx);
874 xml_parse_doctype_decl(ctx);
876 if (xml_peek_char(ctx) == '[')
880 if (ctx->flags & XML_PARSE_DTD)
883 if (ctx->h_dtd_start)
884 ctx->h_dtd_start(ctx);
885 // FIXME: pull iface?
886 xml_parse_internal_subset(ctx);
887 // FIXME: external subset
892 xml_skip_internal_subset(ctx);
894 xml_parse_white(ctx, 0);
895 xml_parse_char(ctx, '>');
900 case XML_STATE_CHARS:
904 if (xml_peek_char(ctx) != '<')
907 xml_append_chars(ctx);
915 if ((c = xml_get_char(ctx)) == '?')
918 if (!(ctx->flags & (XML_REPORT_PIS | XML_ALLOC_PIS)))
922 if (xml_flush_chars(ctx))
924 PULL_STATE(CHARS, CHARS_BEFORE_PI);
934 if ((c = xml_get_char(ctx)) == '-')
937 if (!(ctx->flags & (XML_REPORT_COMMENTS | XML_ALLOC_COMMENTS)))
938 xml_skip_comment(ctx);
941 if (xml_flush_chars(ctx))
943 PULL_STATE(CHARS, CHARS_BEFORE_COMMENT);
946 xml_push_comment(ctx);
948 xml_pop_comment(ctx);
954 if (!(ctx->flags & XML_UNFOLD_CDATA))
955 xml_append_cdata(ctx);
958 if (xml_flush_chars(ctx))
960 PULL_STATE(CHARS, CHARS_BEFORE_CDATA);
969 xml_fatal(ctx, "Unexpected character after '<!'");
973 /* STag | EmptyElemTag */
975 if (xml_flush_chars(ctx))
977 PULL_STATE(CHARS, CHARS_BEFORE_STAG);
981 xml_push_element(ctx);
983 if (ctx->flags & XML_EMPTY_ELEM_TAG)
990 if (xml_flush_chars(ctx))
992 PULL_STATE(CHARS, CHARS_BEFORE_ETAG);
999 xml_pop_element(ctx);
1007 TRACE(ctx, "entering epilog");
1010 /* Epilog whitespace is the only place, where a valid document can reach EOF */
1011 if (setjmp(throw_buf))
1012 if (ctx->err_code == XML_ERR_EOF)
1014 TRACE(ctx, "reached EOF");
1015 ctx->state = XML_STATE_EOF;
1016 if (ctx->h_document_end)
1017 ctx->h_document_end(ctx);
1020 ctx->err_msg = NULL;
1021 return XML_STATE_EOF;
1025 xml_parse_white(ctx, 0);
1026 if (setjmp(throw_buf))
1030 xml_parse_char(ctx, '<');
1032 if ((c = xml_get_char(ctx)) == '?')
1033 /* Processing instruction */
1034 if (!(ctx->flags & XML_REPORT_PIS))
1039 PULL_STATE(PI, EPILOG_PI);
1044 xml_parse_char(ctx, '-');
1046 if (!(ctx->flags & XML_REPORT_COMMENTS))
1047 xml_skip_comment(ctx);
1050 xml_push_comment(ctx);
1051 PULL_STATE(COMMENT, EPILOG_COMMENT);
1052 xml_pop_comment(ctx);
1056 xml_fatal(ctx, "Syntax error in the epilog");
1064 xml_parse(struct xml_context *ctx)
1066 /* This cycle shoud run only once unless the user overrides the value of ctx->pull in a SAX handler */
1071 while (xml_next(ctx));
1072 return ctx->err_code;