Search in sources :

Example 1 with InlineMap

use of cn.taketoday.expression.spel.ast.InlineMap in project today-infrastructure by TAKETODAY.

the class MapTests method checkConstantMap.

private void checkConstantMap(String expressionText, boolean expectedToBeConstant) {
    SpelExpressionParser parser = new SpelExpressionParser();
    SpelExpression expression = (SpelExpression) parser.parseExpression(expressionText);
    SpelNode node = expression.getAST();
    boolean condition = node instanceof InlineMap;
    assertThat(condition).isTrue();
    InlineMap inlineMap = (InlineMap) node;
    if (expectedToBeConstant) {
        assertThat(inlineMap.isConstant()).isTrue();
    } else {
        assertThat(inlineMap.isConstant()).isFalse();
    }
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) InlineMap(cn.taketoday.expression.spel.ast.InlineMap)

Example 2 with InlineMap

use of cn.taketoday.expression.spel.ast.InlineMap in project today-infrastructure by TAKETODAY.

the class InternalSpelExpressionParser method maybeEatInlineListOrMap.

// list = LCURLY (element (COMMA element)*) RCURLY
// map  = LCURLY (key ':' value (COMMA key ':' value)*) RCURLY
private boolean maybeEatInlineListOrMap() {
    Token t = peekToken();
    if (!peekToken(TokenKind.LCURLY, true)) {
        return false;
    }
    Assert.state(t != null, "No token");
    SpelNodeImpl expr = null;
    Token closingCurly = peekToken();
    if (peekToken(TokenKind.RCURLY, true)) {
        // empty list '{}'
        Assert.state(closingCurly != null, "No token");
        expr = new InlineList(t.startPos, closingCurly.endPos);
    } else if (peekToken(TokenKind.COLON, true)) {
        closingCurly = eatToken(TokenKind.RCURLY);
        // empty map '{:}'
        expr = new InlineMap(t.startPos, closingCurly.endPos);
    } else {
        SpelNodeImpl firstExpression = eatExpression();
        // ':' - this is a map!
        if (peekToken(TokenKind.RCURLY)) {
            // list with one item in it
            List<SpelNodeImpl> elements = new ArrayList<>();
            elements.add(firstExpression);
            closingCurly = eatToken(TokenKind.RCURLY);
            expr = new InlineList(t.startPos, closingCurly.endPos, elements.toArray(new SpelNodeImpl[0]));
        } else if (peekToken(TokenKind.COMMA, true)) {
            // multi-item list
            List<SpelNodeImpl> elements = new ArrayList<>();
            elements.add(firstExpression);
            do {
                elements.add(eatExpression());
            } while (peekToken(TokenKind.COMMA, true));
            closingCurly = eatToken(TokenKind.RCURLY);
            expr = new InlineList(t.startPos, closingCurly.endPos, elements.toArray(new SpelNodeImpl[0]));
        } else if (peekToken(TokenKind.COLON, true)) {
            // map!
            List<SpelNodeImpl> elements = new ArrayList<>();
            elements.add(firstExpression);
            elements.add(eatExpression());
            while (peekToken(TokenKind.COMMA, true)) {
                elements.add(eatExpression());
                eatToken(TokenKind.COLON);
                elements.add(eatExpression());
            }
            closingCurly = eatToken(TokenKind.RCURLY);
            expr = new InlineMap(t.startPos, closingCurly.endPos, elements.toArray(new SpelNodeImpl[0]));
        } else {
            throw internalException(t.startPos, SpelMessage.OOD);
        }
    }
    this.constructedNodes.push(expr);
    return true;
}
Also used : SpelNodeImpl(cn.taketoday.expression.spel.ast.SpelNodeImpl) ArrayList(java.util.ArrayList) InlineList(cn.taketoday.expression.spel.ast.InlineList) List(java.util.List) ArrayList(java.util.ArrayList) InlineList(cn.taketoday.expression.spel.ast.InlineList) InlineMap(cn.taketoday.expression.spel.ast.InlineMap)

Example 3 with InlineMap

use of cn.taketoday.expression.spel.ast.InlineMap in project today-framework by TAKETODAY.

