@@ -3,11 +3,8 @@
33#include <stdlib.h>
44#include <string.h>
55#include <assert.h>
6−#include <ctype.h>
76#include <stdio.h>
87
9−typedef int bool;
10−
118/////////////////////////////////////////////////////////////////////
129// TYPES
1310
@@ -44,7 +41,8 @@ typedef struct
4441 // nodes
4542 int size;
4643 int used_front; // front entries, stored for good
47− int used_back; // back entries, stack based, copied into front
44+ int used_back; // back entries, stack based, copied into front on </close>
45+ // back entries which haven't yet been closed have nodes.length == -1
4846 node* nodes; // dynamically allocated buffer
4947 node* alloc; // what to call free on
5048} node_buffer;
@@ -53,30 +51,24 @@ typedef struct
5351struct document
5452{
5553 const char* body; // pointer to initial argument, not owned by us
56−
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
54+ int body_len; // length of the body
6155
6256 char* error_message;
6357 node_buffer nodes;
6458 attr_buffer attrs;
6559};
6660
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); }
69−
7061
7162/////////////////////////////////////////////////////////////////////
7263// RENDER CODE
7364
7465typedef struct
7566{
76− const document* d;
77− char* buffer;
78− int length;
79− int cursor;
67+ const document* d; // Document, used to get at the body
68+ char* buffer; // Buffer we are writing too
69+ int length; // Length of buffer, do not write past the end.
70+ // No character is reserved for a NULL terminator.
71+ int cursor; // Cursor of where we are writing now
8072} render;
8173
8274static inline void render_char(render* r, char c)
@@ -100,7 +92,7 @@ static inline void bound_str(str s, int mn, int mx)
10092
10193static void render_str(render* r, str s)
10294{
103− bound_str(s, 0, doc_length(r->d));
95+ bound_str(s, 0, r->d->body_len);
10496 for (int i = 0; i < s.length; i++)
10597 render_char(r, r->d->body[s.start + i]);
10698}
@@ -109,7 +101,7 @@ static void render_tag(render* r, const node* n);
109101
110102static void render_content(render* r, const node* n)
111103{
112− bound_str(n->inner, 0, doc_length(r->d));
104+ bound_str(n->inner, 0, r->d->body_len);
113105 bound_str(n->nodes, 0, r->d->nodes.used_front);
114106 bound_str(n->attrs, 0, r->d->attrs.used);
115107
@@ -188,7 +180,6 @@ attr* hexml_node_attributes(const document* d, const node* n, int* res)
188180 return &d->attrs.attrs[n->attrs.start];
189181}
190182
191−
192183attr* hexml_node_attribute(const document* d, const node* n, const char* s, int slen)
193184{
194185 if (slen == -1) slen = (int) strlen(s);
@@ -220,33 +211,95 @@ node* hexml_node_child(const document* d, const node* parent, const node* prev,
220211/////////////////////////////////////////////////////////////////////
221212// PARSING CODE
222213
223−static inline void node_alloc(node_buffer* b, int ask)
214+static inline node* node_alloc(node_buffer* b)
224215{
225216 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));
229− assert(buf2);
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));
232− free(b->alloc);
233− b->size = size2;
234− b->nodes = buf2;
235− b->alloc = buf2;
217+ if (space < 1)
218+ {
219+ int size2 = (b->size + 1000) * 2;
220+ node* buf2 = malloc(size2 * sizeof(node));
221+ assert(buf2);
222+ memcpy(buf2, b->nodes, b->used_front * sizeof(node));
223+ memcpy(&buf2[size2 - b->used_back], &b->nodes[b->size - b->used_back], b->used_back * sizeof(node));
224+ free(b->alloc);
225+ b->size = size2;
226+ b->nodes = buf2;
227+ b->alloc = buf2;
228+ }
229+ b->used_back++;
230+ return &b->nodes[b->size - b->used_back];
231+}
232+
233+// buffer[from .. from+n] = reverse (buffer[from .. from+n])
234+// The ranges completely overlap
235+static inline void reverse(node* buffer, int from, int n)
236+{
237+ for (int to = from + n - 1; to > from; from++, to--)
238+ {
239+ node temp = buffer[to];
240+ buffer[to] = buffer[from];
241+ buffer[from] = temp;
242+ }
243+}
244+
245+// buffer[to .. to+n] = reverse (buffer[from .. from+n])
246+// The ranges may not overlap
247+static inline void memcpy_reverse(node* buffer, int from, int to, int n)
248+{
249+ int end = from + n - 1;
250+ for (int i = 0; i < n; i++)
251+ buffer[to+i] = buffer[end-i];
252+}
253+
254+// buffer[to .. to+n] = reverse (buffer[from .. from+n])
255+// The ranges may overlap
256+static inline void memmove_reverse(node* buffer, int from, int to, int n)
257+{
258+ assert(from >= to); // always the case in my usages
259+ if (to + n < from)
260+ memcpy_reverse(buffer, from, to, n);
261+ else
262+ {
263+ int overlap = from - to;
264+ memcpy_reverse(buffer, from, to + n - overlap, overlap);
265+ reverse(buffer, to, n - overlap);
266+ }
267+}
268+
269+static inline str node_commit(node_buffer* b)
270+{
271+ // first search for an "open" node (nodes.length == -1)
272+ int open;
273+ for (open = b->size - b->used_back; open < b->size; open++)
274+ {
275+ if (b->nodes[open].nodes.length == -1)
276+ break;
277+ }
278+
279+ int n = open - (b->size - b->used_back);
280+ memmove_reverse(b->nodes, b->size - b->used_back, b->used_front, n);
281+ b->used_back -= n;
282+ b->used_front += n;
283+ return start_length(b->used_front - n, n);
236284}
237285
238−static inline void attr_alloc(attr_buffer* b, int ask)
286+static inline attr* attr_alloc(attr_buffer* b)
239287{
288+ // Ensure there is at least one node left
240289 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));
244− assert(buf2);
245− memcpy(buf2, b->attrs, b->used * sizeof(attr));
246− free(b->alloc);
247− b->size = size2;
248− b->attrs = buf2;
249− b->alloc = buf2;
290+ if (space < 1)
291+ {
292+ int size2 = (b->size + 1000) * 2;
293+ attr* buf2 = malloc(size2 * sizeof(attr));
294+ assert(buf2);
295+ memcpy(buf2, b->attrs, b->used * sizeof(attr));
296+ free(b->alloc);
297+ b->size = size2;
298+ b->attrs = buf2;
299+ b->alloc = buf2;
300+ }
301+ b->used++;
302+ return &b->attrs[b->used - 1];
250303}
251304
252305static void set_error(document* d, const char* msg)
@@ -257,35 +310,58 @@ static void set_error(document* d, const char* msg)
257310 strcpy(d->error_message, msg);
258311}
259312
260−#define ARGUMENTS , document* d
261−#define VARIABLES const char *tag_start, *name_start, *name_end, *quote_start, *quote_end
262−
263313static inline str gap(const char* ref, const char* start, const char* end)
264314{
265315 return start_end(start - ref, end - ref);
266316}
267317
268−#define abort(x) return x
269−#define NameStart name_start = p
270−#define NameEnd name_end = p
271−#define QuoteStart quote_start = p
272−#define QuoteEnd \
273− quote_end = p; \
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); \
277− d->attrs.used++;
278−#define AttribsStart printf("AttribsStart %s\n", p)
279−#define AttribsEnd printf("AttribsEnd %s\n", p)
280−#define Tag tag_start = p
281−#define TagComment printf("TagComment %s\n", p)
282−#define TagOpen printf("TagOpen %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); \
286− printf("TagOpenClose %s\n", p)
287−
288−#include "test.h"
318+#define P_Abort(x) return x
319+#define P_Tag tag_start = p;
320+#define P_NameStart name_start = p
321+#define P_NameEnd name_end = p
322+#define P_AttribsStart \
323+ me = node_alloc(&d->nodes); \
324+ me->attrs.start = d->attrs.used; \
325+ me->name = gap(d->body, name_start, name_end);
326+#define P_AttribsEnd \
327+ me->attrs = start_end(me->attrs.start, d->attrs.used);
328+#define P_QuoteStart quote_start = p
329+#define P_QuoteEnd \
330+ attr* attr = attr_alloc(&d->attrs); \
331+ attr->name = gap(d->body, name_start, name_end); \
332+ attr->value = gap(d->body, quote_start, p);
333+#define P_TagComment \
334+ // No action required - comments just flow as normal text
335+#define P_TagOpen \
336+ me->nodes.length = -1; \
337+ me->inner = gap(d->body, p, p); \
338+ me->outer = gap(d->body, tag_start, tag_start);
339+#define P_TagClose \
340+ str nodes = node_commit(&d->nodes); \
341+ if (d->nodes.used_back == 0) \
342+ return "Closing tag with no openning"; \
343+ me = &d->nodes.nodes[d->nodes.size - d->nodes.used_back]; \
344+ me->nodes = nodes; \
345+ if (me->name.length != gap(d->body, name_start, name_end).length || \
346+ memcmp(&d->body[me->name.start], name_start, me->name.length) != 0) \
347+ return "Mismatch in closing tag"; \
348+ me->inner = gap(d->body, &d->body[me->inner.start], tag_start); \
349+ me->outer = gap(d->body, &d->body[me->outer.start], p);
350+#define P_TagOpenClose \
351+ me->nodes = start_end(0, 0); \
352+ me->inner = gap(d->body, p, p); \
353+ me->outer = gap(d->body, tag_start, p);
354+
355+// Given the parsed string, return either NULL (success) or an error message (failure)
356+static const char* parser(const char* p, document* d)
357+{
358+ const char *tag_start; // Where the tag starts [<foo
359+ const char *name_start, *name_end; // Where a name is <[foo] or <foo [bar]=
360+ const char *quote_start; // Where an attribute quote is <foo bar='[123'>
361+ node* me; // The current node I'm working on
362+# include "test.h"
363+ return NULL;
364+}
289365
290366// Based on looking at ~50Kb XML documents, they seem to have ~700 attributes
291367// and ~300 nodes, so size appropriately to cope with that.
@@ -300,14 +376,12 @@ document* hexml_document_parse(const char* s, int slen)
300376{
301377 if (slen == -1) slen = (int) strlen(s);
302378 assert(s[slen] == 0);
303− // init_parse_table();
304379
305380 buffer* buf = malloc(sizeof(buffer));
306381 assert(buf);
307382 document* d = &buf->document;
308383 d->body = s;
309− d->cursor = s;
310− d->end = &s[slen];
384+ d->body_len = slen;
311385 d->error_message = NULL;
312386 d->attrs.size = 1000;
313387 d->attrs.used = 0;
@@ -328,20 +402,70 @@ document* hexml_document_parse(const char* s, int slen)
328402 // because there is no guaranteed ordering between LHS and RHS evaluation
329403 const char* res = parser(s, d);
330404 if (res != NULL)
331− d->error_message = strdup(res);
332− // d->nodes.nodes[0].nodes = content;
405+ set_error(d, res);
406+ else
407+ {
408+ d->nodes.nodes[0].nodes = node_commit(&d->nodes);
409+ if (d->nodes.used_back != 0)
410+ d->error_message = strdup("used_back failure");
411+ }
333412 return d;
334413}
335414
336−void main()
415+/////////////////////////////////////////////////////////////////////
416+// TEST HARNESS (temporary)
417+
418+#ifdef TEST
419+
420+static char* resolve(const document* d, str loc)
337421{
338− const document* d = hexml_document_parse("<test foo='123 xyz'/>", -1);
339− char buf[1000];
340− int len = hexml_node_render(d, hexml_document_node(d), buf, sizeof(buf));
341− buf[len] = 0;
342− printf("Result = %s\n", buf);
422+ char* buf = malloc(loc.length + 3);
423+ memcpy(&buf[1], &d->body[loc.start], loc.length);
424+ buf[0] = '\"';
425+ buf[loc.length+1] = '\"';
426+ buf[loc.length+2] = 0;
427+ return buf;
343428}
344429
430+static char* disp(str loc)
431+{
432+ char* buf = malloc(100);
433+ if (loc.length == 0)
434+ strcpy(buf, "[]");
435+ else
436+ sprintf(buf, "[%i..%i]", loc.start, end(loc)-1);
437+ return buf;
438+}
439+
440+int main()
441+{
442+ setbuf(stdout, NULL);
443+ const char* example = "<test foo='123 xyz' bar='t'><inner /> value</test>";
444+ document* d = hexml_document_parse(example, -1);
445+ if (d->error_message != NULL)
446+ printf("Parse failure: %s\n", d->error_message);
447+ else
448+ {
449+ char buf[1000];
450+ int len = hexml_node_render(d, hexml_document_node(d), buf, sizeof(buf));
451+ buf[len] = 0;
452+ printf("Result = %s\n", buf);
453+ printf("Used nodes = %i, attributes = %i\n", d->nodes.used_front, d->attrs.used);
454+
455+ for (int i = 0; i < d->attrs.used; i++)
456+ printf("Attr %i: %s=%s\n", i, resolve(d, d->attrs.attrs[i].name), resolve(d, d->attrs.attrs[i].value));
457+ for (int i = 0; i < d->nodes.used_front; i++)
458+ {
459+ printf("Node %i: %s attrs=%s, nodes=%s\n", i, resolve(d, d->nodes.nodes[i].name), disp(d->nodes.nodes[i].attrs), disp(d->nodes.nodes[i].nodes));
460+ printf(" inner %s\n outer %s\n", resolve(d, d->nodes.nodes[i].inner), resolve(d, d->nodes.nodes[i].outer));
461+ }
462+ }
463+ hexml_document_free(d);
464+ return 0;
465+}
466+
467+#endif
468+
345469/*
346470void main()
347471{