include/frx/frx_model.h

filter: Statementstmt 96.6% 202 / 209 Branchbran 75.2% 140 / 186 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.5% 342 / 395 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_xpath_parse.h ↑ Summary include/frx/frx_arena.h »
1 #ifndef FRX_MODEL_H
2 #define FRX_MODEL_H
3  
4 /* Content models compiled to a deterministic automaton.
5 *
6 * frx_dtd.h stores each ELEMENT declaration's content model as the token
7 * list it was written as: parentheses, names, `,` and `|`, and the `?`,
8 * `*` and `+` that follow whatever they modify. This turns that list
9 * into something a document can be run against, once per element type,
10 * and never looks at the tokens again.
11 *
12 * THE CONSTRUCTION is Glushkov's. Every name in the model is a position;
13 * a particle has a `nullable` flag, a set of positions it can start
14 * with, and a set it can end with; and `follow[p]` is every position
15 * that may come directly after position p. They are built bottom up by
16 * folding the token list with an explicit stack of groups (rule 2: the
17 * model is the DTD author's text, and nothing here recurses over it):
18 *
19 * leaf p nullable 0, first {p}, last {p}
20 * A , B follow[q] |= first(B) for q in last(A)
21 * nullable nA && nB
22 * first first(A) + (nA ? first(B) : {})
23 * last last(B) + (nB ? last(A) : {})
24 * A | B nullable nA || nB, first and last the unions
25 * A? nullable 1
26 * A* follow[q] |= first(A) for q in last(A), nullable 1
27 * A+ follow[q] |= first(A) for q in last(A)
28 *
29 * The automaton's states are the positions plus a start state. From the
30 * start, a name takes you to the position in first(root) carrying it;
31 * from position p, to the one in follow[p]. It accepts when the position
32 * it stops on is in last(root), or when nothing was read and the root is
33 * nullable.
34 *
35 * THAT IS ONLY AN AUTOMATON IF THE MODEL IS DETERMINISTIC, which is
36 * exactly XML 1.0's requirement in section 3.2.1 and its appendix E: a
37 * processor must be able to tell which position a name matches without
38 * looking ahead. Stated in these terms it is one check: no two positions
39 * in first(root) carry the same name, and no two positions in any
40 * follow[p] carry the same name. `((a, b) | (a, c))` fails it, because
41 * an `a` at the start could be either position; `(a, (b | c))` is the
42 * deterministic rewrite. The check is a validity constraint, so it is
43 * made only when the caller asked to validate, and a model that fails it
44 * is reported with its declaration's offset and validated no further.
45 *
46 * MIXED CONTENT is not compiled: `(#PCDATA | a | b)*` admits its names
47 * in any order and any number, so the whole of it is the name list, and
48 * the only thing to check at build time is that no name is in it twice
49 * (VC: No Duplicate Types).
50 *
51 * The cost of a pathological model is memory in the DTD, never time in
52 * the document: the run over one element's children is a loop with a
53 * single current position.
54 *
55 * Needs frx_arena.h, frx_tree.h, frx_dtd.h. */
56  
57 typedef struct frx_cm_dfa {
58 const frx_str *name; /* name[i], the name at position i */
59 int n_pos;
60 const int *first; /* the positions the start state goes to */
61 int n_first;
62 const int *last; /* the accepting positions */
63 int n_last;
64 const int **follow; /* follow[i], the positions after position i */
65 const int *n_follow;
66 int nullable; /* the empty sequence is accepted */
67 int mixed; /* a mixed model: `name` is the whole of it */
68 } frx_cm_dfa;
69  
70 enum { FRX_MODEL_OK = 0, FRX_MODEL_NOMEM, FRX_MODEL_NONDET, FRX_MODEL_DUPTYPE };
71  
72 /* ---- growable position sets, malloc'd for the build only --------------- */
73  
74 typedef struct frx_pset {
75 int *v;
76 int n, cap;
77 } frx_pset;
78  
79 static int
80 17497 frx_pset_add(frx_pset *s, int p)
81 {
82 int i;
83 111325 ▶ for (i = 0; i < s->n; i++) if (s->v[i] == p) return 1;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < s->n; i++) if (s->v[i] == p) return 1; 5 93828 93833
for (i = 0; i < s->n; i++) if (s->v[i] == p) return 1; 93833 17492 111325
84 17492 ▶ if (s->n == s->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->n == s->cap) { 6014 11478 17492
85 6014 ▶ int ncap = s->cap ? s->cap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = s->cap ? s->cap * 2 : 8; 783 5231 6014
86 6014 int *nv = (int *)realloc(s->v, (size_t)ncap * sizeof *nv);
87 6014 ▶ if (!nv) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!nv) return 0; 0 6014 6014
88 6014 s->v = nv; s->cap = ncap;
89 }
90 17492 s->v[s->n++] = p;
91 17492 return 1;
92 }
93  
94 static int
95 3608 frx_pset_union(frx_pset *d, const frx_pset *s)
96 {
97 int i;
98 17417 ▶ for (i = 0; i < s->n; i++) if (!frx_pset_add(d, s->v[i])) return 0;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < s->n; i++) if (!frx_pset_add(d, s->v[i])) return 0; 0 13809 13809
for (i = 0; i < s->n; i++) if (!frx_pset_add(d, s->v[i])) return 0; 13809 3608 17417
99 3608 return 1;
100 }
101  
102 /* a particle: what it may start with, end with, and whether it may be empty */
103 typedef struct frx_cm_part {
104 int nullable;
105 frx_pset first;
106 frx_pset last;
107 } frx_cm_part;
108  
109 static void
110 1844 frx_cm_part_free(frx_cm_part *p)
111 {
112 1844 free(p->first.v);
113 1844 free(p->last.v);
114 1844 memset(p, 0, sizeof *p);
115 1844 }
116  
117 /* one group being read: what has been combined, its separator, and the
118 * operand a modifier would apply to */
119 typedef struct frx_cm_frame {
120 frx_cm_part acc;
121 int have_acc;
122 int sep; /* ',' or '|', 0 until the first one */
123 frx_cm_part pending;
124 int have_pending;
125 } frx_cm_frame;
126  
127 typedef struct frx_cm_build {
128 frx_pset *follow; /* follow[i], one per position */
129 frx_str *name; /* name[i], borrowed from the model tokens */
130 int n_pos, cap_pos;
131 frx_cm_frame *stack;
132 int sp, cap_stack;
133 int failed;
134 } frx_cm_build;
135  
136 static void
137 623 frx_cm_build_free(frx_cm_build *b)
138 {
139 int i;
140 2467 ▶ for (i = 0; i < b->n_pos; i++) free(b->follow[i].v);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->n_pos; i++) free(b->follow[i].v); 1844 623 2467
141 623 ▶ for (i = 0; i < b->sp; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->sp; i++) { 0 623 623
142 0 frx_cm_part_free(&b->stack[i].acc);
143 0 frx_cm_part_free(&b->stack[i].pending);
144 }
145 623 free(b->follow);
146 623 free(b->name);
147 623 free(b->stack);
148 623 memset(b, 0, sizeof *b);
149 623 }
150  
151 static int
152 1844 frx_cm_new_pos(frx_cm_build *b, const frx_str *name)
153 {
154 1844 ▶ if (b->n_pos == b->cap_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->n_pos == b->cap_pos) { 641 1203 1844
155 641 ▶ int ncap = b->cap_pos ? b->cap_pos * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = b->cap_pos ? b->cap_pos * 2 : 16; 18 623 641
156 641 frx_pset *nf = (frx_pset *)realloc(b->follow, (size_t)ncap * sizeof *nf);
157 frx_str *nn;
158 641 ▶ if (!nf) { b->failed = 1; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!nf) { b->failed = 1; return -1; } 0 641 641
159 641 b->follow = nf;
160 641 nn = (frx_str *)realloc(b->name, (size_t)ncap * sizeof *nn);
161 641 ▶ if (!nn) { b->failed = 1; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!nn) { b->failed = 1; return -1; } 0 641 641
162 641 b->name = nn;
163 641 b->cap_pos = ncap;
164 }
165 1844 memset(&b->follow[b->n_pos], 0, sizeof b->follow[b->n_pos]);
166 1844 b->name[b->n_pos] = *name;
167 1844 return b->n_pos++;
168 }
169  
170 /* follow[q] gains every position of s, for every q in the set l */
171 static int
172 848 frx_cm_link(frx_cm_build *b, const frx_pset *l, const frx_pset *s)
173 {
174 int i;
175 2662 ▶ for (i = 0; i < l->n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l->n; i++) 1814 848 2662
176 1814 ▶ if (!frx_pset_union(&b->follow[l->v[i]], s)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_pset_union(&b->follow[l->v[i]], s)) return 0; 0 1814 1814
177 848 return 1;
178 }
179  
180 /* acc = acc `sep` p, consuming p */
181 static int
182 1221 frx_cm_combine(frx_cm_build *b, frx_cm_part *acc, frx_cm_part *p, int sep)
183 {
184 1221 int ok = 1;
185 1221 ▶ if (sep == ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (sep == ',') { 462 759 1221
186 462 ok = frx_cm_link(b, &acc->last, &p->first);
187 462 ▶ if (ok && acc->nullable) ok = frx_pset_union(&acc->first, &p->first);
Branch: 2 branches
BranchTrueFalseTotal
if (ok && acc->nullable) ok = frx_pset_union(&acc->first, &p->first); 462 0 462
if (ok && acc->nullable) ok = frx_pset_union(&acc->first, &p->first); 17 445 462
188 462 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 462 0 462
189 462 ▶ if (p->nullable) { ok = frx_pset_union(&acc->last, &p->last); }
Branch: 1 branch
BranchTrueFalseTotal
if (p->nullable) { ok = frx_pset_union(&acc->last, &p->last); } 259 203 462
190 203 else { free(acc->last.v); acc->last = p->last; memset(&p->last, 0, sizeof p->last); }
191 }
192 462 ▶ acc->nullable = acc->nullable && p->nullable;
Branch: 2 branches
BranchTrueFalseTotal
acc->nullable = acc->nullable && p->nullable; 17 445 462
acc->nullable = acc->nullable && p->nullable; 8 9 17
193 } else {
194 759 ▶ ok = frx_pset_union(&acc->first, &p->first) && frx_pset_union(&acc->last, &p->last);
Branch: 2 branches
BranchTrueFalseTotal
ok = frx_pset_union(&acc->first, &p->first) && frx_pset_union(&acc->last, &p->last); 759 0 759
ok = frx_pset_union(&acc->first, &p->first) && frx_pset_union(&acc->last, &p->last); 759 0 759
195 759 ▶ acc->nullable = acc->nullable || p->nullable;
Branch: 2 branches
BranchTrueFalseTotal
acc->nullable = acc->nullable || p->nullable; 754 5 759
acc->nullable = acc->nullable || p->nullable; 4 750 754
196 }
197 1221 frx_cm_part_free(p);
198 1221 ▶ if (!ok) b->failed = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) b->failed = 1; 0 1221 1221
199 1221 return ok;
200 }
201  
202 static int
203 2183 frx_cm_fold_pending(frx_cm_build *b, frx_cm_frame *f)
204 {
205 2183 ▶ if (!f->have_pending) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!f->have_pending) return 1; 0 2183 2183
206 2183 ▶ if (!f->have_acc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!f->have_acc) { 962 1221 2183
207 962 f->acc = f->pending;
208 962 f->have_acc = 1;
209 962 memset(&f->pending, 0, sizeof f->pending);
210 962 f->have_pending = 0;
211 962 return 1;
212 }
213 1221 f->have_pending = 0;
214 1221 ▶ return frx_cm_combine(b, &f->acc, &f->pending, f->sep ? f->sep : ',');
Branch: 1 branch
BranchTrueFalseTotal
return frx_cm_combine(b, &f->acc, &f->pending, f->sep ? f->sep : ','); 1221 0 1221
215 }
216  
217 static int
218 962 frx_cm_push_frame(frx_cm_build *b)
219 {
220 962 ▶ if (b->sp == b->cap_stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->sp == b->cap_stack) { 623 339 962
221 623 ▶ int ncap = b->cap_stack ? b->cap_stack * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = b->cap_stack ? b->cap_stack * 2 : 16; 0 623 623
222 623 frx_cm_frame *ns = (frx_cm_frame *)realloc(b->stack, (size_t)ncap * sizeof *ns);
223 623 ▶ if (!ns) { b->failed = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ns) { b->failed = 1; return 0; } 0 623 623
224 623 b->stack = ns;
225 623 b->cap_stack = ncap;
226 }
227 962 memset(&b->stack[b->sp], 0, sizeof b->stack[b->sp]);
228 962 b->sp++;
229 962 return 1;
230 }
231  
232 /* ---- mixed content ------------------------------------------------------ */
233  
234 /* `(#PCDATA | a | b)*`: the names, with a repeat refused as VC: No
235 * Duplicate Types */
236 static int
237 557 frx_model_mixed(frx_arena *a, const frx_eldecl *ed, frx_cm_dfa **out)
238 {
239 frx_cm_dfa *d;
240 frx_str *names;
241 557 int i, n = 0, k = 0, j;
242  
243 4082 ▶ for (i = 0; i < ed->n_model; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ed->n_model; i++) 3525 557 4082
244 3525 ▶ if (ed->model[i].kind == FRX_CM_NAME) n++;
Branch: 1 branch
BranchTrueFalseTotal
if (ed->model[i].kind == FRX_CM_NAME) n++; 854 2671 3525
245 557 d = (frx_cm_dfa *)frx_arena_alloc(a, sizeof *d);
246 557 ▶ if (!d) return FRX_MODEL_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!d) return FRX_MODEL_NOMEM; 0 557 557
247 557 memset(d, 0, sizeof *d);
248 557 d->mixed = 1;
249 557 d->nullable = 1;
250 557 ▶ if (n) {
Branch: 1 branch
BranchTrueFalseTotal
if (n) { 131 426 557
251 131 names = (frx_str *)frx_arena_alloc(a, (size_t)n * sizeof *names);
252 131 ▶ if (!names) return FRX_MODEL_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!names) return FRX_MODEL_NOMEM; 0 131 131
253 2347 ▶ for (i = 0; i < ed->n_model; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ed->n_model; i++) { 2220 127 2347
254 2220 ▶ if (ed->model[i].kind != FRX_CM_NAME) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (ed->model[i].kind != FRX_CM_NAME) continue; 1368 852 2220
255 6391 ▶ for (j = 0; j < k; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < k; j++) 5543 848 6391
256 5543 ▶ if (frx_str_eq2(&names[j], &ed->model[i].name)) return FRX_MODEL_DUPTYPE;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&names[j], &ed->model[i].name)) return FRX_MODEL_DUPTYPE; 4 5539 5543
257 848 names[k++] = ed->model[i].name;
258 }
259 127 d->name = names;
260 127 d->n_pos = k;
261 }
262 553 *out = d;
263 553 return FRX_MODEL_OK;
264 }
265  
266 /* ---- the automaton ------------------------------------------------------ */
267  
268 /* no two positions of the set carry the same name */
269 static int
270 2459 frx_model_deterministic(const frx_cm_build *b, const frx_pset *s)
271 {
272 int i, j;
273 15187 ▶ for (i = 0; i < s->n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->n; i++) 12730 2457 15187
274 95561 ▶ for (j = i + 1; j < s->n; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = i + 1; j < s->n; j++) 82833 12728 95561
275 82833 ▶ if (frx_str_eq2(&b->name[s->v[i]], &b->name[s->v[j]])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&b->name[s->v[i]], &b->name[s->v[j]])) return 0; 2 82831 82833
276 2457 return 1;
277 }
278  
279 static const int *
280 3078 frx_model_copy_set(frx_arena *a, const frx_pset *s, int *n)
281 {
282 int *v;
283 3078 *n = s->n;
284 3078 ▶ if (!s->n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!s->n) return NULL; 297 2781 3078
285 2781 v = (int *)frx_arena_alloc(a, (size_t)s->n * sizeof *v);
286 2781 ▶ if (!v) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return NULL; 0 2781 2781
287 2781 memcpy(v, s->v, (size_t)s->n * sizeof *v);
288 2781 return v;
289 }
290  
291 /* The element's content model as an automaton in the arena. One of
292 * FRX_MODEL_*; *out is filled only on FRX_MODEL_OK. Only MIXED and
293 * CHILDREN reach here: EMPTY and ANY are not models and the validator
294 * answers them without one. */
295 static int
296 1180 frx_model_build(frx_arena *a, const frx_eldecl *ed, frx_cm_dfa **out)
297 {
298 frx_cm_build b;
299 frx_cm_dfa *d;
300 frx_cm_part root;
301 1180 int i, rc = FRX_MODEL_OK;
302  
303 1180 *out = NULL;
304 1180 ▶ if (ed->content == FRX_CONTENT_MIXED) return frx_model_mixed(a, ed, out);
Branch: 1 branch
BranchTrueFalseTotal
if (ed->content == FRX_CONTENT_MIXED) return frx_model_mixed(a, ed, out); 557 623 1180
305  
306 623 memset(&b, 0, sizeof b);
307 623 memset(&root, 0, sizeof root);
308  
309 6181 ▶ for (i = 0; i < ed->n_model && !b.failed; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < ed->n_model && !b.failed; i++) { 5558 623 6181
for (i = 0; i < ed->n_model && !b.failed; i++) { 5558 0 5558
310 5558 const frx_cm_tok *t = &ed->model[i];
311 frx_cm_frame *f;
312 5558 switch (t->kind) {
313 962 case '(':
314 962 ▶ if (!frx_cm_push_frame(&b)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_cm_push_frame(&b)) break; 0 962 962
315 962 continue;
316 3688 case FRX_CM_NAME: {
317 int p;
318 1844 ▶ if (b.sp == 0) { b.failed = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (b.sp == 0) { b.failed = 1; break; } 0 1844 1844
319 1844 p = frx_cm_new_pos(&b, &t->name);
320 1844 ▶ if (p < 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (p < 0) break; 0 1844 1844
321 1844 f = &b.stack[b.sp - 1];
322 1844 ▶ if (f->have_pending) frx_cm_part_free(&f->pending);
Branch: 1 branch
BranchTrueFalseTotal
if (f->have_pending) frx_cm_part_free(&f->pending); 0 1844 1844
323 1844 memset(&f->pending, 0, sizeof f->pending);
324 1844 ▶ if (!frx_pset_add(&f->pending.first, p) || !frx_pset_add(&f->pending.last, p)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_pset_add(&f->pending.first, p) || !frx_pset_add(&f->pending.last, p)) { 1844 0 1844
if (!frx_pset_add(&f->pending.first, p) || !frx_pset_add(&f->pending.last, p)) { 0 1844 1844
325 0 b.failed = 1;
326 0 break;
327 }
328 1844 f->pending.nullable = 0;
329 1844 f->have_pending = 1;
330 1844 continue;
331 }
332 1138 case '?': case '*': case '+': {
333 /* a modifier applies to the operand just read, which after a
334 * closing parenthesis is the group, and after the outermost
335 * one is the whole model */
336 frx_cm_part *p;
337 569 ▶ if (b.sp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (b.sp == 0) { 88 481 569
338 88 p = &root;
339 } else {
340 481 f = &b.stack[b.sp - 1];
341 481 ▶ p = f->have_pending ? &f->pending : &f->acc;
Branch: 1 branch
BranchTrueFalseTotal
p = f->have_pending ? &f->pending : &f->acc; 481 0 481
342 }
343 569 ▶ if (t->kind != '?' && !frx_cm_link(&b, &p->last, &p->first)) { b.failed = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (t->kind != '?' && !frx_cm_link(&b, &p->last, &p->first)) { b.failed = 1; break; } 386 183 569
if (t->kind != '?' && !frx_cm_link(&b, &p->last, &p->first)) { b.failed = 1; break; } 0 386 386
344 569 ▶ if (t->kind != '+') p->nullable = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (t->kind != '+') p->nullable = 1; 394 175 569
345 569 continue;
346 }
347 1221 case ',': case '|':
348 1221 ▶ if (b.sp == 0) { b.failed = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (b.sp == 0) { b.failed = 1; break; } 0 1221 1221
349 1221 f = &b.stack[b.sp - 1];
350 1221 ▶ if (!f->sep) f->sep = t->kind;
Branch: 1 branch
BranchTrueFalseTotal
if (!f->sep) f->sep = t->kind; 479 742 1221
351 1221 ▶ if (!frx_cm_fold_pending(&b, f)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_cm_fold_pending(&b, f)) break; 0 1221 1221
352 1221 continue;
353 1924 case ')': {
354 frx_cm_part done;
355 962 ▶ if (b.sp == 0) { b.failed = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (b.sp == 0) { b.failed = 1; break; } 0 962 962
356 962 f = &b.stack[b.sp - 1];
357 962 ▶ if (!frx_cm_fold_pending(&b, f)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_cm_fold_pending(&b, f)) break; 0 962 962
358 962 done = f->acc;
359 962 b.sp--;
360 962 ▶ if (b.sp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (b.sp == 0) { 623 339 962
361 623 root = done;
362 } else {
363 339 frx_cm_frame *up = &b.stack[b.sp - 1];
364 339 ▶ if (up->have_pending) frx_cm_part_free(&up->pending);
Branch: 1 branch
BranchTrueFalseTotal
if (up->have_pending) frx_cm_part_free(&up->pending); 0 339 339
365 339 up->pending = done;
366 339 up->have_pending = 1;
367 }
368 962 continue;
369 }
370 0 default:
371 0 continue;
372 }
373 0 break;
374 }
375  
376 623 ▶ if (b.failed) { rc = FRX_MODEL_NOMEM; goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (b.failed) { rc = FRX_MODEL_NOMEM; goto done; } 0 623 623
377  
378 /* the determinism check, section 3.2.1, stated over the sets */
379 623 ▶ if (!frx_model_deterministic(&b, &root.first)) { rc = FRX_MODEL_NONDET; goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_model_deterministic(&b, &root.first)) { rc = FRX_MODEL_NONDET; goto done; } 2 621 623
380 2457 ▶ for (i = 0; i < b.n_pos; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b.n_pos; i++) 1836 621 2457
381 1836 ▶ if (!frx_model_deterministic(&b, &b.follow[i])) { rc = FRX_MODEL_NONDET; goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_model_deterministic(&b, &b.follow[i])) { rc = FRX_MODEL_NONDET; goto done; } 0 1836 1836
382  
383 621 d = (frx_cm_dfa *)frx_arena_alloc(a, sizeof *d);
384 621 ▶ if (!d) { rc = FRX_MODEL_NOMEM; goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!d) { rc = FRX_MODEL_NOMEM; goto done; } 0 621 621
385 621 memset(d, 0, sizeof *d);
386 621 d->nullable = root.nullable;
387 621 d->n_pos = b.n_pos;
388 621 ▶ if (b.n_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (b.n_pos) { 621 0 621
389 621 frx_str *names = (frx_str *)frx_arena_alloc(a, (size_t)b.n_pos * sizeof *names);
390 621 const int **fol = (const int **)frx_arena_alloc(a, (size_t)b.n_pos * sizeof *fol);
391 621 int *nfol = (int *)frx_arena_alloc(a, (size_t)b.n_pos * sizeof *nfol);
392 621 ▶ if (!names || !fol || !nfol) { rc = FRX_MODEL_NOMEM; goto done; }
Branch: 3 branches
BranchTrueFalseTotal
if (!names || !fol || !nfol) { rc = FRX_MODEL_NOMEM; goto done; } 621 0 621
if (!names || !fol || !nfol) { rc = FRX_MODEL_NOMEM; goto done; } 621 0 621
if (!names || !fol || !nfol) { rc = FRX_MODEL_NOMEM; goto done; } 0 621 621
393 621 memcpy(names, b.name, (size_t)b.n_pos * sizeof *names);
394 2457 ▶ for (i = 0; i < b.n_pos; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b.n_pos; i++) { 1836 621 2457
395 1836 fol[i] = frx_model_copy_set(a, &b.follow[i], &nfol[i]);
396 1836 ▶ if (!fol[i] && nfol[i]) { rc = FRX_MODEL_NOMEM; goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!fol[i] && nfol[i]) { rc = FRX_MODEL_NOMEM; goto done; } 297 1539 1836
if (!fol[i] && nfol[i]) { rc = FRX_MODEL_NOMEM; goto done; } 0 297 297
397 }
398 621 d->name = names;
399 621 d->follow = fol;
400 621 d->n_follow = nfol;
401 }
402 621 d->first = frx_model_copy_set(a, &root.first, &d->n_first);
403 621 ▶ if (!d->first && d->n_first) { rc = FRX_MODEL_NOMEM; goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!d->first && d->n_first) { rc = FRX_MODEL_NOMEM; goto done; } 0 621 621
if (!d->first && d->n_first) { rc = FRX_MODEL_NOMEM; goto done; } 0 0 0
404 621 d->last = frx_model_copy_set(a, &root.last, &d->n_last);
405 621 ▶ if (!d->last && d->n_last) { rc = FRX_MODEL_NOMEM; goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!d->last && d->n_last) { rc = FRX_MODEL_NOMEM; goto done; } 0 621 621
if (!d->last && d->n_last) { rc = FRX_MODEL_NOMEM; goto done; } 0 0 0
406 621 *out = d;
407  
408 623 done:
409 623 frx_cm_part_free(&root);
410 623 frx_cm_build_free(&b);
411 623 return rc;
412 }
413  
414 /* ---- running it --------------------------------------------------------- */
415  
416 /* the position a name goes to from `at` (-1 for the start state), or -1
417 * when the name may not stand there */
418 static int
419 4130 frx_model_step(const frx_cm_dfa *d, int at, const frx_str *name)
420 {
421 4130 ▶ const int *set = at < 0 ? d->first : d->follow[at];
Branch: 1 branch
BranchTrueFalseTotal
const int *set = at < 0 ? d->first : d->follow[at]; 1492 2638 4130
422 4130 ▶ int n = at < 0 ? d->n_first : d->n_follow[at];
Branch: 1 branch
BranchTrueFalseTotal
int n = at < 0 ? d->n_first : d->n_follow[at]; 1492 2638 4130
423 int i;
424 6007 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 5989 18 6007
425 5989 ▶ if (frx_str_eq2(&d->name[set[i]], name)) return set[i];
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&d->name[set[i]], name)) return set[i]; 4112 1877 5989
426 18 return -1;
427 }
428  
429 static int
430 1529 frx_model_accepts(const frx_cm_dfa *d, int at)
431 {
432 int i;
433 1529 ▶ if (at < 0) return d->nullable;
Branch: 1 branch
BranchTrueFalseTotal
if (at < 0) return d->nullable; 57 1472 1529
434 2472 ▶ for (i = 0; i < d->n_last; i++) if (d->last[i] == at) return 1;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < d->n_last; i++) if (d->last[i] == at) return 1; 1469 1000 2469
for (i = 0; i < d->n_last; i++) if (d->last[i] == at) return 1; 2469 3 2472
435 3 return 0;
436 }
437  
438 /* mixed content: is this one of the names the model lists? */
439 static int
440 3154 frx_model_mixed_has(const frx_cm_dfa *d, const frx_str *name)
441 {
442 int i;
443 26570 ▶ for (i = 0; i < d->n_pos; i++) if (frx_str_eq2(&d->name[i], name)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < d->n_pos; i++) if (frx_str_eq2(&d->name[i], name)) return 1; 3151 23416 26567
for (i = 0; i < d->n_pos; i++) if (frx_str_eq2(&d->name[i], name)) return 1; 26567 3 26570
444 3 return 0;
445 }
446  
447 #endif /* FRX_MODEL_H */
« include/frx/frx_xpath_parse.h ↑ Summary include/frx/frx_arena.h »