include/frx/frx_tree.h

filter: Statementstmt 100% 128 / 128 Branchbran 88.1% 74 / 84 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 95.2% 202 / 212 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_parse.h ↑ Summary include/frx/frx_utf8.h »
1 #ifndef FRX_TREE_H
2 #define FRX_TREE_H
3  
4 /* The arena tree the ABI describes.
5 *
6 * frx_node and frx_doc are opaque in frx_abi.h and defined here. Every
7 * string is an frx_str in the document's arena, NUL-terminated, so a
8 * consumer compares with memcmp and prints with %s and never copies.
9 * Every pointer is stable for the life of the document because the arena
10 * never reallocates (rule 3).
11 *
12 * An element carries its resolved namespace, prefix and local name, its
13 * attributes in document order with their namespaces resolved, its own
14 * namespace declarations, and a pointer to the scope that binds prefixes
15 * for it and its descendants. xmlns and xmlns:* are declarations, never
16 * attributes. Text, comments and PIs carry a value; a PI's target is in
17 * local. The document node is kind FRX_DOCUMENT; its children are the
18 * top-level comments and PIs with the root element among them.
19 *
20 * frx_doc_free releases the arena and the doc, and the one thing an edit
21 * may have added: an ID index that grew out of the arena into a malloc'd
22 * array (frx_edit.h). Nothing else is allocated.
23 *
24 * Mutation (frx_edit.h) pays in the arena's currency: a node is never
25 * moved or freed by an edit, so every pointer stays stable as rule 3
26 * promises; attributes and declarations live in blocks with a capacity,
27 * and growing past it allocates a larger block, copies, and orphans the
28 * old one until the document is freed. An edit loop that runs for the
29 * document's life grows the arena; to_string and a reparse are the
30 * compaction.
31 *
32 * Needs frx_abi.h, frx_arena.h, frx_utf8.h; frx_dtd is frx_dtd.h's, held
33 * by pointer. This file precedes frx_lex.h, which reads frx_str helpers
34 * and the doctype record from here. */
35  
36 typedef struct frx_dtd frx_dtd;
37 struct frx_verr; /* frx_validate.h: one collected violation */
38  
39 typedef struct frx_attr {
40 frx_str ns; /* "" when none; never the default namespace */
41 frx_str prefix; /* "" when none */
42 frx_str local;
43 frx_str qname; /* as written */
44 frx_str value; /* normalised */
45 size_t offset; /* where it was written; a default's is its declaration's */
46 int defaulted; /* full: materialised from an ATTLIST default, not written */
47 } frx_attr;
48  
49 typedef struct frx_nsdecl {
50 frx_str prefix; /* "" for the default namespace */
51 frx_str uri; /* "" for xmlns="" */
52 } frx_nsdecl;
53  
54 /* An element with declarations creates a scope pointing at the enclosing
55 * one; lookup walks the chain (a chain, not copies). */
56 typedef struct frx_scope {
57 const struct frx_scope *up;
58 const frx_nsdecl *decls;
59 int n_decls;
60 } frx_scope;
61  
62 /* What a run of character data knows about how it got there, which
63 * second edition erratum E15 needs and nothing else does. REF_CHARS: a
64 * character or predefined-entity reference expanded into this run while
65 * content was being read, so its characters do not "appear directly in
66 * the document" and cannot be the ignorable white space of section 2.10.
67 * REF_HERE: a reference stood in this run at all, the empty-replacement
68 * case included, which is content even when it leaves nothing behind.
69 * An entity's own replacement text carries neither: the entity is a
70 * storage unit and its text stands where the reference was. */
71 enum { FRX_TEXT_REF_CHARS = 1, FRX_TEXT_REF_HERE = 2 };
72  
73 /* frx_node.from_ref. FROM_REF is Second edition erratum E15: on a text
74 * node, some of its characters were produced by a character or
75 * predefined-entity reference here rather than standing in the document;
76 * on an element, a reference stood directly in its content and left no
77 * characters behind, which an element declared EMPTY may not hold either.
78 * XMLNS_XML is on an element that wrote an xmlns:xml declaration. That
79 * declaration is accepted and dropped from decls, because binding the xml
80 * prefix to its own namespace changes nothing a lookup, a canonical form
81 * or the writer would do; validation is the one pass that still has to
82 * know it was written, because to a DTD it is an Attribute like any other
83 * and VC: Attribute Value Type applies to it. */
84 enum { FRX_NODE_FROM_REF = 1, FRX_NODE_XMLNS_XML = 2 };
85  
86 /* A CDATA section inside a merged text node: where in the value it began
87 * and how long it is. Only the writer cares; the data model and
88 * the canonical form see text. Recorded under the full profile only. */
89 typedef struct frx_span {
90 size_t offset;
91 size_t len;
92 } frx_span;
93  
94 struct frx_node {
95 int kind;
96 struct frx_node *parent;
97 struct frx_node *first_child;
98 struct frx_node *last_child;
99 struct frx_node *next;
100 struct frx_node *prev; /* so that detaching is O(1); set at parse and by every edit */
101 frx_str ns; /* element */
102 frx_str prefix; /* element */
103 frx_str local; /* element; a PI's target */
104 frx_str qname; /* element, as written */
105 frx_str value; /* text, comment, PI */
106 const frx_attr *attrs;
107 int n_attrs;
108 int cap_attrs; /* the block's capacity; 0 means exactly n_attrs (a parsed node) */
109 const frx_nsdecl *decls;
110 int n_decls;
111 int cap_decls;
112 const frx_scope *scope;
113 size_t offset; /* where it started, for messages */
114 const frx_span *cdata_spans; /* text under full: the CDATA sections in value; else NULL */
115 int n_cdata_spans;
116 int from_ref; /* FRX_NODE_* below: the provenance bits the
117 * tree keeps, tested by mask and never for
118 * plain truth */
119 long order; /* document order, assigned at parse; stale when doc->renumber */
120 };
121  
122 /* what a DOCTYPE said, under the full profile; NULL under strict */
123 typedef struct frx_doctype {
124 frx_str name;
125 frx_str public_id; /* "" when none */
126 frx_str system_id; /* "" when none */
127 frx_str subset; /* the internal subset's text, "" when none */
128 int has_subset; /* there was a [ ], even an empty one */
129 size_t offset;
130 } frx_doctype;
131  
132 /* Where the ID index came from, so XPath's id() does not guess: the
133 * `id_attrs` option today, the DTD's declared ID attributes under
134 * validate, xml:id under full. */
135 enum { FRX_ID_NONE = 0, FRX_ID_ATTRS, FRX_ID_DTD, FRX_ID_XMLID };
136  
137 /* one ID index entry: sorted by (attr, value) */
138 typedef struct frx_idx {
139 frx_str attr; /* the attribute's local name */
140 frx_str value;
141 const frx_node *node;
142 size_t offset;
143 } frx_idx;
144  
145 struct frx_doc {
146 frx_arena arena;
147 frx_node *document;
148 frx_node *root;
149 const frx_idx *ids; /* NULL without id_attrs; sorted by (attr, value) */
150 int n_ids;
151 frx_idx *ids_mut; /* the index once an edit has touched it: malloc'd, ids points at it */
152 int cap_ids_mut;
153 const frx_str *id_attrs; /* the names indexed, copied into the arena */
154 int n_id_attrs;
155 int id_source; /* FRX_ID_* : what the index was built from */
156 int max_depth; /* as applied */
157 int profile; /* FRX_PROFILE_* it was parsed under */
158 frx_str version; /* "1.0" unless the declaration said otherwise */
159 frx_str base_uri; /* the document's own URI, from the base option; "" when unknown */
160 int standalone;/* 1 yes, 0 no or unsaid */
161 const frx_doctype *doctype; /* full only; NULL otherwise */
162 const frx_dtd *dtd; /* full: what the internal subset declared; NULL without one */
163 long next_order;/* the next order value to assign */
164 int renumber; /* set by an edit; order is stale until renumbered */
165 const struct frx_verr *errors; /* validate => collect: the violations, in document order */
166 int n_errors;
167 const size_t *undeclared_refs; /* full: where entity references stood that
168 * WFC: Entity Declared exempted, so that
169 * validation can report VC: Entity Declared */
170 int n_undeclared_refs;
171 };
172  
173 static const frx_str FRX_EMPTY_STR = { "", 0 };
174  
175 static frx_doc *
176 788945 frx_doc_new(void)
177 {
178 788945 frx_doc *d = (frx_doc *)malloc(sizeof *d);
179 788945 ▶ if (!d) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!d) return NULL; 0 788945 788945
180 /* Zero FIRST, then set what is not zero. This function used to name
181 * every field and nothing else, which made adding a field to frx_doc a
182 * trap: 0.02's `undeclared_refs` was added, missed here, and shipped
183 * reading malloc's leavings - two CPAN smokers segfaulted and a third
184 * reported a validity error at a garbage offset, none of it
185 * reproducible on the author's machine. The memset costs one pass over
186 * a struct allocated once per document. Note the ORDER: the arena is
187 * initialised after it, never before, or its head pointer is wiped. */
188 788945 memset(d, 0, sizeof *d);
189 788945 frx_arena_init(&d->arena);
190 788945 d->document = NULL;
191 788945 d->root = NULL;
192 788945 d->ids = NULL;
193 788945 d->n_ids = 0;
194 788945 d->ids_mut = NULL;
195 788945 d->cap_ids_mut = 0;
196 788945 d->id_attrs = NULL;
197 788945 d->n_id_attrs = 0;
198 788945 d->id_source = FRX_ID_NONE;
199 788945 d->max_depth = 0;
200 788945 d->profile = FRX_PROFILE_STRICT;
201 788945 d->version.p = "1.0";
202 788945 d->version.len = 3;
203 788945 d->base_uri = FRX_EMPTY_STR;
204 788945 d->standalone = 0;
205 788945 d->doctype = NULL;
206 788945 d->dtd = NULL;
207 788945 d->next_order = 0;
208 788945 d->renumber = 0;
209 788945 d->errors = NULL;
210 788945 d->n_errors = 0;
211 788945 d->undeclared_refs = NULL;
212 788945 d->n_undeclared_refs = 0;
213 788945 return d;
214 }
215  
216 static void
217 788945 frx_doc_free(frx_doc *d)
218 {
219 788945 ▶ if (!d) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!d) return; 0 788945 788945
220 788945 free(d->ids_mut);
221 788945 frx_arena_free(&d->arena);
222 788945 free(d);
223 }
224  
225 static frx_node *
226 6172204 frx_node_new(frx_doc *d, int kind, size_t offset)
227 {
228 6172204 frx_node *n = (frx_node *)frx_arena_alloc(&d->arena, sizeof *n);
229 6172204 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 6172204 6172204
230 6172204 memset(n, 0, sizeof *n);
231 6172204 n->kind = kind;
232 6172204 n->offset = offset;
233 6172204 n->ns = n->prefix = n->local = n->qname = n->value = FRX_EMPTY_STR;
234 6172204 n->order = d->next_order++; /* nodes are created in document order */
235 6172204 return n;
236 }
237  
238 static void
239 5383105 frx_node_append(frx_node *parent, frx_node *child)
240 {
241 5383105 child->parent = parent;
242 5383105 child->next = NULL;
243 5383105 child->prev = parent->last_child;
244 5383105 ▶ if (parent->last_child) parent->last_child->next = child;
Branch: 1 branch
BranchTrueFalseTotal
if (parent->last_child) parent->last_child->next = child; 1822968 3560137 5383105
245 3560137 else parent->first_child = child;
246 5383105 parent->last_child = child;
247 5383105 }
248  
249 /* child before ref, under ref's parent */
250 static void
251 148 frx_node_insert_before(frx_node *ref, frx_node *child)
252 {
253 148 frx_node *parent = ref->parent;
254 148 child->parent = parent;
255 148 child->next = ref;
256 148 child->prev = ref->prev;
257 148 ▶ if (ref->prev) ref->prev->next = child;
Branch: 1 branch
BranchTrueFalseTotal
if (ref->prev) ref->prev->next = child; 107 41 148
258 41 else parent->first_child = child;
259 148 ref->prev = child;
260 148 }
261  
262 /* out of its parent's list; the node itself is untouched and still its
263 * document's */
264 static void
265 155 frx_node_unlink(frx_node *n)
266 {
267 155 frx_node *parent = n->parent;
268 155 ▶ if (!parent) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!parent) return; 0 155 155
269 155 ▶ if (n->prev) n->prev->next = n->next; else parent->first_child = n->next;
Branch: 1 branch
BranchTrueFalseTotal
if (n->prev) n->prev->next = n->next; else parent->first_child = n->next; 146 9 155
270 155 ▶ if (n->next) n->next->prev = n->prev; else parent->last_child = n->prev;
Branch: 1 branch
BranchTrueFalseTotal
if (n->next) n->next->prev = n->prev; else parent->last_child = n->prev; 5 150 155
271 155 n->parent = n->prev = n->next = NULL;
272 }
273  
274 /* document order reassigned from the document node down, iteratively,
275 * after edits have made it stale; cheap when nothing changed */
276 static void
277 219 frx_doc_renumber(frx_doc *d)
278 {
279 219 frx_node *n = d->document;
280 219 long k = 0;
281 219 ▶ if (!d->renumber || !n) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!d->renumber || !n) return; 5 214 219
if (!d->renumber || !n) return; 0 5 5
282 41 ▶ while (n) {
Branch: 1 branch
BranchTrueFalseTotal
while (n) { 36 5 41
283 36 n->order = k++;
284 36 ▶ if (n->first_child) { n = n->first_child; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (n->first_child) { n = n->first_child; continue; } 19 17 36
285 41 ▶ while (n && !n->next) n = n->parent;
Branch: 2 branches
BranchTrueFalseTotal
while (n && !n->next) n = n->parent; 36 5 41
while (n && !n->next) n = n->parent; 24 12 36
286 17 ▶ if (n) n = n->next;
Branch: 1 branch
BranchTrueFalseTotal
if (n) n = n->next; 12 5 17
287 }
288 5 d->next_order = k;
289 5 d->renumber = 0;
290 }
291  
292 static int
293 70569706 frx_str_eq(const frx_str *a, const char *p, size_t n)
294 {
295 70569706 ▶ return a->len == n && (n == 0 || memcmp(a->p, p, n) == 0);
Branch: 3 branches
BranchTrueFalseTotal
return a->len == n && (n == 0 || memcmp(a->p, p, n) == 0); 52561344 18008362 70569706
return a->len == n && (n == 0 || memcmp(a->p, p, n) == 0); 50153578 2407766 52561344
return a->len == n && (n == 0 || memcmp(a->p, p, n) == 0); 8488452 41665126 50153578
296 }
297  
298 static int
299 56944001 frx_str_eq2(const frx_str *a, const frx_str *b)
300 {
301 56944001 return frx_str_eq(a, b->p, b->len);
302 }
303  
304 /* every byte an S: what is dropped outside the root */
305 static int
306 41732 frx_str_is_s(const frx_str *s)
307 {
308 size_t i;
309 87224 ▶ for (i = 0; i < s->len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->len; i++) 53608 33616 87224
310 53608 ▶ if (!frx_is_s((unsigned char)s->p[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_is_s((unsigned char)s->p[i])) return 0; 8116 45492 53608
311 33616 return 1;
312 }
313  
314 /* ---- equality over the data model ------------------------------------
315 *
316 * Two nodes are equal when their kinds match, an element's namespace
317 * name and local name match and its attributes match as an unordered
318 * set of (namespace, local name, value), text and comment values match,
319 * a PI's target and data match, and their children are equal in order.
320 * Prefixes, CDATA spans, the defaulted flag and offsets are not
321 * compared: they are not the data model. This is the round-trip oracle
322 * for the writer, iterative over an explicit stack of pairs (rule 2). */
323  
324 static int
325 8191 frx_node_same(const frx_node *a, const frx_node *b)
326 {
327 int i, j;
328 8191 ▶ if (a->kind != b->kind) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (a->kind != b->kind) return 0; 0 8191 8191
329 8191 switch (a->kind) {
330 3626 case FRX_ELEMENT:
331 3626 ▶ if (!frx_str_eq2(&a->ns, &b->ns) || !frx_str_eq2(&a->local, &b->local)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_str_eq2(&a->ns, &b->ns) || !frx_str_eq2(&a->local, &b->local)) return 0; 3625 1 3626
if (!frx_str_eq2(&a->ns, &b->ns) || !frx_str_eq2(&a->local, &b->local)) return 0; 1 3624 3625
332 3624 ▶ if (a->n_attrs != b->n_attrs) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (a->n_attrs != b->n_attrs) return 0; 0 3624 3624
333 4970 ▶ for (i = 0; i < a->n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->n_attrs; i++) { 1347 3623 4970
334 1347 int found = 0;
335 4079 ▶ for (j = 0; j < b->n_attrs && !found; j++)
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < b->n_attrs && !found; j++) 3176 903 4079
for (j = 0; j < b->n_attrs && !found; j++) 2732 444 3176
336 2732 ▶ if (frx_str_eq2(&a->attrs[i].ns, &b->attrs[j].ns)
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&a->attrs[i].ns, &b->attrs[j].ns) 2708 24 2732
337 2708 ▶ && frx_str_eq2(&a->attrs[i].local, &b->attrs[j].local)
Branch: 1 branch
BranchTrueFalseTotal
&& frx_str_eq2(&a->attrs[i].local, &b->attrs[j].local) 1347 1361 2708
338 1347 ▶ && frx_str_eq2(&a->attrs[i].value, &b->attrs[j].value))
Branch: 1 branch
BranchTrueFalseTotal
&& frx_str_eq2(&a->attrs[i].value, &b->attrs[j].value)) 1346 1 1347
339 1346 found = 1;
340 1347 ▶ if (!found) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!found) return 0; 1 1346 1347
341 }
342 3623 return 1;
343 123 case FRX_PI:
344 123 ▶ if (!frx_str_eq2(&a->local, &b->local)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_str_eq2(&a->local, &b->local)) return 0; 0 123 123
345 /* fall through */
346 case FRX_TEXT:
347 case FRX_COMMENT:
348 2874 return frx_str_eq2(&a->value, &b->value);
349 1691 default:
350 1691 return 1;
351 }
352 }
353  
354 typedef struct frx_node_pair { const frx_node *a; const frx_node *b; } frx_node_pair;
355  
356 static int
357 1692 frx_tree_equal(const frx_node *a, const frx_node *b)
358 {
359 1692 frx_node_pair *stack = NULL;
360 1692 int sp = 0, cap = 0, equal = 1;
361 1692 stack = (frx_node_pair *)malloc(16 * sizeof *stack);
362 1692 ▶ if (!stack) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!stack) return 0; 0 1692 1692
363 1692 cap = 16;
364 1692 stack[sp].a = a; stack[sp].b = b; sp++;
365 9858 ▶ while (sp && equal) {
Branch: 2 branches
BranchTrueFalseTotal
while (sp && equal) { 8226 1632 9858
while (sp && equal) { 8191 35 8226
366 const frx_node *ca, *cb;
367 8191 sp--;
368 8191 a = stack[sp].a; b = stack[sp].b;
369 8191 ▶ if (!frx_node_same(a, b)) { equal = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_node_same(a, b)) { equal = 0; break; } 25 8166 8191
370 8166 ca = a->first_child; cb = b->first_child;
371 15019 ▶ while (ca && cb) {
Branch: 2 branches
BranchTrueFalseTotal
while (ca && cb) { 6886 8133 15019
while (ca && cb) { 6853 33 6886
372 6853 ▶ if (sp == cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (sp == cap) { 40 6813 6853
373 40 int ncap = cap * 2;
374 40 frx_node_pair *ns = (frx_node_pair *)realloc(stack, (size_t)ncap * sizeof *ns);
375 40 ▶ if (!ns) { free(stack); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ns) { free(stack); return 0; } 0 40 40
376 40 stack = ns; cap = ncap;
377 }
378 6853 stack[sp].a = ca; stack[sp].b = cb; sp++;
379 6853 ca = ca->next; cb = cb->next;
380 }
381 8166 ▶ if (ca || cb) equal = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (ca || cb) equal = 0; 8133 33 8166
if (ca || cb) equal = 0; 2 8131 8133
382 }
383 1692 free(stack);
384 1692 return equal;
385 }
386  
387 #endif /* FRX_TREE_H */
« include/frx/frx_parse.h ↑ Summary include/frx/frx_utf8.h »