use of cn.taketoday.expression.spel.ast.Ternary in project today-infrastructure by TAKETODAY.
the class InternalSpelExpressionParser method eatExpression.
// expression
// : logicalOrExpression
// ( (ASSIGN^ logicalOrExpression)
// | (DEFAULT^ logicalOrExpression)
// | (QMARK^ expression COLON! expression)
// | (ELVIS^ expression))?;
@Nullable
private SpelNodeImpl eatExpression() {
SpelNodeImpl expr = eatLogicalOrExpression();
Token t = peekToken();
if (t != null) {
if (t.kind == TokenKind.ASSIGN) {
// a=b
if (expr == null) {
expr = new NullLiteral(t.startPos - 1, t.endPos - 1);
}
nextToken();
SpelNodeImpl assignedValue = eatLogicalOrExpression();
return new Assign(t.startPos, t.endPos, expr, assignedValue);
}
if (t.kind == TokenKind.ELVIS) {
// a?:b (a if it isn't null, otherwise b)
if (expr == null) {
expr = new NullLiteral(t.startPos - 1, t.endPos - 2);
}
// elvis has left the building
nextToken();
SpelNodeImpl valueIfNull = eatExpression();
if (valueIfNull == null) {
valueIfNull = new NullLiteral(t.startPos + 1, t.endPos + 1);
}
return new Elvis(t.startPos, t.endPos, expr, valueIfNull);
}
if (t.kind == TokenKind.QMARK) {
// a?b:c
if (expr == null) {
expr = new NullLiteral(t.startPos - 1, t.endPos - 1);
}
nextToken();
SpelNodeImpl ifTrueExprValue = eatExpression();
eatToken(TokenKind.COLON);
SpelNodeImpl ifFalseExprValue = eatExpression();
return new Ternary(t.startPos, t.endPos, expr, ifTrueExprValue, ifFalseExprValue);
}
}
return expr;
}
use of cn.taketoday.expression.spel.ast.Ternary in project today-framework by TAKETODAY.
the class InternalSpelExpressionParser method eatExpression.
// expression
// : logicalOrExpression
// ( (ASSIGN^ logicalOrExpression)
// | (DEFAULT^ logicalOrExpression)
// | (QMARK^ expression COLON! expression)
// | (ELVIS^ expression))?;
@Nullable
private SpelNodeImpl eatExpression() {
SpelNodeImpl expr = eatLogicalOrExpression();
Token t = peekToken();
if (t != null) {
if (t.kind == TokenKind.ASSIGN) {
// a=b
if (expr == null) {
expr = new NullLiteral(t.startPos - 1, t.endPos - 1);
}
nextToken();
SpelNodeImpl assignedValue = eatLogicalOrExpression();
return new Assign(t.startPos, t.endPos, expr, assignedValue);
}
if (t.kind == TokenKind.ELVIS) {
// a?:b (a if it isn't null, otherwise b)
if (expr == null) {
expr = new NullLiteral(t.startPos - 1, t.endPos - 2);
}
// elvis has left the building
nextToken();
SpelNodeImpl valueIfNull = eatExpression();
if (valueIfNull == null) {
valueIfNull = new NullLiteral(t.startPos + 1, t.endPos + 1);
}
return new Elvis(t.startPos, t.endPos, expr, valueIfNull);
}
if (t.kind == TokenKind.QMARK) {
// a?b:c
if (expr == null) {
expr = new NullLiteral(t.startPos - 1, t.endPos - 1);
}
nextToken();
SpelNodeImpl ifTrueExprValue = eatExpression();
eatToken(TokenKind.COLON);
SpelNodeImpl ifFalseExprValue = eatExpression();
return new Ternary(t.startPos, t.endPos, expr, ifTrueExprValue, ifFalseExprValue);
}
}
return expr;
}
use of cn.taketoday.expression.spel.ast.Ternary in project today-framework by TAKETODAY.
the class SpelCompilationCoverageTests method mixingItUp_propertyAccessIndexerOpLtTernaryRootNull.
@Test
void mixingItUp_propertyAccessIndexerOpLtTernaryRootNull() {
Payload payload = new Payload();
expression = parser.parseExpression("DR[0].three");
Object v = expression.getValue(payload);
assertThat(getAst().getExitDescriptor()).isEqualTo("Lcn/taketoday/expression/spel/SpelCompilationCoverageTests$Three");
Expression expression = parser.parseExpression("DR[0].three.four lt 0.1d?#root:null");
v = expression.getValue(payload);
SpelExpression sExpr = (SpelExpression) expression;
Ternary ternary = (Ternary) sExpr.getAST();
OpLT oplt = (OpLT) ternary.getChild(0);
CompoundExpression cExpr = (CompoundExpression) oplt.getLeftOperand();
String cExprExitDescriptor = cExpr.getExitDescriptor();
assertThat(cExprExitDescriptor).isEqualTo("D");
assertThat(oplt.getExitDescriptor()).isEqualTo("Z");
assertCanCompile(expression);
Object vc = expression.getValue(payload);
assertThat(v).isEqualTo(payload);
assertThat(vc).isEqualTo(payload);
payload.DR[0].three.four = 0.13d;
vc = expression.getValue(payload);
assertThat(vc).isNull();
}
use of cn.taketoday.expression.spel.ast.Ternary in project today-infrastructure by TAKETODAY.
the class SpelCompilationCoverageTests method mixingItUp_propertyAccessIndexerOpLtTernaryRootNull.
@Test
void mixingItUp_propertyAccessIndexerOpLtTernaryRootNull() {
Payload payload = new Payload();
expression = parser.parseExpression("DR[0].three");
Object v = expression.getValue(payload);
assertThat(getAst().getExitDescriptor()).isEqualTo("Lcn/taketoday/expression/spel/SpelCompilationCoverageTests$Three");
Expression expression = parser.parseExpression("DR[0].three.four lt 0.1d?#root:null");
v = expression.getValue(payload);
SpelExpression sExpr = (SpelExpression) expression;
Ternary ternary = (Ternary) sExpr.getAST();
OpLT oplt = (OpLT) ternary.getChild(0);
CompoundExpression cExpr = (CompoundExpression) oplt.getLeftOperand();
String cExprExitDescriptor = cExpr.getExitDescriptor();
assertThat(cExprExitDescriptor).isEqualTo("D");
assertThat(oplt.getExitDescriptor()).isEqualTo("Z");
assertCanCompile(expression);
Object vc = expression.getValue(payload);
assertThat(v).isEqualTo(payload);
assertThat(vc).isEqualTo(payload);
payload.DR[0].three.four = 0.13d;
vc = expression.getValue(payload);
assertThat(vc).isNull();
}
Aggregations