| 1 |
|
| /* po_lex.h - the query language's lexer. |
| 2 |
|
| * |
| 3 |
|
| * Hand-written, no generator, no regex. About 200 lines, and it is the reason |
| 4 |
|
| * the language reads the way it does: DURATIONS ARE FIRST-CLASS TOKENS. |
| 5 |
|
| * `duration > 500ms` is not a function call and not a string that something |
| 6 |
|
| * later parses; the lexer knows what `500ms` is, so the parser can type-check |
| 7 |
|
| * it against the column. |
| 8 |
|
| * |
| 9 |
|
| * Every token carries its byte OFFSET. An error that says "syntax error" is a |
| 10 |
|
| * language people avoid; an error that points at the character is one they |
| 11 |
|
| * can use. |
| 12 |
|
| */ |
| 13 |
|
| #ifndef PO_LEX_H |
| 14 |
|
| #define PO_LEX_H |
| 15 |
|
| |
| 16 |
|
| #include "punk_observe/po_compat.h" |
| 17 |
|
| |
| 18 |
|
| typedef enum { |
| 19 |
|
| PO_T_EOF = 0, |
| 20 |
|
| PO_T_IDENT, /* bare word: a keyword, a column, or a metric name */ |
| 21 |
|
| PO_T_STRING, /* quoted */ |
| 22 |
|
| PO_T_NUMBER, /* integer or float */ |
| 23 |
|
| PO_T_DURATION, /* 500ms, 5m, 2h, 7d - value in NANOSECONDS */ |
| 24 |
|
| PO_T_PIPE, /* | */ |
| 25 |
|
| PO_T_LPAREN, PO_T_RPAREN, |
| 26 |
|
| PO_T_LBRACE, PO_T_RBRACE, |
| 27 |
|
| PO_T_COMMA, |
| 28 |
|
| PO_T_OP, /* = != < <= > >= =~ !~ */ |
| 29 |
|
| PO_T_ERROR |
| 30 |
|
| } po_tok_kind; |
| 31 |
|
| |
| 32 |
|
| /* comparison operators, in `op` */ |
| 33 |
|
| #define PO_OP_EQ 1 |
| 34 |
|
| #define PO_OP_NE 2 |
| 35 |
|
| #define PO_OP_LT 3 |
| 36 |
|
| #define PO_OP_LE 4 |
| 37 |
|
| #define PO_OP_GT 5 |
| 38 |
|
| #define PO_OP_GE 6 |
| 39 |
|
| #define PO_OP_MATCH 7 /* =~ */ |
| 40 |
|
| #define PO_OP_NMATCH 8 /* !~ */ |
| 41 |
|
| |
| 42 |
|
| typedef struct { |
| 43 |
|
| po_tok_kind kind; |
| 44 |
|
| const char *p; /* into the query string; borrowed */ |
| 45 |
|
| size_t len; |
| 46 |
|
| size_t off; /* byte offset, for the error message */ |
| 47 |
|
| po_u64 dur; /* PO_T_DURATION: nanoseconds */ |
| 48 |
|
| double num; /* PO_T_NUMBER */ |
| 49 |
|
| int is_int; /* PO_T_NUMBER: no fractional part seen */ |
| 50 |
|
| po_u64 inum; /* PO_T_NUMBER when is_int: exact, no NV rounding */ |
| 51 |
|
| int op; |
| 52 |
|
| } po_tok; |
| 53 |
|
| |
| 54 |
|
| typedef struct { |
| 55 |
|
| const char *src; |
| 56 |
|
| size_t len; |
| 57 |
|
| size_t pos; |
| 58 |
|
| po_tok cur; |
| 59 |
|
| char err[192]; |
| 60 |
|
| size_t err_off; |
| 61 |
|
| int failed; |
| 62 |
|
| } po_lex; |
| 63 |
|
| |
| 64 |
7 |
| static void po_lex_fail(po_lex *l, size_t off, const char *msg) { |
| 65 |
7 |
▶ | if (l->failed) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (l->failed) return; |
0 |
7 |
7 |
|
| 66 |
7 |
| l->failed = 1; |
| 67 |
7 |
| l->err_off = off; |
| 68 |
|
| { |
| 69 |
7 |
| size_t n = strlen(msg); |
| 70 |
7 |
▶ | if (n >= sizeof(l->err)) n = sizeof(l->err) - 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n >= sizeof(l->err)) n = sizeof(l->err) - 1; |
0 |
7 |
7 |
|
| 71 |
7 |
| memcpy(l->err, msg, n); |
| 72 |
7 |
| l->err[n] = '\0'; |
| 73 |
|
| } |
| 74 |
7 |
| l->cur.kind = PO_T_ERROR; |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
1233585 |
| static int po_is_ident_start(char c) { |
| 78 |
1233585 |
▶ | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; |
Branch: 5 branches
| Branch | True | False | Total |
| return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; |
1042801 |
190784 |
1233585 |
| return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; |
4 |
1042797 |
1042801 |
| return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; |
31 |
190757 |
190788 |
| return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; |
25 |
6 |
31 |
| return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; |
21 |
190761 |
190782 |
|
| 79 |
|
| } |
| 80 |
1092113 |
| static int po_is_ident(char c) { |
| 81 |
|
| /* Dots are part of an identifier: `http.response.status_code` is ONE |
| 82 |
|
| * column name, not a field access. Semantic conventions are dotted, so a |
| 83 |
|
| * lexer that split on dots would make every real attribute unquotable. */ |
| 84 |
1092113 |
▶ | return po_is_ident_start(c) || (c >= '0' && c <= '9') || c == '.' || c == '-'; |
Branch: 5 branches
| Branch | True | False | Total |
| return po_is_ident_start(c) || (c >= '0' && c <= '9') || c == '.' || c == '-'; |
190759 |
901354 |
1092113 |
| return po_is_ident_start(c) || (c >= '0' && c <= '9') || c == '.' || c == '-'; |
20064 |
170695 |
190759 |
| return po_is_ident_start(c) || (c >= '0' && c <= '9') || c == '.' || c == '-'; |
11 |
20053 |
20064 |
| return po_is_ident_start(c) || (c >= '0' && c <= '9') || c == '.' || c == '-'; |
130479 |
40227 |
170706 |
| return po_is_ident_start(c) || (c >= '0' && c <= '9') || c == '.' || c == '-'; |
0 |
130479 |
130479 |
|
| 85 |
|
| } |
| 86 |
159729 |
▶ | static int po_is_digit(char c) { return c >= '0' && c <= '9'; } |
Branch: 2 branches
| Branch | True | False | Total |
| static int po_is_digit(char c) { return c >= '0' && c <= '9'; } |
159659 |
70 |
159729 |
| static int po_is_digit(char c) { return c >= '0' && c <= '9'; } |
12556 |
147103 |
159659 |
|
| 87 |
|
| |
| 88 |
|
| /* A duration suffix, in nanoseconds. `m` is minutes; there is deliberately no |
| 89 |
|
| * month, because `1m` meaning a month somewhere would be a trap nobody |
| 90 |
|
| * recovers from - and because a month has no length, so `1mo` would have to |
| 91 |
|
| * pick one and be wrong eleven times a year. |
| 92 |
|
| * |
| 93 |
|
| * `y` IS EXACTLY 365 DAYS, not a calendar year. Retention is "delete what is |
| 94 |
|
| * older than this many nanoseconds", which is arithmetic and not a calendar |
| 95 |
|
| * operation: there is no date to be relative to, so a leap day has nothing to |
| 96 |
|
| * attach to. It is the same 365 days Prometheus and Go's duration parser |
| 97 |
|
| * mean, and the POD says so rather than leaving somebody to discover that |
| 98 |
|
| * seven years is 2,555 days and not 2,557. */ |
| 99 |
5933 |
| static po_u64 po_dur_unit(const char *p, size_t n) { |
| 100 |
5933 |
▶ | if (n == 2 && p[0] == 'n' && p[1] == 's') return 1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (n == 2 && p[0] == 'n' && p[1] == 's') return 1; |
107 |
5826 |
5933 |
| if (n == 2 && p[0] == 'n' && p[1] == 's') return 1; |
32 |
75 |
107 |
| if (n == 2 && p[0] == 'n' && p[1] == 's') return 1; |
32 |
0 |
32 |
|
| 101 |
5901 |
▶ | if (n == 2 && p[0] == 'u' && p[1] == 's') return 1000; |
Branch: 3 branches
| Branch | True | False | Total |
| if (n == 2 && p[0] == 'u' && p[1] == 's') return 1000; |
75 |
5826 |
5901 |
| if (n == 2 && p[0] == 'u' && p[1] == 's') return 1000; |
29 |
46 |
75 |
| if (n == 2 && p[0] == 'u' && p[1] == 's') return 1000; |
29 |
0 |
29 |
|
| 102 |
5872 |
▶ | if (n == 2 && p[0] == 'm' && p[1] == 's') return 1000000; |
Branch: 3 branches
| Branch | True | False | Total |
| if (n == 2 && p[0] == 'm' && p[1] == 's') return 1000000; |
46 |
5826 |
5872 |
| if (n == 2 && p[0] == 'm' && p[1] == 's') return 1000000; |
45 |
1 |
46 |
| if (n == 2 && p[0] == 'm' && p[1] == 's') return 1000000; |
44 |
1 |
45 |
|
| 103 |
5828 |
▶ | if (n == 1 && p[0] == 's') return 1000000000ULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == 1 && p[0] == 's') return 1000000000ULL; |
5824 |
4 |
5828 |
| if (n == 1 && p[0] == 's') return 1000000000ULL; |
5167 |
657 |
5824 |
|
| 104 |
661 |
▶ | if (n == 1 && p[0] == 'm') return 60ULL * 1000000000ULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == 1 && p[0] == 'm') return 60ULL * 1000000000ULL; |
657 |
4 |
661 |
| if (n == 1 && p[0] == 'm') return 60ULL * 1000000000ULL; |
521 |
136 |
657 |
|
| 105 |
140 |
▶ | if (n == 1 && p[0] == 'h') return 3600ULL * 1000000000ULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == 1 && p[0] == 'h') return 3600ULL * 1000000000ULL; |
136 |
4 |
140 |
| if (n == 1 && p[0] == 'h') return 3600ULL * 1000000000ULL; |
44 |
92 |
136 |
|
| 106 |
96 |
▶ | if (n == 1 && p[0] == 'd') return 86400ULL * 1000000000ULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == 1 && p[0] == 'd') return 86400ULL * 1000000000ULL; |
92 |
4 |
96 |
| if (n == 1 && p[0] == 'd') return 86400ULL * 1000000000ULL; |
34 |
58 |
92 |
|
| 107 |
62 |
▶ | if (n == 1 && p[0] == 'w') return 7ULL * 86400ULL * 1000000000ULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == 1 && p[0] == 'w') return 7ULL * 86400ULL * 1000000000ULL; |
58 |
4 |
62 |
| if (n == 1 && p[0] == 'w') return 7ULL * 86400ULL * 1000000000ULL; |
28 |
30 |
58 |
|
| 108 |
34 |
▶ | if (n == 1 && p[0] == 'y') return 365ULL * 86400ULL * 1000000000ULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == 1 && p[0] == 'y') return 365ULL * 86400ULL * 1000000000ULL; |
30 |
4 |
34 |
| if (n == 1 && p[0] == 'y') return 365ULL * 86400ULL * 1000000000ULL; |
28 |
2 |
30 |
|
| 109 |
6 |
| return 0; |
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
16933 |
| static void po_lex_init(po_lex *l, const char *src, size_t len) { |
| 113 |
16933 |
| memset(l, 0, sizeof(*l)); |
| 114 |
16933 |
| l->src = src; |
| 115 |
16933 |
| l->len = len; |
| 116 |
16933 |
| } |
| 117 |
|
| |
| 118 |
268788 |
| static void po_lex_next(po_lex *l) { |
| 119 |
268788 |
▶ | if (l->failed) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (l->failed) return; |
0 |
268788 |
268788 |
|
| 120 |
|
| |
| 121 |
501810 |
▶ | while (l->pos < l->len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (l->pos < l->len) { |
484922 |
16888 |
501810 |
|
| 122 |
484922 |
| char c = l->src[l->pos]; |
| 123 |
484922 |
▶ | if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { l->pos++; continue; } |
Branch: 4 branches
| Branch | True | False | Total |
| if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { l->pos++; continue; } |
251904 |
233018 |
484922 |
| if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { l->pos++; continue; } |
251904 |
0 |
251904 |
| if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { l->pos++; continue; } |
251902 |
2 |
251904 |
| if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { l->pos++; continue; } |
0 |
251902 |
251902 |
|
| 124 |
251902 |
▶ | if (c == '#') { /* a comment to end of line */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '#') { /* a comment to end of line */ |
2 |
251900 |
251902 |
|
| 125 |
34 |
▶ | while (l->pos < l->len && l->src[l->pos] != '\n') l->pos++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (l->pos < l->len && l->src[l->pos] != '\n') l->pos++; |
33 |
1 |
34 |
| while (l->pos < l->len && l->src[l->pos] != '\n') l->pos++; |
32 |
1 |
33 |
|
| 126 |
2 |
| continue; |
| 127 |
|
| } |
| 128 |
251900 |
| break; |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
268788 |
| memset(&l->cur, 0, sizeof(l->cur)); |
| 132 |
268788 |
| l->cur.off = l->pos; |
| 133 |
268788 |
| l->cur.p = l->src + l->pos; |
| 134 |
|
| |
| 135 |
268788 |
▶ | if (l->pos >= l->len) { l->cur.kind = PO_T_EOF; return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (l->pos >= l->len) { l->cur.kind = PO_T_EOF; return; } |
16888 |
251900 |
268788 |
|
| 136 |
|
| |
| 137 |
|
| { |
| 138 |
251900 |
| char c = l->src[l->pos]; |
| 139 |
|
| |
| 140 |
251900 |
| switch (c) { |
| 141 |
57051 |
| case '|': l->pos++; l->cur.kind = PO_T_PIPE; l->cur.len = 1; return; |
| 142 |
693 |
| case '(': l->pos++; l->cur.kind = PO_T_LPAREN; l->cur.len = 1; return; |
| 143 |
622 |
| case ')': l->pos++; l->cur.kind = PO_T_RPAREN; l->cur.len = 1; return; |
| 144 |
9 |
| case '{': l->pos++; l->cur.kind = PO_T_LBRACE; l->cur.len = 1; return; |
| 145 |
8 |
| case '}': l->pos++; l->cur.kind = PO_T_RBRACE; l->cur.len = 1; return; |
| 146 |
14 |
| case ',': l->pos++; l->cur.kind = PO_T_COMMA; l->cur.len = 1; return; |
| 147 |
193503 |
| default: break; |
| 148 |
|
| } |
| 149 |
|
| |
| 150 |
|
| /* operators */ |
| 151 |
193503 |
▶ | if (c == '=' || c == '!' || c == '<' || c == '>') { |
Branch: 4 branches
| Branch | True | False | Total |
| if (c == '=' || c == '!' || c == '<' || c == '>') { |
173311 |
20192 |
193503 |
| if (c == '=' || c == '!' || c == '<' || c == '>') { |
173289 |
22 |
173311 |
| if (c == '=' || c == '!' || c == '<' || c == '>') { |
173275 |
14 |
173289 |
| if (c == '=' || c == '!' || c == '<' || c == '>') { |
5177 |
168098 |
173275 |
|
| 152 |
25405 |
▶ | char d = (l->pos + 1 < l->len) ? l->src[l->pos + 1] : '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| char d = (l->pos + 1 < l->len) ? l->src[l->pos + 1] : '\0'; |
25404 |
1 |
25405 |
|
| 153 |
25405 |
| l->cur.kind = PO_T_OP; |
| 154 |
25405 |
▶ | if (c == '=' && d == '~') { l->cur.op = PO_OP_MATCH; l->pos += 2; l->cur.len = 2; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '=' && d == '~') { l->cur.op = PO_OP_MATCH; l->pos += 2; l->cur.len = 2; } |
20192 |
5213 |
25405 |
| if (c == '=' && d == '~') { l->cur.op = PO_OP_MATCH; l->pos += 2; l->cur.len = 2; } |
10021 |
10171 |
20192 |
|
| 155 |
15384 |
▶ | else if (c == '!' && d == '~') { l->cur.op = PO_OP_NMATCH; l->pos += 2; l->cur.len = 2; } |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c == '!' && d == '~') { l->cur.op = PO_OP_NMATCH; l->pos += 2; l->cur.len = 2; } |
22 |
15362 |
15384 |
| else if (c == '!' && d == '~') { l->cur.op = PO_OP_NMATCH; l->pos += 2; l->cur.len = 2; } |
3 |
19 |
22 |
|
| 156 |
15381 |
▶ | else if (c == '!' && d == '=') { l->cur.op = PO_OP_NE; l->pos += 2; l->cur.len = 2; } |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c == '!' && d == '=') { l->cur.op = PO_OP_NE; l->pos += 2; l->cur.len = 2; } |
19 |
15362 |
15381 |
| else if (c == '!' && d == '=') { l->cur.op = PO_OP_NE; l->pos += 2; l->cur.len = 2; } |
17 |
2 |
19 |
|
| 157 |
15364 |
▶ | else if (c == '<' && d == '=') { l->cur.op = PO_OP_LE; l->pos += 2; l->cur.len = 2; } |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c == '<' && d == '=') { l->cur.op = PO_OP_LE; l->pos += 2; l->cur.len = 2; } |
14 |
15350 |
15364 |
| else if (c == '<' && d == '=') { l->cur.op = PO_OP_LE; l->pos += 2; l->cur.len = 2; } |
9 |
5 |
14 |
|
| 158 |
15355 |
▶ | else if (c == '>' && d == '=') { l->cur.op = PO_OP_GE; l->pos += 2; l->cur.len = 2; } |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c == '>' && d == '=') { l->cur.op = PO_OP_GE; l->pos += 2; l->cur.len = 2; } |
5177 |
10178 |
15355 |
| else if (c == '>' && d == '=') { l->cur.op = PO_OP_GE; l->pos += 2; l->cur.len = 2; } |
88 |
5089 |
5177 |
|
| 159 |
15267 |
▶ | else if (c == '=' && d == '=') { l->cur.op = PO_OP_EQ; l->pos += 2; l->cur.len = 2; } |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c == '=' && d == '=') { l->cur.op = PO_OP_EQ; l->pos += 2; l->cur.len = 2; } |
10171 |
5096 |
15267 |
| else if (c == '=' && d == '=') { l->cur.op = PO_OP_EQ; l->pos += 2; l->cur.len = 2; } |
40 |
10131 |
10171 |
|
| 160 |
15227 |
▶ | else if (c == '=') { l->cur.op = PO_OP_EQ; l->pos++; l->cur.len = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (c == '=') { l->cur.op = PO_OP_EQ; l->pos++; l->cur.len = 1; } |
10131 |
5096 |
15227 |
|
| 161 |
5096 |
▶ | else if (c == '<') { l->cur.op = PO_OP_LT; l->pos++; l->cur.len = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (c == '<') { l->cur.op = PO_OP_LT; l->pos++; l->cur.len = 1; } |
5 |
5091 |
5096 |
|
| 162 |
5091 |
▶ | else if (c == '>') { l->cur.op = PO_OP_GT; l->pos++; l->cur.len = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (c == '>') { l->cur.op = PO_OP_GT; l->pos++; l->cur.len = 1; } |
5089 |
2 |
5091 |
|
| 163 |
2 |
| else { po_lex_fail(l, l->pos, "'!' must be followed by '=' or '~'"); return; } |
| 164 |
25403 |
| return; |
| 165 |
|
| } |
| 166 |
|
| |
| 167 |
|
| /* strings */ |
| 168 |
168098 |
▶ | if (c == '"' || c == '\'') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '"' || c == '\'') { |
147307 |
20791 |
168098 |
| if (c == '"' || c == '\'') { |
1 |
147306 |
147307 |
|
| 169 |
20792 |
| char q = c; |
| 170 |
|
| size_t start; |
| 171 |
20792 |
| l->pos++; |
| 172 |
20792 |
| start = l->pos; |
| 173 |
107022 |
▶ | while (l->pos < l->len && l->src[l->pos] != q) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (l->pos < l->len && l->src[l->pos] != q) { |
107021 |
1 |
107022 |
| while (l->pos < l->len && l->src[l->pos] != q) { |
86230 |
20791 |
107021 |
|
| 174 |
86230 |
▶ | if (l->src[l->pos] == '\\' && l->pos + 1 < l->len) l->pos++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (l->src[l->pos] == '\\' && l->pos + 1 < l->len) l->pos++; |
6 |
86224 |
86230 |
| if (l->src[l->pos] == '\\' && l->pos + 1 < l->len) l->pos++; |
6 |
0 |
6 |
|
| 175 |
86230 |
| l->pos++; |
| 176 |
|
| } |
| 177 |
20792 |
▶ | if (l->pos >= l->len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (l->pos >= l->len) { |
1 |
20791 |
20792 |
|
| 178 |
1 |
| po_lex_fail(l, l->cur.off, "unterminated string"); |
| 179 |
1 |
| return; |
| 180 |
|
| } |
| 181 |
20791 |
| l->cur.kind = PO_T_STRING; |
| 182 |
20791 |
| l->cur.p = l->src + start; |
| 183 |
20791 |
| l->cur.len = l->pos - start; |
| 184 |
20791 |
| l->pos++; /* the closing quote */ |
| 185 |
20791 |
| return; |
| 186 |
|
| } |
| 187 |
|
| |
| 188 |
|
| /* numbers and durations */ |
| 189 |
147306 |
▶ | if (po_is_digit(c)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (po_is_digit(c)) { |
5834 |
141472 |
147306 |
|
| 190 |
5834 |
| size_t start = l->pos; |
| 191 |
5834 |
| int seen_dot = 0; |
| 192 |
5834 |
| po_u64 ival = 0; |
| 193 |
5834 |
| int overflow = 0; |
| 194 |
|
| |
| 195 |
24977 |
| while (l->pos < l->len && |
| 196 |
12421 |
| (po_is_digit(l->src[l->pos]) || |
| 197 |
5701 |
▶ | (l->src[l->pos] == '.' && !seen_dot |
Branch: 2 branches
| Branch | True | False | Total |
| (l->src[l->pos] == '.' && !seen_dot |
2 |
5699 |
5701 |
| (l->src[l->pos] == '.' && !seen_dot |
2 |
0 |
2 |
|
| 198 |
2 |
▶ | && l->pos + 1 < l->len && po_is_digit(l->src[l->pos + 1])))) { |
Branch: 2 branches
| Branch | True | False | Total |
| && l->pos + 1 < l->len && po_is_digit(l->src[l->pos + 1])))) { |
2 |
0 |
2 |
| && l->pos + 1 < l->len && po_is_digit(l->src[l->pos + 1])))) { |
2 |
0 |
2 |
|
| 199 |
6722 |
▶ | if (l->src[l->pos] == '.') seen_dot = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (l->src[l->pos] == '.') seen_dot = 1; |
2 |
6720 |
6722 |
|
| 200 |
6720 |
▶ | else if (!seen_dot) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!seen_dot) { |
6718 |
2 |
6720 |
|
| 201 |
6718 |
| po_u64 d = (po_u64)(l->src[l->pos] - '0'); |
| 202 |
6718 |
▶ | if (ival > (PO_U64_MAX - d) / 10) overflow = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ival > (PO_U64_MAX - d) / 10) overflow = 1; |
1 |
6717 |
6718 |
|
| 203 |
6717 |
| else ival = ival * 10 + d; |
| 204 |
|
| } |
| 205 |
6722 |
| l->pos++; |
| 206 |
|
| } |
| 207 |
|
| |
| 208 |
|
| /* a unit suffix makes it a duration */ |
| 209 |
|
| { |
| 210 |
5834 |
| size_t us = l->pos; |
| 211 |
12152 |
▶ | while (l->pos < l->len && l->src[l->pos] >= 'a' |
Branch: 1 branch
| Branch | True | False | Total |
| while (l->pos < l->len && l->src[l->pos] >= 'a' |
5651 |
667 |
6318 |
|
| 212 |
11485 |
▶ | && l->src[l->pos] <= 'z') l->pos++; |
Branch: 2 branches
| Branch | True | False | Total |
| && l->src[l->pos] <= 'z') l->pos++; |
6318 |
5166 |
11484 |
| && l->src[l->pos] <= 'z') l->pos++; |
5650 |
1 |
5651 |
|
| 213 |
5834 |
▶ | if (l->pos > us) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (l->pos > us) { |
5633 |
201 |
5834 |
|
| 214 |
5633 |
| po_u64 unit = po_dur_unit(l->src + us, l->pos - us); |
| 215 |
5633 |
▶ | if (!unit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!unit) { |
2 |
5631 |
5633 |
|
| 216 |
2 |
| po_lex_fail(l, us, |
| 217 |
|
| "unknown duration unit " |
| 218 |
|
| "(use ns, us, ms, s, m, h, d, w, y)"); |
| 219 |
2 |
| return; |
| 220 |
|
| } |
| 221 |
5631 |
▶ | if (seen_dot) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (seen_dot) { |
1 |
5630 |
5631 |
|
| 222 |
|
| /* 1.5s is meaningful and exact enough at nanosecond |
| 223 |
|
| * scale via the double, but only because the unit is |
| 224 |
|
| * large; the integer path is preferred everywhere. */ |
| 225 |
1 |
| double v = 0, frac = 0.1; |
| 226 |
|
| size_t k; |
| 227 |
1 |
| int after = 0; |
| 228 |
4 |
▶ | for (k = start; k < us; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = start; k < us; k++) { |
3 |
1 |
4 |
|
| 229 |
3 |
▶ | if (l->src[k] == '.') { after = 1; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (l->src[k] == '.') { after = 1; continue; } |
1 |
2 |
3 |
|
| 230 |
2 |
▶ | if (!after) v = v * 10 + (l->src[k] - '0'); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!after) v = v * 10 + (l->src[k] - '0'); |
1 |
1 |
2 |
|
| 231 |
1 |
| else { v += (l->src[k] - '0') * frac; frac /= 10; } |
| 232 |
|
| } |
| 233 |
1 |
| l->cur.dur = (po_u64)(v * (double)unit); |
| 234 |
|
| } |
| 235 |
|
| else { |
| 236 |
5630 |
▶ | if (overflow || (unit && ival > PO_U64_MAX / unit)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (overflow || (unit && ival > PO_U64_MAX / unit)) { |
5630 |
0 |
5630 |
| if (overflow || (unit && ival > PO_U64_MAX / unit)) { |
5630 |
0 |
5630 |
| if (overflow || (unit && ival > PO_U64_MAX / unit)) { |
0 |
5630 |
5630 |
|
| 237 |
0 |
| po_lex_fail(l, start, "duration out of range"); |
| 238 |
0 |
| return; |
| 239 |
|
| } |
| 240 |
5630 |
| l->cur.dur = ival * unit; |
| 241 |
|
| } |
| 242 |
5631 |
| l->cur.kind = PO_T_DURATION; |
| 243 |
5631 |
| l->cur.len = l->pos - start; |
| 244 |
5631 |
| return; |
| 245 |
|
| } |
| 246 |
|
| } |
| 247 |
|
| |
| 248 |
201 |
| l->cur.kind = PO_T_NUMBER; |
| 249 |
201 |
| l->cur.len = l->pos - start; |
| 250 |
201 |
▶ | l->cur.is_int = !seen_dot && !overflow; |
Branch: 2 branches
| Branch | True | False | Total |
| l->cur.is_int = !seen_dot && !overflow; |
200 |
1 |
201 |
| l->cur.is_int = !seen_dot && !overflow; |
199 |
1 |
200 |
|
| 251 |
201 |
| l->cur.inum = ival; |
| 252 |
|
| { /* the NV form, for comparisons against a metric value */ |
| 253 |
201 |
| double v = 0, frac = 0.1; |
| 254 |
201 |
| size_t k; int after = 0; |
| 255 |
799 |
▶ | for (k = start; k < l->pos; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = start; k < l->pos; k++) { |
598 |
201 |
799 |
|
| 256 |
598 |
▶ | if (l->src[k] == '.') { after = 1; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (l->src[k] == '.') { after = 1; continue; } |
1 |
597 |
598 |
|
| 257 |
597 |
▶ | if (!after) v = v * 10 + (l->src[k] - '0'); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!after) v = v * 10 + (l->src[k] - '0'); |
596 |
1 |
597 |
|
| 258 |
1 |
| else { v += (l->src[k] - '0') * frac; frac /= 10; } |
| 259 |
|
| } |
| 260 |
201 |
| l->cur.num = v; |
| 261 |
|
| } |
| 262 |
201 |
| return; |
| 263 |
|
| } |
| 264 |
|
| |
| 265 |
|
| /* identifiers */ |
| 266 |
141472 |
▶ | if (po_is_ident_start(c)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (po_is_ident_start(c)) { |
141470 |
2 |
141472 |
|
| 267 |
141470 |
| size_t start = l->pos; |
| 268 |
1103104 |
▶ | while (l->pos < l->len && po_is_ident(l->src[l->pos])) l->pos++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (l->pos < l->len && po_is_ident(l->src[l->pos])) l->pos++; |
1092113 |
10991 |
1103104 |
| while (l->pos < l->len && po_is_ident(l->src[l->pos])) l->pos++; |
961634 |
130479 |
1092113 |
|
| 269 |
141470 |
| l->cur.kind = PO_T_IDENT; |
| 270 |
141470 |
| l->cur.p = l->src + start; |
| 271 |
141470 |
| l->cur.len = l->pos - start; |
| 272 |
141470 |
| return; |
| 273 |
|
| } |
| 274 |
|
| |
| 275 |
2 |
| po_lex_fail(l, l->pos, "unexpected character"); |
| 276 |
|
| } |
| 277 |
|
| } |
| 278 |
|
| |
| 279 |
1025968 |
| static int po_tok_is(const po_tok *t, const char *word) { |
| 280 |
1025968 |
| size_t n = strlen(word); |
| 281 |
1025968 |
▶ | return t->kind == PO_T_IDENT && t->len == n && memcmp(t->p, word, n) == 0; |
Branch: 3 branches
| Branch | True | False | Total |
| return t->kind == PO_T_IDENT && t->len == n && memcmp(t->p, word, n) == 0; |
994654 |
31314 |
1025968 |
| return t->kind == PO_T_IDENT && t->len == n && memcmp(t->p, word, n) == 0; |
247229 |
747425 |
994654 |
| return t->kind == PO_T_IDENT && t->len == n && memcmp(t->p, word, n) == 0; |
115260 |
131969 |
247229 |
|
| 282 |
|
| } |
| 283 |
|
| |
| 284 |
|
| #endif /* PO_LEX_H */ |