-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathConverter.php
421 lines (355 loc) · 12.6 KB
/
Converter.php
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
/**
* Perl-to-PHP main converter class.
*
* @author Tim Behrendsen <[email protected]>
* @created 2016-10-14
*/
require_once('PpiElement.php');
require_once('PpiNode.php');
require_once('PpiDocument.php');
require_once('PpiStatement.php');
require_once('PpiStructure.php');
require_once('PpiToken.php');
class Converter
{
protected $root;
protected $flatList;
protected $quietOpt = false;
protected $verboseOpt = false;
public function setQuiet(
$opt)
{
$this->quietOpt = $opt;
}
public function setVerbose(
$opt)
{
$this->verboseOpt = $opt;
}
/**
* Read perl file and build lexical structure using external perl
* program, which uses PPI::Dumper.
*/
public function readFile(
$fn)
{
if (! file_exists($fn)) {
print "File not found: $fn\n";
exit(0);
}
$path = realpath(dirname(__FILE__));
$pipe = popen("$path/dumplex.pl $fn", 'r');
$n = 0;
$lineNum = 1;
$this->flatList = [];
$lastLevel = -1;
$lastObj = null;
$parentObj = null;
$parentStack = [];
while (($line = fgets($pipe)) !== false) {
++$n;
if (preg_match('/^(\s*)(\S+)\s*(.*)/', rtrim($line), $matches)) {
$spaces = $matches[1];
$type = $matches[2];
$content = $matches[3];
$level = strlen($spaces) / 2;
// Convert to PPI class
$className = str_replace('::', '', $type);
$className = str_replace('PPI', 'Ppi', $className);
$obj = new $className;
$this->flatList[] = $obj;
if ($this->root === null) {
$this->root = $obj;
}
$obj->root = $this->root;
$obj->id = $n;
$obj->level = $level;
$obj->lineNum = $lineNum;
if (strpos($content, '\n') !== false) {
++$lineNum;
}
if ($obj instanceof PpiStructure) {
// Special content is start/end characters. Format is:
// "( ... )", etc.
if (strlen($content) != 7) {
print "Invalid structure content: $content\n";
exit(0);
}
$obj->startContent = substr($content, 0, 1);
$obj->endContent = substr($content, -1, 1);
} elseif ($obj instanceof PpiTokenWhitespace) {
// Whitespace token, translate escaped characters and
// remove outer single quotes
$obj->content = str_replace('\t', "\t",
str_replace('\n', "\n",
substr($content, 1, -1)));
} elseif ($obj instanceof PpiTokenComment) {
// Comment token, translate escaped characters and
// remove outer single quotes. Might have \\ escaped.
$content = str_replace('\\\\', "\001", $content);
$content = str_replace('\t', "\t",
str_replace('\n', "\n",
substr($content, 1, -1)));
$obj->content = str_replace("\001", '\\', $content);
} elseif ($obj instanceof PpiTokenQuoteLikeWords) {
// qw() can have embedded newlines.
// Note that PPI has a bug with things like:
// @a = qw( abc def \n
// ghi jkl );
// It gives us: "qw( abc def \n\n ghi jlk )".
// Might have \\ escaped, which we keep escaped.
$content = str_replace('\\\\', "\001", $content);
$content = str_replace('\t', "\t",
str_replace('\n', "\n",
substr($content, 1, -1)));
$obj->content = str_replace("\001", '\\\\', $content);
} else {
// Regular content, just remove outer single quotes
$obj->content = substr($content, 1, -1);
}
if ($level > $lastLevel) {
// Children of prior object
if ($parentObj !== null) {
$parentStack[] = $parentObj;
}
$parentObj = $lastObj;
$lastLevel = $level;
} elseif ($level < $lastLevel) {
// Going back up a level
while ($level < $lastLevel) {
$parentObj = array_pop($parentStack);
--$lastLevel;
}
}
$obj->parent = $parentObj;
if ($parentObj === null) {
$parentObj = $obj;
} else {
$prior = end($parentObj->children);
$parentObj->children[] = $obj;
if (! empty($prior)) {
$obj->prevSibling = $prior;
$prior->nextSibling = $obj;
}
}
if ($lastObj !== null) {
$lastObj->next = $obj;
}
$obj->prev = $lastObj;
// Set expression precedence in object
$obj->setPrecedence();
$lastObj = $obj;
} else {
print "BAD: $line\n";
}
}
pclose($pipe);
if (! $this->quietOpt) {
error_log("$n lines read");
}
}
/**
* Do whitespace consolidation to simplify conversion. It's a lot
* easier if we don't have to deal with whether we have whitespace
* tokens in our tree. Each token has a field with its own leading
* spaces. This also makes it easier to change indent.
*
* Tasks:
* 1) Eliminate whitespace tokens and put in the next token object.
* 2) Convert newlines into special newline object
*/
private function consolidateWhitespace()
{
// Recursively scan the tree, doing the processing. We'll
// maintain a parent stack so we can scan the parents if we need to.
$parentStack = [];
$process = null;
$process = function ($obj) use (&$process, &$parentStack) {
if ($obj instanceof PpiTokenComment) {
$this->chgComment($obj);
} elseif ($obj instanceof PpiTokenWhitespace) {
$this->chgWhitespace($obj);
}
if (count($obj->children)) {
array_push($parentStack, $obj);
foreach ($obj->children as $sub) {
$process($sub);
}
array_pop($parentStack);
}
};
$process($this->root);
}
private function chgComment(
$obj)
{
if (! preg_match('/^([ \t]*)(.*)(\n*)(.*)/', $obj->content, $matches)) {
error_log("WARNING: chgComment BAD CONTENT: {$obj->content}");
return;
}
$spaces = $matches[1];
$content = $matches[2];
$nl = $matches[3];
$rest = $matches[4];
if ($rest !== '') {
error_log("WARNING: comment had spaces after newline: '$obj->content'");
return;
}
// Note might have spaces from prior WS token
$obj->preWs .= $spaces;
$obj->content = $content;
if ($nl !== '') {
// Add a whitespace token after this one.
$newObj = self::copyNewClass($obj, 'PpiTokenNewline');
$newObj->preWs = '';
$newObj->content = "\n";
$obj->insertRightSibling($newObj);
}
return;
}
private function chgWhitespace(
$obj)
{
if (! preg_match('/^([ \t]*)(.*)(\n*)(.*)/', $obj->content, $matches)) {
error_log("WARNING: chgWhitespace BAD CONTENT: {$obj->content}");
return;
}
$spaces = $matches[1];
$content = $matches[2];
$nl = $matches[3];
$rest = $matches[4];
$isNew = false;
if ($nl !== '') {
// Translate object to a PpiTokenNewline
$newObj = self::copyNewClass($obj, 'PpiTokenNewline');
$obj->insertRightSibling($newObj);
$obj->delete();
$newObj->preWs = $spaces;
$newObj->content = "\n";
// If no spaces after newline, we're done
if ($rest === '') {
return;
}
// Add the spaces to following token
$obj = $newObj;
$spaces = $rest;
$isNew = true;
}
// Move whitespace to following PpiToken or PpiStructure. We'll
// stop at the end of our current list of children. If we hit the
// end, then the spaces belong to the end of a PpiStructure.
$last = $obj->parent;
while (count($last->children)) {
$last = end($last->children);
}
// Determine:
// 1) Are we already on the last node
// 2) Is there a PpiToken somewhere through the last node.
$node = $obj;
$foundToken = false;
if ($node !== $last) {
do {
$node = $node->next;
if ($node instanceof PpiToken
|| $node instanceof PpiStructure) {
$node->preWs .= $spaces;
if (! $isNew) {
$obj->delete();
}
$foundToken = true;
break;
}
} while ($node !== $last);
}
if (! $foundToken) {
// If we ran out of tokens, then spaces belong at end of
// parent PpiStructure, just before endContent
for ($node = $obj->parent; $node !== null; $node = $node->parent) {
if ($node instanceof PpiStructure) {
$node->endPreWs .= $spaces;
$obj->delete();
break;
}
}
}
}
/**
* Create new node with different class and duplicate content.
*/
static function copyNewClass(
$obj,
$newClass,
$noContent = false)
{
$newObj = new $newClass;
$newObj->id = $obj->id;
$newObj->lineNum = $obj->lineNum;
$newObj->level = $obj->level;
$newObj->root = $obj->root;
if (! $noContent) {
$newObj->children = $obj->children;
$newObj->preWs = $obj->preWs;
$newObj->endPreWs = $obj->endPreWs;
$newObj->content = $obj->content;
$newObj->startContent = $obj->startContent;
$newObj->endContent = $obj->endContent;
}
return $newObj;
}
/**
* Dump out lexical structure for debug purposes.
*/
static function dumpStruct(
$obj,
$level = 0)
{
$s = $obj->fmtObj($level) . "\n";
foreach ($obj->children as $sub) {
if ($sub->parent !== $obj) {
print "bad parent!\n";
exit(0);
}
$s .= self::dumpStruct($sub, $level+1);
}
return $s;
}
/**
* Start the conversion process
* @return string Converted code.
*/
public function convert(
$ppiFn = null)
{
if (! $this->quietOpt) {
error_log("Phase 1: Do whitespace consolidation");
}
$this->consolidateWhitespace();
if (! $this->quietOpt) {
error_log("Phase 2: Analyze lexical structure");
}
$this->root->analyzeTreeContext();
if (! empty($ppiFn)) {
file_put_contents($ppiFn, self::dumpStruct($this->root));
}
if (! $this->quietOpt) {
error_log("Phase 3: Calling all converters");
}
// Step 1: Call all converters
foreach ($this->flatList as $obj) {
if ($this->verboseOpt) {
print "Convert: {$obj->id}\n";
}
$obj->genCode();
}
if (! empty($ppiFn) && strpos($ppiFn, 'ppi') !== false) {
$newFn = str_replace('ppi', 'pp2', $ppiFn);
file_put_contents($newFn, self::dumpStruct($this->root));
}
// Recursively generate code
if (! $this->quietOpt) {
error_log("Phase 4: Writing source");
}
return "<?php\n" . $this->root->getRecursiveContent();
}
}