MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

make highlight lexer dry

+31 -53
+19
include/silver/lexer.h
··· 53 53 bool is_space(int c); 54 54 bool is_digit(int c); 55 55 56 + bool is_ident_begin(int c); 57 + bool is_ident_continue(int c); 58 + 56 59 bool is_eval_or_arguments_name(const char *buf, size_t len); 57 60 bool is_strict_reserved_name(const char *buf, size_t len); 61 + 62 + #define CHAR_DIGIT 0x01 63 + #define CHAR_XDIGIT 0x02 64 + #define CHAR_ALPHA 0x04 65 + #define CHAR_IDENT 0x08 66 + #define CHAR_IDENT1 0x10 67 + #define CHAR_WS 0x20 68 + #define CHAR_OCTAL 0x40 69 + 70 + extern const uint8_t char_type[256]; 71 + 72 + #define IS_DIGIT(c) (char_type[(uint8_t)(c)] & CHAR_DIGIT) 73 + #define IS_XDIGIT(c) (char_type[(uint8_t)(c)] & CHAR_XDIGIT) 74 + #define IS_IDENT(c) (char_type[(uint8_t)(c)] & CHAR_IDENT) 75 + #define IS_IDENT1(c) (char_type[(uint8_t)(c)] & CHAR_IDENT1) 76 + #define IS_OCTAL(c) (char_type[(uint8_t)(c)] & CHAR_OCTAL) 58 77 59 78 #endif
+9 -36
src/highlight.c
··· 92 92 l_null: return HL_LITERAL_NULL; 93 93 } 94 94 95 - static inline bool hl_is_ident1(unsigned char c) { 96 - return 97 - (c >= 'a' && c <= 'z') 98 - || (c >= 'A' && c <= 'Z') 99 - || c == '_' 100 - || c == '$' 101 - || c >= 0x80; 102 - } 103 - 104 - // TODO: dry 105 - static inline bool hl_is_ident(unsigned char c) { 106 - return hl_is_ident1(c) || (c >= '0' && c <= '9'); 107 - } 108 - 109 - static inline bool hl_is_digit(unsigned char c) { 110 - return c >= '0' && c <= '9'; 111 - } 112 - 113 - static inline bool hl_is_xdigit(unsigned char c) { 114 - return hl_is_digit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); 115 - } 116 - 117 - static inline bool hl_is_bindigit(unsigned char c) { return c == '0' || c == '1'; } 118 - static inline bool hl_is_octdigit(unsigned char c) { return c >= '0' && c <= '7'; } 119 - 120 95 void hl_iter_init(hl_iter *it, const char *input, size_t input_len, const highlight_state *state) { 121 96 it->input = input; 122 97 it->input_len = input_len; ··· 268 243 return true; 269 244 } 270 245 271 - if (hl_is_digit(c) || (c == '.' && i + 1 < input_len && hl_is_digit((unsigned char)input[i + 1]))) { 246 + if (IS_DIGIT(c) || (c == '.' && i + 1 < input_len && IS_DIGIT(input[i + 1]))) { 272 247 it->ctx = HL_CTX_NONE; 273 248 size_t start = i; 274 249 if (c == '0' && i + 1 < input_len) { 275 250 unsigned char next = (unsigned char)input[i + 1]; 276 251 if (next == 'x' || next == 'X') { 277 252 i += 2; 278 - while (i < input_len && (hl_is_xdigit((unsigned char)input[i]) || input[i] == '_')) i++; 253 + while (i < input_len && (IS_XDIGIT(input[i]) || input[i] == '_')) i++; 279 254 goto num_done; 280 255 } else if (next == 'b' || next == 'B') { 281 256 i += 2; 282 - while (i < input_len && (hl_is_bindigit((unsigned char)input[i]) || input[i] == '_')) i++; 257 + while (i < input_len && (input[i] == '0' || input[i] == '1' || input[i] == '_')) i++; 283 258 goto num_done; 284 259 } else if (next == 'o' || next == 'O') { 285 260 i += 2; 286 - while (i < input_len && (hl_is_octdigit((unsigned char)input[i]) || input[i] == '_')) i++; 261 + while (i < input_len && (IS_OCTAL(input[i]) || input[i] == '_')) i++; 287 262 goto num_done; 288 263 } 289 264 } 290 - while (i < input_len && (hl_is_digit((unsigned char)input[i]) || input[i] == '_')) i++; 265 + while (i < input_len && (IS_DIGIT(input[i]) || input[i] == '_')) i++; 291 266 if (i < input_len && input[i] == '.') { 292 267 i++; 293 - while (i < input_len && (hl_is_digit((unsigned char)input[i]) || input[i] == '_')) i++; 268 + while (i < input_len && (IS_DIGIT(input[i]) || input[i] == '_')) i++; 294 269 } 295 270 if (i < input_len && (input[i] == 'e' || input[i] == 'E')) { 296 271 i++; 297 272 if (i < input_len && (input[i] == '+' || input[i] == '-')) i++; 298 - while (i < input_len && (hl_is_digit((unsigned char)input[i]) || input[i] == '_')) i++; 273 + while (i < input_len && (IS_DIGIT(input[i]) || input[i] == '_')) i++; 299 274 } 300 275 num_done: 301 276 if (i < input_len && input[i] == 'n') i++; ··· 315 290 } 316 291 } 317 292 318 - if (hl_is_ident1(c)) { 293 + if (is_ident_begin(c)) { 319 294 size_t start = i; 320 295 i++; 321 - while (i < input_len && hl_is_ident((unsigned char)input[i])) i++; 296 + while (i < input_len && is_ident_continue(input[i])) i++; 322 297 size_t word_len = i - start; 323 298 const char *word = input + start; 324 299 ··· 563 538 o.buf[o.pos] = '\0'; 564 539 return (int)o.pos; 565 540 } 566 - 567 -
+3 -17
src/silver/lexer.c
··· 325 325 #undef K 326 326 #undef M 327 327 328 - #define CHAR_DIGIT 0x01 329 - #define CHAR_XDIGIT 0x02 330 - #define CHAR_ALPHA 0x04 331 - #define CHAR_IDENT 0x08 332 - #define CHAR_IDENT1 0x10 333 - #define CHAR_WS 0x20 334 - #define CHAR_OCTAL 0x40 335 - 336 - static const uint8_t char_type[256] = { 328 + const uint8_t char_type[256] = { 337 329 ['\t'] = CHAR_WS, ['\n'] = CHAR_WS, ['\r'] = CHAR_WS, [' '] = CHAR_WS, 338 330 ['0'] = CHAR_DIGIT | CHAR_XDIGIT | CHAR_IDENT | CHAR_OCTAL, 339 331 ['1'] = CHAR_DIGIT | CHAR_XDIGIT | CHAR_IDENT | CHAR_OCTAL, ··· 381 373 ['$'] = CHAR_IDENT | CHAR_IDENT1, 382 374 }; 383 375 384 - #define IS_DIGIT(c) (char_type[(uint8_t)(c)] & CHAR_DIGIT) 385 - #define IS_XDIGIT(c) (char_type[(uint8_t)(c)] & CHAR_XDIGIT) 386 - #define IS_IDENT(c) (char_type[(uint8_t)(c)] & CHAR_IDENT) 387 - #define IS_IDENT1(c) (char_type[(uint8_t)(c)] & CHAR_IDENT1) 388 - #define IS_OCTAL(c) (char_type[(uint8_t)(c)] & CHAR_OCTAL) 389 - 390 376 #define likely(x) __builtin_expect(!!(x), 1) 391 377 #define unlikely(x) __builtin_expect(!!(x), 0) 392 378 ··· 414 400 return (char_type[(uint8_t)c] & CHAR_DIGIT) != 0; 415 401 } 416 402 417 - static bool is_ident_begin(int c) { 403 + bool is_ident_begin(int c) { 418 404 if (c < 0) return false; 419 405 if (c < 128) return (char_type[(uint8_t)c] & CHAR_IDENT1) != 0; 420 406 return (c & 0x80) != 0; 421 407 } 422 408 423 - static bool is_ident_continue(int c) { 409 + bool is_ident_continue(int c) { 424 410 if (c < 0) return false; 425 411 if (c < 128) return (char_type[(uint8_t)c] & (CHAR_IDENT | CHAR_IDENT1)) != 0; 426 412 return (c & 0x80) != 0;