|
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\CodeGen\Language; |
|
11
|
|
|
|
|
12
|
|
|
use PhpYacc\CodeGen\Language; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class PHP. |
|
16
|
|
|
*/ |
|
17
|
|
|
class PHP implements Language |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var resource |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $fp; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var resource |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $hp; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $fileBuffer = ''; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $headerBuffer = ''; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param $file |
|
41
|
|
|
* @param $headerFile |
|
42
|
|
|
*/ |
|
43
|
|
|
public function begin($file, $headerFile) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->fp = $file; |
|
46
|
|
|
$this->hp = $headerFile; |
|
47
|
|
|
$this->fileBuffer = ''; |
|
48
|
|
|
$this->headerBuffer = ''; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
public function commit() |
|
55
|
|
|
{ |
|
56
|
|
|
\fwrite($this->fp, $this->fileBuffer); |
|
57
|
|
|
\fwrite($this->hp, $this->headerBuffer); |
|
58
|
|
|
$this->fp = $this->hp = null; |
|
59
|
|
|
$this->fileBuffer = ''; |
|
60
|
|
|
$this->headerBuffer = ''; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $text |
|
65
|
|
|
*/ |
|
66
|
|
|
public function inline_comment(string $text) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->fileBuffer .= '/* '.$text.' */'; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $text |
|
73
|
|
|
*/ |
|
74
|
|
|
public function comment(string $text) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->fileBuffer .= '//'.$text."\n"; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $indent |
|
81
|
|
|
* @param int $num |
|
82
|
|
|
* @param string $value |
|
83
|
|
|
*/ |
|
84
|
|
|
public function case_block(string $indent, int $num, string $value) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->fileBuffer .= \sprintf("%scase %d: return %s;\n", $indent, $num, \var_export($value, true)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string $text |
|
91
|
|
|
* @param bool $includeHeader |
|
92
|
|
|
*/ |
|
93
|
|
|
public function write(string $text, bool $includeHeader = false) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->fileBuffer .= $text; |
|
96
|
|
|
if ($includeHeader) { |
|
97
|
|
|
$this->headerBuffer .= $text; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $text |
|
103
|
|
|
*/ |
|
104
|
|
|
public function writeQuoted(string $text) |
|
105
|
|
|
{ |
|
106
|
|
|
$buf = []; |
|
107
|
|
|
for($i = 0; $i < \mb_strlen($text); $i++) { |
|
108
|
|
|
$char = $text[$i]; |
|
109
|
|
|
|
|
110
|
|
|
if ($char == '\\' || $char == '"' || $char == '$') { |
|
111
|
|
|
$buf[] = '\\'; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$buf[] = $char; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$this->fileBuffer .= implode('', $buf); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|