Rename expand_error_t to expand_result_t and make it class enum

Also lowercase it all.
This commit is contained in:
ridiculousfish 2019-04-22 15:06:52 -07:00
parent b54c44f2f6
commit dcaac58f45
8 changed files with 95 additions and 92 deletions

View file

@ -665,11 +665,11 @@ void completer_t::complete_cmd(const wcstring &str_cmd, bool use_function, bool
if (use_command) {
// Append all possible executables
expand_error_t result = expand_string(str_cmd, &this->completions,
EXPAND_SPECIAL_FOR_COMMAND | EXPAND_FOR_COMPLETIONS |
EXECUTABLES_ONLY | this->expand_flags(),
vars, NULL);
if (result != EXPAND_ERROR && this->wants_descriptions()) {
expand_result_t result = expand_string(str_cmd, &this->completions,
EXPAND_SPECIAL_FOR_COMMAND | EXPAND_FOR_COMPLETIONS |
EXECUTABLES_ONLY | this->expand_flags(),
vars, NULL);
if (result != expand_result_t::error && this->wants_descriptions()) {
this->complete_cmd_desc(str_cmd);
}
}
@ -677,7 +677,7 @@ void completer_t::complete_cmd(const wcstring &str_cmd, bool use_function, bool
if (use_implicit_cd) {
// We don't really care if this succeeds or fails. If it succeeds this->completions will be
// updated with choices for the user.
expand_error_t ignore =
expand_result_t ignore =
// Append all matching directories
expand_string(str_cmd, &this->completions,
EXPAND_FOR_COMPLETIONS | DIRECTORIES_ONLY | this->expand_flags(), vars,
@ -1092,7 +1092,8 @@ void completer_t::complete_param_expand(const wcstring &str, bool do_file,
// See #4954.
const wcstring sep_string = wcstring(str, sep_index + 1);
std::vector<completion_t> local_completions;
if (expand_string(sep_string, &local_completions, flags, vars, NULL) == EXPAND_ERROR) {
if (expand_string(sep_string, &local_completions, flags, vars, NULL) ==
expand_result_t::error) {
debug(3, L"Error while expanding string '%ls'", sep_string.c_str());
}
@ -1111,7 +1112,7 @@ void completer_t::complete_param_expand(const wcstring &str, bool do_file,
// consider relaxing this if there was a preceding double-dash argument.
if (string_prefixes_string(L"-", str)) flags &= ~EXPAND_FUZZY_MATCH;
if (expand_string(str, &this->completions, flags, vars, NULL) == EXPAND_ERROR) {
if (expand_string(str, &this->completions, flags, vars, NULL) == expand_result_t::error) {
debug(3, L"Error while expanding string '%ls'", str.c_str());
}
}

View file

@ -470,8 +470,8 @@ static bool expand_variables(wcstring instr, std::vector<completion_t> *out, siz
}
/// Perform brace expansion.
static expand_error_t expand_braces(const wcstring &instr, expand_flags_t flags,
std::vector<completion_t> *out, parse_error_list_t *errors) {
static expand_result_t expand_braces(const wcstring &instr, expand_flags_t flags,
std::vector<completion_t> *out, parse_error_list_t *errors) {
bool syntax_error = false;
int brace_count = 0;
@ -542,12 +542,12 @@ static expand_error_t expand_braces(const wcstring &instr, expand_flags_t flags,
if (syntax_error) {
append_syntax_error(errors, SOURCE_LOCATION_UNKNOWN, _(L"Mismatched braces"));
return EXPAND_ERROR;
return expand_result_t::error;
}
if (brace_begin == NULL) {
append_completion(out, instr);
return EXPAND_OK;
return expand_result_t::ok;
}
length_preceding_braces = (brace_begin - in);
@ -585,7 +585,7 @@ static expand_error_t expand_braces(const wcstring &instr, expand_flags_t flags,
brace_count--;
}
}
return EXPAND_OK;
return expand_result_t::ok;
}
/// Perform cmdsubst expansion.
@ -880,24 +880,24 @@ class expander_t {
/// An expansion stage is a member function pointer.
/// It accepts the input string (transferring ownership) and returns the list of output
/// completions by reference. It may return an error, which halts expansion.
using stage_t = expand_error_t (expander_t::*)(wcstring, std::vector<completion_t> *);
using stage_t = expand_result_t (expander_t::*)(wcstring, std::vector<completion_t> *);
expand_error_t stage_cmdsubst(wcstring input, std::vector<completion_t> *out);
expand_error_t stage_variables(wcstring input, std::vector<completion_t> *out);
expand_error_t stage_braces(wcstring input, std::vector<completion_t> *out);
expand_error_t stage_home_and_self(wcstring input, std::vector<completion_t> *out);
expand_error_t stage_wildcards(wcstring path_to_expand, std::vector<completion_t> *out);
expand_result_t stage_cmdsubst(wcstring input, std::vector<completion_t> *out);
expand_result_t stage_variables(wcstring input, std::vector<completion_t> *out);
expand_result_t stage_braces(wcstring input, std::vector<completion_t> *out);
expand_result_t stage_home_and_self(wcstring input, std::vector<completion_t> *out);
expand_result_t stage_wildcards(wcstring path_to_expand, std::vector<completion_t> *out);
expander_t(const environment_t &vars, expand_flags_t flags, parse_error_list_t *errors)
: vars(vars), flags(flags), errors(errors) {}
public:
static expand_error_t expand_string(wcstring input, std::vector<completion_t> *out_completions,
expand_flags_t flags, const environment_t &vars,
parse_error_list_t *errors);
static expand_result_t expand_string(wcstring input, std::vector<completion_t> *out_completions,
expand_flags_t flags, const environment_t &vars,
parse_error_list_t *errors);
};
expand_error_t expander_t::stage_cmdsubst(wcstring input, std::vector<completion_t> *out) {
expand_result_t expander_t::stage_cmdsubst(wcstring input, std::vector<completion_t> *out) {
if (EXPAND_SKIP_CMDSUBST & flags) {
size_t cur = 0, start = 0, end;
switch (parse_util_locate_cmdsubst_range(input, &cur, nullptr, &start, &end, true)) {
@ -908,17 +908,17 @@ expand_error_t expander_t::stage_cmdsubst(wcstring input, std::vector<completion
append_cmdsub_error(errors, start, L"Command substitutions not allowed");
case -1:
default:
return EXPAND_ERROR;
return expand_result_t::error;
}
} else {
bool cmdsubst_ok = expand_cmdsubst(std::move(input), out, errors);
if (!cmdsubst_ok) return EXPAND_ERROR;
if (!cmdsubst_ok) return expand_result_t::error;
}
return EXPAND_OK;
return expand_result_t::ok;
}
expand_error_t expander_t::stage_variables(wcstring input, std::vector<completion_t> *out) {
expand_result_t expander_t::stage_variables(wcstring input, std::vector<completion_t> *out) {
// We accept incomplete strings here, since complete uses expand_string to expand incomplete
// strings from the commandline.
wcstring next;
@ -934,29 +934,29 @@ expand_error_t expander_t::stage_variables(wcstring input, std::vector<completio
} else {
size_t size = next.size();
if (!expand_variables(std::move(next), out, size, vars, errors)) {
return EXPAND_ERROR;
return expand_result_t::error;
}
}
return EXPAND_OK;
return expand_result_t::ok;
}
expand_error_t expander_t::stage_braces(wcstring input, std::vector<completion_t> *out) {
expand_result_t expander_t::stage_braces(wcstring input, std::vector<completion_t> *out) {
UNUSED(vars);
return expand_braces(input, flags, out, errors);
}
expand_error_t expander_t::stage_home_and_self(wcstring input, std::vector<completion_t> *out) {
expand_result_t expander_t::stage_home_and_self(wcstring input, std::vector<completion_t> *out) {
if (!(EXPAND_SKIP_HOME_DIRECTORIES & flags)) {
expand_home_directory(input, vars);
}
expand_percent_self(input);
append_completion(out, std::move(input));
return EXPAND_OK;
return expand_result_t::ok;
}
expand_error_t expander_t::stage_wildcards(wcstring path_to_expand,
std::vector<completion_t> *out) {
expand_error_t result = EXPAND_OK;
expand_result_t expander_t::stage_wildcards(wcstring path_to_expand,
std::vector<completion_t> *out) {
expand_result_t result = expand_result_t::ok;
remove_internal_separator(&path_to_expand, flags & EXPAND_SKIP_WILDCARDS);
const bool has_wildcard = wildcard_has(path_to_expand, true /* internal, i.e. ANY_STRING */);
@ -1017,17 +1017,17 @@ expand_error_t expander_t::stage_wildcards(wcstring path_to_expand,
}
}
result = EXPAND_WILDCARD_NO_MATCH;
result = expand_result_t::wildcard_no_match;
std::vector<completion_t> expanded;
for (size_t wd_idx = 0; wd_idx < effective_working_dirs.size(); wd_idx++) {
int local_wc_res = wildcard_expand_string(
path_to_expand, effective_working_dirs.at(wd_idx), flags, &expanded);
if (local_wc_res > 0) {
// Something matched,so overall we matched.
result = EXPAND_WILDCARD_MATCH;
result = expand_result_t::wildcard_match;
} else if (local_wc_res < 0) {
// Cancellation
result = EXPAND_ERROR;
result = expand_result_t::error;
break;
}
}
@ -1045,13 +1045,14 @@ expand_error_t expander_t::stage_wildcards(wcstring path_to_expand,
return result;
}
expand_error_t expander_t::expand_string(wcstring input, std::vector<completion_t> *out_completions,
expand_flags_t flags, const environment_t &vars,
parse_error_list_t *errors) {
expand_result_t expander_t::expand_string(wcstring input,
std::vector<completion_t> *out_completions,
expand_flags_t flags, const environment_t &vars,
parse_error_list_t *errors) {
// Early out. If we're not completing, and there's no magic in the input, we're done.
if (!(flags & EXPAND_FOR_COMPLETIONS) && expand_is_clean(input)) {
append_completion(out_completions, std::move(input));
return EXPAND_OK;
return expand_result_t::ok;
}
expander_t expand(vars, flags, errors);
@ -1065,18 +1066,18 @@ expand_error_t expander_t::expand_string(wcstring input, std::vector<completion_
std::vector<completion_t> completions, output_storage;
append_completion(&completions, input);
expand_error_t total_result = EXPAND_OK;
expand_result_t total_result = expand_result_t::ok;
for (stage_t stage : stages) {
for (completion_t &comp : completions) {
expand_error_t this_result =
expand_result_t this_result =
(expand.*stage)(std::move(comp.completion), &output_storage);
// If this_result was no match, but total_result is that we have a match, then don't
// change it.
if (!(this_result == EXPAND_WILDCARD_NO_MATCH &&
total_result == EXPAND_WILDCARD_MATCH)) {
if (!(this_result == expand_result_t::wildcard_no_match &&
total_result == expand_result_t::wildcard_match)) {
total_result = this_result;
}
if (total_result == EXPAND_ERROR) {
if (total_result == expand_result_t::error) {
break;
}
}
@ -1084,12 +1085,12 @@ expand_error_t expander_t::expand_string(wcstring input, std::vector<completion_
// Output becomes our next stage's input.
completions.swap(output_storage);
output_storage.clear();
if (total_result == EXPAND_ERROR) {
if (total_result == expand_result_t::error) {
break;
}
}
if (total_result != EXPAND_ERROR) {
if (total_result != expand_result_t::error) {
// Hack to un-expand tildes (see #647).
if (!(flags & EXPAND_SKIP_HOME_DIRECTORIES)) {
unexpand_tildes(input, vars, &completions);
@ -1102,9 +1103,9 @@ expand_error_t expander_t::expand_string(wcstring input, std::vector<completion_
}
} // namespace
expand_error_t expand_string(wcstring input, std::vector<completion_t> *out_completions,
expand_flags_t flags, const environment_t &vars,
parse_error_list_t *errors) {
expand_result_t expand_string(wcstring input, std::vector<completion_t> *out_completions,
expand_flags_t flags, const environment_t &vars,
parse_error_list_t *errors) {
return expander_t::expand_string(std::move(input), out_completions, flags, vars, errors);
}
@ -1116,7 +1117,8 @@ bool expand_one(wcstring &string, expand_flags_t flags, const environment_t &var
return true;
}
if (expand_string(string, &completions, flags | EXPAND_NO_DESCRIPTIONS, vars, errors) &&
if (expand_string(string, &completions, flags | EXPAND_NO_DESCRIPTIONS, vars, errors) !=
expand_result_t::error &&
completions.size() == 1) {
string = std::move(completions.at(0).completion);
return true;
@ -1124,21 +1126,21 @@ bool expand_one(wcstring &string, expand_flags_t flags, const environment_t &var
return false;
}
expand_error_t expand_to_command_and_args(const wcstring &instr, const environment_t &vars,
wcstring *out_cmd, wcstring_list_t *out_args,
parse_error_list_t *errors) {
expand_result_t expand_to_command_and_args(const wcstring &instr, const environment_t &vars,
wcstring *out_cmd, wcstring_list_t *out_args,
parse_error_list_t *errors) {
// Fast path.
if (expand_is_clean(instr)) {
*out_cmd = instr;
return EXPAND_OK;
return expand_result_t::ok;
}
std::vector<completion_t> completions;
expand_error_t expand_err = expand_string(
expand_result_t expand_err = expand_string(
instr, &completions, EXPAND_SKIP_CMDSUBST | EXPAND_NO_DESCRIPTIONS | EXPAND_SKIP_JOBS, vars,
errors);
if (expand_err == EXPAND_OK || expand_err == EXPAND_WILDCARD_MATCH) {
if (expand_err == expand_result_t::ok || expand_err == expand_result_t::wildcard_match) {
// The first completion is the command, any remaning are arguments.
bool first = true;
for (auto &comp : completions) {

View file

@ -89,15 +89,15 @@ enum {
};
/// These are the possible return values for expand_string. Note how zero value is the only error.
enum expand_error_t {
enum class expand_result_t {
/// Error
EXPAND_ERROR,
error,
/// Ok
EXPAND_OK,
ok,
/// Ok, a wildcard in the string matched no files.
EXPAND_WILDCARD_NO_MATCH,
wildcard_no_match,
/// Ok, a wildcard in the string matched a file.
EXPAND_WILDCARD_MATCH
wildcard_match,
};
/// The string represented by PROCESS_EXPAND_SELF
@ -118,12 +118,12 @@ enum expand_error_t {
/// \param vars variables used during expansion.
/// \param errors Resulting errors, or NULL to ignore
///
/// \return One of EXPAND_OK, EXPAND_ERROR, EXPAND_WILDCARD_MATCH and EXPAND_WILDCARD_NO_MATCH.
/// EXPAND_WILDCARD_NO_MATCH and EXPAND_WILDCARD_MATCH are normal exit conditions used only on
/// \return An expand_result_t.
/// wildcard_no_match and wildcard_match are normal exit conditions used only on
/// strings containing wildcards to tell if the wildcard produced any matches.
__warn_unused expand_error_t expand_string(wcstring input, std::vector<completion_t> *output,
expand_flags_t flags, const environment_t &vars,
parse_error_list_t *errors);
__warn_unused expand_result_t expand_string(wcstring input, std::vector<completion_t> *output,
expand_flags_t flags, const environment_t &vars,
parse_error_list_t *errors);
/// expand_one is identical to expand_string, except it will fail if in expands to more than one
/// string. This is used for expanding command names.
@ -143,9 +143,9 @@ bool expand_one(wcstring &inout_str, expand_flags_t flags, const environment_t &
/// that API does not distinguish between expansion resulting in an empty command (''), and
/// expansion resulting in no command (e.g. unset variable).
// \return an expand error.
expand_error_t expand_to_command_and_args(const wcstring &instr, const environment_t &vars,
wcstring *out_cmd, wcstring_list_t *out_args,
parse_error_list_t *errors = NULL);
expand_result_t expand_to_command_and_args(const wcstring &instr, const environment_t &vars,
wcstring *out_cmd, wcstring_list_t *out_args,
parse_error_list_t *errors = NULL);
/// Convert the variable value to a human readable form, i.e. escape things, handle arrays, etc.
/// Suitable for pretty-printing.

View file

@ -1592,7 +1592,7 @@ static bool expand_test(const wchar_t *in, expand_flags_t flags, ...) {
wchar_t *arg;
parse_error_list_t errors;
if (expand_string(in, &output, flags, pwd_environment_t{}, &errors) == EXPAND_ERROR) {
if (expand_string(in, &output, flags, pwd_environment_t{}, &errors) == expand_result_t::error) {
if (errors.empty()) {
err(L"Bug: Parse error reported but no error text found.");
} else {

View file

@ -344,8 +344,8 @@ static bool plain_statement_get_expanded_command(const wcstring &src,
// Get the command. Try expanding it. If we cannot, it's an error.
maybe_t<wcstring> cmd = command_for_plain_statement(stmt, src);
if (!cmd) return false;
expand_error_t err = expand_to_command_and_args(*cmd, vars, out_cmd, nullptr);
return err == EXPAND_OK || err == EXPAND_WILDCARD_MATCH;
expand_result_t err = expand_to_command_and_args(*cmd, vars, out_cmd, nullptr);
return err == expand_result_t::ok || err == expand_result_t::wildcard_match;
}
rgb_color_t highlight_get_color(const highlight_spec_t &highlight, bool is_background) {

View file

@ -449,21 +449,21 @@ parse_execution_result_t parse_execution_context_t::run_switch_statement(
// Expand it. We need to offset any errors by the position of the string.
std::vector<completion_t> switch_values_expanded;
parse_error_list_t errors;
int expand_ret = expand_string(switch_value, &switch_values_expanded, EXPAND_NO_DESCRIPTIONS,
parser->vars(), &errors);
auto expand_ret = expand_string(switch_value, &switch_values_expanded, EXPAND_NO_DESCRIPTIONS,
parser->vars(), &errors);
parse_error_offset_source_start(&errors, switch_value_n.source_range()->start);
switch (expand_ret) {
case EXPAND_ERROR: {
case expand_result_t::error: {
result = report_errors(errors);
break;
}
case EXPAND_WILDCARD_NO_MATCH: {
case expand_result_t::wildcard_no_match: {
result = report_unmatched_wildcard_error(switch_value_n);
break;
}
case EXPAND_WILDCARD_MATCH:
case EXPAND_OK: {
case expand_result_t::wildcard_match:
case expand_result_t::ok: {
break;
}
default: {
@ -746,15 +746,15 @@ parse_execution_result_t parse_execution_context_t::expand_command(
wcstring unexp_cmd = *command_for_plain_statement(statement, pstree->src);
// Expand the string to produce completions, and report errors.
expand_error_t expand_err =
expand_result_t expand_err =
expand_to_command_and_args(unexp_cmd, parser->vars(), out_cmd, out_args, &errors);
if (expand_err == EXPAND_ERROR) {
if (expand_err == expand_result_t::error) {
proc_set_last_statuses(statuses_t::just(STATUS_ILLEGAL_CMD));
return report_errors(errors);
} else if (expand_err == EXPAND_WILDCARD_NO_MATCH) {
} else if (expand_err == expand_result_t::wildcard_no_match) {
return report_unmatched_wildcard_error(statement);
}
assert(expand_err == EXPAND_OK || expand_err == EXPAND_WILDCARD_MATCH);
assert(expand_err == expand_result_t::ok || expand_err == expand_result_t::wildcard_match);
// Complain if the resulting expansion was empty, or expanded to an empty string.
if (out_cmd->empty()) {
@ -906,15 +906,15 @@ parse_execution_result_t parse_execution_context_t::expand_arguments_from_nodes(
// Expand this string.
parse_error_list_t errors;
arg_expanded.clear();
int expand_ret =
auto expand_ret =
expand_string(arg_str, &arg_expanded, EXPAND_NO_DESCRIPTIONS, parser->vars(), &errors);
parse_error_offset_source_start(&errors, arg_node.source_range()->start);
switch (expand_ret) {
case EXPAND_ERROR: {
case expand_result_t::error: {
this->report_errors(errors);
return parse_execution_errored;
}
case EXPAND_WILDCARD_NO_MATCH: {
case expand_result_t::wildcard_no_match: {
if (glob_behavior == failglob) {
// Report the unmatched wildcard error and stop processing.
report_unmatched_wildcard_error(arg_node);
@ -922,8 +922,8 @@ parse_execution_result_t parse_execution_context_t::expand_arguments_from_nodes(
}
break;
}
case EXPAND_WILDCARD_MATCH:
case EXPAND_OK: {
case expand_result_t::wildcard_match:
case expand_result_t::ok: {
break;
}
default: {

View file

@ -1134,7 +1134,7 @@ static bool detect_errors_in_plain_statement(const wcstring &buff_src,
wcstring command;
// Check that we can expand the command.
if (expand_to_command_and_args(*unexp_command, null_environment_t{}, &command, nullptr,
parse_errors) == EXPAND_ERROR) {
parse_errors) == expand_result_t::error) {
errored = true;
parse_error_offset_source_start(parse_errors, source_start);
}

View file

@ -332,7 +332,7 @@ void parser_t::expand_argument_list(const wcstring &arg_list_src, expand_flags_t
while (auto arg = arg_list.next_in_list<grammar::argument>()) {
const wcstring arg_src = arg.get_source(arg_list_src);
if (expand_string(arg_src, output_arg_list, eflags, vars, NULL /* errors */) ==
EXPAND_ERROR) {
expand_result_t::error) {
break; // failed to expand a string
}
}