From 1305c02579a3b4976ddbd964cf174b71a36b8e41 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 16 Mar 2014 22:06:32 -0700 Subject: [PATCH] Rewrite parser_t::current_line() to respect new parser --- parse_execution.cpp | 14 ++++ parse_execution.h | 6 ++ parser.cpp | 151 +++++++++----------------------------------- 3 files changed, 50 insertions(+), 121 deletions(-) diff --git a/parse_execution.cpp b/parse_execution.cpp index 0dfb85a0f..127309a8b 100644 --- a/parse_execution.cpp +++ b/parse_execution.cpp @@ -1590,3 +1590,17 @@ int parse_execution_context_t::get_current_line_number() } return line_number; } + +int parse_execution_context_t::get_current_source_offset() const +{ + int result = -1; + if (executing_node_idx != NODE_OFFSET_INVALID) + { + const parse_node_t &node = tree.at(executing_node_idx); + if (node.has_source()) + { + result = static_cast(node.source_start); + } + } + return result; +} diff --git a/parse_execution.h b/parse_execution.h index be04c1b68..cb2ff65f9 100644 --- a/parse_execution.h +++ b/parse_execution.h @@ -119,6 +119,12 @@ public: /* Returns the current line number, indexed from 1. Not const since it touches cached_lineno_offset */ int get_current_line_number(); + /* Returns the source offset, or -1 */ + int get_current_source_offset() const; + + /* Returns the source string */ + const wcstring &get_source() const { return src; } + /* Start executing at the given node offset. Returns 0 if there was no error, 1 if there was an error */ parse_execution_result_t eval_node_at_offset(node_offset_t offset, const block_t *associated_block, const io_chain_t &io); diff --git a/parser.cpp b/parser.cpp index 25f2619e9..b361fe494 100644 --- a/parser.cpp +++ b/parser.cpp @@ -823,149 +823,58 @@ const wchar_t *parser_t::current_filename() const return NULL; } -/** - Calculates the on-screen width of the specified substring of the - specified string. This function takes into account the width and - alignment of the tab character, but other wise behaves like - repeatedly calling wcwidth. -*/ -static int printed_width(const wchar_t *str, int len) -{ - int res=0; - int i; - - CHECK(str, 0); - - for (i=0; str[i] && iget_current_source_offset(); + if (source_offset < 0) + { + return wcstring(); } - file = parser_t::current_filename(); - whole_str = tok_string(current_tokenizer); - line = whole_str; + const int lineno = this->get_lineno(); + const wchar_t *file = this->current_filename(); - if (!line) - return L""; + wcstring prefix; - - /* - Calculate line number, line offset, etc. - */ - for (i=0; i= 0); + parse_error_t empty_error = {}; + empty_error.source_start = source_offset; + + wcstring line_info = empty_error.describe_with_prefix(context->get_source(), prefix, skip_caret); + if (! line_info.empty()) { - offset=0; + line_info.push_back(L'\n'); } -// debug( 1, L"Current pos %d, line pos %d, file_length %d, is_interactive %d, offset %d\n", current_tokenizer_pos, current_line_pos, wcslen(whole_str), is_interactive, offset); - /* - Skip printing character position if we are in interactive mode - and the error was on the first character of the line. - */ - if (!get_is_interactive() || is_function() || (current_line_width!=0)) - { - // Workaround since it seems impossible to print 0 copies of a character using %*lc - if (offset+current_line_width) - { - append_format(lineinfo, - L"%ls\n%*lc^\n", - line, - offset+current_line_width, - L' '); - } - else - { - append_format(lineinfo, - L"%ls\n^\n", - line); - } - } - - free((void *)line); - parser_t::stack_trace(0, lineinfo); - - return lineinfo; + parser_t::stack_trace(0, line_info); + return line_info; }