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
|
|
|
|
|
|
|
// 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 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";
|
|
|
|
$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 18:39:55 +02:00
|
|
|
echo "Building token definition.\n";
|
|
|
|
$output = trim(shell_exec("$kmyacc -l -m $tokensTemplate $tmpGrammarFile 2>&1"));
|
|
|
|
assert($output === '');
|
|
|
|
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
|
|
|
|
2011-12-04 16:52:43 +01:00
|
|
|
if ('parseEncapsedDoc' == $name) {
|
2015-06-13 20:51:02 +02:00
|
|
|
assertArgs(2, $args, $name);
|
2011-12-04 16:52:43 +01: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, null, ' . $args[1] . '); } }'
|
|
|
|
. ' $s->value = preg_replace(\'~(\r\n|\n|\r)\z~\', \'\', $s->value);'
|
|
|
|
. ' if (\'\' === $s->value) array_pop(' . $args[0] . ');';
|
2011-12-04 16:52:43 +01:00
|
|
|
}
|
|
|
|
|
2015-10-02 11:03:12 +02:00
|
|
|
if ('makeNop' == $name) {
|
|
|
|
assertArgs(2, $args, $name);
|
|
|
|
|
2016-04-05 11:53:50 +02:00
|
|
|
return '$startAttributes = ' . $args[1] . ';'
|
2015-10-02 11:03:12 +02:00
|
|
|
. ' if (isset($startAttributes[\'comments\']))'
|
|
|
|
. ' { ' . $args[0] . ' = new Stmt\Nop([\'comments\' => $startAttributes[\'comments\']]); }'
|
|
|
|
. ' 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)';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('setDocStringAttrs' == $name) {
|
|
|
|
assertArgs(2, $args, $name);
|
|
|
|
|
|
|
|
return $args[0] . '[\'kind\'] = strpos(' . $args[1] . ', "\'") === false '
|
|
|
|
. '? Scalar\String_::KIND_HEREDOC : Scalar\String_::KIND_NOWDOC; '
|
|
|
|
. 'preg_match(\'/\A[bB]?<<<[ \t]*[\\\'"]?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)[\\\'"]?(?:\r\n|\n|\r)\z/\', ' . $args[1] . ', $matches); '
|
|
|
|
. $args[0] . '[\'docLabel\'] = $matches[1];';
|
|
|
|
}
|
|
|
|
|
2016-08-30 22:37:51 +02:00
|
|
|
if ('prependLeadingComments' == $name) {
|
|
|
|
assertArgs(1, $args, $name);
|
|
|
|
|
|
|
|
return '$attrs = $this->startAttributeStack[#1]; $stmts = ' . $args[0] . '; '
|
|
|
|
. 'if (!empty($attrs[\'comments\']) && isset($stmts[0])) {'
|
|
|
|
. '$stmts[0]->setAttribute(\'comments\', '
|
|
|
|
. 'array_merge($attrs[\'comments\'], $stmts[0]->getAttribute(\'comments\', []))); }';
|
|
|
|
}
|
|
|
|
|
2016-12-22 21:13:42 +01:00
|
|
|
if ('makeIdent' == $name) {
|
|
|
|
assertArgs(1, $args, $name);
|
|
|
|
|
|
|
|
return '($this->useIdentifierNodes ? new Node\Identifier(' . $args[0] . ', '
|
|
|
|
. '$this->startAttributeStack[#1] + $this->endAttributes) : ' . $args[0] . ')';
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|