@Override public Task awaitTask() throws InterruptedException { for (; ; ) { try { com.github.anba.es6draft.ast.Script parsedScript = read(realm, line); if (parsedScript == null) { return null; } if (parsedScript.getStatements().isEmpty()) { continue; } return new EvalPrintTask(realm, parsedScript); } catch (RuntimeException e) { return new ThrowExceptionTask<>(e); } catch (Error e) { return new ThrowErrorTask<>(e); } } }
/** * REPL: Read * * @param realm the realm instance * @param line the current line * @return the parsed script node or {@coden null} if the end of stream has been reached */ private com.github.anba.es6draft.ast.Script read(Realm realm, int[] line) { StringBuilder sourceBuffer = new StringBuilder(); for (String prompt = PROMPT; ; prompt = "") { String s = console.readLine(prompt); if (s == null) { return null; } sourceBuffer.append(s).append('\n'); String sourceCode = sourceBuffer.toString(); Source source = new Source(Paths.get(".").toAbsolutePath(), "typein", line[0]); try { com.github.anba.es6draft.ast.Script script = parse(realm, source, sourceCode); line[0] += script.getEndLine() - script.getBeginLine(); return script; } catch (ParserEOFException e) { continue; } catch (ParserException e) { throw new ParserExceptionWithSource(e, source, sourceCode); } } }