From c284c4ca99f57f113c07adf7461416fffe4e0faf Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Thu, 8 Sep 2022 20:41:02 +0200 Subject: [PATCH] Add length also for too-many/few-args error --- src/tinyexpr.cpp | 11 ++++++++++- tests/checks/math.fish | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/tinyexpr.cpp b/src/tinyexpr.cpp index 3feb4638f..baaf84d80 100644 --- a/src/tinyexpr.cpp +++ b/src/tinyexpr.cpp @@ -433,7 +433,9 @@ double state::base() { std::vector parameters; int i; - for (i = 0; arity < 0 || i < arity; i++) { + const wchar_t *first_err = nullptr; + for (i = 0; ; i++) { + if (i == arity) first_err = next_; parameters.push_back(expr()); if (type_ != TOK_SEP) { break; @@ -461,6 +463,13 @@ double state::base() { // a closing parenthesis should be more obvious. error_ = i < arity ? TE_ERROR_TOO_FEW_ARGS : TE_ERROR_TOO_MANY_ARGS; type_ = TOK_ERROR; + if (first_err) { + errpos_ = first_err; + errlen_ = next_ - first_err; + // TODO: Rationalize where we put the cursor exactly. + // If we have a closing paren it's on it, if we don't it's before the number. + if (type_ != TOK_CLOSE) errlen_++; + } } break; } diff --git a/tests/checks/math.fish b/tests/checks/math.fish index f5b97f1d4..788dbece8 100644 --- a/tests/checks/math.fish +++ b/tests/checks/math.fish @@ -254,6 +254,11 @@ math pow sin 3, 5 # CHECKERR: 'pow sin 3, 5' # CHECKERR: ^ +math pow sin 3, 5 + 2 +# CHECKERR: math: Error: Too many arguments +# CHECKERR: 'pow sin 3, 5 + 2' +# CHECKERR: ^~~~^ + math sin pow 3, 5 # CHECK: -0.890009