forked from greyblue9/text-adventure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.class.inc
475 lines (426 loc) · 11.3 KB
/
Parser.class.inc
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/**
Grammar for recursive descent parser
[BEGIN] -> [GLOBAL] [BEGIN0]
[BEGIN0] -> [GLOBAL] [BEGIN0]
-> <epsilon>
[GLOBAL] -> [AREA]
-> [OBJECT]
-> [GAME]
[AREA] -> @area [AREA0]
[AREA0] -> @name id [AREA1]
-> @title [TITLE] [AREA1]
-> @description <string> [AREA1]
-> @objects <csv id-string> [AREA1]
-> @exits [EXITS] [AREA1]
[AREA1] -> [AREA0]
-> <epsilon>
[TITLE] -> <string>
-> [SEM] <string>
[EXITS] -> [SEM] [EXITS0]
[EXITS0] -> [EXITS]
-> id [EXITS1]
[EXITS1] -> [EXITS]
-> <epsilon>
[SEM] -> <semantic token>
[OBJECT] -> @object [OBJECT0]
[OBJECT0] -> @name id [OBJECT1]
-> @title [TITLE] [OBJECT1]
-> @description <string> [OBJECT1]
-> @tagline <string> [OBJECT1]
-> @carry? [VALUE] [CARRYSTR] [OBJECT1]
[CARRYSTR] -> <string>
-> <epsilon>
[OBJECT1] -> [OBJECT0]
-> <epsilon>
[GAME] -> @game [GAME0]
[GAME0] -> @start id [GAME1]
[GAME1] -> [GAME0]
-> <epsilon>
[VALUE] -> <value token>
*/
require_once('Tokenizer.class.inc');
// Game data classes
require_once('Area.class.inc');
require_once('Object.class.inc');
require_once('Exit.class.inc');
class Parser
{
private $tokenizer;
private $token;
public $areas;
public $objects;
public $metadata;
function __construct($pTokenizer)
{
$this->tokenizer = $pTokenizer;
$this->areas = array();
$this->objects = array();
$this->metadata = array();
}
function parse()
{
$this->tokenizer->goToStart();
$this->getNextToken();
return $this->BEGIN();
}
private function BEGIN()
{
return ($this->_GLOBAL() && $this->BEGIN0());
}
private function BEGIN0()
{
$this->getNextToken();
if ($this->token === false)
{
return true;
}
else
{
return ($this->_GLOBAL() && $this->BEGIN0());
}
}
private function _GLOBAL()
{
if ($this->token->name == TK_AREA)
{
$this->getNextToken();
return $this->AREA();
}
else if ($this->token->name == TK_OBJECT)
{
$this->getNextToken();
return $this->_OBJECT();
}
else if ($this->token->name == TK_GAME)
{
$this->getNextToken();
return $this->GAME();
}
else
{
print("Expected global token on line {$this->token->lineNum}");
print(" (found {$this->token})");
return false;
}
}
private function AREA()
{
$newArea = new Area();
$this->areas[] = &$newArea;
return $this->AREA0($newArea);
}
private function AREA0(&$pNewArea)
{
if ($this->token->name == TK_NAME
&& $this->getNextToken()->type == STRING_TOKEN)
{
$pNewArea->name = $this->token->name;
return $this->AREA1($pNewArea);
}
else if ($this->token->name == TK_TITLE)
{
$this->getNextToken();
return ($this->TITLE($pNewArea) && $this->AREA1($pNewArea));
}
else if ($this->token->name == TK_DESCRIPTION
&& $this->getNextToken()->type == STRING_TOKEN)
{
$pNewArea->description = $this->token->name;
return $this->AREA1($pNewArea);
}
else if ($this->token->name == TK_OBJECTS
&& $this->getNextToken()->type == STRING_TOKEN)
{
$objectsCSV = $this->token->name;
$objects = explode(',', $objectsCSV);
foreach ($objects as $object)
{
$pNewArea->objects[] = trim($object);
}
return $this->AREA1($pNewArea);
}
else if ($this->token->name == TK_EXITS)
{
$this->getNextToken();
return ($this->EXITS($pNewArea) && $this->AREA1($pNewArea));
}
else
{
print("Invalid area field token on line {$this->token->lineNum}");
print(" (found {$this->token})");
return false;
}
}
private function AREA1(&$pNewArea)
{
$this->getNextToken();
if ($this->token === false)
{
// reached end
return true;
}
if ($this->token->type != GLOBAL_TOKEN)
{
// more area fieldset
return $this->AREA0($pNewArea);
}
else
{
$this->tokenizer->backUpOne();
return true;
}
}
/**
shared by @area and @object construction
Note: Title "article" does not currently apply to @areas.
*/
private function TITLE(&$pNewItem)
{
if ($this->token->type == SEMANTIC_TOKEN)
{
$pNewItem->titleArticle = ($this->token->name == TK_AN)? 'an': 'a';
$this->getNextToken();
}
if ($this->token->type == STRING_TOKEN)
{
$pNewItem->title = $this->token->name;
return true;
}
else
{
print("Invalid token for title on line {$this->token->lineNum}");
print(" (found {$this->token})");
return false;
}
}
/**
exits are defined with tokens in this order:
@sem [@sem..] string
@sem [@sem..] string
*/
private function EXITS(&$pNewArea)
{
return ($this->SEM($pNewArea) && $this->EXITS0($pNewArea));
}
private function EXITS0(&$pNewArea)
{
$this->getNextToken();
if ($this->token === false)
{
print("Incomplete exit on line {$this->token->lineNum}");
return false;
}
else if ($this->token->type == STRING_TOKEN)
{
// Save destination area for last exit
$exits = &$pNewArea->outlets;
$exits[count($exits)-1]->destArea = $this->token->name;
$this->getNextToken();
return $this->EXITS1($pNewArea);
}
else
{
return $this->EXITS($pNewArea);
}
}
private function EXITS1(&$pNewArea)
{
if ($this->token->type == SEMANTIC_TOKEN)
{
return $this->EXITS($pNewArea);
}
else
{
$this->tokenizer->backUpOne();
return true;
}
}
private function SEM(&$pNewArea)
{
if ($this->token->type == SEMANTIC_TOKEN)
{
if (count($pNewArea->outlets) == 0)
{
// Create first exit for area
$newExit = new Outlet();
$newExit->tags[] = $this->token->name;
$pNewArea->outlets[] = $newExit;
}
else
{
// Existing exits
$exits = &$pNewArea->outlets;
$lastExitIndex = count($exits) - 1;
if ($exits[$lastExitIndex]->destArea === false)
{ // Last exit has no dest. area
// Add tag to last exit
$exits[$lastExitIndex]->tags[] = $this->token->name;
}
else
{
// create new exit
$newExit = new Outlet();
$newExit->tags[] = $this->token->name;
$pNewArea->outlets[] = $newExit;
}
}
return true;
}
return false;
}
private function _OBJECT()
{
$newObject = new Object();
$this->objects[] = &$newObject;
return $this->OBJECT0($newObject);
}
private function OBJECT0(&$pNewObject)
{
if ($this->token->name == TK_NAME
&& $this->getNextToken()->type == STRING_TOKEN)
{
$pNewObject->name = $this->token->name;
return $this->OBJECT1($pNewObject);
}
else if ($this->token->name == TK_TITLE)
{
$this->getNextToken();
return ($this->TITLE($pNewObject) && $this->OBJECT1($pNewObject));
}
else if ($this->token->name == TK_DESCRIPTION
&& $this->getNextToken()->type == STRING_TOKEN)
{
$pNewObject->description = $this->token->name;
return $this->OBJECT1($pNewObject);
}
else if ($this->token->name == TK_TAGLINE
&& $this->getNextToken()->type == STRING_TOKEN)
{
$pNewObject->tagline = $this->token->name;
return $this->OBJECT1($pNewObject);
}
else if ($this->token->name == TK_CARRY)
{
if ($this->getNextToken()->type == VALUE_TOKEN)
{
$pNewObject->canCarry = ($this->token->name == TK_YES)? true: false;
$this->getNextToken();
return ($this->CARRYSTR($pNewObject) && $this->OBJECT1($pNewObject));
}
else
{
print("Expected value token for carry on line {$this->token->lineNum}");
print(" (found {$this->token})");
return false;
}
}
else if ($this->token->name == TK_NOUNS
&& $this->getNextToken()->type == STRING_TOKEN)
{
$nounsCSV = $this->token->name;
$nouns = explode(',', $nounsCSV);
foreach ($nouns as $noun)
{
$pNewObject->nouns[] = trim($noun);
}
return $this->OBJECT1($pNewObject);
}
else if ($this->token->name == TK_ADJS
&& $this->getNextToken()->type == STRING_TOKEN)
{
$adjsCSV = $this->token->name;
$adjs = explode(',', $adjsCSV);
foreach ($adjs as $adj)
{
$pNewObject->adjs[] = trim($adj);
}
return $this->OBJECT1($pNewObject);
}
else
{
print("Invalid object field token on line {$this->token->lineNum}");
print(" (found {$this->token})");
return false;
}
}
private function OBJECT1(&$pNewObject)
{
$this->getNextToken();
if ($this->token === false)
{
// reached end
return true;
}
else if ($this->token->type != GLOBAL_TOKEN)
{
// more objects fieldset
return $this->OBJECT0($pNewObject);
}
else
{
$this->tokenizer->backUpOne();
return true;
}
}
private function CARRYSTR(&$pNewObject)
{
if ($this->token->type == STRING_TOKEN)
{
$pNewObject->carryStr = $this->token->name;
return true;
}
else
{
$this->tokenizer->backUpOne();
return true;
}
}
private function GAME()
{
return $this->GAME0();
}
private function GAME0()
{
if ($this->token->name == TK_START
&& $this->getNextToken()->type == STRING_TOKEN)
{
$this->metadata['start'] = $this->token->name;
return $this->GAME1();
}
else
{
print("Invalid game field token on line {$this->token->lineNum}");
print(" (found {$this->token})");
return false;
}
}
private function GAME1()
{
$this->getNextToken();
if ($this->token === false)
{
// reached end
return true;
}
if ($this->token->type != GLOBAL_TOKEN)
{
// more game fieldset
return $this->GAME0();
}
else
{
$this->tokenizer->backUpOne();
return true;
}
}
private function VALUE()
{
return ($this->token->type == VALUE_TOKEN);
}
private function getNextToken()
{
$this->token = $this->tokenizer->getNextToken();
return $this->token;
}
}