The following class members are obsolete. They are provided to keep old source code working. We strongly advise against using them in new code.
bool | canEvaluate ( const QString & program ) const (obsolete) |
Returns true if program can be evaluated; i.e. the code is sufficient to determine whether it appears to be a syntactically correct program, or contains a syntax error.
This function returns false if program is incomplete; i.e. the input is syntactically correct up to the point where the input is terminated.
Note that this function only does a static check of program; e.g. it does not check whether references to variables are valid, and so on.
A typical usage of canEvaluate() is to implement an interactive interpreter for QtScript. The user is repeatedly queried for individual lines of code; the lines are concatened internally, and only when canEvaluate() returns true for the resulting program is it passed to evaluate().
The following are some examples to illustrate the behavior of canEvaluate(). (Note that all example inputs are assumed to have an explicit newline as their last character, since otherwise the QtScript parser would automatically insert a semi-colon character at the end of the input, and this could cause canEvaluate() to produce different results.)
Given the input
if (hello && world)
print("hello world");
canEvaluate() will return true, since the program appears to be complete.
Given the input
if (hello &&
canEvaluate() will return false, since the if-statement is not complete, but is syntactically correct so far.
Given the input
0 = 0
canEvaluate() will return true, but evaluate() will throw a SyntaxError given the same input.
Given the input
./test.js
canEvaluate() will return true, even though the code is clearly not syntactically valid QtScript code. evaluate() will throw a SyntaxError when this code is evaluated.
Given the input
foo["bar"]
canEvaluate() will return true, but evaluate() will throw a ReferenceError if foo is not defined in the script environment.
See also evaluate() and checkSyntax().