Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 51 additions & 76 deletions third_party/stb/stb_c_lexer.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// stb_c_lexer.h - v0.11 - public domain Sean Barrett 2013
// stb_c_lexer.h - v0.12 - public domain Sean Barrett 2013
// lexer for making little C-like languages with recursive-descent parsers
//
// This file provides both the interface and the implementation.
Expand All @@ -10,6 +10,7 @@
// suffixes on integer constants are not handled (you can override this).
//
// History:
// 0.12 fix compilation bug for NUL support; better support separate inclusion
// 0.11 fix clang static analysis warning
// 0.10 fix warnings
// 0.09 hex floats, no-stdlib fixes
Expand Down Expand Up @@ -37,17 +38,25 @@
// Contributors:
// Arpad Goretity (bugfix)
// Alan Hickman (hex floats)
// github:mundusnine (bugfix)
//
// LICENSE
//
// See end of file for license information.

#ifdef STB_C_LEXER_IMPLEMENTATION
#ifndef STB_C_LEXER_DEFINITIONS
// to change the default parsing rules, copy the following lines
// into your C/C++ file *before* including this, and then replace
// the Y's with N's for the ones you don't want.
// the Y's with N's for the ones you don't want. This needs to be
// set to the same values for every place in your program where
// stb_c_lexer.h is included.
// --BEGIN--

#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 // "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
Expand Down Expand Up @@ -95,7 +104,7 @@

#define STB_C_LEXER_DEFINITIONS // This line prevents the header file from replacing your definitions
// --END--

#endif
#endif

#ifndef INCLUDE_STB_C_LEXER_H
Expand Down Expand Up @@ -165,14 +174,44 @@ extern void stb_c_lexer_get_location(const stb_lexer *lexer, const char *where,
}
#endif

#endif // INCLUDE_STB_C_LEXER_H
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,

#ifdef STB_C_LEXER_IMPLEMENTATION
CLEX_first_unused_token

#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
};
#endif // INCLUDE_STB_C_LEXER_H

#ifdef STB_C_LEXER_IMPLEMENTATION

// Hacky definitions so we can easily #if on them
#define Y(x) 1
Expand All @@ -194,14 +233,6 @@ typedef long stb__clex_int;
#define STB__clex_parse_suffixes
#endif

#if STB_C_LEX_C_DECIMAL_INTS(x) || STB_C_LEX_C_HEX_INTS(x) || STB_C_LEX_DEFINE_ALL_TOKEN_NAMES(x)
#define STB__clex_define_int
#endif

#if (STB_C_LEX_C_ARITHEQ(x) && STB_C_LEX_C_SHIFTS(x)) || STB_C_LEX_DEFINE_ALL_TOKEN_NAMES(x)
#define STB__clex_define_shifts
#endif

#if STB_C_LEX_C99_HEX_FLOATS(x)
#define STB__clex_hex_floats
#endif
Expand Down Expand Up @@ -231,66 +262,10 @@ typedef long stb__clex_int;
#include <stdlib.h>
#endif

// Now pick a definition of Y/N that's conducive to
// defining the enum of token names.
#if STB_C_LEX_DEFINE_ALL_TOKEN_NAMES(x) || defined(STB_C_LEXER_SELF_TEST)
#undef N
#define N(a) Y(a)
#else
#undef N
#define N(a)
#endif

#undef Y
#define Y(a) a,

enum
{
CLEX_eof = 256,
CLEX_parse_error,

#ifdef STB__clex_define_int
CLEX_intlit,
#endif

STB_C_LEX_C_DECIMAL_FLOATS( CLEX_floatlit )
STB_C_LEX_C_IDENTIFIERS( CLEX_id )
STB_C_LEX_C_DQ_STRINGS( CLEX_dqstring )
STB_C_LEX_C_SQ_STRINGS( CLEX_sqstring )
STB_C_LEX_C_CHARS( CLEX_charlit )
STB_C_LEX_C_COMPARISONS( CLEX_eq )
STB_C_LEX_C_COMPARISONS( CLEX_noteq )
STB_C_LEX_C_COMPARISONS( CLEX_lesseq )
STB_C_LEX_C_COMPARISONS( CLEX_greatereq )
STB_C_LEX_C_LOGICAL( CLEX_andand )
STB_C_LEX_C_LOGICAL( CLEX_oror )
STB_C_LEX_C_SHIFTS( CLEX_shl )
STB_C_LEX_C_SHIFTS( CLEX_shr )
STB_C_LEX_C_INCREMENTS( CLEX_plusplus )
STB_C_LEX_C_INCREMENTS( CLEX_minusminus )
STB_C_LEX_C_ARITHEQ( CLEX_pluseq )
STB_C_LEX_C_ARITHEQ( CLEX_minuseq )
STB_C_LEX_C_ARITHEQ( CLEX_muleq )
STB_C_LEX_C_ARITHEQ( CLEX_diveq )
STB_C_LEX_C_ARITHEQ( CLEX_modeq )
STB_C_LEX_C_BITWISEEQ( CLEX_andeq )
STB_C_LEX_C_BITWISEEQ( CLEX_oreq )
STB_C_LEX_C_BITWISEEQ( CLEX_xoreq )
STB_C_LEX_C_ARROW( CLEX_arrow )
STB_C_LEX_EQUAL_ARROW( CLEX_eqarrow )

#ifdef STB__clex_define_shifts
CLEX_shleq, CLEX_shreq,
#endif

CLEX_first_unused_token

#undef Y
#define Y(a) a
};

