I'm using Rhino to evaluate js expressions, by putting all the possible variable values in the scope and evaluating an anonymous function. However the expressions are fairly simple and I would like to put only the values used in the expression, for performance.
Code sample:
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects(null);
// Build javascript anonymous function
String script = "(function () {" ;
for (String key : values.keySet()) {
ScriptableObject.putProperty(scope, key, values.get(key));
}
script += "return " + expression + ";})();";
Object result = cx.evaluateString(scope, script, "<cmd>", 1, null);
I want to get all the tokens from the expressions that are variable names.
For instance, if the expression is
(V1ND < 0 ? Math.abs(V1ND) : 0)
it will return V1ND
.