From d90d0ee08e9a2eae004604d0776090e213412dfc Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Sun, 25 Feb 2018 21:41:38 +0100 Subject: [PATCH] [tinyexpr] Let specific errors take precedence over generic ones Fixes the case where `sin()` reported the generic "bogus" error instead of "too few arguments". Also rename the constant to "TE_ERROR_UNKNOWN". --- src/builtin_math.cpp | 2 +- src/tinyexpr.c | 25 ++++++++++++------------- src/tinyexpr.h | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/builtin_math.cpp b/src/builtin_math.cpp index fee462418..eb323294a 100644 --- a/src/builtin_math.cpp +++ b/src/builtin_math.cpp @@ -124,7 +124,7 @@ wcstring math_describe_error(te_error_t& error) { case TE_ERROR_TOO_FEW_ARGS: return _(L"Too few arguments"); case TE_ERROR_TOO_MANY_ARGS: return _(L"Too many arguments"); case TE_ERROR_MISSING_OPERATOR: return _(L"Missing operator"); - case TE_ERROR_BOGUS: return _(L"Expression is bogus"); + case TE_ERROR_UNKNOWN: return _(L"Expression is bogus"); default: return L""; } } diff --git a/src/tinyexpr.c b/src/tinyexpr.c index 74aa9c47a..b91717706 100755 --- a/src/tinyexpr.c +++ b/src/tinyexpr.c @@ -239,8 +239,9 @@ void next_token(state *s) { s->function = var->address; break; } - } else if (s->type != TOK_ERROR) { - // TODO: Better error - "Not a variable"? + } else if (s->type != TOK_ERROR + || s->error == TE_ERROR_UNKNOWN) { + // Our error is more specific, so it takes precedence. s->type = TOK_ERROR; s->error = TE_ERROR_UNKNOWN_VARIABLE; } @@ -295,8 +296,8 @@ static te_expr *base(state *s) { next_token(s); if (s->type == TOK_CLOSE) { next_token(s); - } else if (s->type != TOK_ERROR) { - // TODO: Better error - "Missing closing parenthesis"? + } else if (s->type != TOK_ERROR + || s->error == TE_ERROR_UNKNOWN) { s->type = TOK_ERROR; s->error = TE_ERROR_MISSING_CLOSING_PAREN; } @@ -323,15 +324,14 @@ static te_expr *base(state *s) { } if(s->type == TOK_CLOSE && i == arity - 1) { next_token(s); - } else if (s->type != TOK_ERROR) { - // TODO: Either a closing paren was needed, - // or too few arguments were given? + } else if (s->type != TOK_ERROR + || s->error == TE_ERROR_UNKNOWN) { s->type = TOK_ERROR; s->error = i < arity ? TE_ERROR_TOO_FEW_ARGS : TE_ERROR_TOO_MANY_ARGS; } - } else if (s->type != TOK_ERROR) { - // TODO: Better error - "Expected opening parenthesis"? + } else if (s->type != TOK_ERROR + || s->error == TE_ERROR_UNKNOWN) { s->type = TOK_ERROR; s->error = TE_ERROR_MISSING_OPENING_PAREN; } @@ -343,8 +343,8 @@ static te_expr *base(state *s) { ret = expr(s); if (s->type == TOK_CLOSE) { next_token(s); - } else if (s->type != TOK_ERROR) { - // TODO: Error - missing closing paren? + } else if (s->type != TOK_ERROR + || s->error == TE_ERROR_UNKNOWN) { s->type = TOK_ERROR; s->error = TE_ERROR_MISSING_CLOSING_PAREN; } @@ -352,9 +352,8 @@ static te_expr *base(state *s) { default: ret = new_expr(0, 0); - // TODO: Error - expression is bogus? s->type = TOK_ERROR; - s->error = TE_ERROR_BOGUS; + s->error = TE_ERROR_UNKNOWN; ret->value = NAN; break; } diff --git a/src/tinyexpr.h b/src/tinyexpr.h index fc46ddc74..92319d595 100644 --- a/src/tinyexpr.h +++ b/src/tinyexpr.h @@ -41,7 +41,7 @@ extern "C" { TE_ERROR_TOO_FEW_ARGS = 4, TE_ERROR_TOO_MANY_ARGS = 5, TE_ERROR_MISSING_OPERATOR = 6, - TE_ERROR_BOGUS = 7 + TE_ERROR_UNKNOWN = 7 }; typedef int te_error_type_t;