jsr223 + writing a script interpreter

OK. ScriptEngine.eval(String string) evaluates a string in its entirety, and ScriptEngine.eval(Reader reader) evaluates the input from a Reader in its entirety.

So if I have a file, I can open a FileInputStream, wrap a Reader around it, and call scriptEngine.eval(reader).

If I have a complete statement as a string, I can call scriptEngine.eval(string).

What do I do if I need to implement an interactive interpreter? I have a user who is interactively typing in a multiline statement, e.g.

 function f() {
     return 3;
 }

If I read the input line by line, and use the String form of eval(), I'll end up passing it incomplete statements, e.g. function f() { and get an error.

If I pass in a Reader, the ScriptEngine will wait forever until the input is complete, and it's not interactive.

Help!


Just to clarify: the problem here is that I can only pass ScriptEngine.eval() complete statements, and as the customer of ScriptEngine, I don't know when an input line is complete without some help from the ScriptEngine itself.


Rhino's interactive shell uses Rhino's Context.stringIsCompilableUnit() (see LXR for usage and implementation).

5
задан Jason S 7 April 2011 в 20:55
поделиться