Search in sources :

Example 1 with BJSONType

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

the class PackageActionFunctionAndTypesFilter method populateIterableOperations.

private void populateIterableOperations(SymbolInfo variable, List<SymbolInfo> symbolInfoList) {
    BType bType = variable.getScopeEntry().symbol.getType();
    if (bType instanceof BArrayType || bType instanceof BMapType || bType instanceof BJSONType || bType instanceof BXMLType || bType instanceof BTableType || bType instanceof BIntermediateCollectionType) {
        fillForeachIterableOperation(bType, symbolInfoList);
        fillMapIterableOperation(bType, symbolInfoList);
        fillFilterIterableOperation(bType, symbolInfoList);
        fillCountIterableOperation(symbolInfoList);
        if (bType instanceof BArrayType && (((BArrayType) bType).eType.toString().equals("int") || ((BArrayType) bType).eType.toString().equals("float"))) {
            fillMinIterableOperation(symbolInfoList);
            fillMaxIterableOperation(symbolInfoList);
            fillAverageIterableOperation(symbolInfoList);
            fillSumIterableOperation(symbolInfoList);
        }
    // TODO: Add support for Table and Tuple collection
    }
}
Also used : BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BXMLType(org.wso2.ballerinalang.compiler.semantics.model.types.BXMLType) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BIntermediateCollectionType(org.wso2.ballerinalang.compiler.semantics.model.types.BIntermediateCollectionType) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)

Example 2 with BJSONType

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

the class BLangMatchContextResolver method resolveItems.

@Override
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
    ArrayList<CompletionItem> completionItems = new ArrayList<>();
    BLangNode symbolEnvNode = completionContext.get(CompletionKeys.SYMBOL_ENV_NODE_KEY);
    List<SymbolInfo> visibleSymbols = completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
    if (!(symbolEnvNode instanceof BLangMatch)) {
        return completionItems;
    }
    BLangMatch bLangMatch = (BLangMatch) symbolEnvNode;
    if (bLangMatch.expr.type instanceof BUnionType) {
        Set<BType> memberTypes = ((BUnionType) ((BLangSimpleVarRef) bLangMatch.expr).type).getMemberTypes();
        memberTypes.forEach(bType -> {
            completionItems.add(this.populateCompletionItem(bType.toString(), ItemResolverConstants.B_TYPE, bType.toString()));
        });
    } else if (bLangMatch.expr.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 (bLangMatch.expr.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 : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) ArrayList(java.util.ArrayList) 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) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) CompletionItem(org.eclipse.lsp4j.CompletionItem) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with BJSONType

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

the class SymbolResolver method visit.

public void visit(BLangConstrainedType constrainedTypeNode) {
    BType type = resolveTypeNode(constrainedTypeNode.type, env);
    BType constraintType = resolveTypeNode(constrainedTypeNode.constraint, env);
    if (type.tag == TypeTags.TABLE) {
        resultType = new BTableType(TypeTags.TABLE, constraintType, type.tsymbol);
    } else if (type.tag == TypeTags.STREAM) {
        resultType = new BStreamType(TypeTags.STREAM, constraintType, type.tsymbol);
    } else if (type.tag == TypeTags.FUTURE) {
        resultType = new BFutureType(TypeTags.FUTURE, constraintType, type.tsymbol);
    } else if (type.tag == TypeTags.MAP) {
        resultType = new BMapType(TypeTags.MAP, constraintType, type.tsymbol);
    } else {
        if (!types.checkStructToJSONCompatibility(constraintType) && constraintType != symTable.errType) {
            dlog.error(constrainedTypeNode.pos, DiagnosticCode.INCOMPATIBLE_TYPE_CONSTRAINT, type, constraintType);
            resultType = symTable.errType;
            return;
        }
        resultType = new BJSONType(TypeTags.JSON, constraintType, type.tsymbol);
    }
}
Also used : BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType) BStreamType(org.wso2.ballerinalang.compiler.semantics.model.types.BStreamType) BFutureType(org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType)

Example 4 with BJSONType

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

the class TypeChecker method visit.

