Skip to content

Commit 24031db

Browse files
authored
Merge pull request #5520 from Goober5000/variable_token_fixes
SEXP parsing cleanup and fixes
2 parents d82b02a + a218761 commit 24031db

File tree

4 files changed

+329
-196
lines changed

4 files changed

+329
-196
lines changed

code/parse/parselo.cpp

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,17 +1114,22 @@ int get_string_or_variable (char *str)
11141114
ignore_white_space();
11151115

11161116
// Variable
1117-
if (*Mp == '@')
1117+
if (*Mp == SEXP_VARIABLE_CHAR)
11181118
{
1119+
auto saved_Mp = Mp;
11191120
Mp++;
11201121
stuff_string_white(str);
11211122
int sexp_variable_index = get_index_sexp_variable_name(str);
11221123

11231124
// We only want String variables
1124-
Assertion (sexp_variable_index != -1, "Didn't find variable name \"%s\"", str);
1125-
Assert (Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_STRING);
1126-
1127-
result = PARSING_FOUND_VARIABLE;
1125+
if (sexp_variable_index >= 0)
1126+
result = PARSING_FOUND_VARIABLE;
1127+
else
1128+
{
1129+
Mp = saved_Mp;
1130+
stuff_string_white(str);
1131+
error_display(1, "Expected \"%s\" to be a variable", str);
1132+
}
11281133
}
11291134
// Quoted string
11301135
else if (*Mp == '"')
@@ -1135,7 +1140,7 @@ int get_string_or_variable (char *str)
11351140
else
11361141
{
11371142
get_string(str);
1138-
Error(LOCATION, "Invalid entry \"%s\" found in get_string_or_variable. Must be a quoted string or a string variable name.", str);
1143+
error_display(1, "Invalid entry \"%s\" found in get_string_or_variable. Must be a quoted string or a string variable name.", str);
11391144
}
11401145

11411146
return result;
@@ -1149,17 +1154,22 @@ int get_string_or_variable (SCP_string &str)
11491154
ignore_white_space();
11501155

11511156
// Variable
1152-
if (*Mp == '@')
1157+
if (*Mp == SEXP_VARIABLE_CHAR)
11531158
{
1159+
auto saved_Mp = Mp;
11541160
Mp++;
11551161
stuff_string_white(str);
11561162
int sexp_variable_index = get_index_sexp_variable_name(str);
11571163

11581164
// We only want String variables
1159-
Assertion (sexp_variable_index != -1, "Didn't find variable name \"%s\"", str.c_str());
1160-
Assert (Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_STRING);
1161-
1162-
result = PARSING_FOUND_VARIABLE;
1165+
if (sexp_variable_index >= 0)
1166+
result = PARSING_FOUND_VARIABLE;
1167+
else
1168+
{
1169+
Mp = saved_Mp;
1170+
stuff_string_white(str);
1171+
error_display(1, "Expected \"%s\" to be a variable", str.c_str());
1172+
}
11631173
}
11641174
// Quoted string
11651175
else if (*Mp == '"')
@@ -1170,7 +1180,7 @@ int get_string_or_variable (SCP_string &str)
11701180
else
11711181
{
11721182
get_string(str);
1173-
Error(LOCATION, "Invalid entry \"%s\" found in get_string_or_variable. Must be a quoted string or a string variable name.", str.c_str());
1183+
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());
11741184
}
11751185

11761186
return result;
@@ -2762,13 +2772,14 @@ int stuff_int_optional(int *i)
27622772
// index of the variable in the following slot.
27632773
void stuff_int_or_variable(int *i, int *var_index, bool need_positive_value)
27642774
{
2765-
if (*Mp == '@')
2775+
if (*Mp == SEXP_VARIABLE_CHAR)
27662776
{
2767-
Mp++;
27682777
int value = -1;
27692778
SCP_string str;
2770-
stuff_string(str, F_NAME);
27712779

2780+
auto saved_Mp = Mp;
2781+
Mp++;
2782+
stuff_string(str, F_NAME);
27722783
int index = get_index_sexp_variable_name(str);
27732784

27742785
if (index > -1 && index < MAX_SEXP_VARIABLES)
@@ -2784,7 +2795,8 @@ void stuff_int_or_variable(int *i, int *var_index, bool need_positive_value)
27842795
}
27852796
else
27862797
{
2787-
2798+
Mp = saved_Mp;
2799+
stuff_string(str, F_NAME);
27882800
error_display(1, "Invalid variable name \"%s\" found.", str.c_str());
27892801
}
27902802

@@ -4088,6 +4100,24 @@ void consolidate_double_characters(char *src, char ch)
40884100
}
40894101
}
40904102

4103+
char *three_dot_truncate(char *buffer, const char *source, size_t buffer_size)
4104+
{
4105+
Assertion(buffer && source, "Arguments must not be null!");
4106+
4107+
// this would be silly
4108+
if (buffer_size < 6)
4109+
{
4110+
*buffer = '\0';
4111+
return buffer;
4112+
}
4113+
4114+
strncpy(buffer, source, buffer_size);
4115+
if (buffer[buffer_size - 1] != '\0')
4116+
strcpy(&buffer[buffer_size - 6], "[...]");
4117+
4118+
return buffer;
4119+
}
4120+
40914121
// Goober5000
40924122
// 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
40934123
// Note that the parameter here is max *length*, not max buffer size. Leave room for the null-terminator!

code/parse/parselo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ extern int get_index_of_first_hash_symbol(SCP_string &src, bool ignore_doubled_h
7979

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

82+
// for limiting strings that may be very long; useful for dialog boxes
83+
char *three_dot_truncate(char *buffer, const char *source, size_t buffer_size);
84+
8285
// white space
8386
extern int is_white_space(char ch);
8487
extern int is_white_space(unicode::codepoint_t cp);

0 commit comments

Comments
 (0)