-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathphpickle_ops_generator.php
More file actions
186 lines (163 loc) · 3.19 KB
/
Copy pathphpickle_ops_generator.php
File metadata and controls
186 lines (163 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
require_once "phpickle_stream.php";
class phpickle_ops_generator
{
public function gen_op_handlers(phpickle_stream $stream)
{
// go over file lines,
// skip starting with //
// skip empty
// handle starting with OP and -- read: and --write:
$read_ct = "";
$write_ct = "";
while (!$stream->eof())
{
$line = trim($stream->get_line());
if ($line == "")
{
continue;
}
else
if (substr($line, 0, 2) == "//")
{
continue;
}
else
if (substr($line, 0, 3) == "OP:")
{
list($read, $write) = $this->_process_op($line, $stream);
$read_ct .= $read;
$write_ct .= $write;
}
else
{
throw "Unexpected line in op handlers! $line";
}
}
$read_ct = <<<EOD
class phpickle_read_ops
{
$read_ct
}
EOD;
$write_ct = <<<EOD
class phpickle_write_ops
{
$write_ct
}
EOD;
return array($read_ct, $write_ct);
}
private function _process_read_op_content($op, $op_line, $stream)
{
$ct = $this->_read_op_content($stream);
$tmpl = <<<EOD
// generated for $op_line
function op_$op(\$stream, \$stack, \$memo, \$debug)
{
$ct
}
EOD;
return $tmpl;
}
private function _read_op_content($stream)
{
$ct = "";
while (!$stream->eof())
{
$line = $stream->get_line();
if (substr($line, 0, 3) == "OP:")
{
// end of op
$stream->unget_line();
break;
}
else
if (substr(trim($line), 0, strlen("--write:")) == "--write:")
{
// end of op
$stream->unget_line();
break;
}
else
if (substr(trim($line), 0, strlen("-- read:")) == "-- read:")
{
// end of op
$stream->unget_line();
break;
}
$ct .= "\t\t\t".$line."\r\n";
}
return $ct;
}
private function _process_write_op_content($op, $op_line, $stream)
{
$ct = $this->_read_op_content($stream);
$tmpl = <<<EOD
// generated for $op_line
function op_$op(\$value, &\$stream, \$stack, \$memo, \$debug, \$mapper)
{
\$stream->write(\$mapper->str2bin("$op"));
$ct
}
EOD;
return $tmpl;
}
private function _process_op($op_line, $stream)
{
// parse op
if (preg_match("/OP\: ([A-Z0-9_]*)/", $op_line, $mt))
{
$op = $mt[1];
}
else
{
throw "OP: line syntax error: $op_line";
}
$read = "";
$write = "";
while (!$stream->eof())
{
$line = trim($stream->get_line());
if ($line == "")
{
continue;
}
else
if (substr($line, 0, 2) == "//")
{
continue;
}
else
if (substr($line, 0, 3) == "OP:")
{
// end of op
$stream->unget_line();
break;
}
else
if (substr($line, 0, strlen("-- read:")) == "-- read:")
{
$read = $this->_process_read_op_content($op, $op_line, $stream);
}
else
if (substr($line, 0, strlen("--write:")) == "--write:")
{
$write = $this->_process_write_op_content($op, $op_line, $stream);
}
else
{
throw "Unexpected line in op handler! $line";
}
}
return array($read, $write);
}
}
function make_defaults()
{
$gen = new phpickle_ops_generator();
$stream = new phpickle_stream("phpickle_op_handlers.pck");
list($read, $write) = $gen->gen_op_handlers($stream);
file_put_contents("phpickle_gen_read_ops.php", "<?"."php \r\n".$read);
file_put_contents("phpickle_gen_write_ops.php", "<?"."php \r\n".$write);
}