| 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 | 3753 | 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 172 times.
✓ Branch 4 taken 433 times.
✓ Branch 5 taken 169 times.
✓ Branch 6 taken 169 times.
✓ Branch 7 taken 172 times.
✓ Branch 8 taken 169 times.
✓ Branch 9 taken 169 times.
✓ Branch 10 taken 860 times.
✓ Branch 11 taken 424 times.
✓ Branch 12 taken 169 times.
✓ Branch 13 taken 172 times.
|
3753 | switch (c) { |
| 58 | 253 | case '0': return '\0'; | |
| 59 | 169 | case 'a': return '\a'; | |
| 60 | 253 | case 'b': return '\b'; | |
| 61 | 172 | case 't': return '\t'; | |
| 62 | 433 | case 'n': return '\n'; | |
| 63 | 169 | case 'v': return '\v'; | |
| 64 | 169 | case 'f': return '\f'; | |
| 65 | 172 | case 'r': return '\r'; | |
| 66 | 169 | case 'e': return 27; | |
| 67 | 169 | case 's': return 32; | |
| 68 | 860 | case '"': return '\"'; | |
| 69 | 424 | case '\\': return '\\'; | |
| 70 | 169 | case 'd': return 127; | |
| 71 | 172 | 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 | 29857881 | 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 355157694 times.
✓ Branch 1 taken 10228816 times.
|
365386510 | for (unsigned int i = 0; i < num; i ++) { |
| 112 | 355157694 | uint32_t tok_len = m[i].len; | |
| 113 | 355157694 | const char *match_str = m[i].str; | |
| 114 | char c; | ||
| 115 | int char_pos; | ||
| 116 |
2/2✓ Branch 0 taken 341369918 times.
✓ Branch 1 taken 19628991 times.
|
360998909 | for (char_pos = 0; char_pos < (int)tok_len; char_pos ++) { |
| 117 | 341369918 | int r = lbm_channel_peek(chan,(unsigned int)char_pos + start_pos, &c); | |
| 118 |
2/2✓ Branch 0 taken 341367883 times.
✓ Branch 1 taken 2035 times.
|
341369918 | if (r == CHANNEL_SUCCESS) { |
| 119 |
2/2✓ Branch 0 taken 335526668 times.
✓ Branch 1 taken 5841215 times.
|
341367883 | if (c != match_str[char_pos]) break; |
| 120 |
2/2✓ Branch 0 taken 74 times.
✓ Branch 1 taken 1961 times.
|
2035 | } else if (r == CHANNEL_MORE ) { |
| 121 | 19629065 | return TOKENIZER_NEED_MORE; | |
| 122 | } else { | ||
| 123 | 1961 | break; | |
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 |
2/2✓ Branch 0 taken 19628991 times.
✓ Branch 1 taken 335528629 times.
|
355157620 | if (char_pos == (int)tok_len) { //match |
| 128 | 19628991 | *res = m[i].token; | |
| 129 | 19628991 | return (int)tok_len; | |
| 130 | } | ||
| 131 | } | ||
| 132 | 10228816 | return TOKENIZER_NO_TOKEN; | |
| 133 | } | ||
| 134 | |||
| 135 | 19545660 | int tok_syntax(lbm_char_channel_t *chan, uint32_t *res) { | |
| 136 | 19545660 | return tok_match_fixed_size_tokens(chan, fixed_size_tokens, 0, NUM_FIXED_SIZE_TOKENS, res); | |
| 137 | } | ||
| 138 | |||
| 139 | 16366357 | static bool alpha_char(char c) { | |
| 140 |
6/6✓ Branch 0 taken 11622661 times.
✓ Branch 1 taken 4743696 times.
✓ Branch 2 taken 252 times.
✓ Branch 3 taken 11622409 times.
✓ Branch 4 taken 31018 times.
✓ Branch 5 taken 4712930 times.
|
16397375 | return ((c >= 'a' && c <= 'z') || |
| 141 |
2/2✓ Branch 0 taken 3568 times.
✓ Branch 1 taken 27450 times.
|
31018 | (c >= 'A' && c <= 'Z')); |
| 142 | } | ||
| 143 | |||
| 144 | 4500303 | static bool num_char(char c) { | |
| 145 |
4/4✓ Branch 0 taken 466081 times.
✓ Branch 1 taken 4034222 times.
✓ Branch 2 taken 428701 times.
✓ Branch 3 taken 37380 times.
|
4500303 | return (c >= '0' && c <= '9'); |
| 146 | } | ||
| 147 | |||
| 148 | 3713604 | static bool symchar0(char c) { | |
| 149 | 3713604 | const char *allowed = "+-*/=<>#!"; | |
| 150 | |||
| 151 |
2/2✓ Branch 1 taken 3473527 times.
✓ Branch 2 taken 240077 times.
|
3713604 | if (alpha_char(c)) return true; |
| 152 | 240077 | int i = 0; | |
| 153 |
2/2✓ Branch 0 taken 767719 times.
✓ Branch 1 taken 2134 times.
|
769853 | while (allowed[i] != 0) { |
| 154 |
2/2✓ Branch 0 taken 237943 times.
✓ Branch 1 taken 529776 times.
|
767719 | if (c == allowed[i]) return true; |
| 155 | 529776 | i ++; | |
| 156 | } | ||
| 157 | 2134 | return false; | |
| 158 | } | ||
| 159 | |||
| 160 | 12652753 | static bool symchar(char c) { | |
| 161 | 12652753 | const char *allowed = "+-*/=<>!?_"; | |
| 162 | |||
| 163 |
4/4✓ Branch 1 taken 4500303 times.
✓ Branch 2 taken 8152450 times.
✓ Branch 4 taken 428701 times.
✓ Branch 5 taken 4071602 times.
|
12652753 | if (alpha_char(c) || num_char(c)) return true; |
| 164 | 4071602 | int i = 0; | |
| 165 |
2/2✓ Branch 0 taken 37738728 times.
✓ Branch 1 taken 3669008 times.
|
41407736 | while (allowed[i] != 0) { |
| 166 |
2/2✓ Branch 0 taken 402594 times.
✓ Branch 1 taken 37336134 times.
|
37738728 | if (c == allowed[i]) return true; |
| 167 | 37336134 | i++; | |
| 168 | } | ||
| 169 | 3669008 | return false; | |
| 170 | } | ||
| 171 | |||
| 172 | 3713606 | int tok_symbol(lbm_char_channel_t *chan) { | |
| 173 | |||
| 174 | char c; | ||
| 175 | 3713606 | int r = 0; | |
| 176 | |||
| 177 | 3713606 | r = lbm_channel_peek(chan, 0, &c); | |
| 178 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3713605 times.
|
3713606 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 179 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3713604 times.
|
3713605 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 180 |
3/4✓ Branch 0 taken 3713604 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2134 times.
✓ Branch 4 taken 3711470 times.
|
3713604 | if (r == CHANNEL_SUCCESS && !symchar0(c)) { |
| 181 | 2134 | return TOKENIZER_NO_TOKEN; | |
| 182 | } | ||
| 183 | 3711470 | memset(tokpar_sym_str,0,TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH+1); | |
| 184 |
4/4✓ Branch 0 taken 3473527 times.
✓ Branch 1 taken 237943 times.
✓ Branch 2 taken 534 times.
✓ Branch 3 taken 3472993 times.
|
3711470 | tokpar_sym_str[0] = (c >= 'A' && c <= 'Z') ? c + 32 : c; // locale independent ASCII only tolower. |
| 185 | |||
| 186 | 3711470 | int len = 1; | |
| 187 | |||
| 188 | 3711470 | r = lbm_channel_peek(chan,(unsigned int)len, &c); | |
| 189 |
4/4✓ Branch 0 taken 12652753 times.
✓ Branch 1 taken 42459 times.
✓ Branch 3 taken 8983745 times.
✓ Branch 4 taken 3669008 times.
|
12695212 | while (r == CHANNEL_SUCCESS && symchar(c)) { |
| 190 |
4/4✓ Branch 0 taken 8177518 times.
✓ Branch 1 taken 806227 times.
✓ Branch 2 taken 3034 times.
✓ Branch 3 taken 8174484 times.
|
8983745 | c = (c >= 'A' && c <= 'Z') ? c + 32 : c; // locale independent ASCII only tolower. |
| 191 |
2/2✓ Branch 0 taken 8983742 times.
✓ Branch 1 taken 3 times.
|
8983745 | if (len < TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH) { |
| 192 | 8983742 | tokpar_sym_str[len] = (char)c; | |
| 193 | } else { | ||
| 194 | 3 | return TOKENIZER_SYMBOL_ERROR; | |
| 195 | } | ||
| 196 | 8983742 | len ++; | |
| 197 | 8983742 | r = lbm_channel_peek(chan,(unsigned int)len, &c); | |
| 198 | } | ||
| 199 |
2/2✓ Branch 0 taken 1382 times.
✓ Branch 1 taken 3710085 times.
|
3711467 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 200 | 3710085 | tokpar_sym_str[len] = 0; | |
| 201 | 3710085 | return len; | |
| 202 | } | ||
| 203 | |||
| 204 | 14032078 | int tok_string(lbm_char_channel_t *chan, unsigned int *string_len) { | |
| 205 | |||
| 206 | 14032078 | unsigned int n = 0; | |
| 207 | 14032078 | unsigned int len = 0; | |
| 208 | char c; | ||
| 209 | 14032078 | int r = 0; | |
| 210 | 14032078 | bool encode = false; | |
| 211 | |||
| 212 | 14032078 | r = lbm_channel_peek(chan,0,&c); | |
| 213 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 14032077 times.
|
14032078 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 214 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 14032076 times.
|
14032077 | else if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 215 | |||
| 216 |
2/2✓ Branch 0 taken 13997342 times.
✓ Branch 1 taken 34734 times.
|
14032076 | if (c != '\"') return TOKENIZER_NO_TOKEN;; |
| 217 | 34734 | n++; | |
| 218 | |||
| 219 | 34734 | memset(tokpar_sym_str,0,TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH+1); | |
| 220 | |||
| 221 | // read string into buffer | ||
| 222 | 34734 | r = lbm_channel_peek(chan,n,&c); | |
| 223 |
7/8✓ Branch 0 taken 251432 times.
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 35376 times.
✓ Branch 3 taken 216056 times.
✓ Branch 4 taken 775 times.
✓ Branch 5 taken 34601 times.
✓ Branch 6 taken 216831 times.
✗ Branch 7 not taken.
|
251478 | while (r == CHANNEL_SUCCESS && (c != '\"' || encode) && |
| 224 | len < TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH) { | ||
| 225 |
4/4✓ Branch 0 taken 2732 times.
✓ Branch 1 taken 214099 times.
✓ Branch 2 taken 2393 times.
✓ Branch 3 taken 339 times.
|
216831 | if (c == '\\' && !encode) { |
| 226 | 2393 | encode = true; | |
| 227 | } else { | ||
| 228 |
2/2✓ Branch 0 taken 2393 times.
✓ Branch 1 taken 212045 times.
|
214438 | if (encode) { |
| 229 | 2393 | signed char result = translate_escape_char(c); | |
| 230 |
2/2✓ Branch 0 taken 87 times.
✓ Branch 1 taken 2306 times.
|
2393 | if (result == -1) { |
| 231 | 87 | return TOKENIZER_STRING_ERROR; | |
| 232 | } else { | ||
| 233 | 2306 | tokpar_sym_str[len] = result; | |
| 234 | } | ||
| 235 | } else { | ||
| 236 | 212045 | tokpar_sym_str[len] = c; | |
| 237 | } | ||
| 238 | 214351 | len++; | |
| 239 | 214351 | encode = false; | |
| 240 | } | ||
| 241 | 216744 | n ++; | |
| 242 | 216744 | r = lbm_channel_peek(chan, n, &c); | |
| 243 | } | ||
| 244 | |||
| 245 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 34610 times.
|
34647 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 246 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 34601 times.
|
34610 | if (c != '\"') return TOKENIZER_STRING_ERROR; |
| 247 | |||
| 248 | 34601 | *string_len = len; | |
| 249 | 34601 | n ++; | |
| 250 | 34601 | return (int)n; | |
| 251 | } | ||
| 252 | |||
| 253 | 2152 | int tok_char(lbm_char_channel_t *chan, char *res) { | |
| 254 | |||
| 255 | char c; | ||
| 256 | int r; | ||
| 257 | |||
| 258 | 2152 | r = lbm_channel_peek(chan, 0, &c); | |
| 259 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2151 times.
|
2152 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 260 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2150 times.
|
2151 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 261 | |||
| 262 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2143 times.
|
2150 | if (c != '\\') return TOKENIZER_NO_TOKEN; |
| 263 | |||
| 264 | 2143 | r = lbm_channel_peek(chan, 1, &c); | |
| 265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2143 times.
|
2143 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 266 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2143 times.
|
2143 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 267 | |||
| 268 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2143 times.
|
2143 | if (c != '#') return TOKENIZER_NO_TOKEN; |
| 269 | |||
| 270 | 2143 | r = lbm_channel_peek(chan, 2, &c); | |
| 271 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2143 times.
|
2143 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 272 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2143 times.
|
2143 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 273 | |||
| 274 |
2/2✓ Branch 0 taken 1360 times.
✓ Branch 1 taken 783 times.
|
2143 | 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 | 783 | *res = c; | |
| 288 | 783 | 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 | 13997345 | int tok_double(lbm_char_channel_t *chan, token_float *result) { | |
| 296 | |||
| 297 | 13997345 | unsigned int n = 0; | |
| 298 | char fbuf[TD_BUF_SIZE]; | ||
| 299 | char c; | ||
| 300 | 13997345 | bool valid_num = false; | |
| 301 | int res; | ||
| 302 | int type_len; | ||
| 303 | uint32_t tok_res; | ||
| 304 | |||
| 305 | 13997345 | memset(fbuf, 0, TD_BUF_SIZE); | |
| 306 | |||
| 307 | 13997345 | result->type = TOKTYPEF32; | |
| 308 | 13997345 | result->negative = false; | |
| 309 | |||
| 310 | 13997345 | res = lbm_channel_peek(chan, n, &c); | |
| 311 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 13997344 times.
|
13997345 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 312 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 13997343 times.
|
13997344 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 313 |
2/2✓ Branch 0 taken 35666 times.
✓ Branch 1 taken 13961677 times.
|
13997343 | if (c == '-') { |
| 314 |
1/2✓ Branch 0 taken 35666 times.
✗ Branch 1 not taken.
|
35666 | FBUF_ADD('-', n); |
| 315 | 35666 | result->negative = true; | |
| 316 | } | ||
| 317 | |||
| 318 | 13997343 | res = lbm_channel_peek(chan, n, &c); | |
| 319 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 13997334 times.
|
13997343 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 320 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13997334 times.
|
13997334 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 321 |
4/4✓ Branch 0 taken 16679213 times.
✓ Branch 1 taken 10303938 times.
✓ Branch 2 taken 12986526 times.
✓ Branch 3 taken 3692687 times.
|
26983151 | while (c >= '0' && c <= '9') { |
| 322 |
1/2✓ Branch 0 taken 12986526 times.
✗ Branch 1 not taken.
|
12986526 | FBUF_ADD(c, n); |
| 323 | 12986526 | res = lbm_channel_peek(chan, n, &c); | |
| 324 |
2/2✓ Branch 0 taken 516 times.
✓ Branch 1 taken 12986010 times.
|
12986526 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 325 |
2/2✓ Branch 0 taken 193 times.
✓ Branch 1 taken 12985817 times.
|
12986010 | if (res == CHANNEL_END) break; |
| 326 | } | ||
| 327 | |||
| 328 |
2/2✓ Branch 0 taken 36883 times.
✓ Branch 1 taken 13959935 times.
|
13996818 | if (c == '.') { |
| 329 |
1/2✓ Branch 0 taken 36883 times.
✗ Branch 1 not taken.
|
36883 | FBUF_ADD(c, n); |
| 330 | } | ||
| 331 | 13959935 | else return TOKENIZER_NO_TOKEN; | |
| 332 | |||
| 333 | 36883 | res = lbm_channel_peek(chan,n, &c); | |
| 334 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 36874 times.
|
36883 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 335 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36874 times.
|
36874 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 336 |
2/4✓ Branch 0 taken 36874 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36874 times.
|
36874 | if (!(c >= '0' && c <= '9')) return TOKENIZER_NO_TOKEN; |
| 337 | |||
| 338 |
4/4✓ Branch 0 taken 70933 times.
✓ Branch 1 taken 18290 times.
✓ Branch 2 taken 52368 times.
✓ Branch 3 taken 18565 times.
|
89223 | while (c >= '0' && c <= '9') { |
| 339 |
1/2✓ Branch 0 taken 52368 times.
✗ Branch 1 not taken.
|
52368 | FBUF_ADD(c, n); |
| 340 | 52368 | res = lbm_channel_peek(chan, n, &c); | |
| 341 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 52350 times.
|
52368 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 342 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 52349 times.
|
52350 | if (res == CHANNEL_END) break; |
| 343 | } | ||
| 344 | |||
| 345 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 36520 times.
|
36856 | if (c == 'e') { |
| 346 |
1/2✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
|
336 | FBUF_ADD(c, n); |
| 347 | 336 | res = lbm_channel_peek(chan,n, &c); | |
| 348 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 349 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 350 |
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; |
| 351 | |||
| 352 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | if (c == '-') { |
| 353 | ✗ | FBUF_ADD(c, n); | |
| 354 | } | ||
| 355 | 336 | res = lbm_channel_peek(chan,n, &c); | |
| 356 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 357 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 358 |
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')) { |
| 359 |
1/2✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
|
336 | FBUF_ADD(c,n); |
| 360 | 336 | res = lbm_channel_peek(chan, n, &c); | |
| 361 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 362 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | if (res == CHANNEL_END) break; |
| 363 | } | ||
| 364 | } | ||
| 365 | |||
| 366 | 36856 | type_len = tok_match_fixed_size_tokens(chan, type_qual_table, n, NUM_TYPE_QUALIFIERS, &tok_res); | |
| 367 | |||
| 368 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 36846 times.
|
36856 | if (type_len == TOKENIZER_NEED_MORE) return type_len; |
| 369 |
2/2✓ Branch 0 taken 18459 times.
✓ Branch 1 taken 18387 times.
|
36846 | if (type_len == TOKENIZER_NO_TOKEN) { |
| 370 | 18459 | result->type = TOKTYPEF32; | |
| 371 | } else { | ||
| 372 | 18387 | result->type = tok_res; | |
| 373 | } | ||
| 374 | |||
| 375 |
3/4✓ Branch 0 taken 820 times.
✓ Branch 1 taken 36026 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 820 times.
|
36846 | if ((result->negative && n > 1) || |
| 376 |
2/4✓ Branch 0 taken 36026 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36026 times.
✗ Branch 3 not taken.
|
36846 | (!result->negative && n > 0)) valid_num = true; |
| 377 | |||
| 378 |
1/2✓ Branch 0 taken 36846 times.
✗ Branch 1 not taken.
|
36846 | if(valid_num) { |
| 379 | 36846 | result->value = (double)strtod(fbuf,NULL); | |
| 380 | 36846 | return (int)n + type_len; | |
| 381 | } | ||
| 382 | |||
| 383 | ✗ | tok_double_no_tok: | |
| 384 | ✗ | return TOKENIZER_NO_TOKEN; | |
| 385 | } | ||
| 386 | |||
| 387 | 19616578 | bool tok_clean_whitespace(lbm_char_channel_t *chan) { | |
| 388 | |||
| 389 | 19616578 | bool cleaning_whitespace = true; | |
| 390 | char c; | ||
| 391 | int r; | ||
| 392 | |||
| 393 |
2/2✓ Branch 0 taken 19652824 times.
✓ Branch 1 taken 19545658 times.
|
39198482 | while (cleaning_whitespace) { |
| 394 | |||
| 395 |
2/2✓ Branch 1 taken 39017 times.
✓ Branch 2 taken 19613807 times.
|
19652824 | if (lbm_channel_comment(chan)) { |
| 396 | while (true) { | ||
| 397 | 2954898 | r = lbm_channel_peek(chan, 0, &c); | |
| 398 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2954898 times.
|
2954898 | if (r == CHANNEL_END) { |
| 399 | ✗ | lbm_channel_set_comment(chan, false); | |
| 400 | ✗ | cleaning_whitespace = false; | |
| 401 | ✗ | break; | |
| 402 | } | ||
| 403 |
2/2✓ Branch 0 taken 2804 times.
✓ Branch 1 taken 2952094 times.
|
2954898 | if (r == CHANNEL_MORE) { |
| 404 | 2804 | return false; | |
| 405 | } | ||
| 406 | 2952094 | lbm_channel_drop(chan,1); | |
| 407 |
2/2✓ Branch 0 taken 36213 times.
✓ Branch 1 taken 2915881 times.
|
2952094 | if (c == '\n') { |
| 408 | 36213 | lbm_channel_set_comment(chan, false); | |
| 409 | 36213 | break; | |
| 410 | } | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | do { | ||
| 415 | 35456514 | r = lbm_channel_peek(chan, 0, &c); | |
| 416 |
2/2✓ Branch 0 taken 2060 times.
✓ Branch 1 taken 35454454 times.
|
35456514 | if (r == CHANNEL_MORE) { |
| 417 | 2060 | return false; | |
| 418 |
2/2✓ Branch 0 taken 66056 times.
✓ Branch 1 taken 35388398 times.
|
35454454 | } else if (r == CHANNEL_END) { |
| 419 | 66056 | return true; | |
| 420 | } | ||
| 421 |
2/2✓ Branch 0 taken 36246 times.
✓ Branch 1 taken 35352152 times.
|
35388398 | if (c == ';') { |
| 422 | 36246 | lbm_channel_set_comment(chan, true); | |
| 423 | 36246 | break; | |
| 424 | } | ||
| 425 | #ifdef LBM_USE_SHEBANG_COMMENTS | ||
| 426 |
4/4✓ Branch 1 taken 302006 times.
✓ Branch 2 taken 3955672 times.
✓ Branch 3 taken 1206 times.
✓ Branch 4 taken 300800 times.
|
4559684 | else if ((lbm_channel_column(chan) == 1) && |
| 427 | 302006 | (lbm_channel_row(chan) == 1) && | |
| 428 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1206 times.
|
1206 | c == '#') { |
| 429 | // Accept #! as comment if it the very first | ||
| 430 | // row of text arriving on the channel | ||
| 431 | ✗ | r = lbm_channel_peek(chan, 1, &c); | |
| 432 | ✗ | if (r == CHANNEL_MORE) return false; | |
| 433 | ✗ | if (r == CHANNEL_END) return true; | |
| 434 | ✗ | if (c == '!') { | |
| 435 | ✗ | lbm_channel_set_comment(chan, true); | |
| 436 | ✗ | break; | |
| 437 | } | ||
| 438 | } | ||
| 439 | #endif | ||
| 440 |
2/2✓ Branch 0 taken 15806494 times.
✓ Branch 1 taken 19545658 times.
|
35352152 | if (isspace(c)) { |
| 441 | 15806494 | lbm_channel_drop(chan,1); | |
| 442 | } else { | ||
| 443 | 19545658 | cleaning_whitespace = false; | |
| 444 | } | ||
| 445 | |||
| 446 |
2/2✓ Branch 0 taken 15806494 times.
✓ Branch 1 taken 19545658 times.
|
35352152 | } while (cleaning_whitespace); |
| 447 | } | ||
| 448 | 19545658 | return true; | |
| 449 | } | ||
| 450 | |||
| 451 | 13959940 | int tok_integer(lbm_char_channel_t *chan, token_int *result) { | |
| 452 | 13959940 | uint64_t acc = 0; | |
| 453 | 13959940 | unsigned int n = 0; | |
| 454 | 13959940 | bool valid_num = false; | |
| 455 | char c; | ||
| 456 | int res; | ||
| 457 | |||
| 458 | 13959940 | result->type = TOKTYPEI; | |
| 459 | 13959940 | result-> negative = false; | |
| 460 | 13959940 | res = lbm_channel_peek(chan, 0, &c); | |
| 461 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 13959939 times.
|
13959940 | if (res == CHANNEL_MORE) { |
| 462 | 1 | return TOKENIZER_NEED_MORE; | |
| 463 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 13959938 times.
|
13959939 | } else if (res == CHANNEL_END) { |
| 464 | 1 | return TOKENIZER_NO_TOKEN; | |
| 465 | } | ||
| 466 |
2/2✓ Branch 0 taken 34836 times.
✓ Branch 1 taken 13925102 times.
|
13959938 | if (c == '-') { |
| 467 | 34836 | n = 1; | |
| 468 | 34836 | result->negative = true; | |
| 469 | } | ||
| 470 | |||
| 471 | 13959938 | bool hex = false; | |
| 472 | 13959938 | res = lbm_channel_peek(chan, n, &c); | |
| 473 |
3/4✓ Branch 0 taken 13959938 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 106384 times.
✓ Branch 3 taken 13853554 times.
|
13959938 | if (res == CHANNEL_SUCCESS && c == '0') { |
| 474 | 106384 | res = lbm_channel_peek(chan, n + 1, &c); | |
| 475 |
4/6✓ Branch 0 taken 106384 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 56047 times.
✓ Branch 3 taken 50337 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 56047 times.
|
106384 | if ( res == CHANNEL_SUCCESS && (c == 'x' || c == 'X')) { |
| 476 | 50337 | hex = true; | |
| 477 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56047 times.
|
56047 | } else if (res == CHANNEL_MORE) { |
| 478 | ✗ | return TOKENIZER_NEED_MORE; | |
| 479 | } | ||
| 480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13853554 times.
|
13853554 | } else if (res == CHANNEL_MORE) { |
| 481 | ✗ | return TOKENIZER_NEED_MORE; | |
| 482 | } | ||
| 483 | |||
| 484 |
2/2✓ Branch 0 taken 50337 times.
✓ Branch 1 taken 13909601 times.
|
13959938 | if (hex) { |
| 485 | 50337 | n += 2; | |
| 486 | |||
| 487 | 50337 | res = lbm_channel_peek(chan,n, &c); | |
| 488 | |||
| 489 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 50328 times.
|
50337 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 490 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50328 times.
|
50328 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 491 | |||
| 492 |
2/2✓ Branch 0 taken 85698 times.
✓ Branch 1 taken 33501 times.
|
169527 | while ((c >= '0' && c <= '9') || |
| 493 |
6/6✓ Branch 0 taken 119199 times.
✓ Branch 1 taken 49050 times.
✓ Branch 2 taken 1620 times.
✓ Branch 3 taken 80931 times.
✓ Branch 4 taken 833 times.
✓ Branch 5 taken 787 times.
|
201750 | (c >= 'a' && c <= 'f') || |
| 494 |
4/4✓ Branch 0 taken 32668 times.
✓ Branch 1 taken 49050 times.
✓ Branch 2 taken 31515 times.
✓ Branch 3 taken 1153 times.
|
81718 | (c >= 'A' && c <= 'F')) { |
| 495 | uint32_t val; /* values between 0 and 16 */ | ||
| 496 |
3/4✓ Branch 0 taken 833 times.
✓ Branch 1 taken 117213 times.
✓ Branch 2 taken 833 times.
✗ Branch 3 not taken.
|
118046 | if (c >= 'a' && c <= 'f') { |
| 497 | 833 | val = 10 + (uint32_t)c - 'a'; | |
| 498 |
3/4✓ Branch 0 taken 31515 times.
✓ Branch 1 taken 85698 times.
✓ Branch 2 taken 31515 times.
✗ Branch 3 not taken.
|
117213 | } else if (c >= 'A' && c <= 'F') { |
| 499 | 31515 | val = 10 + (uint32_t)(c - 'A'); | |
| 500 | } else { | ||
| 501 | 85698 | val = (uint32_t)c - '0'; | |
| 502 | } | ||
| 503 | 118046 | acc = (acc * 0x10) + val; | |
| 504 | 118046 | n++; | |
| 505 | 118046 | res = lbm_channel_peek(chan, n, &c); | |
| 506 |
2/2✓ Branch 0 taken 125 times.
✓ Branch 1 taken 117921 times.
|
118046 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 507 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 117921 times.
|
117921 | if (res == CHANNEL_END) break; |
| 508 | |||
| 509 | } | ||
| 510 | } else { | ||
| 511 | 13909601 | res = lbm_channel_peek(chan, n, &c); | |
| 512 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13909601 times.
|
13909601 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 513 |
4/4✓ Branch 0 taken 16538428 times.
✓ Branch 1 taken 10267057 times.
✓ Branch 2 taken 12896078 times.
✓ Branch 3 taken 3642350 times.
|
26805485 | while (c >= '0' && c <= '9') { |
| 514 | 12896078 | acc = (acc*10) + (uint32_t)(c - '0'); | |
| 515 | 12896078 | n++; | |
| 516 | 12896078 | res = lbm_channel_peek(chan, n, &c); | |
| 517 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12896078 times.
|
12896078 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 518 |
2/2✓ Branch 0 taken 194 times.
✓ Branch 1 taken 12895884 times.
|
12896078 | if (res == CHANNEL_END) break; |
| 519 | } | ||
| 520 | } | ||
| 521 | |||
| 522 |
5/6✓ Branch 0 taken 10275365 times.
✓ Branch 1 taken 3684439 times.
✓ Branch 2 taken 50203 times.
✓ Branch 3 taken 10225162 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 50203 times.
|
13959804 | if (n == 0 || (hex && n == 2)) return TOKENIZER_NO_TOKEN; |
| 523 | |||
| 524 | uint32_t tok_res; | ||
| 525 | 10275365 | int type_len = tok_match_fixed_size_tokens(chan, type_qual_table, n, NUM_TYPE_QUALIFIERS, &tok_res); | |
| 526 | |||
| 527 |
2/2✓ Branch 0 taken 61 times.
✓ Branch 1 taken 10275304 times.
|
10275365 | if (type_len == TOKENIZER_NEED_MORE) return type_len; |
| 528 |
2/2✓ Branch 0 taken 64947 times.
✓ Branch 1 taken 10210357 times.
|
10275304 | if (type_len != TOKENIZER_NO_TOKEN) { |
| 529 | 64947 | result->type = tok_res; | |
| 530 | } | ||
| 531 | |||
| 532 |
4/4✓ Branch 0 taken 34836 times.
✓ Branch 1 taken 10240468 times.
✓ Branch 2 taken 29162 times.
✓ Branch 3 taken 5674 times.
|
10275304 | if ((result->negative && n > 1) || |
| 533 |
2/2✓ Branch 0 taken 10240468 times.
✓ Branch 1 taken 29162 times.
|
10275304 | !result->negative) valid_num = true; |
| 534 | |||
| 535 |
2/2✓ Branch 0 taken 10246142 times.
✓ Branch 1 taken 29162 times.
|
10275304 | if (valid_num) { |
| 536 | 10246142 | result->value = acc; | |
| 537 | 10246142 | return (int)n + type_len; | |
| 538 | } | ||
| 539 | 29162 | return TOKENIZER_NO_TOKEN; | |
| 540 | } | ||
| 541 |