Adopt tnode_t in detect_errors_in_backgrounded_job

This commit is contained in:
ridiculousfish 2018-01-13 16:24:21 -08:00
parent 539e9bf970
commit d4f9fcc7c7
2 changed files with 33 additions and 35 deletions

View file

@ -281,6 +281,10 @@ class tnode_t {
/// Check whether we're populated.
explicit operator bool() const { return nodeptr != nullptr; }
bool operator==(const tnode_t &rhs) const { return tree == rhs.tree && nodeptr == rhs.nodeptr; }
bool operator!=(const tnode_t &rhs) const { return !(*this == rhs); }
bool has_source() const { return nodeptr && nodeptr->has_source(); }
maybe_t<source_range_t> source_range() const {

View file

@ -1040,30 +1040,28 @@ parser_test_error_bits_t parse_util_detect_errors_in_argument(tnode_t<grammar::a
/// Given that the job given by node should be backgrounded, return true if we detect any errors.
static bool detect_errors_in_backgrounded_job(const parse_node_tree_t &node_tree,
tnode_t<grammar::job> node,
tnode_t<grammar::job> job,
parse_error_list_t *parse_errors) {
auto source_range = job.source_range();
if (!source_range) return false;
bool errored = false;
// Disallow background in the following cases:
// foo & ; and bar
// foo & ; or bar
// if foo & ; end
// while foo & ; end
const parse_node_t *job_parent = node_tree.get_parent(node);
assert(job_parent != NULL);
switch (job_parent->type) {
case symbol_if_clause:
case symbol_while_header: {
assert(node_tree.get_child(*job_parent, 1) == node);
errored = append_syntax_error(parse_errors, node.source_range()->start,
if (job.try_get_parent<grammar::if_clause>()) {
errored = append_syntax_error(parse_errors, source_range->start,
BACKGROUND_IN_CONDITIONAL_ERROR_MSG);
break;
}
case symbol_job_list: {
} else if (job.try_get_parent<grammar::while_header>()) {
errored = append_syntax_error(parse_errors, source_range->start,
BACKGROUND_IN_CONDITIONAL_ERROR_MSG);
} else if (auto job_list = job.try_get_parent<grammar::job_list>()) {
// This isn't very complete, e.g. we don't catch 'foo & ; not and bar'.
// Build the job list and then advance it by one.
tnode_t<grammar::job_list> job_list{&node_tree, job_parent};
auto first_job = job_list.next_in_list<grammar::job>();
assert(first_job.node() == node && "Expected first job to be the node we found");
assert(first_job == job && "Expected first job to be the node we found");
(void)first_job;
// Try getting the next job as a boolean statement.
auto next_job = job_list.next_in_list<grammar::job>();
@ -1080,10 +1078,6 @@ static bool detect_errors_in_backgrounded_job(const parse_node_tree_t &node_tree
BOOL_AFTER_BACKGROUND_ERROR_MSG, L"or");
}
}
break;
}
default:
break;
}
return errored;
}