1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-25 08:12:40 +01:00

Add option to symbol import script to ignore errors in input

Summary:
I was running the generate_ctags_symbols script on a project, and some
combination of this script and ctags was generating invalid rows for this
script. It's nice if this script can ignore invalid input instead of having
to add another step to filter out invalid input.

Test Plan: run script with input that has some invalid lines

Reviewers: epriestley, vrana

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D3532
This commit is contained in:
Nick Harper 2012-09-27 15:36:38 -07:00
parent 5a5d0b2b56
commit d591007d27
2 changed files with 78 additions and 60 deletions

View file

@ -73,6 +73,11 @@ foreach (Futures($futures)->limit(8) as $file => $future) {
list($token, $file_path, $line_num) = $tag_info;
list($type, $language, $context) = $extension_fields;
// skip lines with tokens containing a space
if (strpos($token, ' ') !== false) {
continue;
}
// strip "language:"
$language = substr($language, 9);

View file

@ -35,6 +35,11 @@ $args->parse(
'help' => 'Do not clear all symbols for this project before '.
'uploading new symbols. Useful for incremental updating.',
),
array(
'name' => 'ignore-errors',
'help' => 'If a line can\'t be parsed, ignore that line and '.
'continue instead of exiting.',
),
array(
'name' => 'more',
'wildcard' => true,
@ -66,72 +71,80 @@ $input = explode("\n", $input);
$symbols = array();
foreach ($input as $key => $line) {
$line_no = $key + 1;
$matches = null;
$ok = preg_match(
'/^((?P<context>[^ ]+)? )?(?P<name>[^ ]+) (?P<type>[^ ]+) '.
'(?P<lang>[^ ]+) (?P<line>\d+) (?P<path>.*)$/',
$line,
$matches);
if (!$ok) {
throw new Exception(
"Line #{$line_no} of input is invalid. Expected five or six ".
"space-delimited fields: maybe symbol context, symbol name, symbol ".
"type, symbol language, line number, path. ".
"For example:\n\n".
"idx function php 13 /path/to/some/file.php\n\n".
"Actual line was:\n\n".
"{$line}");
}
if (empty($matches['context'])) {
$matches['context'] = '';
}
$context = $matches['context'];
$name = $matches['name'];
$type = $matches['type'];
$lang = $matches['lang'];
$line_number = $matches['line'];
$path = $matches['path'];
try {
$line_no = $key + 1;
$matches = null;
$ok = preg_match(
'/^((?P<context>[^ ]+)? )?(?P<name>[^ ]+) (?P<type>[^ ]+) '.
'(?P<lang>[^ ]+) (?P<line>\d+) (?P<path>.*)$/',
$line,
$matches);
if (!$ok) {
throw new Exception(
"Line #{$line_no} of input is invalid. Expected five or six ".
"space-delimited fields: maybe symbol context, symbol name, symbol ".
"type, symbol language, line number, path. ".
"For example:\n\n".
"idx function php 13 /path/to/some/file.php\n\n".
"Actual line was:\n\n".
"{$line}");
}
if (empty($matches['context'])) {
$matches['context'] = '';
}
$context = $matches['context'];
$name = $matches['name'];
$type = $matches['type'];
$lang = $matches['lang'];
$line_number = $matches['line'];
$path = $matches['path'];
if (strlen($context) > 128) {
throw new Exception(
"Symbol context '{$context}' defined on line #{$line_no} is too long, ".
"maximum symbol context length is 128 characters.");
}
if (strlen($context) > 128) {
throw new Exception(
"Symbol context '{$context}' defined on line #{$line_no} is too long, ".
"maximum symbol context length is 128 characters.");
}
if (strlen($name) > 128) {
throw new Exception(
"Symbol name '{$name}' defined on line #{$line_no} is too long, maximum ".
"symbol name length is 128 characters.");
}
if (strlen($name) > 128) {
throw new Exception(
"Symbol name '{$name}' defined on line #{$line_no} is too long, ".
"maximum symbol name length is 128 characters.");
}
if (strlen($type) > 12) {
throw new Exception(
"Symbol type '{$type}' defined on line #{$line_no} is too long, maximum ".
"symbol type length is 12 characters.");
}
if (strlen($type) > 12) {
throw new Exception(
"Symbol type '{$type}' defined on line #{$line_no} is too long, ".
"maximum symbol type length is 12 characters.");
}
if (strlen($lang) > 32) {
throw new Exception(
"Symbol language '{$lang}' defined on line #{$line_no} is too long, ".
"maximum symbol language length is 32 characters.");
}
if (strlen($lang) > 32) {
throw new Exception(
"Symbol language '{$lang}' defined on line #{$line_no} is too long, ".
"maximum symbol language length is 32 characters.");
}
if (!strlen($path) || $path[0] != 0) {
throw new Exception(
"Path '{$path}' defined on line #{$line_no} is invalid. Paths should be ".
"begin with '/' and specify a path from the root of the project, like ".
"'/src/utils/utils.php'.");
}
if (!strlen($path) || $path[0] != 0) {
throw new Exception(
"Path '{$path}' defined on line #{$line_no} is invalid. Paths should ".
"begin with '/' and specify a path from the root of the project, like ".
"'/src/utils/utils.php'.");
}
$symbols[] = array(
'ctxt' => $context,
'name' => $name,
'type' => $type,
'lang' => $lang,
'line' => $line_number,
'path' => $path,
);
$symbols[] = array(
'ctxt' => $context,
'name' => $name,
'type' => $type,
'lang' => $lang,
'line' => $line_number,
'path' => $path,
);
} catch (Exception $e) {
if ($args->getArg('ignore-errors')) {
continue;
} else {
throw $e;
}
}
}
echo "Looking up path IDs...\n";