From 05ead8c40dc12a058ff10d9df088dc83b6edbf67 Mon Sep 17 00:00:00 2001 From: Dianne Skoll Date: Thu, 30 Oct 2025 14:30:29 -0400 Subject: [PATCH] More helpful errors when parsing expressions. --- src/err.h | 2 ++ src/expr.c | 14 +++++++++++++- tests/test.cmp | 14 +++++++++----- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/err.h b/src/err.h index 17bdca43..dc32001a 100644 --- a/src/err.h +++ b/src/err.h @@ -137,6 +137,7 @@ #define E_NO_MB 113 #define E_BAD_MB_SEQ 114 #define E_EXPR_NODES_EXCEEDED 115 +#define E_EXPECTING_EOXPR 116 #ifdef MK_GLOBALS #undef EXTERN #define EXTERN @@ -270,6 +271,7 @@ EXTERN char *ErrMsg[] /* E_NO_MB */ "C library does not support multibyte characters", /* E_BAD_MB_SEQ */ "Invalid multibyte sequence", /* E_EXPR_NODES_EXCEEDED */ "Maximum expression complexity exceeded", +/* E_EXPECTING_EOXPR */ "Expecting end-of-expression", } #endif /* MK_GLOBALS */ ; diff --git a/src/expr.c b/src/expr.c index 0d3d5f36..1940eb7a 100644 --- a/src/expr.c +++ b/src/expr.c @@ -2759,9 +2759,20 @@ expr_node *parse_expression(char const **e, int *r, Var *locals) print_expr_tree(node, ErrFp); fprintf(ErrFp, "\n"); } - if (**e && (**e != ']')) { + } + if (**e && (**e != ']')) { + if (DebugFlag & DB_PARSE_EXPR) { fprintf(ErrFp, " Unparsed: %s\n", *e); } + if (*r == OK) { + *r = E_EXPECTING_EOXPR; + } + } + if (*r != OK) { + if (node) { + free_expr_tree(node); + node = NULL; + } } if (!SuppressErrorOutputInCatch) { if (*r == E_EXPECT_COMMA || @@ -2771,6 +2782,7 @@ expr_node *parse_expression(char const **e, int *r, Var *locals) *r == E_2FEW_ARGS || *r == E_PARSE_ERR || *r == E_EOLN || + *r == E_EXPECTING_EOXPR || *r == E_BAD_NUMBER || *r == E_BAD_DATE || *r == E_BAD_TIME || diff --git a/tests/test.cmp b/tests/test.cmp index 33dfd2e9..b24d0a0d 100644 --- a/tests/test.cmp +++ b/tests/test.cmp @@ -5040,8 +5040,9 @@ set a 7 * "Cabbage! " # Should result in errors set pqxya 1+2) -1 + 2 => 3 -../tests/test.rem(1014): Expecting end-of-line +1+2) + ^-- here +../tests/test.rem(1014): Expecting end-of-expression # Should result in an error REM Tue OMIT 2024-01-01 MSG Wookie @@ -16669,7 +16670,7 @@ Expression nodes high-water: 302076 Expression nodes leaked: 0 Parse level high-water: 34 Max expr node evaluations per line: 2001 -Total expression node evaluations: 106467 +Total expression node evaluations: 106464 Test 2 @@ -23905,7 +23906,9 @@ Parsed expression: 1||1 Parsed expression: 2 => 2 Unparsed: , 3 -../tests/expr.rem(15): Expecting end-of-line +, 3 + ^-- here +../tests/expr.rem(15): Expecting end-of-expression Parsed expression: iif(0, "foo", 0, "bar", 1, "blech", 0, "quux", 1, "borhy", "wacka") => (Iif 0 "foo" 0 "bar" 1 "blech" 0 "quux" 1 "borhy" "wacka") iif(0, ?, 0, ?, 1, "blech", ?, ?, ?, ?, ?) => "blech" @@ -24284,7 +24287,7 @@ Expression nodes high-water: 499 Expression nodes leaked: 0 Parse level high-water: 2001 Max expr node evaluations per line: 499 -Total expression node evaluations: 632 +Total expression node evaluations: 631 -stdin-(14): Unmatched PUSH-OMIT-CONTEXT at -stdin-(7) -stdin-(14): Warning: PUSH-OMIT-CONTEXT without matching POP-OMIT-CONTEXT No reminders. @@ -25063,6 +25066,7 @@ TRANSLATE "TZ specified for non-timed reminder" "" TRANSLATE "C library does not support multibyte characters" "" TRANSLATE "Invalid multibyte sequence" "" TRANSLATE "Maximum expression complexity exceeded" "" +TRANSLATE "Expecting end-of-expression" "" # Other Messages TRANSLATE "%s function `%s' defined at %s(%s) does not use its argument" ""