| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
require __DIR__ . '/phpyLang.php';
|
|
|
4 |
|
|
|
5 |
$grammarFileToName = [
|
|
|
6 |
__DIR__ . '/php5.y' => 'Php5',
|
|
|
7 |
__DIR__ . '/php7.y' => 'Php7',
|
|
|
8 |
];
|
|
|
9 |
|
|
|
10 |
$tokensFile = __DIR__ . '/tokens.y';
|
|
|
11 |
$tokensTemplate = __DIR__ . '/tokens.template';
|
|
|
12 |
$skeletonFile = __DIR__ . '/parser.template';
|
|
|
13 |
$tmpGrammarFile = __DIR__ . '/tmp_parser.phpy';
|
|
|
14 |
$tmpResultFile = __DIR__ . '/tmp_parser.php';
|
|
|
15 |
$resultDir = __DIR__ . '/../lib/PhpParser/Parser';
|
|
|
16 |
$tokensResultsFile = $resultDir . '/Tokens.php';
|
|
|
17 |
|
|
|
18 |
$kmyacc = getenv('KMYACC');
|
|
|
19 |
if (!$kmyacc) {
|
|
|
20 |
// Use phpyacc from dev dependencies by default.
|
|
|
21 |
$kmyacc = __DIR__ . '/../vendor/bin/phpyacc';
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
$options = array_flip($argv);
|
|
|
25 |
$optionDebug = isset($options['--debug']);
|
|
|
26 |
$optionKeepTmpGrammar = isset($options['--keep-tmp-grammar']);
|
|
|
27 |
|
|
|
28 |
///////////////////
|
|
|
29 |
/// Main script ///
|
|
|
30 |
///////////////////
|
|
|
31 |
|
|
|
32 |
$tokens = file_get_contents($tokensFile);
|
|
|
33 |
|
|
|
34 |
foreach ($grammarFileToName as $grammarFile => $name) {
|
|
|
35 |
echo "Building temporary $name grammar file.\n";
|
|
|
36 |
|
|
|
37 |
$grammarCode = file_get_contents($grammarFile);
|
|
|
38 |
$grammarCode = str_replace('%tokens', $tokens, $grammarCode);
|
|
|
39 |
$grammarCode = preprocessGrammar($grammarCode);
|
|
|
40 |
|
|
|
41 |
file_put_contents($tmpGrammarFile, $grammarCode);
|
|
|
42 |
|
|
|
43 |
$additionalArgs = $optionDebug ? '-t -v' : '';
|
|
|
44 |
|
|
|
45 |
echo "Building $name parser.\n";
|
|
|
46 |
$output = execCmd("$kmyacc $additionalArgs -m $skeletonFile -p $name $tmpGrammarFile");
|
|
|
47 |
|
|
|
48 |
$resultCode = file_get_contents($tmpResultFile);
|
|
|
49 |
$resultCode = removeTrailingWhitespace($resultCode);
|
|
|
50 |
|
|
|
51 |
ensureDirExists($resultDir);
|
|
|
52 |
file_put_contents("$resultDir/$name.php", $resultCode);
|
|
|
53 |
unlink($tmpResultFile);
|
|
|
54 |
|
|
|
55 |
echo "Building token definition.\n";
|
|
|
56 |
$output = execCmd("$kmyacc -m $tokensTemplate $tmpGrammarFile");
|
|
|
57 |
rename($tmpResultFile, $tokensResultsFile);
|
|
|
58 |
|
|
|
59 |
if (!$optionKeepTmpGrammar) {
|
|
|
60 |
unlink($tmpGrammarFile);
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
////////////////////////////////
|
|
|
65 |
/// Utility helper functions ///
|
|
|
66 |
////////////////////////////////
|
|
|
67 |
|
|
|
68 |
function ensureDirExists($dir) {
|
|
|
69 |
if (!is_dir($dir)) {
|
|
|
70 |
mkdir($dir, 0777, true);
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
function execCmd($cmd) {
|
|
|
75 |
$output = trim(shell_exec("$cmd 2>&1"));
|
|
|
76 |
if ($output !== "") {
|
|
|
77 |
echo "> " . $cmd . "\n";
|
|
|
78 |
echo $output;
|
|
|
79 |
}
|
|
|
80 |
return $output;
|
|
|
81 |
}
|