aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2023-11-23 08:08:26 +1000
committerSteve Bennett <steveb@workware.net.au>2024-02-02 11:22:30 +1000
commit172b5c42010d329e84e498e9347c7f09c9b923e2 (patch)
treee0e81908c5c84ece8ef581e7037114e3ae4fd380
parenta227bf74d2fc5013fed3d3de025086c586bc950c (diff)
downloadjimtcl-172b5c42010d329e84e498e9347c7f09c9b923e2.zip
jimtcl-172b5c42010d329e84e498e9347c7f09c9b923e2.tar.gz
jimtcl-172b5c42010d329e84e498e9347c7f09c9b923e2.tar.bz2
expr: better error message on nested $()
And explain in the manual that it isn't allowed. Fixes: #285 Signed-off-by: Steve Bennett <steveb@workware.net.au>
-rw-r--r--jim.c8
-rw-r--r--jim_tcl.txt17
2 files changed, 25 insertions, 0 deletions
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\...?'+