Rename BRACKET in reference to { to BRACE instead per #3802

This `{` is a curly brace. This `[` is a square bracket.
This commit is contained in:
Mahmoud Al-Qudsi 2018-03-10 13:16:07 -06:00
parent 18a9aa58cd
commit 65a03c86cb
5 changed files with 59 additions and 58 deletions

View file

@ -1353,20 +1353,20 @@ static bool unescape_string_internal(const wchar_t *const input, const size_t in
case L'{': {
if (unescape_special) {
bracket_count++;
to_append_or_none = BRACKET_BEGIN;
to_append_or_none = BRACE_BEGIN;
}
break;
}
case L'}': {
if (unescape_special) {
bracket_count--;
to_append_or_none = BRACKET_END;
to_append_or_none = BRACE_END;
}
break;
}
case L',': {
if (unescape_special && bracket_count > 0) {
to_append_or_none = BRACKET_SEP;
to_append_or_none = BRACE_SEP;
}
break;
}

View file

@ -570,7 +570,7 @@ static void find_process(const wchar_t *proc, expand_flags_t flags,
static size_t parse_slice(const wchar_t *in, wchar_t **end_ptr, std::vector<long> &idx,
std::vector<size_t> &source_positions, size_t array_size) {
const long size = (long)array_size;
size_t pos = 1; // skip past the opening square bracket
size_t pos = 1; // skip past the opening square brace
while (1) {
while (iswspace(in[pos]) || (in[pos] == INTERNAL_SEPARATOR)) pos++;
@ -846,39 +846,39 @@ static bool expand_variables(const wcstring &instr, std::vector<completion_t> *o
return true;
}
/// Perform bracket expansion.
static expand_error_t expand_brackets(const wcstring &instr, expand_flags_t flags,
/// 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) {
bool syntax_error = false;
int bracket_count = 0;
int brace_count = 0;
const wchar_t *bracket_begin = NULL, *bracket_end = NULL;
const wchar_t *brace_begin = NULL, *brace_end = NULL;
const wchar_t *last_sep = NULL;
const wchar_t *item_begin;
size_t length_preceding_brackets, length_following_brackets, tot_len;
size_t length_preceding_braces, length_following_braces, tot_len;
const wchar_t *const in = instr.c_str();
// Locate the first non-nested bracket pair.
// Locate the first non-nested brace pair.
for (const wchar_t *pos = in; (*pos) && !syntax_error; pos++) {
switch (*pos) {
case BRACKET_BEGIN: {
if (bracket_count == 0) bracket_begin = pos;
bracket_count++;
case BRACE_BEGIN: {
if (brace_count == 0) brace_begin = pos;
brace_count++;
break;
}
case BRACKET_END: {
bracket_count--;
if (bracket_count < 0) {
case BRACE_END: {
brace_count--;
if (brace_count < 0) {
syntax_error = true;
} else if (bracket_count == 0) {
bracket_end = pos;
} else if (brace_count == 0) {
brace_end = pos;
}
break;
}
case BRACKET_SEP: {
if (bracket_count == 1) last_sep = pos;
case BRACE_SEP: {
if (brace_count == 1) last_sep = pos;
break;
}
default: {
@ -887,72 +887,73 @@ static expand_error_t expand_brackets(const wcstring &instr, expand_flags_t flag
}
}
if (bracket_count > 0) {
if (brace_count > 0) {
if (!(flags & EXPAND_FOR_COMPLETIONS)) {
syntax_error = true;
} else {
// The user hasn't typed an end bracket yet; make one up and append it, then expand
// The user hasn't typed an end brace yet; make one up and append it, then expand
// that.
wcstring mod;
if (last_sep) {
mod.append(in, bracket_begin - in + 1);
mod.append(in, brace_begin - in + 1);
mod.append(last_sep + 1);
mod.push_back(BRACKET_END);
mod.push_back(BRACE_END);
} else {
mod.append(in);
mod.push_back(BRACKET_END);
mod.push_back(BRACE_END);
}
// Note: this code looks very fishy, apparently it has never worked.
return expand_brackets(mod, 1, out, errors);
return expand_braces(mod, 1, out, errors);
}
}
// Expand a literal "{}" to itself because it is useless otherwise,
// and this eases e.g. `find -exec {}`. See #1109.
if (bracket_begin + 1 == bracket_end) {
if (brace_begin + 1 == brace_end) {
wcstring newstr = instr;
newstr.at(bracket_begin - in) = L'{';
newstr.at(bracket_end - in) = L'}';
return expand_brackets(newstr, flags, out, errors);
newstr.at(brace_begin - in) = L'{';
newstr.at(brace_end - in) = L'}';
return expand_braces(newstr, flags, out, errors);
}
if (syntax_error) {
append_syntax_error(errors, SOURCE_LOCATION_UNKNOWN, _(L"Mismatched brackets"));
append_syntax_error(errors, SOURCE_LOCATION_UNKNOWN, _(L"Mismatched braces"));
return EXPAND_ERROR;
}
if (bracket_begin == NULL) {
if (brace_begin == NULL) {
append_completion(out, instr);
return EXPAND_OK;
}
length_preceding_brackets = (bracket_begin - in);
length_following_brackets = wcslen(bracket_end) - 1;
tot_len = length_preceding_brackets + length_following_brackets;
item_begin = bracket_begin + 1;
for (const wchar_t *pos = (bracket_begin + 1); true; pos++) {
if (bracket_count == 0 && ((*pos == BRACKET_SEP) || (pos == bracket_end))) {
length_preceding_braces = (brace_begin - in);
length_following_braces = wcslen(brace_end) - 1;
tot_len = length_preceding_braces + length_following_braces;
item_begin = brace_begin + 1;
for (const wchar_t *pos = (brace_begin + 1); true; pos++) {
if (brace_count == 0 && ((*pos == BRACE_SEP) || (pos == brace_end))) {
assert(pos >= item_begin);
size_t item_len = pos - item_begin;
wcstring whole_item;
whole_item.reserve(tot_len + item_len + 2);
whole_item.append(in, length_preceding_brackets);
whole_item.append(in, length_preceding_braces);
whole_item.append(item_begin, item_len);
whole_item.append(bracket_end + 1);
expand_brackets(whole_item, flags, out, errors);
whole_item.append(brace_end + 1);
debug(0, L"Found brace item: %ls\n", whole_item.c_str());
expand_braces(whole_item, flags, out, errors);
item_begin = pos + 1;
if (pos == bracket_end) break;
if (pos == brace_end) break;
}
if (*pos == BRACKET_BEGIN) {
bracket_count++;
if (*pos == BRACE_BEGIN) {
brace_count++;
}
if (*pos == BRACKET_END) {
bracket_count--;
if (*pos == BRACE_END) {
brace_count--;
}
}
return EXPAND_OK;
@ -1274,9 +1275,9 @@ static expand_error_t expand_stage_variables(const wcstring &input, std::vector<
return EXPAND_OK;
}
static expand_error_t expand_stage_brackets(const wcstring &input, std::vector<completion_t> *out,
static expand_error_t expand_stage_braces(const wcstring &input, std::vector<completion_t> *out,
expand_flags_t flags, parse_error_list_t *errors) {
return expand_brackets(input, flags, out, errors);
return expand_braces(input, flags, out, errors);
}
static expand_error_t expand_stage_home(const wcstring &input,
@ -1393,7 +1394,7 @@ expand_error_t expand_string(const wcstring &input, std::vector<completion_t> *o
// Our expansion stages.
const expand_stage_t stages[] = {expand_stage_cmdsubst, expand_stage_variables,
expand_stage_brackets, expand_stage_home,
expand_stage_braces, expand_stage_home,
expand_stage_wildcards};
// Load up our single initial completion.

View file

@ -65,11 +65,11 @@ enum {
/// Character representing variable expansion into a single element.
VARIABLE_EXPAND_SINGLE,
/// Character representing the start of a bracket expansion.
BRACKET_BEGIN,
BRACE_BEGIN,
/// Character representing the end of a bracket expansion.
BRACKET_END,
BRACE_END,
/// Character representing separation between two bracket elements.
BRACKET_SEP,
BRACE_SEP,
/// Separate subtokens in a token with this character.
INTERNAL_SEPARATOR,
/// Character representing an empty variable expansion. Only used transitively while expanding

View file

@ -122,9 +122,9 @@ bool is_potential_path(const wcstring &potential_path_fragment, const wcstring_l
switch (c) {
case VARIABLE_EXPAND:
case VARIABLE_EXPAND_SINGLE:
case BRACKET_BEGIN:
case BRACKET_END:
case BRACKET_SEP:
case BRACE_BEGIN:
case BRACE_END:
case BRACE_SEP:
case ANY_CHAR:
case ANY_STRING:
case ANY_STRING_RECURSIVE: {

View file

@ -834,14 +834,14 @@ void parse_util_expand_variable_error(const wcstring &token, size_t global_token
wchar_t char_after_dollar = dollar_pos + 1 >= token.size() ? 0 : token.at(dollar_pos + 1);
switch (char_after_dollar) {
case BRACKET_BEGIN:
case BRACE_BEGIN:
case L'{': {
// The BRACKET_BEGIN is for unquoted, the { is for quoted. Anyways we have (possible
// The BRACE_BEGIN is for unquoted, the { is for quoted. Anyways we have (possible
// quoted) ${. See if we have a }, and the stuff in between is variable material. If so,
// report a bracket error. Otherwise just complain about the ${.
bool looks_like_variable = false;
size_t closing_bracket =
token.find(char_after_dollar == L'{' ? L'}' : wchar_t(BRACKET_END), dollar_pos + 2);
token.find(char_after_dollar == L'{' ? L'}' : wchar_t(BRACE_END), dollar_pos + 2);
wcstring var_name;
if (closing_bracket != wcstring::npos) {
size_t var_start = dollar_pos + 2, var_end = closing_bracket;