@@ -1,5 +1,5 @@
11#include "hexml.h"
2−#include <stdlib.h>
2+#include <malloc.h>
33#include <string.h>
44#include <assert.h>
55#include <ctype.h>
@@ -10,18 +10,18 @@ typedef int bool;
1010/////////////////////////////////////////////////////////////////////
1111// TYPES
1212
13−static inline int end(str s) { return s.start + s.length; }
13+int static inline end(str s) { return s.start + s.length; }
1414
15−static inline str start_length(int32_t start, int32_t length)
15+str static inline start_length(int32_t start, int32_t length)
1616{
17− assert(start >= 0 && length >= 0);
17+ if (start < 0 || length < 0) assert(0);
1818 str res;
1919 res.start = start;
2020 res.length = length;
2121 return res;
2222}
2323
24−static inline str start_end(int32_t start, int32_t end)
24+str static inline start_end(int32_t start, int32_t end)
2525{
2626 return start_length(start, end - start);
2727}
@@ -40,7 +40,7 @@ typedef struct
4040 // have a cursor at the end, which is stack scoped, so children write out, then I do
4141 // when you commit, you copy over from end to front
4242
43− // nodes
43+ // nodes
4444 int size;
4545 int used_front; // front entries, stored for good
4646 int used_back; // back entries, stack based, copied into front
@@ -51,11 +51,11 @@ typedef struct
5151
5252struct document
5353{
54− const char* body; // pointer to initial argument, not owned by us
54+ char* body; // pointer to initial argument, not owned by us
5555
5656 // things only used while parsing
57− const char* cursor; // pointer to where we are in body
58− const char* end; // pointer to one past the last char
57+ char* cursor; // pointer to where we are in body
58+ char* end; // pointer to one past the last char
5959 // if cursor is > end we have gone past the end
6060
6161 char* error_message;
@@ -63,8 +63,8 @@ struct document
6363 attr_buffer attrs;
6464};
6565
66−static inline int doc_length(const document* d) { return (int) (d->end - d->body); }
67−static inline int doc_position(const document* d) { return (int) (d->cursor - d->body); }
66+int static inline doc_length(document* d) { return d->end - d->body; }
67+int static inline doc_position(document* d) { return d->cursor - d->body; }
6868
6969
7070/////////////////////////////////////////////////////////////////////
@@ -72,45 +72,49 @@ static inline int doc_position(const document* d) { return (int) (d->cursor - d-
7272
7373typedef struct
7474{
75− const document* d;
75+ document* d;
7676 char* buffer;
7777 int length;
7878 int cursor;
7979} render;
8080
81−static inline void render_char(render* r, char c)
81+void static inline render_char(render* r, char c)
8282{
8383 if (r->cursor < r->length)
8484 r->buffer[r->cursor] = c;
8585 r->cursor++;
8686}
8787
88−static inline void bound(int idx, int mn, int mx)
88+void static inline bound(char* msg, int index, int mn, int mx)
8989{
90− assert(idx >= mn && idx <= mx);
90+ if (index < mn || index > mx)
91+ {
92+ //printf("Bounds checking failed %s, got %i, which should be in %i:%i\n", msg, index, mn, mx);
93+ assert(0);
94+ }
9195}
9296
93−static inline void bound_str(str s, int mn, int mx)
97+void static inline bound_str(char* msg, str s, int mn, int mx)
9498{
95− assert(s.length >= 0);
96− bound(s.start, mn, mx);
97− bound(end(s), mn, mx);
99+ if (s.length < 0) assert(0);
100+ bound(msg, s.start, mn, mx);
101+ bound(msg, end(s), mn, mx);
98102}
99103
100−static void render_str(render* r, str s)
104+void render_str(render* r, str s)
101105{
102− bound_str(s, 0, doc_length(r->d));
106+ bound_str("render_str", s, 0, doc_length(r->d));
103107 for (int i = 0; i < s.length; i++)
104108 render_char(r, r->d->body[s.start + i]);
105109}
106110
107−static void render_tag(render* r, const node* n);
111+void render_tag(render* r, node* n);
108112
109−static void render_content(render* r, const node* n)
113+void render_content(render* r, node* n)
110114{
111− bound_str(n->inner, 0, doc_length(r->d));
112− bound_str(n->nodes, 0, r->d->nodes.used_front);
113− bound_str(n->attrs, 0, r->d->attrs.used);
115+ bound_str("render_conent inner", n->inner, 0, doc_length(r->d));
116+ bound_str("render_conent nodes", n->nodes, 0, r->d->nodes.used_front);
117+ bound_str("render_conent attrs", n->attrs, 0, r->d->attrs.used);
114118
115119 int done = n->inner.start;
116120 for (int i = 0; i < n->nodes.length; i++)
@@ -123,7 +127,7 @@ static void render_content(render* r, const node* n)
123127 render_str(r, start_end(done, end(n->inner)));
124128}
125129
126−static void render_tag(render* r, const node* n)
130+void render_tag(render* r, node* n)
127131{
128132 render_char(r, '<');
129133 render_str(r, n->name);
@@ -145,18 +149,14 @@ static void render_tag(render* r, const node* n)
145149 render_char(r, '>');
146150}
147151
148−int hexml_node_render(const document* d, const node* n, char* buffer, int length)
152+int document_render(document* d, char* buffer, int length)
149153{
150154 render r;
151155 r.d = d;
152156 r.buffer = buffer;
153157 r.length = length;
154158 r.cursor = 0;
155− // The root node (and only the root node) has an empty length, so just render its innards
156− if (n->name.length == 0)
157− render_content(&r, n);
158− else
159− render_tag(&r, n);
159+ render_content(&r, document_node(d));
160160 return r.cursor;
161161}
162162
@@ -164,10 +164,10 @@ int hexml_node_render(const document* d, const node* n, char* buffer, int length
164164/////////////////////////////////////////////////////////////////////
165165// NOT THE PARSER
166166
167−char* hexml_document_error(const document* d){return d->error_message;}
168−node* hexml_document_node(const document* d){return &d->nodes.nodes[0];}
167+char* document_error(document* d){return d->error_message;}
168+node* document_node(document* d){return &d->nodes.nodes[0];}
169169
170−void hexml_document_free(document* d)
170+void document_free(document* d)
171171{
172172 free(d->error_message);
173173 free(d->nodes.alloc);
@@ -175,94 +175,93 @@ void hexml_document_free(document* d)
175175 free(d);
176176}
177177
178−node* hexml_node_children(const document* d, const node* n, int* res)
178+node* node_children(document* d, node* n, int* res)
179179{
180180 *res = n->nodes.length;
181181 return &d->nodes.nodes[n->nodes.start];
182182}
183183
184−attr* hexml_node_attributes(const document* d, const node* n, int* res)
184+attr* node_attributes(document* d, node* n, int* res)
185185{
186186 *res = n->attrs.length;
187187 return &d->attrs.attrs[n->attrs.start];
188188}
189189
190190
191−attr* hexml_node_attribute(const document* d, const node* n, const char* s, int slen)
191+attr* node_attributeBy(document* d, node* n, char* s, int slen)
192192{
193− if (slen == -1) slen = (int) strlen(s);
194− const int limit = end(n->attrs);
195− for (int i = n->attrs.start; i < limit; i++)
196− {
197− attr* r = &d->attrs.attrs[i];
198− if (r->name.length == slen && memcmp(s, &d->body[r->name.start], slen) == 0)
199− return r;
200− }
193+ assert(0);
201194 return NULL;
202195}
203196
204197// Search for given strings within a node
205−node* hexml_node_child(const document* d, const node* parent, const node* prev, const char* s, int slen)
198+node* node_firstChildBy(document* d, node* n, char* s, int slen)
206199{
207− if (slen == -1) slen = (int) strlen(s);
208− int i = prev == NULL ? parent->nodes.start : (int) (prev + 1 - d->nodes.nodes);
209− const int limit = end(parent->nodes);
200+ if (slen == -1) slen = strlen(s);
201+ int i = n->nodes.start;
202+ int limit = i + n->nodes.length;
210203 for (; i < limit; i++)
211204 {
212− node* r = &d->nodes.nodes[i];
213− if (r->name.length == slen && memcmp(s, &d->body[r->name.start], slen) == 0)
214− return r;
205+ if (d->nodes.nodes[i].name.length == slen &&
206+ memcmp(s, &d->body[d->nodes.nodes[i].name.start], slen) == 0)
207+ return &d->nodes.nodes[i];
215208 }
216209 return NULL;
217210}
218211
212+node* node_nextChildBy(document* d, node* parent, node* prev, char* s, int slen)
213+{
214+ assert(0);
215+ return NULL;
216+}
217+
219218
220219/////////////////////////////////////////////////////////////////////
221220// PARSE TABLE
222221
223−static const char tag_name1 = 0x1;
224−static const char tag_name = 0x2;
225−static const char tag_space = 0x4;
222+const char tag_name1 = 0x1;
223+const char tag_name = 0x2;
224+const char tag_space = 0x4;
226225
227−static char parse_table[256];
226+char parse_table[256];
228227
229−static void init_parse_table()
228+void init_parse_table()
230229{
231230 static bool done = 0;
232231 if (done) return;
233232 for (int i = 0; i < 256; i++)
234233 {
235234 bool name1 = i == ':' || i == '_' || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z');
236− bool name = name1 || i == '-' || i == '.' || (i >= '0' && i <= '9');
235+ bool name = name1 || i == '-' || (i >= '0' && i <= '9');
237236 bool space = i == ' ' || i == '\t' || i == '\r' || i == '\n';
238237 parse_table[i] = (name1 ? tag_name1 : 0) | (name ? tag_name : 0) | (space ? tag_space : 0);
239238 }
240239 done = 1;
241240}
242241
243−static inline bool is(char c, char tag) { return parse_table[(unsigned char) c] & tag; }
244−static inline bool is_name1(char c) { return is(c, tag_name1); }
245−static inline bool is_name(char c) { return is(c, tag_name); }
246−static inline bool is_space(char c) { return is(c, tag_space); }
242+bool static inline is(char c, char tag) { return parse_table[c] & tag; }
243+bool static inline is_name1(char c) { return is(c, tag_name1); }
244+bool static inline is_name(char c) { return is(c, tag_name); }
245+bool static inline is_space(char c) { return is(c, tag_space); }
247246
248247
249248/////////////////////////////////////////////////////////////////////
250249// PARSER COMBINATORS
251250
252−static inline char peek_at(const document* d, int i) { return d->cursor[i]; }
253−static inline char peek(const document* d) { return peek_at(d, 0); }
254−static inline void skip(document* d, int i) { d->cursor += i; }
255−static inline char get(document* d) { char c = peek(d); skip(d, 1); return c; }
251+char static inline peekAt(document* d, int i) { return d->cursor[i]; }
252+void static inline skip(document* d, int i) { d->cursor += i; }
253+char static inline peek(document* d) { return peekAt(d, 0); }
254+char static inline get(document* d) { char c = peek(d); skip(d, 1); return c; }
256255
257256// Remove whitespace characters from the cursor while they are still whitespace
258−static inline void trim(document* d)
257+void static inline trim(document* d)
259258{
260259 while (is_space(peek(d)))
261260 skip(d, 1);
262261}
263262
264263// Find this character form the cursor onwards, if true adjust the cursor to that char, otherwise leave it at the end
265−static bool find(document* d, char c)
264+bool find(document* d, char c)
266265{
267266 char* x = memchr(d->cursor, c, d->end - d->cursor);
268267 if (x == NULL)
@@ -281,47 +280,37 @@ static bool find(document* d, char c)
281280/////////////////////////////////////////////////////////////////////
282281// PARSING CODE
283282
284−static inline void node_alloc(node_buffer* b, int ask)
283+void static inline node_alloc(node_buffer* b, int ask)
285284{
286285 int space = b->size - b->used_back - b->used_front;
287286 if (space >= ask) return;
288− int size2 = (b->size + 1000 + ask) * 2;
289− node* buf2 = malloc(size2 * sizeof(node));
290− assert(buf2);
291− memcpy(buf2, b->nodes, b->used_front * sizeof(node));
292− memcpy(&buf2[size2 - b->used_back], &b->nodes[b->size - b->used_back], b->used_back * sizeof(node));
293− free(b->alloc);
294− b->size = size2;
295− b->nodes = buf2;
296− b->alloc = buf2;
287+ assert(0);
297288}
298289
299−static inline void attr_alloc(attr_buffer* b, int ask)
290+void static inline attr_alloc(attr_buffer* b, int ask)
300291{
301292 int space = b->size - b->used;
302293 if (space >= ask) return;
303294 int size2 = (b->size + 1000 + ask) * 2;
304295 attr* buf2 = malloc(size2 * sizeof(attr));
305− assert(buf2);
306296 memcpy(buf2, b->attrs, b->used * sizeof(attr));
307297 free(b->alloc);
308− b->size = size2;
309298 b->attrs = buf2;
310299 b->alloc = buf2;
311300}
312301
313−static void set_error(document* d, const char* msg)
302+void set_error(document* d, char* msg)
314303{
315304 if (d->error_message != NULL) return; // keep the first error message
316305 d->error_message = malloc(strlen(msg)+1);
317− assert(d->error_message);
318306 strcpy(d->error_message, msg);
319307}
320308
321309// you now expect a name, perhaps preceeded by whitespace
322310// the name may be empty
323−static inline str parse_name(document* d)
311+str static inline parse_name(document* d)
324312{
313+ trim(d);
325314 int start = doc_position(d);
326315 if (!is_name1(peek(d)))
327316 return start_length(start, 0);
@@ -331,14 +320,10 @@ static inline str parse_name(document* d)
331320 return start_end(start, doc_position(d));
332321}
333322
334−static inline str parse_attrval(document* d)
323+str static inline parse_attrval(document* d)
335324{
336325 trim(d);
337− if (peek(d) != '=')
338− {
339− set_error(d, "Expected = in attribute, but missing");
340− return start_length(0, 0);
341− }
326+ if (peek(d) != '=') return start_length(0,0);
342327 skip(d, 1);
343328 trim(d);
344329 char c = peek(d);
@@ -355,22 +340,19 @@ static inline str parse_attrval(document* d)
355340 return start_end(start, doc_position(d) - 1);
356341 }
357342 else
358− {
359− set_error(d, "Invalid attribute");
360− return start_length(0, 0);
361− }
343+ return parse_name(d);
362344}
363345
364346
365347
366348// seen a tag name, now looking for attributes terminated by >
367349// puts the attributes it finds in the attribute buffer
368−static inline str parse_attributes(document* d)
350+str static inline parse_attributes(document* d)
369351{
370− int start = d->attrs.used;
371− while (d->error_message == NULL)
352+ str res;
353+ res.start = d->attrs.used;
354+ for (int i = 0; ; i++)
372355 {
373− trim(d);
374356 str name = parse_name(d);
375357 if (name.length == 0) break;
376358 attr_alloc(&d->attrs, 1);
@@ -378,39 +360,34 @@ static inline str parse_attributes(document* d)
378360 d->attrs.attrs[d->attrs.used].value = parse_attrval(d);
379361 d->attrs.used++;
380362 }
381− return start_end(start, d->attrs.used);
363+ res.length = d->attrs.used - res.start;
364+ return res;
382365}
383366
384−static str parse_content(document* d);
367+str parse_content(document* d);
385368
386369
387370// Add a new entry into tag, am at a '<'
388−static inline void parse_tag(document* d)
371+void static inline parse_tag(document* d)
389372{
390373 node_alloc(&d->nodes, 1);
391374 d->nodes.used_back++;
392− node* me = &d->nodes.nodes[d->nodes.size - d->nodes.used_back];
375+ int me = d->nodes.size - d->nodes.used_back;
393376
394− me->outer.start = doc_position(d);
377+ d->nodes.nodes[me].outer.start = doc_position(d);
395378 char c = get(d);
396379 assert(c == '<');
397380 if (peek(d) == '?') skip(d, 1);
398− me->name = parse_name(d);
399− if (me->name.length == 0)
400− {
401− set_error(d, "Missing tag name");
402− return;
403− }
404− me->attrs = parse_attributes(d);
405− if (d->error_message != NULL) return;
381+ d->nodes.nodes[me].name = parse_name(d);
382+ d->nodes.nodes[me].attrs = parse_attributes(d);
406383
407− c = peek(d);
408− if ((c == '/' || c == '?') && peek_at(d, 1) == '>')
384+ c = get(d);
385+ if ((c == '/' || c == '?') && peek(d) == '>')
409386 {
410− skip(d, 2);
411− me->nodes = start_length(0, 0);
412− me->outer.length = start_end(me->outer.start, doc_position(d)).length;
413− me->inner = start_length(doc_position(d), 0);
387+ skip(d, 1);
388+ d->nodes.nodes[me].nodes = start_length(0, 0);
389+ d->nodes.nodes[me].outer.length = start_end(d->nodes.nodes[me].outer.start, doc_position(d)).length;
390+ d->nodes.nodes[me].inner = start_length(doc_position(d), 0);
414391 return;
415392 }
416393 else if (c != '>')
@@ -418,28 +395,23 @@ static inline void parse_tag(document* d)
418395 set_error(d, "Gunk at the end of the tag");
419396 return;
420397 }
421− else
422− skip(d, 1);
423− me->inner.start = doc_position(d);
424− str content = parse_content(d);
425−
426− // parse_content may have allocated more nodes, so recompute me
427− me = &d->nodes.nodes[d->nodes.size - d->nodes.used_back];
428− me->nodes = content;
429− me->inner.length = start_end(me->inner.start, doc_position(d)).length;
398+ d->nodes.nodes[me].inner.start = doc_position(d);
399+ d->nodes.nodes[me].nodes = parse_content(d);
400+ d->nodes.nodes[me].inner.length = start_end(d->nodes.nodes[me].inner.start, doc_position(d)).length;
430401
431402 if (d->error_message != NULL) return;
432− if (peek(d) == '<' && peek_at(d, 1) == '/')
403+ if (peek(d) == '<' && peekAt(d, 1) == '/')
433404 {
434405 skip(d, 2);
435− if (d->end - d->cursor >= me->name.length &&
436− memcmp(d->cursor, &d->body[me->name.start], me->name.length) == 0)
406+ trim(d);
407+ if (d->end - d->cursor >= d->nodes.nodes[me].name.length &&
408+ memcmp(d->cursor, &d->body[d->nodes.nodes[me].name.start], d->nodes.nodes[me].name.length) == 0)
437409 {
438− skip(d, me->name.length);
410+ skip(d, d->nodes.nodes[me].name.length);
439411 trim(d);
440412 if (get(d) == '>')
441413 {
442− me->outer.length = start_end(me->outer.start, doc_position(d)).length;
414+ d->nodes.nodes[me].outer.length = start_end(d->nodes.nodes[me].outer.start, doc_position(d)).length;
443415 return;
444416 }
445417 }
@@ -451,7 +423,7 @@ static inline void parse_tag(document* d)
451423
452424// Parser until </, return the index of your node children
453425// Not inline as it is recursive
454−static str parse_content(document* d)
426+str parse_content(document* d)
455427{
456428 int before = d->nodes.used_back;
457429 while (d->error_message == NULL)
@@ -461,33 +433,11 @@ static str parse_content(document* d)
461433 {
462434 break;
463435 }
464− else if (peek_at(d, 1) == '/')
436+ if (peekAt(d, 1) == '/')
465437 {
466438 // have found a </
467439 break;
468440 }
469− else if (peek_at(d, 1) == '!' && peek_at(d, 2) == '-' && peek_at(d, 3) == '-')
470− {
471− skip(d, 3);
472− // you can't reuse the two '-' characters for the closing as well
473− if (peek_at(d, 0) == '\0' || peek_at(d, 1) == '\0')
474− {
475− set_error(d, "Didn't get a closing comment");
476− return start_end(0, 0);
477− }
478− skip(d, 2);
479− for (;;)
480− {
481− if (!find(d, '>'))
482− {
483− set_error(d, "Didn't get a closing comment");
484− return start_end(0, 0);
485− }
486− skip(d, 1);
487− if (peek_at(d, -3) == '-' && peek_at(d, -2) == '-')
488− break;
489− }
490− }
491441 else
492442 {
493443 parse_tag(d);
@@ -503,23 +453,20 @@ static str parse_content(document* d)
503453 return res;
504454}
505455
506−// Based on looking at ~50Kb XML documents, they seem to have ~700 attributes
507−// and ~300 nodes, so size appropriately to cope with that.
508456typedef struct
509457{
510458 document document;
511459 attr attrs[1000];
512− node nodes[500];
460+ node nodes[1000];
513461} buffer;
514462
515−document* hexml_document_parse(const char* s, int slen)
463+document* document_parse(char* s, int slen)
516464{
517− if (slen == -1) slen = (int) strlen(s);
465+ if (slen == -1) slen = strlen(s);
518466 assert(s[slen] == 0);
519467 init_parse_table();
520468
521469 buffer* buf = malloc(sizeof(buffer));
522− assert(buf);
523470 document* d = &buf->document;
524471 d->body = s;
525472 d->cursor = s;
@@ -529,7 +476,7 @@ document* hexml_document_parse(const char* s, int slen)
529476 d->attrs.used = 0;
530477 d->attrs.attrs = buf->attrs;
531478 d->attrs.alloc = NULL;
532− d->nodes.size = 500;
479+ d->nodes.size = 1000;
533480 d->nodes.used_back = 0;
534481 d->nodes.used_front = 1;
535482 d->nodes.nodes = buf->nodes;
@@ -539,13 +486,11 @@ document* hexml_document_parse(const char* s, int slen)
539486 d->nodes.nodes[0].outer = start_length(0, slen);
540487 d->nodes.nodes[0].inner = start_length(0, slen);
541488 d->nodes.nodes[0].attrs = start_length(0, 0);
489+ d->nodes.nodes[0].nodes = parse_content(d);
542490
543− // Introduce an intermediate result, otherwise behaviour is undefined
544− // because there is no guaranteed ordering between LHS and RHS evaluation
545− str content = parse_content(d);
546− d->nodes.nodes[0].nodes = content;
547−
548− if (d->cursor < d->end)
491+ if (d->cursor < d->end && d->error_message == NULL)
492+ {
549493 set_error(d, "Trailing junk at the end of the document");
494+ }
550495 return d;
551496}