Skip to content

Commit

Permalink
Parser/lexer cleanup
Browse files Browse the repository at this point in the history
Also warn if trying to use C23 #embed feature in a file
  • Loading branch information
ensiform committed Dec 2, 2024
1 parent fbce476 commit eb22da9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
26 changes: 11 additions & 15 deletions src/qcommon/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ static punctuation_t default_punctuations[] =
{"<<=",P_LSHIFT_ASSIGN},
//
{"...",P_PARMS},
//C++20 spaceship 3-way comparison operator
//{"<=>",P_SPACESHIP},
//define merge operator
{"##",P_PRECOMPMERGE},
//logic operators
Expand Down Expand Up @@ -113,9 +115,7 @@ static punctuation_t default_punctuations[] =
{"\\",P_BACKSLASH},
//precompiler operator
{"#",P_PRECOMP},
#ifdef DOLLAR
{"$",P_DOLLAR},
#endif //DOLLAR
{NULL, 0}
};

Expand Down Expand Up @@ -487,7 +487,7 @@ static int PS_ReadString( script_t *script, token_t *token, int quote ) {
//
tmpscript_p = script->script_p;
tmpline = script->line;
//read unusefull stuff between possible two following strings
//read white space between possible two consecutive strings
if ( !PS_ReadWhiteSpace( script ) ) {
script->script_p = tmpscript_p;
script->line = tmpline;
Expand Down Expand Up @@ -631,8 +631,6 @@ static int PS_ReadNumber( script_t *script, token_t *token ) {
int len = 0, i;
int octal, dot;
char c;
// unsigned long int intvalue = 0;
// long double floatvalue = 0;

token->type = TT_NUMBER;
//check for a hexadecimal number
Expand Down Expand Up @@ -724,9 +722,7 @@ static int PS_ReadNumber( script_t *script, token_t *token ) {
} //end if
} //end for
token->string[len] = '\0';
#ifdef NUMBERVALUE
NumberValue( token->string, token->subtype, &token->intvalue, &token->floatvalue );
#endif //NUMBERVALUE
if ( !( token->subtype & TT_FLOAT ) ) {
token->subtype |= TT_INTEGER;
}
Expand All @@ -738,7 +734,7 @@ static int PS_ReadNumber( script_t *script, token_t *token ) {
// Returns: -
// Changes Globals: -
//============================================================================
int PS_ReadLiteral( script_t *script, token_t *token ) {
/*int PS_ReadLiteral( script_t *script, token_t *token ) {
token->type = TT_LITERAL;
//first quote
token->string[0] = *script->script_p++;
Expand Down Expand Up @@ -778,7 +774,7 @@ int PS_ReadLiteral( script_t *script, token_t *token ) {
token->subtype = token->string[1];
//
return 1;
} //end of the function PS_ReadLiteral
}*/ //end of the function PS_ReadLiteral
//============================================================================
//
// Parameter: -
Expand Down Expand Up @@ -824,7 +820,7 @@ int PS_ReadPunctuation( script_t *script, token_t *token ) {
// Returns: -
// Changes Globals: -
//============================================================================
int PS_ReadPrimitive( script_t *script, token_t *token ) {
/*int PS_ReadPrimitive( script_t *script, token_t *token ) {
int len;
len = 0;
Expand All @@ -841,7 +837,7 @@ int PS_ReadPrimitive( script_t *script, token_t *token ) {
memcpy( &script->token, token, sizeof( token_t ) );
//primitive reading successfull
return 1;
} //end of the function PS_ReadPrimitive
}*/ //end of the function PS_ReadPrimitive
//============================================================================
//
// Parameter: -
Expand All @@ -864,7 +860,7 @@ int PS_ReadToken( script_t *script, token_t *token ) {
//start of the white space
script->whitespace_p = script->script_p;
token->whitespace_p = script->script_p;
//read unusefull stuff
//read white space before token
if ( !PS_ReadWhiteSpace( script ) ) {
return 0;
}
Expand Down Expand Up @@ -897,9 +893,9 @@ int PS_ReadToken( script_t *script, token_t *token ) {
}
} //end if
//if this is a primitive script
else if ( script->flags & SCFL_PRIMITIVE ) {
return PS_ReadPrimitive( script, token );
} //end else if
//else if ( script->flags & SCFL_PRIMITIVE ) {
// return PS_ReadPrimitive( script, token );
//} //end else if
//if there is a name
else if ( ( *script->script_p >= 'a' && *script->script_p <= 'z' ) ||
( *script->script_p >= 'A' && *script->script_p <= 'Z' ) ||
Expand Down
15 changes: 1 addition & 14 deletions src/qcommon/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ If you have questions concerning this license or the applicable additional terms
// for QDECL
#include "q_platform.h"

//undef if binary numbers of the form 0b... or 0B... are not allowed
#define BINARYNUMBERS
//undef if not using the token.intvalue and token.floatvalue
#define NUMBERVALUE
//use dollar sign also as punctuation
#define DOLLAR

//maximum token length
#define MAX_TOKEN 1024

Expand All @@ -56,9 +49,6 @@ If you have questions concerning this license or the applicable additional terms
#define SCFL_NOWARNINGS 0x0002
#define SCFL_NOSTRINGWHITESPACES 0x0004
#define SCFL_NOSTRINGESCAPECHARS 0x0008
#define SCFL_PRIMITIVE 0x0010
#define SCFL_NOBINARYNUMBERS 0x0020
#define SCFL_NONUMBERVALUES 0x0040

//token types
#define TT_STRING 1 // string
Expand All @@ -78,9 +68,7 @@ If you have questions concerning this license or the applicable additional terms
#define TT_DECIMAL 0x0008 // decimal number
#define TT_HEX 0x0100 // hexadecimal number
#define TT_OCTAL 0x0200 // octal number
#ifdef BINARYNUMBERS
#define TT_BINARY 0x0400 // binary number
#endif //BINARYNUMBERS
#define TT_FLOAT 0x0800 // floating point number
#define TT_INTEGER 0x1000 // integer number
#define TT_LONG 0x2000 // long number
Expand Down Expand Up @@ -148,6 +136,7 @@ If you have questions concerning this license or the applicable additional terms

#define P_PRECOMP 51
#define P_DOLLAR 52
//#define P_LOGIC_SPACESHIP 53
//name sub type
//-------------
// the length of the name
Expand All @@ -165,10 +154,8 @@ typedef struct token_s
char string[MAX_TOKEN]; //available token
int type; //last read token type
int subtype; //last read token sub type
#ifdef NUMBERVALUE
unsigned int intvalue; //integer value
float floatvalue; //floating point value
#endif //NUMBERVALUE
const char *whitespace_p; //start of white space before token
const char *endwhitespace_p; //start of white space before token
int line; //line the token was on
Expand Down
22 changes: 15 additions & 7 deletions src/qcommon/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,8 @@ int PC_ExpandBuiltinDefine( source_t *source, token_t *deftoken, define_t *defin
case BUILTIN_LINE:
{
sprintf( token->string, "%d", deftoken->line );
#ifdef NUMBERVALUE
token->intvalue = deftoken->line;
token->floatvalue = deftoken->line;
#endif //NUMBERVALUE
token->type = TT_NUMBER;
token->subtype = TT_DECIMAL | TT_INTEGER;
*firsttoken = token;
Expand Down Expand Up @@ -1302,7 +1300,7 @@ int PC_AddGlobalDefine(const char *string)
// Returns: -
// Changes Globals: -
//============================================================================
/*int PC_RemoveGlobalDefine( char *name ) {
/*int PC_RemoveGlobalDefine( const char *name ) {
define_t *define;
define = PC_FindDefine( globaldefines, name );
Expand Down Expand Up @@ -2391,6 +2389,19 @@ int PC_Directive_pragma( source_t *source ) {
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_embed( source_t *source ) {
token_t token;

SourceWarning( source, "#embed directive not supported" );
while ( PC_ReadLine( source, &token ) ) ;
return qtrue;
} //end of the function PC_Directive_embed
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void UnreadSignToken( source_t *source ) {
token_t token;

Expand Down Expand Up @@ -2479,6 +2490,7 @@ static const directive_t directives[] =
{"error", PC_Directive_error},
{"warning", PC_Directive_warning},
{"pragma", PC_Directive_pragma},
{"embed", PC_Directive_embed},
{"eval", PC_Directive_eval},
{"evalfloat", PC_Directive_evalfloat},
{NULL, NULL}
Expand Down Expand Up @@ -2533,10 +2545,8 @@ int PC_DollarDirective_evalint( source_t *source ) {
sprintf( token.string, "%d", abs( value ) );
token.type = TT_NUMBER;
token.subtype = TT_INTEGER | TT_LONG | TT_DECIMAL;
#ifdef NUMBERVALUE
token.intvalue = value;
token.floatvalue = value;
#endif //NUMBERVALUE
PC_UnreadSourceToken( source, &token );
if ( value < 0 ) {
UnreadSignToken( source );
Expand All @@ -2563,10 +2573,8 @@ int PC_DollarDirective_evalfloat( source_t *source ) {
sprintf( token.string, "%1.2f", Q_fabs( value ) );
token.type = TT_NUMBER;
token.subtype = TT_FLOAT | TT_LONG | TT_DECIMAL;
#ifdef NUMBERVALUE
token.intvalue = (unsigned int) value;
token.floatvalue = value;
#endif //NUMBERVALUE
PC_UnreadSourceToken( source, &token );
if ( value < 0 ) {
UnreadSignToken( source );
Expand Down

0 comments on commit eb22da9

Please sign in to comment.