mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-06-15 09:21:15 -03:00
Initial work on backtrace support with new parser
This commit is contained in:
183
parser.cpp
183
parser.cpp
@@ -801,7 +801,7 @@ void parser_t::eval_args(const wchar_t *line, std::vector<completion_t> &args)
|
||||
proc_pop_interactive();
|
||||
}
|
||||
|
||||
void parser_t::stack_trace(block_t *b, wcstring &buff)
|
||||
void parser_t::stack_trace(block_t *b, wcstring &buff) const
|
||||
{
|
||||
/*
|
||||
Check if we should end the recursion
|
||||
@@ -844,7 +844,7 @@ void parser_t::stack_trace(block_t *b, wcstring &buff)
|
||||
{
|
||||
const source_block_t *sb = static_cast<const source_block_t*>(b);
|
||||
const wchar_t *source_dest = sb->source_file;
|
||||
append_format(buff, _(L"in . (source) call of file '%ls',\n"), source_dest);
|
||||
append_format(buff, _(L"from sourcing file '%ls',\n"), source_dest);
|
||||
break;
|
||||
}
|
||||
case FUNCTION_CALL:
|
||||
@@ -2762,7 +2762,7 @@ int parser_t::parser_test_argument(const wchar_t *arg, wcstring *out, const wcha
|
||||
// debug( 1, L"%ls -> %ls %ls", arg_cpy, subst, tmp.buff );
|
||||
|
||||
parse_error_list_t errors;
|
||||
err |= parser_t::detect_errors(subst, &errors);
|
||||
err |= parse_util_detect_errors(subst, &errors);
|
||||
if (out && ! errors.empty())
|
||||
{
|
||||
out->append(parse_errors_description(errors, subst, prefix));
|
||||
@@ -2890,21 +2890,6 @@ int parser_t::test_args(const wchar_t * buff, wcstring *out, const wchar_t *pre
|
||||
return err;
|
||||
}
|
||||
|
||||
// Check if the first argument under the given node is --help
|
||||
static bool first_argument_is_help(const parse_node_tree_t &node_tree, const parse_node_t &node, const wcstring &src)
|
||||
{
|
||||
bool is_help = false;
|
||||
const parse_node_tree_t::parse_node_list_t arg_nodes = node_tree.find_nodes(node, symbol_argument, 1);
|
||||
if (! arg_nodes.empty())
|
||||
{
|
||||
// Check the first argument only
|
||||
const parse_node_t &arg = *arg_nodes.at(0);
|
||||
const wcstring first_arg_src = arg.get_source(src);
|
||||
is_help = parser_t::is_help(first_arg_src.c_str(), 3);
|
||||
}
|
||||
return is_help;
|
||||
}
|
||||
|
||||
// helper type used in parser::test below
|
||||
struct block_info_t
|
||||
{
|
||||
@@ -2929,153 +2914,31 @@ static bool append_syntax_error(parse_error_list_t *errors, const parse_node_t &
|
||||
return true;
|
||||
}
|
||||
|
||||
parser_test_error_bits_t parser_t::detect_errors(const wcstring &buff_src, parse_error_list_t *out_errors, const wchar_t *prefix)
|
||||
void parser_t::get_backtrace(const wcstring &src, const parse_error_list_t &errors, wcstring *output) const
|
||||
{
|
||||
ASSERT_IS_MAIN_THREAD();
|
||||
|
||||
parse_node_tree_t node_tree;
|
||||
parse_error_list_t parse_errors;
|
||||
|
||||
// Whether we encountered a parse error
|
||||
bool errored = false;
|
||||
|
||||
// Whether we encountered an unclosed block
|
||||
// We detect this via an 'end_command' block without source
|
||||
bool has_unclosed_block = false;
|
||||
|
||||
// Parse the input string into a parse tree
|
||||
// Some errors are detected here
|
||||
bool parsed = parse_t::parse(buff_src, 0, &node_tree, &parse_errors);
|
||||
if (! parsed)
|
||||
assert(output != NULL);
|
||||
if (! errors.empty())
|
||||
{
|
||||
errored = true;
|
||||
}
|
||||
|
||||
// Expand all commands
|
||||
// Verify 'or' and 'and' not used inside pipelines
|
||||
// Verify pipes via parser_is_pipe_forbidden
|
||||
// Verify return only within a function
|
||||
|
||||
if (! errored)
|
||||
{
|
||||
const size_t node_tree_size = node_tree.size();
|
||||
for (size_t i=0; i < node_tree_size; i++)
|
||||
const parse_error_t err = errors.at(0);
|
||||
output->append(err.describe(src));
|
||||
output->push_back(L'\n');
|
||||
|
||||
// Determine which line we're on
|
||||
assert(err.source_start <= src.size());
|
||||
size_t which_line = 1 + std::count(src.begin(), src.begin() + err.source_start, L'\n');
|
||||
|
||||
const wchar_t *filename = this->current_filename();
|
||||
if (filename)
|
||||
{
|
||||
const parse_node_t &node = node_tree.at(i);
|
||||
if (node.type == symbol_end_command && ! node.has_source())
|
||||
{
|
||||
// an 'end' without source is an unclosed block
|
||||
has_unclosed_block = true;
|
||||
}
|
||||
else if (node.type == symbol_plain_statement)
|
||||
{
|
||||
wcstring command;
|
||||
if (node_tree.command_for_plain_statement(node, buff_src, &command))
|
||||
{
|
||||
// Check that we can expand the command
|
||||
if (! expand_one(command, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_VARIABLES | EXPAND_SKIP_JOBS))
|
||||
{
|
||||
errored = append_syntax_error(&parse_errors, node, ILLEGAL_CMD_ERR_MSG, command.c_str());
|
||||
}
|
||||
|
||||
// Check that pipes are sound
|
||||
bool is_boolean_command = contains(command, L"or", L"and");
|
||||
bool is_pipe_forbidden = parser_is_pipe_forbidden(command);
|
||||
if (! errored && (is_boolean_command || is_pipe_forbidden))
|
||||
{
|
||||
// 'or' and 'and' can be first in the pipeline. forbidden commands cannot be in a pipeline at all
|
||||
if (node_tree.plain_statement_is_in_pipeline(node, is_pipe_forbidden))
|
||||
{
|
||||
errored = append_syntax_error(&parse_errors, node, EXEC_ERR_MSG);
|
||||
}
|
||||
}
|
||||
|
||||
// Check that we don't return from outside a function
|
||||
// But we allow it if it's 'return --help'
|
||||
if (! errored && command == L"return")
|
||||
{
|
||||
const parse_node_t *ancestor = &node;
|
||||
bool found_function = false;
|
||||
while (ancestor != NULL)
|
||||
{
|
||||
const parse_node_t *possible_function_header = node_tree.header_node_for_block_statement(*ancestor);
|
||||
if (possible_function_header != NULL && possible_function_header->type == symbol_function_header)
|
||||
{
|
||||
found_function = true;
|
||||
break;
|
||||
}
|
||||
ancestor = node_tree.get_parent(*ancestor);
|
||||
|
||||
}
|
||||
if (! found_function && ! first_argument_is_help(node_tree, node, buff_src))
|
||||
{
|
||||
errored = append_syntax_error(&parse_errors, node, INVALID_RETURN_ERR_MSG);
|
||||
}
|
||||
}
|
||||
|
||||
// Check that we don't return from outside a function
|
||||
if (! errored && (command == L"break" || command == L"continue"))
|
||||
{
|
||||
// Walk up until we hit a 'for' or 'while' loop. If we hit a function first, stop the search; we can't break an outer loop from inside a function.
|
||||
// This is a little funny because we can't tell if it's a 'for' or 'while' loop from the ancestor alone; we need the header. That is, we hit a block_statement, and have to check its header.
|
||||
bool found_loop = false, end_search = false;
|
||||
const parse_node_t *ancestor = &node;
|
||||
while (ancestor != NULL && ! end_search)
|
||||
{
|
||||
const parse_node_t *loop_or_function_header = node_tree.header_node_for_block_statement(*ancestor);
|
||||
if (loop_or_function_header != NULL)
|
||||
{
|
||||
switch (loop_or_function_header->type)
|
||||
{
|
||||
case symbol_while_header:
|
||||
case symbol_for_header:
|
||||
// this is a loop header, so we can break or continue
|
||||
found_loop = true;
|
||||
end_search = true;
|
||||
break;
|
||||
|
||||
case symbol_function_header:
|
||||
// this is a function header, so we cannot break or continue. We stop our search here.
|
||||
found_loop = false;
|
||||
end_search = true;
|
||||
break;
|
||||
|
||||
default:
|
||||
// most likely begin / end style block, which makes no difference
|
||||
break;
|
||||
}
|
||||
}
|
||||
ancestor = node_tree.get_parent(*ancestor);
|
||||
}
|
||||
|
||||
if (! found_loop && ! first_argument_is_help(node_tree, node, buff_src))
|
||||
{
|
||||
errored = append_syntax_error(&parse_errors, node, INVALID_LOOP_ERR_MSG);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
append_format(*output, _(L"line %lu of '%ls'\n"), which_line, filename);
|
||||
}
|
||||
else
|
||||
{
|
||||
append_format(*output, L"%ls: ", _(L"Standard input"), which_line);
|
||||
}
|
||||
|
||||
this->stack_trace(current_block, *output);
|
||||
}
|
||||
|
||||
parser_test_error_bits_t res = 0;
|
||||
|
||||
if (errored)
|
||||
res |= PARSER_TEST_ERROR;
|
||||
|
||||
if (has_unclosed_block)
|
||||
res |= PARSER_TEST_INCOMPLETE;
|
||||
|
||||
if (out_errors)
|
||||
{
|
||||
out_errors->swap(parse_errors);
|
||||
}
|
||||
|
||||
error_code=0;
|
||||
|
||||
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
parser_test_error_bits_t parser_t::detect_errors2(const wchar_t *buff, wcstring *out, const wchar_t *prefix)
|
||||
|
||||
Reference in New Issue
Block a user