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; |
11
|
|
|
|
12
|
|
|
use Railt\Compiler\Grammar\Reader; |
13
|
|
|
use Railt\Io\Readable; |
14
|
|
|
use Railt\Parser\Driver\Proxy; |
15
|
|
|
use Railt\Parser\ParserInterface; |
16
|
|
|
use Zend\Code\Generator\ValueGenerator as Value; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Compiler |
20
|
|
|
*/ |
21
|
|
|
class Compiler extends Proxy |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string|null |
25
|
|
|
*/ |
26
|
|
|
private $namespace; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $class = 'Parser'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Readable $grammar |
35
|
|
|
* @return Compiler |
36
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
37
|
|
|
* @throws \Railt\Io\Exception\NotReadableException |
38
|
|
|
*/ |
39
|
|
|
public static function load(Readable $grammar): self |
40
|
|
|
{ |
41
|
|
|
$reader = new Reader($grammar); |
42
|
|
|
|
43
|
|
|
return new static($reader->getParser()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param ParserInterface $parser |
48
|
|
|
* @return Compiler |
49
|
|
|
*/ |
50
|
|
|
public static function fromParser(ParserInterface $parser): self |
51
|
|
|
{ |
52
|
|
|
return new static($parser); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $namespace |
57
|
|
|
* @return Compiler |
58
|
|
|
*/ |
59
|
|
|
public function setNamespace(string $namespace): self |
60
|
|
|
{ |
61
|
|
|
$this->namespace = $namespace; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $name |
68
|
|
|
* @return Compiler |
69
|
|
|
*/ |
70
|
|
|
public function setClassName(string $name): self |
71
|
|
|
{ |
72
|
|
|
$this->class = $name; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $path |
79
|
|
|
* @throws \Throwable |
80
|
|
|
*/ |
81
|
|
|
public function saveTo(string $path): void |
82
|
|
|
{ |
83
|
|
|
$pathName = $path . '/' . $this->class . '.php'; |
84
|
|
|
|
85
|
|
|
if (\is_file($pathName)) { |
86
|
|
|
\unlink($pathName); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
\file_put_contents($pathName, $this->build()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
* @throws \Throwable |
95
|
|
|
*/ |
96
|
|
|
public function build(): string |
97
|
|
|
{ |
98
|
|
|
\ob_start(); |
99
|
|
|
|
100
|
|
|
try { |
101
|
|
|
require __DIR__ . '/../resources/templates/parser.tpl.php'; |
102
|
|
|
return \ob_get_contents(); |
103
|
|
|
} catch (\Throwable $e) { |
104
|
|
|
throw $e; |
105
|
|
|
} finally { |
106
|
|
|
\ob_end_clean(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param mixed $value |
112
|
|
|
* @return string |
113
|
|
|
* @throws \Zend\Code\Exception\InvalidArgumentException |
114
|
|
|
* @throws \Zend\Code\Generator\Exception\RuntimeException |
115
|
|
|
*/ |
116
|
|
|
protected function render($value): string |
117
|
|
|
{ |
118
|
|
|
$generator = new Value($value, Value::TYPE_AUTO, Value::OUTPUT_SINGLE_LINE); |
119
|
|
|
|
120
|
|
|
return $generator->generate(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|