| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | Copyright 2019, 2021, 2022, 2025 Joel Svensson svenssonjoel@yahoo.se | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 3 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <stdbool.h> | ||
| 19 | #include <ctype.h> | ||
| 20 | #include <string.h> | ||
| 21 | #include <stdlib.h> | ||
| 22 | |||
| 23 | #include "lbm_memory.h" | ||
| 24 | #include "lbm_types.h" | ||
| 25 | #include "lbm_channel.h" | ||
| 26 | #include "tokpar.h" | ||
| 27 | #include "symrepr.h" | ||
| 28 | #include "heap.h" | ||
| 29 | #include "env.h" | ||
| 30 | |||
| 31 | // +1 to ensure there is always a zero at last ix | ||
| 32 | char tokpar_sym_str[TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH+1]; | ||
| 33 | |||
| 34 | typedef struct { | ||
| 35 | const char *str; | ||
| 36 | uint16_t token; | ||
| 37 | uint16_t len; | ||
| 38 | } matcher; | ||
| 39 | |||
| 40 | /* | ||
| 41 | \#\0 -> 0 ; NUL | ||
| 42 | \#\a -> 7 ; bell character, BEL | ||
| 43 | \#\b -> 8 ; backspace, BS | ||
| 44 | \#\t -> 9 ; tab, TAB | ||
| 45 | \#\n -> 10 ; newline | ||
| 46 | \#\v -> 11 ; vertical tab | ||
| 47 | \#\f -> 12 ; formfeed character | ||
| 48 | \#\r -> 13 ; carriage return, RET | ||
| 49 | \#\e -> 27 ; escape character, ESC | ||
| 50 | \#\s -> 32 ; space character, SPC | ||
| 51 | \#\" -> 34 ; double quote | ||
| 52 | \#\\ -> 92 ; backslash character, \ | ||
| 53 | \#\d -> 127 ; delete character, DEL | ||
| 54 | */ | ||
| 55 | |||
| 56 | 1249 | static inline signed char translate_escape_char(char c) { | |
| 57 |
14/14✓ Branch 0 taken 84 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 57 times.
✓ Branch 4 taken 146 times.
✓ Branch 5 taken 56 times.
✓ Branch 6 taken 56 times.
✓ Branch 7 taken 57 times.
✓ Branch 8 taken 56 times.
✓ Branch 9 taken 56 times.
✓ Branch 10 taken 287 times.
✓ Branch 11 taken 141 times.
✓ Branch 12 taken 56 times.
✓ Branch 13 taken 57 times.
|
1249 | switch (c) { |
| 58 | 84 | case '0': return '\0'; | |
| 59 | 56 | case 'a': return '\a'; | |
| 60 | 84 | case 'b': return '\b'; | |
| 61 | 57 | case 't': return '\t'; | |
| 62 | 146 | case 'n': return '\n'; | |
| 63 | 56 | case 'v': return '\v'; | |
| 64 | 56 | case 'f': return '\f'; | |
| 65 | 57 | case 'r': return '\r'; | |
| 66 | 56 | case 'e': return 27; | |
| 67 | 56 | case 's': return 32; | |
| 68 | 287 | case '"': return '\"'; | |
| 69 | 141 | case '\\': return '\\'; | |
| 70 | 56 | case 'd': return 127; | |
| 71 | 57 | default: return -1; | |
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | #define NUM_FIXED_SIZE_TOKENS 18 | ||
| 76 | const matcher fixed_size_tokens[NUM_FIXED_SIZE_TOKENS] = { | ||
| 77 | {"(", TOKOPENPAR, 1}, | ||
| 78 | {")", TOKCLOSEPAR, 1}, | ||
| 79 | {"[|", TOKOPENARRAY, 2}, | ||
| 80 | {"[", TOKOPENBRACK, 1}, | ||
| 81 | {"]", TOKCLOSEBRACK, 1}, | ||
| 82 | {".", TOKDOT, 1}, | ||
| 83 | {"_", TOKDONTCARE, 1}, | ||
| 84 | {"'", TOKQUOTE, 1}, | ||
| 85 | {"`", TOKBACKQUOTE, 1}, | ||
| 86 | {",@", TOKCOMMAAT, 2}, | ||
| 87 | {",", TOKCOMMA, 1}, | ||
| 88 | {"?", TOKMATCHANY, 1}, | ||
| 89 | {"{", TOKOPENCURL, 1}, | ||
| 90 | {"}", TOKCLOSECURL, 1}, | ||
| 91 | {"|]", TOKCLOSEARRAY, 2}, | ||
| 92 | {"@const-start", TOKCONSTSTART, 12}, | ||
| 93 | {"@const-end", TOKCONSTEND, 10}, | ||
| 94 | }; | ||
| 95 | |||
| 96 | #define NUM_TYPE_QUALIFIERS 9 | ||
| 97 | const matcher type_qual_table[NUM_TYPE_QUALIFIERS] = { | ||
| 98 | {"f64", TOKTYPEF64, 3}, | ||
| 99 | {"f32", TOKTYPEF32, 3}, | ||
| 100 | {"i64", TOKTYPEI64, 3}, | ||
| 101 | {"u64", TOKTYPEU64, 3}, | ||
| 102 | {"i32", TOKTYPEI32, 3}, | ||
| 103 | {"u32", TOKTYPEU32, 3}, | ||
| 104 | {"i" , TOKTYPEI, 1}, | ||
| 105 | {"u" , TOKTYPEU, 1}, | ||
| 106 | {"b" , TOKTYPEBYTE, 1} | ||
| 107 | }; | ||
| 108 | |||
| 109 | 9961582 | static int tok_match_fixed_size_tokens(lbm_char_channel_t *chan, const matcher *m, unsigned int start_pos, unsigned int num, uint32_t *res) { | |
| 110 | |||
| 111 |
2/2✓ Branch 0 taken 118497806 times.
✓ Branch 1 taken 3412026 times.
|
121909832 | for (unsigned int i = 0; i < num; i ++) { |
| 112 | 118497806 | uint32_t tok_len = m[i].len; | |
| 113 | 118497806 | const char *match_str = m[i].str; | |
| 114 | char c; | ||
| 115 | int char_pos; | ||
| 116 |
2/2✓ Branch 0 taken 113897277 times.
✓ Branch 1 taken 6549549 times.
|
120446826 | for (char_pos = 0; char_pos < (int)tok_len; char_pos ++) { |
| 117 | 113897277 | int r = lbm_channel_peek(chan,(unsigned int)char_pos + start_pos, &c); | |
| 118 |
2/2✓ Branch 0 taken 113896631 times.
✓ Branch 1 taken 646 times.
|
113897277 | if (r == CHANNEL_SUCCESS) { |
| 119 |
2/2✓ Branch 0 taken 111947611 times.
✓ Branch 1 taken 1949020 times.
|
113896631 | if (c != match_str[char_pos]) break; |
| 120 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 639 times.
|
646 | } else if (r == CHANNEL_MORE ) { |
| 121 | 6549556 | return TOKENIZER_NEED_MORE; | |
| 122 | } else { | ||
| 123 | 639 | break; | |
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 |
2/2✓ Branch 0 taken 6549549 times.
✓ Branch 1 taken 111948250 times.
|
118497799 | if (char_pos == (int)tok_len) { //match |
| 128 | 6549549 | *res = m[i].token; | |
| 129 | 6549549 | return (int)tok_len; | |
| 130 | } | ||
| 131 | } | ||
| 132 | 3412026 | return TOKENIZER_NO_TOKEN; | |
| 133 | } | ||
| 134 | |||
| 135 | 6521819 | int tok_syntax(lbm_char_channel_t *chan, uint32_t *res) { | |
| 136 | 6521819 | return tok_match_fixed_size_tokens(chan, fixed_size_tokens, 0, NUM_FIXED_SIZE_TOKENS, res); | |
| 137 | } | ||
| 138 | |||
| 139 | 5486107 | static bool alpha_char(char c) { | |
| 140 |
6/6✓ Branch 0 taken 3896238 times.
✓ Branch 1 taken 1589869 times.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 3896154 times.
✓ Branch 4 taken 10694 times.
✓ Branch 5 taken 1579259 times.
|
5496801 | return ((c >= 'a' && c <= 'z') || |
| 141 |
2/2✓ Branch 0 taken 1187 times.
✓ Branch 1 taken 9507 times.
|
10694 | (c >= 'A' && c <= 'Z')); |
| 142 | } | ||
| 143 | |||
| 144 | 1509003 | static bool num_char(char c) { | |
| 145 |
4/4✓ Branch 0 taken 161089 times.
✓ Branch 1 taken 1347914 times.
✓ Branch 2 taken 148267 times.
✓ Branch 3 taken 12822 times.
|
1509003 | return (c >= '0' && c <= '9'); |
| 146 | } | ||
| 147 | |||
| 148 | 1240245 | static bool symchar0(char c) { | |
| 149 | 1240245 | const char *allowed = "+-*/=<>#!"; | |
| 150 | |||
| 151 |
2/2✓ Branch 0 taken 1160482 times.
✓ Branch 1 taken 79763 times.
|
1240245 | if (alpha_char(c)) return true; |
| 152 | 79763 | int i = 0; | |
| 153 |
2/2✓ Branch 0 taken 255665 times.
✓ Branch 1 taken 723 times.
|
256388 | while (allowed[i] != 0) { |
| 154 |
2/2✓ Branch 0 taken 79040 times.
✓ Branch 1 taken 176625 times.
|
255665 | if (c == allowed[i]) return true; |
| 155 | 176625 | i ++; | |
| 156 | } | ||
| 157 | 723 | return false; | |
| 158 | } | ||
| 159 | |||
| 160 | 4245862 | static bool symchar(char c) { | |
| 161 | 4245862 | const char *allowed = "+-*/=<>!?_"; | |
| 162 | |||
| 163 |
4/4✓ Branch 0 taken 1509003 times.
✓ Branch 1 taken 2736859 times.
✓ Branch 2 taken 148267 times.
✓ Branch 3 taken 1360736 times.
|
4245862 | if (alpha_char(c) || num_char(c)) return true; |
| 164 | 1360736 | int i = 0; | |
| 165 |
2/2✓ Branch 0 taken 12610670 times.
✓ Branch 1 taken 1225653 times.
|
13836323 | while (allowed[i] != 0) { |
| 166 |
2/2✓ Branch 0 taken 135083 times.
✓ Branch 1 taken 12475587 times.
|
12610670 | if (c == allowed[i]) return true; |
| 167 | 12475587 | i++; | |
| 168 | } | ||
| 169 | 1225653 | return false; | |
| 170 | } | ||
| 171 | |||
| 172 | 1240245 | int tok_symbol(lbm_char_channel_t *chan) { | |
| 173 | |||
| 174 | char c; | ||
| 175 | 1240245 | int r = 0; | |
| 176 | |||
| 177 | 1240245 | r = lbm_channel_peek(chan, 0, &c); | |
| 178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1240245 times.
|
1240245 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1240245 times.
|
1240245 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 180 |
3/4✓ Branch 0 taken 1240245 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 723 times.
✓ Branch 3 taken 1239522 times.
|
1240245 | if (r == CHANNEL_SUCCESS && !symchar0(c)) { |
| 181 | 723 | return TOKENIZER_NO_TOKEN; | |
| 182 | } | ||
| 183 | 1239522 | memset(tokpar_sym_str,0,TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH+1); | |
| 184 |
4/4✓ Branch 0 taken 1160482 times.
✓ Branch 1 taken 79040 times.
✓ Branch 2 taken 178 times.
✓ Branch 3 taken 1160304 times.
|
1239522 | tokpar_sym_str[0] = (c >= 'A' && c <= 'Z') ? c + 32 : c; // locale independent ASCII only tolower. |
| 185 | |||
| 186 | 1239522 | int len = 1; | |
| 187 | |||
| 188 | 1239522 | r = lbm_channel_peek(chan,(unsigned int)len, &c); | |
| 189 |
4/4✓ Branch 0 taken 4245862 times.
✓ Branch 1 taken 13868 times.
✓ Branch 2 taken 3020209 times.
✓ Branch 3 taken 1225653 times.
|
4259730 | while (r == CHANNEL_SUCCESS && symchar(c)) { |
| 190 |
4/4✓ Branch 0 taken 2745560 times.
✓ Branch 1 taken 274649 times.
✓ Branch 2 taken 1009 times.
✓ Branch 3 taken 2744551 times.
|
3020209 | c = (c >= 'A' && c <= 'Z') ? c + 32 : c; // locale independent ASCII only tolower. |
| 191 |
2/2✓ Branch 0 taken 3020208 times.
✓ Branch 1 taken 1 times.
|
3020209 | if (len < TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH) { |
| 192 | 3020208 | tokpar_sym_str[len] = (char)c; | |
| 193 | } else { | ||
| 194 | 1 | return TOKENIZER_SYMBOL_ERROR; | |
| 195 | } | ||
| 196 | 3020208 | len ++; | |
| 197 | 3020208 | r = lbm_channel_peek(chan,(unsigned int)len, &c); | |
| 198 | } | ||
| 199 |
2/2✓ Branch 0 taken 176 times.
✓ Branch 1 taken 1239345 times.
|
1239521 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 200 | 1239345 | tokpar_sym_str[len] = 0; | |
| 201 | 1239345 | return len; | |
| 202 | } | ||
| 203 | |||
| 204 | 4681975 | int tok_string(lbm_char_channel_t *chan, unsigned int *string_len) { | |
| 205 | |||
| 206 | 4681975 | unsigned int n = 0; | |
| 207 | 4681975 | unsigned int len = 0; | |
| 208 | char c; | ||
| 209 | 4681975 | int r = 0; | |
| 210 | 4681975 | bool encode = false; | |
| 211 | |||
| 212 | 4681975 | r = lbm_channel_peek(chan,0,&c); | |
| 213 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4681975 times.
|
4681975 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 214 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4681975 times.
|
4681975 | else if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 215 | |||
| 216 |
2/2✓ Branch 0 taken 4670365 times.
✓ Branch 1 taken 11610 times.
|
4681975 | if (c != '\"') return TOKENIZER_NO_TOKEN;; |
| 217 | 11610 | n++; | |
| 218 | |||
| 219 | 11610 | memset(tokpar_sym_str,0,TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH+1); | |
| 220 | |||
| 221 | // read string into buffer | ||
| 222 | 11610 | r = lbm_channel_peek(chan,n,&c); | |
| 223 |
7/8✓ Branch 0 taken 81539 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 11830 times.
✓ Branch 3 taken 69709 times.
✓ Branch 4 taken 259 times.
✓ Branch 5 taken 11571 times.
✓ Branch 6 taken 69968 times.
✗ Branch 7 not taken.
|
81549 | while (r == CHANNEL_SUCCESS && (c != '\"' || encode) && |
| 224 | len < TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH) { | ||
| 225 |
4/4✓ Branch 0 taken 912 times.
✓ Branch 1 taken 69056 times.
✓ Branch 2 taken 799 times.
✓ Branch 3 taken 113 times.
|
69968 | if (c == '\\' && !encode) { |
| 226 | 799 | encode = true; | |
| 227 | } else { | ||
| 228 |
2/2✓ Branch 0 taken 799 times.
✓ Branch 1 taken 68370 times.
|
69169 | if (encode) { |
| 229 | 799 | signed char result = translate_escape_char(c); | |
| 230 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 770 times.
|
799 | if (result == -1) { |
| 231 | 29 | return TOKENIZER_STRING_ERROR; | |
| 232 | } else { | ||
| 233 | 770 | tokpar_sym_str[len] = result; | |
| 234 | } | ||
| 235 | } else { | ||
| 236 | 68370 | tokpar_sym_str[len] = c; | |
| 237 | } | ||
| 238 | 69140 | len++; | |
| 239 | 69140 | encode = false; | |
| 240 | } | ||
| 241 | 69939 | n ++; | |
| 242 | 69939 | r = lbm_channel_peek(chan, n, &c); | |
| 243 | } | ||
| 244 | |||
| 245 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 11574 times.
|
11581 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 246 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11571 times.
|
11574 | if (c != '\"') return TOKENIZER_STRING_ERROR; |
| 247 | |||
| 248 | 11571 | *string_len = len; | |
| 249 | 11571 | n ++; | |
| 250 | 11571 | return (int)n; | |
| 251 | } | ||
| 252 | |||
| 253 | 723 | int tok_char(lbm_char_channel_t *chan, char *res) { | |
| 254 | |||
| 255 | char c; | ||
| 256 | int r; | ||
| 257 | |||
| 258 | 723 | r = lbm_channel_peek(chan, 0, &c); | |
| 259 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 723 times.
|
723 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 260 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 723 times.
|
723 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 261 | |||
| 262 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 721 times.
|
723 | if (c != '\\') return TOKENIZER_NO_TOKEN; |
| 263 | |||
| 264 | 721 | r = lbm_channel_peek(chan, 1, &c); | |
| 265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 721 times.
|
721 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 266 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 721 times.
|
721 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 267 | |||
| 268 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 721 times.
|
721 | if (c != '#') return TOKENIZER_NO_TOKEN; |
| 269 | |||
| 270 | 721 | r = lbm_channel_peek(chan, 2, &c); | |
| 271 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 721 times.
|
721 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 272 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 721 times.
|
721 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 273 | |||
| 274 |
2/2✓ Branch 0 taken 450 times.
✓ Branch 1 taken 271 times.
|
721 | if (c == '\\') { |
| 275 | 450 | r = lbm_channel_peek(chan, 3, &c); | |
| 276 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
|
450 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 277 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
|
450 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 278 | |||
| 279 | 450 | signed char result = translate_escape_char(c); | |
| 280 |
2/2✓ Branch 0 taken 422 times.
✓ Branch 1 taken 28 times.
|
450 | if (result != -1) { |
| 281 | 422 | *res = result; | |
| 282 | 422 | return 4; | |
| 283 | } else { | ||
| 284 | 28 | return TOKENIZER_CHAR_ERROR; | |
| 285 | } | ||
| 286 | } | ||
| 287 | 271 | *res = c; | |
| 288 | 271 | return 3; | |
| 289 | } | ||
| 290 | |||
| 291 | |||
| 292 | #define TD_BUF_SIZE 128 | ||
| 293 | |||
| 294 | #define FBUF_ADD(X,N) if ((N) < TD_BUF_SIZE) { fbuf[(N)] = (X); N++; } else goto tok_double_no_tok; | ||
| 295 | 4670365 | int tok_double(lbm_char_channel_t *chan, token_float *result) { | |
| 296 | |||
| 297 | 4670365 | unsigned int n = 0; | |
| 298 | char fbuf[TD_BUF_SIZE]; | ||
| 299 | char c; | ||
| 300 | 4670365 | bool valid_num = false; | |
| 301 | int res; | ||
| 302 | |||
| 303 | 4670365 | memset(fbuf, 0, TD_BUF_SIZE); | |
| 304 | |||
| 305 | 4670365 | result->type = TOKTYPEF32; | |
| 306 | 4670365 | result->negative = false; | |
| 307 | |||
| 308 | 4670365 | res = lbm_channel_peek(chan, n, &c); | |
| 309 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4670365 times.
|
4670365 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 310 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4670365 times.
|
4670365 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 311 |
2/2✓ Branch 0 taken 11992 times.
✓ Branch 1 taken 4658373 times.
|
4670365 | if (c == '-') { |
| 312 |
1/2✓ Branch 0 taken 11992 times.
✗ Branch 1 not taken.
|
11992 | FBUF_ADD('-', n); |
| 313 | 11992 | result->negative = true; | |
| 314 | } | ||
| 315 | |||
| 316 | 4670365 | res = lbm_channel_peek(chan, n, &c); | |
| 317 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4670364 times.
|
4670365 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 318 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4670364 times.
|
4670364 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 319 |
4/4✓ Branch 0 taken 5567794 times.
✓ Branch 1 taken 3437176 times.
✓ Branch 2 taken 4334732 times.
✓ Branch 3 taken 1233062 times.
|
9004970 | while (c >= '0' && c <= '9') { |
| 320 |
1/2✓ Branch 0 taken 4334732 times.
✗ Branch 1 not taken.
|
4334732 | FBUF_ADD(c, n); |
| 321 | 4334732 | res = lbm_channel_peek(chan, n, &c); | |
| 322 |
2/2✓ Branch 0 taken 62 times.
✓ Branch 1 taken 4334670 times.
|
4334732 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 323 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 4334606 times.
|
4334670 | if (res == CHANNEL_END) break; |
| 324 | } | ||
| 325 | |||
| 326 |
2/2✓ Branch 0 taken 12265 times.
✓ Branch 1 taken 4658037 times.
|
4670302 | if (c == '.') { |
| 327 |
1/2✓ Branch 0 taken 12265 times.
✗ Branch 1 not taken.
|
12265 | FBUF_ADD(c, n); |
| 328 | } | ||
| 329 | 4658037 | else return TOKENIZER_NO_TOKEN; | |
| 330 | |||
| 331 | 12265 | res = lbm_channel_peek(chan,n, &c); | |
| 332 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12265 times.
|
12265 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 333 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12265 times.
|
12265 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 334 |
2/4✓ Branch 0 taken 12265 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12265 times.
|
12265 | if (!(c >= '0' && c <= '9')) return TOKENIZER_NO_TOKEN; |
| 335 | |||
| 336 |
4/4✓ Branch 0 taken 23617 times.
✓ Branch 1 taken 6123 times.
✓ Branch 2 taken 17476 times.
✓ Branch 3 taken 6141 times.
|
29740 | while (c >= '0' && c <= '9') { |
| 337 |
1/2✓ Branch 0 taken 17476 times.
✗ Branch 1 not taken.
|
17476 | FBUF_ADD(c, n); |
| 338 | 17476 | res = lbm_channel_peek(chan, n, &c); | |
| 339 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17475 times.
|
17476 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 340 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17475 times.
|
17475 | if (res == CHANNEL_END) break; |
| 341 | } | ||
| 342 | |||
| 343 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 12152 times.
|
12264 | if (c == 'e') { |
| 344 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | FBUF_ADD(c, n); |
| 345 | 112 | res = lbm_channel_peek(chan,n, &c); | |
| 346 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 347 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 348 |
2/6✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 112 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
112 | if (!((c >= '0' && c <= '9') || c == '-')) return TOKENIZER_NO_TOKEN; |
| 349 | |||
| 350 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | if (c == '-') { |
| 351 | ✗ | FBUF_ADD(c, n); | |
| 352 | } | ||
| 353 | 112 | res = lbm_channel_peek(chan,n, &c); | |
| 354 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 355 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 356 |
4/4✓ Branch 0 taken 168 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 112 times.
✓ Branch 3 taken 56 times.
|
224 | while ((c >= '0' && c <= '9')) { |
| 357 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | FBUF_ADD(c,n); |
| 358 | 112 | res = lbm_channel_peek(chan, n, &c); | |
| 359 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 360 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | if (res == CHANNEL_END) break; |
| 361 | } | ||
| 362 | } | ||
| 363 | |||
| 364 | uint32_t tok_res; | ||
| 365 | 12264 | int type_len = tok_match_fixed_size_tokens(chan, type_qual_table, n, NUM_TYPE_QUALIFIERS, &tok_res); | |
| 366 | |||
| 367 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12262 times.
|
12264 | if (type_len == TOKENIZER_NEED_MORE) return type_len; |
| 368 |
2/2✓ Branch 0 taken 6179 times.
✓ Branch 1 taken 6083 times.
|
12262 | if (type_len == TOKENIZER_NO_TOKEN) { |
| 369 | 6179 | result->type = TOKTYPEF32; | |
| 370 | } else { | ||
| 371 | 6083 | result->type = tok_res; | |
| 372 | } | ||
| 373 | |||
| 374 |
3/4✓ Branch 0 taken 292 times.
✓ Branch 1 taken 11970 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 292 times.
|
12262 | if ((result->negative && n > 1) || |
| 375 |
2/4✓ Branch 0 taken 11970 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11970 times.
✗ Branch 3 not taken.
|
12262 | (!result->negative && n > 0)) valid_num = true; |
| 376 | |||
| 377 |
1/2✓ Branch 0 taken 12262 times.
✗ Branch 1 not taken.
|
12262 | if(valid_num) { |
| 378 | 12262 | result->value = (double)strtod(fbuf,NULL); | |
| 379 | 12262 | return (int)n + type_len; | |
| 380 | } | ||
| 381 | |||
| 382 | ✗ | tok_double_no_tok: | |
| 383 | ✗ | return TOKENIZER_NO_TOKEN; | |
| 384 | } | ||
| 385 | |||
| 386 | 6544805 | bool tok_clean_whitespace(lbm_char_channel_t *chan) { | |
| 387 | |||
| 388 | 6544805 | bool cleaning_whitespace = true; | |
| 389 | char c; | ||
| 390 | int r; | ||
| 391 | |||
| 392 |
2/2✓ Branch 0 taken 6557376 times.
✓ Branch 1 taken 6521819 times.
|
13079195 | while (cleaning_whitespace) { |
| 393 | |||
| 394 |
2/2✓ Branch 0 taken 13273 times.
✓ Branch 1 taken 6544103 times.
|
6557376 | if (lbm_channel_comment(chan)) { |
| 395 | while (true) { | ||
| 396 | 995357 | r = lbm_channel_peek(chan, 0, &c); | |
| 397 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 995357 times.
|
995357 | if (r == CHANNEL_END) { |
| 398 | ✗ | lbm_channel_set_comment(chan, false); | |
| 399 | ✗ | cleaning_whitespace = false; | |
| 400 | ✗ | break; | |
| 401 | } | ||
| 402 |
2/2✓ Branch 0 taken 710 times.
✓ Branch 1 taken 994647 times.
|
995357 | if (r == CHANNEL_MORE) { |
| 403 | 710 | return false; | |
| 404 | } | ||
| 405 | 994647 | lbm_channel_drop(chan,1); | |
| 406 |
2/2✓ Branch 0 taken 12563 times.
✓ Branch 1 taken 982084 times.
|
994647 | if (c == '\n') { |
| 407 | 12563 | lbm_channel_set_comment(chan, false); | |
| 408 | 12563 | break; | |
| 409 | } | ||
| 410 | } | ||
| 411 | } | ||
| 412 | |||
| 413 | do { | ||
| 414 | 11810335 | r = lbm_channel_peek(chan, 0, &c); | |
| 415 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 11810079 times.
|
11810335 | if (r == CHANNEL_MORE) { |
| 416 | 256 | return false; | |
| 417 |
2/2✓ Branch 0 taken 22020 times.
✓ Branch 1 taken 11788059 times.
|
11810079 | } else if (r == CHANNEL_END) { |
| 418 | 22020 | return true; | |
| 419 | } | ||
| 420 |
2/2✓ Branch 0 taken 12571 times.
✓ Branch 1 taken 11775488 times.
|
11788059 | if (c == ';') { |
| 421 | 12571 | lbm_channel_set_comment(chan, true); | |
| 422 | 12571 | break; | |
| 423 | } | ||
| 424 |
2/2✓ Branch 0 taken 5253669 times.
✓ Branch 1 taken 6521819 times.
|
11775488 | if (isspace(c)) { |
| 425 | 5253669 | lbm_channel_drop(chan,1); | |
| 426 | } else { | ||
| 427 | 6521819 | cleaning_whitespace = false; | |
| 428 | } | ||
| 429 | |||
| 430 |
2/2✓ Branch 0 taken 5253669 times.
✓ Branch 1 taken 6521819 times.
|
11775488 | } while (cleaning_whitespace); |
| 431 | } | ||
| 432 | 6521819 | return true; | |
| 433 | } | ||
| 434 | |||
| 435 | 4658037 | int tok_integer(lbm_char_channel_t *chan, token_int *result) { | |
| 436 | 4658037 | uint64_t acc = 0; | |
| 437 | 4658037 | unsigned int n = 0; | |
| 438 | 4658037 | bool valid_num = false; | |
| 439 | char c; | ||
| 440 | int res; | ||
| 441 | |||
| 442 | 4658037 | result->type = TOKTYPEI; | |
| 443 | 4658037 | result-> negative = false; | |
| 444 | 4658037 | res = lbm_channel_peek(chan, 0, &c); | |
| 445 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4658037 times.
|
4658037 | if (res == CHANNEL_MORE) { |
| 446 | ✗ | return TOKENIZER_NEED_MORE; | |
| 447 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4658037 times.
|
4658037 | } else if (res == CHANNEL_END) { |
| 448 | ✗ | return TOKENIZER_NO_TOKEN; | |
| 449 | } | ||
| 450 |
2/2✓ Branch 0 taken 11699 times.
✓ Branch 1 taken 4646338 times.
|
4658037 | if (c == '-') { |
| 451 | 11699 | n = 1; | |
| 452 | 11699 | result->negative = true; | |
| 453 | } | ||
| 454 | |||
| 455 | 4658037 | bool hex = false; | |
| 456 | 4658037 | res = lbm_channel_peek(chan, n, &c); | |
| 457 |
3/4✓ Branch 0 taken 4658037 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35189 times.
✓ Branch 3 taken 4622848 times.
|
4658037 | if (res == CHANNEL_SUCCESS && c == '0') { |
| 458 | 35189 | res = lbm_channel_peek(chan, n + 1, &c); | |
| 459 |
4/6✓ Branch 0 taken 35189 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18870 times.
✓ Branch 3 taken 16319 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 18870 times.
|
35189 | if ( res == CHANNEL_SUCCESS && (c == 'x' || c == 'X')) { |
| 460 | 16319 | hex = true; | |
| 461 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18870 times.
|
18870 | } else if (res == CHANNEL_MORE) { |
| 462 | ✗ | return TOKENIZER_NEED_MORE; | |
| 463 | } | ||
| 464 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4622848 times.
|
4622848 | } else if (res == CHANNEL_MORE) { |
| 465 | ✗ | return TOKENIZER_NEED_MORE; | |
| 466 | } | ||
| 467 | |||
| 468 |
2/2✓ Branch 0 taken 16319 times.
✓ Branch 1 taken 4641718 times.
|
4658037 | if (hex) { |
| 469 | 16319 | n += 2; | |
| 470 | |||
| 471 | 16319 | res = lbm_channel_peek(chan,n, &c); | |
| 472 | |||
| 473 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16316 times.
|
16319 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 474 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16316 times.
|
16316 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 475 | |||
| 476 |
2/2✓ Branch 0 taken 28097 times.
✓ Branch 1 taken 11080 times.
|
55493 | while ((c >= '0' && c <= '9') || |
| 477 |
6/6✓ Branch 0 taken 39177 times.
✓ Branch 1 taken 15959 times.
✓ Branch 2 taken 306 times.
✓ Branch 3 taken 26733 times.
✓ Branch 4 taken 38 times.
✓ Branch 5 taken 268 times.
|
66216 | (c >= 'a' && c <= 'f') || |
| 478 |
4/4✓ Branch 0 taken 11042 times.
✓ Branch 1 taken 15959 times.
✓ Branch 2 taken 10690 times.
✓ Branch 3 taken 352 times.
|
27001 | (c >= 'A' && c <= 'F')) { |
| 479 | uint32_t val; /* values between 0 and 16 */ | ||
| 480 |
3/4✓ Branch 0 taken 38 times.
✓ Branch 1 taken 38787 times.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
|
38825 | if (c >= 'a' && c <= 'f') { |
| 481 | 38 | val = 10 + (uint32_t)c - 'a'; | |
| 482 |
3/4✓ Branch 0 taken 10690 times.
✓ Branch 1 taken 28097 times.
✓ Branch 2 taken 10690 times.
✗ Branch 3 not taken.
|
38787 | } else if (c >= 'A' && c <= 'F') { |
| 483 | 10690 | val = 10 + (uint32_t)(c - 'A'); | |
| 484 | } else { | ||
| 485 | 28097 | val = (uint32_t)c - '0'; | |
| 486 | } | ||
| 487 | 38825 | acc = (acc * 0x10) + val; | |
| 488 | 38825 | n++; | |
| 489 | 38825 | res = lbm_channel_peek(chan, n, &c); | |
| 490 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 38820 times.
|
38825 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 491 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38820 times.
|
38820 | if (res == CHANNEL_END) break; |
| 492 | |||
| 493 | } | ||
| 494 | } else { | ||
| 495 | 4641718 | res = lbm_channel_peek(chan, n, &c); | |
| 496 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4641718 times.
|
4641718 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 497 |
4/4✓ Branch 0 taken 5522019 times.
✓ Branch 1 taken 3424911 times.
✓ Branch 2 taken 4305276 times.
✓ Branch 3 taken 1216743 times.
|
8946930 | while (c >= '0' && c <= '9') { |
| 498 | 4305276 | acc = (acc*10) + (uint32_t)(c - '0'); | |
| 499 | 4305276 | n++; | |
| 500 | 4305276 | res = lbm_channel_peek(chan, n, &c); | |
| 501 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4305276 times.
|
4305276 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 502 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 4305212 times.
|
4305276 | if (res == CHANNEL_END) break; |
| 503 | } | ||
| 504 | } | ||
| 505 | |||
| 506 |
5/6✓ Branch 0 taken 3427499 times.
✓ Branch 1 taken 1230530 times.
✓ Branch 2 taken 16311 times.
✓ Branch 3 taken 3411188 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 16311 times.
|
4658029 | if (n == 0 || (hex && n == 2)) return TOKENIZER_NO_TOKEN; |
| 507 | |||
| 508 | uint32_t tok_res; | ||
| 509 | 3427499 | int type_len = tok_match_fixed_size_tokens(chan, type_qual_table, n, NUM_TYPE_QUALIFIERS, &tok_res); | |
| 510 | |||
| 511 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3427494 times.
|
3427499 | if (type_len == TOKENIZER_NEED_MORE) return type_len; |
| 512 |
2/2✓ Branch 0 taken 21647 times.
✓ Branch 1 taken 3405847 times.
|
3427494 | if (type_len != TOKENIZER_NO_TOKEN) { |
| 513 | 21647 | result->type = tok_res; | |
| 514 | } | ||
| 515 | |||
| 516 |
4/4✓ Branch 0 taken 11699 times.
✓ Branch 1 taken 3415795 times.
✓ Branch 2 taken 9715 times.
✓ Branch 3 taken 1984 times.
|
3427494 | if ((result->negative && n > 1) || |
| 517 |
2/2✓ Branch 0 taken 3415795 times.
✓ Branch 1 taken 9715 times.
|
3427494 | !result->negative) valid_num = true; |
| 518 | |||
| 519 |
2/2✓ Branch 0 taken 3417779 times.
✓ Branch 1 taken 9715 times.
|
3427494 | if (valid_num) { |
| 520 | 3417779 | result->value = acc; | |
| 521 | 3417779 | return (int)n + type_len; | |
| 522 | } | ||
| 523 | 9715 | return TOKENIZER_NO_TOKEN; | |
| 524 | } | ||
| 525 |