1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
<?php
chdir('buildtools');
require __DIR__ . '/vendor/autoload.php';
use Dpp\StructGeneratorInterface;
if (count($argv) < 2) {
die("You must specify a generator type\n");
} else {
$generatorName = $argv[1];
$generator = new $generatorName();
}
chdir('..');
/* Get the content of all cluster source files into an array */
exec("cat src/dpp/cluster/*.cpp", $clustercpp);
/* These methods have signatures incompatible with this script */
$blacklist = [
];
/* The script cannot determine the correct return type of these methods,
* so we specify it by hand here.
*/
$forcedReturn = [
'direct_message_create' => 'message',
'guild_get_members' => 'guild_member_map',
'guild_search_members' => 'guild_member_map',
'message_create' => 'message',
'message_edit' => 'message',
'message_add_reaction' => 'confirmation',
'message_delete_reaction' => 'confirmation',
'message_delete_reaction_emoji' => 'confirmation',
'message_delete_all_reactions' => 'confirmation',
'message_delete_own_reaction' => 'confirmation',
'channel_edit_permissions' => 'confirmation',
'channel_typing' => 'confirmation',
'message_get_reactions' => 'emoji_map',
'thread_create_in_forum' => 'thread',
'threads_get_private_archived' => 'thread_map',
'threads_get_public_archived' => 'thread_map',
'threads_get_active' => 'active_threads',
'user_get_cached' => 'user_identified',
'channel_pins_get' => 'message_pin_map'
];
/* Get the contents of cluster.h into an array */
$header = explode("\n", file_get_contents('include/dpp/cluster.h'));
/* Finite state machine state constants */
const STATE_SEARCH_FOR_FUNCTION = 0;
const STATE_IN_FUNCTION = 1;
const STATE_END_OF_FUNCTION = 2;
$state = STATE_SEARCH_FOR_FUNCTION;
$currentFunction = $parameters = $returnType = '';
$content = $generator->generateHeaderStart();
$cppcontent = $generator->generatecppStart();
if (!$generator->checkForChanges()) {
exit(0);
}
$lastFunc = '<none>';
$l = 0;
/* Scan every line of the C++ source */
foreach ($clustercpp as $cpp) {
$l++;
/* Look for declaration of function body */
if ($state == STATE_SEARCH_FOR_FUNCTION &&
preg_match('/^\s*void\s+cluster::([^(]+)\s*\((.*)command_completion_event_t\s*callback\s*\)/', $cpp, $matches)) {
$currentFunction = $matches[1];
$parameters = preg_replace('/,\s*$/', '', $matches[2]);
if (!in_array($currentFunction, $blacklist)) {
$state = STATE_IN_FUNCTION;
}
/* Scan function body */
} elseif ($state == STATE_IN_FUNCTION) {
/* End of function */
if (preg_match('/^\}\s*$/', $cpp)) {
$state = STATE_END_OF_FUNCTION;
/* look for the return type of the method */
} elseif (preg_match('/rest_request<([^>]+)>/', $cpp, $matches)) {
/* rest_request<T> */
$returnType = $matches[1];
} elseif (preg_match('/rest_request_list<([^>]+)>/', $cpp, $matches)) {
/* rest_request_list<T> */
$returnType = $matches[1] . '_map';
} elseif (preg_match('/rest_request_vector<([^>]+)>/', $cpp, $matches)) {
/* rest_request_vector<T> */
$returnType = $matches[1] . '_list';
} elseif (preg_match('/callback\(confirmation_callback_t\(\w+, ([^(]+)\(.*, \w+\)\)/', $cpp, $matches)) {
/* confirmation_callback_t */
$returnType = $matches[1];
} elseif (!empty($forcedReturn[$currentFunction])) {
/* Forced return type */
$returnType = $forcedReturn[$currentFunction];
}
}
/* Completed parsing of function body */
if ($state == STATE_END_OF_FUNCTION && !empty($currentFunction) && !empty($returnType)) {
if (!in_array($currentFunction, $blacklist)) {
$parameterList = explode(',', $parameters);
$parameterNames = [];
$parameterTypes = [];
foreach ($parameterList as $parameter) {
$parts = explode(' ', trim($parameter));
$name = trim(preg_replace('/[\s\*\&]+/', '', $parts[count($parts) - 1]));
$parameterNames[] = $name;
$parameterTypes[] = trim(substr($parameter, 0, strlen($parameter) - strlen($name)));
}
$content .= getComments($generator, $currentFunction, $returnType, $parameterNames) . "\n";
$fullParameters = getFullParameters($currentFunction, $parameterNames);
$parameterNames = trim(join(', ', $parameterNames));
$parameterTypes = trim(join(', ', $parameterTypes));
if (!empty($parameterNames)) {
$parameterNames = ', ' . $parameterNames;
}
$noDefaults = $parameters;
$parameters = !empty($fullParameters) ? $fullParameters : $parameters;
$content .= $generator->generateHeaderDef($returnType, $currentFunction, $parameters, $noDefaults, $parameterTypes, $parameterNames);
$cppcontent .= $generator->generateCppDef($returnType, $currentFunction, $parameters, $noDefaults, $parameterTypes, $parameterNames);
}
$lastFunc = $currentFunction;
$currentFunction = $parameters = $returnType = '';
$state = STATE_SEARCH_FOR_FUNCTION;
}
}
if ($state != STATE_SEARCH_FOR_FUNCTION) {
die("\n\n\nBuilding headers is broken ($l) - state machine finished in the middle of function $currentFunction (previous $lastFunc) with parameters $parameters rv $returnType state=$state\n\n\n");
}
$content .= <<<EOT
/* End of auto-generated definitions */
EOT;
$cppcontent .= <<<EOT
};
/* End of auto-generated definitions */
EOT;
/**
* @brief Get parameters of a function with defaults
* @param string $currentFunction Current function name
* @param array $parameters Parameter names
* @return string Parameter list
*/
function getFullParameters(string $currentFunction, array $parameters): string
{
global $header;
foreach ($header as $line) {
if (preg_match('/^\s*void\s+' . $currentFunction . '\s*\((.*' . join('.*', $parameters) . '.*)command_completion_event_t\s*callback\s*/', $line, $matches)) {
return preg_replace('/,\s*$/', '', $matches[1]);
}
}
return '';
}
/**
* @brief Get the comment block of a function.
* Adds see/return doxygen tags
* @param string $currentFunction function name
* @param string $returnType Return type of function
* @param array $parameters Parameter names
* @return string Comment block
*/
function getComments(StructGeneratorInterface $generator, string $currentFunction, string $returnType, array $parameters): string
{
global $header;
/* First find the function */
foreach ($header as $i => $line) {
if (preg_match('/^\s*void\s+' . $currentFunction . '\s*\(.*' . join('.*', $parameters) . '.*command_completion_event_t\s*callback\s*/', $line)) {
/* Backpeddle */
$lineIndex = 1;
for ($n = $i; $n != 0; --$n, $lineIndex++) {
$header[$n] = preg_replace('/^\t+/', '', $header[$n]);
$header[$n] = preg_replace('/@see (.+?)$/', '@see dpp::cluster::' . $currentFunction . "\n * @see \\1", $header[$n]);
$header[$n] = preg_replace('/@param callback .*$/', '@return ' . $returnType . ' returned object on completion', $header[$n]);
if (preg_match('/\s*\* On success /i', $header[$n])) {
$header[$n] = "";
}
if (preg_match('/\s*\/\*\*\s*$/', $header[$n])) {
$part = array_slice($header, $n, $lineIndex - 1);
array_splice($part, count($part) - 1, 0, $generator->getCommentArray());
return str_replace("\n\n", "\n", join("\n", $part));
}
}
return '';
}
}
return '';
}
/* Finished parsing, output autogenerated files */
$generator->saveHeader($content);
$generator->savecpp($cppcontent);
|