Skip to content

Commit d591007

Browse files
author
Nick Harper
committedSep 27, 2012
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
1 parent 5a5d0b2 commit d591007

File tree

2 files changed

+83
-65
lines changed

2 files changed

+83
-65
lines changed
 

‎scripts/symbols/generate_ctags_symbols.php

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@
7373
list($token, $file_path, $line_num) = $tag_info;
7474
list($type, $language, $context) = $extension_fields;
7575

76+
// skip lines with tokens containing a space
77+
if (strpos($token, ' ') !== false) {
78+
continue;
79+
}
80+
7681
// strip "language:"
7782
$language = substr($language, 9);
7883

‎scripts/symbols/import_project_symbols.php

+78-65
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
'help' => 'Do not clear all symbols for this project before '.
3636
'uploading new symbols. Useful for incremental updating.',
3737
),
38+
array(
39+
'name' => 'ignore-errors',
40+
'help' => 'If a line can\'t be parsed, ignore that line and '.
41+
'continue instead of exiting.',
42+
),
3843
array(
3944
'name' => 'more',
4045
'wildcard' => true,
@@ -66,72 +71,80 @@
6671

6772
$symbols = array();
6873
foreach ($input as $key => $line) {
69-
$line_no = $key + 1;
70-
$matches = null;
71-
$ok = preg_match(
72-
'/^((?P<context>[^ ]+)? )?(?P<name>[^ ]+) (?P<type>[^ ]+) '.
73-
'(?P<lang>[^ ]+) (?P<line>\d+) (?P<path>.*)$/',
74-
$line,
75-
$matches);
76-
if (!$ok) {
77-
throw new Exception(
78-
"Line #{$line_no} of input is invalid. Expected five or six ".
79-
"space-delimited fields: maybe symbol context, symbol name, symbol ".
80-
"type, symbol language, line number, path. ".
81-
"For example:\n\n".
82-
"idx function php 13 /path/to/some/file.php\n\n".
83-
"Actual line was:\n\n".
84-
"{$line}");
85-
}
86-
if (empty($matches['context'])) {
87-
$matches['context'] = '';
88-
}
89-
$context = $matches['context'];
90-
$name = $matches['name'];
91-
$type = $matches['type'];
92-
$lang = $matches['lang'];
93-
$line_number = $matches['line'];
94-
$path = $matches['path'];
95-
96-
if (strlen($context) > 128) {
97-
throw new Exception(
98-
"Symbol context '{$context}' defined on line #{$line_no} is too long, ".
99-
"maximum symbol context length is 128 characters.");
100-
}
101-
102-
if (strlen($name) > 128) {
103-
throw new Exception(
104-
"Symbol name '{$name}' defined on line #{$line_no} is too long, maximum ".
105-
"symbol name length is 128 characters.");
106-
}
107-
108-
if (strlen($type) > 12) {
109-
throw new Exception(
110-
"Symbol type '{$type}' defined on line #{$line_no} is too long, maximum ".
111-
"symbol type length is 12 characters.");
112-
}
113-
114-
if (strlen($lang) > 32) {
115-
throw new Exception(
116-
"Symbol language '{$lang}' defined on line #{$line_no} is too long, ".
117-
"maximum symbol language length is 32 characters.");
74+
try {
75+
$line_no = $key + 1;
76+
$matches = null;
77+
$ok = preg_match(
78+
'/^((?P<context>[^ ]+)? )?(?P<name>[^ ]+) (?P<type>[^ ]+) '.
79+
'(?P<lang>[^ ]+) (?P<line>\d+) (?P<path>.*)$/',
80+
$line,
81+
$matches);
82+
if (!$ok) {
83+
throw new Exception(
84+
"Line #{$line_no} of input is invalid. Expected five or six ".
85+
"space-delimited fields: maybe symbol context, symbol name, symbol ".
86+
"type, symbol language, line number, path. ".
87+
"For example:\n\n".
88+
"idx function php 13 /path/to/some/file.php\n\n".
89+
"Actual line was:\n\n".
90+
"{$line}");
91+
}
92+
if (empty($matches['context'])) {
93+
$matches['context'] = '';
94+
}
95+
$context = $matches['context'];
96+
$name = $matches['name'];
97+
$type = $matches['type'];
98+
$lang = $matches['lang'];
99+
$line_number = $matches['line'];
100+
$path = $matches['path'];
101+
102+
if (strlen($context) > 128) {
103+
throw new Exception(
104+
"Symbol context '{$context}' defined on line #{$line_no} is too long, ".
105+
"maximum symbol context length is 128 characters.");
106+
}
107+
108+
if (strlen($name) > 128) {
109+
throw new Exception(
110+
"Symbol name '{$name}' defined on line #{$line_no} is too long, ".
111+
"maximum symbol name length is 128 characters.");
112+
}
113+
114+
if (strlen($type) > 12) {
115+
throw new Exception(
116+
"Symbol type '{$type}' defined on line #{$line_no} is too long, ".
117+
"maximum symbol type length is 12 characters.");
118+
}
119+
120+
if (strlen($lang) > 32) {
121+
throw new Exception(
122+
"Symbol language '{$lang}' defined on line #{$line_no} is too long, ".
123+
"maximum symbol language length is 32 characters.");
124+
}
125+
126+
if (!strlen($path) || $path[0] != 0) {
127+
throw new Exception(
128+
"Path '{$path}' defined on line #{$line_no} is invalid. Paths should ".
129+
"begin with '/' and specify a path from the root of the project, like ".
130+
"'/src/utils/utils.php'.");
131+
}
132+
133+
$symbols[] = array(
134+
'ctxt' => $context,
135+
'name' => $name,
136+
'type' => $type,
137+
'lang' => $lang,
138+
'line' => $line_number,
139+
'path' => $path,
140+
);
141+
} catch (Exception $e) {
142+
if ($args->getArg('ignore-errors')) {
143+
continue;
144+
} else {
145+
throw $e;
146+
}
118147
}
119-
120-
if (!strlen($path) || $path[0] != 0) {
121-
throw new Exception(
122-
"Path '{$path}' defined on line #{$line_no} is invalid. Paths should be ".
123-
"begin with '/' and specify a path from the root of the project, like ".
124-
"'/src/utils/utils.php'.");
125-
}
126-
127-
$symbols[] = array(
128-
'ctxt' => $context,
129-
'name' => $name,
130-
'type' => $type,
131-
'lang' => $lang,
132-
'line' => $line_number,
133-
'path' => $path,
134-
);
135148
}
136149

137150
echo "Looking up path IDs...\n";

0 commit comments

Comments
 (0)
Failed to load comments.