| 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 | 3726 | static inline signed char translate_escape_char(char c) { | |
| 57 |
14/14✓ Branch 0 taken 253 times.
✓ Branch 1 taken 169 times.
✓ Branch 2 taken 253 times.
✓ Branch 3 taken 170 times.
✓ Branch 4 taken 427 times.
✓ Branch 5 taken 169 times.
✓ Branch 6 taken 169 times.
✓ Branch 7 taken 170 times.
✓ Branch 8 taken 169 times.
✓ Branch 9 taken 169 times.
✓ Branch 10 taken 847 times.
✓ Branch 11 taken 422 times.
✓ Branch 12 taken 169 times.
✓ Branch 13 taken 170 times.
|
3726 | switch (c) { |
| 58 | 253 | case '0': return '\0'; | |
| 59 | 169 | case 'a': return '\a'; | |
| 60 | 253 | case 'b': return '\b'; | |
| 61 | 170 | case 't': return '\t'; | |
| 62 | 427 | case 'n': return '\n'; | |
| 63 | 169 | case 'v': return '\v'; | |
| 64 | 169 | case 'f': return '\f'; | |
| 65 | 170 | case 'r': return '\r'; | |
| 66 | 169 | case 'e': return 27; | |
| 67 | 169 | case 's': return 32; | |
| 68 | 847 | case '"': return '\"'; | |
| 69 | 422 | case '\\': return '\\'; | |
| 70 | 169 | case 'd': return 127; | |
| 71 | 170 | 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 | 28251153 | 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 339733424 times.
✓ Branch 1 taken 10117347 times.
|
349850771 | for (unsigned int i = 0; i < num; i ++) { |
| 112 | 339733424 | uint32_t tok_len = m[i].len; | |
| 113 | 339733424 | const char *match_str = m[i].str; | |
| 114 | char c; | ||
| 115 | int char_pos; | ||
| 116 |
2/2✓ Branch 0 taken 326677708 times.
✓ Branch 1 taken 18133789 times.
|
344811497 | for (char_pos = 0; char_pos < (int)tok_len; char_pos ++) { |
| 117 | 326677708 | int r = lbm_channel_peek(chan,(unsigned int)char_pos + start_pos, &c); | |
| 118 |
2/2✓ Branch 0 taken 326675888 times.
✓ Branch 1 taken 1820 times.
|
326677708 | if (r == CHANNEL_SUCCESS) { |
| 119 |
2/2✓ Branch 0 taken 321597815 times.
✓ Branch 1 taken 5078073 times.
|
326675888 | if (c != match_str[char_pos]) break; |
| 120 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1803 times.
|
1820 | } else if (r == CHANNEL_MORE ) { |
| 121 | 18133806 | return TOKENIZER_NEED_MORE; | |
| 122 | } else { | ||
| 123 | 1803 | break; | |
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 |
2/2✓ Branch 0 taken 18133789 times.
✓ Branch 1 taken 321599618 times.
|
339733407 | if (char_pos == (int)tok_len) { //match |
| 128 | 18133789 | *res = m[i].token; | |
| 129 | 18133789 | return (int)tok_len; | |
| 130 | } | ||
| 131 | } | ||
| 132 | 10117347 | return TOKENIZER_NO_TOKEN; | |
| 133 | } | ||
| 134 | |||
| 135 | 18050789 | int tok_syntax(lbm_char_channel_t *chan, uint32_t *res) { | |
| 136 | 18050789 | return tok_match_fixed_size_tokens(chan, fixed_size_tokens, 0, NUM_FIXED_SIZE_TOKENS, res); | |
| 137 | } | ||
| 138 | |||
| 139 | 12705612 | static bool alpha_char(char c) { | |
| 140 |
6/6✓ Branch 0 taken 8778301 times.
✓ Branch 1 taken 3927311 times.
✓ Branch 2 taken 252 times.
✓ Branch 3 taken 8778049 times.
✓ Branch 4 taken 26509 times.
✓ Branch 5 taken 3901054 times.
|
12732121 | return ((c >= 'a' && c <= 'z') || |
| 141 |
2/2✓ Branch 0 taken 3379 times.
✓ Branch 1 taken 23130 times.
|
26509 | (c >= 'A' && c <= 'Z')); |
| 142 | } | ||
| 143 | |||
| 144 | 3728005 | static bool num_char(char c) { | |
| 145 |
4/4✓ Branch 0 taken 413299 times.
✓ Branch 1 taken 3314706 times.
✓ Branch 2 taken 384559 times.
✓ Branch 3 taken 28740 times.
|
3728005 | return (c >= '0' && c <= '9'); |
| 146 | } | ||
| 147 | |||
| 148 | 3082406 | static bool symchar0(char c) { | |
| 149 | 3082406 | const char *allowed = "+-*/=<>#!"; | |
| 150 | |||
| 151 |
2/2✓ Branch 0 taken 2886227 times.
✓ Branch 1 taken 196179 times.
|
3082406 | if (alpha_char(c)) return true; |
| 152 | 196179 | int i = 0; | |
| 153 |
2/2✓ Branch 0 taken 630505 times.
✓ Branch 1 taken 2124 times.
|
632629 | while (allowed[i] != 0) { |
| 154 |
2/2✓ Branch 0 taken 194055 times.
✓ Branch 1 taken 436450 times.
|
630505 | if (c == allowed[i]) return true; |
| 155 | 436450 | i ++; | |
| 156 | } | ||
| 157 | 2124 | return false; | |
| 158 | } | ||
| 159 | |||
| 160 | 9623206 | static bool symchar(char c) { | |
| 161 | 9623206 | const char *allowed = "+-*/=<>!?_"; | |
| 162 | |||
| 163 |
4/4✓ Branch 0 taken 3728005 times.
✓ Branch 1 taken 5895201 times.
✓ Branch 2 taken 384559 times.
✓ Branch 3 taken 3343446 times.
|
9623206 | if (alpha_char(c) || num_char(c)) return true; |
| 164 | 3343446 | int i = 0; | |
| 165 |
2/2✓ Branch 0 taken 31223408 times.
✓ Branch 1 taken 3042677 times.
|
34266085 | while (allowed[i] != 0) { |
| 166 |
2/2✓ Branch 0 taken 300769 times.
✓ Branch 1 taken 30922639 times.
|
31223408 | if (c == allowed[i]) return true; |
| 167 | 30922639 | i++; | |
| 168 | } | ||
| 169 | 3042677 | return false; | |
| 170 | } | ||
| 171 | |||
| 172 | 3082408 | int tok_symbol(lbm_char_channel_t *chan) { | |
| 173 | |||
| 174 | char c; | ||
| 175 | 3082408 | int r = 0; | |
| 176 | |||
| 177 | 3082408 | r = lbm_channel_peek(chan, 0, &c); | |
| 178 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3082407 times.
|
3082408 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 179 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3082406 times.
|
3082407 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 180 |
3/4✓ Branch 0 taken 3082406 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2124 times.
✓ Branch 3 taken 3080282 times.
|
3082406 | if (r == CHANNEL_SUCCESS && !symchar0(c)) { |
| 181 | 2124 | return TOKENIZER_NO_TOKEN; | |
| 182 | } | ||
| 183 | 3080282 | memset(tokpar_sym_str,0,TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH+1); | |
| 184 |
4/4✓ Branch 0 taken 2886227 times.
✓ Branch 1 taken 194055 times.
✓ Branch 2 taken 514 times.
✓ Branch 3 taken 2885713 times.
|
3080282 | tokpar_sym_str[0] = (c >= 'A' && c <= 'Z') ? c + 32 : c; // locale independent ASCII only tolower. |
| 185 | |||
| 186 | 3080282 | int len = 1; | |
| 187 | |||
| 188 | 3080282 | r = lbm_channel_peek(chan,(unsigned int)len, &c); | |
| 189 |
4/4✓ Branch 0 taken 9623206 times.
✓ Branch 1 taken 37604 times.
✓ Branch 2 taken 6580529 times.
✓ Branch 3 taken 3042677 times.
|
9660810 | while (r == CHANNEL_SUCCESS && symchar(c)) { |
| 190 |
4/4✓ Branch 0 taken 5915958 times.
✓ Branch 1 taken 664571 times.
✓ Branch 2 taken 2865 times.
✓ Branch 3 taken 5913093 times.
|
6580529 | c = (c >= 'A' && c <= 'Z') ? c + 32 : c; // locale independent ASCII only tolower. |
| 191 |
2/2✓ Branch 0 taken 6580528 times.
✓ Branch 1 taken 1 times.
|
6580529 | if (len < TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH) { |
| 192 | 6580528 | tokpar_sym_str[len] = (char)c; | |
| 193 | } else { | ||
| 194 | 1 | return TOKENIZER_SYMBOL_ERROR; | |
| 195 | } | ||
| 196 | 6580528 | len ++; | |
| 197 | 6580528 | r = lbm_channel_peek(chan,(unsigned int)len, &c); | |
| 198 | } | ||
| 199 |
2/2✓ Branch 0 taken 559 times.
✓ Branch 1 taken 3079722 times.
|
3080281 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 200 | 3079722 | tokpar_sym_str[len] = 0; | |
| 201 | 3079722 | return len; | |
| 202 | } | ||
| 203 | |||
| 204 | 13297843 | int tok_string(lbm_char_channel_t *chan, unsigned int *string_len) { | |
| 205 | |||
| 206 | 13297843 | unsigned int n = 0; | |
| 207 | 13297843 | unsigned int len = 0; | |
| 208 | char c; | ||
| 209 | 13297843 | int r = 0; | |
| 210 | 13297843 | bool encode = false; | |
| 211 | |||
| 212 | 13297843 | r = lbm_channel_peek(chan,0,&c); | |
| 213 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13297842 times.
|
13297843 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 214 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13297841 times.
|
13297842 | else if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 215 | |||
| 216 |
2/2✓ Branch 0 taken 13266153 times.
✓ Branch 1 taken 31688 times.
|
13297841 | if (c != '\"') return TOKENIZER_NO_TOKEN;; |
| 217 | 31688 | n++; | |
| 218 | |||
| 219 | 31688 | memset(tokpar_sym_str,0,TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH+1); | |
| 220 | |||
| 221 | // read string into buffer | ||
| 222 | 31688 | r = lbm_channel_peek(chan,n,&c); | |
| 223 |
7/8✓ Branch 0 taken 212631 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 32344 times.
✓ Branch 3 taken 180287 times.
✓ Branch 4 taken 762 times.
✓ Branch 5 taken 31582 times.
✓ Branch 6 taken 181049 times.
✗ Branch 7 not taken.
|
212652 | while (r == CHANNEL_SUCCESS && (c != '\"' || encode) && |
| 224 | len < TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH) { | ||
| 225 |
4/4✓ Branch 0 taken 2703 times.
✓ Branch 1 taken 178346 times.
✓ Branch 2 taken 2366 times.
✓ Branch 3 taken 337 times.
|
181049 | if (c == '\\' && !encode) { |
| 226 | 2366 | encode = true; | |
| 227 | } else { | ||
| 228 |
2/2✓ Branch 0 taken 2366 times.
✓ Branch 1 taken 176317 times.
|
178683 | if (encode) { |
| 229 | 2366 | signed char result = translate_escape_char(c); | |
| 230 |
2/2✓ Branch 0 taken 85 times.
✓ Branch 1 taken 2281 times.
|
2366 | if (result == -1) { |
| 231 | 85 | return TOKENIZER_STRING_ERROR; | |
| 232 | } else { | ||
| 233 | 2281 | tokpar_sym_str[len] = result; | |
| 234 | } | ||
| 235 | } else { | ||
| 236 | 176317 | tokpar_sym_str[len] = c; | |
| 237 | } | ||
| 238 | 178598 | len++; | |
| 239 | 178598 | encode = false; | |
| 240 | } | ||
| 241 | 180964 | n ++; | |
| 242 | 180964 | r = lbm_channel_peek(chan, n, &c); | |
| 243 | } | ||
| 244 | |||
| 245 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 31585 times.
|
31603 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 246 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 31582 times.
|
31585 | if (c != '\"') return TOKENIZER_STRING_ERROR; |
| 247 | |||
| 248 | 31582 | *string_len = len; | |
| 249 | 31582 | n ++; | |
| 250 | 31582 | return (int)n; | |
| 251 | } | ||
| 252 | |||
| 253 | 2142 | int tok_char(lbm_char_channel_t *chan, char *res) { | |
| 254 | |||
| 255 | char c; | ||
| 256 | int r; | ||
| 257 | |||
| 258 | 2142 | r = lbm_channel_peek(chan, 0, &c); | |
| 259 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2141 times.
|
2142 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 260 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2140 times.
|
2141 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 261 | |||
| 262 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2137 times.
|
2140 | if (c != '\\') return TOKENIZER_NO_TOKEN; |
| 263 | |||
| 264 | 2137 | r = lbm_channel_peek(chan, 1, &c); | |
| 265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2137 times.
|
2137 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 266 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2137 times.
|
2137 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 267 | |||
| 268 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2137 times.
|
2137 | if (c != '#') return TOKENIZER_NO_TOKEN; |
| 269 | |||
| 270 | 2137 | r = lbm_channel_peek(chan, 2, &c); | |
| 271 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2137 times.
|
2137 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 272 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2137 times.
|
2137 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 273 | |||
| 274 |
2/2✓ Branch 0 taken 1360 times.
✓ Branch 1 taken 777 times.
|
2137 | if (c == '\\') { |
| 275 | 1360 | r = lbm_channel_peek(chan, 3, &c); | |
| 276 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1360 times.
|
1360 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 277 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1360 times.
|
1360 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 278 | |||
| 279 | 1360 | signed char result = translate_escape_char(c); | |
| 280 |
2/2✓ Branch 0 taken 1275 times.
✓ Branch 1 taken 85 times.
|
1360 | if (result != -1) { |
| 281 | 1275 | *res = result; | |
| 282 | 1275 | return 4; | |
| 283 | } else { | ||
| 284 | 85 | return TOKENIZER_CHAR_ERROR; | |
| 285 | } | ||
| 286 | } | ||
| 287 | 777 | *res = c; | |
| 288 | 777 | 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 | 13266156 | int tok_double(lbm_char_channel_t *chan, token_float *result) { | |
| 296 | |||
| 297 | 13266156 | unsigned int n = 0; | |
| 298 | char fbuf[TD_BUF_SIZE]; | ||
| 299 | char c; | ||
| 300 | 13266156 | bool valid_num = false; | |
| 301 | int res; | ||
| 302 | |||
| 303 | 13266156 | memset(fbuf, 0, TD_BUF_SIZE); | |
| 304 | |||
| 305 | 13266156 | result->type = TOKTYPEF32; | |
| 306 | 13266156 | result->negative = false; | |
| 307 | |||
| 308 | 13266156 | res = lbm_channel_peek(chan, n, &c); | |
| 309 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13266155 times.
|
13266156 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 310 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13266154 times.
|
13266155 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 311 |
2/2✓ Branch 0 taken 23201 times.
✓ Branch 1 taken 13242953 times.
|
13266154 | if (c == '-') { |
| 312 |
1/2✓ Branch 0 taken 23201 times.
✗ Branch 1 not taken.
|
23201 | FBUF_ADD('-', n); |
| 313 | 23201 | result->negative = true; | |
| 314 | } | ||
| 315 | |||
| 316 | 13266154 | res = lbm_channel_peek(chan, n, &c); | |
| 317 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 13266149 times.
|
13266154 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 318 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13266149 times.
|
13266149 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 319 |
4/4✓ Branch 0 taken 15964759 times.
✓ Branch 1 taken 10176949 times.
✓ Branch 2 taken 12875981 times.
✓ Branch 3 taken 3088778 times.
|
26141708 | while (c >= '0' && c <= '9') { |
| 320 |
1/2✓ Branch 0 taken 12875981 times.
✗ Branch 1 not taken.
|
12875981 | FBUF_ADD(c, n); |
| 321 | 12875981 | res = lbm_channel_peek(chan, n, &c); | |
| 322 |
2/2✓ Branch 0 taken 245 times.
✓ Branch 1 taken 12875736 times.
|
12875981 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 323 |
2/2✓ Branch 0 taken 177 times.
✓ Branch 1 taken 12875559 times.
|
12875736 | if (res == CHANNEL_END) break; |
| 324 | } | ||
| 325 | |||
| 326 |
2/2✓ Branch 0 taken 36239 times.
✓ Branch 1 taken 13229665 times.
|
13265904 | if (c == '.') { |
| 327 |
1/2✓ Branch 0 taken 36239 times.
✗ Branch 1 not taken.
|
36239 | FBUF_ADD(c, n); |
| 328 | } | ||
| 329 | 13229665 | else return TOKENIZER_NO_TOKEN; | |
| 330 | |||
| 331 | 36239 | res = lbm_channel_peek(chan,n, &c); | |
| 332 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 36235 times.
|
36239 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 333 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36235 times.
|
36235 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 334 |
2/4✓ Branch 0 taken 36235 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36235 times.
|
36235 | if (!(c >= '0' && c <= '9')) return TOKENIZER_NO_TOKEN; |
| 335 | |||
| 336 |
4/4✓ Branch 0 taken 69680 times.
✓ Branch 1 taken 17884 times.
✓ Branch 2 taken 51330 times.
✓ Branch 3 taken 18350 times.
|
87564 | while (c >= '0' && c <= '9') { |
| 337 |
1/2✓ Branch 0 taken 51330 times.
✗ Branch 1 not taken.
|
51330 | FBUF_ADD(c, n); |
| 338 | 51330 | res = lbm_channel_peek(chan, n, &c); | |
| 339 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 51330 times.
|
51330 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 340 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 51329 times.
|
51330 | if (res == CHANNEL_END) break; |
| 341 | } | ||
| 342 | |||
| 343 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 35899 times.
|
36235 | if (c == 'e') { |
| 344 |
1/2✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
|
336 | FBUF_ADD(c, n); |
| 345 | 336 | res = lbm_channel_peek(chan,n, &c); | |
| 346 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 347 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 348 |
2/6✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 336 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
336 | if (!((c >= '0' && c <= '9') || c == '-')) return TOKENIZER_NO_TOKEN; |
| 349 | |||
| 350 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | if (c == '-') { |
| 351 | ✗ | FBUF_ADD(c, n); | |
| 352 | } | ||
| 353 | 336 | res = lbm_channel_peek(chan,n, &c); | |
| 354 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 355 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 356 |
4/4✓ Branch 0 taken 504 times.
✓ Branch 1 taken 168 times.
✓ Branch 2 taken 336 times.
✓ Branch 3 taken 168 times.
|
672 | while ((c >= '0' && c <= '9')) { |
| 357 |
1/2✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
|
336 | FBUF_ADD(c,n); |
| 358 | 336 | res = lbm_channel_peek(chan, n, &c); | |
| 359 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 360 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | if (res == CHANNEL_END) break; |
| 361 | } | ||
| 362 | } | ||
| 363 | |||
| 364 | uint32_t tok_res; | ||
| 365 | 36235 | int type_len = tok_match_fixed_size_tokens(chan, type_qual_table, n, NUM_TYPE_QUALIFIERS, &tok_res); | |
| 366 | |||
| 367 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36235 times.
|
36235 | if (type_len == TOKENIZER_NEED_MORE) return type_len; |
| 368 |
2/2✓ Branch 0 taken 18053 times.
✓ Branch 1 taken 18182 times.
|
36235 | if (type_len == TOKENIZER_NO_TOKEN) { |
| 369 | 18053 | result->type = TOKTYPEF32; | |
| 370 | } else { | ||
| 371 | 18182 | result->type = tok_res; | |
| 372 | } | ||
| 373 | |||
| 374 |
3/4✓ Branch 0 taken 796 times.
✓ Branch 1 taken 35439 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 796 times.
|
36235 | if ((result->negative && n > 1) || |
| 375 |
2/4✓ Branch 0 taken 35439 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35439 times.
✗ Branch 3 not taken.
|
36235 | (!result->negative && n > 0)) valid_num = true; |
| 376 | |||
| 377 |
1/2✓ Branch 0 taken 36235 times.
✗ Branch 1 not taken.
|
36235 | if(valid_num) { |
| 378 | 36235 | result->value = (double)strtod(fbuf,NULL); | |
| 379 | 36235 | return (int)n + type_len; | |
| 380 | } | ||
| 381 | |||
| 382 | ✗ | tok_double_no_tok: | |
| 383 | ✗ | return TOKENIZER_NO_TOKEN; | |
| 384 | } | ||
| 385 | |||
| 386 | 18119760 | bool tok_clean_whitespace(lbm_char_channel_t *chan) { | |
| 387 | |||
| 388 | 18119760 | bool cleaning_whitespace = true; | |
| 389 | char c; | ||
| 390 | int r; | ||
| 391 | |||
| 392 |
2/2✓ Branch 0 taken 18144584 times.
✓ Branch 1 taken 18050787 times.
|
36195371 | while (cleaning_whitespace) { |
| 393 | |||
| 394 |
2/2✓ Branch 0 taken 27079 times.
✓ Branch 1 taken 18117505 times.
|
18144584 | if (lbm_channel_comment(chan)) { |
| 395 | while (true) { | ||
| 396 | 2372845 | r = lbm_channel_peek(chan, 0, &c); | |
| 397 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2372845 times.
|
2372845 | if (r == CHANNEL_END) { |
| 398 | ✗ | lbm_channel_set_comment(chan, false); | |
| 399 | ✗ | cleaning_whitespace = false; | |
| 400 | ✗ | break; | |
| 401 | } | ||
| 402 |
2/2✓ Branch 0 taken 2275 times.
✓ Branch 1 taken 2370570 times.
|
2372845 | if (r == CHANNEL_MORE) { |
| 403 | 2275 | return false; | |
| 404 | } | ||
| 405 | 2370570 | lbm_channel_drop(chan,1); | |
| 406 |
2/2✓ Branch 0 taken 24804 times.
✓ Branch 1 taken 2345766 times.
|
2370570 | if (c == '\n') { |
| 407 | 24804 | lbm_channel_set_comment(chan, false); | |
| 408 | 24804 | break; | |
| 409 | } | ||
| 410 | } | ||
| 411 | } | ||
| 412 | |||
| 413 | do { | ||
| 414 | 32597440 | r = lbm_channel_peek(chan, 0, &c); | |
| 415 |
2/2✓ Branch 0 taken 1082 times.
✓ Branch 1 taken 32596358 times.
|
32597440 | if (r == CHANNEL_MORE) { |
| 416 | 1082 | return false; | |
| 417 |
2/2✓ Branch 0 taken 65616 times.
✓ Branch 1 taken 32530742 times.
|
32596358 | } else if (r == CHANNEL_END) { |
| 418 | 65616 | return true; | |
| 419 | } | ||
| 420 |
2/2✓ Branch 0 taken 24824 times.
✓ Branch 1 taken 32505918 times.
|
32530742 | if (c == ';') { |
| 421 | 24824 | lbm_channel_set_comment(chan, true); | |
| 422 | 24824 | break; | |
| 423 | } | ||
| 424 |
2/2✓ Branch 0 taken 14455131 times.
✓ Branch 1 taken 18050787 times.
|
32505918 | if (isspace(c)) { |
| 425 | 14455131 | lbm_channel_drop(chan,1); | |
| 426 | } else { | ||
| 427 | 18050787 | cleaning_whitespace = false; | |
| 428 | } | ||
| 429 | |||
| 430 |
2/2✓ Branch 0 taken 14455131 times.
✓ Branch 1 taken 18050787 times.
|
32505918 | } while (cleaning_whitespace); |
| 431 | } | ||
| 432 | 18050787 | return true; | |
| 433 | } | ||
| 434 | |||
| 435 | 13229670 | int tok_integer(lbm_char_channel_t *chan, token_int *result) { | |
| 436 | 13229670 | uint64_t acc = 0; | |
| 437 | 13229670 | unsigned int n = 0; | |
| 438 | 13229670 | bool valid_num = false; | |
| 439 | char c; | ||
| 440 | int res; | ||
| 441 | |||
| 442 | 13229670 | result->type = TOKTYPEI; | |
| 443 | 13229670 | result-> negative = false; | |
| 444 | 13229670 | res = lbm_channel_peek(chan, 0, &c); | |
| 445 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13229669 times.
|
13229670 | if (res == CHANNEL_MORE) { |
| 446 | 1 | return TOKENIZER_NEED_MORE; | |
| 447 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13229668 times.
|
13229669 | } else if (res == CHANNEL_END) { |
| 448 | 1 | return TOKENIZER_NO_TOKEN; | |
| 449 | } | ||
| 450 |
2/2✓ Branch 0 taken 22400 times.
✓ Branch 1 taken 13207268 times.
|
13229668 | if (c == '-') { |
| 451 | 22400 | n = 1; | |
| 452 | 22400 | result->negative = true; | |
| 453 | } | ||
| 454 | |||
| 455 | 13229668 | bool hex = false; | |
| 456 | 13229668 | res = lbm_channel_peek(chan, n, &c); | |
| 457 |
3/4✓ Branch 0 taken 13229668 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 94457 times.
✓ Branch 3 taken 13135211 times.
|
13229668 | if (res == CHANNEL_SUCCESS && c == '0') { |
| 458 | 94457 | res = lbm_channel_peek(chan, n + 1, &c); | |
| 459 |
4/6✓ Branch 0 taken 94457 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 46650 times.
✓ Branch 3 taken 47807 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 46650 times.
|
94457 | if ( res == CHANNEL_SUCCESS && (c == 'x' || c == 'X')) { |
| 460 | 47807 | hex = true; | |
| 461 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 46650 times.
|
46650 | } else if (res == CHANNEL_MORE) { |
| 462 | ✗ | return TOKENIZER_NEED_MORE; | |
| 463 | } | ||
| 464 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13135211 times.
|
13135211 | } else if (res == CHANNEL_MORE) { |
| 465 | ✗ | return TOKENIZER_NEED_MORE; | |
| 466 | } | ||
| 467 | |||
| 468 |
2/2✓ Branch 0 taken 47807 times.
✓ Branch 1 taken 13181861 times.
|
13229668 | if (hex) { |
| 469 | 47807 | n += 2; | |
| 470 | |||
| 471 | 47807 | res = lbm_channel_peek(chan,n, &c); | |
| 472 | |||
| 473 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 47801 times.
|
47807 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 474 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 47801 times.
|
47801 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 475 | |||
| 476 |
2/2✓ Branch 0 taken 80307 times.
✓ Branch 1 taken 31485 times.
|
159593 | while ((c >= '0' && c <= '9') || |
| 477 |
6/6✓ Branch 0 taken 111792 times.
✓ Branch 1 taken 46759 times.
✓ Branch 2 taken 866 times.
✓ Branch 3 taken 77378 times.
✓ Branch 4 taken 94 times.
✓ Branch 5 taken 772 times.
|
190036 | (c >= 'a' && c <= 'f') || |
| 478 |
4/4✓ Branch 0 taken 31391 times.
✓ Branch 1 taken 46759 times.
✓ Branch 2 taken 30367 times.
✓ Branch 3 taken 1024 times.
|
78150 | (c >= 'A' && c <= 'F')) { |
| 479 | uint32_t val; /* values between 0 and 16 */ | ||
| 480 |
3/4✓ Branch 0 taken 94 times.
✓ Branch 1 taken 110674 times.
✓ Branch 2 taken 94 times.
✗ Branch 3 not taken.
|
110768 | if (c >= 'a' && c <= 'f') { |
| 481 | 94 | val = 10 + (uint32_t)c - 'a'; | |
| 482 |
3/4✓ Branch 0 taken 30367 times.
✓ Branch 1 taken 80307 times.
✓ Branch 2 taken 30367 times.
✗ Branch 3 not taken.
|
110674 | } else if (c >= 'A' && c <= 'F') { |
| 483 | 30367 | val = 10 + (uint32_t)(c - 'A'); | |
| 484 | } else { | ||
| 485 | 80307 | val = (uint32_t)c - '0'; | |
| 486 | } | ||
| 487 | 110768 | acc = (acc * 0x10) + val; | |
| 488 | 110768 | n++; | |
| 489 | 110768 | res = lbm_channel_peek(chan, n, &c); | |
| 490 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 110750 times.
|
110768 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 491 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 110750 times.
|
110750 | if (res == CHANNEL_END) break; |
| 492 | |||
| 493 | } | ||
| 494 | } else { | ||
| 495 | 13181861 | res = lbm_channel_peek(chan, n, &c); | |
| 496 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13181861 times.
|
13181861 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 497 |
4/4✓ Branch 0 taken 15830322 times.
✓ Branch 1 taken 10140712 times.
✓ Branch 2 taken 12789351 times.
✓ Branch 3 taken 3040971 times.
|
25971034 | while (c >= '0' && c <= '9') { |
| 498 | 12789351 | acc = (acc*10) + (uint32_t)(c - '0'); | |
| 499 | 12789351 | n++; | |
| 500 | 12789351 | res = lbm_channel_peek(chan, n, &c); | |
| 501 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12789351 times.
|
12789351 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 502 |
2/2✓ Branch 0 taken 178 times.
✓ Branch 1 taken 12789173 times.
|
12789351 | if (res == CHANNEL_END) break; |
| 503 | } | ||
| 504 | } | ||
| 505 | |||
| 506 |
5/6✓ Branch 0 taken 10164129 times.
✓ Branch 1 taken 3065515 times.
✓ Branch 2 taken 47783 times.
✓ Branch 3 taken 10116346 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 47783 times.
|
13229644 | if (n == 0 || (hex && n == 2)) return TOKENIZER_NO_TOKEN; |
| 507 | |||
| 508 | uint32_t tok_res; | ||
| 509 | 10164129 | 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 12 times.
✓ Branch 1 taken 10164117 times.
|
10164129 | if (type_len == TOKENIZER_NEED_MORE) return type_len; |
| 512 |
2/2✓ Branch 0 taken 64823 times.
✓ Branch 1 taken 10099294 times.
|
10164117 | if (type_len != TOKENIZER_NO_TOKEN) { |
| 513 | 64823 | result->type = tok_res; | |
| 514 | } | ||
| 515 | |||
| 516 |
4/4✓ Branch 0 taken 22400 times.
✓ Branch 1 taken 10141717 times.
✓ Branch 2 taken 16888 times.
✓ Branch 3 taken 5512 times.
|
10164117 | if ((result->negative && n > 1) || |
| 517 |
2/2✓ Branch 0 taken 10141717 times.
✓ Branch 1 taken 16888 times.
|
10164117 | !result->negative) valid_num = true; |
| 518 | |||
| 519 |
2/2✓ Branch 0 taken 10147229 times.
✓ Branch 1 taken 16888 times.
|
10164117 | if (valid_num) { |
| 520 | 10147229 | result->value = acc; | |
| 521 | 10147229 | return (int)n + type_len; | |
| 522 | } | ||
| 523 | 16888 | return TOKENIZER_NO_TOKEN; | |
| 524 | } | ||
| 525 |