Search in sources :

Example 1 with ConstructorReference

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

the class InternalSpelExpressionParser method maybeEatConstructorReference.

// constructor
// :	('new' qualifiedId LPAREN) => 'new' qualifiedId ctorArgs -> ^(CONSTRUCTOR qualifiedId ctorArgs)
private boolean maybeEatConstructorReference() {
    if (peekIdentifierToken("new")) {
        Token newToken = takeToken();
        // It looks like a constructor reference but is NEW being used as a map key?
        if (peekToken(TokenKind.RSQUARE)) {
            // looks like 'NEW]' (so NEW used as map key)
            push(new PropertyOrFieldReference(false, newToken.stringValue(), newToken.startPos, newToken.endPos));
            return true;
        }
        SpelNodeImpl possiblyQualifiedConstructorName = eatPossiblyQualifiedId();
        List<SpelNodeImpl> nodes = new ArrayList<>();
        nodes.add(possiblyQualifiedConstructorName);
        if (peekToken(TokenKind.LSQUARE)) {
            // array initializer
            var dimensions = new ArrayList<SpelNodeImpl>();
            while (peekToken(TokenKind.LSQUARE, true)) {
                if (!peekToken(TokenKind.RSQUARE)) {
                    dimensions.add(eatExpression());
                } else {
                    dimensions.add(null);
                }
                eatToken(TokenKind.RSQUARE);
            }
            if (maybeEatInlineListOrMap()) {
                nodes.add(pop());
            }
            push(new ConstructorReference(newToken.startPos, newToken.endPos, dimensions.toArray(new SpelNodeImpl[0]), nodes.toArray(new SpelNodeImpl[0])));
        } else {
            // regular constructor invocation
            eatConstructorArgs(nodes);
            // TODO correct end position?
            push(new ConstructorReference(newToken.startPos, newToken.endPos, nodes.toArray(new SpelNodeImpl[0])));
        }
        return true;
    }
    return false;
}
Also used : SpelNodeImpl(cn.taketoday.expression.spel.ast.SpelNodeImpl) ArrayList(java.util.ArrayList) PropertyOrFieldReference(cn.taketoday.expression.spel.ast.PropertyOrFieldReference) ConstructorReference(cn.taketoday.expression.spel.ast.ConstructorReference)

Example 2 with ConstructorReference

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

the class InternalSpelExpressionParser method maybeEatConstructorReference.

// constructor
// :	('new' qualifiedId LPAREN) => 'new' qualifiedId ctorArgs -> ^(CONSTRUCTOR qualifiedId ctorArgs)
private boolean maybeEatConstructorReference() {
    if (peekIdentifierToken("new")) {
        Token newToken = takeToken();
        // It looks like a constructor reference but is NEW being used as a map key?
        if (peekToken(TokenKind.RSQUARE)) {
            // looks like 'NEW]' (so NEW used as map key)
            push(new PropertyOrFieldReference(false, newToken.stringValue(), newToken.startPos, newToken.endPos));
            return true;
        }
        SpelNodeImpl possiblyQualifiedConstructorName = eatPossiblyQualifiedId();
        List<SpelNodeImpl> nodes = new ArrayList<>();
        nodes.add(possiblyQualifiedConstructorName);
        if (peekToken(TokenKind.LSQUARE)) {
            // array initializer
            var dimensions = new ArrayList<SpelNodeImpl>();
            while (peekToken(TokenKind.LSQUARE, true)) {
                if (!peekToken(TokenKind.RSQUARE)) {
                    dimensions.add(eatExpression());
                } else {
                    dimensions.add(null);
                }
                eatToken(TokenKind.RSQUARE);
            }
            if (maybeEatInlineListOrMap()) {
                nodes.add(pop());
            }
            push(new ConstructorReference(newToken.startPos, newToken.endPos, dimensions.toArray(new SpelNodeImpl[0]), nodes.toArray(new SpelNodeImpl[0])));
        } else {
            // regular constructor invocation
            eatConstructorArgs(nodes);
            // TODO correct end position?
            push(new ConstructorReference(newToken.startPos, newToken.endPos, nodes.toArray(new SpelNodeImpl[0])));
        }
        return true;
    }
    return false;
}
Also used : SpelNodeImpl(cn.taketoday.expression.spel.ast.SpelNodeImpl) ArrayList(java.util.ArrayList) PropertyOrFieldReference(cn.taketoday.expression.spel.ast.PropertyOrFieldReference) ConstructorReference(cn.taketoday.expression.spel.ast.ConstructorReference)

Aggregations

ConstructorReference (cn.taketoday.expression.spel.ast.ConstructorReference)2 PropertyOrFieldReference (cn.taketoday.expression.spel.ast.PropertyOrFieldReference)2 SpelNodeImpl (cn.taketoday.expression.spel.ast.SpelNodeImpl)2 ArrayList (java.util.ArrayList)2