public void visit(BLangFieldBasedAccess fieldAccessExpr) {
    // First analyze the variable reference expression.
    BType actualType = symTable.errType;
    BType varRefType = getTypeOfExprInFieldAccess(fieldAccessExpr.expr);
    if (fieldAccessExpr.fieldType == FieldType.ALL && varRefType.tag != TypeTags.XML) {
        dlog.error(fieldAccessExpr.pos, DiagnosticCode.CANNOT_GET_ALL_FIELDS, varRefType);
    }
    Name fieldName = names.fromIdNode(fieldAccessExpr.field);
    switch(varRefType.tag) {
        case TypeTags.STRUCT:
            actualType = checkStructFieldAccess(fieldAccessExpr, fieldName, varRefType);
            break;
        case TypeTags.MAP:
            actualType = ((BMapType) varRefType).getConstraint();
            break;
        case TypeTags.JSON:
            BType constraintType = ((BJSONType) varRefType).constraint;
            if (constraintType.tag == TypeTags.STRUCT) {
                BType fieldType = checkStructFieldAccess(fieldAccessExpr, fieldName, constraintType);
                // If the type of the field is struct, treat it as constraint JSON type.
                if (fieldType.tag == TypeTags.STRUCT) {
                    actualType = new BJSONType(TypeTags.JSON, fieldType, symTable.jsonType.tsymbol);
                    break;
                }
            }
            actualType = symTable.jsonType;
            break;
        case TypeTags.ENUM:
            // Enumerator access expressions only allow enum type name as the first part e.g state.INSTALLED,
            BEnumType enumType = (BEnumType) varRefType;
            if (fieldAccessExpr.expr.getKind() != NodeKind.SIMPLE_VARIABLE_REF || !((BLangSimpleVarRef) fieldAccessExpr.expr).variableName.value.equals(enumType.tsymbol.name.value)) {
                dlog.error(fieldAccessExpr.pos, DiagnosticCode.INVALID_ENUM_EXPR, enumType.tsymbol.name.value);
                break;
            }
            BSymbol symbol = symResolver.lookupMemberSymbol(fieldAccessExpr.pos, enumType.tsymbol.scope, this.env, fieldName, SymTag.VARIABLE);
            if (symbol == symTable.notFoundSymbol) {
                dlog.error(fieldAccessExpr.pos, DiagnosticCode.UNDEFINED_SYMBOL, fieldName.value);
                break;
            }
            fieldAccessExpr.symbol = (BVarSymbol) symbol;
            actualType = fieldAccessExpr.expr.type;
            break;
        case TypeTags.XML:
            if (fieldAccessExpr.lhsVar) {
                dlog.error(fieldAccessExpr.pos, DiagnosticCode.CANNOT_UPDATE_XML_SEQUENCE);
                break;
            }
            actualType = symTable.xmlType;
            break;
        case TypeTags.ERROR:
            // Do nothing
            break;
        default:
            dlog.error(fieldAccessExpr.pos, DiagnosticCode.OPERATION_DOES_NOT_SUPPORT_FIELD_ACCESS, varRefType);
    }
    resultTypes = types.checkTypes(fieldAccessExpr, Lists.of(actualType), this.expTypes);
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name) BEnumType(org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType)

Example 5 with BJSONType

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

the class TypeChecker method visit.

public void visit(BLangIndexBasedAccess indexBasedAccessExpr) {
    BType actualType = symTable.errType;
    // First analyze the variable reference expression.
    checkExpr(indexBasedAccessExpr.expr, this.env, Lists.of(symTable.noType));
    BType indexExprType;
    BType varRefType = indexBasedAccessExpr.expr.type;
    BLangExpression indexExpr = indexBasedAccessExpr.indexExpr;
    switch(varRefType.tag) {
        case TypeTags.STRUCT:
            indexExprType = checkIndexExprForStructFieldAccess(indexExpr);
            if (indexExprType.tag == TypeTags.STRING) {
                String fieldName = (String) ((BLangLiteral) indexExpr).value;
                actualType = checkStructFieldAccess(indexBasedAccessExpr, names.fromString(fieldName), varRefType);
            }
            break;
        case TypeTags.MAP:
            indexExprType = checkExpr(indexExpr, this.env, Lists.of(symTable.stringType)).get(0);
            if (indexExprType.tag == TypeTags.STRING) {
                actualType = ((BMapType) varRefType).getConstraint();
            }
            break;
        case TypeTags.JSON:
            BType constraintType = ((BJSONType) varRefType).constraint;
            if (constraintType.tag == TypeTags.STRUCT) {
                indexExprType = checkIndexExprForStructFieldAccess(indexExpr);
                if (indexExprType.tag != TypeTags.STRING) {
                    break;
                }
                String fieldName = (String) ((BLangLiteral) indexExpr).value;
                BType fieldType = checkStructFieldAccess(indexBasedAccessExpr, names.fromString(fieldName), constraintType);
                // If the type of the field is struct, treat it as constraint JSON type.
                if (fieldType.tag == TypeTags.STRUCT) {
                    actualType = new BJSONType(TypeTags.JSON, fieldType, symTable.jsonType.tsymbol);
                    break;
                }
            } else {
                indexExprType = checkExpr(indexExpr, this.env, Lists.of(symTable.noType)).get(0);
                if (indexExprType.tag != TypeTags.STRING && indexExprType.tag != TypeTags.INT) {
                    dlog.error(indexExpr.pos, DiagnosticCode.INCOMPATIBLE_TYPES, symTable.stringType, indexExprType);
                    break;
                }
            }
            actualType = symTable.jsonType;
            break;
        case TypeTags.ARRAY:
            indexExprType = checkExpr(indexExpr, this.env, Lists.of(symTable.intType)).get(0);
            if (indexExprType.tag == TypeTags.INT) {
                actualType = ((BArrayType) varRefType).getElementType();
            }
            break;
        case TypeTags.XML:
            if (indexBasedAccessExpr.lhsVar) {
                dlog.error(indexBasedAccessExpr.pos, DiagnosticCode.CANNOT_UPDATE_XML_SEQUENCE);
                break;
            }
            checkExpr(indexExpr, this.env).get(0);
            actualType = symTable.xmlType;
            break;
        case TypeTags.ERROR:
            // Do nothing
            break;
        default:
            dlog.error(indexBasedAccessExpr.pos, DiagnosticCode.OPERATION_DOES_NOT_SUPPORT_INDEXING, indexBasedAccessExpr.expr.type);
    }
    resultTypes = types.checkTypes(indexBasedAccessExpr, Lists.of(actualType), this.expTypes);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

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