Skip to content
Myr

Merge branch 'master' of https://github.com/fahickman/stb into working

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

authored by Sean Barrett · committed by Sean Barrett · parents e77673f, 49d3871 · Browse files · 1 file+132 added−35 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+132 added−35 removed

  • other:+132 added−35 removed

1 file+132 added−35 removed

modified:stb_c_lexer.h+132 added-35 removed
45 unmodified lines
4647#define STB_C_LEX_C_DECIMAL_INTS Y // "0|[1-9][0-9]*" CLEX_intlit48#define STB_C_LEX_C_HEX_INTS Y // "0x[0-9a-fA-F]+" CLEX_intlit49#define STB_C_LEX_C_OCTAL_INTS Y // "[0-7]+" CLEX_intlit50#define STB_C_LEX_C_DECIMAL_FLOATS Y // "[0-9]*(.[0-9]*([eE]-?[0-9]+)?) CLEX_floatlit50#define STB_C_LEX_C_DECIMAL_FLOATS Y // "[0-9]*(.[0-9]*([eE][-+]?[0-9]+)?) CLEX_floatlit51#define STB_C_LEX_C99_HEX_FLOATS N // "0x{hex}+(.{hex}*)?[pP][-+]?{hex}+ CLEX_floatlit52#define STB_C_LEX_C_IDENTIFIERS Y // "[_a-zA-Z][_a-zA-Z0-9]*" CLEX_id53#define STB_C_LEX_C_DQ_STRINGS Y // double-quote-delimited strings with escapes CLEX_dqstring54#define STB_C_LEX_C_SQ_STRINGS N // single-quote-delimited strings with escapes CLEX_ssstring55#define STB_C_LEX_C_CHARS Y // single-quote-delimited character with escape CLEX_charlits
117 unmodified lines
173// Hacky definitions so we can easily #if on them174#define Y(x) 1175#define N(x) 0176176#if STB_C_LEX_USE_STDLIB(x)177#define STB__CLEX_use_stdlib178#include <stdlib.h>179#endif180177#if STB_C_LEX_INTEGERS_AS_DOUBLES(x)178typedef double stb__clex_int;179#define intfield real_number180#define STB__clex_int_as_double
16 unmodified lines
197#if (STB_C_LEX_C_ARITHEQ(x) && STB_C_LEX_C_SHIFTS(x)) || STB_C_LEX_DEFINE_ALL_TOKEN_NAMES(x)198#define STB__clex_define_shifts199#endif200201#if STB_C_LEX_C99_HEX_FLOATS(x)202#define STB__clex_hex_floats203#endif204205#if STB_C_LEX_C_HEX_INTS(x)206#define STB__clex_hex_ints207#endif208
12 unmodified lines
221#if STB_C_LEX_DISCARD_PREPROCESSOR(x)222#define STB__clex_discard_preprocessor223#endif224225#if STB_C_LEX_USE_STDLIB(x) && (!defined(STB__clex_hex_floats) || __STDC_VERSION__ >= 199901L)226#define STB__CLEX_use_stdlib227#include <stdlib.h>228#endif229230// Now pick a definition of Y/N that's conducive to231// defining the enum of token names.232#if STB_C_LEX_DEFINE_ALL_TOKEN_NAMES(x) || defined(STB_C_LEXER_SELF_TEST)233 #undef N
136 unmodified lines
370 return stb__clex_token(lexer, tokenid, start, cur-1);371}372373#ifndef STB__CLEX_use_stdlib374static double stb__clex_pow(double base, unsigned int exponent)375{376 double value=1;377 for ( ; exponent; exponent >>= 1) {378 if (exponent & 1)379 value *= base;380 base *= base;381 }382 return value;383}384385static double stb__clex_parse_float(char *p, char **q)386{387 char *s = p;388 double value=0;389 int base=10;390 int exponent=0;391392#ifdef STB__clex_hex_floats393 if (*p == '0') {394 if (p[1] == 'x' || p[1] == 'X') {395 base=16;396 p += 2;397 }398 }399#endif400401 for (;;) {372 while (*p >= '0' && *p <= '9')373 value = value*10 + (*p++ - '0');402 if (*p >= '0' && *p <= '9')403 value = value*base + (*p++ - '0');404#ifdef STB__clex_hex_floats405 else if (base == 16 && *p >= 'a' && *p <= 'f')406 value = value*base + 10 + (*p++ - 'a');407 else if (base == 16 && *p >= 'A' && *p <= 'F')408 value = value*base + 10 + (*p++ - 'A');409#endif410 else411 break;412 }413414 if (*p == '.') {375 double powten=1, addend = 0;415 double pow, addend = 0;416 ++p;377 while (*p >= '0' && *p <= '9') {378 addend = addend + 10*(*p++ - '0');379 powten *= 10;417 for (pow=1; ; pow*=base) {418 if (*p >= '0' && *p <= '9')419 addend = addend*base + (*p++ - '0');420#ifdef STB__clex_hex_floats421 else if (base == 16 && *p >= 'a' && *p <= 'f')422 addend = addend*base + 10 + (*p++ - 'a');423 else if (base == 16 && *p >= 'A' && *p <= 'F')424 addend = addend*base + 10 + (*p++ - 'A');425#endif426 else427 break;428 }381 value += addend / powten;429 value += addend / pow;430 }383 if (*p == 'e' || *p == 'E') {431#ifdef STB__clex_hex_floats432 if (base == 16) {433 // exponent required for hex float literal434 if (*p != 'p' && *p != 'P') {435 *q = s;436 return 0;437 }438 exponent = 1;439 } else440#endif441 exponent = (*p == 'e' || *p == 'E');442443 if (exponent) {444 int sign = p[1] == '-';385 int exponent=0;386 double pow10=1;387 p += 1+sign;445 unsigned int exponent=0;446 double power=1;447 ++p;448 if (*p == '-' || *p == '+')449 ++p;450 while (*p >= '0' && *p <= '9')451 exponent = exponent*10 + (*p++ - '0');390 // can't use pow() from stdlib, so do it slow way391 while (exponent-- > 0)392 pow10 *= 10;452453#ifdef STB__clex_hex_floats454 if (base == 16)455 power = stb__clex_pow(2, exponent);456 else457#endif458 power = stb__clex_pow(10, exponent);459 if (sign)394 value /= pow10;460 value /= power;461 else396 value *= pow10;462 value *= power;463 }464 *q = p;465 return value;466}
230 unmodified lines
697 })698 goto single_char;699700 case '0':635 #ifdef STB__clex_hex_ints701 #if defined(STB__clex_hex_ints) || defined(STB__clex_hex_floats)702 if (p+1 != lexer->eof) {703 if (p[1] == 'x' || p[1] == 'X') {638 char *q = p+2;704 char *q;705706 #ifdef STB__clex_hex_floats707 for (q=p+2;708 q != lexer->eof && ((*q >= '0' && *q <= '9') || (*q >= 'a' && *q <= 'f') || (*q >= 'A' && *q <= 'F'));709 ++q);710 if (q != lexer->eof) {711 if (*q == '.' STB_C_LEX_FLOAT_NO_DECIMAL(|| *q == 'p' || *q == 'P')) {712 #ifdef STB__CLEX_use_stdlib713 lexer->real_number = strtod((char *) p, (char**) &q);714 #else715 lexer->real_number = stb__clex_parse_float(p, &q);716 #endif717718 if (p == q)719 return stb__clex_token(lexer, CLEX_parse_error, p,q);720 return stb__clex_parse_suffixes(lexer, CLEX_floatlit, p,q, STB_C_LEX_FLOAT_SUFFIXES);721722 }723 }724 #endif // STB__CLEX_hex_floats725726 #ifdef STB__clex_hex_ints727 #ifdef STB__CLEX_use_stdlib728 lexer->int_number = strtol((char *) p, (char **) &q, 16);729 #else730 stb__clex_int n=0;643 while (q != lexer->eof) {731 for (q=p+2; q != lexer->eof; ++q) {732 if (*q >= '0' && *q <= '9')733 n = n*16 + (*q - '0');734 else if (*q >= 'a' && *q <= 'f')735 n = n*16 + (*q - 'a') + 10;736 else if (*q >= 'A' && *q <= 'F')737 n = n*16 + (*q - 'A') + 10;738 else739 break;652 ++q;740 }654 lexer->int_field = n; // int_field is macro that expands to real_number/int_number depending on type of n741 lexer->int_number = n;742 #endif743 if (q == p+2)744 return stb__clex_token(lexer, CLEX_parse_error, p-2,p-1);745 return stb__clex_parse_suffixes(lexer, CLEX_intlit, p,q, STB_C_LEX_HEX_SUFFIXES);746 #endif747 }748 }661 #endif // STB__clex_hex_ints749 #endif // defined(STB__clex_hex_ints) || defined(STB__clex_hex_floats)750 // can't test for octal because we might parse '0.0' as float or as '0' '.' '0',751 // so have to do float first752753 /* FALL THROUGH */
27 unmodified lines
781 #else782 stb__clex_int n=0;783 while (q != lexer->eof) {784 if (*q >= '0' && *q <= '7')697 n = n*8 + (q - '0');785 n = n*8 + (*q - '0');786 else787 break;788 ++q;789 }790 if (q != lexer->eof && (*q == '8' || *q=='9'))703 return stb__clex_token(tok, CLEX_parse_error, p, q);704 lexer->int_field = n;791 return stb__clex_token(lexer, CLEX_parse_error, p, q);792 lexer->int_number = n;793 #endif794 return stb__clex_parse_suffixes(lexer, CLEX_intlit, p,q, STB_C_LEX_OCTAL_SUFFIXES);795 }796 #endif // STB__clex_octal_ints
6 unmodified lines
803 #else804 stb__clex_int n=0;805 while (q != lexer->eof) {806 if (*q >= '0' && *q <= '9')719 n = n*10 + (q - '0');807 n = n*10 + (*q - '0');808 else809 break;810 ++q;811 }724 lexer->int_field = n;812 lexer->int_number = n;813 #endif814 return stb__clex_parse_suffixes(lexer, CLEX_intlit, p,q, STB_C_LEX_OCTAL_SUFFIXES);815 }816 #endif // STB__clex_decimal_ints
4 unmodified lines
821822#ifdef STB_C_LEXER_SELF_TEST823824#include <stdio.h>825#include <stdlib.h>826827static void print_token(stb_lexer *lexer)828{829 switch (lexer->token) {
47 unmodified lines
877/**/ extern /**/878879void dummy(void)880{881 double some_floats[] = {882 1.0501, -10.4e12, 5E+10,883#ifdef STB__clex_hex_floats884 0x1.0p+24, 0xff.FP-8, 0x1p-23,885#endif886 4.887 };888792 printf("test",1); // https://github.com/nothings/stb/issues/13889 printf("test %d",1); // https://github.com/nothings/stb/issues/13890}891892int main(int argc, char **argv)893{
8 unmodified lines
902 return 1;903 }904 fclose(f);905809 stb_c_lexer_init(&lex, text, text+len, (char *) malloc(1<<16), 1<<16);906 stb_c_lexer_init(&lex, text, text+len, (char *) malloc(0x10000), 0x10000);907 while (stb_c_lexer_get_token(&lex)) {908 if (lex.token == CLEX_parse_error) {909 printf("\n<<<PARSE ERROR>>>\n");910 break;
48 unmodified lines