@@ -41,7 +41,8 @@ typedef struct
43 int used_front; // front entries, stored for good
43 int used_front; // front entries, stored for good
44− 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
45 node* nodes; // dynamically allocated buffer
46 node* nodes; // dynamically allocated buffer
46 node* alloc; // what to call free on
47 node* alloc; // what to call free on
47} node_buffer;
48} node_buffer;
@@ -63,10 +64,11 @@ struct document
64typedef struct
65typedef struct
66− const document* d;
67+ const document* d; // Document, used to get at the body
67− char* buffer;
68+ char* buffer; // Buffer we are writing too
68− int length;
69+ int length; // Length of buffer, do not write past the end.
69− int cursor;
70+ // No character is reserved for a NULL terminator.
71+ int cursor; // Cursor of where we are writing now
72static inline void render_char(render* r, char c)
74static inline void render_char(render* r, char c)
@@ -178,7 +180,6 @@ attr* hexml_node_attributes(const document* d, const node* n, int* res)
178 return &d->attrs.attrs[n->attrs.start];
180 return &d->attrs.attrs[n->attrs.start];
182attr* hexml_node_attribute(const document* d, const node* n, const char* s, int slen)
183attr* hexml_node_attribute(const document* d, const node* n, const char* s, int slen)
184 if (slen == -1) slen = (int) strlen(s);
185 if (slen == -1) slen = (int) strlen(s);
@@ -210,33 +211,95 @@ node* hexml_node_child(const document* d, const node* parent, const node* prev,
210/////////////////////////////////////////////////////////////////////
211/////////////////////////////////////////////////////////////////////
211// PARSING CODE
212// PARSING CODE
213−static inline void node_alloc(node_buffer* b, int ask)
214+static inline node* node_alloc(node_buffer* b)
215 int space = b->size - b->used_back - b->used_front;
216 int space = b->size - b->used_back - b->used_front;
216− if (space >= ask) return;
217+ if (space < 1)
217− int size2 = (b->size + 1000 + ask) * 2;
219+ int size2 = (b->size + 1000) * 2;
218− node* buf2 = malloc(size2 * sizeof(node));
220+ node* buf2 = malloc(size2 * sizeof(node));
219− assert(buf2);
221+ assert(buf2);
220− memcpy(buf2, b->nodes, b->used_front * sizeof(node));
222+ memcpy(buf2, b->nodes, b->used_front * sizeof(node));
221− memcpy(&buf2[size2 - b->used_back], &b->nodes[b->size - b->used_back], b->used_back * sizeof(node));
223+ memcpy(&buf2[size2 - b->used_back], &b->nodes[b->size - b->used_back], b->used_back * sizeof(node));
222− free(b->alloc);
224+ free(b->alloc);
223− b->size = size2;
225+ b->size = size2;
224− b->nodes = buf2;
226+ b->nodes = buf2;
225− b->alloc = buf2;
227+ b->alloc = buf2;
230+ return &b->nodes[b->size - b->used_back];
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)
237+ for (int to = from + n - 1; to > from; from++, to--)
239+ node temp = buffer[to];
240+ buffer[to] = buffer[from];
241+ buffer[from] = temp;
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)
249+ int end = from + n - 1;
250+ for (int i = 0; i < n; i++)
251+ buffer[to+i] = buffer[end-i];
228−static inline void attr_alloc(attr_buffer* b, int ask)
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)
258+ assert(from >= to); // always the case in my usages
260+ memcpy_reverse(buffer, from, to, n);
263+ int overlap = from - to;
264+ memcpy_reverse(buffer, from, to + n - overlap, overlap);
265+ reverse(buffer, to, n - overlap);
269+static inline str node_commit(node_buffer* b)
271+ // first search for an "open" node (nodes.length == -1)
273+ for (open = b->size - b->used_back; open < b->size; open++)
275+ if (b->nodes[open].nodes.length == -1)
279+ int n = open - (b->size - b->used_back);
280+ memmove_reverse(b->nodes, b->size - b->used_back, b->used_front, n);
283+ return start_length(b->used_front - n, n);
286+static inline attr* attr_alloc(attr_buffer* b)
288+ // Ensure there is at least one node left
230 int space = b->size - b->used;
289 int space = b->size - b->used;
231− if (space >= ask) return;
290+ if (space < 1)
232− int size2 = (b->size + 1000 + ask) * 2;
292+ int size2 = (b->size + 1000) * 2;
233− attr* buf2 = malloc(size2 * sizeof(attr));
293+ attr* buf2 = malloc(size2 * sizeof(attr));
234− assert(buf2);
294+ assert(buf2);
235− memcpy(buf2, b->attrs, b->used * sizeof(attr));
295+ memcpy(buf2, b->attrs, b->used * sizeof(attr));
236− free(b->alloc);
296+ free(b->alloc);
237− b->size = size2;
297+ b->size = size2;
238− b->attrs = buf2;
298+ b->attrs = buf2;
239− b->alloc = buf2;
299+ b->alloc = buf2;
302+ return &b->attrs[b->used - 1];
242static void set_error(document* d, const char* msg)
305static void set_error(document* d, const char* msg)
@@ -253,28 +316,49 @@ static inline str gap(const char* ref, const char* start, const char* end)
255#define P_Abort(x) return x
318#define P_Abort(x) return x
319+#define P_Tag tag_start = p;
256#define P_NameStart name_start = p
320#define P_NameStart name_start = p
257#define P_NameEnd name_end = 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);
258#define P_QuoteStart quote_start = p
328#define P_QuoteStart quote_start = p
259#define P_QuoteEnd \
329#define P_QuoteEnd \
261− attr_alloc(&d->attrs, 1); \
330+ attr* attr = attr_alloc(&d->attrs); \
262− d->attrs.attrs[d->attrs.used].name = gap(d->body, name_start, name_end); \
331+ attr->name = gap(d->body, name_start, name_end); \
263− d->attrs.attrs[d->attrs.used].value = gap(d->body, quote_start, quote_end); \
332+ attr->value = gap(d->body, quote_start, p);
265−#define P_AttribsStart printf("AttribsStart %s\n", p)
266−#define P_AttribsEnd printf("AttribsEnd %s\n", p)
267−#define P_Tag tag_start = p
268−#define P_TagComment printf("TagComment %s\n", p)
333+#define P_TagComment \
334+ // No action required - comments just flow as normal text
269−#define P_TagOpen printf("TagOpen %s\n", p)
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);
270−#define P_TagClose printf("TagClose %s\n", p)
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);
271#define P_TagOpenClose \
350#define P_TagOpenClose \
351+ me->nodes = start_end(0, 0); \
352+ me->inner = gap(d->body, p, p); \
272− d->nodes.nodes[d->nodes.used_front].outer = gap(d->body, tag_start, p); \
353+ me->outer = gap(d->body, tag_start, p);
273− printf("TagOpenClose %s\n", p)
355+// Given the parsed string, return either NULL (success) or an error message (failure)
275static const char* parser(const char* p, document* d)
356static const char* parser(const char* p, document* d)
277− const char *tag_start, *name_start, *name_end, *quote_start, *quote_end;
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
278# include "test.h"
362# include "test.h"
279 return NULL;
363 return NULL;
@@ -292,7 +376,6 @@ document* hexml_document_parse(const char* s, int slen)
293 if (slen == -1) slen = (int) strlen(s);
377 if (slen == -1) slen = (int) strlen(s);
294 assert(s[slen] == 0);
378 assert(s[slen] == 0);
295− // init_parse_table();
297 buffer* buf = malloc(sizeof(buffer));
380 buffer* buf = malloc(sizeof(buffer));
298 assert(buf);
381 assert(buf);
@@ -319,21 +402,70 @@ document* hexml_document_parse(const char* s, int slen)
319 // because there is no guaranteed ordering between LHS and RHS evaluation
402 // because there is no guaranteed ordering between LHS and RHS evaluation
320 const char* res = parser(s, d);
403 const char* res = parser(s, d);
321 if (res != NULL)
404 if (res != NULL)
322− d->error_message = strdup(res);
405+ set_error(d, res);
323− // d->nodes.nodes[0].nodes = content;
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");
324 return d;
412 return d;
415+/////////////////////////////////////////////////////////////////////
416+// TEST HARNESS (temporary)
420+static char* resolve(const document* d, str loc)
422+ char* buf = malloc(loc.length + 3);
423+ memcpy(&buf[1], &d->body[loc.start], loc.length);
425+ buf[loc.length+1] = '\"';
426+ buf[loc.length+2] = 0;
430+static char* disp(str loc)
432+ char* buf = malloc(100);
433+ if (loc.length == 0)
436+ sprintf(buf, "[%i..%i]", loc.start, end(loc)-1);
327int main()
440int main()
442+ setbuf(stdout, NULL);
329− const document* d = hexml_document_parse("<test foo='123 xyz'><inner /> value</test>", -1);
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);
330− char buf[1000];
449+ char buf[1000];
331− int len = hexml_node_render(d, hexml_document_node(d), buf, sizeof(buf));
450+ int len = hexml_node_render(d, hexml_document_node(d), buf, sizeof(buf));
332− buf[len] = 0;
451+ buf[len] = 0;
333− printf("Result = %s\n", buf);
452+ printf("Result = %s\n", buf);
453+ printf("Used nodes = %i, attributes = %i\n", d->nodes.used_front, d->attrs.used);
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++)
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));
463+ hexml_document_free(d);
334− return 0;
464+ return 0;
338void main()
470void main()