Search in sources :

Example 1 with ExpressionLexer

use of com.googlecode.aviator.lexer.ExpressionLexer 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 2 with ExpressionLexer

use of com.googlecode.aviator.lexer.ExpressionLexer in project aviatorscript by killme2008.

the class AviatorEvaluatorInstance method compileStringSegments.

/**
 * Compile a string to string segments, if string doesn't have a interpolation,returns an empty
 * list.
 *
 * @param lexeme
 * @param sourceFile
 * @param lineNo;
 * @return
 */
public StringSegments compileStringSegments(final String lexeme, final String sourceFile, final int lineNo) {
    List<StringSegment> segs = new ArrayList<StringSegment>();
    boolean hasInterpolationOrEscaped = false;
    StringCharacterIterator it = new StringCharacterIterator(lexeme);
    char ch = it.current(), prev = StringCharacterIterator.DONE;
    int lastInterPos = 0;
    int i = 1;
    for (; ; ) {
        if (ch == '#') {
            if (prev == '\\') {
                // # is escaped, skip the backslash.
                final String segStr = lexeme.substring(lastInterPos, i - 2);
                segs.add(new LiteralSegment(segStr));
                lastInterPos = i - 1;
                hasInterpolationOrEscaped = true;
            } else {
                // # is not escaped.
                prev = ch;
                ch = it.next();
                i++;
                if (ch == '{') {
                    // Find a interpolation position.
                    if (i - 2 > lastInterPos) {
                        final String segStr = lexeme.substring(lastInterPos, i - 2);
                        segs.add(new LiteralSegment(segStr));
                    }
                    try {
                        ExpressionLexer lexer = new ExpressionLexer(this, lexeme.substring(i));
                        lexer.setLineNo(lineNo);
                        ExpressionParser parser = new ExpressionParser(this, lexer, newCodeGenerator(sourceFile, false));
                        Expression exp = parser.parse(false);
                        final Token<?> lookhead = parser.getLookhead();
                        if (lookhead == null || (lookhead.getType() != TokenType.Char || ((CharToken) lookhead).getCh() != '}')) {
                            parser.reportSyntaxError("expect '}' to complete string interpolation");
                        }
                        int expStrLen = lookhead.getStartIndex() + 1;
                        while (expStrLen-- > 0) {
                            prev = ch;
                            ch = it.next();
                            i++;
                        }
                        Token<?> previousToken = null;
                        if (parser.getParsedTokens() == 2 && (previousToken = parser.getPrevToken()) != null && previousToken.getType() == TokenType.Variable) {
                            // special case for inline variable.
                            if (previousToken == Variable.TRUE) {
                                segs.add(new LiteralSegment("true"));
                            } else if (previousToken == Variable.FALSE) {
                                segs.add(new LiteralSegment("false"));
                            } else if (previousToken == Variable.NIL) {
                                segs.add(new LiteralSegment("null"));
                            } else {
                                segs.add(new VarSegment(parser.getSymbolTable().reserve(previousToken.getLexeme()).getLexeme()));
                            }
                        } else {
                            segs.add(new ExpressionSegment(exp));
                        }
                        hasInterpolationOrEscaped = true;
                        lastInterPos = i;
                    } catch (Throwable t) {
                        throw new CompileExpressionErrorException("Fail to compile string interpolation: " + lexeme, t);
                    }
                // End of interpolation
                }
            // End of # is not escaped.
            }
        }
        if (ch == StringCharacterIterator.DONE) {
            if (i - 1 > lastInterPos) {
                final String segStr = lexeme.substring(lastInterPos, i - 1);
                segs.add(new LiteralSegment(segStr));
            }
            break;
        }
        prev = ch;
        ch = it.next();
        i++;
    }
    if (hasInterpolationOrEscaped) {
        return new StringSegments(segs, lexeme.length() * 2 / 3);
    } else {
        return new StringSegments(Collections.<StringSegment>emptyList(), 0);
    }
}
Also used : ArrayList(java.util.ArrayList) LiteralSegment(com.googlecode.aviator.runtime.type.string.LiteralSegment) ExpressionSegment(com.googlecode.aviator.runtime.type.string.ExpressionSegment) StringSegment(com.googlecode.aviator.runtime.type.string.StringSegment) VarSegment(com.googlecode.aviator.runtime.type.string.VarSegment) StringCharacterIterator(java.text.StringCharacterIterator) ExpressionLexer(com.googlecode.aviator.lexer.ExpressionLexer) CompileExpressionErrorException(com.googlecode.aviator.exception.CompileExpressionErrorException) CharToken(com.googlecode.aviator.lexer.token.CharToken) ExpressionParser(com.googlecode.aviator.parser.ExpressionParser)

