2011-05-27 18:20:44 +02:00
|
|
|
<?php
|
|
|
|
|
2011-08-04 12:46:48 +02:00
|
|
|
const GRAMMAR_FILE = './zend_language_parser.phpy';
|
2011-08-14 14:52:24 +02:00
|
|
|
const TMP_FILE = './tmp_parser.phpy';
|
|
|
|
const RESULT_FILE = './tmp_parser.php';
|
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 ///
|
|
|
|
///////////////////
|
|
|
|
|
2011-05-27 18:20:44 +02:00
|
|
|
echo '<pre>';
|
|
|
|
|
2011-08-04 12:46:48 +02:00
|
|
|
echo 'Building temporary preproprocessed grammar file.', "\n";
|
|
|
|
|
|
|
|
$grammarCode = file_get_contents(GRAMMAR_FILE);
|
|
|
|
|
|
|
|
$grammarCode = preg_replace('~[A-Z][a-zA-Z_]++::~', 'PHPParser_Node_$0', $grammarCode);
|
|
|
|
$grammarCode = resolveNodes($grammarCode);
|
|
|
|
$grammarCode = resolveMacros($grammarCode);
|
|
|
|
|
2011-08-14 14:52:24 +02:00
|
|
|
file_put_contents(TMP_FILE, $grammarCode);
|
2011-08-04 12:46:48 +02:00
|
|
|
|
|
|
|
echo 'Building parser. Output: "',
|
2011-08-14 14:52:24 +02:00
|
|
|
trim(shell_exec('kmyacc -l -m kmyacc.php.parser -p PHPParser_Parser ' . TMP_FILE . ' 2>&1')),
|
2011-05-27 18:20:44 +02:00
|
|
|
'"', "\n";
|
|
|
|
|
2011-08-14 14:52:24 +02:00
|
|
|
rename(RESULT_FILE, '../lib/PHPParser/Parser.php');
|
2011-05-29 20:51:15 +02:00
|
|
|
|
|
|
|
echo 'Building debug parser. Output: "',
|
2011-08-14 15:36:15 +02:00
|
|
|
trim(shell_exec('kmyacc -t -l -m kmyacc.php.parser -p PHPParser_Parser ' . TMP_FILE . ' 2>&1')),
|
2011-05-29 20:51:15 +02:00
|
|
|
'"', "\n";
|
2011-05-27 18:20:44 +02:00
|
|
|
|
2011-08-14 15:36:15 +02:00
|
|
|
rename(RESULT_FILE, '../lib/PHPParser/Parser/Debug.php');
|
2011-08-14 14:52:24 +02:00
|
|
|
|
|
|
|
unlink(TMP_FILE);
|
2011-08-04 12:46:48 +02:00
|
|
|
|
|
|
|
echo 'The following temporary preproprocessed grammar file was used:', "\n", $grammarCode;
|
|
|
|
|
|
|
|
echo '</pre>';
|
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
/// Preprocessing functions ///
|
|
|
|
///////////////////////////////
|
|
|
|
|
|
|
|
function resolveNodes($code) {
|
|
|
|
return preg_replace_callback(
|
2011-08-11 08:35:10 +02: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-08-09 14:55:45 +02:00
|
|
|
if (array() === $params) {
|
|
|
|
return 'new PHPParser_Node_' . $matches['name'] . '($line, $docComment)';
|
|
|
|
}
|
|
|
|
|
|
|
|
$withArray = false;
|
|
|
|
|
2011-08-04 12:46:48 +02:00
|
|
|
$paramCodes = array();
|
|
|
|
foreach ($params as $param) {
|
2011-08-09 14:55:45 +02:00
|
|
|
if (false !== strpos($param, ': ')) {
|
|
|
|
$withArray = true;
|
|
|
|
|
|
|
|
list($key, $value) = explode(': ', $param, 2);
|
|
|
|
$paramCodes[] = '\'' . $key . '\' => ' . $value;
|
|
|
|
} else {
|
|
|
|
$paramCodes[] = $param;
|
|
|
|
}
|
2011-08-04 12:46:48 +02:00
|
|
|
}
|
|
|
|
|
2011-08-09 14:55:45 +02:00
|
|
|
if (!$withArray) {
|
|
|
|
return 'new PHPParser_Node_' . $matches['name'] . '(' . implode(', ', $paramCodes) . ', $line, $docComment)';
|
|
|
|
} else {
|
|
|
|
return 'new PHPParser_Node_' . $matches['name'] . '(array(' . implode(', ', $paramCodes) . '), $line, $docComment)';
|
|
|
|
}
|
2011-08-04 12:46:48 +02:00
|
|
|
},
|
|
|
|
$code
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolveMacros($code) {
|
|
|
|
return preg_replace_callback(
|
2011-08-20 10:40:27 +02:00
|
|
|
'~(?<name>error|init|push|pushNormalizing|toArray|parse(?:Var|LNumber|DNumber|Encapsed))' . 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']
|
|
|
|
);
|
|
|
|
|
|
|
|
if ('error' == $name) {
|
|
|
|
assertArgs(1, $args, $name);
|
|
|
|
|
|
|
|
return 'throw new PHPParser_Error(' . $args[0] . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
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)';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('parseLNumber' == $name) {
|
|
|
|
assertArgs(1, $args, $name);
|
|
|
|
|
|
|
|
return '(int) ' . $args[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('parseDNumber' == $name) {
|
|
|
|
assertArgs(1, $args, $name);
|
|
|
|
|
|
|
|
return '(double) ' . $args[0];
|
|
|
|
}
|
2011-08-20 10:40:27 +02:00
|
|
|
|
|
|
|
if ('parseEncapsed' == $name) {
|
|
|
|
assertArgs(2, $args, $name);
|
|
|
|
|
|
|
|
return 'foreach (' . $args[0] . ' as &$s) { if (is_string($s)) { $s = PHPParser_Node_Scalar_String::parseEscapeSequences($s, ' . $args[1] . '); } }';
|
|
|
|
}
|
2011-08-04 12:46:48 +02:00
|
|
|
},
|
|
|
|
$code
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function assertArgs($num, $args, $name) {
|
|
|
|
if ($num != count($args)) {
|
|
|
|
die('Wrong argument count for ' . $name . '().');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////
|
|
|
|
/// 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
|
|
|
|
2011-08-04 12:46:48 +02:00
|
|
|
return array_filter($pieces);
|
|
|
|
}
|