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
|
|
|
*/ |
37
|
|
|
public static function read(Readable $grammar): Compiler |
38
|
|
|
{ |
39
|
|
|
$reader = new Reader($grammar); |
40
|
|
|
|
41
|
|
|
return new static($reader->getParser()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param ParserInterface $parser |
46
|
|
|
* @return Compiler |
47
|
|
|
*/ |
48
|
|
|
public static function fromParser(ParserInterface $parser): Compiler |
49
|
|
|
{ |
50
|
|
|
return new static($parser); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $namespace |
55
|
|
|
* @return Compiler |
56
|
|
|
*/ |
57
|
|
|
public function setNamespace(string $namespace): Compiler |
58
|
|
|
{ |
59
|
|
|
$this->namespace = $namespace; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $name |
66
|
|
|
* @return Compiler |
67
|
|
|
*/ |
68
|
|
|
public function setClassName(string $name): Compiler |
69
|
|
|
{ |
70
|
|
|
$this->class = $name; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $path |
77
|
|
|
*/ |
78
|
|
|
public function saveTo(string $path): void |
79
|
|
|
{ |
80
|
|
|
$pathName = $path . '/' . $this->class . '.php'; |
81
|
|
|
|
82
|
|
|
\file_put_contents($pathName, $this->build()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
* @throws \Throwable |
88
|
|
|
*/ |
89
|
|
|
public function build(): string |
90
|
|
|
{ |
91
|
|
|
\ob_start(); |
92
|
|
|
|
93
|
|
|
try { |
94
|
|
|
require __DIR__ . '/../resources/parser.tpl.php'; |
95
|
|
|
return \ob_get_contents(); |
96
|
|
|
} catch (\Throwable $e) { |
97
|
|
|
throw $e; |
98
|
|
|
} finally { |
99
|
|
|
\ob_end_clean(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param mixed $value |
105
|
|
|
* @return string |
106
|
|
|
* @throws \Zend\Code\Exception\InvalidArgumentException |
107
|
|
|
* @throws \Zend\Code\Generator\Exception\RuntimeException |
108
|
|
|
*/ |
109
|
|
|
protected function render($value): string |
110
|
|
|
{ |
111
|
|
|
$generator = new Value($value, Value::TYPE_AUTO, Value::OUTPUT_SINGLE_LINE); |
112
|
|
|
|
113
|
|
|
return $generator->generate(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|