Make parse_t::parse a static method so we don't have to create lots of

useless parse_t objects
This commit is contained in:
ridiculousfish 2013-10-12 01:17:55 -07:00
parent 58447c147f
commit 5490f54d00
5 changed files with 16 additions and 13 deletions

View file

@ -3976,8 +3976,7 @@ int builtin_parse(parser_t &parser, wchar_t **argv)
const wcstring src = str2wcstring(&txt.at(0), txt.size());
parse_node_tree_t parse_tree;
parse_error_list_t errors;
parse_t parser;
bool success = parser.parse(src, parse_flag_none, &parse_tree, &errors, true);
bool success = parse_t::parse(src, parse_flag_none, &parse_tree, &errors, true);
if (! success)
{
stdout_buffer.append(L"Parsing failed:\n");

View file

@ -1845,8 +1845,7 @@ static void test_new_parser_correctness(void)
const parser_test_t *test = &parser_tests[i];
parse_node_tree_t parse_tree;
parse_t parser;
bool success = parser.parse(test->src, parse_flag_none, &parse_tree, NULL);
bool success = parse_t::parse(test->src, parse_flag_none, &parse_tree, NULL);
say(L"%lu / %lu: Parse \"%ls\": %s", i+1, sizeof parser_tests / sizeof *parser_tests, test->src, success ? "yes" : "no");
if (success && ! test->ok)
{
@ -1954,8 +1953,7 @@ static bool test_1_parse_ll2(const wcstring &src, wcstring *out_cmd, wcstring *o
bool result = false;
parse_node_tree_t tree;
parse_t parser;
if (parser.parse(src, parse_flag_none, &tree, NULL))
if (parse_t::parse(src, parse_flag_none, &tree, NULL))
{
/* Get the statement. Should only have one */
const parse_node_tree_t::parse_node_list_t stmt_nodes = tree.find_nodes(tree.at(0), symbol_plain_statement);
@ -2030,8 +2028,7 @@ static void test_new_parser(void)
say(L"Testing new parser");
const wcstring src = L"echo hello world";
parse_node_tree_t parse_tree;
parse_t parser;
bool success = parser.parse(src, parse_flag_none, &parse_tree, NULL);
bool success = parse_t::parse(src, parse_flag_none, &parse_tree, NULL);
if (! success)
{
say(L"Parsing failed");

View file

@ -827,7 +827,7 @@ static inline parse_token_t next_parse_token(tokenizer_t *tok)
return result;
}
bool parse_t::parse(const wcstring &str, parse_tree_flags_t parse_flags, parse_node_tree_t *output, parse_error_list_t *errors, bool log_it)
bool parse_t::parse_internal(const wcstring &str, parse_tree_flags_t parse_flags, parse_node_tree_t *output, parse_error_list_t *errors, bool log_it)
{
this->parser->set_should_generate_error_messages(errors != NULL);
@ -889,6 +889,12 @@ bool parse_t::parse(const wcstring &str, parse_tree_flags_t parse_flags, parse_n
return ! this->parser->has_fatal_error();
}
bool parse_t::parse(const wcstring &str, parse_tree_flags_t flags, parse_node_tree_t *output, parse_error_list_t *errors, bool log_it)
{
parse_t parse;
return parse.parse_internal(str, flags, output, errors, log_it);
}
bool parse_t::parse_1_token(parse_token_type_t token_type, parse_keyword_t keyword, parse_node_tree_t *output, parse_error_list_t *errors)
{
const parse_token_t invalid_token = {token_type_invalid, parse_keyword_none, -1, -1};

View file

@ -141,12 +141,14 @@ class parse_t
{
parse_ll_t * const parser;
bool parse_internal(const wcstring &str, parse_tree_flags_t flags, parse_node_tree_t *output, parse_error_list_t *errors, bool log_it = false);
public:
parse_t();
~parse_t();
/* Parse a string */
bool parse(const wcstring &str, parse_tree_flags_t flags, parse_node_tree_t *output, parse_error_list_t *errors, bool log_it = false);
/* Parse a string all at once */
static bool parse(const wcstring &str, parse_tree_flags_t flags, parse_node_tree_t *output, parse_error_list_t *errors, bool log_it = false);
/* Parse a single token */
bool parse_1_token(parse_token_type_t token, parse_keyword_t keyword, parse_node_tree_t *output, parse_error_list_t *errors);

View file

@ -664,8 +664,7 @@ bool reader_expand_abbreviation_in_command(const wcstring &cmdline, size_t curso
/* Parse this subcmd */
parse_node_tree_t parse_tree;
parse_t parser;
parser.parse(subcmd, parse_flag_continue_after_error | parse_flag_accept_incomplete_tokens, &parse_tree, NULL);
parse_t::parse(subcmd, parse_flag_continue_after_error | parse_flag_accept_incomplete_tokens, &parse_tree, NULL);
/* Look for plain statements where the cursor is at the end of the command */
const parse_node_t *matching_cmd_node = NULL;