Checking scripts for syntax errors



Hi,


We have an application that executes Tcl scripts, some of them created
by users. I tried to make a C code that checks these scripts to detect
as many errors as possible, but I have some problems. Can someone help
me with informations about how to find errors like the ones described
below?

My code looks like:
/* compile Tcl script */
CompileEnv compEnv;
TclInitCompileEnv(interp, &compEnv, szTclCode, strlen(szTclCode));
int result = TclCompileScript(interp, szTclCode, strlen(szTclCode), 0,
&compEnv);

/* if compilation failed, we have an error message */
if (result != TCL_OK)
### error with interp->errorLine, Tcl_GetStringResult(interp)


It seems this method does not detect errors in procedures (as in
example 1) and several errors while matching brackets (as in example
3).


Example 1:

proc myProc { val } {
if {val < 1}
set x 0
}
}
myProc 3

no error is detected (should be "Variable references require preceding
$" in expression "val < 1").


Example 2:

if {$val < 1}
set x 0
}

the error is:
wrong # args: no script following "{$val < 1}" argument


Example 3:

{
if {$val < 1}
set x 0
}

no error is detected, but when the script is evaluated, due to the
extra bracket on line 1, the error is:
invalid command name "
if {$val < 1}
set x 0
"


Another version for the C code, using Tcl_ParseCommand (but it works
even worse - it didn't detect the error in Example 2 ):

while (1)
{
int nRet = Tcl_ParseCommand(interp, szUnparsed, -1, 0, &parse);
if (nRet == TCL_OK)
{
szUnparsed = (char*)(parse.commandStart + parse.commandSize);
if (!*szUnparsed)
break;
}
}
else
###error with Tcl_GetStringResult(interp), szUnparsed
}



Thank you,
Arthur

.