Search in sources :

Example 1 with PatternToken

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

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

the class ASMCodeGeneratorUnitTest method testOnConstant_Pattern.

@Test
public void testOnConstant_Pattern() throws Exception {
    this.codeGenerator.onConstant(new PatternToken("[a-z_A-Z]+", 0, 0));
    Expression exp = this.codeGenerator.getResult(true);
    Object result = exp.execute();
    assertTrue(result instanceof Pattern);
    assertEquals("[a-z_A-Z]+", result.toString());
}
Also used : Pattern(java.util.regex.Pattern) PatternToken(com.googlecode.aviator.lexer.token.PatternToken) Expression(com.googlecode.aviator.Expression) Test(org.junit.Test)

Example 3 with PatternToken

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

the class ASMCodeGeneratorUnitTest method testOnMatch.

@Test
public void testOnMatch() throws Exception {
    this.codeGenerator.onConstant(new StringToken("killme2008@gmail.com", 0, 0));
    this.codeGenerator.onConstant(new PatternToken("^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[a-z]{2,4}$", 0, 1));
    this.codeGenerator.onMatch(null);
    Object result = eval(new HashMap<String, Object>());
    assertEquals(Boolean.TRUE, result);
}
Also used : PatternToken(com.googlecode.aviator.lexer.token.PatternToken) StringToken(com.googlecode.aviator.lexer.token.StringToken) Test(org.junit.Test)

Example 4 with PatternToken

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

the class ExpressionParser method pattern.

private void pattern() {
    // It is a pattern
    int startIndex = this.lookhead.getStartIndex();
    move(true);
    this.inPattern = true;
    StringBuilder sb = new StringBuilder();
    while (this.lookhead != null) {
        while (!expectChar('/')) {
            sb.append(this.lookhead.getLexeme());
            move(false);
        }
        if (getPrevToken().getType() == TokenType.Char && ((CharToken) getPrevToken()).getLexeme().equals("\\")) {
            sb.append("/");
            move(false);
            continue;
        }
        this.inPattern = false;
        break;
    }
    if (this.inPattern) {
        reportSyntaxError("invalid regular pattern:" + sb.toString());
    }
    getCodeGeneratorWithTimes().onConstant(new PatternToken(sb.toString(), this.lexer.getLineNo(), startIndex));
    move(true);
}
Also used : PatternToken(com.googlecode.aviator.lexer.token.PatternToken) CharToken(com.googlecode.aviator.lexer.token.CharToken)

Aggregations

PatternToken (com.googlecode.aviator.lexer.token.PatternToken)4 StringToken (com.googlecode.aviator.lexer.token.StringToken)2 Pattern (java.util.regex.Pattern)2 Test (org.junit.Test)2 Expression (com.googlecode.aviator.Expression)1 CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)1 CharToken (com.googlecode.aviator.lexer.token.CharToken)1 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)1 AviatorBoolean (com.googlecode.aviator.runtime.type.AviatorBoolean)1 AviatorNumber (com.googlecode.aviator.runtime.type.AviatorNumber)1 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)1 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)1 AviatorRuntimeJavaType (com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType)1 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)1