|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\Compiler\Grammar; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Compiler\Grammar\PP2\Lexer; |
|
13
|
|
|
use Railt\Compiler\Grammar\PP2\Parser; |
|
14
|
|
|
use Railt\Compiler\Reader\GrammarInterface; |
|
15
|
|
|
use Railt\Compiler\Reader\Result; |
|
16
|
|
|
use Railt\Io\Readable; |
|
17
|
|
|
use Railt\Parser\Ast\LeafInterface; |
|
18
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Grammar |
|
22
|
|
|
*/ |
|
23
|
|
|
class PP2 implements GrammarInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var PP2 |
|
27
|
|
|
*/ |
|
28
|
|
|
private $parser; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $ast = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* PP2 constructor. |
|
37
|
|
|
* @throws \InvalidArgumentException |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->parser = new Parser(); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Readable $grammar |
|
46
|
|
|
* @return GrammarInterface |
|
47
|
|
|
* @throws \LogicException |
|
48
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
|
49
|
|
|
* @throws \Railt\Parser\Exception\UnrecognizedRuleException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function add(Readable $grammar): GrammarInterface |
|
52
|
|
|
{ |
|
53
|
|
|
/** @var RuleInterface|LeafInterface $rule */ |
|
54
|
|
|
foreach ($this->parse($grammar) as $rule) { |
|
55
|
|
|
switch ($rule->getName()) { |
|
56
|
|
|
case 'Pragma': |
|
57
|
|
|
echo \get_class($rule) . "\n"; |
|
58
|
|
|
break; |
|
59
|
|
|
case 'Include': |
|
60
|
|
|
echo \get_class($rule) . "\n"; |
|
61
|
|
|
break; |
|
62
|
|
|
case 'Rule': |
|
63
|
|
|
echo \get_class($rule) . "\n"; |
|
64
|
|
|
break; |
|
65
|
|
|
case 'Token': |
|
66
|
|
|
echo \get_class($rule) . "\n"; |
|
67
|
|
|
break; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param Readable $grammar |
|
76
|
|
|
* @return RuleInterface |
|
77
|
|
|
* @throws \LogicException |
|
78
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
|
79
|
|
|
* @throws \Railt\Parser\Exception\UnrecognizedRuleException |
|
80
|
|
|
*/ |
|
81
|
|
|
private function parse(Readable $grammar): RuleInterface |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->parser->parse($grammar); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return Result |
|
88
|
|
|
*/ |
|
89
|
|
|
public function make(): Result |
|
90
|
|
|
{ |
|
91
|
|
|
foreach ($this->ast as $rule) { |
|
92
|
|
|
echo $rule . "\n\n"; |
|
93
|
|
|
} |
|
94
|
|
|
die; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..