123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941 |
- #ifdef STB_C_LEXER_IMPLEMENTATION
- #ifndef STB_C_LEXER_DEFINITIONS
- #if defined(Y) || defined(N)
- #error "Can only use stb_c_lexer in contexts where the preprocessor symbols 'Y' and 'N' are not defined"
- #endif
- #define STB_C_LEX_C_DECIMAL_INTS Y
- #define STB_C_LEX_C_HEX_INTS Y
- #define STB_C_LEX_C_OCTAL_INTS Y
- #define STB_C_LEX_C_DECIMAL_FLOATS Y
- #define STB_C_LEX_C99_HEX_FLOATS N
- #define STB_C_LEX_C_IDENTIFIERS Y
- #define STB_C_LEX_C_DQ_STRINGS Y
- #define STB_C_LEX_C_SQ_STRINGS N
- #define STB_C_LEX_C_CHARS Y
- #define STB_C_LEX_C_COMMENTS Y
- #define STB_C_LEX_CPP_COMMENTS Y
- #define STB_C_LEX_C_COMPARISONS Y
- #define STB_C_LEX_C_LOGICAL Y
- #define STB_C_LEX_C_SHIFTS Y
- #define STB_C_LEX_C_INCREMENTS Y
- #define STB_C_LEX_C_ARROW Y
- #define STB_C_LEX_EQUAL_ARROW N
- #define STB_C_LEX_C_BITWISEEQ Y
- #define STB_C_LEX_C_ARITHEQ Y
-
-
-
- #define STB_C_LEX_PARSE_SUFFIXES N
- #define STB_C_LEX_DECIMAL_SUFFIXES ""
- #define STB_C_LEX_HEX_SUFFIXES ""
- #define STB_C_LEX_OCTAL_SUFFIXES ""
- #define STB_C_LEX_FLOAT_SUFFIXES ""
- #define STB_C_LEX_0_IS_EOF N
- #define STB_C_LEX_INTEGERS_AS_DOUBLES N
- #define STB_C_LEX_MULTILINE_DSTRINGS N
- #define STB_C_LEX_MULTILINE_SSTRINGS N
- #define STB_C_LEX_USE_STDLIB Y
- #define STB_C_LEX_DOLLAR_IDENTIFIER Y
- #define STB_C_LEX_FLOAT_NO_DECIMAL Y
- #define STB_C_LEX_DEFINE_ALL_TOKEN_NAMES N
-
- #define STB_C_LEX_DISCARD_PREPROCESSOR Y
-
- #define STB_C_LEXER_DEFINITIONS
- #endif
- #endif
- #ifndef INCLUDE_STB_C_LEXER_H
- #define INCLUDE_STB_C_LEXER_H
- typedef struct
- {
-
- char *input_stream;
- char *eof;
- char *parse_point;
- char *string_storage;
- int string_storage_len;
-
- char *where_firstchar;
- char *where_lastchar;
-
- long token;
- double real_number;
- long int_number;
- char *string;
- int string_len;
- } stb_lexer;
- typedef struct
- {
- int line_number;
- int line_offset;
- } stb_lex_location;
- #ifdef __cplusplus
- extern "C" {
- #endif
- extern void stb_c_lexer_init(stb_lexer *lexer, const char *input_stream, const char *input_stream_end, char *string_store, int store_length);
- extern int stb_c_lexer_get_token(stb_lexer *lexer);
- extern void stb_c_lexer_get_location(const stb_lexer *lexer, const char *where, stb_lex_location *loc);
- #ifdef __cplusplus
- }
- #endif
- enum
- {
- CLEX_eof = 256,
- CLEX_parse_error,
- CLEX_intlit ,
- CLEX_floatlit ,
- CLEX_id ,
- CLEX_dqstring ,
- CLEX_sqstring ,
- CLEX_charlit ,
- CLEX_eq ,
- CLEX_noteq ,
- CLEX_lesseq ,
- CLEX_greatereq ,
- CLEX_andand ,
- CLEX_oror ,
- CLEX_shl ,
- CLEX_shr ,
- CLEX_plusplus ,
- CLEX_minusminus ,
- CLEX_pluseq ,
- CLEX_minuseq ,
- CLEX_muleq ,
- CLEX_diveq ,
- CLEX_modeq ,
- CLEX_andeq ,
- CLEX_oreq ,
- CLEX_xoreq ,
- CLEX_arrow ,
- CLEX_eqarrow ,
- CLEX_shleq, CLEX_shreq,
- CLEX_first_unused_token
- };
- #endif
- #ifdef STB_C_LEXER_IMPLEMENTATION
- #define Y(x) 1
- #define N(x) 0
- #if STB_C_LEX_INTEGERS_AS_DOUBLES(x)
- typedef double stb__clex_int;
- #define intfield real_number
- #define STB__clex_int_as_double
- #else
- typedef long stb__clex_int;
- #define intfield int_number
- #endif
- #if STB_C_LEX_PARSE_SUFFIXES(x)
- #define STB__clex_parse_suffixes
- #endif
- #if STB_C_LEX_C99_HEX_FLOATS(x)
- #define STB__clex_hex_floats
- #endif
- #if STB_C_LEX_C_HEX_INTS(x)
- #define STB__clex_hex_ints
- #endif
- #if STB_C_LEX_C_DECIMAL_INTS(x)
- #define STB__clex_decimal_ints
- #endif
- #if STB_C_LEX_C_OCTAL_INTS(x)
- #define STB__clex_octal_ints
- #endif
- #if STB_C_LEX_C_DECIMAL_FLOATS(x)
- #define STB__clex_decimal_floats
- #endif
- #if STB_C_LEX_DISCARD_PREPROCESSOR(x)
- #define STB__clex_discard_preprocessor
- #endif
- #if STB_C_LEX_USE_STDLIB(x) && (!defined(STB__clex_hex_floats) || __STDC_VERSION__ >= 199901L)
- #define STB__CLEX_use_stdlib
- #include <stdlib.h>
- #endif
- #undef Y
- #define Y(a) a
- #undef N
- #define N(a)
- void stb_c_lexer_init(stb_lexer *lexer, const char *input_stream, const char *input_stream_end, char *string_store, int store_length)
- {
- lexer->input_stream = (char *) input_stream;
- lexer->eof = (char *) input_stream_end;
- lexer->parse_point = (char *) input_stream;
- lexer->string_storage = string_store;
- lexer->string_storage_len = store_length;
- }
- void stb_c_lexer_get_location(const stb_lexer *lexer, const char *where, stb_lex_location *loc)
- {
- char *p = lexer->input_stream;
- int line_number = 1;
- int char_offset = 0;
- while (*p && p < where) {
- if (*p == '\n' || *p == '\r') {
- p += (p[0]+p[1] == '\r'+'\n' ? 2 : 1);
- line_number += 1;
- char_offset = 0;
- } else {
- ++p;
- ++char_offset;
- }
- }
- loc->line_number = line_number;
- loc->line_offset = char_offset;
- }
- static int stb__clex_token(stb_lexer *lexer, int token, char *start, char *end)
- {
- lexer->token = token;
- lexer->where_firstchar = start;
- lexer->where_lastchar = end;
- lexer->parse_point = end+1;
- return 1;
- }
- static int stb__clex_eof(stb_lexer *lexer)
- {
- lexer->token = CLEX_eof;
- return 0;
- }
- static int stb__clex_iswhite(int x)
- {
- return x == ' ' || x == '\t' || x == '\r' || x == '\n' || x == '\f';
- }
- static const char *stb__strchr(const char *str, int ch)
- {
- for (; *str; ++str)
- if (*str == ch)
- return str;
- return 0;
- }
- static int stb__clex_parse_suffixes(stb_lexer *lexer, long tokenid, char *start, char *cur, const char *suffixes)
- {
- #ifdef STB__clex_parse_suffixes
- lexer->string = lexer->string_storage;
- lexer->string_len = 0;
- while ((*cur >= 'a' && *cur <= 'z') || (*cur >= 'A' && *cur <= 'Z')) {
- if (stb__strchr(suffixes, *cur) == 0)
- return stb__clex_token(lexer, CLEX_parse_error, start, cur);
- if (lexer->string_len+1 >= lexer->string_storage_len)
- return stb__clex_token(lexer, CLEX_parse_error, start, cur);
- lexer->string[lexer->string_len++] = *cur++;
- }
- #else
- suffixes = suffixes;
- #endif
- return stb__clex_token(lexer, tokenid, start, cur-1);
- }
- #ifndef STB__CLEX_use_stdlib
- static double stb__clex_pow(double base, unsigned int exponent)
- {
- double value=1;
- for ( ; exponent; exponent >>= 1) {
- if (exponent & 1)
- value *= base;
- base *= base;
- }
- return value;
- }
- static double stb__clex_parse_float(char *p, char **q)
- {
- char *s = p;
- double value=0;
- int base=10;
- int exponent=0;
- #ifdef STB__clex_hex_floats
- if (*p == '0') {
- if (p[1] == 'x' || p[1] == 'X') {
- base=16;
- p += 2;
- }
- }
- #endif
- for (;;) {
- if (*p >= '0' && *p <= '9')
- value = value*base + (*p++ - '0');
- #ifdef STB__clex_hex_floats
- else if (base == 16 && *p >= 'a' && *p <= 'f')
- value = value*base + 10 + (*p++ - 'a');
- else if (base == 16 && *p >= 'A' && *p <= 'F')
- value = value*base + 10 + (*p++ - 'A');
- #endif
- else
- break;
- }
- if (*p == '.') {
- double pow, addend = 0;
- ++p;
- for (pow=1; ; pow*=base) {
- if (*p >= '0' && *p <= '9')
- addend = addend*base + (*p++ - '0');
- #ifdef STB__clex_hex_floats
- else if (base == 16 && *p >= 'a' && *p <= 'f')
- addend = addend*base + 10 + (*p++ - 'a');
- else if (base == 16 && *p >= 'A' && *p <= 'F')
- addend = addend*base + 10 + (*p++ - 'A');
- #endif
- else
- break;
- }
- value += addend / pow;
- }
- #ifdef STB__clex_hex_floats
- if (base == 16) {
-
- if (*p != 'p' && *p != 'P') {
- *q = s;
- return 0;
- }
- exponent = 1;
- } else
- #endif
- exponent = (*p == 'e' || *p == 'E');
- if (exponent) {
- int sign = p[1] == '-';
- unsigned int exponent=0;
- double power=1;
- ++p;
- if (*p == '-' || *p == '+')
- ++p;
- while (*p >= '0' && *p <= '9')
- exponent = exponent*10 + (*p++ - '0');
- #ifdef STB__clex_hex_floats
- if (base == 16)
- power = stb__clex_pow(2, exponent);
- else
- #endif
- power = stb__clex_pow(10, exponent);
- if (sign)
- value /= power;
- else
- value *= power;
- }
- *q = p;
- return value;
- }
- #endif
- static int stb__clex_parse_char(char *p, char **q)
- {
- if (*p == '\\') {
- *q = p+2;
- switch(p[1]) {
- case '\\': return '\\';
- case '\'': return '\'';
- case '"': return '"';
- case 't': return '\t';
- case 'f': return '\f';
- case 'n': return '\n';
- case 'r': return '\r';
- case '0': return '\0';
- case 'x': case 'X': return -1;
- case 'u': return -1;
- }
- }
- *q = p+1;
- return (unsigned char) *p;
- }
- static int stb__clex_parse_string(stb_lexer *lexer, char *p, int type)
- {
- char *start = p;
- char delim = *p++;
- char *out = lexer->string_storage;
- char *outend = lexer->string_storage + lexer->string_storage_len;
- while (*p != delim) {
- int n;
- if (*p == '\\') {
- char *q;
- n = stb__clex_parse_char(p, &q);
- if (n < 0)
- return stb__clex_token(lexer, CLEX_parse_error, start, q);
- p = q;
- } else {
-
- n = (unsigned char) *p++;
- }
- if (out+1 > outend)
- return stb__clex_token(lexer, CLEX_parse_error, start, p);
-
- *out++ = (char) n;
- }
- *out = 0;
- lexer->string = lexer->string_storage;
- lexer->string_len = (int) (out - lexer->string_storage);
- return stb__clex_token(lexer, type, start, p);
- }
- int stb_c_lexer_get_token(stb_lexer *lexer)
- {
- char *p = lexer->parse_point;
-
- for (;;) {
- #ifdef STB_C_LEX_ISWHITE
- while (p != lexer->stream_end) {
- int n;
- n = STB_C_LEX_ISWHITE(p);
- if (n == 0) break;
- if (lexer->eof && lexer->eof - lexer->parse_point < n)
- return stb__clex_token(tok, CLEX_parse_error, p,lexer->eof-1);
- p += n;
- }
- #else
- while (p != lexer->eof && stb__clex_iswhite(*p))
- ++p;
- #endif
- STB_C_LEX_CPP_COMMENTS(
- if (p != lexer->eof && p[0] == '/' && p[1] == '/') {
- while (p != lexer->eof && *p != '\r' && *p != '\n')
- ++p;
- continue;
- }
- )
- STB_C_LEX_C_COMMENTS(
- if (p != lexer->eof && p[0] == '/' && p[1] == '*') {
- char *start = p;
- p += 2;
- while (p != lexer->eof && (p[0] != '*' || p[1] != '/'))
- ++p;
- if (p == lexer->eof)
- return stb__clex_token(lexer, CLEX_parse_error, start, p-1);
- p += 2;
- continue;
- }
- )
- #ifdef STB__clex_discard_preprocessor
-
-
-
-
- if (p != lexer->eof && p[0] == '#') {
- while (p != lexer->eof && *p != '\r' && *p != '\n')
- ++p;
- continue;
- }
- #endif
- break;
- }
- if (p == lexer->eof)
- return stb__clex_eof(lexer);
- switch (*p) {
- default:
- if ( (*p >= 'a' && *p <= 'z')
- || (*p >= 'A' && *p <= 'Z')
- || *p == '_' || (unsigned char) *p >= 128
- STB_C_LEX_DOLLAR_IDENTIFIER( || *p == '$' ) )
- {
- int n = 0;
- lexer->string = lexer->string_storage;
- lexer->string_len = n;
- do {
- if (n+1 >= lexer->string_storage_len)
- return stb__clex_token(lexer, CLEX_parse_error, p, p+n);
- lexer->string[n] = p[n];
- ++n;
- } while (
- (p[n] >= 'a' && p[n] <= 'z')
- || (p[n] >= 'A' && p[n] <= 'Z')
- || (p[n] >= '0' && p[n] <= '9')
- || p[n] == '_' || (unsigned char) p[n] >= 128
- STB_C_LEX_DOLLAR_IDENTIFIER( || p[n] == '$' )
- );
- lexer->string[n] = 0;
- return stb__clex_token(lexer, CLEX_id, p, p+n-1);
- }
-
- STB_C_LEX_0_IS_EOF(
- if (*p == 0)
- return stb__clex_eof(lexer);
- )
- single_char:
-
- return stb__clex_token(lexer, *p, p, p);
- case '+':
- if (p+1 != lexer->eof) {
- STB_C_LEX_C_INCREMENTS(if (p[1] == '+') return stb__clex_token(lexer, CLEX_plusplus, p,p+1);)
- STB_C_LEX_C_ARITHEQ( if (p[1] == '=') return stb__clex_token(lexer, CLEX_pluseq , p,p+1);)
- }
- goto single_char;
- case '-':
- if (p+1 != lexer->eof) {
- STB_C_LEX_C_INCREMENTS(if (p[1] == '-') return stb__clex_token(lexer, CLEX_minusminus, p,p+1);)
- STB_C_LEX_C_ARITHEQ( if (p[1] == '=') return stb__clex_token(lexer, CLEX_minuseq , p,p+1);)
- STB_C_LEX_C_ARROW( if (p[1] == '>') return stb__clex_token(lexer, CLEX_arrow , p,p+1);)
- }
- goto single_char;
- case '&':
- if (p+1 != lexer->eof) {
- STB_C_LEX_C_LOGICAL( if (p[1] == '&') return stb__clex_token(lexer, CLEX_andand, p,p+1);)
- STB_C_LEX_C_BITWISEEQ(if (p[1] == '=') return stb__clex_token(lexer, CLEX_andeq , p,p+1);)
- }
- goto single_char;
- case '|':
- if (p+1 != lexer->eof) {
- STB_C_LEX_C_LOGICAL( if (p[1] == '|') return stb__clex_token(lexer, CLEX_oror, p,p+1);)
- STB_C_LEX_C_BITWISEEQ(if (p[1] == '=') return stb__clex_token(lexer, CLEX_oreq, p,p+1);)
- }
- goto single_char;
- case '=':
- if (p+1 != lexer->eof) {
- STB_C_LEX_C_COMPARISONS(if (p[1] == '=') return stb__clex_token(lexer, CLEX_eq, p,p+1);)
- STB_C_LEX_EQUAL_ARROW( if (p[1] == '>') return stb__clex_token(lexer, CLEX_eqarrow, p,p+1);)
- }
- goto single_char;
- case '!':
- STB_C_LEX_C_COMPARISONS(if (p+1 != lexer->eof && p[1] == '=') return stb__clex_token(lexer, CLEX_noteq, p,p+1);)
- goto single_char;
- case '^':
- STB_C_LEX_C_BITWISEEQ(if (p+1 != lexer->eof && p[1] == '=') return stb__clex_token(lexer, CLEX_xoreq, p,p+1));
- goto single_char;
- case '%':
- STB_C_LEX_C_ARITHEQ(if (p+1 != lexer->eof && p[1] == '=') return stb__clex_token(lexer, CLEX_modeq, p,p+1));
- goto single_char;
- case '*':
- STB_C_LEX_C_ARITHEQ(if (p+1 != lexer->eof && p[1] == '=') return stb__clex_token(lexer, CLEX_muleq, p,p+1));
- goto single_char;
- case '/':
- STB_C_LEX_C_ARITHEQ(if (p+1 != lexer->eof && p[1] == '=') return stb__clex_token(lexer, CLEX_diveq, p,p+1));
- goto single_char;
- case '<':
- if (p+1 != lexer->eof) {
- STB_C_LEX_C_COMPARISONS(if (p[1] == '=') return stb__clex_token(lexer, CLEX_lesseq, p,p+1);)
- STB_C_LEX_C_SHIFTS( if (p[1] == '<') {
- STB_C_LEX_C_ARITHEQ(if (p+2 != lexer->eof && p[2] == '=')
- return stb__clex_token(lexer, CLEX_shleq, p,p+2);)
- return stb__clex_token(lexer, CLEX_shl, p,p+1);
- }
- )
- }
- goto single_char;
- case '>':
- if (p+1 != lexer->eof) {
- STB_C_LEX_C_COMPARISONS(if (p[1] == '=') return stb__clex_token(lexer, CLEX_greatereq, p,p+1);)
- STB_C_LEX_C_SHIFTS( if (p[1] == '>') {
- STB_C_LEX_C_ARITHEQ(if (p+2 != lexer->eof && p[2] == '=')
- return stb__clex_token(lexer, CLEX_shreq, p,p+2);)
- return stb__clex_token(lexer, CLEX_shr, p,p+1);
- }
- )
- }
- goto single_char;
- case '"':
- STB_C_LEX_C_DQ_STRINGS(return stb__clex_parse_string(lexer, p, CLEX_dqstring);)
- goto single_char;
- case '\'':
- STB_C_LEX_C_SQ_STRINGS(return stb__clex_parse_string(lexer, p, CLEX_sqstring);)
- STB_C_LEX_C_CHARS(
- {
- char *start = p;
- lexer->int_number = stb__clex_parse_char(p+1, &p);
- if (lexer->int_number < 0)
- return stb__clex_token(lexer, CLEX_parse_error, start,start);
- if (p == lexer->eof || *p != '\'')
- return stb__clex_token(lexer, CLEX_parse_error, start,p);
- return stb__clex_token(lexer, CLEX_charlit, start, p+1);
- })
- goto single_char;
- case '0':
- #if defined(STB__clex_hex_ints) || defined(STB__clex_hex_floats)
- if (p+1 != lexer->eof) {
- if (p[1] == 'x' || p[1] == 'X') {
- char *q;
- #ifdef STB__clex_hex_floats
- for (q=p+2;
- q != lexer->eof && ((*q >= '0' && *q <= '9') || (*q >= 'a' && *q <= 'f') || (*q >= 'A' && *q <= 'F'));
- ++q);
- if (q != lexer->eof) {
- if (*q == '.' STB_C_LEX_FLOAT_NO_DECIMAL(|| *q == 'p' || *q == 'P')) {
- #ifdef STB__CLEX_use_stdlib
- lexer->real_number = strtod((char *) p, (char**) &q);
- #else
- lexer->real_number = stb__clex_parse_float(p, &q);
- #endif
- if (p == q)
- return stb__clex_token(lexer, CLEX_parse_error, p,q);
- return stb__clex_parse_suffixes(lexer, CLEX_floatlit, p,q, STB_C_LEX_FLOAT_SUFFIXES);
- }
- }
- #endif
- #ifdef STB__clex_hex_ints
- #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;
- }
- lexer->int_number = n;
- }
- #endif
- if (q == p+2)
- return stb__clex_token(lexer, CLEX_parse_error, p-2,p-1);
- return stb__clex_parse_suffixes(lexer, CLEX_intlit, p,q, STB_C_LEX_HEX_SUFFIXES);
- #endif
- }
- }
- #endif
-
-
-
- case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
- #ifdef STB__clex_decimal_floats
- {
- char *q = p;
- while (q != lexer->eof && (*q >= '0' && *q <= '9'))
- ++q;
- if (q != lexer->eof) {
- if (*q == '.' STB_C_LEX_FLOAT_NO_DECIMAL(|| *q == 'e' || *q == 'E')) {
- #ifdef STB__CLEX_use_stdlib
- lexer->real_number = strtod((char *) p, (char**) &q);
- #else
- lexer->real_number = stb__clex_parse_float(p, &q);
- #endif
- return stb__clex_parse_suffixes(lexer, CLEX_floatlit, p,q, STB_C_LEX_FLOAT_SUFFIXES);
- }
- }
- }
- #endif
- #ifdef STB__clex_octal_ints
- if (p[0] == '0') {
- char *q = p;
- #ifdef STB__CLEX_use_stdlib
- lexer->int_number = strtol((char *) p, (char **) &q, 8);
- #else
- stb__clex_int n=0;
- while (q != lexer->eof) {
- if (*q >= '0' && *q <= '7')
- n = n*8 + (*q - '0');
- else
- break;
- ++q;
- }
- if (q != lexer->eof && (*q == '8' || *q=='9'))
- return stb__clex_token(lexer, CLEX_parse_error, p, q);
- lexer->int_number = n;
- #endif
- return stb__clex_parse_suffixes(lexer, CLEX_intlit, p,q, STB_C_LEX_OCTAL_SUFFIXES);
- }
- #endif
- #ifdef STB__clex_decimal_ints
- {
- char *q = p;
- #ifdef STB__CLEX_use_stdlib
- lexer->int_number = strtol((char *) p, (char **) &q, 10);
- #else
- stb__clex_int n=0;
- while (q != lexer->eof) {
- if (*q >= '0' && *q <= '9')
- n = n*10 + (*q - '0');
- else
- break;
- ++q;
- }
- lexer->int_number = n;
- #endif
- return stb__clex_parse_suffixes(lexer, CLEX_intlit, p,q, STB_C_LEX_OCTAL_SUFFIXES);
- }
- #endif
- goto single_char;
- }
- }
- #endif
- #ifdef STB_C_LEXER_SELF_TEST
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- static void print_token(stb_lexer *lexer)
- {
- switch (lexer->token) {
- case CLEX_id : printf("_%s", lexer->string); break;
- case CLEX_eq : printf("=="); break;
- case CLEX_noteq : printf("!="); break;
- case CLEX_lesseq : printf("<="); break;
- case CLEX_greatereq : printf(">="); break;
- case CLEX_andand : printf("&&"); break;
- case CLEX_oror : printf("||"); break;
- case CLEX_shl : printf("<<"); break;
- case CLEX_shr : printf(">>"); break;
- case CLEX_plusplus : printf("++"); break;
- case CLEX_minusminus: printf("--"); break;
- case CLEX_arrow : printf("->"); break;
- case CLEX_andeq : printf("&="); break;
- case CLEX_oreq : printf("|="); break;
- case CLEX_xoreq : printf("^="); break;
- case CLEX_pluseq : printf("+="); break;
- case CLEX_minuseq : printf("-="); break;
- case CLEX_muleq : printf("*="); break;
- case CLEX_diveq : printf("/="); break;
- case CLEX_modeq : printf("%%="); break;
- case CLEX_shleq : printf("<<="); break;
- case CLEX_shreq : printf(">>="); break;
- case CLEX_eqarrow : printf("=>"); break;
- case CLEX_dqstring : printf("\"%s\"", lexer->string); break;
- case CLEX_sqstring : printf("'\"%s\"'", lexer->string); break;
- case CLEX_charlit : printf("'%s'", lexer->string); break;
- #if defined(STB__clex_int_as_double) && !defined(STB__CLEX_use_stdlib)
- case CLEX_intlit : printf("#%g", lexer->real_number); break;
- #else
- case CLEX_intlit : printf("#%ld", lexer->int_number); break;
- #endif
- case CLEX_floatlit : printf("%g", lexer->real_number); break;
- default:
- if (lexer->token >= 0 && lexer->token < 256)
- printf("%c", (int) lexer->token);
- else {
- printf("<<<UNKNOWN TOKEN %ld >>>\n", lexer->token);
- }
- break;
- }
- }
|