| 1 |
|
| #ifndef FRX_XPATH_PARSE_H |
| 2 |
|
| #define FRX_XPATH_PARSE_H |
| 3 |
|
| |
| 4 |
|
| /* The XPath 1.0 grammar, section 3, to an AST in the expression's arena. |
| 5 |
|
| * |
| 6 |
|
| * Recursive descent. Recursion over the expression is allowed, unlike |
| 7 |
|
| * recursion over a document (rule 2), because the expression's depth is |
| 8 |
|
| * the caller's own text and not an attacker's input; `max_expr_depth` |
| 9 |
|
| * (default 64) bounds it here so the evaluator, which recurses over the |
| 10 |
|
| * same AST, inherits the same bound. |
| 11 |
|
| * |
| 12 |
|
| * Every string an AST node holds is copied into the arena, so a compiled |
| 13 |
|
| * expression outlives the bytes it was compiled from and is independent |
| 14 |
|
| * of any document. |
| 15 |
|
| * |
| 16 |
|
| * NAME RESOLUTION IS AT COMPILE. A prefix in a name test is looked up in |
| 17 |
|
| * the caller's map and stored as a URI; an unmapped prefix is a compile |
| 18 |
|
| * error. There is no mode that reads the document's own declarations, |
| 19 |
|
| * which would make an expression's meaning depend on where it is |
| 20 |
|
| * evaluated. An unprefixed name test resolves to no namespace, which is |
| 21 |
|
| * XPath 1.0 section 2.3: a QName without a prefix expands to a null |
| 22 |
|
| * namespace URI, never to the default one. |
| 23 |
|
| * |
| 24 |
|
| * The abbreviations of section 2.5 are expanded here and nowhere else: |
| 25 |
|
| * `//` becomes `/descendant-or-self::node()/`, `.` becomes `self::node()`, |
| 26 |
|
| * `..` becomes `parent::node()`, and `@x` becomes `attribute::x`. That is |
| 27 |
|
| * why `//a[1]` is not the first `a` in the document: the predicate belongs |
| 28 |
|
| * to the `child::a` step, whose proximity position is among one parent's |
| 29 |
|
| * children. |
| 30 |
|
| * |
| 31 |
|
| * Needs frx_arena.h, frx_utf8.h, frx_tree.h, frx_xpath_lex.h. */ |
| 32 |
|
| |
| 33 |
|
| #define FRX_XP_MAX_EXPR_DEPTH 64 |
| 34 |
|
| |
| 35 |
|
| /* frx_ns_binding and frx_ns_map are frx_abi.h's: a consumer compiles an |
| 36 |
|
| * expression under one, so their shape is public. */ |
| 37 |
|
| |
| 38 |
|
| /* axes, section 2.2 */ |
| 39 |
|
| enum { |
| 40 |
|
| FRX_AX_CHILD = 0, FRX_AX_DESCENDANT, FRX_AX_PARENT, FRX_AX_ANCESTOR, |
| 41 |
|
| FRX_AX_FOLLOWING_SIBLING, FRX_AX_PRECEDING_SIBLING, FRX_AX_FOLLOWING, |
| 42 |
|
| FRX_AX_PRECEDING, FRX_AX_ATTRIBUTE, FRX_AX_NAMESPACE, FRX_AX_SELF, |
| 43 |
|
| FRX_AX_DESCENDANT_OR_SELF, FRX_AX_ANCESTOR_OR_SELF |
| 44 |
|
| }; |
| 45 |
|
| |
| 46 |
|
| /* node tests, section 2.3 */ |
| 47 |
|
| enum { |
| 48 |
|
| FRX_XTEST_NAME = 0, /* ns + local */ |
| 49 |
|
| FRX_XTEST_STAR, /* * */ |
| 50 |
|
| FRX_XTEST_NSSTAR, /* prefix:* (ns only) */ |
| 51 |
|
| FRX_XTEST_NODE, /* node() */ |
| 52 |
|
| FRX_XTEST_TEXT, /* text() */ |
| 53 |
|
| FRX_XTEST_COMMENT, /* comment() */ |
| 54 |
|
| FRX_XTEST_PI /* processing-instruction(target?) */ |
| 55 |
|
| }; |
| 56 |
|
| |
| 57 |
|
| /* the 27 core functions of section 4 */ |
| 58 |
|
| enum { |
| 59 |
|
| FRX_FN_LAST = 1, FRX_FN_POSITION, FRX_FN_COUNT, FRX_FN_ID, |
| 60 |
|
| FRX_FN_LOCAL_NAME, FRX_FN_NAMESPACE_URI, FRX_FN_NAME, |
| 61 |
|
| FRX_FN_STRING, FRX_FN_CONCAT, FRX_FN_STARTS_WITH, FRX_FN_CONTAINS, |
| 62 |
|
| FRX_FN_SUBSTRING_BEFORE, FRX_FN_SUBSTRING_AFTER, FRX_FN_SUBSTRING, |
| 63 |
|
| FRX_FN_STRING_LENGTH, FRX_FN_NORMALIZE_SPACE, FRX_FN_TRANSLATE, |
| 64 |
|
| FRX_FN_BOOLEAN, FRX_FN_NOT, FRX_FN_TRUE, FRX_FN_FALSE, FRX_FN_LANG, |
| 65 |
|
| FRX_FN_NUMBER, FRX_FN_SUM, FRX_FN_FLOOR, FRX_FN_CEILING, FRX_FN_ROUND |
| 66 |
|
| }; |
| 67 |
|
| |
| 68 |
|
| /* AST node kinds */ |
| 69 |
|
| enum { |
| 70 |
|
| FRX_XN_OR = 1, FRX_XN_AND, |
| 71 |
|
| FRX_XN_EQ, FRX_XN_NE, FRX_XN_LT, FRX_XN_LE, FRX_XN_GT, FRX_XN_GE, |
| 72 |
|
| FRX_XN_ADD, FRX_XN_SUB, FRX_XN_MUL, FRX_XN_DIV, FRX_XN_MOD, |
| 73 |
|
| FRX_XN_UNION, FRX_XN_NEG, |
| 74 |
|
| FRX_XN_PATH, FRX_XN_STEP, |
| 75 |
|
| FRX_XN_LITERAL, FRX_XN_NUMBER, FRX_XN_VAR, FRX_XN_FUNC, FRX_XN_FILTER |
| 76 |
|
| }; |
| 77 |
|
| |
| 78 |
|
| typedef struct frx_xn frx_xn; |
| 79 |
|
| struct frx_xn { |
| 80 |
|
| int kind; |
| 81 |
|
| frx_xn *a, *b; /* operands; FILTER's primary; PATH's leading filter */ |
| 82 |
|
| int axis; /* STEP */ |
| 83 |
|
| int test; /* STEP */ |
| 84 |
|
| frx_str ns, local; /* STEP: the resolved name test */ |
| 85 |
|
| frx_xn **preds; /* STEP, FILTER */ |
| 86 |
|
| int n_preds; |
| 87 |
|
| frx_xn **steps; /* PATH */ |
| 88 |
|
| int n_steps; |
| 89 |
|
| int absolute; /* PATH: begins at the root */ |
| 90 |
|
| frx_str str; /* LITERAL, VAR's name, PI target */ |
| 91 |
|
| double num; /* NUMBER */ |
| 92 |
|
| int func; /* FUNC */ |
| 93 |
|
| int var; /* VAR: index into the expression's variables */ |
| 94 |
|
| frx_xn **args; /* FUNC */ |
| 95 |
|
| int n_args; |
| 96 |
|
| int d; /* this subtree's depth, capped at max_expr_depth */ |
| 97 |
|
| }; |
| 98 |
|
| |
| 99 |
|
| /* what the parser collects beside the AST: every variable named, once, |
| 100 |
|
| * so the compiled expression can carry one value per name */ |
| 101 |
|
| typedef struct frx_xvars { |
| 102 |
|
| frx_str *name; |
| 103 |
|
| int n, cap; |
| 104 |
|
| } frx_xvars; |
| 105 |
|
| |
| 106 |
|
| typedef struct frx_xparse { |
| 107 |
|
| const frx_xtok *t; |
| 108 |
|
| int n, i; |
| 109 |
|
| frx_arena *arena; |
| 110 |
|
| const frx_ns_map *ns; |
| 111 |
|
| frx_xvars *vars; |
| 112 |
|
| int depth, max_depth; |
| 113 |
|
| const char *err; |
| 114 |
|
| size_t err_at; |
| 115 |
|
| } frx_xparse; |
| 116 |
|
| |
| 117 |
|
| static frx_xn *frx_xp_expr(frx_xparse *p); |
| 118 |
|
| |
| 119 |
|
| static void * |
| 120 |
15 |
| frx_xp_fail(frx_xparse *p, const char *what) |
| 121 |
|
| { |
| 122 |
15 |
▶ | if (!p->err) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p->err) { |
15 |
0 |
15 |
|
| 123 |
15 |
| p->err = what; |
| 124 |
15 |
▶ | p->err_at = p->i < p->n ? p->t[p->i].offset : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| p->err_at = p->i < p->n ? p->t[p->i].offset : 0; |
15 |
0 |
15 |
|
| 125 |
|
| } |
| 126 |
15 |
| return NULL; |
| 127 |
|
| } |
| 128 |
|
| |
| 129 |
|
| static const frx_xtok * |
| 130 |
21273 |
| frx_xp_peek(const frx_xparse *p, int ahead) |
| 131 |
|
| { |
| 132 |
21273 |
| int k = p->i + ahead; |
| 133 |
21273 |
▶ | return k < p->n ? &p->t[k] : &p->t[p->n - 1]; /* the EOF token */ |
Branch: 1 branch
| Branch | True | False | Total |
| return k < p->n ? &p->t[k] : &p->t[p->n - 1]; /* the EOF token */ |
21273 |
0 |
21273 |
|
| 134 |
|
| } |
| 135 |
|
| |
| 136 |
|
| static int |
| 137 |
18266 |
| frx_xp_at(const frx_xparse *p, int kind) |
| 138 |
|
| { |
| 139 |
18266 |
| return frx_xp_peek(p, 0)->kind == kind; |
| 140 |
|
| } |
| 141 |
|
| |
| 142 |
|
| static int |
| 143 |
1319 |
| frx_xp_eat(frx_xparse *p, int kind) |
| 144 |
|
| { |
| 145 |
1319 |
▶ | if (!frx_xp_at(p, kind)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_at(p, kind)) return 0; |
711 |
608 |
1319 |
|
| 146 |
608 |
| p->i++; |
| 147 |
608 |
| return 1; |
| 148 |
|
| } |
| 149 |
|
| |
| 150 |
|
| static frx_xn * |
| 151 |
2383 |
| frx_xp_new(frx_xparse *p, int kind) |
| 152 |
|
| { |
| 153 |
2383 |
| frx_xn *n = (frx_xn *)frx_arena_alloc(p->arena, sizeof *n); |
| 154 |
2383 |
▶ | if (!n) return (frx_xn *)frx_xp_fail(p, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return (frx_xn *)frx_xp_fail(p, "out of memory"); |
0 |
2383 |
2383 |
|
| 155 |
2383 |
| memset(n, 0, sizeof *n); |
| 156 |
2383 |
| n->kind = kind; |
| 157 |
2383 |
| n->d = 1; |
| 158 |
2383 |
| n->ns = n->local = n->str = FRX_EMPTY_STR; |
| 159 |
2383 |
| return n; |
| 160 |
|
| } |
| 161 |
|
| |
| 162 |
|
| /* The AST's own depth, which is what the evaluator recurses down and is |
| 163 |
|
| * not the same as the parser's. `a and b and c and ...` is one loop here |
| 164 |
|
| * and a left-leaning chain there, so counting productions alone would let |
| 165 |
|
| * a long chain of associative operators reach a stack the parser never |
| 166 |
|
| * touched. Every composite node passes through this on the way out. */ |
| 167 |
|
| static frx_xn * |
| 168 |
1715 |
| frx_xp_depth_ok(frx_xparse *p, frx_xn *n) |
| 169 |
|
| { |
| 170 |
1715 |
| int i, d = 0; |
| 171 |
1715 |
▶ | if (!n) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return NULL; |
0 |
1715 |
1715 |
|
| 172 |
1715 |
▶ | if (n->a && n->a->d > d) d = n->a->d; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n->a && n->a->d > d) d = n->a->d; |
1149 |
566 |
1715 |
| if (n->a && n->a->d > d) d = n->a->d; |
1149 |
0 |
1149 |
|
| 173 |
1715 |
▶ | if (n->b && n->b->d > d) d = n->b->d; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n->b && n->b->d > d) d = n->b->d; |
321 |
1394 |
1715 |
| if (n->b && n->b->d > d) d = n->b->d; |
2 |
319 |
321 |
|
| 174 |
1741 |
▶ | for (i = 0; i < n->n_preds; i++) if (n->preds[i]->d > d) d = n->preds[i]->d; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < n->n_preds; i++) if (n->preds[i]->d > d) d = n->preds[i]->d; |
23 |
3 |
26 |
| for (i = 0; i < n->n_preds; i++) if (n->preds[i]->d > d) d = n->preds[i]->d; |
26 |
1715 |
1741 |
|
| 175 |
2008 |
▶ | for (i = 0; i < n->n_args; i++) if (n->args[i]->d > d) d = n->args[i]->d; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < n->n_args; i++) if (n->args[i]->d > d) d = n->args[i]->d; |
256 |
37 |
293 |
| for (i = 0; i < n->n_args; i++) if (n->args[i]->d > d) d = n->args[i]->d; |
293 |
1715 |
2008 |
|
| 176 |
1988 |
▶ | for (i = 0; i < n->n_steps; i++) if (n->steps[i]->d > d) d = n->steps[i]->d; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < n->n_steps; i++) if (n->steps[i]->d > d) d = n->steps[i]->d; |
148 |
125 |
273 |
| for (i = 0; i < n->n_steps; i++) if (n->steps[i]->d > d) d = n->steps[i]->d; |
273 |
1715 |
1988 |
|
| 177 |
1715 |
| n->d = d + 1; |
| 178 |
1715 |
▶ | if (n->d > p->max_depth) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->d > p->max_depth) |
1 |
1714 |
1715 |
|
| 179 |
1 |
| return (frx_xn *)frx_xp_fail(p, "the expression nests deeper than max_expr_depth"); |
| 180 |
1714 |
| return n; |
| 181 |
|
| } |
| 182 |
|
| |
| 183 |
|
| /* a growable array of frx_xn * in the arena: the arena never reallocates, |
| 184 |
|
| * so growth allocates a new block and the old one is simply left behind. |
| 185 |
|
| * Every list here is a handful of entries. */ |
| 186 |
|
| static int |
| 187 |
607 |
| frx_xp_push(frx_xparse *p, frx_xn ***v, int *n, int *cap, frx_xn *item) |
| 188 |
|
| { |
| 189 |
607 |
▶ | if (!item) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!item) return 0; |
8 |
599 |
607 |
|
| 190 |
599 |
▶ | if (*n == *cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*n == *cap) { |
529 |
70 |
599 |
|
| 191 |
529 |
▶ | int ncap = *cap ? *cap * 2 : 4; |
Branch: 1 branch
| Branch | True | False | Total |
| int ncap = *cap ? *cap * 2 : 4; |
121 |
408 |
529 |
|
| 192 |
529 |
| frx_xn **nv = (frx_xn **)frx_arena_alloc(p->arena, (size_t)ncap * sizeof *nv); |
| 193 |
529 |
▶ | if (!nv) { (void)frx_xp_fail(p, "out of memory"); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nv) { (void)frx_xp_fail(p, "out of memory"); return 0; } |
0 |
529 |
529 |
|
| 194 |
529 |
▶ | if (*n) memcpy(nv, *v, (size_t)*n * sizeof *nv); |
Branch: 1 branch
| Branch | True | False | Total |
| if (*n) memcpy(nv, *v, (size_t)*n * sizeof *nv); |
121 |
408 |
529 |
|
| 195 |
529 |
| *v = nv; |
| 196 |
529 |
| *cap = ncap; |
| 197 |
|
| } |
| 198 |
599 |
| (*v)[(*n)++] = item; |
| 199 |
599 |
| return 1; |
| 200 |
|
| } |
| 201 |
|
| |
| 202 |
|
| static frx_str |
| 203 |
247 |
| frx_xp_dup(frx_xparse *p, const frx_str *s) |
| 204 |
|
| { |
| 205 |
247 |
▶ | frx_str out = frx_arena_strndup(p->arena, s->p ? s->p : "", s->len); |
Branch: 1 branch
| Branch | True | False | Total |
| frx_str out = frx_arena_strndup(p->arena, s->p ? s->p : "", s->len); |
247 |
0 |
247 |
|
| 206 |
247 |
▶ | if (!out.p) (void)frx_xp_fail(p, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!out.p) (void)frx_xp_fail(p, "out of memory"); |
0 |
247 |
247 |
|
| 207 |
247 |
| return out; |
| 208 |
|
| } |
| 209 |
|
| |
| 210 |
|
| /* the URI the caller's map binds to prefix; NULL when it binds none */ |
| 211 |
|
| static const char * |
| 212 |
15 |
| frx_xp_ns_uri(const frx_ns_map *m, const char *prefix, size_t plen) |
| 213 |
|
| { |
| 214 |
|
| int i; |
| 215 |
15 |
▶ | if (plen == 3 && memcmp(prefix, "xml", 3) == 0) return FRX_XML_NS; |
Branch: 2 branches
| Branch | True | False | Total |
| if (plen == 3 && memcmp(prefix, "xml", 3) == 0) return FRX_XML_NS; |
0 |
15 |
15 |
| if (plen == 3 && memcmp(prefix, "xml", 3) == 0) return FRX_XML_NS; |
0 |
0 |
0 |
|
| 216 |
15 |
▶ | if (!m) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) return NULL; |
1 |
14 |
15 |
|
| 217 |
18 |
▶ | for (i = 0; i < m->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < m->n; i++) { |
17 |
1 |
18 |
|
| 218 |
17 |
▶ | const char *k = m->v[i].prefix ? m->v[i].prefix : ""; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *k = m->v[i].prefix ? m->v[i].prefix : ""; |
17 |
0 |
17 |
|
| 219 |
17 |
▶ | if (strlen(k) == plen && (plen == 0 || memcmp(k, prefix, plen) == 0)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (strlen(k) == plen && (plen == 0 || memcmp(k, prefix, plen) == 0)) |
17 |
0 |
17 |
| if (strlen(k) == plen && (plen == 0 || memcmp(k, prefix, plen) == 0)) |
17 |
0 |
17 |
| if (strlen(k) == plen && (plen == 0 || memcmp(k, prefix, plen) == 0)) |
13 |
4 |
17 |
|
| 220 |
13 |
▶ | return m->v[i].uri ? m->v[i].uri : ""; |
Branch: 1 branch
| Branch | True | False | Total |
| return m->v[i].uri ? m->v[i].uri : ""; |
13 |
0 |
13 |
|
| 221 |
|
| } |
| 222 |
1 |
| return NULL; |
| 223 |
|
| } |
| 224 |
|
| |
| 225 |
|
| static int |
| 226 |
24 |
| frx_xp_axis_by_name(const frx_str *s) |
| 227 |
|
| { |
| 228 |
|
| static const struct { const char *n; int a; } A[] = { |
| 229 |
|
| { "ancestor", FRX_AX_ANCESTOR }, |
| 230 |
|
| { "ancestor-or-self", FRX_AX_ANCESTOR_OR_SELF }, |
| 231 |
|
| { "attribute", FRX_AX_ATTRIBUTE }, |
| 232 |
|
| { "child", FRX_AX_CHILD }, |
| 233 |
|
| { "descendant", FRX_AX_DESCENDANT }, |
| 234 |
|
| { "descendant-or-self", FRX_AX_DESCENDANT_OR_SELF }, |
| 235 |
|
| { "following", FRX_AX_FOLLOWING }, |
| 236 |
|
| { "following-sibling", FRX_AX_FOLLOWING_SIBLING }, |
| 237 |
|
| { "namespace", FRX_AX_NAMESPACE }, |
| 238 |
|
| { "parent", FRX_AX_PARENT }, |
| 239 |
|
| { "preceding", FRX_AX_PRECEDING }, |
| 240 |
|
| { "preceding-sibling", FRX_AX_PRECEDING_SIBLING }, |
| 241 |
|
| { "self", FRX_AX_SELF } |
| 242 |
|
| }; |
| 243 |
|
| size_t i; |
| 244 |
185 |
▶ | for (i = 0; i < sizeof A / sizeof A[0]; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < sizeof A / sizeof A[0]; i++) |
184 |
1 |
185 |
|
| 245 |
184 |
▶ | if (frx_str_eq(s, A[i].n, strlen(A[i].n))) return A[i].a; |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_str_eq(s, A[i].n, strlen(A[i].n))) return A[i].a; |
23 |
161 |
184 |
|
| 246 |
1 |
| return -1; |
| 247 |
|
| } |
| 248 |
|
| |
| 249 |
|
| /* a NodeType name, or 0 */ |
| 250 |
|
| static int |
| 251 |
280 |
| frx_xp_nodetype(const frx_str *s) |
| 252 |
|
| { |
| 253 |
280 |
▶ | if (frx_str_eq(s, "node", 4)) return FRX_XTEST_NODE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_str_eq(s, "node", 4)) return FRX_XTEST_NODE; |
3 |
277 |
280 |
|
| 254 |
277 |
▶ | if (frx_str_eq(s, "text", 4)) return FRX_XTEST_TEXT; |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_str_eq(s, "text", 4)) return FRX_XTEST_TEXT; |
1 |
276 |
277 |
|
| 255 |
276 |
▶ | if (frx_str_eq(s, "comment", 7)) return FRX_XTEST_COMMENT; |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_str_eq(s, "comment", 7)) return FRX_XTEST_COMMENT; |
1 |
275 |
276 |
|
| 256 |
275 |
▶ | if (frx_str_eq(s, "processing-instruction", 22)) return FRX_XTEST_PI; |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_str_eq(s, "processing-instruction", 22)) return FRX_XTEST_PI; |
3 |
272 |
275 |
|
| 257 |
272 |
| return 0; |
| 258 |
|
| } |
| 259 |
|
| |
| 260 |
|
| typedef struct frx_xp_fn { const char *n; int id; int min; int max; } frx_xp_fn; |
| 261 |
|
| |
| 262 |
|
| static const frx_xp_fn * |
| 263 |
271 |
| frx_xp_func_by_name(const frx_str *s) |
| 264 |
|
| { |
| 265 |
|
| static const frx_xp_fn F[] = { |
| 266 |
|
| { "last", FRX_FN_LAST, 0, 0 }, |
| 267 |
|
| { "position", FRX_FN_POSITION, 0, 0 }, |
| 268 |
|
| { "count", FRX_FN_COUNT, 1, 1 }, |
| 269 |
|
| { "id", FRX_FN_ID, 1, 1 }, |
| 270 |
|
| { "local-name", FRX_FN_LOCAL_NAME, 0, 1 }, |
| 271 |
|
| { "namespace-uri", FRX_FN_NAMESPACE_URI, 0, 1 }, |
| 272 |
|
| { "name", FRX_FN_NAME, 0, 1 }, |
| 273 |
|
| { "string", FRX_FN_STRING, 0, 1 }, |
| 274 |
|
| { "concat", FRX_FN_CONCAT, 2, -1 }, |
| 275 |
|
| { "starts-with", FRX_FN_STARTS_WITH, 2, 2 }, |
| 276 |
|
| { "contains", FRX_FN_CONTAINS, 2, 2 }, |
| 277 |
|
| { "substring-before", FRX_FN_SUBSTRING_BEFORE, 2, 2 }, |
| 278 |
|
| { "substring-after", FRX_FN_SUBSTRING_AFTER, 2, 2 }, |
| 279 |
|
| { "substring", FRX_FN_SUBSTRING, 2, 3 }, |
| 280 |
|
| { "string-length", FRX_FN_STRING_LENGTH, 0, 1 }, |
| 281 |
|
| { "normalize-space", FRX_FN_NORMALIZE_SPACE, 0, 1 }, |
| 282 |
|
| { "translate", FRX_FN_TRANSLATE, 3, 3 }, |
| 283 |
|
| { "boolean", FRX_FN_BOOLEAN, 1, 1 }, |
| 284 |
|
| { "not", FRX_FN_NOT, 1, 1 }, |
| 285 |
|
| { "true", FRX_FN_TRUE, 0, 0 }, |
| 286 |
|
| { "false", FRX_FN_FALSE, 0, 0 }, |
| 287 |
|
| { "lang", FRX_FN_LANG, 1, 1 }, |
| 288 |
|
| { "number", FRX_FN_NUMBER, 0, 1 }, |
| 289 |
|
| { "sum", FRX_FN_SUM, 1, 1 }, |
| 290 |
|
| { "floor", FRX_FN_FLOOR, 1, 1 }, |
| 291 |
|
| { "ceiling", FRX_FN_CEILING, 1, 1 }, |
| 292 |
|
| { "round", FRX_FN_ROUND, 1, 1 } |
| 293 |
|
| }; |
| 294 |
|
| size_t i; |
| 295 |
2822 |
▶ | for (i = 0; i < sizeof F / sizeof F[0]; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < sizeof F / sizeof F[0]; i++) |
2821 |
1 |
2822 |
|
| 296 |
2821 |
▶ | if (frx_str_eq(s, F[i].n, strlen(F[i].n))) return &F[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_str_eq(s, F[i].n, strlen(F[i].n))) return &F[i]; |
270 |
2551 |
2821 |
|
| 297 |
1 |
| return NULL; |
| 298 |
|
| } |
| 299 |
|
| |
| 300 |
|
| /* the index of name in the expression's variable list, adding it once */ |
| 301 |
|
| static int |
| 302 |
10 |
| frx_xp_var_index(frx_xparse *p, const frx_str *name) |
| 303 |
|
| { |
| 304 |
10 |
| frx_xvars *vs = p->vars; |
| 305 |
|
| int i; |
| 306 |
11 |
▶ | for (i = 0; i < vs->n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < vs->n; i++) |
1 |
10 |
11 |
|
| 307 |
1 |
▶ | if (frx_str_eq2(&vs->name[i], name)) return i; |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_str_eq2(&vs->name[i], name)) return i; |
0 |
1 |
1 |
|
| 308 |
10 |
▶ | if (vs->n == vs->cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (vs->n == vs->cap) { |
9 |
1 |
10 |
|
| 309 |
9 |
▶ | int ncap = vs->cap ? vs->cap * 2 : 8; |
Branch: 1 branch
| Branch | True | False | Total |
| int ncap = vs->cap ? vs->cap * 2 : 8; |
0 |
9 |
9 |
|
| 310 |
9 |
| frx_str *nn = (frx_str *)realloc(vs->name, (size_t)ncap * sizeof *nn); |
| 311 |
9 |
▶ | if (!nn) { (void)frx_xp_fail(p, "out of memory"); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nn) { (void)frx_xp_fail(p, "out of memory"); return -1; } |
0 |
9 |
9 |
|
| 312 |
9 |
| vs->name = nn; |
| 313 |
9 |
| vs->cap = ncap; |
| 314 |
|
| } |
| 315 |
10 |
| vs->name[vs->n] = frx_xp_dup(p, name); |
| 316 |
10 |
▶ | if (!vs->name[vs->n].p) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!vs->name[vs->n].p) return -1; |
0 |
10 |
10 |
|
| 317 |
10 |
| return vs->n++; |
| 318 |
|
| } |
| 319 |
|
| |
| 320 |
|
| /* ---- the productions --------------------------------------------------- */ |
| 321 |
|
| |
| 322 |
|
| /* NodeTest, with the axis already known: the principal node type decides |
| 323 |
|
| * what a name test matches, and that is the evaluator's business */ |
| 324 |
|
| static int |
| 325 |
178 |
| frx_xp_node_test(frx_xparse *p, frx_xn *step) |
| 326 |
|
| { |
| 327 |
178 |
| const frx_xtok *t = frx_xp_peek(p, 0); |
| 328 |
|
| |
| 329 |
178 |
▶ | if (t->kind == FRX_XT_NAME && frx_xp_peek(p, 1)->kind == FRX_XT_LPAREN) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (t->kind == FRX_XT_NAME && frx_xp_peek(p, 1)->kind == FRX_XT_LPAREN) { |
176 |
2 |
178 |
| if (t->kind == FRX_XT_NAME && frx_xp_peek(p, 1)->kind == FRX_XT_LPAREN) { |
9 |
167 |
176 |
|
| 330 |
9 |
| int nt = frx_xp_nodetype(&t->s); |
| 331 |
9 |
▶ | if (nt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nt) { |
8 |
1 |
9 |
|
| 332 |
8 |
| p->i += 2; |
| 333 |
8 |
| step->test = nt; |
| 334 |
8 |
▶ | if (nt == FRX_XTEST_PI && frx_xp_at(p, FRX_XT_LITERAL)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (nt == FRX_XTEST_PI && frx_xp_at(p, FRX_XT_LITERAL)) { |
3 |
5 |
8 |
| if (nt == FRX_XTEST_PI && frx_xp_at(p, FRX_XT_LITERAL)) { |
2 |
1 |
3 |
|
| 335 |
2 |
| step->str = frx_xp_dup(p, &frx_xp_peek(p, 0)->s); |
| 336 |
2 |
▶ | if (!step->str.p) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!step->str.p) return 0; |
0 |
2 |
2 |
|
| 337 |
2 |
| p->i++; |
| 338 |
|
| } |
| 339 |
8 |
▶ | if (!frx_xp_eat(p, FRX_XT_RPAREN)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_eat(p, FRX_XT_RPAREN)) |
0 |
8 |
8 |
|
| 340 |
0 |
| return frx_xp_fail(p, "a node type takes no argument but processing-instruction's target") != NULL; |
| 341 |
8 |
| return 1; |
| 342 |
|
| } |
| 343 |
|
| } |
| 344 |
170 |
▶ | if (t->kind != FRX_XT_NAME) |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->kind != FRX_XT_NAME) |
2 |
168 |
170 |
|
| 345 |
2 |
| return frx_xp_fail(p, "a node test was expected") != NULL; |
| 346 |
168 |
| p->i++; |
| 347 |
|
| { |
| 348 |
168 |
| const char *colon = (const char *)memchr(t->s.p, ':', t->s.len); |
| 349 |
|
| frx_str prefix, local; |
| 350 |
168 |
▶ | if (!colon) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!colon) { |
153 |
15 |
168 |
|
| 351 |
153 |
▶ | if (t->s.len == 1 && t->s.p[0] == '*') { step->test = FRX_XTEST_STAR; return 1; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (t->s.len == 1 && t->s.p[0] == '*') { step->test = FRX_XTEST_STAR; return 1; } |
130 |
23 |
153 |
| if (t->s.len == 1 && t->s.p[0] == '*') { step->test = FRX_XTEST_STAR; return 1; } |
31 |
99 |
130 |
|
| 352 |
122 |
| step->test = FRX_XTEST_NAME; |
| 353 |
122 |
| step->ns = FRX_EMPTY_STR; /* never the default namespace */ |
| 354 |
122 |
| step->local = frx_xp_dup(p, &t->s); |
| 355 |
122 |
| return step->local.p != NULL; |
| 356 |
|
| } |
| 357 |
15 |
| prefix.p = t->s.p; |
| 358 |
15 |
| prefix.len = (size_t)(colon - t->s.p); |
| 359 |
15 |
| local.p = colon + 1; |
| 360 |
15 |
| local.len = t->s.len - prefix.len - 1; |
| 361 |
|
| { |
| 362 |
15 |
| const char *uri = frx_xp_ns_uri(p->ns, prefix.p, prefix.len); |
| 363 |
|
| frx_str u; |
| 364 |
15 |
▶ | if (!uri) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!uri) { |
2 |
13 |
15 |
|
| 365 |
2 |
| p->i--; /* report at the name */ |
| 366 |
2 |
| return frx_xp_fail(p, "the prefix of a name test is not in the expression's namespace map") != NULL; |
| 367 |
|
| } |
| 368 |
13 |
| u.p = uri; |
| 369 |
13 |
| u.len = strlen(uri); |
| 370 |
13 |
| step->ns = frx_xp_dup(p, &u); |
| 371 |
13 |
▶ | if (!step->ns.p) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!step->ns.p) return 0; |
0 |
13 |
13 |
|
| 372 |
|
| } |
| 373 |
13 |
▶ | if (local.len == 1 && local.p[0] == '*') { step->test = FRX_XTEST_NSSTAR; return 1; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (local.len == 1 && local.p[0] == '*') { step->test = FRX_XTEST_NSSTAR; return 1; } |
10 |
3 |
13 |
| if (local.len == 1 && local.p[0] == '*') { step->test = FRX_XTEST_NSSTAR; return 1; } |
1 |
9 |
10 |
|
| 374 |
12 |
| step->test = FRX_XTEST_NAME; |
| 375 |
12 |
| step->local = frx_xp_dup(p, &local); |
| 376 |
12 |
| return step->local.p != NULL; |
| 377 |
|
| } |
| 378 |
|
| } |
| 379 |
|
| |
| 380 |
|
| static int |
| 381 |
988 |
| frx_xp_predicates(frx_xparse *p, frx_xn ***v, int *n, int *cap) |
| 382 |
|
| { |
| 383 |
988 |
| int pcap = *cap; |
| 384 |
1014 |
▶ | while (frx_xp_at(p, FRX_XT_LBRACK)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (frx_xp_at(p, FRX_XT_LBRACK)) { |
29 |
985 |
1014 |
|
| 385 |
|
| frx_xn *e; |
| 386 |
29 |
| p->i++; |
| 387 |
29 |
| e = frx_xp_expr(p); |
| 388 |
29 |
▶ | if (!e) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) return 0; |
1 |
28 |
29 |
|
| 389 |
28 |
▶ | if (!frx_xp_eat(p, FRX_XT_RBRACK)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_eat(p, FRX_XT_RBRACK)) |
2 |
26 |
28 |
|
| 390 |
2 |
| return frx_xp_fail(p, "a predicate is never closed with ']'") != NULL; |
| 391 |
26 |
▶ | if (!frx_xp_push(p, v, n, &pcap, e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_push(p, v, n, &pcap, e)) return 0; |
0 |
26 |
26 |
|
| 392 |
|
| } |
| 393 |
985 |
| *cap = pcap; |
| 394 |
985 |
| return 1; |
| 395 |
|
| } |
| 396 |
|
| |
| 397 |
|
| static frx_xn * |
| 398 |
180 |
| frx_xp_step(frx_xparse *p) |
| 399 |
|
| { |
| 400 |
|
| frx_xn *s; |
| 401 |
180 |
| int cap = 0; |
| 402 |
|
| |
| 403 |
180 |
▶ | if (frx_xp_at(p, FRX_XT_DOT) || frx_xp_at(p, FRX_XT_DOTDOT)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (frx_xp_at(p, FRX_XT_DOT) || frx_xp_at(p, FRX_XT_DOTDOT)) { |
180 |
0 |
180 |
| if (frx_xp_at(p, FRX_XT_DOT) || frx_xp_at(p, FRX_XT_DOTDOT)) { |
1 |
179 |
180 |
|
| 404 |
1 |
| int dotdot = frx_xp_at(p, FRX_XT_DOTDOT); |
| 405 |
1 |
| p->i++; |
| 406 |
1 |
| s = frx_xp_new(p, FRX_XN_STEP); |
| 407 |
1 |
▶ | if (!s) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return NULL; |
0 |
1 |
1 |
|
| 408 |
1 |
▶ | s->axis = dotdot ? FRX_AX_PARENT : FRX_AX_SELF; |
Branch: 1 branch
| Branch | True | False | Total |
| s->axis = dotdot ? FRX_AX_PARENT : FRX_AX_SELF; |
1 |
0 |
1 |
|
| 409 |
1 |
| s->test = FRX_XTEST_NODE; |
| 410 |
1 |
| return s; |
| 411 |
|
| } |
| 412 |
|
| |
| 413 |
179 |
| s = frx_xp_new(p, FRX_XN_STEP); |
| 414 |
179 |
▶ | if (!s) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return NULL; |
0 |
179 |
179 |
|
| 415 |
179 |
| s->axis = FRX_AX_CHILD; |
| 416 |
179 |
▶ | if (frx_xp_eat(p, FRX_XT_AT)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_xp_eat(p, FRX_XT_AT)) { |
17 |
162 |
179 |
|
| 417 |
17 |
| s->axis = FRX_AX_ATTRIBUTE; |
| 418 |
162 |
▶ | } else if (frx_xp_at(p, FRX_XT_NAME) && frx_xp_peek(p, 1)->kind == FRX_XT_COLON2) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (frx_xp_at(p, FRX_XT_NAME) && frx_xp_peek(p, 1)->kind == FRX_XT_COLON2) { |
160 |
2 |
162 |
| } else if (frx_xp_at(p, FRX_XT_NAME) && frx_xp_peek(p, 1)->kind == FRX_XT_COLON2) { |
24 |
136 |
160 |
|
| 419 |
24 |
| int ax = frx_xp_axis_by_name(&frx_xp_peek(p, 0)->s); |
| 420 |
24 |
▶ | if (ax < 0) return (frx_xn *)frx_xp_fail(p, "not one of the thirteen axis names"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ax < 0) return (frx_xn *)frx_xp_fail(p, "not one of the thirteen axis names"); |
1 |
23 |
24 |
|
| 421 |
23 |
| s->axis = ax; |
| 422 |
23 |
| p->i += 2; |
| 423 |
|
| } |
| 424 |
178 |
▶ | if (!frx_xp_node_test(p, s)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_node_test(p, s)) return NULL; |
4 |
174 |
178 |
|
| 425 |
174 |
▶ | if (!frx_xp_predicates(p, &s->preds, &s->n_preds, &cap)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_predicates(p, &s->preds, &s->n_preds, &cap)) return NULL; |
3 |
171 |
174 |
|
| 426 |
171 |
| return frx_xp_depth_ok(p, s); |
| 427 |
|
| } |
| 428 |
|
| |
| 429 |
|
| /* `//` is `/descendant-or-self::node()/`, spelled out here */ |
| 430 |
|
| static frx_xn * |
| 431 |
106 |
| frx_xp_dos_step(frx_xparse *p) |
| 432 |
|
| { |
| 433 |
106 |
| frx_xn *s = frx_xp_new(p, FRX_XN_STEP); |
| 434 |
106 |
▶ | if (!s) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return NULL; |
0 |
106 |
106 |
|
| 435 |
106 |
| s->axis = FRX_AX_DESCENDANT_OR_SELF; |
| 436 |
106 |
| s->test = FRX_XTEST_NODE; |
| 437 |
106 |
| return s; |
| 438 |
|
| } |
| 439 |
|
| |
| 440 |
|
| static int |
| 441 |
21 |
| frx_xp_starts_step(const frx_xparse *p) |
| 442 |
|
| { |
| 443 |
21 |
| const frx_xtok *t = frx_xp_peek(p, 0); |
| 444 |
21 |
▶ | switch (t->kind) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (t->kind) { |
20 |
1 |
21 |
|
| 445 |
20 |
| case FRX_XT_AT: case FRX_XT_DOT: case FRX_XT_DOTDOT: case FRX_XT_NAME: |
| 446 |
20 |
| return 1; |
| 447 |
1 |
| default: |
| 448 |
1 |
| return 0; |
| 449 |
|
| } |
| 450 |
|
| } |
| 451 |
|
| |
| 452 |
|
| static int |
| 453 |
135 |
| frx_xp_rel_path(frx_xparse *p, frx_xn *path) |
| 454 |
|
| { |
| 455 |
135 |
| int cap = path->n_steps; |
| 456 |
|
| for (;;) { |
| 457 |
180 |
▶ | if (!frx_xp_push(p, &path->steps, &path->n_steps, &cap, frx_xp_step(p))) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_push(p, &path->steps, &path->n_steps, &cap, frx_xp_step(p))) return 0; |
8 |
172 |
180 |
|
| 458 |
172 |
▶ | if (frx_xp_eat(p, FRX_XT_SLASH)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_xp_eat(p, FRX_XT_SLASH)) continue; |
45 |
127 |
172 |
|
| 459 |
127 |
▶ | if (frx_xp_eat(p, FRX_XT_SLASH2)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_xp_eat(p, FRX_XT_SLASH2)) { |
0 |
127 |
127 |
|
| 460 |
0 |
▶ | if (!frx_xp_push(p, &path->steps, &path->n_steps, &cap, frx_xp_dos_step(p))) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_push(p, &path->steps, &path->n_steps, &cap, frx_xp_dos_step(p))) return 0; |
0 |
0 |
0 |
|
| 461 |
0 |
| continue; |
| 462 |
|
| } |
| 463 |
127 |
| return 1; |
| 464 |
|
| } |
| 465 |
|
| } |
| 466 |
|
| |
| 467 |
|
| static frx_xn * |
| 468 |
136 |
| frx_xp_location_path(frx_xparse *p) |
| 469 |
|
| { |
| 470 |
136 |
| frx_xn *path = frx_xp_new(p, FRX_XN_PATH); |
| 471 |
136 |
▶ | if (!path) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!path) return NULL; |
0 |
136 |
136 |
|
| 472 |
136 |
▶ | if (frx_xp_eat(p, FRX_XT_SLASH2)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_xp_eat(p, FRX_XT_SLASH2)) { |
106 |
30 |
136 |
|
| 473 |
106 |
| int cap = 0; |
| 474 |
106 |
| path->absolute = 1; |
| 475 |
106 |
▶ | if (!frx_xp_push(p, &path->steps, &path->n_steps, &cap, frx_xp_dos_step(p))) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_push(p, &path->steps, &path->n_steps, &cap, frx_xp_dos_step(p))) return NULL; |
0 |
106 |
106 |
|
| 476 |
106 |
▶ | if (!frx_xp_rel_path(p, path)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_rel_path(p, path)) return NULL; |
5 |
101 |
106 |
|
| 477 |
101 |
| return frx_xp_depth_ok(p, path); |
| 478 |
|
| } |
| 479 |
30 |
▶ | if (frx_xp_eat(p, FRX_XT_SLASH)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_xp_eat(p, FRX_XT_SLASH)) { |
21 |
9 |
30 |
|
| 480 |
21 |
| path->absolute = 1; |
| 481 |
21 |
▶ | if (!frx_xp_starts_step(p)) return path; /* `/` alone: the root */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_starts_step(p)) return path; /* `/` alone: the root */ |
1 |
20 |
21 |
|
| 482 |
20 |
▶ | if (!frx_xp_rel_path(p, path)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_rel_path(p, path)) return NULL; |
0 |
20 |
20 |
|
| 483 |
20 |
| return frx_xp_depth_ok(p, path); |
| 484 |
|
| } |
| 485 |
9 |
▶ | if (!frx_xp_rel_path(p, path)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_rel_path(p, path)) return NULL; |
3 |
6 |
9 |
|
| 486 |
6 |
| return frx_xp_depth_ok(p, path); |
| 487 |
|
| } |
| 488 |
|
| |
| 489 |
|
| static frx_xn * |
| 490 |
271 |
| frx_xp_function_call(frx_xparse *p) |
| 491 |
|
| { |
| 492 |
271 |
| const frx_xtok *t = frx_xp_peek(p, 0); |
| 493 |
271 |
| const frx_xp_fn *f = frx_xp_func_by_name(&t->s); |
| 494 |
|
| frx_xn *n; |
| 495 |
271 |
| int cap = 0; |
| 496 |
|
| |
| 497 |
271 |
▶ | if (!f) return (frx_xn *)frx_xp_fail(p, "not one of the 27 core functions"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f) return (frx_xn *)frx_xp_fail(p, "not one of the 27 core functions"); |
1 |
270 |
271 |
|
| 498 |
270 |
| n = frx_xp_new(p, FRX_XN_FUNC); |
| 499 |
270 |
▶ | if (!n) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return NULL; |
0 |
270 |
270 |
|
| 500 |
270 |
| n->func = f->id; |
| 501 |
270 |
| p->i += 2; /* the name and the '(' */ |
| 502 |
270 |
▶ | if (!frx_xp_at(p, FRX_XT_RPAREN)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_at(p, FRX_XT_RPAREN)) { |
253 |
17 |
270 |
|
| 503 |
|
| for (;;) { |
| 504 |
295 |
▶ | if (!frx_xp_push(p, &n->args, &n->n_args, &cap, frx_xp_expr(p))) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_push(p, &n->args, &n->n_args, &cap, frx_xp_expr(p))) return NULL; |
0 |
295 |
295 |
|
| 505 |
295 |
▶ | if (frx_xp_eat(p, FRX_XT_COMMA)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_xp_eat(p, FRX_XT_COMMA)) continue; |
42 |
253 |
295 |
|
| 506 |
253 |
| break; |
| 507 |
|
| } |
| 508 |
|
| } |
| 509 |
270 |
▶ | if (!frx_xp_eat(p, FRX_XT_RPAREN)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_eat(p, FRX_XT_RPAREN)) |
0 |
270 |
270 |
|
| 510 |
0 |
| return (frx_xn *)frx_xp_fail(p, "an argument list is never closed with ')'"); |
| 511 |
270 |
▶ | if (n->n_args < f->min || (f->max >= 0 && n->n_args > f->max)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (n->n_args < f->min || (f->max >= 0 && n->n_args > f->max)) { |
269 |
1 |
270 |
| if (n->n_args < f->min || (f->max >= 0 && n->n_args > f->max)) { |
268 |
1 |
269 |
| if (n->n_args < f->min || (f->max >= 0 && n->n_args > f->max)) { |
1 |
267 |
268 |
|
| 512 |
2 |
| p->i--; |
| 513 |
2 |
| return (frx_xn *)frx_xp_fail(p, "the function was given the wrong number of arguments"); |
| 514 |
|
| } |
| 515 |
268 |
| return frx_xp_depth_ok(p, n); |
| 516 |
|
| } |
| 517 |
|
| |
| 518 |
|
| static frx_xn * |
| 519 |
882 |
| frx_xp_primary(frx_xparse *p) |
| 520 |
|
| { |
| 521 |
882 |
| const frx_xtok *t = frx_xp_peek(p, 0); |
| 522 |
|
| frx_xn *n; |
| 523 |
|
| |
| 524 |
882 |
| switch (t->kind) { |
| 525 |
10 |
| case FRX_XT_VAR: |
| 526 |
10 |
| n = frx_xp_new(p, FRX_XN_VAR); |
| 527 |
10 |
▶ | if (!n) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return NULL; |
0 |
10 |
10 |
|
| 528 |
10 |
| n->str = frx_xp_dup(p, &t->s); |
| 529 |
10 |
▶ | if (!n->str.p) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n->str.p) return NULL; |
0 |
10 |
10 |
|
| 530 |
10 |
| n->var = frx_xp_var_index(p, &t->s); |
| 531 |
10 |
▶ | if (n->var < 0) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->var < 0) return NULL; |
0 |
10 |
10 |
|
| 532 |
10 |
| p->i++; |
| 533 |
10 |
| return n; |
| 534 |
138 |
| case FRX_XT_LPAREN: |
| 535 |
138 |
| p->i++; |
| 536 |
138 |
| n = frx_xp_expr(p); |
| 537 |
138 |
▶ | if (!n) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return NULL; |
64 |
74 |
138 |
|
| 538 |
74 |
▶ | if (!frx_xp_eat(p, FRX_XT_RPAREN)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_eat(p, FRX_XT_RPAREN)) |
1 |
73 |
74 |
|
| 539 |
1 |
| return (frx_xn *)frx_xp_fail(p, "a parenthesised expression is never closed"); |
| 540 |
73 |
| return n; |
| 541 |
78 |
| case FRX_XT_LITERAL: |
| 542 |
78 |
| n = frx_xp_new(p, FRX_XN_LITERAL); |
| 543 |
78 |
▶ | if (!n) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return NULL; |
0 |
78 |
78 |
|
| 544 |
78 |
| n->str = frx_xp_dup(p, &t->s); |
| 545 |
78 |
▶ | if (!n->str.p) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n->str.p) return NULL; |
0 |
78 |
78 |
|
| 546 |
78 |
| p->i++; |
| 547 |
78 |
| return n; |
| 548 |
385 |
| case FRX_XT_NUMBER: |
| 549 |
385 |
| n = frx_xp_new(p, FRX_XN_NUMBER); |
| 550 |
385 |
▶ | if (!n) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return NULL; |
0 |
385 |
385 |
|
| 551 |
385 |
| n->num = t->num; |
| 552 |
385 |
| p->i++; |
| 553 |
385 |
| return n; |
| 554 |
271 |
| case FRX_XT_NAME: |
| 555 |
271 |
| return frx_xp_function_call(p); |
| 556 |
0 |
| default: |
| 557 |
0 |
| return (frx_xn *)frx_xp_fail(p, "an expression was expected"); |
| 558 |
|
| } |
| 559 |
|
| } |
| 560 |
|
| |
| 561 |
|
| /* does a PathExpr begin with a FilterExpr rather than a LocationPath? */ |
| 562 |
|
| static int |
| 563 |
1018 |
| frx_xp_starts_filter(const frx_xparse *p) |
| 564 |
|
| { |
| 565 |
1018 |
| const frx_xtok *t = frx_xp_peek(p, 0); |
| 566 |
1018 |
▶ | if (t->kind == FRX_XT_VAR || t->kind == FRX_XT_LPAREN |
Branch: 2 branches
| Branch | True | False | Total |
| if (t->kind == FRX_XT_VAR || t->kind == FRX_XT_LPAREN |
1008 |
10 |
1018 |
| if (t->kind == FRX_XT_VAR || t->kind == FRX_XT_LPAREN |
870 |
138 |
1008 |
|
| 567 |
1018 |
▶ | || t->kind == FRX_XT_LITERAL || t->kind == FRX_XT_NUMBER) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| || t->kind == FRX_XT_LITERAL || t->kind == FRX_XT_NUMBER) return 1; |
792 |
78 |
870 |
| || t->kind == FRX_XT_LITERAL || t->kind == FRX_XT_NUMBER) return 1; |
385 |
407 |
792 |
|
| 568 |
|
| /* a FunctionName is a QName that is not a NodeType */ |
| 569 |
407 |
| return t->kind == FRX_XT_NAME |
| 570 |
275 |
▶ | && frx_xp_peek(p, 1)->kind == FRX_XT_LPAREN |
Branch: 1 branch
| Branch | True | False | Total |
| && frx_xp_peek(p, 1)->kind == FRX_XT_LPAREN |
271 |
4 |
275 |
|
| 571 |
682 |
▶ | && !frx_xp_nodetype(&t->s); |
Branch: 2 branches
| Branch | True | False | Total |
| && !frx_xp_nodetype(&t->s); |
275 |
132 |
407 |
| && !frx_xp_nodetype(&t->s); |
271 |
0 |
271 |
|
| 572 |
|
| } |
| 573 |
|
| |
| 574 |
|
| static frx_xn * |
| 575 |
1018 |
| frx_xp_path_expr(frx_xparse *p) |
| 576 |
|
| { |
| 577 |
|
| frx_xn *filter, *path; |
| 578 |
1018 |
| int cap = 0; |
| 579 |
|
| |
| 580 |
1018 |
▶ | if (!frx_xp_starts_filter(p)) return frx_xp_location_path(p); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_starts_filter(p)) return frx_xp_location_path(p); |
136 |
882 |
1018 |
|
| 581 |
|
| |
| 582 |
882 |
| filter = frx_xp_new(p, FRX_XN_FILTER); |
| 583 |
882 |
▶ | if (!filter) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!filter) return NULL; |
0 |
882 |
882 |
|
| 584 |
882 |
| filter->a = frx_xp_primary(p); |
| 585 |
882 |
▶ | if (!filter->a) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!filter->a) return NULL; |
68 |
814 |
882 |
|
| 586 |
814 |
▶ | if (!frx_xp_predicates(p, &filter->preds, &filter->n_preds, &cap)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_predicates(p, &filter->preds, &filter->n_preds, &cap)) return NULL; |
0 |
814 |
814 |
|
| 587 |
|
| |
| 588 |
814 |
▶ | if (!frx_xp_at(p, FRX_XT_SLASH) && !frx_xp_at(p, FRX_XT_SLASH2)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!frx_xp_at(p, FRX_XT_SLASH) && !frx_xp_at(p, FRX_XT_SLASH2)) |
814 |
0 |
814 |
| if (!frx_xp_at(p, FRX_XT_SLASH) && !frx_xp_at(p, FRX_XT_SLASH2)) |
814 |
0 |
814 |
|
| 589 |
814 |
| return frx_xp_depth_ok(p, filter); |
| 590 |
0 |
▶ | if (!frx_xp_depth_ok(p, filter)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_depth_ok(p, filter)) return NULL; |
0 |
0 |
0 |
|
| 591 |
|
| |
| 592 |
0 |
| path = frx_xp_new(p, FRX_XN_PATH); |
| 593 |
0 |
▶ | if (!path) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!path) return NULL; |
0 |
0 |
0 |
|
| 594 |
0 |
| path->a = filter; |
| 595 |
0 |
▶ | if (frx_xp_eat(p, FRX_XT_SLASH2)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_xp_eat(p, FRX_XT_SLASH2)) { |
0 |
0 |
0 |
|
| 596 |
0 |
| int c2 = 0; |
| 597 |
0 |
▶ | if (!frx_xp_push(p, &path->steps, &path->n_steps, &c2, frx_xp_dos_step(p))) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_push(p, &path->steps, &path->n_steps, &c2, frx_xp_dos_step(p))) return NULL; |
0 |
0 |
0 |
|
| 598 |
|
| } else { |
| 599 |
0 |
| p->i++; /* the '/' */ |
| 600 |
|
| } |
| 601 |
0 |
▶ | if (!frx_xp_rel_path(p, path)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xp_rel_path(p, path)) return NULL; |
0 |
0 |
0 |
|
| 602 |
0 |
| return frx_xp_depth_ok(p, path); |
| 603 |
|
| } |
| 604 |
|
| |
| 605 |
|
| static frx_xn * |
| 606 |
1014 |
| frx_xp_union(frx_xparse *p) |
| 607 |
|
| { |
| 608 |
1014 |
| frx_xn *l = frx_xp_path_expr(p); |
| 609 |
1018 |
▶ | while (l && frx_xp_at(p, FRX_XT_PIPE)) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (l && frx_xp_at(p, FRX_XT_PIPE)) { |
942 |
76 |
1018 |
| while (l && frx_xp_at(p, FRX_XT_PIPE)) { |
4 |
938 |
942 |
|
| 610 |
|
| frx_xn *n; |
| 611 |
4 |
| p->i++; |
| 612 |
4 |
| n = frx_xp_new(p, FRX_XN_UNION); |
| 613 |
4 |
▶ | if (!n) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return NULL; |
0 |
4 |
4 |
|
| 614 |
4 |
| n->a = l; |
| 615 |
4 |
| n->b = frx_xp_path_expr(p); |
| 616 |
4 |
▶ | if (!n->b) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n->b) return NULL; |
0 |
4 |
4 |
|
| 617 |
4 |
| l = frx_xp_depth_ok(p, n); |
| 618 |
|
| } |
| 619 |
1014 |
| return l; |
| 620 |
|
| } |
| 621 |
|
| |
| 622 |
|
| /* the second recursion over the expression, and so the second place the |
| 623 |
|
| * depth is counted: `- - - -x` is a chain of UnaryExpr and nothing else |
| 624 |
|
| * on the way down calls frx_xp_expr */ |
| 625 |
|
| static frx_xn * |
| 626 |
1028 |
| frx_xp_unary(frx_xparse *p) |
| 627 |
|
| { |
| 628 |
1028 |
▶ | if (frx_xp_at(p, FRX_XT_MINUS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_xp_at(p, FRX_XT_MINUS)) { |
14 |
1014 |
1028 |
|
| 629 |
|
| frx_xn *n; |
| 630 |
14 |
▶ | if (++p->depth > p->max_depth) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (++p->depth > p->max_depth) { |
0 |
14 |
14 |
|
| 631 |
0 |
| p->depth--; |
| 632 |
0 |
| return (frx_xn *)frx_xp_fail(p, "the expression nests deeper than max_expr_depth"); |
| 633 |
|
| } |
| 634 |
14 |
| p->i++; |
| 635 |
14 |
| n = frx_xp_new(p, FRX_XN_NEG); |
| 636 |
14 |
▶ | if (n) n->a = frx_xp_unary(p); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n) n->a = frx_xp_unary(p); |
14 |
0 |
14 |
|
| 637 |
14 |
| p->depth--; |
| 638 |
14 |
▶ | return (n && n->a) ? frx_xp_depth_ok(p, n) : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| return (n && n->a) ? frx_xp_depth_ok(p, n) : NULL; |
14 |
0 |
14 |
| return (n && n->a) ? frx_xp_depth_ok(p, n) : NULL; |
14 |
0 |
14 |
|
| 639 |
|
| } |
| 640 |
1014 |
| return frx_xp_union(p); |
| 641 |
|
| } |
| 642 |
|
| |
| 643 |
|
| /* one left-associative binary level, driven by a table of (token, kind) */ |
| 644 |
|
| static frx_xn * |
| 645 |
5310 |
| frx_xp_binary(frx_xparse *p, const int *pairs, int n_pairs, frx_xn *(*next)(frx_xparse *)) |
| 646 |
|
| { |
| 647 |
5310 |
| frx_xn *l = next(p); |
| 648 |
317 |
| for (;;) { |
| 649 |
5627 |
| int i, kind = 0; |
| 650 |
5627 |
▶ | if (!l) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!l) return NULL; |
457 |
5170 |
5627 |
|
| 651 |
16391 |
▶ | for (i = 0; i < n_pairs; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n_pairs; i++) |
11539 |
4852 |
16391 |
|
| 652 |
11539 |
▶ | if (frx_xp_at(p, pairs[i * 2])) { kind = pairs[i * 2 + 1]; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_xp_at(p, pairs[i * 2])) { kind = pairs[i * 2 + 1]; break; } |
318 |
11221 |
11539 |
|
| 653 |
5170 |
▶ | if (!kind) return l; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!kind) return l; |
4852 |
318 |
5170 |
|
| 654 |
318 |
| p->i++; |
| 655 |
|
| { |
| 656 |
318 |
| frx_xn *n = frx_xp_new(p, kind); |
| 657 |
318 |
▶ | if (!n) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return NULL; |
0 |
318 |
318 |
|
| 658 |
318 |
| n->a = l; |
| 659 |
318 |
| n->b = next(p); |
| 660 |
318 |
▶ | if (!n->b) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n->b) return NULL; |
1 |
317 |
318 |
|
| 661 |
317 |
| l = frx_xp_depth_ok(p, n); |
| 662 |
|
| } |
| 663 |
|
| } |
| 664 |
|
| } |
| 665 |
|
| |
| 666 |
|
| static frx_xn * |
| 667 |
994 |
| frx_xp_multiplicative(frx_xparse *p) |
| 668 |
|
| { |
| 669 |
|
| static const int ops[] = { FRX_XT_STAR, FRX_XN_MUL, |
| 670 |
|
| FRX_XT_DIV, FRX_XN_DIV, |
| 671 |
|
| FRX_XT_MOD, FRX_XN_MOD }; |
| 672 |
994 |
| return frx_xp_binary(p, ops, 3, frx_xp_unary); |
| 673 |
|
| } |
| 674 |
|
| |
| 675 |
|
| static frx_xn * |
| 676 |
986 |
| frx_xp_additive(frx_xparse *p) |
| 677 |
|
| { |
| 678 |
|
| static const int ops[] = { FRX_XT_PLUS, FRX_XN_ADD, FRX_XT_MINUS, FRX_XN_SUB }; |
| 679 |
986 |
| return frx_xp_binary(p, ops, 2, frx_xp_multiplicative); |
| 680 |
|
| } |
| 681 |
|
| |
| 682 |
|
| static frx_xn * |
| 683 |
979 |
| frx_xp_relational(frx_xparse *p) |
| 684 |
|
| { |
| 685 |
|
| static const int ops[] = { FRX_XT_LT, FRX_XN_LT, FRX_XT_LE, FRX_XN_LE, |
| 686 |
|
| FRX_XT_GT, FRX_XN_GT, FRX_XT_GE, FRX_XN_GE }; |
| 687 |
979 |
| return frx_xp_binary(p, ops, 4, frx_xp_additive); |
| 688 |
|
| } |
| 689 |
|
| |
| 690 |
|
| static frx_xn * |
| 691 |
959 |
| frx_xp_equality(frx_xparse *p) |
| 692 |
|
| { |
| 693 |
|
| static const int ops[] = { FRX_XT_EQ, FRX_XN_EQ, FRX_XT_NE, FRX_XN_NE }; |
| 694 |
959 |
| return frx_xp_binary(p, ops, 2, frx_xp_relational); |
| 695 |
|
| } |
| 696 |
|
| |
| 697 |
|
| static frx_xn * |
| 698 |
696 |
| frx_xp_and(frx_xparse *p) |
| 699 |
|
| { |
| 700 |
|
| static const int ops[] = { FRX_XT_AND, FRX_XN_AND }; |
| 701 |
696 |
| return frx_xp_binary(p, ops, 1, frx_xp_equality); |
| 702 |
|
| } |
| 703 |
|
| |
| 704 |
|
| static frx_xn * |
| 705 |
696 |
| frx_xp_or(frx_xparse *p) |
| 706 |
|
| { |
| 707 |
|
| static const int ops[] = { FRX_XT_OR, FRX_XN_OR }; |
| 708 |
696 |
| return frx_xp_binary(p, ops, 1, frx_xp_and); |
| 709 |
|
| } |
| 710 |
|
| |
| 711 |
|
| /* the one place the expression's depth is counted: every production above |
| 712 |
|
| * reaches the next through here */ |
| 713 |
|
| static frx_xn * |
| 714 |
697 |
| frx_xp_expr(frx_xparse *p) |
| 715 |
|
| { |
| 716 |
|
| frx_xn *n; |
| 717 |
697 |
▶ | if (p->err) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->err) return NULL; |
0 |
697 |
697 |
|
| 718 |
697 |
▶ | if (++p->depth > p->max_depth) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (++p->depth > p->max_depth) { |
1 |
696 |
697 |
|
| 719 |
1 |
| p->depth--; |
| 720 |
1 |
| return (frx_xn *)frx_xp_fail(p, "the expression nests deeper than max_expr_depth"); |
| 721 |
|
| } |
| 722 |
696 |
| n = frx_xp_or(p); |
| 723 |
696 |
| p->depth--; |
| 724 |
696 |
| return n; |
| 725 |
|
| } |
| 726 |
|
| |
| 727 |
|
| #endif /* FRX_XPATH_PARSE_H */ |