Turn rebuildParser.php into a CLI script

The paths are now more generic so it can be run from any directory, not
just grammar/.
This commit is contained in:
nikic 2012-10-19 19:11:47 +02:00
parent 9e43acee2c
commit 759c04db9b
2 changed files with 41 additions and 28 deletions

View File

@ -24,7 +24,7 @@ Building the parser
===================
In order to rebuild the parser, you need [moriyoshi's fork of kmyacc](https://github.com/moriyoshi/kmyacc-forked).
After you compiled/installed it, run the `rebuildParser.php` file.
After you compiled/installed it, run the `rebuildParser.php` script.
By default only the Parser.php is built. If you want to build the Parser/Debug.php and the y.output
file you need to call the file with the debug option: `rebuildParser.php?debug`.
By default only the `Parser.php` is built. If you want to build the `Parser/Debug.php` and the `y.output` run the
script with `--debug`. If you want to retain the preprocessed grammar pass `--keep-tmp-grammar`.

View File

@ -1,8 +1,21 @@
<?php
const GRAMMAR_FILE = './zend_language_parser.phpy';
const TMP_FILE = './tmp_parser.phpy';
const RESULT_FILE = './tmp_parser.php';
$grammarFile = __DIR__ . '/zend_language_parser.phpy';
$skeletonFile = __DIR__ . '/kmyacc.php.parser';
$tmpGrammarFile = __DIR__ . '/tmp_parser.phpy';
$tmpResultFile = __DIR__ . '/tmp_parser.php';
$parserResultFile = __DIR__ . '/../lib/PHPParser/Parser.php';
$debugParserResultFile = __DIR__ . '/../lib/PHPParser/Parser/Debug.php';
// check for kmyacc.exe binary in this directory, otherwise fall back to global name
$kmyacc = __DIR__ . '/kmyacc.exe';
if (!file_exists($kmyacc)) {
$kmyacc = 'kmyacc';
}
$options = array_flip($argv);
$optionDebug = isset($options['--debug']);
$optionKeepTmpGrammar = isset($options['--keep-tmp-grammar']);
///////////////////////////////
/// Utility regex constants ///
@ -23,42 +36,34 @@ const ARGS = '\((?<args>[^()]*+(?:\((?&args)\)[^()]*+)*+)\)';
/// Main script ///
///////////////////
echo '<pre>';
echo 'Building temporary preproprocessed grammar file.', "\n";
$grammarCode = file_get_contents(GRAMMAR_FILE);
$grammarCode = file_get_contents($grammarFile);
$grammarCode = resolveConstants($grammarCode);
$grammarCode = resolveNodes($grammarCode);
$grammarCode = resolveMacros($grammarCode);
$grammarCode = resolveArrays($grammarCode);
file_put_contents(TMP_FILE, $grammarCode);
file_put_contents($tmpGrammarFile, $grammarCode);
echo 'Building parser. Output: "',
trim(shell_exec('kmyacc -l -m kmyacc.php.parser -p PHPParser_Parser ' . TMP_FILE . ' 2>&1')),
'"', "\n";
echo "Building parser.\n";
$output = trim(shell_exec("$kmyacc -l -m $skeletonFile -p PHPParser_Parser $tmpGrammarFile 2>&1"));
echo "Output: \"$output\"\n";
rename(RESULT_FILE, '../lib/PHPParser/Parser.php');
moveFileWithDirCheck($tmpResultFile, $parserResultFile);
if (isset($_GET['debug'])) {
echo 'Building debug parser. Output: "',
trim(shell_exec('kmyacc -t -v -l -m kmyacc.php.parser -p PHPParser_Parser ' . TMP_FILE . ' 2>&1')),
'"', "\n";
if ($optionDebug) {
echo "Building debug parser.\n";
$output = trim(shell_exec("$kmyacc -t -v -l -m $skeletonFile -p PHPParser_Parser $tmpGrammarFile 2>&1"));
echo "Output: \"$output\"\n";
if (!is_dir('../lib/PHPParser/Parser')) {
mkdir('../lib/PHPParser/Parser');
}
rename(RESULT_FILE, '../lib/PHPParser/Parser/Debug.php');
moveFileWithDirCheck($tmpResultFile, $debugParserResultFile);
}
unlink(TMP_FILE);
echo 'The following temporary preproprocessed grammar file was used:', "\n", $grammarCode;
echo '</pre>';
if (!$optionKeepTmpGrammar) {
unlink($tmpGrammarFile);
}
///////////////////////////////
/// Preprocessing functions ///
@ -193,6 +198,14 @@ function resolveArrays($code) {
);
}
function moveFileWithDirCheck($fromPath, $toPath) {
$dir = dirname($toPath);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
rename($fromPath, $toPath);
}
//////////////////////////////
/// Regex helper functions ///
//////////////////////////////