|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of PHP-Yacc 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 PhpYacc; |
|
11
|
|
|
|
|
12
|
|
|
use PhpYacc\CodeGen\Language\PHP; |
|
13
|
|
|
use PhpYacc\CodeGen\Template; |
|
14
|
|
|
use PhpYacc\Compress\Compress; |
|
15
|
|
|
use PhpYacc\Grammar\Context; |
|
16
|
|
|
use PhpYacc\Lalr\Generator as Lalr; |
|
17
|
|
|
use PhpYacc\Yacc\Lexer; |
|
18
|
|
|
use PhpYacc\Yacc\MacroSet; |
|
19
|
|
|
use PhpYacc\Yacc\Parser; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class Generator. |
|
23
|
|
|
*/ |
|
24
|
|
|
class Generator |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Parser |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $parser; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Lalr |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $lalr; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var Compress |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $compressor; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Generator constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param Parser|null $parser |
|
45
|
|
|
* @param Lalr|null $lalr |
|
46
|
|
|
* @param Compress|null $compressor |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(Parser $parser = null, Lalr $lalr = null, Compress $compressor = null) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->parser = $parser ?: new Parser(new Lexer(), new MacroSet()); |
|
51
|
|
|
$this->lalr = $lalr ?: new Lalr(); |
|
52
|
|
|
$this->compressor = $compressor ?: new Compress(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param Context $context |
|
57
|
|
|
* @param string $grammar |
|
58
|
|
|
* @param string $template |
|
59
|
|
|
* @param string $resultFile |
|
60
|
|
|
* |
|
61
|
|
|
* @throws Exception\LexingException |
|
62
|
|
|
* @throws Exception\LogicException |
|
63
|
|
|
* @throws Exception\ParseException |
|
64
|
|
|
* @throws Exception\TemplateException |
|
65
|
|
|
* |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
public function generate(Context $context, string $grammar, string $template, string $resultFile) |
|
69
|
|
|
{ |
|
70
|
|
|
$template = new Template(new PHP(), $template, $context); |
|
71
|
|
|
|
|
72
|
|
|
$this->parser->parse($grammar, $context); |
|
73
|
|
|
|
|
74
|
|
|
$this->lalr->compute($context); |
|
75
|
|
|
|
|
76
|
|
|
$template->render($this->compressor->compress($context), \fopen($resultFile, 'w')); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|