From 172b5c42010d329e84e498e9347c7f09c9b923e2 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Thu, 23 Nov 2023 08:08:26 +1000 Subject: expr: better error message on nested $() And explain in the manual that it isn't allowed. Fixes: #285 Signed-off-by: Steve Bennett --- jim.c | 8 ++++++++ jim_tcl.txt | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/jim.c b/jim.c index 396c662..3134b83 100644 --- a/jim.c +++ b/jim.c @@ -1281,6 +1281,7 @@ struct JimParserCtx int inquote; /* Parsing a quoted string */ int comment; /* Non zero if the next chars may be a comment. */ struct JimParseMissing missing; /* Details of any missing quotes, etc. */ + const char *errmsg; /* Additional error message, or NULL if none */ }; static int JimParseScript(struct JimParserCtx *pc); @@ -9060,6 +9061,8 @@ static const struct Jim_ExprOperator Jim_ExprOperators[] = { static int JimParseExpression(struct JimParserCtx *pc) { + pc->errmsg = NULL; + while (1) { /* Discard spaces and quoted newline */ while (isspace(UCHAR(*pc->p)) || (*(pc->p) == '\\' && *(pc->p + 1) == '\n')) { @@ -9110,6 +9113,7 @@ singlechar: else { /* Don't allow expr sugar in expressions */ if (pc->tt == JIM_TT_EXPRSUGAR) { + pc->errmsg = "nesting expr in expr is not allowed"; return JIM_ERR; } return JIM_OK; @@ -9257,6 +9261,7 @@ static int JimParseExprOperator(struct JimParserCtx *pc) p++; } if (*p != '(') { + pc->errmsg = "function requires parentheses"; return JIM_ERR; } } @@ -9740,6 +9745,9 @@ static int SetExprFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) if (JimParseExpression(&parser) != JIM_OK) { ScriptTokenListFree(&tokenlist); Jim_SetResultFormatted(interp, "syntax error in expression: \"%#s\"", objPtr); + if (parser.errmsg) { + Jim_AppendStrings(interp, Jim_GetResult(interp), ": ", parser.errmsg, NULL); + } expr = NULL; goto err; } diff --git a/jim_tcl.txt b/jim_tcl.txt index 8684ed9..2c5409e 100644 --- a/jim_tcl.txt +++ b/jim_tcl.txt @@ -2489,6 +2489,23 @@ The following two are identical. set x $(3 * 2 + 1) ---- +However note that the expr shorthand syntax may not be nested in an expression. +This is to prevent the common mistake of writing: + +---- + if {$(1 + 2) == 3) { + ... + } +---- + +rather than: + +---- + if {(1 + 2) == 3) { + ... + } +---- + file ~~~~ +*file* 'option name ?arg\...?'+ -- cgit v1.1