stb_c_lexer: allow including stb_c_lexer.h without defining overrides (all tokens are always defined; token values have changed)
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 · parent 0be82e4 · Browse files · 3 files·+77 added−114 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+77 added−114 removed
- tests:+49 added−49 removed
- other:+28 added−65 removed
Provenance
- Commit
fd7807e- Tree
2c5ffd4- Author
Sean Barrett <seanb@radgametools.com>- Committer
Sean Barrett <seanb@radgametools.com>- Parent
0be82e4
3 files changed+77 added−114 removed
modified:tests/test_c_lexer.c+49 added
1 #include "stb_c_lexer.h" 2+3+#define STB_C_LEX_C_DECIMAL_INTS Y // "0|[1-9][0-9]*" CLEX_intlit4+#define STB_C_LEX_C_HEX_INTS Y // "0x[0-9a-fA-F]+" CLEX_intlit5+#define STB_C_LEX_C_OCTAL_INTS Y // "[0-7]+" CLEX_intlit6+#define STB_C_LEX_C_DECIMAL_FLOATS Y // "[0-9]*(.[0-9]*([eE][-+]?[0-9]+)?) CLEX_floatlit7+#define STB_C_LEX_C99_HEX_FLOATS N // "0x{hex}+(.{hex}*)?[pP][-+]?{hex}+ CLEX_floatlit8+#define STB_C_LEX_C_IDENTIFIERS Y // "[_a-zA-Z][_a-zA-Z0-9]*" CLEX_id9+#define STB_C_LEX_C_DQ_STRINGS Y // double-quote-delimited strings with escapes CLEX_dqstring10+#define STB_C_LEX_C_SQ_STRINGS N // single-quote-delimited strings with escapes CLEX_ssstring11+#define STB_C_LEX_C_CHARS Y // single-quote-delimited character with escape CLEX_charlits12+#define STB_C_LEX_C_COMMENTS Y // "/* comment */"13+#define STB_C_LEX_CPP_COMMENTS Y // "// comment to end of line\n"14+#define STB_C_LEX_C_COMPARISONS Y // "==" CLEX_eq "!=" CLEX_noteq "<=" CLEX_lesseq ">=" CLEX_greatereq15+#define STB_C_LEX_C_LOGICAL Y // "&&" CLEX_andand "||" CLEX_oror16+#define STB_C_LEX_C_SHIFTS Y // "<<" CLEX_shl ">>" CLEX_shr17+#define STB_C_LEX_C_INCREMENTS Y // "++" CLEX_plusplus "--" CLEX_minusminus18+#define STB_C_LEX_C_ARROW Y // "->" CLEX_arrow19+#define STB_C_LEX_EQUAL_ARROW N // "=>" CLEX_eqarrow20+#define STB_C_LEX_C_BITWISEEQ Y // "&=" CLEX_andeq "|=" CLEX_oreq "^=" CLEX_xoreq21+#define STB_C_LEX_C_ARITHEQ Y // "+=" CLEX_pluseq "-=" CLEX_minuseq22+ // "*=" CLEX_muleq "/=" CLEX_diveq "%=" CLEX_modeq23+ // if both STB_C_LEX_SHIFTS & STB_C_LEX_ARITHEQ:24+ // "<<=" CLEX_shleq ">>=" CLEX_shreq25+26+#define STB_C_LEX_PARSE_SUFFIXES N // letters after numbers are parsed as part of those numbers, and must be in suffix list below27+#define STB_C_LEX_DECIMAL_SUFFIXES "" // decimal integer suffixes e.g. "uUlL" -- these are returned as-is in string storage28+#define STB_C_LEX_HEX_SUFFIXES "" // e.g. "uUlL"29+#define STB_C_LEX_OCTAL_SUFFIXES "" // e.g. "uUlL"30+#define STB_C_LEX_FLOAT_SUFFIXES "" //31+32+#define STB_C_LEX_0_IS_EOF Y // if Y, ends parsing at '\0'; if N, returns '\0' as token33+#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==N34+#define STB_C_LEX_MULTILINE_DSTRINGS N // allow newlines in double-quoted strings35+#define STB_C_LEX_MULTILINE_SSTRINGS N // allow newlines in single-quoted strings36+#define STB_C_LEX_USE_STDLIB Y // use strtod,strtol for parsing #s; otherwise inaccurate hack37+#define STB_C_LEX_DOLLAR_IDENTIFIER Y // allow $ as an identifier character38+#define STB_C_LEX_FLOAT_NO_DECIMAL Y // allow floats that have no decimal point if they have an exponent39+40+#define STB_C_LEX_DEFINE_ALL_TOKEN_NAMES N // if Y, all CLEX_ token names are defined, even if never returned41+ // leaving it as N should help you catch config bugs42+43+#define STB_C_LEX_DISCARD_PREPROCESSOR Y // discard C-preprocessor directives (e.g. after prepocess44+ // still have #line, #pragma, etc)45+46+//#define STB_C_LEX_ISWHITE(str) ... // return length in bytes of whitespace characters if first char is whitespace47+48+#define STB_C_LEXER_DEFINITIONS // This line prevents the header file from replacing your definitions49+50+#include "stb_c_lexer.h" modified:tests/test_cpp_compilation.cpp-49 removed
181 unmodified lines182 stb_textedit_initialize_state(0,0);183 stb_textedit_paste(0,0,0,0);184 }185 186-#if 0187-#define STB_C_LEX_C_DECIMAL_INTS Y // "0|[1-9][0-9]*" CLEX_intlit188-#define STB_C_LEX_C_HEX_INTS Y // "0x[0-9a-fA-F]+" CLEX_intlit189-#define STB_C_LEX_C_OCTAL_INTS Y // "[0-7]+" CLEX_intlit190-#define STB_C_LEX_C_DECIMAL_FLOATS Y // "[0-9]*(.[0-9]*([eE][-+]?[0-9]+)?) CLEX_floatlit191-#define STB_C_LEX_C99_HEX_FLOATS N // "0x{hex}+(.{hex}*)?[pP][-+]?{hex}+ CLEX_floatlit192-#define STB_C_LEX_C_IDENTIFIERS Y // "[_a-zA-Z][_a-zA-Z0-9]*" CLEX_id193-#define STB_C_LEX_C_DQ_STRINGS Y // double-quote-delimited strings with escapes CLEX_dqstring194-#define STB_C_LEX_C_SQ_STRINGS N // single-quote-delimited strings with escapes CLEX_ssstring195-#define STB_C_LEX_C_CHARS Y // single-quote-delimited character with escape CLEX_charlits196-#define STB_C_LEX_C_COMMENTS Y // "/* comment */"197-#define STB_C_LEX_CPP_COMMENTS Y // "// comment to end of line\n"198-#define STB_C_LEX_C_COMPARISONS Y // "==" CLEX_eq "!=" CLEX_noteq "<=" CLEX_lesseq ">=" CLEX_greatereq199-#define STB_C_LEX_C_LOGICAL Y // "&&" CLEX_andand "||" CLEX_oror200-#define STB_C_LEX_C_SHIFTS Y // "<<" CLEX_shl ">>" CLEX_shr201-#define STB_C_LEX_C_INCREMENTS Y // "++" CLEX_plusplus "--" CLEX_minusminus202-#define STB_C_LEX_C_ARROW Y // "->" CLEX_arrow203-#define STB_C_LEX_EQUAL_ARROW N // "=>" CLEX_eqarrow204-#define STB_C_LEX_C_BITWISEEQ Y // "&=" CLEX_andeq "|=" CLEX_oreq "^=" CLEX_xoreq205-#define STB_C_LEX_C_ARITHEQ Y // "+=" CLEX_pluseq "-=" CLEX_minuseq206- // "*=" CLEX_muleq "/=" CLEX_diveq "%=" CLEX_modeq207- // if both STB_C_LEX_SHIFTS & STB_C_LEX_ARITHEQ:208- // "<<=" CLEX_shleq ">>=" CLEX_shreq209-210-#define STB_C_LEX_PARSE_SUFFIXES N // letters after numbers are parsed as part of those numbers, and must be in suffix list below211-#define STB_C_LEX_DECIMAL_SUFFIXES "" // decimal integer suffixes e.g. "uUlL" -- these are returned as-is in string storage212-#define STB_C_LEX_HEX_SUFFIXES "" // e.g. "uUlL"213-#define STB_C_LEX_OCTAL_SUFFIXES "" // e.g. "uUlL"214-#define STB_C_LEX_FLOAT_SUFFIXES "" //215-216-#define STB_C_LEX_0_IS_EOF Y // if Y, ends parsing at '\0'; if N, returns '\0' as token217-#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==N218-#define STB_C_LEX_MULTILINE_DSTRINGS N // allow newlines in double-quoted strings219-#define STB_C_LEX_MULTILINE_SSTRINGS N // allow newlines in single-quoted strings220-#define STB_C_LEX_USE_STDLIB Y // use strtod,strtol for parsing #s; otherwise inaccurate hack221-#define STB_C_LEX_DOLLAR_IDENTIFIER Y // allow $ as an identifier character222-#define STB_C_LEX_FLOAT_NO_DECIMAL Y // allow floats that have no decimal point if they have an exponent223-224-#define STB_C_LEX_DEFINE_ALL_TOKEN_NAMES N // if Y, all CLEX_ token names are defined, even if never returned225- // leaving it as N should help you catch config bugs226-227-#define STB_C_LEX_DISCARD_PREPROCESSOR Y // discard C-preprocessor directives (e.g. after prepocess228- // still have #line, #pragma, etc)229-230-//#define STB_C_LEX_ISWHITE(str) ... // return length in bytes of whitespace characters if first char is whitespace231-232-#define STB_C_LEXER_DEFINITIONS // This line prevents the header file from replacing your definitions233-#endif234-186 #include "stb_c_lexer.h"modified:stb_c_lexer.h+28 added-65 removed
40 unmodified lines41 // LICENSE42 //43 // See end of file for license information.44 45+#ifdef STB_C_LEXER_IMPLEMENTATION46 #ifndef STB_C_LEXER_DEFINITIONS47 // to change the default parsing rules, copy the following lines48 // into your C/C++ file *before* including this, and then replace49 // the Y's with N's for the ones you don't want. This needs to be4 unmodified lines54 #if defined(Y) || defined(N)55 #error "Can only use stb_c_lexer in contexts where the preprocessor symbols 'Y' and 'N' are not defined"56 #endif57 57-58 #define STB_C_LEX_C_DECIMAL_INTS Y // "0|[1-9][0-9]*" CLEX_intlit59 #define STB_C_LEX_C_HEX_INTS Y // "0x[0-9a-fA-F]+" CLEX_intlit60 #define STB_C_LEX_C_OCTAL_INTS Y // "[0-7]+" CLEX_intlit61 #define STB_C_LEX_C_DECIMAL_FLOATS Y // "[0-9]*(.[0-9]*([eE][-+]?[0-9]+)?) CLEX_floatlit39 unmodified lines101 //#define STB_C_LEX_ISWHITE(str) ... // return length in bytes of whitespace characters if first char is whitespace102 103 #define STB_C_LEXER_DEFINITIONS // This line prevents the header file from replacing your definitions104 // --END--105-105 #endif106+#endif107 108 #ifndef INCLUDE_STB_C_LEXER_H109 #define INCLUDE_STB_C_LEXER_H110 60 unmodified lines171 #ifdef __cplusplus172 }173 #endif174 175-// Hacky definitions so we can easily #if on them176-#define Y(x) 1177-#define N(x) 0178-179-// Config variable that influence which lexer tokens get declared need to go here180-#if STB_C_LEX_C_DECIMAL_INTS(x) || STB_C_LEX_C_HEX_INTS(x) || STB_C_LEX_DEFINE_ALL_TOKEN_NAMES(x)181-#define STB__clex_define_int182-#endif183-184-#if (STB_C_LEX_C_ARITHEQ(x) && STB_C_LEX_C_SHIFTS(x)) || STB_C_LEX_DEFINE_ALL_TOKEN_NAMES(x)185-#define STB__clex_define_shifts186-#endif187-188-// Now pick a definition of Y/N that's conducive to189-// defining the enum of token names.190-#if STB_C_LEX_DEFINE_ALL_TOKEN_NAMES(x) || defined(STB_C_LEXER_SELF_TEST)191- #undef N192- #define N(a) Y(a)193-#else194- #undef N195- #define N(a)196-#endif197-198-#undef Y199-#define Y(a) a,200-175 enum176 {177 CLEX_eof = 256,178 CLEX_parse_error,205-206-#ifdef STB__clex_define_int207- CLEX_intlit,208-#endif209-210- STB_C_LEX_C_DECIMAL_FLOATS( CLEX_floatlit )211- STB_C_LEX_C_IDENTIFIERS( CLEX_id )212- STB_C_LEX_C_DQ_STRINGS( CLEX_dqstring )213- STB_C_LEX_C_SQ_STRINGS( CLEX_sqstring )214- STB_C_LEX_C_CHARS( CLEX_charlit )215- STB_C_LEX_C_COMPARISONS( CLEX_eq )216- STB_C_LEX_C_COMPARISONS( CLEX_noteq )217- STB_C_LEX_C_COMPARISONS( CLEX_lesseq )218- STB_C_LEX_C_COMPARISONS( CLEX_greatereq )219- STB_C_LEX_C_LOGICAL( CLEX_andand )220- STB_C_LEX_C_LOGICAL( CLEX_oror )221- STB_C_LEX_C_SHIFTS( CLEX_shl )222- STB_C_LEX_C_SHIFTS( CLEX_shr )223- STB_C_LEX_C_INCREMENTS( CLEX_plusplus )224- STB_C_LEX_C_INCREMENTS( CLEX_minusminus )225- STB_C_LEX_C_ARITHEQ( CLEX_pluseq )226- STB_C_LEX_C_ARITHEQ( CLEX_minuseq )227- STB_C_LEX_C_ARITHEQ( CLEX_muleq )228- STB_C_LEX_C_ARITHEQ( CLEX_diveq )229- STB_C_LEX_C_ARITHEQ( CLEX_modeq )230- STB_C_LEX_C_BITWISEEQ( CLEX_andeq )231- STB_C_LEX_C_BITWISEEQ( CLEX_oreq )232- STB_C_LEX_C_BITWISEEQ( CLEX_xoreq )233- STB_C_LEX_C_ARROW( CLEX_arrow )234- STB_C_LEX_EQUAL_ARROW( CLEX_eqarrow )235-236-#ifdef STB__clex_define_shifts179+ CLEX_intlit ,180+ CLEX_floatlit ,181+ CLEX_id ,182+ CLEX_dqstring ,183+ CLEX_sqstring ,184+ CLEX_charlit ,185+ CLEX_eq ,186+ CLEX_noteq ,187+ CLEX_lesseq ,188+ CLEX_greatereq ,189+ CLEX_andand ,190+ CLEX_oror ,191+ CLEX_shl ,192+ CLEX_shr ,193+ CLEX_plusplus ,194+ CLEX_minusminus ,195+ CLEX_pluseq ,196+ CLEX_minuseq ,197+ CLEX_muleq ,198+ CLEX_diveq ,199+ CLEX_modeq ,200+ CLEX_andeq ,201+ CLEX_oreq ,202+ CLEX_xoreq ,203+ CLEX_arrow ,204+ CLEX_eqarrow ,205 CLEX_shleq, CLEX_shreq,238-#endif206 207 CLEX_first_unused_token208 209 };243-244-#undef Y245-#undef N246-210 #endif // INCLUDE_STB_C_LEXER_H211 212 #ifdef STB_C_LEXER_IMPLEMENTATION213 726 unmodified lines