-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.php
479 lines (361 loc) · 10.1 KB
/
parser.php
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
476
477
478
479
<?php
/**
* parser.php
*/
namespace phphaml;
/**
* The Parser class forms the root of a source document, and handles traversing and delegating the
* source lines.
*/
abstract class Parser {
/**
* A flag representing that less indentation is required on the next line.
*/
const EXPECT_LESS = 1;
/**
* A flag representing that the same indentation is required on the next line.
*/
const EXPECT_SAME = 2;
/**
* A flag representing that more indentation is required on the next line.
*/
const EXPECT_MORE = 4;
/**
* A flag representing that any indentation can be given on the next line.
*/
const EXPECT_ANY = 7;
/**
* An array of line handler classes for this parser.
*/
protected static $handlers = array();
/**
* An array of options affecting parsing or output generation.
*/
protected $options = array();
/**
* An array of variable => value pairs to substitute into the render.
*/
protected $variables = array();
/**
* The source this parser is parsing.
*/
protected $source;
/**
* The indentation string used in the source being parsed.
*/
protected $indent_string;
/**
* The root node of the tree.
*/
protected $root;
/**
* The context node in the tree.
*
* This will be the parent of any created nodes.
*/
protected $context;
/**
* The number of the line being handled.
*/
protected $line_number;
/**
* The indentation level of the line being handled.
*/
protected $indent_level;
/**
* The content of the line being handled.
*/
protected $content;
/**
* A flag indicating what indentation to expect on the next line relative to the current
* indentation.
*/
protected $expect_indent = self::EXPECT_SAME;
/**
* When set, the named handler will be used on the next non-empty source line.
*/
protected $force_handler = false;
/**
* When !== false, the context will not change.
*/
protected $context_locked = false;
/**
* Finds handlers in this Parser's directory.
*/
protected static function find_handlers() {
$class_info = Library::get_class_info(get_called_class());
$handler_namespace = $class_info['namespace'] . '\\handlers';
foreach(scandir(Library::directory_from_namespace($handler_namespace)) as $file_name) {
if($file_name[0] == '.')
continue;
$handler = $handler_namespace . '\\' . Library::class_name_from_file_name($file_name);
$trigger = $handler::trigger();
if(!$trigger)
continue;
if(is_array($trigger)) {
foreach($trigger as $_trigger)
static::$handlers[$_trigger] = $handler;
} else
static::$handlers[$trigger] = $handler;
}
if(empty(static::$handlers))
throw new Exception('Sanity error: there are no handlers');
uksort(static::$handlers, function($a, $b) {
if(strlen($a) < strlen($b)) return 1;
if(strlen($a) == strlen($b)) return 0;
if(strlen($a) > strlen($b)) return -1;
});
}
/**
* Accessor for {$options}.
*/
public function options() {
return $this->options;
}
/**
* Accessor for individual options.
*/
public function option($key) {
if(!isset($this->options[$key]))
throw new Exception('Sanity error: unknown option - ' . $key);
return $this->options[$key];
}
/**
* Accessor for {$variables}.
*/
public function variables() {
return $this->variables;
}
/**
* Accessor for {$indent_string}.
*/
public function indent_string() {
return $this->indent_string;
}
/**
* Accessor for {$context}.
*/
public function context() {
return $this->context;
}
/**
* Accessor for {$line_number}.
*/
public function line_number() {
return $this->line_number;
}
/**
* Accessor for {$indent_level}.
*/
public function indent_level() {
return $this->indent_level;
}
/**
* Accessor for {$content}.
*/
public function content() {
return $this->content;
}
/**
* Sets the indent expectation.
*/
public function expect_indent($flag) {
$this->expect_indent = $flag;
}
/**
* Forces the next source line to be handled by the given handler.
*/
public function force_handler($handler) {
if($handler[0] != '\\')
$handler = '\\' . $handler;
if(!in_array($handler, static::$handlers))
$this->exception('Sanity error: cannot force unregistered handler');
$this->force_handler = $handler;
}
/**
* Locks the context so that it will not change until the indent level is
* next equal to the current indent level.
*/
public function lock_context() {
if($this->context_locked !== false)
$this->exception('Sanity error: cannot lock context - it\'s already locked');
$this->context_locked = $this->indent_level;
}
/**
* Accessor for {$context_locked}.
*/
public function context_locked() {
return $this->context_locked;
}
/**
* Initialises the parser with given source and options.
*/
public function __construct($source, array $options = array()) {
if(empty(static::$handlers))
static::find_handlers();
$this->options = array_merge($this->options, $options);
if(!is_file($source))
$this->source = explode("\n", str_replace(array("\r\n", "\r"), "\n", $source));
else {
$sub = array('file' => $source);
if(!file_exists($source))
throw new Exception('File error: given file does not exist - :file', $sub);
if(!is_readable($source))
throw new Exception('File error: given file cannot be read - :file', $sub);
if(!($this->source = fopen($source, 'r')))
throw new Exception('File error: given file could not be opened - :file', $sub);
}
}
/**
* Parses the source.
*
* Most actual parsing is delegated to discovered handlers.
*/
public function parse() {
Handler::set_parser($this);
$node_namespace = Library::namespace_from_class(get_called_class()) . '\\' . 'nodes';
$root_class = $node_namespace . '\\Node';
$this->root = new $root_class;
$this->root->set_from_parser($this);
$this->context = $this->root;
$this->line_number = 0;
$this->indent_level = 0;
$this->force_handler = false;
$this->context_locked = false;
while(($this->content = $this->get_line()) !== false) {
$this->line_number ++;
$this->update_context();
if($this->content = trim($this->content))
$this->handle();
}
foreach(static::$handlers as $handler)
$handler::reset();
}
/**
* Renders the parsed tree.
*/
abstract public function render();
/**
* Gets a line from the source.
*/
public function get_line() {
static $get_line;
if(!$get_line) {
if(is_array($this->source)) {
$get_line = function(&$source) {
$line = each($source);
if(!$line)
return false;
else
return $line[1];
};
} elseif(is_resource($this->source)) {
$get_line = function(&$source) {
if(feof($source))
return false;
else
return rtrim(fgets($source), "\r\n");
};
} else
throw new Exception('Sanity error: unexpected source type - ' . typeof($this->source));
}
return $get_line($this->source);
}
/**
* Updates the context based on the indent level, and checks the indent level
* is as expected.
*/
protected function update_context() {
$re = '/^\s+/';
preg_match($re, $this->content, $match);
if($match) {
if($this->indent_string) {
$indent_level = substr_count($match[0], $this->indent_string);
} else {
$indent_level = 1;
$this->indent_string = $match[0];
}
if(str_replace($this->indent_string, '', $match[0]))
$this->exception('Parse error: mixed indentation');
} else {
$indent_level = 0;
}
if($this->context_locked !== false) {
if($indent_level <= $this->context_locked) {
$this->indent_level = $indent_level = $this->context_locked;
$this->force_handler = false;
$this->context_locked = false;
}
}
if($this->context_locked === false) {
if($indent_level < $this->indent_level) {
if(!($this->expect_indent & self::EXPECT_LESS))
$this->exception('Parse error: unexpected indentation decrease');
$difference = $this->indent_level - $indent_level;
while($difference--)
$this->context = $this->context->parent;
}
if($indent_level == $this->indent_level) {
if(!($this->expect_indent & self::EXPECT_SAME))
$this->exception('Parse error: expected indentation change');
}
if($indent_level > $this->indent_level) {
if(!($this->expect_indent & self::EXPECT_MORE))
$this->exception('Parse error: unexpected indentation increase');
if($indent_level - $this->indent_level > 1)
$this->exception('Parse error: indent increased by more than 1');
if(empty($this->context->children))
$this->exception('Parse error: indent increased without parent node');
$this->context = end($this->context->children);
}
}
$this->indent_level = $indent_level;
$this->expect_indent = self::EXPECT_ANY;
}
/**
* Handles a line of source.
*/
public function handle() {
$handled = false;
if($this->force_handler) {
$handled = true;
$handler = $this->force_handler;
$this->force_handler = false;
$handler::handle();
} else {
foreach(static::$handlers as $trigger => $handler) {
if($trigger == '*')
continue; // Leave the wildcard trigger until last.
if(substr($this->content, 0, strlen($trigger)) == $trigger) {
try {
$handler::handle($this);
$handled = true;
break;
} catch(NotHandledException $e) {
// Fall through
}
}
}
}
if(!$handled and isset(static::$handlers['*'])) {
$handled = true;
$handler = static::$handlers['*'];
$handler::handle();
}
if(!$handled)
$this->exception('Parse error: unexpected input');
}
/**
* Throws an exception, and appends the line number to the given message.
*/
public function exception($message, array $sub = array()) {
$sub['line'] = $this->line_number;
throw new Exception($message . ' - line :line', $sub);
}
}
/**
* An exception thrown when a handler does not handle a source line.
*/
class NotHandledException extends Exception {
}
?>