// Now for the rest of the file we'll use the basic definition where
// where Y expands to its contents and N expands to nothing
#undef Y
#define Y(a) a
#undef N
#define N(a)

Expand Down Expand Up @@ -588,7 +563,6 @@ int stb_c_lexer_get_token(stb_lexer *lexer)
{
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);
Expand All @@ -602,13 +576,14 @@ int stb_c_lexer_get_token(stb_lexer *lexer)
STB_C_LEX_DOLLAR_IDENTIFIER( || p[n] == '$' )
);
lexer->string[n] = 0;
lexer->string_len = n;
return stb__clex_token(lexer, CLEX_id, p, p+n-1);
}

// check for EOF
STB_C_LEX_0_IS_EOF(
if (*p == 0)
return stb__clex_eof(tok);
return stb__clex_eof(lexer);
)

single_char:
Expand Down
21 changes: 12 additions & 9 deletions third_party/stb/stb_divide.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// stb_divide.h - v0.93 - public domain - Sean Barrett, Feb 2010
// stb_divide.h - v0.94 - public domain - Sean Barrett, Feb 2010
// Three kinds of divide/modulus of signed integers.
//
// HISTORY
//
// v0.94 Fix integer overflow issues
// v0.93 2020-02-02 Write useful exit() value from main()
// v0.92 2019-02-25 Fix warning
// v0.91 2010-02-27 Fix euclidean division by INT_MIN for non-truncating C
Expand Down Expand Up @@ -166,15 +167,15 @@ int stb_div_floor(int v1, int v2)
return v1/v2;
#else
if (v1 >= 0 && v2 < 0) {
if ((-v1)+v2+1 < 0) // check if increasing v1's magnitude overflows
return -stb__div(-v1+v2+1,v2); // nope, so just compute it
if (v2 + 1 >= INT_MIN + v1) // check if increasing v1's magnitude overflows
return -stb__div((v2+1)-v1,v2); // nope, so just compute it
else
return -stb__div(-v1,v2) + ((-v1)%v2 ? -1 : 0);
}
if (v1 < 0 && v2 >= 0) {
if (v1 != INT_MIN) {
if (v1-v2+1 < 0) // check if increasing v1's magnitude overflows
return -stb__div(v1-v2+1,-v2); // nope, so just compute it
if (v1 + 1 >= INT_MIN + v2) // check if increasing v1's magnitude overflows
return -stb__div((v1+1)-v2,-v2); // nope, so just compute it
else
return -stb__div(-v1,v2) + (stb__mod(v1,-v2) ? -1 : 0);
} else // it must be possible to compute -(v1+v2) without overflowing
Expand Down Expand Up @@ -209,8 +210,10 @@ int stb_div_eucl(int v1, int v2)
else // if v1 is INT_MIN, we have to move away from overflow place
if (v2 >= 0)
q = -stb__div(-(v1+v2),v2)-1, r = -stb__mod(-(v1+v2),v2);
else
else if (v2 != INT_MIN)
q = stb__div(-(v1-v2),-v2)+1, r = -stb__mod(-(v1-v2),-v2);
else // for INT_MIN / INT_MIN, we need to be extra-careful to avoid overflow
q = 1, r = 0;
#endif
if (r >= 0)
return q;
Expand All @@ -228,13 +231,13 @@ int stb_mod_trunc(int v1, int v2)
if (r >= 0)
return r;
else
return r + (v2 > 0 ? v2 : -v2);
return r - (v2 < 0 ? v2 : -v2);
} else { // modulus result should always be negative
int r = stb__mod(v1,v2);
if (r <= 0)
return r;
else
return r - (v2 > 0 ? v2 : -v2);
return r + (v2 < 0 ? v2 : -v2);
}
#endif
}
Expand Down Expand Up @@ -267,7 +270,7 @@ int stb_mod_eucl(int v1, int v2)
if (r >= 0)
return r;
else
return r + (v2 > 0 ? v2 : -v2); // abs()
return r - (v2 < 0 ? v2 : -v2); // negative abs() [to avoid overflow]
}

#ifdef STB_DIVIDE_TEST
Expand Down
Loading