include/frx/frx_xpath.h

filter: Statementstmt 95.3% 102 / 107 Branchbran 73.8% 31 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.2% 133 / 149 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_validate.h ↑ Summary include/frx/frx_dtd.h »
1 #ifndef FRX_XPATH_H
2 #define FRX_XPATH_H
3  
4 /* A compiled XPath 1.0 expression: compile once, evaluate many times.
5 *
6 * The compiled object owns an arena holding its AST and every string in
7 * it, and a variable table the caller fills before evaluating. It is
8 * independent of any document: the same object evaluates against as many
9 * documents as the caller has, and holds none of them alive.
10 *
11 * The prefix map is the caller's and is read only at compile. Nothing
12 * here ever reads a document's own namespace declarations, so an
13 * expression means the same thing wherever it is evaluated.
14 *
15 * `order` IS RENUMBERED HERE. Document order is what a node-set is
16 * sorted by, and an edit (frx_edit.h) marks it stale rather than paying
17 * to keep it current, so every evaluation calls frx_tree.h's
18 * frx_doc_renumber first. It walks the document once and costs nothing
19 * when no edit has happened. That call is the only thing under this
20 * header that writes to a document.
21 *
22 * Needs frx_arena.h, frx_tree.h, frx_parse.h, frx_xpath_lex.h,
23 * frx_xpath_parse.h, frx_xpath_eval.h. */
24  
25 typedef struct frx_xpath {
26 frx_arena arena;
27 frx_xn *ast;
28 frx_xp_var *vars;
29 int n_vars;
30 int max_depth;
31 } frx_xpath;
32  
33 static void
34 238 frx_xpath_free(frx_xpath *x)
35 {
36 int i;
37 238 ▶ if (!x) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!x) return; 0 238 238
38 248 ▶ for (i = 0; i < x->n_vars; i++) frx_xp_val_free(&x->vars[i].val);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < x->n_vars; i++) frx_xp_val_free(&x->vars[i].val); 10 238 248
39 238 free(x->vars);
40 238 frx_arena_free(&x->arena);
41 238 free(x);
42 }
43  
44 /* Compile expr under the caller's prefix map. NULL on refusal, with *err
45 * a static message and *err_at the byte offset in expr it was found at.
46 * max_depth of 0 means the default. */
47 static frx_xpath *
48 238 frx_xpath_compile(const char *expr, size_t len, const frx_ns_map *ns,
49 int max_depth, const char **err, size_t *err_at)
50 {
51 frx_xpath *x;
52 frx_xlex lex;
53 frx_xparse ps;
54 frx_xvars vars;
55 int i;
56  
57 238 *err = NULL;
58 238 *err_at = 0;
59  
60 238 x = (frx_xpath *)malloc(sizeof *x);
61 238 ▶ if (!x) { *err = "out of memory"; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!x) { *err = "out of memory"; return NULL; } 0 238 238
62 238 memset(x, 0, sizeof *x);
63 238 frx_arena_init(&x->arena);
64 238 ▶ x->max_depth = max_depth > 0 ? max_depth : FRX_XP_MAX_EXPR_DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
x->max_depth = max_depth > 0 ? max_depth : FRX_XP_MAX_EXPR_DEPTH; 2 236 238
65  
66 238 frx_xlex_init(&lex, expr, len);
67 238 ▶ if (!frx_xlex_run(&lex)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xlex_run(&lex)) { 3 235 238
68 3 ▶ *err = lex.err ? lex.err : "the expression cannot be read";
Branch: 1 branch
BranchTrueFalseTotal
*err = lex.err ? lex.err : "the expression cannot be read"; 3 0 3
69 3 *err_at = lex.err_at;
70 3 frx_xlex_free(&lex);
71 3 frx_xpath_free(x);
72 3 return NULL;
73 }
74  
75 235 memset(&vars, 0, sizeof vars);
76 235 memset(&ps, 0, sizeof ps);
77 235 ps.t = lex.t;
78 235 ps.n = lex.n;
79 235 ps.arena = &x->arena;
80 235 ps.ns = ns;
81 235 ps.vars = &vars;
82 235 ps.max_depth = x->max_depth;
83  
84 235 x->ast = frx_xp_expr(&ps);
85 235 ▶ if (x->ast && ps.i != ps.n - 1)
Branch: 2 branches
BranchTrueFalseTotal
if (x->ast && ps.i != ps.n - 1) 222 13 235
if (x->ast && ps.i != ps.n - 1) 2 220 222
86 2 (void)frx_xp_fail(&ps, "the expression has more after its end");
87 235 ▶ if (!x->ast || ps.err) {
Branch: 2 branches
BranchTrueFalseTotal
if (!x->ast || ps.err) { 222 13 235
if (!x->ast || ps.err) { 2 220 222
88 15 ▶ *err = ps.err ? ps.err : "the expression cannot be parsed";
Branch: 1 branch
BranchTrueFalseTotal
*err = ps.err ? ps.err : "the expression cannot be parsed"; 15 0 15
89 15 *err_at = ps.err_at;
90 15 free(vars.name);
91 15 frx_xlex_free(&lex);
92 15 frx_xpath_free(x);
93 15 return NULL;
94 }
95  
96 220 ▶ if (vars.n) {
Branch: 1 branch
BranchTrueFalseTotal
if (vars.n) { 9 211 220
97 9 x->vars = (frx_xp_var *)malloc((size_t)vars.n * sizeof *x->vars);
98 9 ▶ if (!x->vars) {
Branch: 1 branch
BranchTrueFalseTotal
if (!x->vars) { 0 9 9
99 0 *err = "out of memory";
100 0 free(vars.name);
101 0 frx_xlex_free(&lex);
102 0 frx_xpath_free(x);
103 0 return NULL;
104 }
105 19 ▶ for (i = 0; i < vars.n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < vars.n; i++) { 10 9 19
106 10 x->vars[i].name = vars.name[i]; /* in the arena, and it stays */
107 10 frx_xp_val_init(&x->vars[i].val);
108 10 x->vars[i].val.kind = FRX_XV_STRING;
109 }
110 9 x->n_vars = vars.n;
111 }
112 220 free(vars.name);
113 220 frx_xlex_free(&lex);
114 220 return x;
115 }
116  
117 /* An evaluation result on the heap, for a caller that cannot see the
118 * struct. frx_xp_val_free releases what the result holds; these two put
119 * it on the heap and take it back, which is what the C ABI hands out. */
120 static frx_xp_val *
121 4 frx_xpath_result_new(void)
122 {
123 4 frx_xp_val *v = (frx_xp_val *)malloc(sizeof *v);
124 4 ▶ if (v) frx_xp_val_init(v);
Branch: 1 branch
BranchTrueFalseTotal
if (v) frx_xp_val_init(v); 4 0 4
125 4 return v;
126 }
127  
128 static void
129 4 frx_xpath_result_destroy(frx_xp_val *v)
130 {
131 4 ▶ if (!v) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return; 0 4 4
132 4 frx_xp_val_free(v);
133 4 free(v);
134 }
135  
136 static int
137 218 frx_xpath_var_count(const frx_xpath *x)
138 {
139 218 return x->n_vars;
140 }
141  
142 static const char *
143 8 frx_xpath_var_name(const frx_xpath *x, int i, size_t *len)
144 {
145 8 ▶ if (len) *len = x->vars[i].name.len;
Branch: 1 branch
BranchTrueFalseTotal
if (len) *len = x->vars[i].name.len; 8 0 8
146 8 return x->vars[i].name.p;
147 }
148  
149 static int
150 3 frx_xpath_bind_str(frx_xpath *x, int i, const char *p, size_t n)
151 {
152 3 frx_xp_val *v = &x->vars[i].val;
153 3 v->kind = FRX_XV_STRING;
154 3 frx_xp_buf_reset(&v->str);
155 3 frx_buf_append_n(&v->str, p, n);
156 3 return !v->str.failed;
157 }
158  
159 static void
160 2 frx_xpath_bind_num(frx_xpath *x, int i, double d)
161 {
162 2 frx_xp_val *v = &x->vars[i].val;
163 2 v->kind = FRX_XV_NUMBER;
164 2 v->num = d;
165 2 }
166  
167 static void
168 1 frx_xpath_bind_bool(frx_xpath *x, int i, int b)
169 {
170 1 frx_xp_val *v = &x->vars[i].val;
171 1 v->kind = FRX_XV_BOOLEAN;
172 1 v->bl = b != 0;
173 1 }
174  
175 /* Evaluate against ctx, a node of d. `out` is filled and the caller frees
176 * it with frx_xp_val_free. 0 with *err set on a failure, which is an
177 * allocation or one of the type errors the specification calls an error. */
178 static int
179 219 frx_xpath_eval_at(frx_xpath *x, frx_doc *d, const frx_node *ctx,
180 frx_xp_val *out, const char **err)
181 {
182 frx_xeval e;
183 frx_xctx c;
184  
185 219 *err = NULL;
186 219 frx_xp_val_init(out);
187 219 ▶ if (d) frx_doc_renumber(d); /* frx_tree.h's; a no-op unless an edit set the flag */
Branch: 1 branch
BranchTrueFalseTotal
if (d) frx_doc_renumber(d); /* frx_tree.h's; a no-op unless an edit set the flag */ 219 0 219
188  
189 219 memset(&e, 0, sizeof e);
190 219 e.doc = d;
191 219 e.vars = x->vars;
192 219 e.n_vars = x->n_vars;
193  
194 219 memset(&c, 0, sizeof c);
195 219 c.item.node = ctx;
196 219 c.item.kind = FRX_XP_KNODE;
197 219 c.item.index = 0;
198 219 c.position = 1;
199 219 c.size = 1;
200  
201 219 ▶ if (!frx_xp_eval(&e, x->ast, &c, out) || e.failed) {
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_xp_eval(&e, x->ast, &c, out) || e.failed) { 217 2 219
if (!frx_xp_eval(&e, x->ast, &c, out) || e.failed) { 0 217 217
202 2 ▶ *err = e.err ? e.err : "the expression could not be evaluated";
Branch: 1 branch
BranchTrueFalseTotal
*err = e.err ? e.err : "the expression could not be evaluated"; 2 0 2
203 2 frx_xp_val_free(out);
204 2 frx_xp_val_init(out);
205 2 return 0;
206 }
207 217 return 1;
208 }
209  
210 /* the compile refusal in the one message shape, over the expression */
211 static size_t
212 17 frx_xpath_err_format(const char *what, const char *expr, size_t len, size_t at,
213 char *out, size_t cap)
214 {
215 frx_err e;
216 17 frx_err_set(&e, FRX_E_SYNTAX, at, what);
217 17 return frx_err_format(&e, expr, len, out, cap);
218 }
219  
220 #endif /* FRX_XPATH_H */
« include/frx/frx_validate.h ↑ Summary include/frx/frx_dtd.h »