Ver Fonte

fix compilation on non-C99 compilers

Sean Barrett há 8 anos atrás
pai
commit
83a2489d5b
2 ficheiros alterados com 61 adições e 12 exclusões
  1. 14 12
      stb_c_lexer.h
  2. 47 0
      tests/c_lexer_test.c

+ 14 - 12
stb_c_lexer.h

@@ -727,18 +727,20 @@ int stb_c_lexer_get_token(stb_lexer *lexer)
                   #ifdef STB__CLEX_use_stdlib
                   lexer->int_number = strtol((char *) p, (char **) &q, 16);
                   #else
-                  stb__clex_int n=0;
-                  for (q=p+2; q != lexer->eof; ++q) {
-                     if (*q >= '0' && *q <= '9')
-                        n = n*16 + (*q - '0');
-                     else if (*q >= 'a' && *q <= 'f')
-                        n = n*16 + (*q - 'a') + 10;
-                     else if (*q >= 'A' && *q <= 'F')
-                        n = n*16 + (*q - 'A') + 10;
-                     else
-                        break;
+                  {
+                     stb__clex_int n=0;
+                     for (q=p+2; q != lexer->eof; ++q) {
+                        if (*q >= '0' && *q <= '9')
+                           n = n*16 + (*q - '0');
+                        else if (*q >= 'a' && *q <= 'f')
+                           n = n*16 + (*q - 'a') + 10;
+                        else if (*q >= 'A' && *q <= 'F')
+                           n = n*16 + (*q - 'A') + 10;
+                        else
+                           break;
+                     }
+                     lexer->int_number = n;
                   }
-                  lexer->int_number = n;
                   #endif
                   if (q == p+2)
                      return stb__clex_token(lexer, CLEX_parse_error, p-2,p-1);
@@ -880,7 +882,7 @@ void dummy(void)
 {
    double some_floats[] = {
       1.0501, -10.4e12, 5E+10,
-#ifdef STB__clex_hex_floats
+#if 0   // not support in C++ or C-pre-99, so don't try to compile it
       0x1.0p+24, 0xff.FP-8, 0x1p-23,
 #endif
       4.

+ 47 - 0
tests/c_lexer_test.c

@@ -1,3 +1,50 @@
+#define STB_C_LEX_C_DECIMAL_INTS    Y   //  "0|[1-9][0-9]*"                        CLEX_intlit
+#define STB_C_LEX_C_HEX_INTS        Y   //  "0x[0-9a-fA-F]+"                       CLEX_intlit
+#define STB_C_LEX_C_OCTAL_INTS      Y   //  "[0-7]+"                               CLEX_intlit
+#define STB_C_LEX_C_DECIMAL_FLOATS  Y   //  "[0-9]*(.[0-9]*([eE][-+]?[0-9]+)?)     CLEX_floatlit
+#define STB_C_LEX_C99_HEX_FLOATS    Y   //  "0x{hex}+(.{hex}*)?[pP][-+]?{hex}+     CLEX_floatlit
+#define STB_C_LEX_C_IDENTIFIERS     Y   //  "[_a-zA-Z][_a-zA-Z0-9]*"               CLEX_id
+#define STB_C_LEX_C_DQ_STRINGS      Y   //  double-quote-delimited strings with escapes  CLEX_dqstring
+#define STB_C_LEX_C_SQ_STRINGS      Y   //  single-quote-delimited strings with escapes  CLEX_ssstring
+#define STB_C_LEX_C_CHARS           Y   //  single-quote-delimited character with escape CLEX_charlits
+#define STB_C_LEX_C_COMMENTS        Y   //  "/* comment */"
+#define STB_C_LEX_CPP_COMMENTS      Y   //  "// comment to end of line\n"
+#define STB_C_LEX_C_COMPARISONS     Y   //  "==" CLEX_eq  "!=" CLEX_noteq   "<=" CLEX_lesseq  ">=" CLEX_greatereq
+#define STB_C_LEX_C_LOGICAL         Y   //  "&&"  CLEX_andand   "||"  CLEX_oror
+#define STB_C_LEX_C_SHIFTS          Y   //  "<<"  CLEX_shl      ">>"  CLEX_shr
+#define STB_C_LEX_C_INCREMENTS      Y   //  "++"  CLEX_plusplus "--"  CLEX_minusminus
+#define STB_C_LEX_C_ARROW           Y   //  "->"  CLEX_arrow
+#define STB_C_LEX_EQUAL_ARROW       Y   //  "=>"  CLEX_eqarrow
+#define STB_C_LEX_C_BITWISEEQ       Y   //  "&="  CLEX_andeq    "|="  CLEX_oreq     "^="  CLEX_xoreq
+#define STB_C_LEX_C_ARITHEQ         Y   //  "+="  CLEX_pluseq   "-="  CLEX_minuseq
+                                        //  "*="  CLEX_muleq    "/="  CLEX_diveq    "%=" CLEX_modeq
+                                        //  if both STB_C_LEX_SHIFTS & STB_C_LEX_ARITHEQ:
+                                        //                      "<<=" CLEX_shleq    ">>=" CLEX_shreq
+
+#define STB_C_LEX_PARSE_SUFFIXES    Y   // letters after numbers are parsed as part of those numbers, and must be in suffix list below
+#define STB_C_LEX_DECIMAL_SUFFIXES  "uUlL"  // decimal integer suffixes e.g. "uUlL" -- these are returned as-is in string storage
+#define STB_C_LEX_HEX_SUFFIXES      "lL"  // e.g. "uUlL"
+#define STB_C_LEX_OCTAL_SUFFIXES    "lL"  // e.g. "uUlL"
+#define STB_C_LEX_FLOAT_SUFFIXES    "uulL"  //
+
+#define STB_C_LEX_0_IS_EOF             N  // if Y, ends parsing at '\0'; if N, returns '\0' as token
+#define STB_C_LEX_INTEGERS_AS_DOUBLES  N  // parses integers as doubles so they can be larger than 'int', but only if STB_C_LEX_STDLIB==N
+#define STB_C_LEX_MULTILINE_DSTRINGS   Y  // allow newlines in double-quoted strings
+#define STB_C_LEX_MULTILINE_SSTRINGS   Y  // allow newlines in single-quoted strings
+#define STB_C_LEX_USE_STDLIB           N  // use strtod,strtol for parsing #s; otherwise inaccurate hack
+#define STB_C_LEX_DOLLAR_IDENTIFIER    Y  // allow $ as an identifier character
+#define STB_C_LEX_FLOAT_NO_DECIMAL     Y  // allow floats that have no decimal point if they have an exponent
+
+#define STB_C_LEX_DEFINE_ALL_TOKEN_NAMES  Y   // if Y, all CLEX_ token names are defined, even if never returned
+                                              // leaving it as N should help you catch config bugs
+
+#define STB_C_LEX_DISCARD_PREPROCESSOR    Y   // discard C-preprocessor directives (e.g. after prepocess
+                                              // still have #line, #pragma, etc)
+
+#define STB_C_LEXER_DEFINITIONS         // This line prevents the header file from replacing your definitions
+
+
+
 #define STB_C_LEXER_IMPLEMENTATION
 #define STB_C_LEXER_SELF_TEST
 #include "../stb_c_lexer.h"