Search in sources :

Example 6 with BJSONType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType in project ballerina by ballerina-lang.

the class TypeChecker method checkRecLiteralKeyValue.

private void checkRecLiteralKeyValue(BLangRecordKeyValue keyValuePair, BType recType) {
    BType fieldType = symTable.errType;
    BLangExpression valueExpr = keyValuePair.valueExpr;
    switch(recType.tag) {
        case TypeTags.STRUCT:
            fieldType = checkStructLiteralKeyExpr(keyValuePair.key, recType, RecordKind.STRUCT);
            break;
        case TypeTags.MAP:
            fieldType = checkMapLiteralKeyExpr(keyValuePair.key.expr, recType, RecordKind.MAP);
            break;
        case TypeTags.JSON:
            fieldType = checkJSONLiteralKeyExpr(keyValuePair.key, recType, RecordKind.JSON);
            // If the field is again a struct, treat that literal expression as another constraint JSON.
            if (fieldType.tag == TypeTags.STRUCT) {
                fieldType = new BJSONType(TypeTags.JSON, fieldType, symTable.jsonType.tsymbol);
            }
            // First visit the expression having field type, as the expected type.
            checkExpr(valueExpr, this.env, Lists.of(fieldType));
            // Again check the type compatibility with JSON
            if (valueExpr.impConversionExpr == null) {
                types.checkTypes(valueExpr, Lists.of(valueExpr.type), Lists.of(symTable.jsonType));
            } else {
                BType valueType = valueExpr.type;
                types.checkTypes(valueExpr, valueExpr.impConversionExpr.types, Lists.of(symTable.jsonType));
                valueExpr.type = valueType;
            }
            resultTypes = Lists.of(valueExpr.type);
            return;
    }
    checkExpr(valueExpr, this.env, Lists.of(fieldType));
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 7 with BJSONType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType in project ballerina by ballerina-lang.

the class ParserRuleMatchStatementContextResolver method resolveItems.

@Override
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
    ArrayList<CompletionItem> completionItems = new ArrayList<>();
    int currentTokenIndex = completionContext.get(DocumentServiceKeys.TOKEN_INDEX_KEY);
    List<SymbolInfo> visibleSymbols = completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
    TokenStream tokenStream = completionContext.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
    while (true) {
        if (currentTokenIndex < 0) {
            // Ideally should not come to this point
            return completionItems;
        }
        Token token = CommonUtil.getPreviousDefaultToken(tokenStream, currentTokenIndex);
        if (token.getText().equals(UtilSymbolKeys.MATCH_KEYWORD_KEY)) {
            currentTokenIndex = token.getTokenIndex();
            break;
        } else {
            currentTokenIndex = token.getTokenIndex();
        }
    }
    String identifierMatched = CommonUtil.getNextDefaultToken(tokenStream, currentTokenIndex).getText();
    SymbolInfo identifierSymbol = visibleSymbols.stream().filter(symbolInfo -> symbolInfo.getScopeEntry().symbol.getName().getValue().equals(identifierMatched)).findFirst().orElseGet(null);
    if (identifierSymbol == null) {
        return completionItems;
    } else if (identifierSymbol.getScopeEntry().symbol.type instanceof BUnionType) {
        Set<BType> memberTypes = ((BUnionType) identifierSymbol.getScopeEntry().symbol.type).getMemberTypes();
        memberTypes.forEach(bType -> {
            completionItems.add(this.populateCompletionItem(bType.toString(), ItemResolverConstants.B_TYPE, bType.toString()));
        });
    } else if (identifierSymbol.getScopeEntry().symbol.type instanceof BJSONType) {
        ArrayList<Integer> typeTagsList = new ArrayList<>(Arrays.asList(TypeTags.INT, TypeTags.FLOAT, TypeTags.BOOLEAN, TypeTags.STRING, TypeTags.NULL, TypeTags.JSON));
        List<SymbolInfo> filteredBasicTypes = visibleSymbols.stream().filter(symbolInfo -> {
            BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
            return bSymbol instanceof BTypeSymbol && typeTagsList.contains(bSymbol.getType().tag);
        }).collect(Collectors.toList());
        this.populateCompletionItemList(filteredBasicTypes, completionItems);
    } else if (identifierSymbol.getScopeEntry().symbol.type instanceof BStructType) {
        List<SymbolInfo> structSymbols = visibleSymbols.stream().filter(symbolInfo -> {
            BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
            return bSymbol instanceof BStructSymbol && !bSymbol.getName().getValue().startsWith(UtilSymbolKeys.ANON_STRUCT_CHECKER);
        }).collect(Collectors.toList());
        this.populateCompletionItemList(structSymbols, completionItems);
    }
    return completionItems;
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) Arrays(java.util.Arrays) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) TokenStream(org.antlr.v4.runtime.TokenStream) Token(org.antlr.v4.runtime.Token) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) Set(java.util.Set) ItemResolverConstants(org.ballerinalang.langserver.completions.util.ItemResolverConstants) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) CompletionItem(org.eclipse.lsp4j.CompletionItem) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) List(java.util.List) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) UtilSymbolKeys(org.ballerinalang.langserver.common.UtilSymbolKeys) CompletionKeys(org.ballerinalang.langserver.completions.CompletionKeys) AbstractItemResolver(org.ballerinalang.langserver.completions.resolvers.AbstractItemResolver) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) TokenStream(org.antlr.v4.runtime.TokenStream) Set(java.util.Set) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) ArrayList(java.util.ArrayList) Token(org.antlr.v4.runtime.Token) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) CompletionItem(org.eclipse.lsp4j.CompletionItem) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

BJSONType (org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType)7 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)7 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)2 CompletionItem (org.eclipse.lsp4j.CompletionItem)2 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)2 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)2 BMapType (org.wso2.ballerinalang.compiler.semantics.model.types.BMapType)2 BStructType (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType)2 BTableType (org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)2 BUnionType (org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType)2 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)2 Arrays (java.util.Arrays)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Token (org.antlr.v4.runtime.Token)1 TokenStream (org.antlr.v4.runtime.TokenStream)1 DocumentServiceKeys (org.ballerinalang.langserver.DocumentServiceKeys)1