Example 3 with ExpressionLexer

use of com.googlecode.aviator.lexer.ExpressionLexer in project aviatorscript by killme2008.

the class AviatorEvaluatorInstance method innerCompile.

private Expression innerCompile(final String expression, final String sourceFile, final boolean cached) {
    ExpressionLexer lexer = new ExpressionLexer(this, expression);
    CodeGenerator codeGenerator = newCodeGenerator(sourceFile, cached);
    ExpressionParser parser = new ExpressionParser(this, lexer, codeGenerator);
    Expression exp = parser.parse();
    if (getOptionValue(Options.TRACE_EVAL).bool) {
        ((BaseExpression) exp).setExpression(expression);
    }
    return exp;
}
Also used : ExpressionLexer(com.googlecode.aviator.lexer.ExpressionLexer) 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 4 with ExpressionLexer

use of com.googlecode.aviator.lexer.ExpressionLexer in project aviatorscript by killme2008.

the class ExpressionParserUnitTest method testParseFunctionParams.

@Test
public void testParseFunctionParams() {
    this.instance.setOption(Options.CAPTURE_FUNCTION_ARGS, true);
    this.parser = new ExpressionParser(this.instance, new ExpressionLexer(this.instance, "println(3+2);4"), this.codeGenerator);
    this.parser.parse();
    assertEquals("3 2 + method_invoke<[FunctionArgument [index=0, expression=3+2]]> ; 4", this.codeGenerator.getPostFixExpression());
    {
        this.codeGenerator = new FakeCodeGenerator();
        this.parser = new ExpressionParser(this.instance, new ExpressionLexer(this.instance, "println(a,be,cdf,2+3)"), this.codeGenerator);
        this.parser.parse();
        assertEquals("a be cdf 2 3 + method_invoke<[FunctionArgument [index=0, expression=a], FunctionArgument [index=1, expression=be], FunctionArgument [index=2, expression=cdf], FunctionArgument [index=3, expression=2+3]]>", this.codeGenerator.getPostFixExpression());
    }
    {
        this.codeGenerator = new FakeCodeGenerator();
        this.parser = new ExpressionParser(this.instance, new ExpressionLexer(this.instance, "println(a,b,println(c,1+2))"), this.codeGenerator);
        this.parser.parse();
        assertEquals("a b c 1 2 + method_invoke<[FunctionArgument [index=0, expression=c], FunctionArgument [index=1, expression=1+2]]> method_invoke<[FunctionArgument [index=0, expression=a], FunctionArgument [index=1, expression=b], FunctionArgument [index=2, expression=println(c,1+2)]]>", this.codeGenerator.getPostFixExpression());
    }
    {
        // function chains
        this.codeGenerator = new FakeCodeGenerator();
        this.parser = new ExpressionParser(this.instance, new ExpressionLexer(this.instance, "test(a, ab, c)(d, e)(foo)"), this.codeGenerator);
        this.parser.parse();
        assertEquals("a ab c method_invoke<[FunctionArgument [index=0, expression=a], FunctionArgument [index=1, expression=ab], FunctionArgument [index=2, expression=c]]> d e method_invoke<[FunctionArgument [index=0, expression=d], FunctionArgument [index=1, expression=e]]> foo method_invoke<[FunctionArgument [index=0, expression=foo]]>", this.codeGenerator.getPostFixExpression());
    }
}
Also used : ExpressionLexer(com.googlecode.aviator.lexer.ExpressionLexer) Test(org.junit.Test)

Aggregations

ExpressionLexer (com.googlecode.aviator.lexer.ExpressionLexer)4 ExpressionParser (com.googlecode.aviator.parser.ExpressionParser)3 CodeGenerator (com.googlecode.aviator.code.CodeGenerator)2 EvalCodeGenerator (com.googlecode.aviator.code.EvalCodeGenerator)2 NoneCodeGenerator (com.googlecode.aviator.code.NoneCodeGenerator)2 OptimizeCodeGenerator (com.googlecode.aviator.code.OptimizeCodeGenerator)2 ASMCodeGenerator (com.googlecode.aviator.code.asm.ASMCodeGenerator)2 InterpretCodeGenerator (com.googlecode.aviator.code.interpreter.InterpretCodeGenerator)2 CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)2 CharToken (com.googlecode.aviator.lexer.token.CharToken)1 ExpressionSegment (com.googlecode.aviator.runtime.type.string.ExpressionSegment)1 LiteralSegment (com.googlecode.aviator.runtime.type.string.LiteralSegment)1 StringSegment (com.googlecode.aviator.runtime.type.string.StringSegment)1 VarSegment (com.googlecode.aviator.runtime.type.string.VarSegment)1 StringCharacterIterator (java.text.StringCharacterIterator)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1