-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.cpp
406 lines (383 loc) · 12.9 KB
/
Parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "Parser.h"
#include "constants.h"
#include "parsetools.cpp"
#include "Player.h"
#define NEWLINE_HACK "(*-*)"
Parser::Parser(char* filename) {
ifstream file;
string line;
string data;
file.open(filename);
if (file.is_open()) {
while (getline(file, line)) {
data += line;
data += NEWLINE_HACK;
}
file.close();
} else {
cerr << FILE_DID_NOT_OPEN << endl;
this->errors.push_front(FILE_DID_NOT_OPEN);
}
this->file_data = data;
}
/* Calls all methods associated with parsing the file */
list<string> Parser::ParseFile(void) {
if (this->errors.size() == 0) {
this->stripComments();
this->ParseDefaults();
this->ParsePlayer();
this->ParseLocations();
// Player's inventory
inventory = new Location("inventory", "inventory", "Player's inventory.");
this->locations.insert(pair<string, Location*>("inventory", inventory));
this->ParseItems();
// Check for initialLocation
if (this->file_data.find("initialLocation") == string::npos) {
cerr << BAD_INITIAL_LOCATION << endl;
}
string location_name = ParseVariableData(this->file_data, "initialLocation");
if (this->locations.count(location_name) > 0) {
this->initialLocation = this->locations.find(location_name)->second;
} else {
cerr << BAD_LOCATION << endl;
exit (1);
}
}
return this->errors;
}
/* Strips all comments from the users game */
int Parser::stripComments() {
// Find all block comments and remove
unsigned int start, end, size;
start = this->file_data.find("/*");
while (start < this->file_data.size()) {
end = this->file_data.find("*/");
if (end < this->file_data.size()) {
size = (end + 2) - start;
this->file_data.replace(start, size, "");
} else {
/* Find Line number and report error */
string error_message = ERROR_UNCLOSED_COMMENT;
this->errors.push_front(error_message);
return this->errors.size();
}
start = this->file_data.find("/*");
}
// Remove Line Comments
start = this->file_data.find("//");
while (start < this->file_data.size()) {
end = this->file_data.find(NEWLINE_HACK, start);
size = (end + 5) - start;
this->file_data.replace(start, size, "");
start = this->file_data.find("//");
}
// Fix Newline Hack (replace with \n)
start = this->file_data.find(NEWLINE_HACK);
while (start < this->file_data.size()) {
this->file_data.replace(start, 5, "\n");
start = this->file_data.find(NEWLINE_HACK);
}
return NO_ERRORS;
}
/* Parses the default settings for verbs */
int Parser::ParseDefaults() {
unsigned int start, end;
string data;
start = this->file_data.find("ItemDefaults");
if (start < this->file_data.size()) {
// Parse verb expressions
start = this->file_data.find("{", start) + 1;
end = ParseEndBrace(start, this->file_data);
data = this->file_data.substr(start, end - start);
default_verb_expressions = ParseVerbs(data);
// Remove default data to avoid collision of items
start = this->file_data.find("ItemDefaults");
this->file_data.replace(start, end + 1 - start, "");
}
start = this->file_data.find("LocationDefaults");
if (start < this->file_data.size()) {
// Parse verb expressions
start = this->file_data.find("{", start) + 1;
end = ParseEndBrace(start, this->file_data);
data = this->file_data.substr(start, end - start);
default_location_verb_expressions = ParseVerbs(data);
// Remove default data to avoid collision of items
start = this->file_data.find("LocationDefaults");
this->file_data.replace(start, end + 1 - start, "");
}
this->gameName = ParseStringData(this->file_data, "gameName", "");
this->gameCredits = ParseStringData(this->file_data, "gameCredits", "");
this->initialDescription = ParseStringData(this->file_data, "initialDescription", "");
this->defaultResponse = ParseStringData(this->file_data, "defaultResponse", "You do not know how to");
this->defaultInventoryName = ParseStringData(this->file_data, "defaultInventoryName", "Inventory: ");
this->defaultInteractiveName = ParseStringData(this->file_data, "defaultInteractiveName", "Objects: ");
this->defaultNoObjects = ParseStringData(this->file_data, "defaultNoObjects", "None ");
this->restoreGameChoice = ParseStringData(this->file_data, "restoreGameChoice", "Restore Game.");
this->restoreGameMessage = ParseStringData(this->file_data, "restoreGameMessage", "Restore game from slot 0-9.");
this->newGameChoice = ParseStringData(this->file_data, "newGameChoice", "New Game.");
this->saveGame = ParseStringData(this->file_data, "saveGame", "save");
this->saveGameMessage = ParseStringData(this->file_data, "saveGameMessage", "Save game to slot 0-9.");
this->gameSavedMessage = ParseStringData(this->file_data, "gameSavedMessage", "Game successfully saved!");
return NO_ERRORS;
}
/* Parses all locations in the game */
int Parser::ParseLocations() {
// Find all block comments and remove
unsigned int start, end, size;
// Parse the Location names first time through
start = this->file_data.find("Location");
while (start < this->file_data.size()) {
if (this->file_data.at(start - 1) == 'l' || this->file_data.at(start - 1) == 'n') {
end = start + 9;
} else {
end = this->file_data.find("{", start);
if (end < this->file_data.size() && end < this->file_data.find(";", start)) {
start += 9;
size = (end) - start;
// Check for Location variable name
if (size == 0) {
cerr << NO_LOCATION_VARIABLE_NAME << endl;
exit (1);
} else if (size == 1 && this->file_data.substr(start, size) == " ") {
cerr << NO_LOCATION_VARIABLE_NAME << endl;
exit (1);
} else if (size > 1 && stringTrim(this->file_data.substr(start, size)) == " ") {
cerr << NO_LOCATION_VARIABLE_NAME << endl;
exit (1);
}
Location* location = new Location();
string location_name = stringTrim(this->file_data.substr(start, size));
location->setVariableName(location_name);
this->file_data.replace(start, size, location_name + " ");
this->locations[location_name] = location;
}
}
if (end < this->file_data.size()) {
start = this->file_data.find("Location", end);
} else {
break;
}
}
// Parse the Location details second time through
map<string, Location*>::iterator it;
for (it = this->locations.begin(); it != this->locations.end(); it++) {
string search = "Location " + it->first + " {";
start = this->file_data.find(search) + search.length();
if (start < this->file_data.size()) {
end = ParseEndBrace(start, this->file_data);
size = (end) - start;
string data = this->file_data.substr(start, size);
ParseLocation(data, it->second);
} else {
cout << BAD_LOCATION << endl;
}
}
return NO_ERRORS;
}
/* Parse the player object */
int Parser::ParsePlayer() {
// Find all block comments and remove
unsigned int start, end, size;
string attribute, data;
// Parse the Items first time through*/
start = this->file_data.find("Player ");
if (start < this->file_data.size()) {
end = this->file_data.find("{", start);
if (end < this->file_data.size()) {
start += 7;
size = end - start;
// Check for Player variable name
if (size == 0) {
cerr << NO_PLAYER_VARIABLE_NAME << endl;
exit (1);
} else if (size == 1 && this->file_data.substr(start, size) == " ") {
cerr << NO_PLAYER_VARIABLE_NAME << endl;
exit (1);
} else if (size > 1 && stringTrim(this->file_data.substr(start, size)) == " ") {
cerr << NO_PLAYER_VARIABLE_NAME << endl;
exit (1);
}
player = new Player();
attribute = stringTrim(this->file_data.substr(start, size));
player->setVariableName(attribute);
end++;
data = this->file_data.substr(end, ParseEndBrace(end, this->file_data));
attribute = ParseVariableData(data, "carryLimit");
player->setMaxItems(atoi(attribute.c_str()));
}
}
return NO_ERRORS;
}
/* Parse all items used in the game */
int Parser::ParseItems() {
// Find all block comments and remove
unsigned int start, end, size;
// Parse the Items first time through
start = this->file_data.find("Item ");
while (start < this->file_data.size()) {
end = this->file_data.find("{", start);
if (end < this->file_data.size()) {
if (this->file_data.at(start - 1) != 't') {
start += 5;
size = (end) - start;
// Check for Item variable name
if (size == 0) {
cerr << NO_ITEM_VARIABLE_NAME << endl;
exit (1);
} else if (size == 1 && this->file_data.substr(start, size) == " ") {
cerr << NO_ITEM_VARIABLE_NAME << endl;
exit (1);
} else if (size > 1 && stringTrim(this->file_data.substr(start, size)) == " ") {
cerr << NO_ITEM_VARIABLE_NAME << endl;
exit (1);
}
Item *item = new Item();
string item_name = stringTrim(this->file_data.substr(start, size));
this->file_data.replace(start, size, item_name + " ");
item->setVariableName(item_name);
this->items[item_name] = item;
}
}
start = this->file_data.find("Item ", end);
}
// Parse the Item details second time through
map<string, Item*>::iterator it;
for (it = this->items.begin(); it != this->items.end(); it++) {
string search = "Item " + it->first + " {";
start = this->file_data.find(search) + search.length();
if (start < this->file_data.size()) {
end = ParseEndBrace(start, this->file_data);
size = (end) - start;
string data = this->file_data.substr(start, size);
ParseItem(data, it->second);
} else {
cout << NO_ITEM << endl;
}
}
return NO_ERRORS;
}
/* Parses a location */
void Parser::ParseLocation(string data, Location *location) {
string attribute;
Location *link;
unsigned int start, end;
// Parse Name
attribute = ParseStringData(data, "name");
if (validAttribute(attribute)) {
location->setName(attribute);
start = data.find("name");
end = data.find(";", start) + 1;
data.replace(start, end - start, "");
} else {
cout << NO_LOCATION_NAME << endl;
}
// Parse Description
attribute = ParseStringData(data, "description");
if (validAttribute(attribute)) {
location->setDescription(attribute);
start = data.find("description");
end = data.find(";", start) + 1;
data.replace(start, end - start, "");
} else {
cout << NO_LOCATION_DESCRIPTION << ": " << location->getName() << endl;
}
// Parse Exits
attribute = ParseVariableData(data, "north");
if (validAttribute(attribute)) {
if (this->locations.count(attribute) > 0) {
link = this->locations.at(attribute);
location->setNorth(link);
} else {
cerr << NO_JOINED_LOCATION << attribute << endl;
}
}
attribute = ParseVariableData(data, "south");
if (validAttribute(attribute)) {
if (this->locations.count(attribute) > 0) {
link = this->locations.at(attribute);
location->setSouth(link);
} else {
cerr << NO_JOINED_LOCATION << attribute << endl;
}
}
attribute = ParseVariableData(data, "east");
if (validAttribute(attribute)) {
if (this->locations.count(attribute) > 0) {
link = this->locations.at(attribute);
location->setEast(link);
} else {
cerr << NO_JOINED_LOCATION << attribute << endl;
}
}
attribute = ParseVariableData(data, "west");
if (validAttribute(attribute)) {
if (this->locations.count(attribute) > 0) {
link = this->locations.at(attribute);
location->setWest(link);
} else {
cerr << NO_JOINED_LOCATION << attribute << endl;
}
}
// Parse hide items
if (data.find("hideItems;") != string::npos) {
location->setShowItems(false);
}
}
/* Parses a particular item */
void Parser::ParseItem(string data, Item *item) {
string attribute;
// Parse Name
attribute = ParseStringData(data, "name");
if (validAttribute(attribute)) {
item->setName(attribute);
} else {
cout << NO_ITEM_NAME << endl;
}
// Parse Description
attribute = ParseStringData(data, "description");
if (validAttribute(attribute)) {
item->setDescription(attribute);
} else {
cout << NO_ITEM_DESCRIPTION << endl;
}
// Parse location
attribute = ParseVariableData(data, "location");
if (attribute == this->player->getVariableName()) {
this->locations.insert(pair<string, Location*>("inventory", inventory));
item->setLocation(this->locations.at("inventory"));
} else if (validAttribute(attribute)) {
if (this->locations.count(attribute) > 0) {
item->setLocation(this->locations.at(attribute));
} else if (this->items.count(attribute) > 0) {
item->setItem(this->items.at(attribute));
} else {
cerr << BAD_LOCATION << attribute << endl;
exit (1);
}
} else {
cerr << NO_ITEM_LOCATION << endl;
}
// Parse verbs
item->addVerbs(this->default_verb_expressions);
item->addVerbs(ParseVerbs(data));
// Parse attributes
attribute = ParseVariableData(data, "hasAttributes");
if (validAttribute(attribute)) {
item->setAttributeString(attribute);
}
// Parse hide items
if (data.find("hideItems;") != string::npos) {
item->setShowItems(false);
}
}
Parser::~Parser() {
map<string, Location*>::iterator locations;
for (locations = this->locations.begin(); locations != this->locations.end(); locations++) {
delete locations->second;
}
map<string, Item*>::iterator objects;
for (objects = this->items.begin(); objects != this->items.end(); objects++) {
delete objects->second;
}
delete this->player;
}