From 925fe65dd8d51f481217ef1c11647a39ddf56351 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 8 Dec 2013 16:22:06 -0800 Subject: [PATCH] Remove the indentation part of parser_t::test(). Rename it to detect_errors(). --- builtin_complete.cpp | 4 +- fish_tests.cpp | 24 +++++----- parser.cpp | 103 ++----------------------------------------- parser.h | 2 +- reader.cpp | 15 ++----- 5 files changed, 23 insertions(+), 125 deletions(-) diff --git a/builtin_complete.cpp b/builtin_complete.cpp index 4bfab1b7c..14b3a4b74 100644 --- a/builtin_complete.cpp +++ b/builtin_complete.cpp @@ -497,14 +497,14 @@ static int builtin_complete(parser_t &parser, wchar_t **argv) { if (condition && wcslen(condition)) { - if (parser.test(condition)) + if (parser.detect_errors(condition)) { append_format(stderr_buffer, L"%ls: Condition '%ls' contained a syntax error\n", argv[0], condition); - parser.test(condition, NULL, &stderr_buffer, argv[0]); + parser.detect_errors(condition, &stderr_buffer, argv[0]); res = true; } diff --git a/fish_tests.cpp b/fish_tests.cpp index 5e488aae6..16f657fc3 100644 --- a/fish_tests.cpp +++ b/fish_tests.cpp @@ -585,52 +585,52 @@ static void test_parser() parser_t parser(PARSER_TYPE_GENERAL, true); say(L"Testing null input to parser"); - if (!parser.test(NULL)) + if (!parser.detect_errors(NULL)) { - err(L"Null input to parser.test undetected"); + err(L"Null input to parser.detect_errors undetected"); } say(L"Testing block nesting"); - if (!parser.test(L"if; end")) + if (!parser.detect_errors(L"if; end")) { err(L"Incomplete if statement undetected"); } - if (!parser.test(L"if test; echo")) + if (!parser.detect_errors(L"if test; echo")) { err(L"Missing end undetected"); } - if (!parser.test(L"if test; end; end")) + if (!parser.detect_errors(L"if test; end; end")) { err(L"Unbalanced end undetected"); } say(L"Testing detection of invalid use of builtin commands"); - if (!parser.test(L"case foo")) + if (!parser.detect_errors(L"case foo")) { err(L"'case' command outside of block context undetected"); } - if (!parser.test(L"switch ggg; if true; case foo;end;end")) + if (!parser.detect_errors(L"switch ggg; if true; case foo;end;end")) { err(L"'case' command outside of switch block context undetected"); } - if (!parser.test(L"else")) + if (!parser.detect_errors(L"else")) { err(L"'else' command outside of conditional block context undetected"); } - if (!parser.test(L"else if")) + if (!parser.detect_errors(L"else if")) { err(L"'else if' command outside of conditional block context undetected"); } - if (!parser.test(L"if false; else if; end")) + if (!parser.detect_errors(L"if false; else if; end")) { err(L"'else if' missing command undetected"); } - if (!parser.test(L"break")) + if (!parser.detect_errors(L"break")) { err(L"'break' command outside of loop block context undetected"); } - if (!parser.test(L"exec ls|less") || !parser.test(L"echo|return")) + if (!parser.detect_errors(L"exec ls|less") || !parser.detect_errors(L"echo|return")) { err(L"Invalid pipe command undetected"); } diff --git a/parser.cpp b/parser.cpp index f9282a242..02a4dffc9 100644 --- a/parser.cpp +++ b/parser.cpp @@ -2771,7 +2771,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 ); - err |= parser_t::test(subst, 0, out, prefix); + err |= parser_t::detect_errors(subst, out, prefix); free(subst); free(arg_cpy); @@ -2906,12 +2906,9 @@ struct block_info_t { int position; //tokenizer position block_type_t type; //type of the block - int indentation; //indentation associated with the block - - bool has_had_case; //if we are a switch, whether we've encountered a case }; -parser_test_error_bits_t parser_t::test(const wchar_t *buff, int *block_level, wcstring *out, const wchar_t *prefix) +parser_test_error_bits_t parser_t::detect_errors(const wchar_t *buff, wcstring *out, const wchar_t *prefix) { ASSERT_IS_MAIN_THREAD(); @@ -2923,9 +2920,8 @@ parser_test_error_bits_t parser_t::test(const wchar_t *buff, int *block_level, w int err=0; int unfinished = 0; - // These are very nearly stacks, but sometimes we have to inspect non-top elements (e.g. return) + // This is very nearly a stack, but sometimes we have to inspect non-top elements (e.g. return) std::vector block_infos; - int indentation_sum = 0; //sum of indentation in block_infos /* Set to 1 if the current command is inside a pipeline @@ -2958,16 +2954,6 @@ parser_test_error_bits_t parser_t::test(const wchar_t *buff, int *block_level, w CHECK(buff, 1); - if (block_level) - { - size_t len = wcslen(buff); - for (size_t i=0; i tokenizer_push(¤t_tokenizer, &tok); @@ -3059,53 +3045,18 @@ parser_test_error_bits_t parser_t::test(const wchar_t *buff, int *block_level, w } else { - indentation_sum -= block_infos.back().indentation; block_infos.pop_back(); } } - /* - Store the block level. This needs to be done - _after_ checking for end commands, but _before_ - checking for block opening commands. - */ - if (block_level != NULL) - { - int indentation_adjust = 0; - if (command == L"else") - { - // if or else if goes back - indentation_adjust = -1; - } - else if (command == L"case") - { - if (! block_infos.empty() && block_infos.back().type == SWITCH) - { - // mark that we've encountered a case, and increase the indentation - // by doing this now, we avoid overly indenting the first case as the user types it - if (! block_infos.back().has_had_case) - { - block_infos.back().has_had_case = true; - block_infos.back().indentation += 1; - indentation_sum += 1; - } - // unindent this case - indentation_adjust = -1; - } - } - - block_level[tok_get_pos(&tok)] = indentation_sum + indentation_adjust; - } - /* Handle block commands */ if (parser_keywords_is_block(command)) { - struct block_info_t info = {current_tokenizer_pos, parser_get_block_type(command), 1 /* indent */}; + struct block_info_t info = {current_tokenizer_pos, parser_get_block_type(command)}; block_infos.push_back(info); - indentation_sum += info.indentation; tok_next(&tok); tok_set_pos(&tok, mark); } @@ -3651,52 +3602,6 @@ parser_test_error_bits_t parser_t::test(const wchar_t *buff, int *block_level, w } - /* - Fill in the unset block_level entries. Until now, only places - where the block level _changed_ have been filled out. This fills - in the rest. - */ - - if (block_level) - { - int last_level = 0; - size_t i, len = wcslen(buff); - for (i=0; i= 0) - { - last_level = block_level[i]; - /* - Make all whitespace before a token have the new - level. This avoid using the wrong indentation level - if a new line starts with whitespace. - */ - size_t prev_char_idx = i; - while (prev_char_idx--) - { - if (!wcschr(L" \n\t\r", buff[prev_char_idx])) - break; - block_level[prev_char_idx] = last_level; - } - } - block_level[i] = last_level; - } - - /* - Make all trailing whitespace have the block level that the - validator had at exit. This makes sure a new line is - correctly indented even if it is empty. - */ - int last_indent = block_infos.empty() ? 0 : block_infos.back().indentation; - size_t suffix_idx = len; - while (suffix_idx--) - { - if (!wcschr(L" \n\t\r", buff[suffix_idx])) - break; - block_level[suffix_idx] = last_indent; - } - } - /* Calculate exit status */ diff --git a/parser.h b/parser.h index 076baceff..39973665d 100644 --- a/parser.h +++ b/parser.h @@ -487,7 +487,7 @@ public: \param out if non-null, any errors in the command will be filled out into this buffer \param prefix the prefix string to prepend to each error message written to the \c out buffer */ - parser_test_error_bits_t test(const wchar_t * buff, int *block_level = NULL, wcstring *out = NULL, const wchar_t *prefix = NULL); + parser_test_error_bits_t detect_errors(const wchar_t * buff, wcstring *out = NULL, const wchar_t *prefix = NULL); /** Test if the specified string can be parsed as an argument list, diff --git a/reader.cpp b/reader.cpp index ac0e52f51..df7f070e5 100644 --- a/reader.cpp +++ b/reader.cpp @@ -519,14 +519,7 @@ wcstring combine_command_and_autosuggestion(const wcstring &cmdline, const wcstr static void reader_repaint() { // Update the indentation - if (0) - { - parser_t::principal_parser().test(data->command_line.c_str(), &data->indents[0]); - } - else - { - data->indents = parse_util_compute_indents(data->command_line); - } + data->indents = parse_util_compute_indents(data->command_line); // Combine the command and autosuggestion into one string wcstring full_line = combine_command_and_autosuggestion(data->command_line, data->autosuggestion); @@ -2479,7 +2472,7 @@ void reader_run_command(parser_t &parser, const wcstring &cmd) int reader_shell_test(const wchar_t *b) { - int res = parser_t::principal_parser().test(b); + int res = parser_t::principal_parser().detect_errors(b); if (res & PARSER_TEST_ERROR) { @@ -2499,7 +2492,7 @@ int reader_shell_test(const wchar_t *b) 0); - parser_t::principal_parser().test(b, NULL, &sb, L"fish"); + parser_t::principal_parser().detect_errors(b, &sb, L"fish"); fwprintf(stderr, L"%ls", sb.c_str()); } return res; @@ -3911,7 +3904,7 @@ static int read_ni(int fd, const io_chain_t &io) } wcstring sb; - if (! parser.test(str.c_str(), 0, &sb, L"fish")) + if (! parser.detect_errors(str.c_str(), &sb, L"fish")) { parser.eval(str, io, TOP); }