Search in sources :

Example 1 with CompileExpressionErrorException

use of com.googlecode.aviator.exception.CompileExpressionErrorException in project aviatorscript by killme2008.

the class GrammarUnitTest method testStringInterpolation.

@Test
public void testStringInterpolation() {
    assertEquals("aviator", this.instance.execute("let name='aviator'; '#{name}'"));
    assertEquals("hello,aviator", this.instance.execute("let name='aviator'; 'hello,#{name}'"));
    assertEquals("hello,aviator,great", this.instance.execute("let name='aviator'; 'hello,#{name},great'"));
    assertEquals("3", this.instance.execute("'#{1+2}'"));
    assertEquals("good,3", this.instance.execute("'good,#{1+2}'"));
    String name = "aviator";
    Expression exp = this.instance.compile(" a ? '#{name}': 'hello,#{name},great'");
    assertEquals("aviator", exp.execute(exp.newEnv("a", true, "name", name)));
    assertEquals("hello,aviator,great", exp.execute(exp.newEnv("a", false, "name", name)));
    assertEquals("aviator", exp.execute(exp.newEnv("a", true, "name", name)));
    assertEquals("hello,aviator,great", exp.execute(exp.newEnv("a", false, "name", name)));
    assertEquals("cool", exp.execute(exp.newEnv("a", true, "name", "cool")));
    assertEquals("hello,cool,great", exp.execute(exp.newEnv("a", false, "name", "cool")));
    // String Interpolation in string.
    exp = this.instance.compile("'hello,#{a+b+\"good,#{c*d}\"}'");
    assertEquals("hello,3good,12", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("hello,3good,12", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("hello,11good,56", exp.execute(exp.newEnv("a", 5, "b", 6, "c", 7, "d", 8)));
    exp = this.instance.compile("'hello,#{a+b+\"good,#{let c=100; c*d}\"}'");
    assertEquals("hello,3good,400", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("hello,3good,400", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("hello,3good,9900", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 99)));
    exp = this.instance.compile("'#{a+b/c}'");
    assertEquals("1", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("1", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    exp = this.instance.compile("'#{a+b/c}' + d");
    assertEquals("14", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("14", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("36", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
    assertEquals("36", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
    exp = this.instance.compile("d + '#{a+b/c}'");
    assertEquals("41", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("41", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("63", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
    assertEquals("63", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
    exp = this.instance.compile("d + '#{a+b/c}' + a");
    assertEquals("411", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("411", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("633", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
    assertEquals("633", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
    exp = this.instance.compile("'#{d}#{a+b/c}#{a}'");
    assertEquals("411", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("411", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("633", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
    assertEquals("633", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
    exp = this.instance.compile("'#{d},a+b/c=#{a+b/c}#{a}'");
    assertEquals("4,a+b/c=11", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("4,a+b/c=11", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("6,a+b/c=33", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
    assertEquals("6,a+b/c=33", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
    exp = this.instance.compile("'d=#{d},a+b/c=#{a+b/c},a=#{a}'");
    assertEquals("d=4,a+b/c=1,a=1", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("d=4,a+b/c=1,a=1", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
    assertEquals("d=6,a+b/c=3,a=3", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
    assertEquals("d=6,a+b/c=3,a=3", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
    assertEquals("#{name}", this.instance.execute("'\\#{name}'"));
    assertEquals("a#{name}", this.instance.execute("'a\\#{name}'"));
    assertEquals("a#{name}b3", this.instance.execute("'a\\#{name}b#{1+2}'"));
    assertEquals("\\#{name}", this.instance.execute("'\\\\\\#{name}'"));
    assertEquals("#{name3", this.instance.execute("'\\#{name#{1+2}'"));
    try {
        this.instance.execute("'#{1+2'");
        fail();
    } catch (CompileExpressionErrorException e) {
        e.printStackTrace();
    }
    try {
        this.instance.execute("'#{1+*2'");
        fail();
    } catch (CompileExpressionErrorException e) {
        e.printStackTrace();
    }
}
Also used : Expression(com.googlecode.aviator.Expression) CompileExpressionErrorException(com.googlecode.aviator.exception.CompileExpressionErrorException) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Example 2 with CompileExpressionErrorException

use of com.googlecode.aviator.exception.CompileExpressionErrorException in project aviatorscript by killme2008.

the class AviatorEvaluatorInstance method validate.

/**
 * Validate a script text whether is a legal aviatorscript text, throw exception if not.
 *
 * @since 5.0.2
 * @param script the script text
 */
public void validate(final String script) {
    if (script == null || script.trim().length() == 0) {
        throw new CompileExpressionErrorException("Blank script");
    }
    ExpressionLexer lexer = new ExpressionLexer(this, script);
    CodeGenerator codeGenerator = new NoneCodeGenerator();
    ExpressionParser parser = new ExpressionParser(this, lexer, codeGenerator);
    parser.parse();
}
Also used : ExpressionLexer(com.googlecode.aviator.lexer.ExpressionLexer) CompileExpressionErrorException(com.googlecode.aviator.exception.CompileExpressionErrorException) NoneCodeGenerator(com.googlecode.aviator.code.NoneCodeGenerator) ExpressionParser(com.googlecode.aviator.parser.ExpressionParser) NoneCodeGenerator(com.googlecode.aviator.code.NoneCodeGenerator) EvalCodeGenerator(com.googlecode.aviator.code.EvalCodeGenerator) ASMCodeGenerator(com.googlecode.aviator.code.asm.ASMCodeGenerator) OptimizeCodeGenerator(com.googlecode.aviator.code.OptimizeCodeGenerator) CodeGenerator(com.googlecode.aviator.code.CodeGenerator) InterpretCodeGenerator(com.googlecode.aviator.code.interpreter.InterpretCodeGenerator)

Example 3 with CompileExpressionErrorException

use of com.googlecode.aviator.exception.CompileExpressionErrorException in project aviatorscript by killme2008.

the class OptimizeCodeGenerator method onLambdaDefineStart.

@Override
public void onLambdaDefineStart(final Token<?> lookhead) {
    if (this.lambdaGenerator == null) {
        // TODO cache?
        Boolean newLexicalScope = lookhead.getMeta(Constants.SCOPE_META, false);
        Boolean inheritEnv = lookhead.getMeta(Constants.INHERIT_ENV_META, false);
        this.lambdaGenerator = new LambdaGenerator(this.instance, this, this.parser, this.codeGen.getClassLoader(), this.sourceFile, newLexicalScope, inheritEnv);
        this.lambdaGenerator.setScopeInfo(this.parser.enterScope(newLexicalScope));
    } else {
        throw new CompileExpressionErrorException("Compile lambda error");
    }
}
Also used : CompileExpressionErrorException(com.googlecode.aviator.exception.CompileExpressionErrorException) AviatorBoolean(com.googlecode.aviator.runtime.type.AviatorBoolean)

Example 4 with CompileExpressionErrorException

use of com.googlecode.aviator.exception.CompileExpressionErrorException in project aviatorscript by killme2008.

the class OptimizeCodeGenerator method getTokenFromOperand.

/**
 * Get token from executing result
 *
 * @param operand
 * @return
 */
private Token<?> getTokenFromOperand(final Token<?> operatorToken, final AviatorObject operand) {
    Token<?> token = null;
    switch(operand.getAviatorType()) {
        case JavaType:
            if (operand instanceof AviatorRuntimeJavaType) {
                Object val = operand.getValue(null);
                if (val == null) {
                    token = Variable.NIL;
                } else if (val instanceof Number) {
                    token = new NumberToken((Number) val, val.toString(), operatorToken.getLineNo(), operatorToken.getStartIndex());
                } else if (val instanceof String || val instanceof Character) {
                    String s = val.toString();
                    token = new StringToken(s, operatorToken.getLineNo(), operatorToken.getStartIndex()).withMeta(Constants.INTER_META, s.contains("#"));
                } else if (val instanceof Pattern) {
                    token = new PatternToken(((Pattern) val).pattern(), operatorToken.getLineNo(), operatorToken.getStartIndex());
                } else if (val instanceof Boolean) {
                    token = (boolean) val ? Variable.TRUE : Variable.FALSE;
                } else {
                    throw new CompileExpressionErrorException("Invalid operand:" + operand.desc(null));
                }
            } else {
                throw new CompileExpressionErrorException("Invalid operand:" + operand.desc(null));
            }
            break;
        case Boolean:
            token = operand.booleanValue(null) ? Variable.TRUE : Variable.FALSE;
            break;
        case Nil:
            token = Variable.NIL;
            break;
        case BigInt:
        case Decimal:
        case Double:
        case Long:
            final Number value = (Number) operand.getValue(null);
            token = new NumberToken(value, value.toString(), operatorToken.getLineNo(), operatorToken.getStartIndex());
            break;
        case String:
            final String str = (String) operand.getValue(null);
            token = new StringToken(str, operatorToken.getLineNo(), operatorToken.getStartIndex());
            break;
        case Pattern:
            token = new PatternToken(((AviatorPattern) operand).getPattern().pattern(), operatorToken.getLineNo(), operatorToken.getStartIndex());
            break;
    }
    return token;
}
Also used : AviatorPattern(com.googlecode.aviator.runtime.type.AviatorPattern) Pattern(java.util.regex.Pattern) PatternToken(com.googlecode.aviator.lexer.token.PatternToken) AviatorNumber(com.googlecode.aviator.runtime.type.AviatorNumber) CompileExpressionErrorException(com.googlecode.aviator.exception.CompileExpressionErrorException) AviatorRuntimeJavaType(com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType) NumberToken(com.googlecode.aviator.lexer.token.NumberToken) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorBoolean(com.googlecode.aviator.runtime.type.AviatorBoolean) StringToken(com.googlecode.aviator.lexer.token.StringToken)

Example 5 with CompileExpressionErrorException

use of com.googlecode.aviator.exception.CompileExpressionErrorException in project aviatorscript by killme2008.

the class ASMCodeGenerator method onLambdaDefineStart.

@Override
public void onLambdaDefineStart(final Token<?> lookhead) {
    if (this.lambdaGenerator == null) {
        Boolean newLexicalScope = lookhead.getMeta(Constants.SCOPE_META, false);
        Boolean inheritEnv = lookhead.getMeta(Constants.INHERIT_ENV_META, false);
        // TODO cache?
        this.lambdaGenerator = new LambdaGenerator(this.instance, this, this.parser, this.classLoader, this.sourceFile, newLexicalScope, inheritEnv);
        this.lambdaGenerator.setScopeInfo(this.parser.enterScope(newLexicalScope));
    } else {
        throw new CompileExpressionErrorException("Compile lambda error");
    }
}
Also used : LambdaGenerator(com.googlecode.aviator.code.LambdaGenerator) CompileExpressionErrorException(com.googlecode.aviator.exception.CompileExpressionErrorException)

Aggregations

CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)9 LambdaGenerator (com.googlecode.aviator.code.LambdaGenerator)2 ExpressionLexer (com.googlecode.aviator.lexer.ExpressionLexer)2 CharToken (com.googlecode.aviator.lexer.token.CharToken)2 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)2 StringToken (com.googlecode.aviator.lexer.token.StringToken)2 ExpressionParser (com.googlecode.aviator.parser.ExpressionParser)2 AviatorBoolean (com.googlecode.aviator.runtime.type.AviatorBoolean)2 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)2 ArrayList (java.util.ArrayList)2 BaseExpression (com.googlecode.aviator.BaseExpression)1 Expression (com.googlecode.aviator.Expression)1 CodeGenerator (com.googlecode.aviator.code.CodeGenerator)1 EvalCodeGenerator (com.googlecode.aviator.code.EvalCodeGenerator)1 NoneCodeGenerator (com.googlecode.aviator.code.NoneCodeGenerator)1 OptimizeCodeGenerator (com.googlecode.aviator.code.OptimizeCodeGenerator)1 ASMCodeGenerator (com.googlecode.aviator.code.asm.ASMCodeGenerator)1 InterpretCodeGenerator (com.googlecode.aviator.code.interpreter.InterpretCodeGenerator)1 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 ExpressionSyntaxErrorException (com.googlecode.aviator.exception.ExpressionSyntaxErrorException)1