| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | Copyright 2019, 2021, 2022 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 | 2477 | static inline signed char translate_escape_char(char c) { | |
| 57 |
14/14✓ Branch 0 taken 168 times.
✓ Branch 1 taken 112 times.
✓ Branch 2 taken 168 times.
✓ Branch 3 taken 113 times.
✓ Branch 4 taken 286 times.
✓ Branch 5 taken 112 times.
✓ Branch 6 taken 112 times.
✓ Branch 7 taken 113 times.
✓ Branch 8 taken 112 times.
✓ Branch 9 taken 112 times.
✓ Branch 10 taken 564 times.
✓ Branch 11 taken 281 times.
✓ Branch 12 taken 112 times.
✓ Branch 13 taken 112 times.
|
2477 | switch (c) { |
| 58 | 168 | case '0': return '\0'; | |
| 59 | 112 | case 'a': return '\a'; | |
| 60 | 168 | case 'b': return '\b'; | |
| 61 | 113 | case 't': return '\t'; | |
| 62 | 286 | case 'n': return '\n'; | |
| 63 | 112 | case 'v': return '\v'; | |
| 64 | 112 | case 'f': return '\f'; | |
| 65 | 113 | case 'r': return '\r'; | |
| 66 | 112 | case 'e': return 27; | |
| 67 | 112 | case 's': return 32; | |
| 68 | 564 | case '"': return '\"'; | |
| 69 | 281 | case '\\': return '\\'; | |
| 70 | 112 | case 'd': return 127; | |
| 71 | 112 | 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 | 18352850 | 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 221898377 times.
✓ Branch 1 taken 6713814 times.
|
228612191 | for (unsigned int i = 0; i < num; i ++) { |
| 112 | 221898377 | uint32_t tok_len = m[i].len; | |
| 113 | 221898377 | const char *match_str = m[i].str; | |
| 114 | char c; | ||
| 115 | int char_pos; | ||
| 116 |
2/2✓ Branch 0 taken 213413945 times.
✓ Branch 1 taken 11639036 times.
|
225052981 | for (char_pos = 0; char_pos < (int)tok_len; char_pos ++) { |
| 117 | 213413945 | int r = lbm_channel_peek(chan,(unsigned int)char_pos + start_pos, &c); | |
| 118 |
2/2✓ Branch 0 taken 213412737 times.
✓ Branch 1 taken 1208 times.
|
213413945 | if (r == CHANNEL_SUCCESS) { |
| 119 |
2/2✓ Branch 0 taken 210258133 times.
✓ Branch 1 taken 3154604 times.
|
213412737 | if (c != match_str[char_pos]) break; |
| 120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1208 times.
|
1208 | } else if (r == CHANNEL_MORE ) { |
| 121 | 11639036 | return TOKENIZER_NEED_MORE; | |
| 122 | } else { | ||
| 123 | 1208 | break; | |
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 |
2/2✓ Branch 0 taken 11639036 times.
✓ Branch 1 taken 210259341 times.
|
221898377 | if (char_pos == (int)tok_len) { //match |
| 128 | 11639036 | *res = m[i].token; | |
| 129 | 11639036 | return (int)tok_len; | |
| 130 | } | ||
| 131 | } | ||
| 132 | 6713814 | return TOKENIZER_NO_TOKEN; | |
| 133 | } | ||
| 134 | |||
| 135 | 11583732 | int tok_syntax(lbm_char_channel_t *chan, uint32_t *res) { | |
| 136 | 11583732 | return tok_match_fixed_size_tokens(chan, fixed_size_tokens, 0, NUM_FIXED_SIZE_TOKENS, res); | |
| 137 | } | ||
| 138 | |||
| 139 | 7369714 | static bool alpha_char(char c) { | |
| 140 |
6/6✓ Branch 0 taken 4990402 times.
✓ Branch 1 taken 2379312 times.
✓ Branch 2 taken 168 times.
✓ Branch 3 taken 4990234 times.
✓ Branch 4 taken 17940 times.
✓ Branch 5 taken 2361540 times.
|
7387654 | return ((c >= 'a' && c <= 'z') || |
| 141 |
2/2✓ Branch 0 taken 2208 times.
✓ Branch 1 taken 15732 times.
|
17940 | (c >= 'A' && c <= 'Z')); |
| 142 | } | ||
| 143 | |||
| 144 | 2260189 | static bool num_char(char c) { | |
| 145 |
4/4✓ Branch 0 taken 268194 times.
✓ Branch 1 taken 1991995 times.
✓ Branch 2 taken 250057 times.
✓ Branch 3 taken 18137 times.
|
2260189 | return (c >= '0' && c <= '9'); |
| 146 | } | ||
| 147 | |||
| 148 | 1862497 | static bool symchar0(char c) { | |
| 149 | 1862497 | const char *allowed = "+-*/=<>#!"; | |
| 150 | |||
| 151 |
2/2✓ Branch 0 taken 1745414 times.
✓ Branch 1 taken 117083 times.
|
1862497 | if (alpha_char(c)) return true; |
| 152 | 117083 | int i = 0; | |
| 153 |
2/2✓ Branch 0 taken 377530 times.
✓ Branch 1 taken 1420 times.
|
378950 | while (allowed[i] != 0) { |
| 154 |
2/2✓ Branch 0 taken 115663 times.
✓ Branch 1 taken 261867 times.
|
377530 | if (c == allowed[i]) return true; |
| 155 | 261867 | i ++; | |
| 156 | } | ||
| 157 | 1420 | return false; | |
| 158 | } | ||
| 159 | |||
| 160 | 5507217 | static bool symchar(char c) { | |
| 161 | 5507217 | const char *allowed = "+-*/=<>!?_"; | |
| 162 | |||
| 163 |
4/4✓ Branch 0 taken 2260189 times.
✓ Branch 1 taken 3247028 times.
✓ Branch 2 taken 250057 times.
✓ Branch 3 taken 2010132 times.
|
5507217 | if (alpha_char(c) || num_char(c)) return true; |
| 164 | 2010132 | int i = 0; | |
| 165 |
2/2✓ Branch 0 taken 18850361 times.
✓ Branch 1 taken 1837681 times.
|
20688042 | while (allowed[i] != 0) { |
| 166 |
2/2✓ Branch 0 taken 172451 times.
✓ Branch 1 taken 18677910 times.
|
18850361 | if (c == allowed[i]) return true; |
| 167 | 18677910 | i++; | |
| 168 | } | ||
| 169 | 1837681 | return false; | |
| 170 | } | ||
| 171 | |||
| 172 | 1862497 | int tok_symbol(lbm_char_channel_t *chan) { | |
| 173 | |||
| 174 | char c; | ||
| 175 | 1862497 | int r = 0; | |
| 176 | |||
| 177 | 1862497 | r = lbm_channel_peek(chan, 0, &c); | |
| 178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1862497 times.
|
1862497 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1862497 times.
|
1862497 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 180 |
3/4✓ Branch 0 taken 1862497 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1420 times.
✓ Branch 3 taken 1861077 times.
|
1862497 | if (r == CHANNEL_SUCCESS && !symchar0(c)) { |
| 181 | 1420 | return TOKENIZER_NO_TOKEN; | |
| 182 | } | ||
| 183 | 1861077 | memset(tokpar_sym_str,0,TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH+1); | |
| 184 | 1861077 | tokpar_sym_str[0] = (char)tolower(c); | |
| 185 | |||
| 186 | 1861077 | int len = 1; | |
| 187 | |||
| 188 | 1861077 | r = lbm_channel_peek(chan,(unsigned int)len, &c); | |
| 189 |
4/4✓ Branch 0 taken 5507217 times.
✓ Branch 1 taken 23396 times.
✓ Branch 2 taken 3669536 times.
✓ Branch 3 taken 1837681 times.
|
5530613 | while (r == CHANNEL_SUCCESS && symchar(c)) { |
| 190 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3669536 times.
|
3669536 | if (len >= 255) return TOKENIZER_SYMBOL_ERROR; |
| 191 | 3669536 | c = (char)tolower(c); | |
| 192 |
1/2✓ Branch 0 taken 3669536 times.
✗ Branch 1 not taken.
|
3669536 | if (len < TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH) { |
| 193 | 3669536 | tokpar_sym_str[len] = (char)c; | |
| 194 | } | ||
| 195 | 3669536 | len ++; | |
| 196 | 3669536 | r = lbm_channel_peek(chan,(unsigned int)len, &c); | |
| 197 | } | ||
| 198 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 1861033 times.
|
1861077 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 199 | 1861033 | tokpar_sym_str[len] = 0; | |
| 200 | 1861033 | return len; | |
| 201 | } | ||
| 202 | |||
| 203 | 8645465 | int tok_string(lbm_char_channel_t *chan, unsigned int *string_len) { | |
| 204 | |||
| 205 | 8645465 | unsigned int n = 0; | |
| 206 | 8645465 | unsigned int len = 0; | |
| 207 | char c; | ||
| 208 | 8645465 | int r = 0; | |
| 209 | 8645465 | bool encode = false; | |
| 210 | |||
| 211 | 8645465 | r = lbm_channel_peek(chan,0,&c); | |
| 212 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8645465 times.
|
8645465 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 213 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8645465 times.
|
8645465 | else if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 214 | |||
| 215 |
2/2✓ Branch 0 taken 8624420 times.
✓ Branch 1 taken 21045 times.
|
8645465 | if (c != '\"') return TOKENIZER_NO_TOKEN;; |
| 216 | 21045 | n++; | |
| 217 | |||
| 218 | 21045 | memset(tokpar_sym_str,0,TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH+1); | |
| 219 | |||
| 220 | // read string into buffer | ||
| 221 | 21045 | r = lbm_channel_peek(chan,n,&c); | |
| 222 |
7/8✓ Branch 0 taken 140150 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 21493 times.
✓ Branch 3 taken 118657 times.
✓ Branch 4 taken 508 times.
✓ Branch 5 taken 20985 times.
✓ Branch 6 taken 119165 times.
✗ Branch 7 not taken.
|
140154 | while (r == CHANNEL_SUCCESS && (c != '\"' || encode) && |
| 223 | len < TOKENIZER_MAX_SYMBOL_AND_STRING_LENGTH) { | ||
| 224 |
4/4✓ Branch 0 taken 1804 times.
✓ Branch 1 taken 117361 times.
✓ Branch 2 taken 1579 times.
✓ Branch 3 taken 225 times.
|
119165 | if (c == '\\' && !encode) { |
| 225 | 1579 | encode = true; | |
| 226 | } else { | ||
| 227 |
2/2✓ Branch 0 taken 1579 times.
✓ Branch 1 taken 116007 times.
|
117586 | if (encode) { |
| 228 | 1579 | signed char result = translate_escape_char(c); | |
| 229 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 1523 times.
|
1579 | if (result == -1) { |
| 230 | 56 | return TOKENIZER_STRING_ERROR; | |
| 231 | } else { | ||
| 232 | 1523 | tokpar_sym_str[len] = result; | |
| 233 | } | ||
| 234 | } else { | ||
| 235 | 116007 | tokpar_sym_str[len] = c; | |
| 236 | } | ||
| 237 | 117530 | len++; | |
| 238 | 117530 | encode = false; | |
| 239 | } | ||
| 240 | 119109 | n ++; | |
| 241 | 119109 | r = lbm_channel_peek(chan, n, &c); | |
| 242 | } | ||
| 243 | |||
| 244 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20987 times.
|
20989 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 245 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20985 times.
|
20987 | if (c != '\"') return TOKENIZER_STRING_ERROR; |
| 246 | |||
| 247 | 20985 | *string_len = len; | |
| 248 | 20985 | n ++; | |
| 249 | 20985 | return (int)n; | |
| 250 | } | ||
| 251 | |||
| 252 | 1420 | int tok_char(lbm_char_channel_t *chan, char *res) { | |
| 253 | |||
| 254 | char c; | ||
| 255 | int r; | ||
| 256 | |||
| 257 | 1420 | r = lbm_channel_peek(chan, 0, &c); | |
| 258 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1420 times.
|
1420 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 259 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1420 times.
|
1420 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 260 | |||
| 261 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1418 times.
|
1420 | if (c != '\\') return TOKENIZER_NO_TOKEN; |
| 262 | |||
| 263 | 1418 | r = lbm_channel_peek(chan, 1, &c); | |
| 264 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1418 times.
|
1418 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1418 times.
|
1418 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 266 | |||
| 267 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1418 times.
|
1418 | if (c != '#') return TOKENIZER_NO_TOKEN; |
| 268 | |||
| 269 | 1418 | r = lbm_channel_peek(chan, 2, &c); | |
| 270 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1418 times.
|
1418 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 271 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1418 times.
|
1418 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 272 | |||
| 273 |
2/2✓ Branch 0 taken 898 times.
✓ Branch 1 taken 520 times.
|
1418 | if (c == '\\') { |
| 274 | 898 | r = lbm_channel_peek(chan, 3, &c); | |
| 275 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 898 times.
|
898 | if (r == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 276 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 898 times.
|
898 | if (r == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 277 | |||
| 278 | 898 | signed char result = translate_escape_char(c); | |
| 279 |
2/2✓ Branch 0 taken 842 times.
✓ Branch 1 taken 56 times.
|
898 | if (result != -1) { |
| 280 | 842 | *res = result; | |
| 281 | 842 | return 4; | |
| 282 | } else { | ||
| 283 | 56 | return TOKENIZER_CHAR_ERROR; | |
| 284 | } | ||
| 285 | } | ||
| 286 | 520 | *res = c; | |
| 287 | 520 | return 3; | |
| 288 | } | ||
| 289 | |||
| 290 | 8624420 | int tok_double(lbm_char_channel_t *chan, token_float *result) { | |
| 291 | |||
| 292 | 8624420 | unsigned int n = 0; | |
| 293 | char fbuf[128]; | ||
| 294 | char c; | ||
| 295 | 8624420 | bool valid_num = false; | |
| 296 | int res; | ||
| 297 | |||
| 298 | 8624420 | memset(fbuf, 0, 128); | |
| 299 | |||
| 300 | 8624420 | result->type = TOKTYPEF32; | |
| 301 | 8624420 | result->negative = false; | |
| 302 | |||
| 303 | 8624420 | res = lbm_channel_peek(chan, 0, &c); | |
| 304 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8624420 times.
|
8624420 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 305 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8624420 times.
|
8624420 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 306 |
2/2✓ Branch 0 taken 11382 times.
✓ Branch 1 taken 8613038 times.
|
8624420 | if (c == '-') { |
| 307 | 11382 | n = 1; | |
| 308 | 11382 | fbuf[0] = '-'; | |
| 309 | 11382 | result->negative = true; | |
| 310 | } | ||
| 311 | |||
| 312 | 8624420 | res = lbm_channel_peek(chan, n, &c); | |
| 313 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8624420 times.
|
8624420 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 314 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8624420 times.
|
8624420 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 315 |
4/4✓ Branch 0 taken 10433481 times.
✓ Branch 1 taken 6748031 times.
✓ Branch 2 taken 8557214 times.
✓ Branch 3 taken 1876267 times.
|
17181512 | while (c >= '0' && c <= '9') { |
| 316 | 8557214 | fbuf[n] = c; | |
| 317 | 8557214 | n++; | |
| 318 | 8557214 | res = lbm_channel_peek(chan, n, &c); | |
| 319 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8557213 times.
|
8557214 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 320 |
2/2✓ Branch 0 taken 121 times.
✓ Branch 1 taken 8557092 times.
|
8557213 | if (res == CHANNEL_END) break; |
| 321 | } | ||
| 322 | |||
| 323 |
2/2✓ Branch 0 taken 24069 times.
✓ Branch 1 taken 8600350 times.
|
8624419 | if (c == '.') { |
| 324 | 24069 | fbuf[n] = c; | |
| 325 | 24069 | n ++; | |
| 326 | } | ||
| 327 | |||
| 328 | 8600350 | else return TOKENIZER_NO_TOKEN; | |
| 329 | |||
| 330 | 24069 | res = lbm_channel_peek(chan,n, &c); | |
| 331 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24069 times.
|
24069 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 332 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24069 times.
|
24069 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 333 |
2/4✓ Branch 0 taken 24069 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24069 times.
|
24069 | if (!(c >= '0' && c <= '9')) return TOKENIZER_NO_TOKEN; |
| 334 | |||
| 335 |
4/4✓ Branch 0 taken 46187 times.
✓ Branch 1 taken 11871 times.
✓ Branch 2 taken 33990 times.
✓ Branch 3 taken 12197 times.
|
58058 | while (c >= '0' && c <= '9') { |
| 336 | 33990 | fbuf[n] = c; | |
| 337 | 33990 | n++; | |
| 338 | 33990 | res = lbm_channel_peek(chan, n, &c); | |
| 339 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 33989 times.
|
33990 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 340 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33989 times.
|
33989 | if (res == CHANNEL_END) break; |
| 341 | } | ||
| 342 | |||
| 343 |
2/2✓ Branch 0 taken 224 times.
✓ Branch 1 taken 23844 times.
|
24068 | if (c == 'e') { |
| 344 | 224 | fbuf[n] = c; | |
| 345 | 224 | n++; | |
| 346 | 224 | res = lbm_channel_peek(chan,n, &c); | |
| 347 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
|
224 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 348 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
|
224 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 349 |
2/6✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 224 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
224 | if (!((c >= '0' && c <= '9') || c == '-')) return TOKENIZER_NO_TOKEN; |
| 350 | |||
| 351 |
5/6✓ Branch 0 taken 336 times.
✓ Branch 1 taken 112 times.
✓ Branch 2 taken 224 times.
✓ Branch 3 taken 112 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 224 times.
|
448 | while ((c >= '0' && c <= '9') || c == '-') { |
| 352 | 224 | fbuf[n] = c; | |
| 353 | 224 | n++; | |
| 354 | 224 | res = lbm_channel_peek(chan, n, &c); | |
| 355 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
|
224 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 356 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
|
224 | if (res == CHANNEL_END) break; |
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | uint32_t tok_res; | ||
| 361 | 24068 | int type_len = tok_match_fixed_size_tokens(chan, type_qual_table, n, NUM_TYPE_QUALIFIERS, &tok_res); | |
| 362 | |||
| 363 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24068 times.
|
24068 | if (type_len == TOKENIZER_NEED_MORE) return type_len; |
| 364 |
2/2✓ Branch 0 taken 11983 times.
✓ Branch 1 taken 12085 times.
|
24068 | if (type_len == TOKENIZER_NO_TOKEN) { |
| 365 | 11983 | result->type = TOKTYPEF32; | |
| 366 | } else { | ||
| 367 | 12085 | result->type = tok_res; | |
| 368 | } | ||
| 369 | |||
| 370 |
3/4✓ Branch 0 taken 513 times.
✓ Branch 1 taken 23555 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 513 times.
|
24068 | if ((result->negative && n > 1) || |
| 371 |
2/4✓ Branch 0 taken 23555 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23555 times.
✗ Branch 3 not taken.
|
24068 | (!result->negative && n > 0)) valid_num = true; |
| 372 | |||
| 373 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24068 times.
|
24068 | if (n > 127) { |
| 374 | ✗ | return TOKENIZER_NO_TOKEN; | |
| 375 | } | ||
| 376 | |||
| 377 |
1/2✓ Branch 0 taken 24068 times.
✗ Branch 1 not taken.
|
24068 | if(valid_num) { |
| 378 | 24068 | result->value = (double)strtod(fbuf,NULL); | |
| 379 | 24068 | return (int)n + type_len; | |
| 380 | } | ||
| 381 | ✗ | return TOKENIZER_NO_TOKEN; | |
| 382 | } | ||
| 383 | |||
| 384 | 11629136 | bool tok_clean_whitespace(lbm_char_channel_t *chan) { | |
| 385 | |||
| 386 | 11629136 | bool cleaning_whitespace = true; | |
| 387 | char c; | ||
| 388 | int r; | ||
| 389 | |||
| 390 |
2/2✓ Branch 0 taken 11643175 times.
✓ Branch 1 taken 11583732 times.
|
23226907 | while (cleaning_whitespace) { |
| 391 | |||
| 392 |
2/2✓ Branch 0 taken 15612 times.
✓ Branch 1 taken 11627563 times.
|
11643175 | if (lbm_channel_comment(chan)) { |
| 393 | while (true) { | ||
| 394 | 1446519 | r = lbm_channel_peek(chan, 0, &c); | |
| 395 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1446519 times.
|
1446519 | if (r == CHANNEL_END) { |
| 396 | ✗ | lbm_channel_set_comment(chan, false); | |
| 397 | ✗ | cleaning_whitespace = false; | |
| 398 | ✗ | break; | |
| 399 | } | ||
| 400 |
2/2✓ Branch 0 taken 1586 times.
✓ Branch 1 taken 1444933 times.
|
1446519 | if (r == CHANNEL_MORE) { |
| 401 | 1586 | return false; | |
| 402 | } | ||
| 403 | 1444933 | lbm_channel_drop(chan,1); | |
| 404 |
2/2✓ Branch 0 taken 14026 times.
✓ Branch 1 taken 1430907 times.
|
1444933 | if (c == '\n') { |
| 405 | 14026 | lbm_channel_set_comment(chan, false); | |
| 406 | 14026 | break; | |
| 407 | } | ||
| 408 | } | ||
| 409 | } | ||
| 410 | |||
| 411 | do { | ||
| 412 | 20887569 | r = lbm_channel_peek(chan, 0, &c); | |
| 413 |
2/2✓ Branch 0 taken 81 times.
✓ Branch 1 taken 20887488 times.
|
20887569 | if (r == CHANNEL_MORE) { |
| 414 | 81 | return false; | |
| 415 |
2/2✓ Branch 0 taken 43737 times.
✓ Branch 1 taken 20843751 times.
|
20887488 | } else if (r == CHANNEL_END) { |
| 416 | 43737 | return true; | |
| 417 | } | ||
| 418 |
2/2✓ Branch 0 taken 14039 times.
✓ Branch 1 taken 20829712 times.
|
20843751 | if (c == ';') { |
| 419 | 14039 | lbm_channel_set_comment(chan, true); | |
| 420 | 14039 | break; | |
| 421 | } | ||
| 422 |
2/2✓ Branch 0 taken 9245980 times.
✓ Branch 1 taken 11583732 times.
|
20829712 | if (isspace(c)) { |
| 423 | 9245980 | lbm_channel_drop(chan,1); | |
| 424 | } else { | ||
| 425 | 11583732 | cleaning_whitespace = false; | |
| 426 | } | ||
| 427 | |||
| 428 |
2/2✓ Branch 0 taken 9245980 times.
✓ Branch 1 taken 11583732 times.
|
20829712 | } while (cleaning_whitespace); |
| 429 | } | ||
| 430 | 11583732 | return true; | |
| 431 | } | ||
| 432 | |||
| 433 | 8600350 | int tok_integer(lbm_char_channel_t *chan, token_int *result) { | |
| 434 | 8600350 | uint64_t acc = 0; | |
| 435 | 8600350 | unsigned int n = 0; | |
| 436 | 8600350 | bool valid_num = false; | |
| 437 | char c; | ||
| 438 | int res; | ||
| 439 | |||
| 440 | 8600350 | result->type = TOKTYPEI; | |
| 441 | 8600350 | result-> negative = false; | |
| 442 | 8600350 | res = lbm_channel_peek(chan, 0, &c); | |
| 443 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8600350 times.
|
8600350 | if (res == CHANNEL_MORE) { |
| 444 | ✗ | return TOKENIZER_NEED_MORE; | |
| 445 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8600350 times.
|
8600350 | } else if (res == CHANNEL_END) { |
| 446 | ✗ | return TOKENIZER_NO_TOKEN; | |
| 447 | } | ||
| 448 |
2/2✓ Branch 0 taken 10869 times.
✓ Branch 1 taken 8589481 times.
|
8600350 | if (c == '-') { |
| 449 | 10869 | n = 1; | |
| 450 | 10869 | result->negative = true; | |
| 451 | } | ||
| 452 | |||
| 453 | 8600350 | bool hex = false; | |
| 454 | 8600350 | res = lbm_channel_peek(chan, n, &c); | |
| 455 |
3/4✓ Branch 0 taken 8600350 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60368 times.
✓ Branch 3 taken 8539982 times.
|
8600350 | if (res == CHANNEL_SUCCESS && c == '0') { |
| 456 | 60368 | res = lbm_channel_peek(chan, n + 1, &c); | |
| 457 |
4/6✓ Branch 0 taken 60368 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28428 times.
✓ Branch 3 taken 31940 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 28428 times.
|
60368 | if ( res == CHANNEL_SUCCESS && (c == 'x' || c == 'X')) { |
| 458 | 31940 | hex = true; | |
| 459 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28428 times.
|
28428 | } else if (res == CHANNEL_MORE) { |
| 460 | ✗ | return TOKENIZER_NEED_MORE; | |
| 461 | } | ||
| 462 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8539982 times.
|
8539982 | } else if (res == CHANNEL_MORE) { |
| 463 | ✗ | return TOKENIZER_NEED_MORE; | |
| 464 | } | ||
| 465 | |||
| 466 |
2/2✓ Branch 0 taken 31940 times.
✓ Branch 1 taken 8568410 times.
|
8600350 | if (hex) { |
| 467 | 31940 | n += 2; | |
| 468 | |||
| 469 | 31940 | res = lbm_channel_peek(chan,n, &c); | |
| 470 | |||
| 471 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 31938 times.
|
31940 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 472 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31938 times.
|
31938 | else if (res == CHANNEL_END) return TOKENIZER_NO_TOKEN; |
| 473 | |||
| 474 |
2/2✓ Branch 0 taken 53944 times.
✓ Branch 1 taken 21210 times.
|
107092 | while ((c >= '0' && c <= '9') || |
| 475 |
6/6✓ Branch 0 taken 75154 times.
✓ Branch 1 taken 31255 times.
✓ Branch 2 taken 580 times.
✓ Branch 3 taken 51885 times.
✓ Branch 4 taken 66 times.
✓ Branch 5 taken 514 times.
|
127619 | (c >= 'a' && c <= 'f') || |
| 476 |
4/4✓ Branch 0 taken 21144 times.
✓ Branch 1 taken 31255 times.
✓ Branch 2 taken 20462 times.
✓ Branch 3 taken 682 times.
|
52399 | (c >= 'A' && c <= 'F')) { |
| 477 | uint32_t val; /* values between 0 and 16 */ | ||
| 478 |
3/4✓ Branch 0 taken 66 times.
✓ Branch 1 taken 74406 times.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
|
74472 | if (c >= 'a' && c <= 'f') { |
| 479 | 66 | val = 10 + (uint32_t)c - 'a'; | |
| 480 |
3/4✓ Branch 0 taken 20462 times.
✓ Branch 1 taken 53944 times.
✓ Branch 2 taken 20462 times.
✗ Branch 3 not taken.
|
74406 | } else if (c >= 'A' && c <= 'F') { |
| 481 | 20462 | val = 10 + (uint32_t)(c - 'A'); | |
| 482 | } else { | ||
| 483 | 53944 | val = (uint32_t)c - '0'; | |
| 484 | } | ||
| 485 | 74472 | acc = (acc * 0x10) + val; | |
| 486 | 74472 | n++; | |
| 487 | 74472 | res = lbm_channel_peek(chan, n, &c); | |
| 488 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 74471 times.
|
74472 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 489 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74471 times.
|
74471 | if (res == CHANNEL_END) break; |
| 490 | |||
| 491 | } | ||
| 492 | } else { | ||
| 493 | 8568410 | res = lbm_channel_peek(chan, n, &c); | |
| 494 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8568410 times.
|
8568410 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 495 |
4/4✓ Branch 0 taken 10344129 times.
✓ Branch 1 taken 6723962 times.
✓ Branch 2 taken 8499802 times.
✓ Branch 3 taken 1844327 times.
|
17068091 | while (c >= '0' && c <= '9') { |
| 496 | 8499802 | acc = (acc*10) + (uint32_t)(c - '0'); | |
| 497 | 8499802 | n++; | |
| 498 | 8499802 | res = lbm_channel_peek(chan, n, &c); | |
| 499 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8499802 times.
|
8499802 | if (res == CHANNEL_MORE) return TOKENIZER_NEED_MORE; |
| 500 |
2/2✓ Branch 0 taken 121 times.
✓ Branch 1 taken 8499681 times.
|
8499802 | if (res == CHANNEL_END) break; |
| 501 | } | ||
| 502 | } | ||
| 503 | |||
| 504 |
2/2✓ Branch 0 taken 1855297 times.
✓ Branch 1 taken 6745050 times.
|
8600347 | if (n == 0) return TOKENIZER_NO_TOKEN; |
| 505 | |||
| 506 | uint32_t tok_res; | ||
| 507 | 6745050 | int type_len = tok_match_fixed_size_tokens(chan, type_qual_table, n, NUM_TYPE_QUALIFIERS, &tok_res); | |
| 508 | |||
| 509 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6745050 times.
|
6745050 | if (type_len == TOKENIZER_NEED_MORE) return type_len; |
| 510 |
2/2✓ Branch 0 taken 43219 times.
✓ Branch 1 taken 6701831 times.
|
6745050 | if (type_len != TOKENIZER_NO_TOKEN) { |
| 511 | 43219 | result->type = tok_res; | |
| 512 | } | ||
| 513 | |||
| 514 |
4/4✓ Branch 0 taken 10869 times.
✓ Branch 1 taken 6734181 times.
✓ Branch 2 taken 7200 times.
✓ Branch 3 taken 3669 times.
|
6745050 | if ((result->negative && n > 1) || |
| 515 |
2/2✓ Branch 0 taken 6734181 times.
✓ Branch 1 taken 7200 times.
|
6745050 | !result->negative) valid_num = true; |
| 516 | |||
| 517 |
2/2✓ Branch 0 taken 6737850 times.
✓ Branch 1 taken 7200 times.
|
6745050 | if (valid_num) { |
| 518 | 6737850 | result->value = acc; | |
| 519 | 6737850 | return (int)n + type_len; | |
| 520 | } | ||
| 521 | 7200 | return TOKENIZER_NO_TOKEN; | |
| 522 | } | ||
| 523 |