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();
}
}
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();
}
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");
}
}
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;
}
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");
}
}
Aggregations