Search in sources :

Example 1 with SSource

use of org.elasticsearch.painless.node.SSource in project elasticsearch by elastic.

the class Walker method visitSource.

@Override
public ANode visitSource(SourceContext ctx) {
    reserved.push(new MainMethodReserved());
    List<SFunction> functions = new ArrayList<>();
    for (FunctionContext function : ctx.function()) {
        functions.add((SFunction) visit(function));
    }
    List<AStatement> statements = new ArrayList<>();
    for (StatementContext statement : ctx.statement()) {
        statements.add((AStatement) visit(statement));
    }
    return new SSource(scriptInterface, settings, sourceName, sourceText, debugStream, (MainMethodReserved) reserved.pop(), location(ctx), functions, globals, statements);
}
Also used : MainMethodReserved(org.elasticsearch.painless.node.SSource.MainMethodReserved) AStatement(org.elasticsearch.painless.node.AStatement) SFunction(org.elasticsearch.painless.node.SFunction) SSource(org.elasticsearch.painless.node.SSource) ArrayList(java.util.ArrayList) FunctionContext(org.elasticsearch.painless.antlr.PainlessParser.FunctionContext) StatementContext(org.elasticsearch.painless.antlr.PainlessParser.StatementContext)

Example 2 with SSource

use of org.elasticsearch.painless.node.SSource in project elasticsearch by elastic.

the class Compiler method compile.

/**
     * Runs the two-pass compiler to generate a Painless script.
     * @param <T> the type of the script
     * @param loader The ClassLoader used to define the script.
     * @param iface Interface the compiled script should implement
     * @param name The name of the script.
     * @param source The source code for the script.
     * @param settings The CompilerSettings to be used during the compilation.
     * @return An executable script that implements both {@code <T>} and is a subclass of {@link PainlessScript}
     */
static <T> T compile(Loader loader, Class<T> iface, String name, String source, CompilerSettings settings) {
    if (source.length() > MAXIMUM_SOURCE_LENGTH) {
        throw new IllegalArgumentException("Scripts may be no longer than " + MAXIMUM_SOURCE_LENGTH + " characters.  The passed in script is " + source.length() + " characters.  Consider using a" + " plugin if a script longer than this length is a requirement.");
    }
    ScriptInterface scriptInterface = new ScriptInterface(iface);
    SSource root = Walker.buildPainlessTree(scriptInterface, name, source, settings, null);
    root.analyze();
    root.write();
    try {
        Class<? extends PainlessScript> clazz = loader.define(CLASS_NAME, root.getBytes());
        java.lang.reflect.Constructor<? extends PainlessScript> constructor = clazz.getConstructor(String.class, String.class, BitSet.class);
        return iface.cast(constructor.newInstance(name, source, root.getStatements()));
    } catch (Exception exception) {
        // Catch everything to let the user know this is something caused internally.
        throw new IllegalStateException("An internal error occurred attempting to define the script [" + name + "].", exception);
    }
}
Also used : SSource(org.elasticsearch.painless.node.SSource) MalformedURLException(java.net.MalformedURLException)

Example 3 with SSource

use of org.elasticsearch.painless.node.SSource in project elasticsearch by elastic.

the class Compiler method compile.

/**
     * Runs the two-pass compiler to generate a Painless script.  (Used by the debugger.)
     * @param iface Interface the compiled script should implement
     * @param source The source code for the script.
     * @param settings The CompilerSettings to be used during the compilation.
     * @return The bytes for compilation.
     */
static byte[] compile(Class<?> iface, String name, String source, CompilerSettings settings, Printer debugStream) {
    if (source.length() > MAXIMUM_SOURCE_LENGTH) {
        throw new IllegalArgumentException("Scripts may be no longer than " + MAXIMUM_SOURCE_LENGTH + " characters.  The passed in script is " + source.length() + " characters.  Consider using a" + " plugin if a script longer than this length is a requirement.");
    }
    ScriptInterface scriptInterface = new ScriptInterface(iface);
    SSource root = Walker.buildPainlessTree(scriptInterface, name, source, settings, debugStream);
    root.analyze();
    root.write();
    return root.getBytes();
}
Also used : SSource(org.elasticsearch.painless.node.SSource)

Aggregations

SSource (org.elasticsearch.painless.node.SSource)3 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 FunctionContext (org.elasticsearch.painless.antlr.PainlessParser.FunctionContext)1 StatementContext (org.elasticsearch.painless.antlr.PainlessParser.StatementContext)1 AStatement (org.elasticsearch.painless.node.AStatement)1 SFunction (org.elasticsearch.painless.node.SFunction)1 MainMethodReserved (org.elasticsearch.painless.node.SSource.MainMethodReserved)1