| 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 | 3738 | static inline signed char translate_escape_char(char c) { | |
| 57 |
14/14✓ Branch 0 taken 252 times.
✓ Branch 1 taken 168 times.
✓ Branch 2 taken 252 times.
✓ Branch 3 taken 171 times.
✓ Branch 4 taken 432 times.
✓ Branch 5 taken 168 times.
✓ Branch 6 taken 168 times.
✓ Branch 7 taken 171 times.
✓ Branch 8 taken 168 times.
✓ Branch 9 taken 168 times.
✓ Branch 10 taken 858 times.
✓ Branch 11 taken 423 times.
✓ Branch 12 taken 168 times.
✓ Branch 13 taken 171 times.
|
3738 | switch (c) { |
| 58 | 252 | case '0': return '\0'; | |
| 59 | 168 | case 'a': return '\a'; | |
| 60 | 252 | case 'b': return '\b'; | |
| 61 | 171 | case 't': return '\t'; | |
| 62 | 432 | case 'n': return '\n'; | |
| 63 | 168 | case 'v': return '\v'; | |
| 64 | 168 | case 'f': return '\f'; | |
| 65 | 171 | case 'r': return '\r'; | |
| 66 | 168 | case 'e': return 27; | |
| 67 | 168 | case 's': return 32; | |
| 68 | 858 | case '"': return '\"'; | |
| 69 | 423 | case '\\': return '\\'; | |
| 70 | 168 | case 'd': return 127; | |
| 71 | 171 | 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 | 29844711 | 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 355012067 times.
✓ Branch 1 taken 10226919 times.
|
365238986 | for (unsigned int i = 0; i < num; i ++) { |
| 112 | 355012067 | uint32_t tok_len = m[i].len; | |
| 113 | 355012067 | const char *match_str = m[i].str; | |
| 114 | char c; | ||
| 115 | int char_pos; | ||
| 116 |
2/2✓ Branch 0 taken 341230866 times.
✓ Branch 1 taken 19617753 times.
|
360848619 | for (char_pos = 0; char_pos < (int)tok_len; char_pos ++) { |
| 117 | 341230866 | int r = lbm_channel_peek(chan,(unsigned int)char_pos + start_pos, &c); | |
| 118 |
2/2✓ Branch 0 taken 341228901 times.
✓ Branch 1 taken 1965 times.
|
341230866 | if (r == CHANNEL_SUCCESS) { |
| 119 |
2/2✓ Branch 0 taken 335392349 times.
✓ Branch 1 taken 5836552 times.
|
341228901 | if (c != match_str[char_pos]) break; |
| 120 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 1926 times.
|
1965 | } else if (r == CHANNEL_MORE ) { |
| 121 | 19617792 | return TOKENIZER_NEED_MORE; | |
| 122 | } else { | ||
| 123 | 1926 | break; | |
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 |
2/2✓ Branch 0 taken 19617753 times.
✓ Branch 1 taken 335394275 times.
|
355012028 | if (char_pos == (int)tok_len) { //match |
| 128 | 19617753 | *res = m[i].token; | |
| 129 | 19617753 | return (int)tok_len; | |
| 130 | } | ||
| 131 | } | ||
| 132 | 10226919 | return TOKENIZER_NO_TOKEN; | |
| 133 | } | ||
| 134 | |||
| 135 | 19534419 | int tok_syntax(lbm_char_channel_t *chan, uint32_t *res) { | |
| 136 | 19534419 | return tok_match_fixed_size_tokens(chan, fixed_size_tokens, 0, NUM_FIXED_SIZE_TOKENS, res); | |
| 137 | } | ||
| 138 | |||
| 139 | 16338400 | static bool alpha_char(char c) { | |
| 140 |
6/6✓ Branch 0 taken 11601625 times.
✓ Branch 1 taken 4736775 times.
✓ Branch 2 taken 253 times.
✓ Branch 3 taken 11601372 times.
✓ Branch 4 taken 30960 times.
✓ Branch 5 taken 4706068 times.
|
16369360 | return ((c >= 'a' && c <= 'z') || |
| 141 |
2/2✓ Branch 0 taken 3567 times.
✓ Branch 1 taken 27393 times.
|
30960 | (c >= 'A' && c <= 'Z')); |
| 142 | } | ||
| 143 | |||
| 144 | 4493496 | static bool num_char(char c) { | |
| 145 |
4/4✓ Branch 0 taken 464540 times.
✓ Branch 1 taken 4028956 times.
✓ Branch 2 taken 427224 times.
✓ Branch 3 taken 37316 times.
|
4493496 | return (c >= '0' && c <= '9'); |
| 146 | } | ||
| 147 | |||
| 148 | 3709407 | static bool symchar0(char c) { | |
| 149 | 3709407 | const char *allowed = "+-*/=<>#!"; | |
| 150 | |||
| 151 |
2/2✓ Branch 0 taken 3469442 times.
✓ Branch 1 taken 239965 times.
|
3709407 | if (alpha_char(c)) return true; |
| 152 | 239965 | int i = 0; | |
| 153 |
2/2✓ Branch 0 taken 767312 times.
✓ Branch 1 taken 2133 times.
|
769445 | while (allowed[i] != 0) { |
| 154 |
2/2✓ Branch 0 taken 237832 times.
✓ Branch 1 taken 529480 times.
|
767312 | if (c == allowed[i]) return true; |
| 155 | 529480 | i ++; | |
| 156 | } | ||
| 157 | 2133 | return false; | |
| 158 | } | ||
| 159 | |||
| 160 | 12628993 | static bool symchar(char c) { | |
| 161 | 12628993 | const char *allowed = "+-*/=<>!?_"; | |
| 162 | |||
| 163 |
4/4✓ Branch 0 taken 4493496 times.
✓ Branch 1 taken 8135497 times.
✓ Branch 2 taken 427224 times.
✓ Branch 3 taken 4066272 times.
|
12628993 | if (alpha_char(c) || num_char(c)) return true; |
| 164 | 4066272 | int i = 0; | |
| 165 |
2/2✓ Branch 0 taken 37696159 times.
✓ Branch 1 taken 3665080 times.
|
41361239 | while (allowed[i] != 0) { |
| 166 |
2/2✓ Branch 0 taken 401192 times.
✓ Branch 1 taken 37294967 times.
|
37696159 | if (c == allowed[i]) return true; |
| 167 | 37294967 | i++; | |
| 168 | } | ||
| 169 | 3665080 | return false; | |
| 170 | } | ||
| 171 | |||
| 172 | 3709407 | int tok_symbol(lbm_char_channel_t *chan) { | |
| 173 | |||
| 174 | char c; | ||
| 175 | 3709407 | int r = 0; | |
| 176 | |||
| 177 | 3709407 | r = lbm_channel_peek(chan, 0, &c); | |
| 178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3709407 times.
|
3709407 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3709407 times.
|
3709407 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 180 |
3/4✓ Branch 0 taken 3709407 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2133 times.
✓ Branch 3 taken 3707274 times.
|
3709407 | if (r == CHANNEL_SUCCESS && !symchar0(c)) { |
| 181 | 2133 | return TOKENIZER_NO_TOKEN; | |
| 182 | } | ||
| 183 | 3707274 | memset(tokpar_sym_str,0,TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH+1); | |
| 184 |
4/4✓ Branch 0 taken 3469442 times.
✓ Branch 1 taken 237832 times.
✓ Branch 2 taken 534 times.
✓ Branch 3 taken 3468908 times.
|
3707274 | tokpar_sym_str[0] = (c >= 'A' && c <= 'Z') ? c + 32 : c; // locale independent ASCII only tolower. |
| 185 | |||
| 186 | 3707274 | int len = 1; | |
| 187 | |||
| 188 | 3707274 | r = lbm_channel_peek(chan,(unsigned int)len, &c); | |
| 189 |
4/4✓ Branch 0 taken 12628993 times.
✓ Branch 1 taken 42191 times.
✓ Branch 2 taken 8963913 times.
✓ Branch 3 taken 3665080 times.
|
12671184 | while (r == CHANNEL_SUCCESS && symchar(c)) { |
| 190 |
4/4✓ Branch 0 taken 8160510 times.
✓ Branch 1 taken 803403 times.
✓ Branch 2 taken 3033 times.
✓ Branch 3 taken 8157477 times.
|
8963913 | c = (c >= 'A' && c <= 'Z') ? c + 32 : c; // locale independent ASCII only tolower. |
| 191 |
2/2✓ Branch 0 taken 8963910 times.
✓ Branch 1 taken 3 times.
|
8963913 | if (len < TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH) { |
| 192 | 8963910 | tokpar_sym_str[len] = (char)c; | |
| 193 | } else { | ||
| 194 | 3 | return TOKENIZER_SYMBOL_ERROR; | |
| 195 | } | ||
| 196 | 8963910 | len ++; | |
| 197 | 8963910 | r = lbm_channel_peek(chan,(unsigned int)len, &c); | |
| 198 | } | ||
| 199 |
2/2✓ Branch 0 taken 1115 times.
✓ Branch 1 taken 3706156 times.
|
3707271 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 200 | 3706156 | tokpar_sym_str[len] = 0; | |
| 201 | 3706156 | return len; | |
| 202 | } | ||
| 203 | |||
| 204 | 14025437 | int tok_string(lbm_char_channel_t *chan, unsigned int *string_len) { | |
| 205 | |||
| 206 | 14025437 | unsigned int n = 0; | |
| 207 | 14025437 | unsigned int len = 0; | |
| 208 | char c; | ||
| 209 | 14025437 | int r = 0; | |
| 210 | 14025437 | bool encode = false; | |
| 211 | |||
| 212 | 14025437 | r = lbm_channel_peek(chan,0,&c); | |
| 213 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14025437 times.
|
14025437 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 214 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14025437 times.
|
14025437 | else if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 215 | |||
| 216 |
2/2✓ Branch 0 taken 13990938 times.
✓ Branch 1 taken 34499 times.
|
14025437 | if (c != '\"') return TOKENIZER_NO_TOKEN;; |
| 217 | 34499 | n++; | |
| 218 | |||
| 219 | 34499 | memset(tokpar_sym_str,0,TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH+1); | |
| 220 | |||
| 221 | // read string into buffer | ||
| 222 | 34499 | r = lbm_channel_peek(chan,n,&c); | |
| 223 |
7/8✓ Branch 0 taken 246034 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 35159 times.
✓ Branch 3 taken 210875 times.
✓ Branch 4 taken 774 times.
✓ Branch 5 taken 34385 times.
✓ Branch 6 taken 211649 times.
✗ Branch 7 not taken.
|
246061 | while (r == CHANNEL_SUCCESS && (c != '\"' || encode) && |
| 224 | len < TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH) { | ||
| 225 |
4/4✓ Branch 0 taken 2731 times.
✓ Branch 1 taken 208918 times.
✓ Branch 2 taken 2392 times.
✓ Branch 3 taken 339 times.
|
211649 | if (c == '\\' && !encode) { |
| 226 | 2392 | encode = true; | |
| 227 | } else { | ||
| 228 |
2/2✓ Branch 0 taken 2392 times.
✓ Branch 1 taken 206865 times.
|
209257 | if (encode) { |
| 229 | 2392 | signed char result = translate_escape_char(c); | |
| 230 |
2/2✓ Branch 0 taken 87 times.
✓ Branch 1 taken 2305 times.
|
2392 | if (result == -1) { |
| 231 | 87 | return TOKENIZER_STRING_ERROR; | |
| 232 | } else { | ||
| 233 | 2305 | tokpar_sym_str[len] = result; | |
| 234 | } | ||
| 235 | } else { | ||
| 236 | 206865 | tokpar_sym_str[len] = c; | |
| 237 | } | ||
| 238 | 209170 | len++; | |
| 239 | 209170 | encode = false; | |
| 240 | } | ||
| 241 | 211562 | n ++; | |
| 242 | 211562 | r = lbm_channel_peek(chan, n, &c); | |
| 243 | } | ||
| 244 | |||
| 245 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 34394 times.
|
34412 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 246 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 34385 times.
|
34394 | if (c != '\"') return TOKENIZER_STRING_ERROR; |
| 247 | |||
| 248 | 34385 | *string_len = len; | |
| 249 | 34385 | n ++; | |
| 250 | 34385 | return (int)n; | |
| 251 | } | ||
| 252 | |||
| 253 | 2133 | int tok_char(lbm_char_channel_t *chan, char *res) { | |
| 254 | |||
| 255 | char c; | ||
| 256 | int r; | ||
| 257 | |||
| 258 | 2133 | r = lbm_channel_peek(chan, 0, &c); | |
| 259 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2133 times.
|
2133 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 260 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2133 times.
|
2133 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 261 | |||
| 262 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2127 times.
|
2133 | if (c != '\\') return TOKENIZER_NO_TOKEN; |
| 263 | |||
| 264 | 2127 | r = lbm_channel_peek(chan, 1, &c); | |
| 265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2127 times.
|
2127 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 266 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2127 times.
|
2127 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 267 | |||
| 268 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2127 times.
|
2127 | if (c != '#') return TOKENIZER_NO_TOKEN; |
| 269 | |||
| 270 | 2127 | r = lbm_channel_peek(chan, 2, &c); | |
| 271 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2127 times.
|
2127 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 272 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2127 times.
|
2127 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 273 | |||
| 274 |
2/2✓ Branch 0 taken 1346 times.
✓ Branch 1 taken 781 times.
|
2127 | if (c == '\\') { |
| 275 | 1346 | r = lbm_channel_peek(chan, 3, &c); | |
| 276 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
|
1346 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 277 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
|
1346 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 278 | |||
| 279 | 1346 | signed char result = translate_escape_char(c); | |
| 280 |
2/2✓ Branch 0 taken 1262 times.
✓ Branch 1 taken 84 times.
|
1346 | if (result != -1) { |
| 281 | 1262 | *res = result; | |
| 282 | 1262 | return 4; | |
| 283 | } else { | ||
| 284 | 84 | return TOKENIZER_CHAR_ERROR; | |
| 285 | } | ||
| 286 | } | ||
| 287 | 781 | *res = c; | |
| 288 | 781 | 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 | 13990938 | int tok_double(lbm_char_channel_t *chan, token_float *result) { | |
| 296 | |||
| 297 | 13990938 | unsigned int n = 0; | |
| 298 | char fbuf[TD_BUF_SIZE]; | ||
| 299 | char c; | ||
| 300 | 13990938 | bool valid_num = false; | |
| 301 | int res; | ||
| 302 | int type_len; | ||
| 303 | uint32_t tok_res; | ||
| 304 | |||
| 305 | 13990938 | memset(fbuf, 0, TD_BUF_SIZE); | |
| 306 | |||
| 307 | 13990938 | result->type = TOKTYPEF32; | |
| 308 | 13990938 | result->negative = false; | |
| 309 | |||
| 310 | 13990938 | res = lbm_channel_peek(chan, n, &c); | |
| 311 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13990938 times.
|
13990938 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 312 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13990938 times.
|
13990938 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 313 |
2/2✓ Branch 0 taken 35572 times.
✓ Branch 1 taken 13955366 times.
|
13990938 | if (c == '-') { |
| 314 |
1/2✓ Branch 0 taken 35572 times.
✗ Branch 1 not taken.
|
35572 | FBUF_ADD('-', n); |
| 315 | 35572 | result->negative = true; | |
| 316 | } | ||
| 317 | |||
| 318 | 13990938 | res = lbm_channel_peek(chan, n, &c); | |
| 319 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 13990924 times.
|
13990938 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 320 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13990924 times.
|
13990924 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 321 |
4/4✓ Branch 0 taken 16670823 times.
✓ Branch 1 taken 10302493 times.
✓ Branch 2 taken 12982930 times.
✓ Branch 3 taken 3687893 times.
|
26973316 | while (c >= '0' && c <= '9') { |
| 322 |
1/2✓ Branch 0 taken 12982930 times.
✗ Branch 1 not taken.
|
12982930 | FBUF_ADD(c, n); |
| 323 | 12982930 | res = lbm_channel_peek(chan, n, &c); | |
| 324 |
2/2✓ Branch 0 taken 345 times.
✓ Branch 1 taken 12982585 times.
|
12982930 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 325 |
2/2✓ Branch 0 taken 193 times.
✓ Branch 1 taken 12982392 times.
|
12982585 | if (res == CHANNEL_END) break; |
| 326 | } | ||
| 327 | |||
| 328 |
2/2✓ Branch 0 taken 36844 times.
✓ Branch 1 taken 13953735 times.
|
13990579 | if (c == '.') { |
| 329 |
1/2✓ Branch 0 taken 36844 times.
✗ Branch 1 not taken.
|
36844 | FBUF_ADD(c, n); |
| 330 | } | ||
| 331 | 13953735 | else return TOKENIZER_NO_TOKEN; | |
| 332 | |||
| 333 | 36844 | res = lbm_channel_peek(chan,n, &c); | |
| 334 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36844 times.
|
36844 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 335 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36844 times.
|
36844 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 336 |
2/4✓ Branch 0 taken 36844 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36844 times.
|
36844 | if (!(c >= '0' && c <= '9')) return TOKENIZER_NO_TOKEN; |
| 337 | |||
| 338 |
4/4✓ Branch 0 taken 70881 times.
✓ Branch 1 taken 18287 times.
✓ Branch 2 taken 52326 times.
✓ Branch 3 taken 18555 times.
|
89168 | while (c >= '0' && c <= '9') { |
| 339 |
1/2✓ Branch 0 taken 52326 times.
✗ Branch 1 not taken.
|
52326 | FBUF_ADD(c, n); |
| 340 | 52326 | res = lbm_channel_peek(chan, n, &c); | |
| 341 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 52324 times.
|
52326 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 342 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 52324 times.
|
52324 | if (res == CHANNEL_END) break; |
| 343 | } | ||
| 344 | |||
| 345 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 36506 times.
|
36842 | 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 | 36842 | type_len = tok_match_fixed_size_tokens(chan, type_qual_table, n, NUM_TYPE_QUALIFIERS, &tok_res); | |
| 367 | |||
| 368 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36842 times.
|
36842 | if (type_len == TOKENIZER_NEED_MORE) return type_len; |
| 369 |
2/2✓ Branch 0 taken 18455 times.
✓ Branch 1 taken 18387 times.
|
36842 | if (type_len == TOKENIZER_NO_TOKEN) { |
| 370 | 18455 | result->type = TOKTYPEF32; | |
| 371 | } else { | ||
| 372 | 18387 | result->type = tok_res; | |
| 373 | } | ||
| 374 | |||
| 375 |
3/4✓ Branch 0 taken 820 times.
✓ Branch 1 taken 36022 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 820 times.
|
36842 | if ((result->negative && n > 1) || |
| 376 |
2/4✓ Branch 0 taken 36022 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36022 times.
✗ Branch 3 not taken.
|
36842 | (!result->negative && n > 0)) valid_num = true; |
| 377 | |||
| 378 |
1/2✓ Branch 0 taken 36842 times.
✗ Branch 1 not taken.
|
36842 | if(valid_num) { |
| 379 | 36842 | result->value = (double)strtod(fbuf,NULL); | |
| 380 | 36842 | return (int)n + type_len; | |
| 381 | } | ||
| 382 | |||
| 383 | ✗ | tok_double_no_tok: | |
| 384 | ✗ | return TOKENIZER_NO_TOKEN; | |
| 385 | } | ||
| 386 | |||
| 387 | 19604361 | bool tok_clean_whitespace(lbm_char_channel_t *chan) { | |
| 388 | |||
| 389 | 19604361 | bool cleaning_whitespace = true; | |
| 390 | char c; | ||
| 391 | int r; | ||
| 392 | |||
| 393 |
2/2✓ Branch 0 taken 19640062 times.
✓ Branch 1 taken 19534419 times.
|
39174481 | while (cleaning_whitespace) { |
| 394 | |||
| 395 |
2/2✓ Branch 0 taken 38058 times.
✓ Branch 1 taken 19602004 times.
|
19640062 | if (lbm_channel_comment(chan)) { |
| 396 | while (true) { | ||
| 397 | 2925025 | r = lbm_channel_peek(chan, 0, &c); | |
| 398 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2925025 times.
|
2925025 | if (r == CHANNEL_END) { |
| 399 | ✗ | lbm_channel_set_comment(chan, false); | |
| 400 | ✗ | cleaning_whitespace = false; | |
| 401 | ✗ | break; | |
| 402 | } | ||
| 403 |
2/2✓ Branch 0 taken 2373 times.
✓ Branch 1 taken 2922652 times.
|
2925025 | if (r == CHANNEL_MORE) { |
| 404 | 2373 | return false; | |
| 405 | } | ||
| 406 | 2922652 | lbm_channel_drop(chan,1); | |
| 407 |
2/2✓ Branch 0 taken 35685 times.
✓ Branch 1 taken 2886967 times.
|
2922652 | if (c == '\n') { |
| 408 | 35685 | lbm_channel_set_comment(chan, false); | |
| 409 | 35685 | break; | |
| 410 | } | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | do { | ||
| 415 | 35433496 | r = lbm_channel_peek(chan, 0, &c); | |
| 416 |
2/2✓ Branch 0 taken 1524 times.
✓ Branch 1 taken 35431972 times.
|
35433496 | if (r == CHANNEL_MORE) { |
| 417 | 1524 | return false; | |
| 418 |
2/2✓ Branch 0 taken 66045 times.
✓ Branch 1 taken 35365927 times.
|
35431972 | } else if (r == CHANNEL_END) { |
| 419 | 66045 | return true; | |
| 420 | } | ||
| 421 |
2/2✓ Branch 0 taken 35701 times.
✓ Branch 1 taken 35330226 times.
|
35365927 | if (c == ';') { |
| 422 | 35701 | lbm_channel_set_comment(chan, true); | |
| 423 | 35701 | break; | |
| 424 | } | ||
| 425 | #ifdef LBM_USE_SHEBANG_COMMENTS | ||
| 426 |
4/4✓ Branch 0 taken 300595 times.
✓ Branch 1 taken 3935770 times.
✓ Branch 2 taken 1152 times.
✓ Branch 3 taken 299443 times.
|
4536960 | else if ((lbm_channel_column(chan) == 1) && |
| 427 | 300595 | (lbm_channel_row(chan) == 1) && | |
| 428 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1152 times.
|
1152 | 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 15795807 times.
✓ Branch 1 taken 19534419 times.
|
35330226 | if (isspace(c)) { |
| 441 | 15795807 | lbm_channel_drop(chan,1); | |
| 442 | } else { | ||
| 443 | 19534419 | cleaning_whitespace = false; | |
| 444 | } | ||
| 445 | |||
| 446 |
2/2✓ Branch 0 taken 15795807 times.
✓ Branch 1 taken 19534419 times.
|
35330226 | } while (cleaning_whitespace); |
| 447 | } | ||
| 448 | 19534419 | return true; | |
| 449 | } | ||
| 450 | |||
| 451 | 13953735 | int tok_integer(lbm_char_channel_t *chan, token_int *result) { | |
| 452 | 13953735 | uint64_t acc = 0; | |
| 453 | 13953735 | unsigned int n = 0; | |
| 454 | 13953735 | bool valid_num = false; | |
| 455 | char c; | ||
| 456 | int res; | ||
| 457 | |||
| 458 | 13953735 | result->type = TOKTYPEI; | |
| 459 | 13953735 | result-> negative = false; | |
| 460 | 13953735 | res = lbm_channel_peek(chan, 0, &c); | |
| 461 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13953735 times.
|
13953735 | if (res == CHANNEL_MORE) { |
| 462 | ✗ | return TOKENIZER_NEED_MORE; | |
| 463 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13953735 times.
|
13953735 | } else if (res == CHANNEL_END) { |
| 464 | ✗ | return TOKENIZER_NO_TOKEN; | |
| 465 | } | ||
| 466 |
2/2✓ Branch 0 taken 34738 times.
✓ Branch 1 taken 13918997 times.
|
13953735 | if (c == '-') { |
| 467 | 34738 | n = 1; | |
| 468 | 34738 | result->negative = true; | |
| 469 | } | ||
| 470 | |||
| 471 | 13953735 | bool hex = false; | |
| 472 | 13953735 | res = lbm_channel_peek(chan, n, &c); | |
| 473 |
3/4✓ Branch 0 taken 13953735 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 105440 times.
✓ Branch 3 taken 13848295 times.
|
13953735 | if (res == CHANNEL_SUCCESS && c == '0') { |
| 474 | 105440 | res = lbm_channel_peek(chan, n + 1, &c); | |
| 475 |
4/6✓ Branch 0 taken 105440 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55730 times.
✓ Branch 3 taken 49710 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 55730 times.
|
105440 | if ( res == CHANNEL_SUCCESS && (c == 'x' || c == 'X')) { |
| 476 | 49710 | hex = true; | |
| 477 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 55730 times.
|
55730 | } else if (res == CHANNEL_MORE) { |
| 478 | ✗ | return TOKENIZER_NEED_MORE; | |
| 479 | } | ||
| 480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13848295 times.
|
13848295 | } else if (res == CHANNEL_MORE) { |
| 481 | ✗ | return TOKENIZER_NEED_MORE; | |
| 482 | } | ||
| 483 | |||
| 484 |
2/2✓ Branch 0 taken 49710 times.
✓ Branch 1 taken 13904025 times.
|
13953735 | if (hex) { |
| 485 | 49710 | n += 2; | |
| 486 | |||
| 487 | 49710 | res = lbm_channel_peek(chan,n, &c); | |
| 488 | |||
| 489 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 49703 times.
|
49710 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 490 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49703 times.
|
49703 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 491 | |||
| 492 |
2/2✓ Branch 0 taken 83445 times.
✓ Branch 1 taken 32534 times.
|
165682 | while ((c >= '0' && c <= '9') || |
| 493 |
6/6✓ Branch 0 taken 115979 times.
✓ Branch 1 taken 48536 times.
✓ Branch 2 taken 1610 times.
✓ Branch 3 taken 79460 times.
✓ Branch 4 taken 823 times.
✓ Branch 5 taken 787 times.
|
197049 | (c >= 'a' && c <= 'f') || |
| 494 |
4/4✓ Branch 0 taken 31711 times.
✓ Branch 1 taken 48536 times.
✓ Branch 2 taken 30558 times.
✓ Branch 3 taken 1153 times.
|
80247 | (c >= 'A' && c <= 'F')) { |
| 495 | uint32_t val; /* values between 0 and 16 */ | ||
| 496 |
3/4✓ Branch 0 taken 823 times.
✓ Branch 1 taken 114003 times.
✓ Branch 2 taken 823 times.
✗ Branch 3 not taken.
|
114826 | if (c >= 'a' && c <= 'f') { |
| 497 | 823 | val = 10 + (uint32_t)c - 'a'; | |
| 498 |
3/4✓ Branch 0 taken 30558 times.
✓ Branch 1 taken 83445 times.
✓ Branch 2 taken 30558 times.
✗ Branch 3 not taken.
|
114003 | } else if (c >= 'A' && c <= 'F') { |
| 499 | 30558 | val = 10 + (uint32_t)(c - 'A'); | |
| 500 | } else { | ||
| 501 | 83445 | val = (uint32_t)c - '0'; | |
| 502 | } | ||
| 503 | 114826 | acc = (acc * 0x10) + val; | |
| 504 | 114826 | n++; | |
| 505 | 114826 | res = lbm_channel_peek(chan, n, &c); | |
| 506 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 114812 times.
|
114826 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 507 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 114812 times.
|
114812 | if (res == CHANNEL_END) break; |
| 508 | |||
| 509 | } | ||
| 510 | } else { | ||
| 511 | 13904025 | res = lbm_channel_peek(chan, n, &c); | |
| 512 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13904025 times.
|
13904025 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 513 |
4/4✓ Branch 0 taken 16531723 times.
✓ Branch 1 taken 10265649 times.
✓ Branch 2 taken 12893540 times.
✓ Branch 3 taken 3638183 times.
|
26797372 | while (c >= '0' && c <= '9') { |
| 514 | 12893540 | acc = (acc*10) + (uint32_t)(c - '0'); | |
| 515 | 12893540 | n++; | |
| 516 | 12893540 | res = lbm_channel_peek(chan, n, &c); | |
| 517 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12893540 times.
|
12893540 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 518 |
2/2✓ Branch 0 taken 193 times.
✓ Branch 1 taken 12893347 times.
|
12893540 | if (res == CHANNEL_END) break; |
| 519 | } | ||
| 520 | } | ||
| 521 | |||
| 522 |
5/6✓ Branch 0 taken 10273450 times.
✓ Branch 1 taken 3680264 times.
✓ Branch 2 taken 49689 times.
✓ Branch 3 taken 10223761 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 49689 times.
|
13953714 | if (n == 0 || (hex && n == 2)) return TOKENIZER_NO_TOKEN; |
| 523 | |||
| 524 | uint32_t tok_res; | ||
| 525 | 10273450 | 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 39 times.
✓ Branch 1 taken 10273411 times.
|
10273450 | if (type_len == TOKENIZER_NEED_MORE) return type_len; |
| 528 |
2/2✓ Branch 0 taken 64947 times.
✓ Branch 1 taken 10208464 times.
|
10273411 | if (type_len != TOKENIZER_NO_TOKEN) { |
| 529 | 64947 | result->type = tok_res; | |
| 530 | } | ||
| 531 | |||
| 532 |
4/4✓ Branch 0 taken 34738 times.
✓ Branch 1 taken 10238673 times.
✓ Branch 2 taken 29143 times.
✓ Branch 3 taken 5595 times.
|
10273411 | if ((result->negative && n > 1) || |
| 533 |
2/2✓ Branch 0 taken 10238673 times.
✓ Branch 1 taken 29143 times.
|
10273411 | !result->negative) valid_num = true; |
| 534 | |||
| 535 |
2/2✓ Branch 0 taken 10244268 times.
✓ Branch 1 taken 29143 times.
|
10273411 | if (valid_num) { |
| 536 | 10244268 | result->value = acc; | |
| 537 | 10244268 | return (int)n + type_len; | |
| 538 | } | ||
| 539 | 29143 | return TOKENIZER_NO_TOKEN; | |
| 540 | } | ||
| 541 |