| 1 |
|
| #ifndef FRX_XPATH_LEX_H |
| 2 |
|
| #define FRX_XPATH_LEX_H |
| 3 |
|
| |
| 4 |
|
| /* XPath 1.0 tokens. |
| 5 |
|
| * |
| 6 |
|
| * The whole expression is lexed into an array before the parser runs, so |
| 7 |
|
| * that the parser has unbounded lookahead and the three disambiguation |
| 8 |
|
| * rules of section 3.7 can be applied where the specification states |
| 9 |
|
| * them: |
| 10 |
|
| * |
| 11 |
|
| * - `*` is the MultiplyOperator, and an NCName is an OperatorName |
| 12 |
|
| * (and, or, mod, div), when there is a preceding token and it is not |
| 13 |
|
| * one of `@`, `::`, `(`, `[`, `,` or an Operator. That rule needs the |
| 14 |
|
| * previous token only, and is applied here. |
| 15 |
|
| * - An NCName followed by `(` is a NodeType or a FunctionName, and one |
| 16 |
|
| * followed by `::` is an AxisName. Both need the next token, so both |
| 17 |
|
| * are the parser's, which is why the array exists. |
| 18 |
|
| * |
| 19 |
|
| * A Number is `Digits ('.' Digits?)? | '.' Digits`: no sign, no exponent, |
| 20 |
|
| * those being an operator and a name respectively. Once its extent is |
| 21 |
|
| * known it is converted by strtod, which is the only correctly rounded |
| 22 |
|
| * conversion available: accumulating the digits by hand drifts past |
| 23 |
|
| * seventeen of them. strtod reads the decimal point of the ambient |
| 24 |
|
| * LC_NUMERIC, which an embedding application may have set to a comma, so |
| 25 |
|
| * the separator is discovered from the C library itself and written into |
| 26 |
|
| * the buffer strtod is given. |
| 27 |
|
| * |
| 28 |
|
| * Names are NCNames and QNames under frx_utf8.h's tables, which are the |
| 29 |
|
| * XML productions XPath refers to. `prefix:*` is one NAME token holding |
| 30 |
|
| * the text as written; the parser splits it. |
| 31 |
|
| * |
| 32 |
|
| * The lexer allocates one growable array with malloc and the caller frees |
| 33 |
|
| * it with frx_xlex_free. Nothing here touches a document. |
| 34 |
|
| * |
| 35 |
|
| * Needs frx_err.h, frx_buf.h, frx_utf8.h, frx_tree.h (frx_str). */ |
| 36 |
|
| |
| 37 |
|
| #include <stdio.h> /* the one formatter the core uses; see below */ |
| 38 |
|
| |
| 39 |
|
| /* snprintf where the C library has it and sprintf where it does not. |
| 40 |
|
| * Every use writes one double into a buffer that cannot be too small, so |
| 41 |
|
| * the difference is not safety; it is that the platforms which deprecate |
| 42 |
|
| * sprintf are exactly the ones that have snprintf, and a warning a reader |
| 43 |
|
| * has to re-derive on every build is worth one macro. Strict C89 has |
| 44 |
|
| * neither the declaration nor the deprecation. */ |
| 45 |
|
| #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L |
| 46 |
|
| # define FRX_FMT1(buf, cap, fmt, arg) ((void)snprintf((buf), (cap), (fmt), (arg))) |
| 47 |
|
| #elif !defined(__STRICT_ANSI__) && !defined(_MSC_VER) |
| 48 |
|
| # define FRX_FMT1(buf, cap, fmt, arg) ((void)snprintf((buf), (cap), (fmt), (arg))) |
| 49 |
|
| #elif defined(_MSC_VER) |
| 50 |
|
| # define FRX_FMT1(buf, cap, fmt, arg) ((void)_snprintf((buf), (cap), (fmt), (arg))) |
| 51 |
|
| #else |
| 52 |
|
| # define FRX_FMT1(buf, cap, fmt, arg) ((void)sprintf((buf), (fmt), (arg))) |
| 53 |
|
| #endif |
| 54 |
|
| |
| 55 |
|
| enum { |
| 56 |
|
| FRX_XT_EOF = 0, |
| 57 |
|
| FRX_XT_LPAREN, FRX_XT_RPAREN, FRX_XT_LBRACK, FRX_XT_RBRACK, |
| 58 |
|
| FRX_XT_DOT, FRX_XT_DOTDOT, FRX_XT_AT, FRX_XT_COMMA, FRX_XT_COLON2, |
| 59 |
|
| FRX_XT_SLASH, FRX_XT_SLASH2, FRX_XT_PIPE, |
| 60 |
|
| FRX_XT_PLUS, FRX_XT_MINUS, FRX_XT_STAR, |
| 61 |
|
| FRX_XT_EQ, FRX_XT_NE, FRX_XT_LT, FRX_XT_LE, FRX_XT_GT, FRX_XT_GE, |
| 62 |
|
| FRX_XT_AND, FRX_XT_OR, FRX_XT_MOD, FRX_XT_DIV, |
| 63 |
|
| FRX_XT_NAME, FRX_XT_VAR, FRX_XT_LITERAL, FRX_XT_NUMBER |
| 64 |
|
| }; |
| 65 |
|
| |
| 66 |
|
| typedef struct frx_xtok { |
| 67 |
|
| int kind; |
| 68 |
|
| frx_str s; /* a name, a variable's name, a literal's text */ |
| 69 |
|
| double num; |
| 70 |
|
| size_t offset; /* where it began in the expression */ |
| 71 |
|
| } frx_xtok; |
| 72 |
|
| |
| 73 |
|
| typedef struct frx_xlex { |
| 74 |
|
| const char *in; |
| 75 |
|
| size_t len; |
| 76 |
|
| size_t at; |
| 77 |
|
| frx_xtok *t; |
| 78 |
|
| int n, cap; |
| 79 |
|
| const char *err; /* static; NULL while well-formed */ |
| 80 |
|
| size_t err_at; |
| 81 |
|
| } frx_xlex; |
| 82 |
|
| |
| 83 |
|
| static void |
| 84 |
238 |
| frx_xlex_init(frx_xlex *l, const char *in, size_t len) |
| 85 |
|
| { |
| 86 |
238 |
| memset(l, 0, sizeof *l); |
| 87 |
238 |
| l->in = in; |
| 88 |
238 |
| l->len = len; |
| 89 |
238 |
| } |
| 90 |
|
| |
| 91 |
|
| static void |
| 92 |
238 |
| frx_xlex_free(frx_xlex *l) |
| 93 |
|
| { |
| 94 |
238 |
| free(l->t); |
| 95 |
238 |
| l->t = NULL; |
| 96 |
238 |
| l->n = l->cap = 0; |
| 97 |
238 |
| } |
| 98 |
|
| |
| 99 |
|
| static int |
| 100 |
3 |
| frx_xlex_fail(frx_xlex *l, size_t at, const char *what) |
| 101 |
|
| { |
| 102 |
3 |
▶ | if (!l->err) { l->err = what; l->err_at = at; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!l->err) { l->err = what; l->err_at = at; } |
3 |
0 |
3 |
|
| 103 |
3 |
| return 0; |
| 104 |
|
| } |
| 105 |
|
| |
| 106 |
|
| static int |
| 107 |
2951 |
| frx_xlex_push(frx_xlex *l, int kind, size_t offset) |
| 108 |
|
| { |
| 109 |
2951 |
▶ | if (l->n == l->cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (l->n == l->cap) { |
250 |
2701 |
2951 |
|
| 110 |
250 |
▶ | int ncap = l->cap ? l->cap * 2 : 32; |
Branch: 1 branch
| Branch | True | False | Total |
| int ncap = l->cap ? l->cap * 2 : 32; |
14 |
236 |
250 |
|
| 111 |
250 |
| frx_xtok *nt = (frx_xtok *)realloc(l->t, (size_t)ncap * sizeof *nt); |
| 112 |
250 |
▶ | if (!nt) return frx_xlex_fail(l, offset, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nt) return frx_xlex_fail(l, offset, "out of memory"); |
0 |
250 |
250 |
|
| 113 |
250 |
| l->t = nt; |
| 114 |
250 |
| l->cap = ncap; |
| 115 |
|
| } |
| 116 |
2951 |
| memset(&l->t[l->n], 0, sizeof l->t[l->n]); |
| 117 |
2951 |
| l->t[l->n].kind = kind; |
| 118 |
2951 |
| l->t[l->n].offset = offset; |
| 119 |
2951 |
| l->t[l->n].s = FRX_EMPTY_STR; |
| 120 |
2951 |
| l->n++; |
| 121 |
2951 |
| return 1; |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
|
| /* Is an operator what may stand here? Section 3.7's first rule, stated as |
| 125 |
|
| * the set of preceding tokens after which one may not. */ |
| 126 |
|
| static int |
| 127 |
877 |
| frx_xlex_op_ok(const frx_xlex *l) |
| 128 |
|
| { |
| 129 |
877 |
▶ | if (!l->n) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!l->n) return 0; |
148 |
729 |
877 |
|
| 130 |
729 |
▶ | switch (l->t[l->n - 1].kind) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (l->t[l->n - 1].kind) { |
309 |
420 |
729 |
|
| 131 |
309 |
| case FRX_XT_AT: case FRX_XT_COLON2: case FRX_XT_LPAREN: |
| 132 |
|
| case FRX_XT_LBRACK: case FRX_XT_COMMA: |
| 133 |
|
| case FRX_XT_SLASH: case FRX_XT_SLASH2: case FRX_XT_PIPE: |
| 134 |
|
| case FRX_XT_PLUS: case FRX_XT_MINUS: case FRX_XT_STAR: |
| 135 |
|
| case FRX_XT_EQ: case FRX_XT_NE: case FRX_XT_LT: |
| 136 |
|
| case FRX_XT_LE: case FRX_XT_GT: case FRX_XT_GE: |
| 137 |
|
| case FRX_XT_AND: case FRX_XT_OR: case FRX_XT_MOD: |
| 138 |
|
| case FRX_XT_DIV: |
| 139 |
309 |
| return 0; |
| 140 |
420 |
| default: |
| 141 |
420 |
| return 1; |
| 142 |
|
| } |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
|
| /* the byte length of the NCName at p, 0 when there is none */ |
| 146 |
|
| static size_t |
| 147 |
883 |
| frx_xlex_ncname(const char *p, size_t n) |
| 148 |
|
| { |
| 149 |
|
| unsigned long cp; |
| 150 |
883 |
| size_t k, at = 0; |
| 151 |
883 |
| k = frx_utf8_decode((const unsigned char *)p, n, &cp); |
| 152 |
883 |
▶ | if (!k || !frx_is_name_start(cp)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!k || !frx_is_name_start(cp)) return 0; |
883 |
0 |
883 |
| if (!k || !frx_is_name_start(cp)) return 0; |
0 |
883 |
883 |
|
| 153 |
883 |
| at = k; |
| 154 |
|
| for (;;) { |
| 155 |
3790 |
| k = frx_utf8_decode((const unsigned char *)p + at, n - at, &cp); |
| 156 |
3790 |
▶ | if (!k || !frx_is_name_char(cp)) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!k || !frx_is_name_char(cp)) break; |
3754 |
36 |
3790 |
| if (!k || !frx_is_name_char(cp)) break; |
2907 |
847 |
3754 |
|
| 157 |
2907 |
| at += k; |
| 158 |
|
| } |
| 159 |
883 |
| return at; |
| 160 |
|
| } |
| 161 |
|
| |
| 162 |
|
| /* what this C library writes, and therefore reads, as a decimal point */ |
| 163 |
|
| static char |
| 164 |
538 |
| frx_xp_decimal_point(void) |
| 165 |
|
| { |
| 166 |
|
| static char sep = 0; |
| 167 |
538 |
▶ | if (!sep) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sep) { |
2 |
536 |
538 |
|
| 168 |
|
| char b[16]; |
| 169 |
2 |
| FRX_FMT1(b, sizeof b, "%.1f", 1.5); |
| 170 |
2 |
| sep = b[1]; |
| 171 |
|
| } |
| 172 |
538 |
| return sep; |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
|
| /* Digits with at most one '.' and an optional leading '-', already known |
| 176 |
|
| * to be exactly that, through strtod with the separator it wants. */ |
| 177 |
|
| static double |
| 178 |
538 |
| frx_xp_atod(const char *p, size_t n) |
| 179 |
|
| { |
| 180 |
|
| char stackbuf[64]; /* not `small`: rpcndr.h defines it to char */ |
| 181 |
538 |
| char *buf = stackbuf; |
| 182 |
538 |
| char sep = frx_xp_decimal_point(); |
| 183 |
|
| double v; |
| 184 |
|
| size_t i; |
| 185 |
|
| |
| 186 |
538 |
▶ | if (n + 1 > sizeof stackbuf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n + 1 > sizeof stackbuf) { |
0 |
538 |
538 |
|
| 187 |
0 |
| buf = (char *)malloc(n + 1); |
| 188 |
0 |
▶ | if (!buf) return 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) return 0.0; |
0 |
0 |
0 |
|
| 189 |
|
| } |
| 190 |
1159 |
▶ | for (i = 0; i < n; i++) buf[i] = p[i] == '.' ? sep : p[i]; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < n; i++) buf[i] = p[i] == '.' ? sep : p[i]; |
604 |
17 |
621 |
| for (i = 0; i < n; i++) buf[i] = p[i] == '.' ? sep : p[i]; |
621 |
538 |
1159 |
|
| 191 |
538 |
| buf[n] = '\0'; |
| 192 |
538 |
| v = strtod(buf, NULL); |
| 193 |
538 |
▶ | if (buf != stackbuf) free(buf); |
Branch: 1 branch
| Branch | True | False | Total |
| if (buf != stackbuf) free(buf); |
0 |
538 |
538 |
|
| 194 |
538 |
| return v; |
| 195 |
|
| } |
| 196 |
|
| |
| 197 |
|
| /* Digits ('.' Digits?)? | '.' Digits, already known to start here */ |
| 198 |
|
| static int |
| 199 |
524 |
| frx_xlex_number(frx_xlex *l) |
| 200 |
|
| { |
| 201 |
524 |
| size_t start = l->at; |
| 202 |
|
| |
| 203 |
1084 |
▶ | while (l->at < l->len && l->in[l->at] >= '0' && l->in[l->at] <= '9') l->at++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (l->at < l->len && l->in[l->at] >= '0' && l->in[l->at] <= '9') l->at++; |
1075 |
9 |
1084 |
| while (l->at < l->len && l->in[l->at] >= '0' && l->in[l->at] <= '9') l->at++; |
577 |
498 |
1075 |
| while (l->at < l->len && l->in[l->at] >= '0' && l->in[l->at] <= '9') l->at++; |
560 |
17 |
577 |
|
| 204 |
524 |
▶ | if (l->at < l->len && l->in[l->at] == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (l->at < l->len && l->in[l->at] == '.') { |
515 |
9 |
524 |
| if (l->at < l->len && l->in[l->at] == '.') { |
15 |
500 |
515 |
|
| 205 |
15 |
| l->at++; |
| 206 |
41 |
▶ | while (l->at < l->len && l->in[l->at] >= '0' && l->in[l->at] <= '9') l->at++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (l->at < l->len && l->in[l->at] >= '0' && l->in[l->at] <= '9') l->at++; |
41 |
0 |
41 |
| while (l->at < l->len && l->in[l->at] >= '0' && l->in[l->at] <= '9') l->at++; |
26 |
15 |
41 |
| while (l->at < l->len && l->in[l->at] >= '0' && l->in[l->at] <= '9') l->at++; |
26 |
0 |
26 |
|
| 207 |
|
| } |
| 208 |
524 |
▶ | if (!frx_xlex_push(l, FRX_XT_NUMBER, start)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xlex_push(l, FRX_XT_NUMBER, start)) return 0; |
0 |
524 |
524 |
|
| 209 |
524 |
| l->t[l->n - 1].num = frx_xp_atod(l->in + start, l->at - start); |
| 210 |
524 |
| return 1; |
| 211 |
|
| } |
| 212 |
|
| |
| 213 |
|
| /* The expression is UTF-8 or it is refused, before a token is read. A |
| 214 |
|
| * name is decoded scalar by scalar anyway, but a string literal is |
| 215 |
|
| * copied through whole, and an expression that reached here as Latin-1 |
| 216 |
|
| * bytes would otherwise put those bytes in a result the Perl layer flags |
| 217 |
|
| * as characters. Every string crossing this seam is UTF-8; this is where |
| 218 |
|
| * that is checked for an expression. */ |
| 219 |
|
| static int |
| 220 |
238 |
| frx_xlex_check_utf8(frx_xlex *l) |
| 221 |
|
| { |
| 222 |
238 |
| size_t at = 0; |
| 223 |
|
| unsigned long cp; |
| 224 |
7406 |
▶ | while (at < l->len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (at < l->len) { |
7169 |
237 |
7406 |
|
| 225 |
7169 |
| size_t k = frx_utf8_decode((const unsigned char *)l->in + at, l->len - at, &cp); |
| 226 |
7169 |
▶ | if (!k) return frx_xlex_fail(l, at, "the expression is not UTF-8"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k) return frx_xlex_fail(l, at, "the expression is not UTF-8"); |
1 |
7168 |
7169 |
|
| 227 |
7168 |
▶ | if (!frx_is_char(cp)) return frx_xlex_fail(l, at, "the expression holds a character XML has no name for"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_is_char(cp)) return frx_xlex_fail(l, at, "the expression holds a character XML has no name for"); |
0 |
7168 |
7168 |
|
| 228 |
7168 |
| at += k; |
| 229 |
|
| } |
| 230 |
237 |
| return 1; |
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
|
| static int |
| 234 |
238 |
| frx_xlex_run(frx_xlex *l) |
| 235 |
|
| { |
| 236 |
238 |
▶ | if (!frx_xlex_check_utf8(l)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xlex_check_utf8(l)) return 0; |
1 |
237 |
238 |
|
| 237 |
3912 |
▶ | while (l->at < l->len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (l->at < l->len) { |
3677 |
235 |
3912 |
|
| 238 |
3677 |
| char c = l->in[l->at]; |
| 239 |
3677 |
| size_t start = l->at; |
| 240 |
|
| |
| 241 |
3677 |
▶ | if (frx_is_s((unsigned char)c)) { l->at++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_is_s((unsigned char)c)) { l->at++; continue; } |
959 |
2718 |
3677 |
|
| 242 |
|
| |
| 243 |
2718 |
| switch (c) { |
| 244 |
424 |
▶ | case '(': l->at++; if (!frx_xlex_push(l, FRX_XT_LPAREN, start)) return 0; continue; |
Branch: 1 branch
| Branch | True | False | Total |
| case '(': l->at++; if (!frx_xlex_push(l, FRX_XT_LPAREN, start)) return 0; continue; |
0 |
424 |
424 |
|
| 245 |
423 |
▶ | case ')': l->at++; if (!frx_xlex_push(l, FRX_XT_RPAREN, start)) return 0; continue; |
Branch: 1 branch
| Branch | True | False | Total |
| case ')': l->at++; if (!frx_xlex_push(l, FRX_XT_RPAREN, start)) return 0; continue; |
0 |
423 |
423 |
|
| 246 |
29 |
▶ | case '[': l->at++; if (!frx_xlex_push(l, FRX_XT_LBRACK, start)) return 0; continue; |
Branch: 1 branch
| Branch | True | False | Total |
| case '[': l->at++; if (!frx_xlex_push(l, FRX_XT_LBRACK, start)) return 0; continue; |
0 |
29 |
29 |
|
| 247 |
26 |
▶ | case ']': l->at++; if (!frx_xlex_push(l, FRX_XT_RBRACK, start)) return 0; continue; |
Branch: 1 branch
| Branch | True | False | Total |
| case ']': l->at++; if (!frx_xlex_push(l, FRX_XT_RBRACK, start)) return 0; continue; |
0 |
26 |
26 |
|
| 248 |
17 |
▶ | case '@': l->at++; if (!frx_xlex_push(l, FRX_XT_AT, start)) return 0; continue; |
Branch: 1 branch
| Branch | True | False | Total |
| case '@': l->at++; if (!frx_xlex_push(l, FRX_XT_AT, start)) return 0; continue; |
0 |
17 |
17 |
|
| 249 |
42 |
▶ | case ',': l->at++; if (!frx_xlex_push(l, FRX_XT_COMMA, start)) return 0; continue; |
Branch: 1 branch
| Branch | True | False | Total |
| case ',': l->at++; if (!frx_xlex_push(l, FRX_XT_COMMA, start)) return 0; continue; |
0 |
42 |
42 |
|
| 250 |
8 |
▶ | case '+': l->at++; if (!frx_xlex_push(l, FRX_XT_PLUS, start)) return 0; continue; |
Branch: 1 branch
| Branch | True | False | Total |
| case '+': l->at++; if (!frx_xlex_push(l, FRX_XT_PLUS, start)) return 0; continue; |
0 |
8 |
8 |
|
| 251 |
14 |
▶ | case '-': l->at++; if (!frx_xlex_push(l, FRX_XT_MINUS, start)) return 0; continue; |
Branch: 1 branch
| Branch | True | False | Total |
| case '-': l->at++; if (!frx_xlex_push(l, FRX_XT_MINUS, start)) return 0; continue; |
0 |
14 |
14 |
|
| 252 |
4 |
▶ | case '|': l->at++; if (!frx_xlex_push(l, FRX_XT_PIPE, start)) return 0; continue; |
Branch: 1 branch
| Branch | True | False | Total |
| case '|': l->at++; if (!frx_xlex_push(l, FRX_XT_PIPE, start)) return 0; continue; |
0 |
4 |
4 |
|
| 253 |
19 |
▶ | case '=': l->at++; if (!frx_xlex_push(l, FRX_XT_EQ, start)) return 0; continue; |
Branch: 1 branch
| Branch | True | False | Total |
| case '=': l->at++; if (!frx_xlex_push(l, FRX_XT_EQ, start)) return 0; continue; |
0 |
19 |
19 |
|
| 254 |
33 |
| case '*': |
| 255 |
33 |
| l->at++; |
| 256 |
33 |
▶ | if (!frx_xlex_push(l, frx_xlex_op_ok(l) ? FRX_XT_STAR : FRX_XT_NAME, start)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!frx_xlex_push(l, frx_xlex_op_ok(l) ? FRX_XT_STAR : FRX_XT_NAME, start)) return 0; |
2 |
31 |
33 |
| if (!frx_xlex_push(l, frx_xlex_op_ok(l) ? FRX_XT_STAR : FRX_XT_NAME, start)) return 0; |
0 |
33 |
33 |
|
| 257 |
33 |
▶ | if (l->t[l->n - 1].kind == FRX_XT_NAME) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (l->t[l->n - 1].kind == FRX_XT_NAME) { |
31 |
2 |
33 |
|
| 258 |
31 |
| l->t[l->n - 1].s.p = l->in + start; |
| 259 |
31 |
| l->t[l->n - 1].s.len = 1; |
| 260 |
|
| } |
| 261 |
33 |
| continue; |
| 262 |
172 |
| case '/': |
| 263 |
172 |
| l->at++; |
| 264 |
172 |
▶ | if (l->at < l->len && l->in[l->at] == '/') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (l->at < l->len && l->in[l->at] == '/') { |
171 |
1 |
172 |
| if (l->at < l->len && l->in[l->at] == '/') { |
106 |
65 |
171 |
|
| 265 |
106 |
| l->at++; |
| 266 |
106 |
▶ | if (!frx_xlex_push(l, FRX_XT_SLASH2, start)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xlex_push(l, FRX_XT_SLASH2, start)) return 0; |
0 |
106 |
106 |
|
| 267 |
66 |
▶ | } else if (!frx_xlex_push(l, FRX_XT_SLASH, start)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (!frx_xlex_push(l, FRX_XT_SLASH, start)) return 0; |
0 |
66 |
66 |
|
| 268 |
172 |
| continue; |
| 269 |
1 |
| case '!': |
| 270 |
1 |
▶ | if (l->at + 1 < l->len && l->in[l->at + 1] == '=') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (l->at + 1 < l->len && l->in[l->at + 1] == '=') { |
1 |
0 |
1 |
| if (l->at + 1 < l->len && l->in[l->at + 1] == '=') { |
1 |
0 |
1 |
|
| 271 |
1 |
| l->at += 2; |
| 272 |
1 |
▶ | if (!frx_xlex_push(l, FRX_XT_NE, start)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xlex_push(l, FRX_XT_NE, start)) return 0; |
0 |
1 |
1 |
|
| 273 |
1 |
| continue; |
| 274 |
|
| } |
| 275 |
0 |
| return frx_xlex_fail(l, start, "'!' is only the start of '!='"); |
| 276 |
7 |
| case '<': |
| 277 |
7 |
| case '>': { |
| 278 |
7 |
| int ge = c == '>'; |
| 279 |
7 |
| l->at++; |
| 280 |
7 |
▶ | if (l->at < l->len && l->in[l->at] == '=') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (l->at < l->len && l->in[l->at] == '=') { |
7 |
0 |
7 |
| if (l->at < l->len && l->in[l->at] == '=') { |
0 |
7 |
7 |
|
| 281 |
0 |
| l->at++; |
| 282 |
0 |
▶ | if (!frx_xlex_push(l, ge ? FRX_XT_GE : FRX_XT_LE, start)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!frx_xlex_push(l, ge ? FRX_XT_GE : FRX_XT_LE, start)) return 0; |
0 |
0 |
0 |
| if (!frx_xlex_push(l, ge ? FRX_XT_GE : FRX_XT_LE, start)) return 0; |
0 |
0 |
0 |
|
| 283 |
7 |
▶ | } else if (!frx_xlex_push(l, ge ? FRX_XT_GT : FRX_XT_LT, start)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (!frx_xlex_push(l, ge ? FRX_XT_GT : FRX_XT_LT, start)) return 0; |
4 |
3 |
7 |
| } else if (!frx_xlex_push(l, ge ? FRX_XT_GT : FRX_XT_LT, start)) return 0; |
0 |
7 |
7 |
|
| 284 |
7 |
| continue; |
| 285 |
|
| } |
| 286 |
81 |
| case '"': |
| 287 |
80 |
| case '\'': { |
| 288 |
81 |
| const char *end = (const char *)memchr(l->in + l->at + 1, c, l->len - l->at - 1); |
| 289 |
81 |
▶ | if (!end) return frx_xlex_fail(l, start, "a string literal is never closed"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!end) return frx_xlex_fail(l, start, "a string literal is never closed"); |
1 |
80 |
81 |
|
| 290 |
80 |
▶ | if (!frx_xlex_push(l, FRX_XT_LITERAL, start)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xlex_push(l, FRX_XT_LITERAL, start)) return 0; |
0 |
80 |
80 |
|
| 291 |
80 |
| l->t[l->n - 1].s.p = l->in + l->at + 1; |
| 292 |
80 |
| l->t[l->n - 1].s.len = (size_t)(end - (l->in + l->at + 1)); |
| 293 |
80 |
| l->at = (size_t)(end - l->in) + 1; |
| 294 |
80 |
| continue; |
| 295 |
|
| } |
| 296 |
20 |
| case '$': { |
| 297 |
|
| size_t k; |
| 298 |
10 |
| l->at++; |
| 299 |
10 |
| k = frx_xlex_ncname(l->in + l->at, l->len - l->at); |
| 300 |
10 |
▶ | if (!k) return frx_xlex_fail(l, start, "'$' must be followed by a variable name"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k) return frx_xlex_fail(l, start, "'$' must be followed by a variable name"); |
0 |
10 |
10 |
|
| 301 |
10 |
▶ | if (!frx_xlex_push(l, FRX_XT_VAR, start)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xlex_push(l, FRX_XT_VAR, start)) return 0; |
0 |
10 |
10 |
|
| 302 |
10 |
| l->t[l->n - 1].s.p = l->in + l->at; |
| 303 |
10 |
| l->t[l->n - 1].s.len = k; |
| 304 |
10 |
| l->at += k; |
| 305 |
|
| /* a variable reference is a QName; a prefix is not resolvable |
| 306 |
|
| * without a binding for variables, so only NCNames are taken */ |
| 307 |
10 |
▶ | if (l->at < l->len && l->in[l->at] == ':') |
Branch: 2 branches
| Branch | True | False | Total |
| if (l->at < l->len && l->in[l->at] == ':') |
5 |
5 |
10 |
| if (l->at < l->len && l->in[l->at] == ':') |
0 |
5 |
5 |
|
| 308 |
0 |
| return frx_xlex_fail(l, start, "a variable name with a prefix is not bound"); |
| 309 |
10 |
| continue; |
| 310 |
|
| } |
| 311 |
24 |
| case ':': |
| 312 |
24 |
▶ | if (l->at + 1 < l->len && l->in[l->at + 1] == ':') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (l->at + 1 < l->len && l->in[l->at + 1] == ':') { |
24 |
0 |
24 |
| if (l->at + 1 < l->len && l->in[l->at + 1] == ':') { |
24 |
0 |
24 |
|
| 313 |
24 |
| l->at += 2; |
| 314 |
24 |
▶ | if (!frx_xlex_push(l, FRX_XT_COLON2, start)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xlex_push(l, FRX_XT_COLON2, start)) return 0; |
0 |
24 |
24 |
|
| 315 |
24 |
| continue; |
| 316 |
|
| } |
| 317 |
0 |
| return frx_xlex_fail(l, start, "':' is only the start of '::' or the middle of a name"); |
| 318 |
1 |
| case '.': |
| 319 |
1 |
▶ | if (l->at + 1 < l->len && l->in[l->at + 1] == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (l->at + 1 < l->len && l->in[l->at + 1] == '.') { |
1 |
0 |
1 |
| if (l->at + 1 < l->len && l->in[l->at + 1] == '.') { |
1 |
0 |
1 |
|
| 320 |
1 |
| l->at += 2; |
| 321 |
1 |
▶ | if (!frx_xlex_push(l, FRX_XT_DOTDOT, start)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xlex_push(l, FRX_XT_DOTDOT, start)) return 0; |
0 |
1 |
1 |
|
| 322 |
1 |
| continue; |
| 323 |
|
| } |
| 324 |
0 |
▶ | if (l->at + 1 < l->len && l->in[l->at + 1] >= '0' && l->in[l->at + 1] <= '9') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (l->at + 1 < l->len && l->in[l->at + 1] >= '0' && l->in[l->at + 1] <= '9') { |
0 |
0 |
0 |
| if (l->at + 1 < l->len && l->in[l->at + 1] >= '0' && l->in[l->at + 1] <= '9') { |
0 |
0 |
0 |
| if (l->at + 1 < l->len && l->in[l->at + 1] >= '0' && l->in[l->at + 1] <= '9') { |
0 |
0 |
0 |
|
| 325 |
0 |
▶ | if (!frx_xlex_number(l)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xlex_number(l)) return 0; |
0 |
0 |
0 |
|
| 326 |
0 |
| continue; |
| 327 |
|
| } |
| 328 |
0 |
| l->at++; |
| 329 |
0 |
▶ | if (!frx_xlex_push(l, FRX_XT_DOT, start)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xlex_push(l, FRX_XT_DOT, start)) return 0; |
0 |
0 |
0 |
|
| 330 |
0 |
| continue; |
| 331 |
1383 |
| default: |
| 332 |
1383 |
| break; |
| 333 |
|
| } |
| 334 |
|
| |
| 335 |
1383 |
▶ | if (c >= '0' && c <= '9') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= '0' && c <= '9') { |
1383 |
0 |
1383 |
| if (c >= '0' && c <= '9') { |
524 |
859 |
1383 |
|
| 336 |
524 |
▶ | if (!frx_xlex_number(l)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xlex_number(l)) return 0; |
0 |
524 |
524 |
|
| 337 |
524 |
| continue; |
| 338 |
|
| } |
| 339 |
|
| |
| 340 |
441 |
| { |
| 341 |
859 |
| size_t k = frx_xlex_ncname(l->in + l->at, l->len - l->at); |
| 342 |
|
| size_t total; |
| 343 |
859 |
▶ | if (!k) return frx_xlex_fail(l, start, "not a name, an operator or a literal"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k) return frx_xlex_fail(l, start, "not a name, an operator or a literal"); |
0 |
859 |
859 |
|
| 344 |
859 |
| total = k; |
| 345 |
859 |
▶ | if (l->at + total + 1 < l->len && l->in[l->at + total] == ':' |
Branch: 2 branches
| Branch | True | False | Total |
| if (l->at + total + 1 < l->len && l->in[l->at + total] == ':' |
822 |
37 |
859 |
| if (l->at + total + 1 < l->len && l->in[l->at + total] == ':' |
39 |
783 |
822 |
|
| 346 |
39 |
▶ | && l->in[l->at + total + 1] != ':') { |
Branch: 1 branch
| Branch | True | False | Total |
| && l->in[l->at + total + 1] != ':') { |
15 |
24 |
39 |
|
| 347 |
15 |
▶ | if (l->in[l->at + total + 1] == '*') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (l->in[l->at + total + 1] == '*') { |
1 |
14 |
15 |
|
| 348 |
1 |
| total += 2; /* prefix:* */ |
| 349 |
|
| } else { |
| 350 |
14 |
| size_t k2 = frx_xlex_ncname(l->in + l->at + total + 1, |
| 351 |
14 |
| l->len - l->at - total - 1); |
| 352 |
14 |
▶ | if (!k2) return frx_xlex_fail(l, start, "a colon must be followed by a name or '*'"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k2) return frx_xlex_fail(l, start, "a colon must be followed by a name or '*'"); |
0 |
14 |
14 |
|
| 353 |
14 |
| total += 1 + k2; |
| 354 |
|
| } |
| 355 |
|
| } |
| 356 |
859 |
▶ | if (total == k && frx_xlex_op_ok(l)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (total == k && frx_xlex_op_ok(l)) { |
844 |
15 |
859 |
| if (total == k && frx_xlex_op_ok(l)) { |
418 |
426 |
844 |
|
| 357 |
418 |
| int op = 0; |
| 358 |
418 |
▶ | if (k == 3 && memcmp(l->in + l->at, "and", 3) == 0) op = FRX_XT_AND; |
Branch: 2 branches
| Branch | True | False | Total |
| if (k == 3 && memcmp(l->in + l->at, "and", 3) == 0) op = FRX_XT_AND; |
417 |
1 |
418 |
| if (k == 3 && memcmp(l->in + l->at, "and", 3) == 0) op = FRX_XT_AND; |
399 |
18 |
417 |
|
| 359 |
19 |
▶ | else if (k == 2 && memcmp(l->in + l->at, "or", 2) == 0) op = FRX_XT_OR; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (k == 2 && memcmp(l->in + l->at, "or", 2) == 0) op = FRX_XT_OR; |
1 |
18 |
19 |
| else if (k == 2 && memcmp(l->in + l->at, "or", 2) == 0) op = FRX_XT_OR; |
0 |
1 |
1 |
|
| 360 |
19 |
▶ | else if (k == 3 && memcmp(l->in + l->at, "mod", 3) == 0) op = FRX_XT_MOD; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (k == 3 && memcmp(l->in + l->at, "mod", 3) == 0) op = FRX_XT_MOD; |
18 |
1 |
19 |
| else if (k == 3 && memcmp(l->in + l->at, "mod", 3) == 0) op = FRX_XT_MOD; |
4 |
14 |
18 |
|
| 361 |
15 |
▶ | else if (k == 3 && memcmp(l->in + l->at, "div", 3) == 0) op = FRX_XT_DIV; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (k == 3 && memcmp(l->in + l->at, "div", 3) == 0) op = FRX_XT_DIV; |
14 |
1 |
15 |
| else if (k == 3 && memcmp(l->in + l->at, "div", 3) == 0) op = FRX_XT_DIV; |
14 |
0 |
14 |
|
| 362 |
418 |
▶ | if (op) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (op) { |
417 |
1 |
418 |
|
| 363 |
417 |
| l->at += k; |
| 364 |
417 |
▶ | if (!frx_xlex_push(l, op, start)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xlex_push(l, op, start)) return 0; |
0 |
417 |
417 |
|
| 365 |
417 |
| continue; |
| 366 |
|
| } |
| 367 |
1 |
| return frx_xlex_fail(l, start, "an operator was expected here, and this name is not one"); |
| 368 |
|
| } |
| 369 |
441 |
▶ | if (!frx_xlex_push(l, FRX_XT_NAME, start)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xlex_push(l, FRX_XT_NAME, start)) return 0; |
0 |
441 |
441 |
|
| 370 |
441 |
| l->t[l->n - 1].s.p = l->in + l->at; |
| 371 |
441 |
| l->t[l->n - 1].s.len = total; |
| 372 |
441 |
| l->at += total; |
| 373 |
441 |
| continue; |
| 374 |
|
| } |
| 375 |
|
| } |
| 376 |
235 |
| return frx_xlex_push(l, FRX_XT_EOF, l->len); |
| 377 |
|
| } |
| 378 |
|
| |
| 379 |
|
| #endif /* FRX_XPATH_LEX_H */ |