2011-05-27 18:20:44 +02:00
|
|
|
<?php
|
|
|
|
|
2015-06-13 13:50:16 +02:00
|
|
|
$grammarFileToName = [
|
|
|
|
__DIR__ . '/php5.y' => 'Php5',
|
|
|
|
__DIR__ . '/php7.y' => 'Php7',
|
2015-06-13 13:09:34 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$skeletonFile = __DIR__ . '/kmyacc.php.parser';
|
|
|
|
$tmpGrammarFile = __DIR__ . '/tmp_parser.phpy';
|
|
|
|
$tmpResultFile = __DIR__ . '/tmp_parser.php';
|
2015-06-13 13:50:16 +02:00
|
|
|
$resultDir = __DIR__ . '/../lib/PhpParser/Parser';
|
2012-10-19 19:11:47 +02:00
|
|
|
|
|
|
|
// 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']);
|
2011-08-04 12:46:48 +02:00
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
/// Utility regex constants ///
|
|
|
|
///////////////////////////////
|
|
|
|
|
|
|
|
const LIB = '(?(DEFINE)
|
|
|
|
(?<singleQuotedString>\'[^\\\\\']*+(?:\\\\.[^\\\\\']*+)*+\')
|
|
|
|
(?<doubleQuotedString>"[^\\\\"]*+(?:\\\\.[^\\\\"]*+)*+")
|
|
|
|
(?<string>(?&singleQuotedString)|(?&doubleQuotedString))
|
|
|
|
(?<comment>/\*[^*]*+(?:\*(?!/)[^*]*+)*+\*/)
|
|
|
|
(?<code>\{[^\'"/{}]*+(?:(?:(?&string)|(?&comment)|(?&code)|/)[^\'"/{}]*+)*+})
|
|
|
|
)';
|
|
|
|
|
|
|
|
const PARAMS = '\[(?<params>[^[\]]*+(?:\[(?¶ms)\][^[\]]*+)*+)\]';
|
|
|
|
const ARGS = '\((?<args>[^()]*+(?:\((?&args)\)[^()]*+)*+)\)';
|
|
|
|
|
|
|
|
///////////////////
|
|
|
|
/// Main script ///
|
|
|
|
///////////////////
|
|
|
|
|
2015-06-13 13:50:16 +02:00
|
|
|
foreach ($grammarFileToName as $grammarFile => $name) {
|
|
|
|
echo "Building temporary $name grammar file.\n";
|
2011-08-04 12:46:48 +02:00
|
|
|
|
2015-06-13 13:09:34 +02:00
|
|
|
$grammarCode = file_get_contents($grammarFile);
|
2011-08-04 12:46:48 +02:00
|
|
|
|
2015-06-13 13:09:34 +02:00
|
|
|
$grammarCode = resolveNodes($grammarCode);
|
|
|
|
$grammarCode = resolveMacros($grammarCode);
|
|
|
|
$grammarCode = resolveStackAccess($grammarCode);
|
2011-08-04 12:46:48 +02:00
|
|
|
|
2015-06-13 13:09:34 +02:00
|
|
|
file_put_contents($tmpGrammarFile, $grammarCode);
|
2011-08-04 12:46:48 +02:00
|
|
|
|
2015-06-13 13:09:34 +02:00
|
|
|
$additionalArgs = $optionDebug ? '-t -v' : '';
|
2014-04-20 00:08:59 +02:00
|
|
|
|
2015-06-13 13:50:16 +02:00
|
|
|
echo "Building $name parser.\n";
|
|
|
|
$output = trim(shell_exec("$kmyacc $additionalArgs -l -m $skeletonFile -p $name $tmpGrammarFile 2>&1"));
|
2015-06-13 13:09:34 +02:00
|
|
|
echo "Output: \"$output\"\n";
|
2011-05-27 18:20:44 +02:00
|
|
|
|
2015-06-13 13:09:34 +02:00
|
|
|
$resultCode = file_get_contents($tmpResultFile);
|
|
|
|
$resultCode = removeTrailingWhitespace($resultCode);
|
2015-04-30 19:18:42 +02:00
|
|
|
|
2015-06-13 13:50:16 +02:00
|
|
|
ensureDirExists($resultDir);
|
|
|
|
file_put_contents("$resultDir/$name.php", $resultCode);
|
2015-06-13 13:09:34 +02:00
|
|
|
unlink($tmpResultFile);
|
2011-05-29 20:51:15 +02:00
|
|
|
|
2015-06-13 13:09:34 +02:00
|
|
|
if (!$optionKeepTmpGrammar) {
|
|
|
|
unlink($tmpGrammarFile);
|
|
|
|
}
|
2012-10-19 19:11:47 +02:00
|
|
|
}
|
2011-08-04 12:46:48 +02:00
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
/// Preprocessing functions ///
|
|
|
|
///////////////////////////////
|
|
|
|
|
|
|
|
function resolveNodes($code) {
|
|
|
|
return preg_replace_callback(
|
2014-02-06 14:44:16 +01:00
|
|
|
'~(?<name>[A-Z][a-zA-Z_\\\\]++)\s*' . PARAMS . '~',
|
2011-08-04 12:46:48 +02:00
|
|
|
function($matches) {
|
|
|
|
// recurse
|
|
|
|
$matches['params'] = resolveNodes($matches['params']);
|
|
|
|
|
|
|
|
$params = magicSplit(
|
|
|
|
'(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
|
|
|
|
$matches['params']
|
|
|
|
);
|
|
|
|
|
2011-10-28 19:06:24 +02:00
|
|
|
$paramCode = '';
|
2011-08-04 12:46:48 +02:00
|
|
|
foreach ($params as $param) {
|
2011-10-28 19:06:24 +02:00
|
|
|
$paramCode .= $param . ', ';
|
2011-08-04 12:46:48 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 19:18:42 +02:00
|
|
|
return 'new ' . $matches['name'] . '(' . $paramCode . 'attributes())';
|
2011-08-04 12:46:48 +02:00
|
|
|
},
|
|
|
|
$code
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolveMacros($code) {
|
|
|
|
return preg_replace_callback(
|
2011-10-28 19:06:24 +02:00
|
|
|
'~\b(?<!::|->)(?!array\()(?<name>[a-z][A-Za-z]++)' . ARGS . '~',
|
2011-08-04 12:46:48 +02:00
|
|
|
function($matches) {
|
|
|
|
// recurse
|
|
|
|
$matches['args'] = resolveMacros($matches['args']);
|
|
|
|
|
|
|
|
$name = $matches['name'];
|
|
|
|
$args = magicSplit(
|
|
|
|
'(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
|
|
|
|
$matches['args']
|
|
|
|
);
|
|
|
|
|
2015-04-22 15:57:29 +02:00
|
|
|
if ('attributes' == $name) {
|
|
|
|
assertArgs(0, $args, $name);
|
2015-04-23 13:13:45 +02:00
|
|
|
return '$this->startAttributeStack[#1] + $this->endAttributes';
|
2015-04-22 15:57:29 +02:00
|
|
|
}
|
|
|
|
|
2011-08-04 12:46:48 +02:00
|
|
|
if ('init' == $name) {
|
|
|
|
return '$$ = array(' . implode(', ', $args) . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('push' == $name) {
|
|
|
|
assertArgs(2, $args, $name);
|
|
|
|
|
|
|
|
return $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('pushNormalizing' == $name) {
|
|
|
|
assertArgs(2, $args, $name);
|
|
|
|
|
|
|
|
return 'if (is_array(' . $args[1] . ')) { $$ = array_merge(' . $args[0] . ', ' . $args[1] . '); } else { ' . $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0] . '; }';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('toArray' == $name) {
|
|
|
|
assertArgs(1, $args, $name);
|
|
|
|
|
|
|
|
return 'is_array(' . $args[0] . ') ? ' . $args[0] . ' : array(' . $args[0] . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('parseVar' == $name) {
|
|
|
|
assertArgs(1, $args, $name);
|
|
|
|
|
|
|
|
return 'substr(' . $args[0] . ', 1)';
|
|
|
|
}
|
|
|
|
|
2011-08-20 10:40:27 +02:00
|
|
|
if ('parseEncapsed' == $name) {
|
|
|
|
assertArgs(2, $args, $name);
|
|
|
|
|
2015-03-21 18:54:52 +01:00
|
|
|
return 'foreach (' . $args[0] . ' as &$s) { if (is_string($s)) { $s = Node\Scalar\String_::parseEscapeSequences($s, ' . $args[1] . '); } }';
|
2011-08-20 10:40:27 +02:00
|
|
|
}
|
2011-10-28 19:06:24 +02:00
|
|
|
|
2011-12-04 16:52:43 +01:00
|
|
|
if ('parseEncapsedDoc' == $name) {
|
|
|
|
assertArgs(1, $args, $name);
|
|
|
|
|
2015-03-21 18:54:52 +01:00
|
|
|
return 'foreach (' . $args[0] . ' as &$s) { if (is_string($s)) { $s = Node\Scalar\String_::parseEscapeSequences($s, null); } } $s = preg_replace(\'~(\r\n|\n|\r)$~\', \'\', $s); if (\'\' === $s) array_pop(' . $args[0] . ');';
|
2011-12-04 16:52:43 +01:00
|
|
|
}
|
|
|
|
|
2015-04-26 23:04:31 +02:00
|
|
|
return $matches[0];
|
2011-08-04 12:46:48 +02:00
|
|
|
},
|
|
|
|
$code
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function assertArgs($num, $args, $name) {
|
|
|
|
if ($num != count($args)) {
|
|
|
|
die('Wrong argument count for ' . $name . '().');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 15:57:29 +02:00
|
|
|
function resolveStackAccess($code) {
|
|
|
|
$code = preg_replace('/\$\d+/', '$this->semStack[$0]', $code);
|
|
|
|
$code = preg_replace('/#(\d+)/', '$$1', $code);
|
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
|
2015-04-30 19:18:42 +02:00
|
|
|
function removeTrailingWhitespace($code) {
|
|
|
|
$lines = explode("\n", $code);
|
|
|
|
$lines = array_map('rtrim', $lines);
|
|
|
|
return implode("\n", $lines);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensureDirExists($dir) {
|
2012-10-19 19:11:47 +02:00
|
|
|
if (!is_dir($dir)) {
|
|
|
|
mkdir($dir, 0777, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-04 12:46:48 +02:00
|
|
|
//////////////////////////////
|
|
|
|
/// Regex helper functions ///
|
|
|
|
//////////////////////////////
|
|
|
|
|
|
|
|
function regex($regex) {
|
|
|
|
return '~' . LIB . '(?:' . str_replace('~', '\~', $regex) . ')~';
|
|
|
|
}
|
|
|
|
|
|
|
|
function magicSplit($regex, $string) {
|
|
|
|
$pieces = preg_split(regex('(?:(?&string)|(?&comment)|(?&code))(*SKIP)(*FAIL)|' . $regex), $string);
|
|
|
|
|
|
|
|
foreach ($pieces as &$piece) {
|
|
|
|
$piece = trim($piece);
|
|
|
|
}
|
2011-05-27 18:20:44 +02:00
|
|
|
|
2015-06-13 11:27:38 +02:00
|
|
|
if ($pieces === ['']) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pieces;
|
2014-02-12 14:12:55 +01:00
|
|
|
}
|