Search in sources :

Example 1 with StringSegments

use of com.googlecode.aviator.AviatorEvaluatorInstance.StringSegments in project aviatorscript by killme2008.

the class AviatorEvaluatorInstanceUnitTest method testCompileStringSegments.

@Test
public void testCompileStringSegments() {
    StringSegments segs = this.instance.compileStringSegments("#test");
    assertTrue(segs.isEmpty());
    segs = this.instance.compileStringSegments("#test#.");
    assertTrue(segs.isEmpty());
    segs = this.instance.compileStringSegments("#test#.#");
    assertTrue(segs.isEmpty());
    segs = this.instance.compileStringSegments("#test#{a}");
    assertFalse(segs.isEmpty());
    assertEquals("#testnull", segs.toString(AviatorEvaluator.newEnv(), null));
    assertEquals("#test1", segs.toString(AviatorEvaluator.newEnv("a", 1), null));
}
Also used : StringSegments(com.googlecode.aviator.AviatorEvaluatorInstance.StringSegments) Test(org.junit.Test)

Example 2 with StringSegments

use of com.googlecode.aviator.AviatorEvaluatorInstance.StringSegments in project aviatorscript by killme2008.

the class AviatorString method getLexeme.

public String getLexeme(final Map<String, Object> env, final boolean warnOnCompile) {
    AviatorEvaluatorInstance engine = RuntimeUtils.getInstance(env);
    if (!this.isLiteral || !this.hasInterpolation || !engine.isFeatureEnabled(Feature.StringInterpolation) || this.lexeme == null || this.lexeme.length() < 3) {
        return this.lexeme;
    }
    StringSegments segs = null;
    BaseExpression exp = (BaseExpression) (env == null ? null : env.get(Constants.EXP_VAR));
    if (exp != null) {
        segs = exp.getStringSegements(this.lexeme, this.lineNo);
    } else {
        segs = engine.compileStringSegments(this.lexeme);
        if (warnOnCompile) {
            warnOnCompileWithoutCaching();
        }
    }
    assert (segs != null);
    return segs.toString(env, this.lexeme);
}
Also used : StringSegments(com.googlecode.aviator.AviatorEvaluatorInstance.StringSegments) AviatorEvaluatorInstance(com.googlecode.aviator.AviatorEvaluatorInstance) BaseExpression(com.googlecode.aviator.BaseExpression)

Example 3 with StringSegments

use of com.googlecode.aviator.AviatorEvaluatorInstance.StringSegments in project aviatorscript by killme2008.

the class BaseExpression method getStringSegements.

public StringSegments getStringSegements(final String lexeme, final int lineNo) {
    FutureTask<StringSegments> task = this.stringSegs.get(lexeme);
    if (task == null) {
        task = new FutureTask<>(new Callable<StringSegments>() {

            @Override
            public StringSegments call() throws Exception {
                final StringSegments compiledSegs = BaseExpression.this.instance.compileStringSegments(lexeme, BaseExpression.this.sourceFile, lineNo);
                return compiledSegs;
            }
        });
        FutureTask<StringSegments> existsTask = this.stringSegs.putIfAbsent(lexeme, task);
        if (existsTask != null) {
            task = existsTask;
        } else {
            // first run
            task.run();
        }
    }
    try {
        return task.get();
    } catch (ExecutionException t) {
        throw Reflector.sneakyThrow(t.getCause());
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw Reflector.sneakyThrow(e);
    }
}
Also used : StringSegments(com.googlecode.aviator.AviatorEvaluatorInstance.StringSegments) ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable)

Aggregations

StringSegments (com.googlecode.aviator.AviatorEvaluatorInstance.StringSegments)3 AviatorEvaluatorInstance (com.googlecode.aviator.AviatorEvaluatorInstance)1 BaseExpression (com.googlecode.aviator.BaseExpression)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 Test (org.junit.Test)1