diff --git a/grammar/README.md b/grammar/README.md index 99a7a75c..ee664dc0 100644 --- a/grammar/README.md +++ b/grammar/README.md @@ -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`. \ No newline at end of file +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`. \ No newline at end of file diff --git a/grammar/rebuildParser.php b/grammar/rebuildParser.php index a7d9e939..0aa1f8b5 100644 --- a/grammar/rebuildParser.php +++ b/grammar/rebuildParser.php @@ -1,8 +1,21 @@ [^()]*+(?:\((?&args)\)[^()]*+)*+)\)'; /// Main script /// /////////////////// -echo '
';
-
 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 '
'; +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 /// //////////////////////////////