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
|
|
|
];
|
|
|
|
|
2015-06-13 16:24:52 +02:00
|
|
|
$tokensFile = __DIR__ . '/tokens.y';
|
2015-06-13 18:39:55 +02:00
|
|
|
$tokensTemplate = __DIR__ . '/tokens.template';
|
|
|
|
$skeletonFile = __DIR__ . '/parser.template';
|
2015-06-13 13:09:34 +02:00
|
|
|
$tmpGrammarFile = __DIR__ . '/tmp_parser.phpy';
|
|
|
|
$tmpResultFile = __DIR__ . '/tmp_parser.php';
|
2015-06-13 13:50:16 +02:00
|
|
|
$resultDir = __DIR__ . '/../lib/PhpParser/Parser';
|
2015-06-13 18:39:55 +02:00
|
|
|
$tokensResultsFile = $resultDir . '/Tokens.php';
|
2012-10-19 19:11:47 +02:00
|
|
|
|
2019-10-19 12:28:45 +02:00
|
|
|
$kmyacc = getenv('KMYACC');
|
|
|
|
if (!$kmyacc) {
|
2019-10-19 15:24:38 +02:00
|
|
|
// Use phpyacc from dev dependencies by default.
|
|
|
|
$kmyacc = PHP_BINARY . ' ' . __DIR__ . '/../vendor/bin/phpyacc';
|
2012-10-19 19:11:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$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 16:24:52 +02:00
|
|
|
$tokens = file_get_contents($tokensFile);
|
|
|
|
|
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);
|
2015-06-13 16:24:52 +02:00
|
|
|
$grammarCode = str_replace('%tokens', $tokens, $grammarCode);
|
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";
|
2019-10-19 12:28:45 +02:00
|
|
|
$output = execCmd("$kmyacc $additionalArgs -m $skeletonFile -p $name $tmpGrammarFile");
|
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 18:39:55 +02:00
|
|
|
echo "Building token definition.\n";
|
2019-10-19 12:28:45 +02:00
|
|
|
$output = execCmd("$kmyacc -m $tokensTemplate $tmpGrammarFile");
|
2015-06-13 18:39:55 +02:00
|
|
|
rename($tmpResultFile, $tokensResultsFile);
|
|
|
|
|
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(
|
2016-04-05 11:53:50 +02:00
|
|
|
'~\b(?<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
|
|
|
}
|
|
|
|
|
2016-10-08 23:54:14 +02:00
|
|
|
if ('stackAttributes' == $name) {
|
|
|
|
assertArgs(1, $args, $name);
|
|
|
|
return '$this->startAttributeStack[' . $args[0] . ']'
|
|
|
|
. ' + $this->endAttributeStack[' . $args[0] . ']';
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2015-06-13 20:51:02 +02:00
|
|
|
return 'if (is_array(' . $args[1] . ')) { $$ = array_merge(' . $args[0] . ', ' . $args[1] . '); }'
|
|
|
|
. ' else { ' . $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0] . '; }';
|
2011-08-04 12:46:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2015-06-13 20:51:02 +02:00
|
|
|
assertArgs(3, $args, $name);
|
2011-08-20 10:40:27 +02:00
|
|
|
|
2015-12-03 22:55:07 +01:00
|
|
|
return 'foreach (' . $args[0] . ' as $s) { if ($s instanceof Node\Scalar\EncapsedStringPart) {'
|
|
|
|
. ' $s->value = Node\Scalar\String_::parseEscapeSequences($s->value, ' . $args[1] . ', ' . $args[2] . '); } }';
|
2011-08-20 10:40:27 +02:00
|
|
|
}
|
2011-10-28 19:06:24 +02:00
|
|
|
|
2015-10-02 11:03:12 +02:00
|
|
|
if ('makeNop' == $name) {
|
2017-01-20 22:27:51 +01:00
|
|
|
assertArgs(3, $args, $name);
|
2015-10-02 11:03:12 +02:00
|
|
|
|
2016-04-05 11:53:50 +02:00
|
|
|
return '$startAttributes = ' . $args[1] . ';'
|
2017-01-20 22:29:41 +01:00
|
|
|
. ' if (isset($startAttributes[\'comments\']))'
|
2017-01-20 22:27:51 +01:00
|
|
|
. ' { ' . $args[0] . ' = new Stmt\Nop($startAttributes + ' . $args[2] . '); }'
|
2015-10-02 11:03:12 +02:00
|
|
|
. ' else { ' . $args[0] . ' = null; }';
|
|
|
|
}
|
|
|
|
|
2019-05-11 20:01:25 +02:00
|
|
|
if ('makeZeroLengthNop' == $name) {
|
|
|
|
assertArgs(2, $args, $name);
|
|
|
|
|
|
|
|
return '$startAttributes = ' . $args[1] . ';'
|
|
|
|
. ' if (isset($startAttributes[\'comments\']))'
|
|
|
|
. ' { ' . $args[0] . ' = new Stmt\Nop($this->createZeroLengthAttributes($startAttributes)); }'
|
|
|
|
. ' else { ' . $args[0] . ' = null; }';
|
|
|
|
}
|
|
|
|
|
2016-04-02 15:22:24 +02:00
|
|
|
if ('strKind' == $name) {
|
|
|
|
assertArgs(1, $args, $name);
|
|
|
|
|
|
|
|
return '(' . $args[0] . '[0] === "\'" || (' . $args[0] . '[1] === "\'" && '
|
|
|
|
. '(' . $args[0] . '[0] === \'b\' || ' . $args[0] . '[0] === \'B\')) '
|
|
|
|
. '? Scalar\String_::KIND_SINGLE_QUOTED : Scalar\String_::KIND_DOUBLE_QUOTED)';
|
|
|
|
}
|
|
|
|
|
2016-08-30 22:37:51 +02:00
|
|
|
if ('prependLeadingComments' == $name) {
|
|
|
|
assertArgs(1, $args, $name);
|
|
|
|
|
|
|
|
return '$attrs = $this->startAttributeStack[#1]; $stmts = ' . $args[0] . '; '
|
2017-10-01 16:54:43 +02:00
|
|
|
. 'if (!empty($attrs[\'comments\'])) {'
|
2016-08-30 22:37:51 +02:00
|
|
|
. '$stmts[0]->setAttribute(\'comments\', '
|
|
|
|
. 'array_merge($attrs[\'comments\'], $stmts[0]->getAttribute(\'comments\', []))); }';
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-19 12:28:45 +02:00
|
|
|
function execCmd($cmd) {
|
|
|
|
$output = trim(shell_exec("$cmd 2>&1"));
|
|
|
|
if ($output !== "") {
|
|
|
|
echo "> " . $cmd . "\n";
|
|
|
|
echo $output;
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|