Search in sources :

Example 1 with scala

use of scala in project zeppelin by apache.

the class SparkInterpreter method interpretInput.

public InterpreterResult interpretInput(String[] lines, InterpreterContext context) {
    SparkEnv.set(env);
    String[] linesToRun = new String[lines.length];
    for (int i = 0; i < lines.length; i++) {
        linesToRun[i] = lines[i];
    }
    Console.setOut(context.out);
    out.setInterpreterOutput(context.out);
    context.out.clear();
    Code r = null;
    String incomplete = "";
    boolean inComment = false;
    for (int l = 0; l < linesToRun.length; l++) {
        String s = linesToRun[l];
        // check if next line starts with "." (but not ".." or "./") it is treated as an invocation
        if (l + 1 < linesToRun.length) {
            String nextLine = linesToRun[l + 1].trim();
            boolean continuation = false;
            if (nextLine.isEmpty() || // skip empty line or comment
            nextLine.startsWith("//") || nextLine.startsWith("}") || nextLine.startsWith("object")) {
                // include "} object" for Scala companion object
                continuation = true;
            } else if (!inComment && nextLine.startsWith("/*")) {
                inComment = true;
                continuation = true;
            } else if (inComment && nextLine.lastIndexOf("*/") >= 0) {
                inComment = false;
                continuation = true;
            } else if (nextLine.length() > 1 && nextLine.charAt(0) == '.' && // ".."
            nextLine.charAt(1) != '.' && nextLine.charAt(1) != '/') {
                // "./"
                continuation = true;
            } else if (inComment) {
                continuation = true;
            }
            if (continuation) {
                incomplete += s + "\n";
                continue;
            }
        }
        scala.tools.nsc.interpreter.Results.Result res = null;
        try {
            res = interpret(incomplete + s);
        } catch (Exception e) {
            sc.clearJobGroup();
            out.setInterpreterOutput(null);
            logger.info("Interpreter exception", e);
            return new InterpreterResult(Code.ERROR, InterpreterUtils.getMostRelevantMessage(e));
        }
        r = getResultCode(res);
        if (r == Code.ERROR) {
            sc.clearJobGroup();
            out.setInterpreterOutput(null);
            return new InterpreterResult(r, "");
        } else if (r == Code.INCOMPLETE) {
            incomplete += s + "\n";
        } else {
            incomplete = "";
        }
    }
    // make sure code does not finish with comment
    if (r == Code.INCOMPLETE) {
        scala.tools.nsc.interpreter.Results.Result res = null;
        res = interpret(incomplete + "\nprint(\"\")");
        r = getResultCode(res);
    }
    if (r == Code.INCOMPLETE) {
        sc.clearJobGroup();
        out.setInterpreterOutput(null);
        return new InterpreterResult(r, "Incomplete expression");
    } else {
        sc.clearJobGroup();
        putLatestVarInResourcePool(context);
        out.setInterpreterOutput(null);
        return new InterpreterResult(Code.SUCCESS);
    }
}
Also used : Results(scala.tools.nsc.interpreter.Results) scala(scala) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Code(org.apache.zeppelin.interpreter.InterpreterResult.Code) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)1 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)1 Code (org.apache.zeppelin.interpreter.InterpreterResult.Code)1 scala (scala)1 Results (scala.tools.nsc.interpreter.Results)1