| 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 | 1642 | static inline signed char translate_escape_char(char c) { | |
| 57 |
14/14✓ Branch 0 taken 109 times.
✓ Branch 1 taken 73 times.
✓ Branch 2 taken 109 times.
✓ Branch 3 taken 76 times.
✓ Branch 4 taken 193 times.
✓ Branch 5 taken 73 times.
✓ Branch 6 taken 73 times.
✓ Branch 7 taken 76 times.
✓ Branch 8 taken 73 times.
✓ Branch 9 taken 73 times.
✓ Branch 10 taken 381 times.
✓ Branch 11 taken 184 times.
✓ Branch 12 taken 73 times.
✓ Branch 13 taken 76 times.
|
1642 | switch (c) { |
| 58 | 109 | case '0': return '\0'; | |
| 59 | 73 | case 'a': return '\a'; | |
| 60 | 109 | case 'b': return '\b'; | |
| 61 | 76 | case 't': return '\t'; | |
| 62 | 193 | case 'n': return '\n'; | |
| 63 | 73 | case 'v': return '\v'; | |
| 64 | 73 | case 'f': return '\f'; | |
| 65 | 76 | case 'r': return '\r'; | |
| 66 | 73 | case 'e': return 27; | |
| 67 | 73 | case 's': return 32; | |
| 68 | 381 | case '"': return '\"'; | |
| 69 | 184 | case '\\': return '\\'; | |
| 70 | 73 | case 'd': return 127; | |
| 71 | 76 | 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 | 14198153 | 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 165694778 times.
✓ Branch 1 taken 4483219 times.
|
170177997 | for (unsigned int i = 0; i < num; i ++) { |
| 112 | 165694778 | uint32_t tok_len = m[i].len; | |
| 113 | 165694778 | const char *match_str = m[i].str; | |
| 114 | char c; | ||
| 115 | int char_pos; | ||
| 116 |
2/2✓ Branch 0 taken 159148812 times.
✓ Branch 1 taken 9714910 times.
|
168863722 | for (char_pos = 0; char_pos < (int)tok_len; char_pos ++) { |
| 117 | 159148812 | int r = lbm_channel_peek(chan,(unsigned int)char_pos + start_pos, &c); | |
| 118 |
2/2✓ Branch 0 taken 159147787 times.
✓ Branch 1 taken 1025 times.
|
159148812 | if (r == CHANNEL_SUCCESS) { |
| 119 |
2/2✓ Branch 0 taken 155978843 times.
✓ Branch 1 taken 3168944 times.
|
159147787 | if (c != match_str[char_pos]) break; |
| 120 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1001 times.
|
1025 | } else if (r == CHANNEL_MORE ) { |
| 121 | 9714934 | return TOKENIZER_NEED_MORE; | |
| 122 | } else { | ||
| 123 | 1001 | break; | |
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 |
2/2✓ Branch 0 taken 9714910 times.
✓ Branch 1 taken 155979844 times.
|
165694754 | if (char_pos == (int)tok_len) { //match |
| 128 | 9714910 | *res = m[i].token; | |
| 129 | 9714910 | return (int)tok_len; | |
| 130 | } | ||
| 131 | } | ||
| 132 | 4483219 | return TOKENIZER_NO_TOKEN; | |
| 133 | } | ||
| 134 | |||
| 135 | 9678056 | int tok_syntax(lbm_char_channel_t *chan, uint32_t *res) { | |
| 136 | 9678056 | return tok_match_fixed_size_tokens(chan, fixed_size_tokens, 0, NUM_FIXED_SIZE_TOKENS, res); | |
| 137 | } | ||
| 138 | |||
| 139 | 10206160 | static bool alpha_char(char c) { | |
| 140 |
6/6✓ Branch 0 taken 7456585 times.
✓ Branch 1 taken 2749575 times.
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 7456477 times.
✓ Branch 4 taken 18049 times.
✓ Branch 5 taken 2731634 times.
|
10224209 | return ((c >= 'a' && c <= 'z') || |
| 141 |
2/2✓ Branch 0 taken 1697 times.
✓ Branch 1 taken 16352 times.
|
18049 | (c >= 'A' && c <= 'Z')); |
| 142 | } | ||
| 143 | |||
| 144 | 2606913 | static bool num_char(char c) { | |
| 145 |
4/4✓ Branch 0 taken 252403 times.
✓ Branch 1 taken 2354510 times.
✓ Branch 2 taken 227870 times.
✓ Branch 3 taken 24533 times.
|
2606913 | return (c >= '0' && c <= '9'); |
| 146 | } | ||
| 147 | |||
| 148 | 2139886 | static bool symchar0(char c) { | |
| 149 | 2139886 | const char *allowed = "+-*/=<>#!"; | |
| 150 | |||
| 151 |
2/2✓ Branch 1 taken 1998813 times.
✓ Branch 2 taken 141073 times.
|
2139886 | if (alpha_char(c)) return true; |
| 152 | 141073 | int i = 0; | |
| 153 |
2/2✓ Branch 0 taken 450515 times.
✓ Branch 1 taken 934 times.
|
451449 | while (allowed[i] != 0) { |
| 154 |
2/2✓ Branch 0 taken 140139 times.
✓ Branch 1 taken 310376 times.
|
450515 | if (c == allowed[i]) return true; |
| 155 | 310376 | i ++; | |
| 156 | } | ||
| 157 | 934 | return false; | |
| 158 | } | ||
| 159 | |||
| 160 | 8066274 | static bool symchar(char c) { | |
| 161 | 8066274 | const char *allowed = "+-*/=<>!?_"; | |
| 162 | |||
| 163 |
4/4✓ Branch 1 taken 2606913 times.
✓ Branch 2 taken 5459361 times.
✓ Branch 4 taken 227870 times.
✓ Branch 5 taken 2379043 times.
|
8066274 | if (alpha_char(c) || num_char(c)) return true; |
| 164 | 2379043 | int i = 0; | |
| 165 |
2/2✓ Branch 0 taken 21850067 times.
✓ Branch 1 taken 2117306 times.
|
23967373 | while (allowed[i] != 0) { |
| 166 |
2/2✓ Branch 0 taken 261737 times.
✓ Branch 1 taken 21588330 times.
|
21850067 | if (c == allowed[i]) return true; |
| 167 | 21588330 | i++; | |
| 168 | } | ||
| 169 | 2117306 | return false; | |
| 170 | } | ||
| 171 | |||
| 172 | 2139888 | int tok_symbol(lbm_char_channel_t *chan) { | |
| 173 | |||
| 174 | char c; | ||
| 175 | 2139888 | int r = 0; | |
| 176 | |||
| 177 | 2139888 | r = lbm_channel_peek(chan, 0, &c); | |
| 178 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2139887 times.
|
2139888 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 179 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2139886 times.
|
2139887 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 180 |
3/4✓ Branch 0 taken 2139886 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 934 times.
✓ Branch 4 taken 2138952 times.
|
2139886 | if (r == CHANNEL_SUCCESS && !symchar0(c)) { |
| 181 | 934 | return TOKENIZER_NO_TOKEN; | |
| 182 | } | ||
| 183 | 2138952 | memset(tokpar_sym_str,0,TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH+1); | |
| 184 |
4/4✓ Branch 0 taken 1998813 times.
✓ Branch 1 taken 140139 times.
✓ Branch 2 taken 246 times.
✓ Branch 3 taken 1998567 times.
|
2138952 | tokpar_sym_str[0] = (c >= 'A' && c <= 'Z') ? c + 32 : c; // locale independent ASCII only tolower. |
| 185 | |||
| 186 | 2138952 | int len = 1; | |
| 187 | |||
| 188 | 2138952 | r = lbm_channel_peek(chan,(unsigned int)len, &c); | |
| 189 |
4/4✓ Branch 0 taken 8066274 times.
✓ Branch 1 taken 21643 times.
✓ Branch 3 taken 5948968 times.
✓ Branch 4 taken 2117306 times.
|
8087917 | while (r == CHANNEL_SUCCESS && symchar(c)) { |
| 190 |
4/4✓ Branch 0 taken 5474678 times.
✓ Branch 1 taken 474290 times.
✓ Branch 2 taken 1451 times.
✓ Branch 3 taken 5473227 times.
|
5948968 | c = (c >= 'A' && c <= 'Z') ? c + 32 : c; // locale independent ASCII only tolower. |
| 191 |
2/2✓ Branch 0 taken 5948965 times.
✓ Branch 1 taken 3 times.
|
5948968 | if (len < TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH) { |
| 192 | 5948965 | tokpar_sym_str[len] = (char)c; | |
| 193 | } else { | ||
| 194 | 3 | return TOKENIZER_SYMBOL_ERROR; | |
| 195 | } | ||
| 196 | 5948965 | len ++; | |
| 197 | 5948965 | r = lbm_channel_peek(chan,(unsigned int)len, &c); | |
| 198 | } | ||
| 199 |
2/2✓ Branch 0 taken 582 times.
✓ Branch 1 taken 2138367 times.
|
2138949 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 200 | 2138367 | tokpar_sym_str[len] = 0; | |
| 201 | 2138367 | return len; | |
| 202 | } | ||
| 203 | |||
| 204 | 6654495 | int tok_string(lbm_char_channel_t *chan, unsigned int *string_len) { | |
| 205 | |||
| 206 | 6654495 | unsigned int n = 0; | |
| 207 | 6654495 | unsigned int len = 0; | |
| 208 | char c; | ||
| 209 | 6654495 | int r = 0; | |
| 210 | 6654495 | bool encode = false; | |
| 211 | |||
| 212 | 6654495 | r = lbm_channel_peek(chan,0,&c); | |
| 213 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6654494 times.
|
6654495 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 214 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6654493 times.
|
6654494 | else if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 215 | |||
| 216 |
2/2✓ Branch 0 taken 6637163 times.
✓ Branch 1 taken 17330 times.
|
6654493 | if (c != '\"') return TOKENIZER_NO_TOKEN;; |
| 217 | 17330 | n++; | |
| 218 | |||
| 219 | 17330 | memset(tokpar_sym_str,0,TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH+1); | |
| 220 | |||
| 221 | // read string into buffer | ||
| 222 | 17330 | r = lbm_channel_peek(chan,n,&c); | |
| 223 |
7/8✓ Branch 0 taken 137984 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 17618 times.
✓ Branch 3 taken 120366 times.
✓ Branch 4 taken 344 times.
✓ Branch 5 taken 17274 times.
✓ Branch 6 taken 120710 times.
✗ Branch 7 not taken.
|
138001 | while (r == CHANNEL_SUCCESS && (c != '\"' || encode) && |
| 224 | len < TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH) { | ||
| 225 |
4/4✓ Branch 0 taken 1197 times.
✓ Branch 1 taken 119513 times.
✓ Branch 2 taken 1050 times.
✓ Branch 3 taken 147 times.
|
120710 | if (c == '\\' && !encode) { |
| 226 | 1050 | encode = true; | |
| 227 | } else { | ||
| 228 |
2/2✓ Branch 0 taken 1050 times.
✓ Branch 1 taken 118610 times.
|
119660 | if (encode) { |
| 229 | 1050 | signed char result = translate_escape_char(c); | |
| 230 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 1011 times.
|
1050 | if (result == -1) { |
| 231 | 39 | return TOKENIZER_STRING_ERROR; | |
| 232 | } else { | ||
| 233 | 1011 | tokpar_sym_str[len] = result; | |
| 234 | } | ||
| 235 | } else { | ||
| 236 | 118610 | tokpar_sym_str[len] = c; | |
| 237 | } | ||
| 238 | 119621 | len++; | |
| 239 | 119621 | encode = false; | |
| 240 | } | ||
| 241 | 120671 | n ++; | |
| 242 | 120671 | r = lbm_channel_peek(chan, n, &c); | |
| 243 | } | ||
| 244 | |||
| 245 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 17283 times.
|
17291 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 246 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 17274 times.
|
17283 | if (c != '\"') return TOKENIZER_STRING_ERROR; |
| 247 | |||
| 248 | 17274 | *string_len = len; | |
| 249 | 17274 | n ++; | |
| 250 | 17274 | return (int)n; | |
| 251 | } | ||
| 252 | |||
| 253 | 952 | int tok_char(lbm_char_channel_t *chan, char *res) { | |
| 254 | |||
| 255 | char c; | ||
| 256 | int r; | ||
| 257 | |||
| 258 | 952 | r = lbm_channel_peek(chan, 0, &c); | |
| 259 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 951 times.
|
952 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 260 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 950 times.
|
951 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 261 | |||
| 262 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 943 times.
|
950 | if (c != '\\') return TOKENIZER_NO_TOKEN; |
| 263 | |||
| 264 | 943 | r = lbm_channel_peek(chan, 1, &c); | |
| 265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 943 times.
|
943 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 266 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 943 times.
|
943 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 267 | |||
| 268 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 943 times.
|
943 | if (c != '#') return TOKENIZER_NO_TOKEN; |
| 269 | |||
| 270 | 943 | r = lbm_channel_peek(chan, 2, &c); | |
| 271 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 943 times.
|
943 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 272 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 943 times.
|
943 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 273 | |||
| 274 |
2/2✓ Branch 0 taken 592 times.
✓ Branch 1 taken 351 times.
|
943 | if (c == '\\') { |
| 275 | 592 | r = lbm_channel_peek(chan, 3, &c); | |
| 276 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
|
592 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 277 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
|
592 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 278 | |||
| 279 | 592 | signed char result = translate_escape_char(c); | |
| 280 |
2/2✓ Branch 0 taken 555 times.
✓ Branch 1 taken 37 times.
|
592 | if (result != -1) { |
| 281 | 555 | *res = result; | |
| 282 | 555 | return 4; | |
| 283 | } else { | ||
| 284 | 37 | return TOKENIZER_CHAR_ERROR; | |
| 285 | } | ||
| 286 | } | ||
| 287 | 351 | *res = c; | |
| 288 | 351 | 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 | 6637166 | int tok_double(lbm_char_channel_t *chan, token_float *result) { | |
| 296 | |||
| 297 | 6637166 | unsigned int n = 0; | |
| 298 | char fbuf[TD_BUF_SIZE]; | ||
| 299 | char c; | ||
| 300 | 6637166 | bool valid_num = false; | |
| 301 | int res; | ||
| 302 | int type_len; | ||
| 303 | uint32_t tok_res; | ||
| 304 | |||
| 305 | 6637166 | memset(fbuf, 0, TD_BUF_SIZE); | |
| 306 | |||
| 307 | 6637166 | result->type = TOKTYPEF32; | |
| 308 | 6637166 | result->negative = false; | |
| 309 | |||
| 310 | 6637166 | res = lbm_channel_peek(chan, n, &c); | |
| 311 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6637165 times.
|
6637166 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 312 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6637164 times.
|
6637165 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 313 |
2/2✓ Branch 0 taken 26242 times.
✓ Branch 1 taken 6610922 times.
|
6637164 | if (c == '-') { |
| 314 |
1/2✓ Branch 0 taken 26242 times.
✗ Branch 1 not taken.
|
26242 | FBUF_ADD('-', n); |
| 315 | 26242 | result->negative = true; | |
| 316 | } | ||
| 317 | |||
| 318 | 6637164 | res = lbm_channel_peek(chan, n, &c); | |
| 319 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6637162 times.
|
6637164 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 320 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6637162 times.
|
6637162 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 321 |
4/4✓ Branch 0 taken 7778257 times.
✓ Branch 1 taken 4529118 times.
✓ Branch 2 taken 5670485 times.
✓ Branch 3 taken 2107772 times.
|
12307375 | while (c >= '0' && c <= '9') { |
| 322 |
1/2✓ Branch 0 taken 5670485 times.
✗ Branch 1 not taken.
|
5670485 | FBUF_ADD(c, n); |
| 323 | 5670485 | res = lbm_channel_peek(chan, n, &c); | |
| 324 |
2/2✓ Branch 0 taken 175 times.
✓ Branch 1 taken 5670310 times.
|
5670485 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 325 |
2/2✓ Branch 0 taken 97 times.
✓ Branch 1 taken 5670213 times.
|
5670310 | if (res == CHANNEL_END) break; |
| 326 | } | ||
| 327 | |||
| 328 |
2/2✓ Branch 0 taken 16496 times.
✓ Branch 1 taken 6620491 times.
|
6636987 | if (c == '.') { |
| 329 |
1/2✓ Branch 0 taken 16496 times.
✗ Branch 1 not taken.
|
16496 | FBUF_ADD(c, n); |
| 330 | } | ||
| 331 | 6620491 | else return TOKENIZER_NO_TOKEN; | |
| 332 | |||
| 333 | 16496 | res = lbm_channel_peek(chan,n, &c); | |
| 334 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16493 times.
|
16496 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 335 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16493 times.
|
16493 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 336 |
2/4✓ Branch 0 taken 16493 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16493 times.
|
16493 | if (!(c >= '0' && c <= '9')) return TOKENIZER_NO_TOKEN; |
| 337 | |||
| 338 |
4/4✓ Branch 0 taken 31803 times.
✓ Branch 1 taken 8234 times.
✓ Branch 2 taken 23548 times.
✓ Branch 3 taken 8255 times.
|
40037 | while (c >= '0' && c <= '9') { |
| 339 |
1/2✓ Branch 0 taken 23548 times.
✗ Branch 1 not taken.
|
23548 | FBUF_ADD(c, n); |
| 340 | 23548 | res = lbm_channel_peek(chan, n, &c); | |
| 341 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 23545 times.
|
23548 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 342 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 23544 times.
|
23545 | if (res == CHANNEL_END) break; |
| 343 | } | ||
| 344 | |||
| 345 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 16346 times.
|
16490 | if (c == 'e') { |
| 346 |
1/2✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
|
144 | FBUF_ADD(c, n); |
| 347 | 144 | res = lbm_channel_peek(chan,n, &c); | |
| 348 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
|
144 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 349 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
|
144 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 350 |
2/6✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 144 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
144 | if (!((c >= '0' && c <= '9') || c == '-')) return TOKENIZER_NO_TOKEN; |
| 351 | |||
| 352 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
|
144 | if (c == '-') { |
| 353 | ✗ | FBUF_ADD(c, n); | |
| 354 | } | ||
| 355 | 144 | res = lbm_channel_peek(chan,n, &c); | |
| 356 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
|
144 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 357 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
|
144 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 358 |
4/4✓ Branch 0 taken 216 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 144 times.
✓ Branch 3 taken 72 times.
|
288 | while ((c >= '0' && c <= '9')) { |
| 359 |
1/2✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
|
144 | FBUF_ADD(c,n); |
| 360 | 144 | res = lbm_channel_peek(chan, n, &c); | |
| 361 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
|
144 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 362 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
|
144 | if (res == CHANNEL_END) break; |
| 363 | } | ||
| 364 | } | ||
| 365 | |||
| 366 | 16490 | type_len = tok_match_fixed_size_tokens(chan, type_qual_table, n, NUM_TYPE_QUALIFIERS, &tok_res); | |
| 367 | |||
| 368 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16488 times.
|
16490 | if (type_len == TOKENIZER_NEED_MORE) return type_len; |
| 369 |
2/2✓ Branch 0 taken 8307 times.
✓ Branch 1 taken 8181 times.
|
16488 | if (type_len == TOKENIZER_NO_TOKEN) { |
| 370 | 8307 | result->type = TOKTYPEF32; | |
| 371 | } else { | ||
| 372 | 8181 | result->type = tok_res; | |
| 373 | } | ||
| 374 | |||
| 375 |
3/4✓ Branch 0 taken 412 times.
✓ Branch 1 taken 16076 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 412 times.
|
16488 | if ((result->negative && n > 1) || |
| 376 |
2/4✓ Branch 0 taken 16076 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16076 times.
✗ Branch 3 not taken.
|
16488 | (!result->negative && n > 0)) valid_num = true; |
| 377 | |||
| 378 |
1/2✓ Branch 0 taken 16488 times.
✗ Branch 1 not taken.
|
16488 | if(valid_num) { |
| 379 | 16488 | result->value = (double)strtod(fbuf,NULL); | |
| 380 | 16488 | return (int)n + type_len; | |
| 381 | } | ||
| 382 | |||
| 383 | ✗ | tok_double_no_tok: | |
| 384 | ✗ | return TOKENIZER_NO_TOKEN; | |
| 385 | } | ||
| 386 | |||
| 387 | 9708693 | bool tok_clean_whitespace(lbm_char_channel_t *chan) { | |
| 388 | |||
| 389 | 9708693 | bool cleaning_whitespace = true; | |
| 390 | char c; | ||
| 391 | int r; | ||
| 392 | |||
| 393 |
2/2✓ Branch 0 taken 9734531 times.
✓ Branch 1 taken 9678054 times.
|
19412585 | while (cleaning_whitespace) { |
| 394 | |||
| 395 |
2/2✓ Branch 1 taken 26920 times.
✓ Branch 2 taken 9707611 times.
|
9734531 | if (lbm_channel_comment(chan)) { |
| 396 | while (true) { | ||
| 397 | 1777131 | r = lbm_channel_peek(chan, 0, &c); | |
| 398 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1777131 times.
|
1777131 | if (r == CHANNEL_END) { |
| 399 | ✗ | lbm_channel_set_comment(chan, false); | |
| 400 | ✗ | cleaning_whitespace = false; | |
| 401 | ✗ | break; | |
| 402 | } | ||
| 403 |
2/2✓ Branch 0 taken 1093 times.
✓ Branch 1 taken 1776038 times.
|
1777131 | if (r == CHANNEL_MORE) { |
| 404 | 1093 | return false; | |
| 405 | } | ||
| 406 | 1776038 | lbm_channel_drop(chan,1); | |
| 407 |
2/2✓ Branch 0 taken 25827 times.
✓ Branch 1 taken 1750211 times.
|
1776038 | if (c == '\n') { |
| 408 | 25827 | lbm_channel_set_comment(chan, false); | |
| 409 | 25827 | break; | |
| 410 | } | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | do { | ||
| 415 | 17667795 | r = lbm_channel_peek(chan, 0, &c); | |
| 416 |
2/2✓ Branch 0 taken 792 times.
✓ Branch 1 taken 17667003 times.
|
17667795 | if (r == CHANNEL_MORE) { |
| 417 | 792 | return false; | |
| 418 |
2/2✓ Branch 0 taken 28754 times.
✓ Branch 1 taken 17638249 times.
|
17667003 | } else if (r == CHANNEL_END) { |
| 419 | 28754 | return true; | |
| 420 | } | ||
| 421 |
2/2✓ Branch 0 taken 25838 times.
✓ Branch 1 taken 17612411 times.
|
17638249 | if (c == ';') { |
| 422 | 25838 | lbm_channel_set_comment(chan, true); | |
| 423 | 25838 | break; | |
| 424 | } | ||
| 425 | #ifdef LBM_USE_SHEBANG_COMMENTS | ||
| 426 |
4/4✓ Branch 1 taken 303052 times.
✓ Branch 2 taken 3972366 times.
✓ Branch 3 taken 1226 times.
✓ Branch 4 taken 301826 times.
|
4578470 | else if ((lbm_channel_column(chan) == 1) && |
| 427 | 303052 | (lbm_channel_row(chan) == 1) && | |
| 428 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1226 times.
|
1226 | 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 7934357 times.
✓ Branch 1 taken 9678054 times.
|
17612411 | if (isspace(c)) { |
| 441 | 7934357 | lbm_channel_drop(chan,1); | |
| 442 | } else { | ||
| 443 | 9678054 | cleaning_whitespace = false; | |
| 444 | } | ||
| 445 | |||
| 446 |
2/2✓ Branch 0 taken 7934357 times.
✓ Branch 1 taken 9678054 times.
|
17612411 | } while (cleaning_whitespace); |
| 447 | } | ||
| 448 | 9678054 | return true; | |
| 449 | } | ||
| 450 | |||
| 451 | 6620496 | int tok_integer(lbm_char_channel_t *chan, token_int *result) { | |
| 452 | 6620496 | uint64_t acc = 0; | |
| 453 | 6620496 | unsigned int n = 0; | |
| 454 | 6620496 | bool valid_num = false; | |
| 455 | char c; | ||
| 456 | int res; | ||
| 457 | |||
| 458 | 6620496 | result->type = TOKTYPEI; | |
| 459 | 6620496 | result-> negative = false; | |
| 460 | 6620496 | res = lbm_channel_peek(chan, 0, &c); | |
| 461 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6620495 times.
|
6620496 | if (res == CHANNEL_MORE) { |
| 462 | 1 | return TOKENIZER_NEED_MORE; | |
| 463 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6620494 times.
|
6620495 | } else if (res == CHANNEL_END) { |
| 464 | 1 | return TOKENIZER_NO_TOKEN; | |
| 465 | } | ||
| 466 |
2/2✓ Branch 0 taken 25827 times.
✓ Branch 1 taken 6594667 times.
|
6620494 | if (c == '-') { |
| 467 | 25827 | n = 1; | |
| 468 | 25827 | result->negative = true; | |
| 469 | } | ||
| 470 | |||
| 471 | 6620494 | bool hex = false; | |
| 472 | 6620494 | res = lbm_channel_peek(chan, n, &c); | |
| 473 |
3/4✓ Branch 0 taken 6620494 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55944 times.
✓ Branch 3 taken 6564550 times.
|
6620494 | if (res == CHANNEL_SUCCESS && c == '0') { |
| 474 | 55944 | res = lbm_channel_peek(chan, n + 1, &c); | |
| 475 |
4/6✓ Branch 0 taken 55944 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32543 times.
✓ Branch 3 taken 23401 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 32543 times.
|
55944 | if ( res == CHANNEL_SUCCESS && (c == 'x' || c == 'X')) { |
| 476 | 23401 | hex = true; | |
| 477 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32543 times.
|
32543 | } else if (res == CHANNEL_MORE) { |
| 478 | ✗ | return TOKENIZER_NEED_MORE; | |
| 479 | } | ||
| 480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6564550 times.
|
6564550 | } else if (res == CHANNEL_MORE) { |
| 481 | ✗ | return TOKENIZER_NEED_MORE; | |
| 482 | } | ||
| 483 | |||
| 484 |
2/2✓ Branch 0 taken 23401 times.
✓ Branch 1 taken 6597093 times.
|
6620494 | if (hex) { |
| 485 | 23401 | n += 2; | |
| 486 | |||
| 487 | 23401 | res = lbm_channel_peek(chan,n, &c); | |
| 488 | |||
| 489 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 23395 times.
|
23401 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 490 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23395 times.
|
23395 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 491 | |||
| 492 |
2/2✓ Branch 0 taken 41147 times.
✓ Branch 1 taken 16609 times.
|
81151 | while ((c >= '0' && c <= '9') || |
| 493 |
6/6✓ Branch 0 taken 57756 times.
✓ Branch 1 taken 22676 times.
✓ Branch 2 taken 1224 times.
✓ Branch 3 taken 38061 times.
✓ Branch 4 taken 785 times.
✓ Branch 5 taken 439 times.
|
97041 | (c >= 'a' && c <= 'f') || |
| 494 |
4/4✓ Branch 0 taken 15824 times.
✓ Branch 1 taken 22676 times.
✓ Branch 2 taken 15163 times.
✓ Branch 3 taken 661 times.
|
38500 | (c >= 'A' && c <= 'F')) { |
| 495 | uint32_t val; /* values between 0 and 16 */ | ||
| 496 |
3/4✓ Branch 0 taken 785 times.
✓ Branch 1 taken 56310 times.
✓ Branch 2 taken 785 times.
✗ Branch 3 not taken.
|
57095 | if (c >= 'a' && c <= 'f') { |
| 497 | 785 | val = 10 + (uint32_t)c - 'a'; | |
| 498 |
3/4✓ Branch 0 taken 15163 times.
✓ Branch 1 taken 41147 times.
✓ Branch 2 taken 15163 times.
✗ Branch 3 not taken.
|
56310 | } else if (c >= 'A' && c <= 'F') { |
| 499 | 15163 | val = 10 + (uint32_t)(c - 'A'); | |
| 500 | } else { | ||
| 501 | 41147 | val = (uint32_t)c - '0'; | |
| 502 | } | ||
| 503 | 57095 | acc = (acc * 0x10) + val; | |
| 504 | 57095 | n++; | |
| 505 | 57095 | res = lbm_channel_peek(chan, n, &c); | |
| 506 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 57037 times.
|
57095 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 507 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57037 times.
|
57037 | if (res == CHANNEL_END) break; |
| 508 | |||
| 509 | } | ||
| 510 | } else { | ||
| 511 | 6597093 | res = lbm_channel_peek(chan, n, &c); | |
| 512 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6597093 times.
|
6597093 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 513 |
4/4✓ Branch 0 taken 7713526 times.
✓ Branch 1 taken 4512624 times.
✓ Branch 2 taken 5629155 times.
✓ Branch 3 taken 2084371 times.
|
12226150 | while (c >= '0' && c <= '9') { |
| 514 | 5629155 | acc = (acc*10) + (uint32_t)(c - '0'); | |
| 515 | 5629155 | n++; | |
| 516 | 5629155 | res = lbm_channel_peek(chan, n, &c); | |
| 517 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5629155 times.
|
5629155 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 518 |
2/2✓ Branch 0 taken 98 times.
✓ Branch 1 taken 5629057 times.
|
5629155 | if (res == CHANNEL_END) break; |
| 519 | } | ||
| 520 | } | ||
| 521 | |||
| 522 |
5/6✓ Branch 0 taken 4503607 times.
✓ Branch 1 taken 2116823 times.
✓ Branch 2 taken 23337 times.
✓ Branch 3 taken 4480270 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 23337 times.
|
6620430 | if (n == 0 || (hex && n == 2)) return TOKENIZER_NO_TOKEN; |
| 523 | |||
| 524 | uint32_t tok_res; | ||
| 525 | 4503607 | 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 21 times.
✓ Branch 1 taken 4503586 times.
|
4503607 | if (type_len == TOKENIZER_NEED_MORE) return type_len; |
| 528 |
2/2✓ Branch 0 taken 28674 times.
✓ Branch 1 taken 4474912 times.
|
4503586 | if (type_len != TOKENIZER_NO_TOKEN) { |
| 529 | 28674 | result->type = tok_res; | |
| 530 | } | ||
| 531 | |||
| 532 |
4/4✓ Branch 0 taken 25827 times.
✓ Branch 1 taken 4477759 times.
✓ Branch 2 taken 23060 times.
✓ Branch 3 taken 2767 times.
|
4503586 | if ((result->negative && n > 1) || |
| 533 |
2/2✓ Branch 0 taken 4477759 times.
✓ Branch 1 taken 23060 times.
|
4503586 | !result->negative) valid_num = true; |
| 534 | |||
| 535 |
2/2✓ Branch 0 taken 4480526 times.
✓ Branch 1 taken 23060 times.
|
4503586 | if (valid_num) { |
| 536 | 4480526 | result->value = acc; | |
| 537 | 4480526 | return (int)n + type_len; | |
| 538 | } | ||
| 539 | 23060 | return TOKENIZER_NO_TOKEN; | |
| 540 | } | ||
| 541 |