Skip to content

Commit

Permalink
expr: better error message on nested $()
Browse files Browse the repository at this point in the history
And explain in the manual that it isn't allowed.

Fixes: #285

Signed-off-by: Steve Bennett <steveb@workware.net.au>
  • Loading branch information
msteveb committed Feb 2, 2024
1 parent a227bf7 commit 172b5c4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions jim.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -9110,6 +9113,7 @@ static int JimParseExpression(struct JimParserCtx *pc)
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;
Expand Down Expand Up @@ -9257,6 +9261,7 @@ static int JimParseExprOperator(struct JimParserCtx *pc)
p++;
}
if (*p != '(') {
pc->errmsg = "function requires parentheses";
return JIM_ERR;
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
17 changes: 17 additions & 0 deletions jim_tcl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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\...?'+
Expand Down

0 comments on commit 172b5c4

Please sign in to comment.