@@ -1,29 +1,364 @@
2+#include "../cbits/hexml.h"
2#include <stdio.h>
7#include <stdio.h>
5−#define parse_table hexml_parse_table
9+typedef int bool;
6−#define parser hexml_parser
10+
11+/////////////////////////////////////////////////////////////////////
14+static inline int end(str s) { return s.start + s.length; }
16+static inline str start_length(int32_t start, int32_t length)
18+ assert(start >= 0 && length >= 0);
25+static inline str start_end(int32_t start, int32_t end)
27+ return start_length(start, end - start);
34+ attr* attrs; // dynamically allocated buffer
35+ attr* alloc; // what to call free on
40+ // have a cursor at the front, which is all the stuff I have written out, final
41+ // have a cursor at the end, which is stack scoped, so children write out, then I do
42+ // when you commit, you copy over from end to front
46+ int used_front; // front entries, stored for good
47+ int used_back; // back entries, stack based, copied into front
48+ node* nodes; // dynamically allocated buffer
49+ node* alloc; // what to call free on
55+ const char* body; // pointer to initial argument, not owned by us
57+ // things only used while parsing
58+ const char* cursor; // pointer to where we are in body
59+ const char* end; // pointer to one past the last char
60+ // if cursor is > end we have gone past the end
67+static inline int doc_length(const document* d) { return (int) (d->end - d->body); }
68+static inline int doc_position(const document* d) { return (int) (d->cursor - d->body); }
71+/////////////////////////////////////////////////////////////////////
82+static inline void render_char(render* r, char c)
84+ if (r->cursor < r->length)
85+ r->buffer[r->cursor] = c;
89+static inline void bound(int idx, int mn, int mx)
91+ assert(idx >= mn && idx <= mx);
94+static inline void bound_str(str s, int mn, int mx)
96+ assert(s.length >= 0);
97+ bound(s.start, mn, mx);
98+ bound(end(s), mn, mx);
101+static void render_str(render* r, str s)
103+ bound_str(s, 0, doc_length(r->d));
104+ for (int i = 0; i < s.length; i++)
105+ render_char(r, r->d->body[s.start + i]);
108+static void render_tag(render* r, const node* n);
110+static void render_content(render* r, const node* n)
112+ bound_str(n->inner, 0, doc_length(r->d));
113+ bound_str(n->nodes, 0, r->d->nodes.used_front);
114+ bound_str(n->attrs, 0, r->d->attrs.used);
116+ int done = n->inner.start;
117+ for (int i = 0; i < n->nodes.length; i++)
119+ node* x = &r->d->nodes.nodes[n->nodes.start + i];
120+ render_str(r, start_end(done, x->outer.start));
121+ done = end(x->outer);
124+ render_str(r, start_end(done, end(n->inner)));
127+static void render_tag(render* r, const node* n)
129+ render_char(r, '<');
130+ render_str(r, n->name);
131+ for (int i = 0; i < n->attrs.length; i++)
133+ attr* x = &r->d->attrs.attrs[n->attrs.start + i];
134+ render_char(r, ' ');
135+ render_str(r, x->name);
136+ render_char(r, '=');
137+ render_char(r, '\"');
138+ render_str(r, x->value);
139+ render_char(r, '\"');
141+ render_char(r, '>');
142+ render_content(r, n);
143+ render_char(r, '<');
144+ render_char(r, '/');
145+ render_str(r, n->name);
146+ render_char(r, '>');
149+int hexml_node_render(const document* d, const node* n, char* buffer, int length)
156+ // The root node (and only the root node) has an empty length, so just render its innards
157+ if (n->name.length == 0)
158+ render_content(&r, n);
165+/////////////////////////////////////////////////////////////////////
168+char* hexml_document_error(const document* d){return d->error_message;}
169+node* hexml_document_node(const document* d){return &d->nodes.nodes[0];}
171+void hexml_document_free(document* d)
173+ free(d->error_message);
174+ free(d->nodes.alloc);
175+ free(d->attrs.alloc);
179+node* hexml_node_children(const document* d, const node* n, int* res)
181+ *res = n->nodes.length;
182+ return &d->nodes.nodes[n->nodes.start];
185+attr* hexml_node_attributes(const document* d, const node* n, int* res)
187+ *res = n->attrs.length;
188+ return &d->attrs.attrs[n->attrs.start];
192+attr* hexml_node_attribute(const document* d, const node* n, const char* s, int slen)
194+ if (slen == -1) slen = (int) strlen(s);
195+ const int limit = end(n->attrs);
196+ for (int i = n->attrs.start; i < limit; i++)
198+ attr* r = &d->attrs.attrs[i];
199+ if (r->name.length == slen && memcmp(s, &d->body[r->name.start], slen) == 0)
205+// Search for given strings within a node
206+node* hexml_node_child(const document* d, const node* parent, const node* prev, const char* s, int slen)
208+ if (slen == -1) slen = (int) strlen(s);
209+ int i = prev == NULL ? parent->nodes.start : (int) (prev + 1 - d->nodes.nodes);
210+ const int limit = end(parent->nodes);
211+ for (; i < limit; i++)
213+ node* r = &d->nodes.nodes[i];
214+ if (r->name.length == slen && memcmp(s, &d->body[r->name.start], slen) == 0)
220+/////////////////////////////////////////////////////////////////////
223+static inline void node_alloc(node_buffer* b, int ask)
225+ int space = b->size - b->used_back - b->used_front;
226+ if (space >= ask) return;
227+ int size2 = (b->size + 1000 + ask) * 2;
228+ node* buf2 = malloc(size2 * sizeof(node));
230+ memcpy(buf2, b->nodes, b->used_front * sizeof(node));
231+ memcpy(&buf2[size2 - b->used_back], &b->nodes[b->size - b->used_back], b->used_back * sizeof(node));
238+static inline void attr_alloc(attr_buffer* b, int ask)
240+ int space = b->size - b->used;
241+ if (space >= ask) return;
242+ int size2 = (b->size + 1000 + ask) * 2;
243+ attr* buf2 = malloc(size2 * sizeof(attr));
245+ memcpy(buf2, b->attrs, b->used * sizeof(attr));
252+static void set_error(document* d, const char* msg)
254+ if (d->error_message != NULL) return; // keep the first error message
255+ d->error_message = malloc(strlen(msg)+1);
256+ assert(d->error_message);
257+ strcpy(d->error_message, msg);
260+#define ARGUMENTS , document* d
261+#define VARIABLES const char *tag_start, *name_start, *name_end, *quote_start, *quote_end
263+static inline str gap(const char* ref, const char* start, const char* end)
265+ return start_end(start - ref, end - ref);
8#define abort(x) return x
268#define abort(x) return x
9−#define NameStart printf("NameStart %s\n", p)
269+#define NameStart name_start = p
10−#define NameEnd printf("NameEnd %s\n", p)
270+#define NameEnd name_end = p
11−#define QuoteStart printf("QuoteStart %s\n", p)
271+#define QuoteStart quote_start = p
12−#define QuoteEnd printf("QuoteEnd %s\n", p)
272+#define QuoteEnd \
274+ attr_alloc(&d->attrs, 1); \
275+ d->attrs.attrs[d->attrs.used].name = gap(d->body, name_start, name_end); \
276+ d->attrs.attrs[d->attrs.used].value = gap(d->body, quote_start, quote_end); \
13#define AttribsStart printf("AttribsStart %s\n", p)
278#define AttribsStart printf("AttribsStart %s\n", p)
14#define AttribsEnd printf("AttribsEnd %s\n", p)
279#define AttribsEnd printf("AttribsEnd %s\n", p)
15−#define Tag printf("Tag %s\n", p)
280+#define Tag tag_start = p
16−#define hexml_Tag printf("Tag %s\n", p)
17#define TagComment printf("TagComment %s\n", p)
281#define TagComment printf("TagComment %s\n", p)
18#define TagOpen printf("TagOpen %s\n", p)
282#define TagOpen printf("TagOpen %s\n", p)
19#define TagClose printf("TagClose %s\n", p)
283#define TagClose printf("TagClose %s\n", p)
284+#define TagOpenClose \
285+ d->nodes.nodes[d->nodes.used_front].outer = gap(d->body, tag_start, p); \
20−#define TagOpenClose printf("TagOpenClose %s\n", p)
286+ printf("TagOpenClose %s\n", p)
22#include "test.h"
288#include "test.h"
290+// Based on looking at ~50Kb XML documents, they seem to have ~700 attributes
291+// and ~300 nodes, so size appropriately to cope with that.
299+document* hexml_document_parse(const char* s, int slen)
301+ if (slen == -1) slen = (int) strlen(s);
302+ assert(s[slen] == 0);
303+ // init_parse_table();
305+ buffer* buf = malloc(sizeof(buffer));
307+ document* d = &buf->document;
311+ d->error_message = NULL;
312+ d->attrs.size = 1000;
314+ d->attrs.attrs = buf->attrs;
315+ d->attrs.alloc = NULL;
316+ d->nodes.size = 500;
317+ d->nodes.used_back = 0;
318+ d->nodes.used_front = 1;
319+ d->nodes.nodes = buf->nodes;
320+ d->nodes.alloc = NULL;
322+ d->nodes.nodes[0].name = start_length(0, 0);
323+ d->nodes.nodes[0].outer = start_length(0, slen);
324+ d->nodes.nodes[0].inner = start_length(0, slen);
325+ d->nodes.nodes[0].attrs = start_length(0, 0);
327+ // Introduce an intermediate result, otherwise behaviour is undefined
328+ // because there is no guaranteed ordering between LHS and RHS evaluation
329+ const char* res = parser(s, d);
331+ d->error_message = strdup(res);
332+ // d->nodes.nodes[0].nodes = content;
338+ const document* d = hexml_document_parse("<test foo='123 xyz'/>", -1);
340+ int len = hexml_node_render(d, hexml_document_node(d), buf, sizeof(buf));
342+ printf("Result = %s\n", buf);
25void main()
346void main()
27− const char* res = hexml_parser("<test foo='123 xyz'/>");
348+ setbuf(stdout, NULL);
349+ char* body = "<?xml version=\"1.0\" lang=\"ja\"?><foo><bar baz=\"qux\">quux</bar><bar baz=\"bar\">hoge</bar><piyo>>piyopiyo<</piyo></foo><foo2 />";
350+ body = "<test>X</test> ";
351+ document* doc = document_parse(body, -1);
352+ char* err = document_error(doc);
355+ int len = document_render(doc, NULL, 0);
356+ char* s = malloc(len + 1);
357+ document_render(doc, s, len);
359+ printf("Parse successful, %s\n", s);
28− printf("Result %s\n", res);
362+ printf("Parse failed, %s\n", err);