the class InternalSpelExpressionParser method maybeEatInlineListOrMap.

// list = LCURLY (element (COMMA element)*) RCURLY
// map  = LCURLY (key ':' value (COMMA key ':' value)*) RCURLY
private boolean maybeEatInlineListOrMap() {
    Token t = peekToken();
    if (!peekToken(TokenKind.LCURLY, true)) {
        return false;
    }
    Assert.state(t != null, "No token");
    SpelNodeImpl expr = null;
    Token closingCurly = peekToken();
    if (peekToken(TokenKind.RCURLY, true)) {
        // empty list '{}'
        Assert.state(closingCurly != null, "No token");
        expr = new InlineList(t.startPos, closingCurly.endPos);
    } else if (peekToken(TokenKind.COLON, true)) {
        closingCurly = eatToken(TokenKind.RCURLY);
        // empty map '{:}'
        expr = new InlineMap(t.startPos, closingCurly.endPos);
    } else {
        SpelNodeImpl firstExpression = eatExpression();
        // ':' - this is a map!
        if (peekToken(TokenKind.RCURLY)) {
            // list with one item in it
            List<SpelNodeImpl> elements = new ArrayList<>();
            elements.add(firstExpression);
            closingCurly = eatToken(TokenKind.RCURLY);
            expr = new InlineList(t.startPos, closingCurly.endPos, elements.toArray(new SpelNodeImpl[0]));
        } else if (peekToken(TokenKind.COMMA, true)) {
            // multi-item list
            List<SpelNodeImpl> elements = new ArrayList<>();
            elements.add(firstExpression);
            do {
                elements.add(eatExpression());
            } while (peekToken(TokenKind.COMMA, true));
            closingCurly = eatToken(TokenKind.RCURLY);
            expr = new InlineList(t.startPos, closingCurly.endPos, elements.toArray(new SpelNodeImpl[0]));
        } else if (peekToken(TokenKind.COLON, true)) {
            // map!
            List<SpelNodeImpl> elements = new ArrayList<>();
            elements.add(firstExpression);
            elements.add(eatExpression());
            while (peekToken(TokenKind.COMMA, true)) {
                elements.add(eatExpression());
                eatToken(TokenKind.COLON);
                elements.add(eatExpression());
            }
            closingCurly = eatToken(TokenKind.RCURLY);
            expr = new InlineMap(t.startPos, closingCurly.endPos, elements.toArray(new SpelNodeImpl[0]));
        } else {
            throw internalException(t.startPos, SpelMessage.OOD);
        }
    }
    this.constructedNodes.push(expr);
    return true;
}
Also used : SpelNodeImpl(cn.taketoday.expression.spel.ast.SpelNodeImpl) ArrayList(java.util.ArrayList) InlineList(cn.taketoday.expression.spel.ast.InlineList) List(java.util.List) ArrayList(java.util.ArrayList) InlineList(cn.taketoday.expression.spel.ast.InlineList) InlineMap(cn.taketoday.expression.spel.ast.InlineMap)

Example 4 with InlineMap

use of cn.taketoday.expression.spel.ast.InlineMap in project today-framework by TAKETODAY.

the class MapTests method checkConstantMap.

private void checkConstantMap(String expressionText, boolean expectedToBeConstant) {
    SpelExpressionParser parser = new SpelExpressionParser();
    SpelExpression expression = (SpelExpression) parser.parseExpression(expressionText);
    SpelNode node = expression.getAST();
    boolean condition = node instanceof InlineMap;
    assertThat(condition).isTrue();
    InlineMap inlineMap = (InlineMap) node;
    if (expectedToBeConstant) {
        assertThat(inlineMap.isConstant()).isTrue();
    } else {
        assertThat(inlineMap.isConstant()).isFalse();
    }
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) InlineMap(cn.taketoday.expression.spel.ast.InlineMap)

Aggregations

InlineMap (cn.taketoday.expression.spel.ast.InlineMap)4 InlineList (cn.taketoday.expression.spel.ast.InlineList)2 SpelNodeImpl (cn.taketoday.expression.spel.ast.SpelNodeImpl)2 SpelExpression (cn.taketoday.expression.spel.standard.SpelExpression)2 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2