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