| 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_abi.h, frx_arena.h, frx_tree.h, frx_parse.h, |
| 23 |
|
| * frx_xpath_lex.h, frx_xpath_parse.h, frx_xpath_eval.h. |
| 24 |
|
| * |
| 25 |
|
| * frx_xpath is opaque in frx_abi.h and defined here: the typedef is |
| 26 |
|
| * written once, there, since repeating it is a C11 feature and a C89 |
| 27 |
|
| * compiler refuses it. */ |
| 28 |
|
| |
| 29 |
|
| struct frx_xpath { |
| 30 |
|
| frx_arena arena; |
| 31 |
|
| frx_xn *ast; |
| 32 |
|
| frx_xp_var *vars; |
| 33 |
|
| int n_vars; |
| 34 |
|
| int max_depth; |
| 35 |
|
| }; |
| 36 |
|
| |
| 37 |
|
| static void |
| 38 |
238 |
| frx_xpath_free(frx_xpath *x) |
| 39 |
|
| { |
| 40 |
|
| int i; |
| 41 |
238 |
▶ | if (!x) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!x) return; |
0 |
238 |
238 |
|
| 42 |
248 |
▶ | for (i = 0; i < x->n_vars; i++) frx_xp_val_free(&x->vars[i].val); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < x->n_vars; i++) frx_xp_val_free(&x->vars[i].val); |
10 |
238 |
248 |
|
| 43 |
238 |
| free(x->vars); |
| 44 |
238 |
| frx_arena_free(&x->arena); |
| 45 |
238 |
| free(x); |
| 46 |
|
| } |
| 47 |
|
| |
| 48 |
|
| /* Compile expr under the caller's prefix map. NULL on refusal, with *err |
| 49 |
|
| * a static message and *err_at the byte offset in expr it was found at. |
| 50 |
|
| * max_depth of 0 means the default. */ |
| 51 |
|
| static frx_xpath * |
| 52 |
238 |
| frx_xpath_compile(const char *expr, size_t len, const frx_ns_map *ns, |
| 53 |
|
| int max_depth, const char **err, size_t *err_at) |
| 54 |
|
| { |
| 55 |
|
| frx_xpath *x; |
| 56 |
|
| frx_xlex lex; |
| 57 |
|
| frx_xparse ps; |
| 58 |
|
| frx_xvars vars; |
| 59 |
|
| int i; |
| 60 |
|
| |
| 61 |
238 |
| *err = NULL; |
| 62 |
238 |
| *err_at = 0; |
| 63 |
|
| |
| 64 |
238 |
| x = (frx_xpath *)malloc(sizeof *x); |
| 65 |
238 |
▶ | if (!x) { *err = "out of memory"; return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!x) { *err = "out of memory"; return NULL; } |
0 |
238 |
238 |
|
| 66 |
238 |
| memset(x, 0, sizeof *x); |
| 67 |
238 |
| frx_arena_init(&x->arena); |
| 68 |
238 |
▶ | x->max_depth = max_depth > 0 ? max_depth : FRX_XP_MAX_EXPR_DEPTH; |
Branch: 1 branch
| Branch | True | False | Total |
| x->max_depth = max_depth > 0 ? max_depth : FRX_XP_MAX_EXPR_DEPTH; |
2 |
236 |
238 |
|
| 69 |
|
| |
| 70 |
238 |
| frx_xlex_init(&lex, expr, len); |
| 71 |
238 |
▶ | if (!frx_xlex_run(&lex)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xlex_run(&lex)) { |
3 |
235 |
238 |
|
| 72 |
3 |
▶ | *err = lex.err ? lex.err : "the expression cannot be read"; |
Branch: 1 branch
| Branch | True | False | Total |
| *err = lex.err ? lex.err : "the expression cannot be read"; |
3 |
0 |
3 |
|
| 73 |
3 |
| *err_at = lex.err_at; |
| 74 |
3 |
| frx_xlex_free(&lex); |
| 75 |
3 |
| frx_xpath_free(x); |
| 76 |
3 |
| return NULL; |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
235 |
| memset(&vars, 0, sizeof vars); |
| 80 |
235 |
| memset(&ps, 0, sizeof ps); |
| 81 |
235 |
| ps.t = lex.t; |
| 82 |
235 |
| ps.n = lex.n; |
| 83 |
235 |
| ps.arena = &x->arena; |
| 84 |
235 |
| ps.ns = ns; |
| 85 |
235 |
| ps.vars = &vars; |
| 86 |
235 |
| ps.max_depth = x->max_depth; |
| 87 |
|
| |
| 88 |
235 |
| x->ast = frx_xp_expr(&ps); |
| 89 |
235 |
▶ | if (x->ast && ps.i != ps.n - 1) |
Branch: 2 branches
| Branch | True | False | Total |
| if (x->ast && ps.i != ps.n - 1) |
222 |
13 |
235 |
| if (x->ast && ps.i != ps.n - 1) |
2 |
220 |
222 |
|
| 90 |
2 |
| (void)frx_xp_fail(&ps, "the expression has more after its end"); |
| 91 |
235 |
▶ | if (!x->ast || ps.err) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!x->ast || ps.err) { |
222 |
13 |
235 |
| if (!x->ast || ps.err) { |
2 |
220 |
222 |
|
| 92 |
15 |
▶ | *err = ps.err ? ps.err : "the expression cannot be parsed"; |
Branch: 1 branch
| Branch | True | False | Total |
| *err = ps.err ? ps.err : "the expression cannot be parsed"; |
15 |
0 |
15 |
|
| 93 |
15 |
| *err_at = ps.err_at; |
| 94 |
15 |
| free(vars.name); |
| 95 |
15 |
| frx_xlex_free(&lex); |
| 96 |
15 |
| frx_xpath_free(x); |
| 97 |
15 |
| return NULL; |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
220 |
▶ | if (vars.n) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (vars.n) { |
9 |
211 |
220 |
|
| 101 |
9 |
| x->vars = (frx_xp_var *)malloc((size_t)vars.n * sizeof *x->vars); |
| 102 |
9 |
▶ | if (!x->vars) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!x->vars) { |
0 |
9 |
9 |
|
| 103 |
0 |
| *err = "out of memory"; |
| 104 |
0 |
| free(vars.name); |
| 105 |
0 |
| frx_xlex_free(&lex); |
| 106 |
0 |
| frx_xpath_free(x); |
| 107 |
0 |
| return NULL; |
| 108 |
|
| } |
| 109 |
19 |
▶ | for (i = 0; i < vars.n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < vars.n; i++) { |
10 |
9 |
19 |
|
| 110 |
10 |
| x->vars[i].name = vars.name[i]; /* in the arena, and it stays */ |
| 111 |
10 |
| frx_xp_val_init(&x->vars[i].val); |
| 112 |
10 |
| x->vars[i].val.kind = FRX_XV_STRING; |
| 113 |
|
| } |
| 114 |
9 |
| x->n_vars = vars.n; |
| 115 |
|
| } |
| 116 |
220 |
| free(vars.name); |
| 117 |
220 |
| frx_xlex_free(&lex); |
| 118 |
220 |
| return x; |
| 119 |
|
| } |
| 120 |
|
| |
| 121 |
|
| /* An evaluation result on the heap, for a caller that cannot see the |
| 122 |
|
| * struct. frx_xp_val_free releases what the result holds; these two put |
| 123 |
|
| * it on the heap and take it back, which is what the C ABI hands out. */ |
| 124 |
|
| static frx_xp_val * |
| 125 |
4 |
| frx_xpath_result_new(void) |
| 126 |
|
| { |
| 127 |
4 |
| frx_xp_val *v = (frx_xp_val *)malloc(sizeof *v); |
| 128 |
4 |
▶ | if (v) frx_xp_val_init(v); |
Branch: 1 branch
| Branch | True | False | Total |
| if (v) frx_xp_val_init(v); |
4 |
0 |
4 |
|
| 129 |
4 |
| return v; |
| 130 |
|
| } |
| 131 |
|
| |
| 132 |
|
| static void |
| 133 |
4 |
| frx_xpath_result_destroy(frx_xp_val *v) |
| 134 |
|
| { |
| 135 |
4 |
▶ | if (!v) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!v) return; |
0 |
4 |
4 |
|
| 136 |
4 |
| frx_xp_val_free(v); |
| 137 |
4 |
| free(v); |
| 138 |
|
| } |
| 139 |
|
| |
| 140 |
|
| static int |
| 141 |
218 |
| frx_xpath_var_count(const frx_xpath *x) |
| 142 |
|
| { |
| 143 |
218 |
| return x->n_vars; |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
|
| static const char * |
| 147 |
8 |
| frx_xpath_var_name(const frx_xpath *x, int i, size_t *len) |
| 148 |
|
| { |
| 149 |
8 |
▶ | if (len) *len = x->vars[i].name.len; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len) *len = x->vars[i].name.len; |
8 |
0 |
8 |
|
| 150 |
8 |
| return x->vars[i].name.p; |
| 151 |
|
| } |
| 152 |
|
| |
| 153 |
|
| static int |
| 154 |
3 |
| frx_xpath_bind_str(frx_xpath *x, int i, const char *p, size_t n) |
| 155 |
|
| { |
| 156 |
3 |
| frx_xp_val *v = &x->vars[i].val; |
| 157 |
3 |
| v->kind = FRX_XV_STRING; |
| 158 |
3 |
| frx_xp_buf_reset(&v->str); |
| 159 |
3 |
| frx_buf_append_n(&v->str, p, n); |
| 160 |
3 |
| return !v->str.failed; |
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
|
| static void |
| 164 |
2 |
| frx_xpath_bind_num(frx_xpath *x, int i, double d) |
| 165 |
|
| { |
| 166 |
2 |
| frx_xp_val *v = &x->vars[i].val; |
| 167 |
2 |
| v->kind = FRX_XV_NUMBER; |
| 168 |
2 |
| v->num = d; |
| 169 |
2 |
| } |
| 170 |
|
| |
| 171 |
|
| static void |
| 172 |
1 |
| frx_xpath_bind_bool(frx_xpath *x, int i, int b) |
| 173 |
|
| { |
| 174 |
1 |
| frx_xp_val *v = &x->vars[i].val; |
| 175 |
1 |
| v->kind = FRX_XV_BOOLEAN; |
| 176 |
1 |
| v->bl = b != 0; |
| 177 |
1 |
| } |
| 178 |
|
| |
| 179 |
|
| /* Evaluate against ctx, a node of d. `out` is filled and the caller frees |
| 180 |
|
| * it with frx_xp_val_free. 0 with *err set on a failure, which is an |
| 181 |
|
| * allocation or one of the type errors the specification calls an error. */ |
| 182 |
|
| static int |
| 183 |
219 |
| frx_xpath_eval_at(frx_xpath *x, frx_doc *d, const frx_node *ctx, |
| 184 |
|
| frx_xp_val *out, const char **err) |
| 185 |
|
| { |
| 186 |
|
| frx_xeval e; |
| 187 |
|
| frx_xctx c; |
| 188 |
|
| |
| 189 |
219 |
| *err = NULL; |
| 190 |
219 |
| frx_xp_val_init(out); |
| 191 |
219 |
▶ | if (d) frx_doc_renumber(d); /* frx_tree.h's; a no-op unless an edit set the flag */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (d) frx_doc_renumber(d); /* frx_tree.h's; a no-op unless an edit set the flag */ |
219 |
0 |
219 |
|
| 192 |
|
| |
| 193 |
219 |
| memset(&e, 0, sizeof e); |
| 194 |
219 |
| e.doc = d; |
| 195 |
219 |
| e.vars = x->vars; |
| 196 |
219 |
| e.n_vars = x->n_vars; |
| 197 |
|
| |
| 198 |
219 |
| memset(&c, 0, sizeof c); |
| 199 |
219 |
| c.item.node = ctx; |
| 200 |
219 |
| c.item.kind = FRX_XP_KNODE; |
| 201 |
219 |
| c.item.index = 0; |
| 202 |
219 |
| c.position = 1; |
| 203 |
219 |
| c.size = 1; |
| 204 |
|
| |
| 205 |
219 |
▶ | if (!frx_xp_eval(&e, x->ast, &c, out) || e.failed) { |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 206 |
2 |
▶ | *err = e.err ? e.err : "the expression could not be evaluated"; |
Branch: 1 branch
| Branch | True | False | Total |
| *err = e.err ? e.err : "the expression could not be evaluated"; |
2 |
0 |
2 |
|
| 207 |
2 |
| frx_xp_val_free(out); |
| 208 |
2 |
| frx_xp_val_init(out); |
| 209 |
2 |
| return 0; |
| 210 |
|
| } |
| 211 |
217 |
| return 1; |
| 212 |
|
| } |
| 213 |
|
| |
| 214 |
|
| /* the compile refusal in the one message shape, over the expression */ |
| 215 |
|
| static size_t |
| 216 |
17 |
| frx_xpath_err_format(const char *what, const char *expr, size_t len, size_t at, |
| 217 |
|
| char *out, size_t cap) |
| 218 |
|
| { |
| 219 |
|
| frx_err e; |
| 220 |
17 |
| frx_err_set(&e, FRX_E_SYNTAX, at, what); |
| 221 |
17 |
| return frx_err_format(&e, expr, len, out, cap); |
| 222 |
|
| } |
| 223 |
|
| |
| 224 |
|
| #endif /* FRX_XPATH_H */ |