Skip to content

Commit

Permalink
Merge pull request #5520 from Goober5000/variable_token_fixes
Browse files Browse the repository at this point in the history
SEXP parsing cleanup and fixes
  • Loading branch information
Goober5000 committed Jul 29, 2023
2 parents d82b02a + a218761 commit 24031db
Show file tree
Hide file tree
Showing 4 changed files with 329 additions and 196 deletions.
62 changes: 46 additions & 16 deletions code/parse/parselo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1114,17 +1114,22 @@ int get_string_or_variable (char *str)
ignore_white_space();

// Variable
if (*Mp == '@')
if (*Mp == SEXP_VARIABLE_CHAR)
{
auto saved_Mp = Mp;
Mp++;
stuff_string_white(str);
int sexp_variable_index = get_index_sexp_variable_name(str);

// We only want String variables
Assertion (sexp_variable_index != -1, "Didn't find variable name \"%s\"", str);
Assert (Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_STRING);

result = PARSING_FOUND_VARIABLE;
if (sexp_variable_index >= 0)
result = PARSING_FOUND_VARIABLE;
else
{
Mp = saved_Mp;
stuff_string_white(str);
error_display(1, "Expected \"%s\" to be a variable", str);
}
}
// Quoted string
else if (*Mp == '"')
Expand All @@ -1135,7 +1140,7 @@ int get_string_or_variable (char *str)
else
{
get_string(str);
Error(LOCATION, "Invalid entry \"%s\" found in get_string_or_variable. Must be a quoted string or a string variable name.", str);
error_display(1, "Invalid entry \"%s\" found in get_string_or_variable. Must be a quoted string or a string variable name.", str);
}

return result;
Expand All @@ -1149,17 +1154,22 @@ int get_string_or_variable (SCP_string &str)
ignore_white_space();

// Variable
if (*Mp == '@')
if (*Mp == SEXP_VARIABLE_CHAR)
{
auto saved_Mp = Mp;
Mp++;
stuff_string_white(str);
int sexp_variable_index = get_index_sexp_variable_name(str);

// We only want String variables
Assertion (sexp_variable_index != -1, "Didn't find variable name \"%s\"", str.c_str());
Assert (Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_STRING);

result = PARSING_FOUND_VARIABLE;
if (sexp_variable_index >= 0)
result = PARSING_FOUND_VARIABLE;
else
{
Mp = saved_Mp;
stuff_string_white(str);
error_display(1, "Expected \"%s\" to be a variable", str.c_str());
}
}
// Quoted string
else if (*Mp == '"')
Expand All @@ -1170,7 +1180,7 @@ int get_string_or_variable (SCP_string &str)
else
{
get_string(str);
Error(LOCATION, "Invalid entry \"%s\" found in get_string_or_variable. Must be a quoted string or a string variable name.", str.c_str());
error_display(1, "Invalid entry \"%s\" found in get_string_or_variable. Must be a quoted string or a string variable name.", str.c_str());
}

return result;
Expand Down Expand Up @@ -2762,13 +2772,14 @@ int stuff_int_optional(int *i)
// index of the variable in the following slot.
void stuff_int_or_variable(int *i, int *var_index, bool need_positive_value)
{
if (*Mp == '@')
if (*Mp == SEXP_VARIABLE_CHAR)
{
Mp++;
int value = -1;
SCP_string str;
stuff_string(str, F_NAME);

auto saved_Mp = Mp;
Mp++;
stuff_string(str, F_NAME);
int index = get_index_sexp_variable_name(str);

if (index > -1 && index < MAX_SEXP_VARIABLES)
Expand All @@ -2784,7 +2795,8 @@ void stuff_int_or_variable(int *i, int *var_index, bool need_positive_value)
}
else
{

Mp = saved_Mp;
stuff_string(str, F_NAME);
error_display(1, "Invalid variable name \"%s\" found.", str.c_str());
}

Expand Down Expand Up @@ -4088,6 +4100,24 @@ void consolidate_double_characters(char *src, char ch)
}
}

char *three_dot_truncate(char *buffer, const char *source, size_t buffer_size)
{
Assertion(buffer && source, "Arguments must not be null!");

// this would be silly
if (buffer_size < 6)
{
*buffer = '\0';
return buffer;
}

strncpy(buffer, source, buffer_size);
if (buffer[buffer_size - 1] != '\0')
strcpy(&buffer[buffer_size - 6], "[...]");

return buffer;
}

// Goober5000
// Returns position of replacement, or a negative value if replacement failed: -1 if search string was not found, -2 if replacement would exceed max length, or -3 if any string argument is null
// Note that the parameter here is max *length*, not max buffer size. Leave room for the null-terminator!
Expand Down
3 changes: 3 additions & 0 deletions code/parse/parselo.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ extern int get_index_of_first_hash_symbol(SCP_string &src, bool ignore_doubled_h

extern void consolidate_double_characters(char *str, char ch);

// for limiting strings that may be very long; useful for dialog boxes
char *three_dot_truncate(char *buffer, const char *source, size_t buffer_size);

// white space
extern int is_white_space(char ch);
extern int is_white_space(unicode::codepoint_t cp);
Expand Down
Loading

0 comments on commit 24031db

Please sign in to comment.