@@ -1,5 +1,5 @@
1#include "hexml.h"
1#include "hexml.h"
2−#include <malloc.h>
2+#include <stdlib.h>
3#include <string.h>
3#include <string.h>
4#include <assert.h>
4#include <assert.h>
5#include <ctype.h>
5#include <ctype.h>
@@ -40,7 +40,7 @@ typedef struct
40 // have a cursor at the end, which is stack scoped, so children write out, then I do
40 // have a cursor at the end, which is stack scoped, so children write out, then I do
41 // when you commit, you copy over from end to front
41 // when you commit, you copy over from end to front
43− // nodes
43+ // nodes
45 int used_front; // front entries, stored for good
45 int used_front; // front entries, stored for good
46 int used_back; // back entries, stack based, copied into front
46 int used_back; // back entries, stack based, copied into front
@@ -85,32 +85,32 @@ static inline void render_char(render* r, char c)
85 r->cursor++;
85 r->cursor++;
88−static inline void bound(const char* msg, int index, int mn, int mx)
88+static inline void bound(int idx, int mn, int mx)
90− assert(index >= mn && index <= mx);
90+ assert(idx >= mn && idx <= mx);
93−static inline void bound_str(const char* msg, str s, int mn, int mx)
93+static inline void bound_str(str s, int mn, int mx)
95 assert(s.length >= 0);
95 assert(s.length >= 0);
96− bound(msg, s.start, mn, mx);
96+ bound(s.start, mn, mx);
97− bound(msg, end(s), mn, mx);
97+ bound(end(s), mn, mx);
100−void render_str(render* r, str s)
100+static void render_str(render* r, str s)
102− bound_str("render_str", s, 0, doc_length(r->d));
102+ bound_str(s, 0, doc_length(r->d));
103 for (int i = 0; i < s.length; i++)
103 for (int i = 0; i < s.length; i++)
104 render_char(r, r->d->body[s.start + i]);
104 render_char(r, r->d->body[s.start + i]);
107−void render_tag(render* r, const node* n);
107+static void render_tag(render* r, const node* n);
109−void render_content(render* r, const node* n)
109+static void render_content(render* r, const node* n)
111− bound_str("render_conent inner", n->inner, 0, doc_length(r->d));
111+ bound_str(n->inner, 0, doc_length(r->d));
112− bound_str("render_conent nodes", n->nodes, 0, r->d->nodes.used_front);
112+ bound_str(n->nodes, 0, r->d->nodes.used_front);
113− bound_str("render_conent attrs", n->attrs, 0, r->d->attrs.used);
113+ bound_str(n->attrs, 0, r->d->attrs.used);
115 int done = n->inner.start;
115 int done = n->inner.start;
116 for (int i = 0; i < n->nodes.length; i++)
116 for (int i = 0; i < n->nodes.length; i++)
@@ -123,7 +123,7 @@ void render_content(render* r, const node* n)
123 render_str(r, start_end(done, end(n->inner)));
123 render_str(r, start_end(done, end(n->inner)));
126−void render_tag(render* r, const node* n)
126+static void render_tag(render* r, const node* n)
128 render_char(r, '<');
128 render_char(r, '<');
129 render_str(r, n->name);
129 render_str(r, n->name);
@@ -145,7 +145,7 @@ void render_tag(render* r, const node* n)
145 render_char(r, '>');
145 render_char(r, '>');
148−int node_render(const document* d, const node* n, char* buffer, int length)
148+int hexml_node_render(const document* d, const node* n, char* buffer, int length)
150 render r;
150 render r;
@@ -164,10 +164,10 @@ int node_render(const document* d, const node* n, char* buffer, int length)
164/////////////////////////////////////////////////////////////////////
164/////////////////////////////////////////////////////////////////////
165// NOT THE PARSER
165// NOT THE PARSER
167−char* document_error(const document* d){return d->error_message;}
167+char* hexml_document_error(const document* d){return d->error_message;}
168−node* document_node(const document* d){return &d->nodes.nodes[0];}
168+node* hexml_document_node(const document* d){return &d->nodes.nodes[0];}
170−void document_free(document* d)
170+void hexml_document_free(document* d)
172 free(d->error_message);
172 free(d->error_message);
173 free(d->nodes.alloc);
173 free(d->nodes.alloc);
@@ -175,20 +175,20 @@ void document_free(document* d)
178−node* node_children(const document* d, const node* n, int* res)
178+node* hexml_node_children(const document* d, const node* n, int* res)
180 *res = n->nodes.length;
180 *res = n->nodes.length;
181 return &d->nodes.nodes[n->nodes.start];
181 return &d->nodes.nodes[n->nodes.start];
184−attr* node_attributes(const document* d, const node* n, int* res)
184+attr* hexml_node_attributes(const document* d, const node* n, int* res)
186 *res = n->attrs.length;
186 *res = n->attrs.length;
187 return &d->attrs.attrs[n->attrs.start];
187 return &d->attrs.attrs[n->attrs.start];
191−attr* node_attributeBy(const document* d, const node* n, const char* s, int slen)
191+attr* hexml_node_attribute(const document* d, const node* n, const char* s, int slen)
193 if (slen == -1) slen = (int) strlen(s);
193 if (slen == -1) slen = (int) strlen(s);
194 const int limit = end(n->attrs);
194 const int limit = end(n->attrs);
@@ -202,7 +202,7 @@ attr* node_attributeBy(const document* d, const node* n, const char* s, int slen
204// Search for given strings within a node
204// Search for given strings within a node
205−node* node_childBy(const document* d, const node* parent, const node* prev, const char* s, int slen)
205+node* hexml_node_child(const document* d, const node* parent, const node* prev, const char* s, int slen)
207 if (slen == -1) slen = (int) strlen(s);
207 if (slen == -1) slen = (int) strlen(s);
208 int i = prev == NULL ? parent->nodes.start : (int) (prev + 1 - d->nodes.nodes);
208 int i = prev == NULL ? parent->nodes.start : (int) (prev + 1 - d->nodes.nodes);
@@ -220,27 +220,27 @@ node* node_childBy(const document* d, const node* parent, const node* prev, cons
220/////////////////////////////////////////////////////////////////////
220/////////////////////////////////////////////////////////////////////
221// PARSE TABLE
221// PARSE TABLE
223−const char tag_name1 = 0x1;
223+static const char tag_name1 = 0x1;
224−const char tag_name = 0x2;
224+static const char tag_name = 0x2;
225−const char tag_space = 0x4;
225+static const char tag_space = 0x4;
227−char parse_table[256];
227+static char parse_table[256];
229−void init_parse_table()
229+static void init_parse_table()
231 static bool done = 0;
231 static bool done = 0;
232 if (done) return;
232 if (done) return;
233 for (int i = 0; i < 256; i++)
233 for (int i = 0; i < 256; i++)
235 bool name1 = i == ':' || i == '_' || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z');
235 bool name1 = i == ':' || i == '_' || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z');
236− bool name = name1 || i == '-' || (i >= '0' && i <= '9');
236+ bool name = name1 || i == '-' || i == '.' || (i >= '0' && i <= '9');
237 bool space = i == ' ' || i == '\t' || i == '\r' || i == '\n';
237 bool space = i == ' ' || i == '\t' || i == '\r' || i == '\n';
238 parse_table[i] = (name1 ? tag_name1 : 0) | (name ? tag_name : 0) | (space ? tag_space : 0);
238 parse_table[i] = (name1 ? tag_name1 : 0) | (name ? tag_name : 0) | (space ? tag_space : 0);
240 done = 1;
240 done = 1;
243−static inline bool is(char c, char tag) { return parse_table[c] & tag; }
243+static inline bool is(char c, char tag) { return parse_table[(unsigned char) c] & tag; }
244static inline bool is_name1(char c) { return is(c, tag_name1); }
244static inline bool is_name1(char c) { return is(c, tag_name1); }
245static inline bool is_name(char c) { return is(c, tag_name); }
245static inline bool is_name(char c) { return is(c, tag_name); }
246static inline bool is_space(char c) { return is(c, tag_space); }
246static inline bool is_space(char c) { return is(c, tag_space); }
@@ -249,20 +249,20 @@ static inline bool is_space(char c) { return is(c, tag_space); }
249/////////////////////////////////////////////////////////////////////
249/////////////////////////////////////////////////////////////////////
250// PARSER COMBINATORS
250// PARSER COMBINATORS
252−char static inline peekAt(document* d, int i) { return d->cursor[i]; }
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); }
253−void static inline skip(document* d, int i) { d->cursor += i; }
254+static inline void skip(document* d, int i) { d->cursor += i; }
254−char static inline peek(document* d) { return peekAt(d, 0); }
255−char static inline get(document* d) { char c = peek(d); skip(d, 1); return c; }
255+static inline char get(document* d) { char c = peek(d); skip(d, 1); return c; }
257// Remove whitespace characters from the cursor while they are still whitespace
257// Remove whitespace characters from the cursor while they are still whitespace
258−void static inline trim(document* d)
258+static inline void trim(document* d)
260 while (is_space(peek(d)))
260 while (is_space(peek(d)))
261 skip(d, 1);
261 skip(d, 1);
264// Find this character form the cursor onwards, if true adjust the cursor to that char, otherwise leave it at the end
264// Find this character form the cursor onwards, if true adjust the cursor to that char, otherwise leave it at the end
265−bool find(document* d, char c)
265+static bool find(document* d, char c)
267 char* x = memchr(d->cursor, c, d->end - d->cursor);
267 char* x = memchr(d->cursor, c, d->end - d->cursor);
268 if (x == NULL)
268 if (x == NULL)
@@ -281,7 +281,7 @@ bool find(document* d, char c)
281/////////////////////////////////////////////////////////////////////
281/////////////////////////////////////////////////////////////////////
282// PARSING CODE
282// PARSING CODE
284−void static inline node_alloc(node_buffer* b, int ask)
284+static inline void node_alloc(node_buffer* b, int ask)
286 int space = b->size - b->used_back - b->used_front;
286 int space = b->size - b->used_back - b->used_front;
287 if (space >= ask) return;
287 if (space >= ask) return;
@@ -296,7 +296,7 @@ void static inline node_alloc(node_buffer* b, int ask)
296 b->alloc = buf2;
296 b->alloc = buf2;
299−void static inline attr_alloc(attr_buffer* b, int ask)
299+static inline void attr_alloc(attr_buffer* b, int ask)
301 int space = b->size - b->used;
301 int space = b->size - b->used;
302 if (space >= ask) return;
302 if (space >= ask) return;
@@ -310,7 +310,7 @@ void static inline attr_alloc(attr_buffer* b, int ask)
310 b->alloc = buf2;
310 b->alloc = buf2;
313−void set_error(document* d, char* msg)
313+static void set_error(document* d, const char* msg)
315 if (d->error_message != NULL) return; // keep the first error message
315 if (d->error_message != NULL) return; // keep the first error message
316 d->error_message = malloc(strlen(msg)+1);
316 d->error_message = malloc(strlen(msg)+1);
@@ -334,11 +334,12 @@ static inline str parse_name(document* d)
334static inline str parse_attrval(document* d)
334static inline str parse_attrval(document* d)
337− if (get(d) != '=')
337+ if (peek(d) != '=')
339 set_error(d, "Expected = in attribute, but missing");
339 set_error(d, "Expected = in attribute, but missing");
340 return start_length(0, 0);
340 return start_length(0, 0);
343 char c = peek(d);
344 char c = peek(d);
344 if (c == '\"' || c == '\'')
345 if (c == '\"' || c == '\'')
@@ -367,7 +368,7 @@ static inline str parse_attrval(document* d)
367static inline str parse_attributes(document* d)
368static inline str parse_attributes(document* d)
369 int start = d->attrs.used;
370 int start = d->attrs.used;
370− for (int i = 0; ; i++)
371+ while (d->error_message == NULL)
373 str name = parse_name(d);
374 str name = parse_name(d);
@@ -380,7 +381,7 @@ static inline str parse_attributes(document* d)
380 return start_end(start, d->attrs.used);
381 return start_end(start, d->attrs.used);
383−str parse_content(document* d);
384+static str parse_content(document* d);
386// Add a new entry into tag, am at a '<'
387// Add a new entry into tag, am at a '<'
@@ -395,12 +396,18 @@ static inline void parse_tag(document* d)
395 assert(c == '<');
396 assert(c == '<');
396 if (peek(d) == '?') skip(d, 1);
397 if (peek(d) == '?') skip(d, 1);
397 me->name = parse_name(d);
398 me->name = parse_name(d);
399+ if (me->name.length == 0)
401+ set_error(d, "Missing tag name");
398 me->attrs = parse_attributes(d);
404 me->attrs = parse_attributes(d);
405+ if (d->error_message != NULL) return;
400− c = get(d);
407+ c = peek(d);
401− if ((c == '/' || c == '?') && peek(d) == '>')
408+ if ((c == '/' || c == '?') && peek_at(d, 1) == '>')
403− skip(d, 1);
410+ skip(d, 2);
404 me->nodes = start_length(0, 0);
411 me->nodes = start_length(0, 0);
405 me->outer.length = start_end(me->outer.start, doc_position(d)).length;
412 me->outer.length = start_end(me->outer.start, doc_position(d)).length;
406 me->inner = start_length(doc_position(d), 0);
413 me->inner = start_length(doc_position(d), 0);
@@ -411,6 +418,8 @@ static inline void parse_tag(document* d)
411 set_error(d, "Gunk at the end of the tag");
418 set_error(d, "Gunk at the end of the tag");
414 me->inner.start = doc_position(d);
423 me->inner.start = doc_position(d);
415 str content = parse_content(d);
424 str content = parse_content(d);
@@ -420,7 +429,7 @@ static inline void parse_tag(document* d)
420 me->inner.length = start_end(me->inner.start, doc_position(d)).length;
429 me->inner.length = start_end(me->inner.start, doc_position(d)).length;
422 if (d->error_message != NULL) return;
431 if (d->error_message != NULL) return;
423− if (peek(d) == '<' && peekAt(d, 1) == '/')
432+ if (peek(d) == '<' && peek_at(d, 1) == '/')
425 skip(d, 2);
434 skip(d, 2);
426 if (d->end - d->cursor >= me->name.length &&
435 if (d->end - d->cursor >= me->name.length &&
@@ -442,7 +451,7 @@ static inline void parse_tag(document* d)
443// Parser until </, return the index of your node children
452// Parser until </, return the index of your node children
444// Not inline as it is recursive
453// Not inline as it is recursive
445−str parse_content(document* d)
454+static str parse_content(document* d)
447 int before = d->nodes.used_back;
456 int before = d->nodes.used_back;
448 while (d->error_message == NULL)
457 while (d->error_message == NULL)
@@ -452,22 +461,22 @@ str parse_content(document* d)
455− else if (peekAt(d, 1) == '/')
464+ else if (peek_at(d, 1) == '/')
457 // have found a </
466 // have found a </
460− else if (peekAt(d, 1) == '!' && peekAt(d, 2) == '-' && peekAt(d, 3) == '-')
469+ else if (peek_at(d, 1) == '!' && peek_at(d, 2) == '-' && peek_at(d, 3) == '-')
462 skip(d, 3);
471 skip(d, 3);
463 // you can't reuse the two '-' characters for the closing as well
472 // you can't reuse the two '-' characters for the closing as well
464− if (peekAt(d, 0) == '\0' || peekAt(d, 1) == '\0')
473+ if (peek_at(d, 0) == '\0' || peek_at(d, 1) == '\0')
466 set_error(d, "Didn't get a closing comment");
475 set_error(d, "Didn't get a closing comment");
467 return start_end(0, 0);
476 return start_end(0, 0);
469 skip(d, 2);
478 skip(d, 2);
470− while (1)
479+ for (;;)
472 if (!find(d, '>'))
481 if (!find(d, '>'))
@@ -475,7 +484,7 @@ str parse_content(document* d)
475 return start_end(0, 0);
484 return start_end(0, 0);
477 skip(d, 1);
486 skip(d, 1);
478− if (peekAt(d, -3) == '-' && peekAt(d, -2))
487+ if (peek_at(d, -3) == '-' && peek_at(d, -2) == '-')
@@ -503,7 +512,7 @@ typedef struct
503 node nodes[500];
512 node nodes[500];
506−document* document_parse(const char* s, int slen)
515+document* hexml_document_parse(const char* s, int slen)
508 if (slen == -1) slen = (int) strlen(s);
517 if (slen == -1) slen = (int) strlen(s);
509 assert(s[slen] == 0);
518 assert(s[slen] == 0);
@@ -530,15 +539,13 @@ document* document_parse(const char* s, int slen)
530 d->nodes.nodes[0].outer = start_length(0, slen);
539 d->nodes.nodes[0].outer = start_length(0, slen);
531 d->nodes.nodes[0].inner = start_length(0, slen);
540 d->nodes.nodes[0].inner = start_length(0, slen);
532 d->nodes.nodes[0].attrs = start_length(0, 0);
541 d->nodes.nodes[0].attrs = start_length(0, 0);
534 // Introduce an intermediate result, otherwise behaviour is undefined
543 // Introduce an intermediate result, otherwise behaviour is undefined
535 // because there is no guaranteed ordering between LHS and RHS evaluation
544 // because there is no guaranteed ordering between LHS and RHS evaluation
536 str content = parse_content(d);
545 str content = parse_content(d);
537 d->nodes.nodes[0].nodes = content;
546 d->nodes.nodes[0].nodes = content;
539− if (d->cursor < d->end && d->error_message == NULL)
548+ if (d->cursor < d->end)
541 set_error(d, "Trailing junk at the end of the document");
549 set_error(d, "Trailing junk at the end of the document");
543 return d;
550 return d;