Skip to content
Myr

stb_c_lexer.h: C99 hex float literals

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Add support for C99 hex float literals * + is acceptable in a floating-point exponent * log(n) implementation of pow for stb__clex_parse_float * Add hex int and float test cases

authored by Alan Hickman · committed by fahickman · parent c6b6239 · Browse files · 1 file+124 added−28 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+124 added−28 removed

  • other:+124 added−28 removed

1 file+124 added−28 removed

modified:stb_c_lexer.h+124 added-28 removed
43 unmodified lines
4445#define STB_C_LEX_C_DECIMAL_INTS Y // "0|[1-9][0-9]*" CLEX_intlit46#define STB_C_LEX_C_HEX_INTS Y // "0x[0-9a-fA-F]+" CLEX_intlit47#define STB_C_LEX_C_OCTAL_INTS Y // "[0-7]+" CLEX_intlit48#define STB_C_LEX_C_DECIMAL_FLOATS Y // "[0-9]*(.[0-9]*([eE]-?[0-9]+)?) CLEX_floatlit48#define STB_C_LEX_C_DECIMAL_FLOATS Y // "[0-9]*(.[0-9]*([eE][-+]?[0-9]+)?) CLEX_floatlit49#define STB_C_LEX_C99_HEX_FLOATS N // "0x{hex}+(.{hex}*)?[pP][-+]?{hex}+ CLEX_floatlit50#define STB_C_LEX_C_IDENTIFIERS Y // "[_a-zA-Z][_a-zA-Z0-9]*" CLEX_id51#define STB_C_LEX_C_DQ_STRINGS Y // double-quote-delimited strings with escapes CLEX_dqstring52#define STB_C_LEX_C_SQ_STRINGS N // single-quote-delimited strings with escapes CLEX_ssstring53#define STB_C_LEX_C_CHARS Y // single-quote-delimited character with escape CLEX_charlits
117 unmodified lines
171// Hacky definitions so we can easily #if on them172#define Y(x) 1173#define N(x) 0174174#if STB_C_LEX_USE_STDLIB(x)175#define STB__CLEX_use_stdlib176#include <stdlib.h>177#endif178175#if STB_C_LEX_INTEGERS_AS_DOUBLES(x)176typedef double stb__clex_int;177#define intfield real_number178#define STB__clex_int_as_double
16 unmodified lines
195#if (STB_C_LEX_C_ARITHEQ(x) && STB_C_LEX_C_SHIFTS(x)) || STB_C_LEX_DEFINE_ALL_TOKEN_NAMES(x)196#define STB__clex_define_shifts197#endif198199#if STB_C_LEX_C99_HEX_FLOATS(x)200#define STB__clex_hex_floats201#endif202203#if STB_C_LEX_C_HEX_INTS(x)204#define STB__clex_hex_ints205#endif206
12 unmodified lines
219#if STB_C_LEX_DISCARD_PREPROCESSOR(x)220#define STB__clex_discard_preprocessor221#endif222223#if STB_C_LEX_USE_STDLIB(x) && (!defined(STB__clex_hex_floats) || __STDC_VERSION__ >= 199901L)224#define STB__CLEX_use_stdlib225#include <stdlib.h>226#endif227228// Now pick a definition of Y/N that's conducive to229// defining the enum of token names.230#if STB_C_LEX_DEFINE_ALL_TOKEN_NAMES(x) || defined(STB_C_LEXER_SELF_TEST)231 #undef N
136 unmodified lines
368 return stb__clex_token(lexer, tokenid, start, cur-1);369}370371#ifndef STB__CLEX_use_stdlib372static double stb__clex_pow(double base, unsigned int exponent)373{374 double value=1;375 for ( ; exponent; exponent >>= 1) {376 if (exponent & 1)377 value *= base;378 base *= base;379 }380 return value;381}382383static double stb__clex_parse_float(char *p, char **q)384{385 char *s = p;386 double value=0;387 int base=10;388 int exponent=0;389390#ifdef STB__clex_hex_floats391 if (*p == '0') {392 if (p[1] == 'x' || p[1] == 'X') {393 base=16;394 p += 2;395 }396 }397#endif398399 for (;;) {370 while (*p >= '0' && *p <= '9')371 value = value*10 + (*p++ - '0');400 if (*p >= '0' && *p <= '9')401 value = value*base + (*p++ - '0');402#ifdef STB__clex_hex_floats403 else if (base == 16 && *p >= 'a' && *p <= 'f')404 value = value*base + 10 + (*p++ - 'a');405 else if (base == 16 && *p >= 'A' && *p <= 'F')406 value = value*base + 10 + (*p++ - 'A');407#endif408 else409 break;410 }411412 if (*p == '.') {373 double powten=1, addend = 0;413 double pow, addend = 0;414 ++p;375 while (*p >= '0' && *p <= '9') {376 addend = addend*10 + (*p++ - '0');377 powten *= 10;415 for (pow=1; ; pow*=base) {416 if (*p >= '0' && *p <= '9')417 addend = addend*base + (*p++ - '0');418#ifdef STB__clex_hex_floats419 else if (base == 16 && *p >= 'a' && *p <= 'f')420 addend = addend*base + 10 + (*p++ - 'a');421 else if (base == 16 && *p >= 'A' && *p <= 'F')422 addend = addend*base + 10 + (*p++ - 'A');423#endif424 else425 break;426 }379 value += addend / powten;427 value += addend / pow;428 }381 if (*p == 'e' || *p == 'E') {429#ifdef STB__clex_hex_floats430 if (base == 16) {431 // exponent required for hex float literal432 if (*p != 'p' && *p != 'P') {433 *q = s;434 return 0;435 }436 exponent = 1;437 } else438#endif439 exponent = (*p == 'e' || *p == 'E');440441 if (exponent) {442 int sign = p[1] == '-';383 int exponent=0;384 double pow10=1;385 p += 1+sign;443 unsigned int exponent=0;444 double power=1;445 ++p;446 if (*p == '-' || *p == '+')447 ++p;448 while (*p >= '0' && *p <= '9')449 exponent = exponent*10 + (*p++ - '0');388 // can't use pow() from stdlib, so do it slow way389 while (exponent-- > 0)390 pow10 *= 10;450451#ifdef STB__clex_hex_floats452 if (base == 16)453 power = stb__clex_pow(2, exponent);454 else455#endif456 power = stb__clex_pow(10, exponent);457 if (sign)392 value /= pow10;458 value /= power;459 else394 value *= pow10;460 value *= power;461 }462 *q = p;463 return value;464}
230 unmodified lines
695 })696 goto single_char;697698 case '0':633 #ifdef STB__clex_hex_ints699 #if defined(STB__clex_hex_ints) || defined(STB__clex_hex_floats)700 if (p+1 != lexer->eof) {701 if (p[1] == 'x' || p[1] == 'X') {636 char *q = p+2;702 char *q;703704 #ifdef STB__clex_hex_floats705 for (q=p+2;706 q != lexer->eof && ((*q >= '0' && *q <= '9') || (*q >= 'a' && *q <= 'f') || (*q >= 'A' && *q <= 'F'));707 ++q);708 if (q != lexer->eof) {709 if (*q == '.' STB_C_LEX_FLOAT_NO_DECIMAL(|| *q == 'p' || *q == 'P')) {710 #ifdef STB__CLEX_use_stdlib711 lexer->real_number = strtod((char *) p, (char**) &q);712 #else713 lexer->real_number = stb__clex_parse_float(p, &q);714 #endif715716 if (p == q)717 return stb__clex_token(lexer, CLEX_parse_error, p,q);718 return stb__clex_parse_suffixes(lexer, CLEX_floatlit, p,q, STB_C_LEX_FLOAT_SUFFIXES);719720 }721 }722 #endif // STB__CLEX_hex_floats723724 #ifdef STB__clex_hex_ints725 #ifdef STB__CLEX_use_stdlib726 lexer->int_number = strtol((char *) p, (char **) &q, 16);727 #else728 stb__clex_int n=0;641 while (q != lexer->eof) {729 for (q=p+2; q != lexer->eof; ++q) {730 if (*q >= '0' && *q <= '9')731 n = n*16 + (*q - '0');732 else if (*q >= 'a' && *q <= 'f')733 n = n*16 + (*q - 'a') + 10;734 else if (*q >= 'A' && *q <= 'F')735 n = n*16 + (*q - 'A') + 10;736 else737 break;650 ++q;738 }739 lexer->int_number = n;740 #endif741 if (q == p+2)742 return stb__clex_token(lexer, CLEX_parse_error, p-2,p-1);743 return stb__clex_parse_suffixes(lexer, CLEX_intlit, p,q, STB_C_LEX_HEX_SUFFIXES);744 #endif745 }746 }659 #endif // STB__clex_hex_ints747 #endif // defined(STB__clex_hex_ints) || defined(STB__clex_hex_floats)748 // can't test for octal because we might parse '0.0' as float or as '0' '.' '0',749 // so have to do float first750751 /* FALL THROUGH */
123 unmodified lines
875/**/ extern /**/876877void dummy(void)878{879 double some_floats[] = {880 1.0501, -10.4e12, 5E+10,881#ifdef STB__clex_hex_floats882 0x1.0p+24, 0xff.FP-8, 0x1p-23,883#endif884 4.885 };886887 printf("test %d",1); // https://github.com/nothings/stb/issues/13888}889890int main(int argc, char **argv)
7 unmodified lines
898 return 1;899 }900 fclose(f);901806 stb_c_lexer_init(&lex, text, text+len, (char *) malloc(1<<16), 1<<16);902 stb_c_lexer_init(&lex, text, text+len, (char *) malloc(0x10000), 0x10000);903 while (stb_c_lexer_get_token(&lex)) {904 if (lex.token == CLEX_parse_error) {905 printf("\n<<<PARSE ERROR>>>\n");906 break;
7 unmodified lines