1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Richard Klees <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This software is licensed under The MIT License. You should have received |
8
|
|
|
* a copy of the license along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Definition; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Rules\Ruleset; |
14
|
|
|
use Lechimp\Dicto\Variables as V; |
15
|
|
|
use Lechimp\Dicto\Rules as R; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Parser for Rulesets. |
19
|
|
|
*/ |
20
|
|
|
class RuleParser extends Parser implements ArgumentParser { |
21
|
|
|
const EXPLANATION_RE = "[/][*][*](([^*]|([*][^/]))*)[*][/]"; |
22
|
|
|
const SINGLE_LINE_COMMENT_RE = "[/][/]([^\n]*)"; |
23
|
|
|
const MULTI_LINE_COMMENT_RE = "[/][*](([^*]|([*][^/]))*)[*][/]"; |
24
|
|
|
const ASSIGNMENT_RE = "(\w+)\s*=\s*"; |
25
|
|
|
const STRING_RE = "[\"]((([\\\\][\"])|[^\"])+)[\"]"; |
26
|
|
|
const RULE_MODE_RE = "must|can(not)?"; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var V\Variable[] |
30
|
|
|
*/ |
31
|
|
|
protected $predefined_variables; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var R\Schema[] |
35
|
|
|
*/ |
36
|
|
|
protected $schemas; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var R\Property[] |
40
|
|
|
*/ |
41
|
|
|
protected $properties; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var V\Variable[] |
45
|
|
|
*/ |
46
|
|
|
protected $variables = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var R\Rule[] |
50
|
|
|
*/ |
51
|
|
|
protected $rules = array(); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string|null |
55
|
|
|
*/ |
56
|
|
|
protected $last_explanation = null; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param V\Variable[] $predefined_variables |
60
|
|
|
* @param R\Schema[] $schemas |
61
|
|
|
* @param V\Property[] $properties |
62
|
|
|
*/ |
63
|
35 |
|
public function __construct( array $predefined_variables |
64
|
|
|
, array $schemas |
65
|
|
|
, array $properties) { |
66
|
|
|
$this->predefined_variables = array_map(function(V\Variable $v) { |
67
|
35 |
|
return $v; |
68
|
35 |
|
}, $predefined_variables); |
69
|
|
|
$this->schemas = array_map(function(R\Schema $s) { |
70
|
35 |
|
return $s; |
71
|
35 |
|
}, $schemas); |
72
|
|
|
$this->properties = array_map(function(V\Property $p) { |
73
|
35 |
|
return $p; |
74
|
35 |
|
}, $properties); |
75
|
35 |
|
parent::__construct(); |
76
|
35 |
|
} |
77
|
|
|
|
78
|
|
|
// Definition of symbols in the parser |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritdocs |
82
|
|
|
*/ |
83
|
35 |
|
protected function add_symbols_to(SymbolTable $table) { |
84
|
35 |
|
$this->add_symbols_for_comments($table); |
85
|
|
|
|
86
|
35 |
|
$this->add_symbols_for_variables_to($table); |
87
|
|
|
|
88
|
35 |
|
$this->add_symbols_for_rules_to($table); |
89
|
|
|
|
90
|
|
|
// Strings |
91
|
35 |
|
$table->symbol(self::STRING_RE); |
92
|
|
|
|
93
|
|
|
// Assignment |
94
|
35 |
|
$table->symbol(self::ASSIGNMENT_RE); |
95
|
|
|
|
96
|
|
|
// Names |
97
|
|
|
$table->literal("\w+", function (array &$matches) { |
98
|
30 |
|
return $this->get_variable($matches[0]); |
99
|
35 |
|
}); |
100
|
|
|
|
101
|
|
|
// End of statement |
102
|
35 |
|
$table->symbol("\n"); |
103
|
35 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param SymbolTable |
107
|
|
|
* @return null |
108
|
|
|
*/ |
109
|
35 |
|
protected function add_symbols_for_comments(SymbolTable $table) { |
110
|
35 |
|
$table->symbol(self::EXPLANATION_RE); |
111
|
35 |
|
$table->symbol(self::SINGLE_LINE_COMMENT_RE); |
112
|
35 |
|
$table->symbol(self::MULTI_LINE_COMMENT_RE); |
113
|
35 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param SymbolTable |
117
|
|
|
* @return null |
118
|
|
|
*/ |
119
|
35 |
|
protected function add_symbols_for_variables_to(SymbolTable $table) { |
120
|
|
|
// Any |
121
|
35 |
|
$table->operator("{") |
122
|
|
|
->null_denotation_is(function() { |
123
|
6 |
|
$arr = array(); |
124
|
6 |
|
while(true) { |
125
|
6 |
|
$arr[] = $this->variable(0); |
126
|
6 |
|
if ($this->is_current_token_operator("}")) { |
127
|
6 |
|
$this->advance_operator("}"); |
128
|
6 |
|
return new V\Any($arr); |
129
|
|
|
} |
130
|
6 |
|
$this->advance_operator(","); |
131
|
6 |
|
} |
132
|
35 |
|
}); |
133
|
35 |
|
$table->operator("}"); |
134
|
35 |
|
$table->operator(","); |
135
|
|
|
|
136
|
|
|
// Except |
137
|
35 |
|
$table->symbol("except", 10) |
138
|
|
|
->left_denotation_is(function($left, array &$matches) { |
|
|
|
|
139
|
4 |
|
if (!($left instanceof V\Variable)) { |
140
|
|
|
throw new ParserException |
141
|
|
|
("Expected a variable at the left of except."); |
142
|
|
|
} |
143
|
4 |
|
$right = $this->variable(10); |
144
|
4 |
|
return new V\Except($left, $right); |
145
|
35 |
|
}); |
146
|
|
|
|
147
|
35 |
|
$this->add_symbols_for_properties_to($table, $this->properties); |
148
|
35 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param SymbolTable $table |
152
|
|
|
* @param V\Property[] $properties |
153
|
|
|
* @return null |
154
|
|
|
*/ |
155
|
35 |
|
protected function add_symbols_for_properties_to(SymbolTable $table, array &$properties) { |
156
|
35 |
|
foreach ($properties as $property) { |
157
|
35 |
|
$table->symbol($property->parse_as().":", 20) |
158
|
|
|
->left_denotation_is(function($left) use ($property) { |
159
|
6 |
|
if (!($left instanceof V\Variable)) { |
160
|
|
|
throw new ParserException |
161
|
|
|
("Expected a variable at the left of \"with name:\"."); |
162
|
|
|
} |
163
|
6 |
|
$this->is_start_of_rule_arguments = true; |
164
|
6 |
|
$arguments = $property->fetch_arguments($this); |
165
|
6 |
|
assert('is_array($arguments)'); |
166
|
6 |
|
return new V\WithProperty($left, $property, $arguments); |
167
|
35 |
|
}); |
168
|
35 |
|
} |
169
|
35 |
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param SymbolTable |
173
|
|
|
* @return null |
174
|
|
|
*/ |
175
|
35 |
|
protected function add_symbols_for_rules_to(SymbolTable $table) { |
176
|
|
|
// Rules |
177
|
35 |
|
$table->symbol("only"); |
178
|
35 |
|
$table->symbol(self::RULE_MODE_RE, 0) |
179
|
|
|
->null_denotation_is(function (array &$matches) { |
180
|
11 |
|
if ($matches[0] == "can") { |
181
|
3 |
|
return R\Rule::MODE_ONLY_CAN; |
182
|
|
|
} |
183
|
8 |
|
if ($matches[0] == "must") { |
184
|
4 |
|
return R\Rule::MODE_MUST; |
185
|
|
|
} |
186
|
4 |
|
if ($matches[0] == "cannot") { |
187
|
4 |
|
return R\Rule::MODE_CANNOT; |
188
|
|
|
} |
189
|
|
|
throw new \LogicException("Unexpected \"".$matches[0]."\"."); |
190
|
35 |
|
}); |
191
|
35 |
|
$this->add_symbols_for_schemas_to($table, $this->schemas); |
192
|
35 |
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param SymbolTable $table |
196
|
|
|
* @param R\Schema[] $schemas |
197
|
|
|
* @return null |
198
|
|
|
*/ |
199
|
35 |
|
protected function add_symbols_for_schemas_to(SymbolTable $table, array &$schemas) { |
200
|
35 |
|
foreach ($schemas as $schema) { |
201
|
35 |
|
$table->symbol($schema->name()) |
202
|
35 |
|
->null_denotation_is(function(array &$_) use ($schema) { |
|
|
|
|
203
|
11 |
|
return $schema; |
204
|
35 |
|
}); |
205
|
35 |
|
} |
206
|
35 |
|
} |
207
|
|
|
|
208
|
|
|
// IMPLEMENTATION OF Parser |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return Ruleset |
212
|
|
|
*/ |
213
|
35 |
|
public function parse($source) { |
214
|
35 |
|
$this->variables = array(); |
215
|
35 |
|
$this->rules = array(); |
216
|
35 |
|
$this->add_predefined_variables(); |
217
|
35 |
|
return parent::parse($source); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Root expression for the parser is some whitespace or comment where a |
222
|
|
|
* top level statement is in the middle. |
223
|
|
|
* |
224
|
|
|
* @return Ruleset |
225
|
|
|
*/ |
226
|
25 |
|
protected function root() { |
227
|
25 |
|
while (true) { |
228
|
|
|
// drop empty lines |
229
|
25 |
|
while ($tok = $this->is_current_token_to_be_dropped()) { |
230
|
11 |
|
$this->advance($tok); |
231
|
11 |
|
} |
232
|
25 |
|
if ($this->is_end_of_file_reached()) { |
233
|
25 |
|
break; |
234
|
|
|
} |
235
|
|
|
|
236
|
24 |
|
$this->top_level_statement(); |
237
|
24 |
|
} |
238
|
25 |
|
$this->purge_predefined_variables(); |
239
|
25 |
|
return new Ruleset($this->variables, $this->rules); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Parses the top level statements in the rules file. |
244
|
|
|
* |
245
|
|
|
* @return null |
246
|
|
|
*/ |
247
|
24 |
|
public function top_level_statement() { |
248
|
|
|
// A top level statements is either.. |
249
|
|
|
// ..an explanation |
250
|
24 |
|
if ($this->is_current_token_matched_by(self::EXPLANATION_RE)) { |
251
|
5 |
|
$m = $this->current_match(); |
252
|
5 |
|
$this->last_explanation = $this->trim_explanation($m[1]); |
253
|
5 |
|
$this->advance(self::EXPLANATION_RE); |
254
|
5 |
|
} |
255
|
|
|
// ..an assignment to a variable. |
256
|
24 |
|
elseif ($this->is_current_token_matched_by(self::ASSIGNMENT_RE)) { |
257
|
14 |
|
$this->variable_assignment(); |
258
|
14 |
|
$this->last_explanation = null; |
259
|
14 |
|
} |
260
|
|
|
// ..or a rule declaration |
261
|
|
|
else { |
262
|
11 |
|
$this->rule_declaration(); |
263
|
11 |
|
$this->last_explanation = null; |
264
|
|
|
} |
265
|
24 |
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Returns currently matched whitespace or comment token if there is any. |
269
|
|
|
* |
270
|
|
|
* @return string|null |
271
|
|
|
*/ |
272
|
25 |
|
public function is_current_token_to_be_dropped() { |
273
|
25 |
|
if ($this->is_current_token_matched_by("\n")) { |
274
|
11 |
|
return "\n"; |
275
|
|
|
} |
276
|
25 |
|
if ($this->is_current_token_matched_by(self::SINGLE_LINE_COMMENT_RE)) { |
277
|
2 |
|
return self::SINGLE_LINE_COMMENT_RE; |
278
|
|
|
} |
279
|
25 |
|
if ($this->is_current_token_matched_by(self::MULTI_LINE_COMMENT_RE)) { |
280
|
2 |
|
return self::MULTI_LINE_COMMENT_RE; |
281
|
|
|
} |
282
|
25 |
|
return null; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param string |
287
|
|
|
* @return string |
288
|
|
|
*/ |
289
|
5 |
|
protected function trim_explanation($content) { |
290
|
5 |
|
return trim( |
291
|
5 |
|
preg_replace("%\s*\n\s*([*]\s*)?%s", "\n", $content) |
292
|
5 |
|
); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
// EXPRESSION TYPES |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Fetch a rule mode from the stream. |
299
|
|
|
* |
300
|
|
|
* @return mixed |
301
|
|
|
*/ |
302
|
11 |
|
protected function rule_mode() { |
303
|
11 |
|
$this->is_current_token_matched_by(self::RULE_MODE_RE); |
304
|
11 |
|
$t = $this->current_symbol(); |
305
|
11 |
|
$m = $this->current_match(); |
306
|
11 |
|
$this->fetch_next_token(); |
307
|
11 |
|
$mode = $t->null_denotation($m); |
308
|
11 |
|
return $mode; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Fetch a string from the stream. |
313
|
|
|
* |
314
|
|
|
* @return string |
315
|
|
|
*/ |
316
|
14 |
|
protected function string() { |
317
|
14 |
|
if (!$this->is_current_token_matched_by(self::STRING_RE)) { |
318
|
|
|
throw new ParserException("Expected string."); |
319
|
|
|
} |
320
|
14 |
|
$m = $this->current_match(); |
321
|
14 |
|
$this->fetch_next_token(); |
322
|
14 |
|
return str_replace("\\\"", "\"", |
323
|
14 |
|
str_replace("\\n", "\n", |
324
|
14 |
|
$m[1])); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Fetch a variable from the stream. |
329
|
|
|
* |
330
|
|
|
* @return V\Variable |
331
|
|
|
*/ |
332
|
30 |
|
protected function variable($right_binding_power = 0) { |
333
|
30 |
|
$t = $this->current_symbol(); |
334
|
30 |
|
$m = $this->current_match(); |
335
|
30 |
|
$this->fetch_next_token(); |
336
|
30 |
|
$left = $t->null_denotation($m); |
337
|
|
|
|
338
|
30 |
|
while ($right_binding_power < $this->token[0]->binding_power()) { |
339
|
9 |
|
$t = $this->current_symbol(); |
340
|
9 |
|
$m = $this->current_match(); |
341
|
9 |
|
$this->fetch_next_token(); |
342
|
9 |
|
$left = $t->left_denotation($left, $m); |
343
|
9 |
|
} |
344
|
|
|
|
345
|
30 |
|
if (!($left instanceof V\Variable)) { |
346
|
|
|
throw new ParserException("Expected variable."); |
347
|
|
|
} |
348
|
|
|
|
349
|
30 |
|
return $left; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Fetch a rule schema and its arguments from the stream. |
354
|
|
|
* |
355
|
|
|
* @return array (R\Schema, array) |
356
|
|
|
*/ |
357
|
11 |
|
protected function schema() { |
358
|
11 |
|
$t = $this->current_symbol(); |
359
|
11 |
|
$m = $this->current_match(); |
360
|
11 |
|
$this->fetch_next_token(); |
361
|
11 |
|
$schema = $t->null_denotation($m); |
362
|
11 |
|
if (!($schema instanceof R\Schema)) { |
363
|
|
|
throw new ParserException("Expected name of a rule schema."); |
364
|
|
|
} |
365
|
11 |
|
return $schema; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
// TOP LEVEL STATEMENTS |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Process a variable assignment. |
372
|
|
|
* |
373
|
|
|
* @return null |
374
|
|
|
*/ |
375
|
14 |
|
protected function variable_assignment() { |
376
|
14 |
|
$m = $this->current_match(); |
377
|
14 |
|
$this->fetch_next_token(); |
378
|
14 |
|
$def = $this->variable(); |
379
|
14 |
|
if ($this->last_explanation !== null) { |
380
|
4 |
|
$def = $def->withExplanation($this->last_explanation); |
381
|
4 |
|
} |
382
|
14 |
|
$this->add_variable($m[1], $def); |
383
|
14 |
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Process a rule declaration. |
387
|
|
|
* |
388
|
|
|
* @return null |
389
|
|
|
*/ |
390
|
11 |
|
protected function rule_declaration() { |
391
|
11 |
|
if ($this->is_current_token_matched_by("only")) { |
392
|
3 |
|
$this->advance("only"); |
393
|
3 |
|
} |
394
|
11 |
|
$var = $this->variable(); |
395
|
11 |
|
$mode = $this->rule_mode(); |
396
|
11 |
|
$schema = $this->schema(); |
397
|
11 |
|
$this->is_start_of_rule_arguments = true; |
398
|
11 |
|
$arguments = $schema->fetch_arguments($this); |
399
|
11 |
|
assert('is_array($arguments)'); |
400
|
11 |
|
$rule = new R\Rule($mode, $var, $schema, $arguments); |
401
|
11 |
|
if ($this->last_explanation !== null) { |
402
|
1 |
|
$rule= $rule->withExplanation($this->last_explanation); |
403
|
1 |
|
} |
404
|
11 |
|
$this->rules[] = $rule; |
405
|
11 |
|
} |
406
|
|
|
|
407
|
|
|
|
408
|
|
|
// HANDLING OF VARIABLES |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Add a variable to the variables currently known. |
412
|
|
|
* |
413
|
|
|
* @param string $name |
414
|
|
|
* @param V\Variable $def |
415
|
|
|
* @return null |
416
|
|
|
*/ |
417
|
35 |
|
protected function add_variable($name, V\Variable $def) { |
418
|
35 |
|
assert('is_string($name)'); |
419
|
35 |
|
if (array_key_exists($name, $this->variables)) { |
420
|
|
|
throw new ParserException("Variable '$name' already defined."); |
421
|
|
|
} |
422
|
35 |
|
assert('$def instanceof Lechimp\\Dicto\\Variables\\Variable'); |
423
|
35 |
|
$this->variables[$name] = $def->withName($name); |
424
|
35 |
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Get a predefined variable. |
428
|
|
|
* |
429
|
|
|
* @param string $name |
430
|
|
|
* @return V\Variable |
431
|
|
|
*/ |
432
|
30 |
|
protected function get_variable($name) { |
433
|
30 |
|
if (!array_key_exists($name, $this->variables)) { |
434
|
|
|
throw new ParserException("Unknown variable '$name'."); |
435
|
|
|
} |
436
|
30 |
|
return $this->variables[$name]; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Add all predefined variables to the current set of variables. |
441
|
|
|
* |
442
|
|
|
* @return null |
443
|
|
|
*/ |
444
|
35 |
|
protected function add_predefined_variables() { |
445
|
35 |
|
foreach ($this->predefined_variables as $predefined_var) { |
446
|
35 |
|
$this->add_variable($predefined_var->name(), $predefined_var); |
447
|
35 |
|
} |
448
|
35 |
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Purge all predefined variables from the current set of variables. |
452
|
|
|
* |
453
|
|
|
* @return null |
454
|
|
|
*/ |
455
|
25 |
|
protected function purge_predefined_variables() { |
456
|
25 |
|
foreach ($this->predefined_variables as $predefined_var) { |
457
|
25 |
|
unset($this->variables[$predefined_var->name()]); |
458
|
25 |
|
} |
459
|
25 |
|
} |
460
|
|
|
|
461
|
|
|
// IMPLEMENTATION OF ArgumentParser |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* @var bool |
465
|
|
|
*/ |
466
|
|
|
protected $is_start_of_rule_arguments = false; |
467
|
|
|
|
468
|
14 |
|
protected function maybe_fetch_argument_delimiter() { |
469
|
14 |
|
if (!$this->is_start_of_rule_arguments) { |
470
|
|
|
$this->advance_operator(","); |
471
|
|
|
$this->is_start_of_rule_arguments = false; |
472
|
|
|
} |
473
|
14 |
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* @inheritdoc |
477
|
|
|
*/ |
478
|
10 |
|
public function fetch_string() { |
479
|
10 |
|
$this->maybe_fetch_argument_delimiter(); |
480
|
10 |
|
return $this->string(); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* @inheritdoc |
485
|
|
|
*/ |
486
|
6 |
|
public function fetch_variable() { |
487
|
6 |
|
$this->maybe_fetch_argument_delimiter(); |
488
|
6 |
|
return $this->variable(); |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.