Search in sources :

Example 11 with BLangEndpoint

use of org.wso2.ballerinalang.compiler.tree.BLangEndpoint in project ballerina by ballerina-lang.

the class TreeVisitor method visit.

public void visit(BLangService serviceNode) {
    if (!ScopeResolverConstants.getResolverByClass(cursorPositionResolver).isCursorBeforeNode(serviceNode.getPosition(), serviceNode, this, this.documentServiceContext)) {
        BSymbol serviceSymbol = serviceNode.symbol;
        SymbolEnv serviceEnv = SymbolEnv.createPkgLevelSymbolEnv(serviceNode, serviceSymbol.scope, symbolEnv);
        // Reset the previous node
        this.setPreviousNode(null);
        if (!(serviceNode.resources.isEmpty() && serviceNode.vars.isEmpty() && serviceNode.endpoints.isEmpty())) {
            // Visit the endpoints
            serviceNode.endpoints.forEach(bLangEndpoint -> this.acceptNode(bLangEndpoint, serviceEnv));
            // Since the service does not contains a block statement, we consider the block owner only.
            // Here it is service
            this.blockOwnerStack.push(serviceNode);
            serviceNode.vars.forEach(v -> {
                this.cursorPositionResolver = ServiceScopeResolver.class;
                this.acceptNode(v, serviceEnv);
            });
            serviceNode.resources.forEach(r -> {
                this.cursorPositionResolver = ServiceScopeResolver.class;
                this.acceptNode(r, serviceEnv);
            });
            if (terminateVisitor) {
                this.acceptNode(null, null);
            }
            this.blockOwnerStack.pop();
        } else {
            this.isCursorWithinBlock(serviceNode.getPosition(), serviceEnv);
        }
    }
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 12 with BLangEndpoint

use of org.wso2.ballerinalang.compiler.tree.BLangEndpoint in project kubernetes by ballerinax.

the class KubernetesPlugin method process.

@Override
public void process(EndpointNode endpointNode, List<AnnotationAttachmentNode> annotations) {
    String endpointName = endpointNode.getName().getValue();
    ServiceModel serviceModel = null;
    setCanProcess(true);
    for (AnnotationAttachmentNode attachmentNode : annotations) {
        String annotationKey = attachmentNode.getAnnotationName().getValue();
        try {
            switch(annotationKey) {
                case "Service":
                    serviceModel = kubernetesAnnotationProcessor.processServiceAnnotation(endpointName, attachmentNode);
                    kubernetesDataHolder.addServiceModel(endpointName, serviceModel);
                    break;
                case "Deployment":
                    kubernetesDataHolder.setDeploymentModel(kubernetesAnnotationProcessor.processDeployment(attachmentNode));
                    break;
                default:
                    break;
            }
        } catch (KubernetesPluginException e) {
            dlog.logDiagnostic(Diagnostic.Kind.ERROR, endpointNode.getPosition(), e.getMessage());
        }
    }
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangEndpoint) endpointNode).configurationExpr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        String key = keyValue.getKey().toString();
        switch(key) {
            case "port":
                int port = Integer.parseInt(keyValue.getValue().toString());
                kubernetesDataHolder.addPort(port);
                if (serviceModel != null) {
                    serviceModel.setPort(port);
                }
                break;
            case "secureSocket":
                List<BLangRecordLiteral.BLangRecordKeyValue> sslKeyValues = ((BLangRecordLiteral) keyValue.valueExpr).getKeyValuePairs();
                try {
                    Set<SecretModel> secretModels = kubernetesAnnotationProcessor.processSecureSocketAnnotation(endpointName, sslKeyValues);
                    kubernetesDataHolder.addEndpointSecret(endpointName, secretModels);
                    kubernetesDataHolder.addSecrets(secretModels);
                } catch (KubernetesPluginException e) {
                    dlog.logDiagnostic(Diagnostic.Kind.ERROR, null, e.getMessage());
                }
                break;
            default:
                break;
        }
    }
}
Also used : ServiceModel(org.ballerinax.kubernetes.models.ServiceModel) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) SecretModel(org.ballerinax.kubernetes.models.SecretModel) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 13 with BLangEndpoint

use of org.wso2.ballerinalang.compiler.tree.BLangEndpoint in project ballerina by ballerina-lang.

the class AnnotationDesugar method rewritePackageAnnotations.

protected void rewritePackageAnnotations(BLangPackage pkgNode) {
    BLangFunction initFunction = pkgNode.initFunction;
    // Remove last return statement. we will add it later. TODO : Fix this properly.
    initFunction.body.stmts.remove(initFunction.body.stmts.size() - 1);
    // This is the variable which store all package level annotations.
    BLangVariable annotationMap = createGlobalAnnotationMapVar(pkgNode);
    // handle Service Annotations.
    for (BLangService service : pkgNode.services) {
        generateAnnotations(service, service.name.value, initFunction, annotationMap);
        for (BLangResource resource : service.resources) {
            String key = service.name.value + DOT + resource.name.value;
            generateAnnotations(resource, key, initFunction, annotationMap);
        }
    }
    // Handle Function Annotations.
    for (BLangFunction function : pkgNode.functions) {
        generateAnnotations(function, function.symbol.name.value, initFunction, annotationMap);
    }
    // Handle Connector Annotations.
    for (BLangConnector connector : pkgNode.connectors) {
        generateAnnotations(connector, connector.name.value, initFunction, annotationMap);
        for (BLangAction action : connector.actions) {
            String key = connector.name.value + DOT + action.name.value;
            generateAnnotations(connector, key, initFunction, annotationMap);
        }
    }
    // Handle Struct Annotations.
    for (BLangStruct struct : pkgNode.structs) {
        generateAnnotations(struct, struct.name.value, initFunction, annotationMap);
        for (BLangVariable field : struct.fields) {
            String key = struct.name.value + DOT + field.name.value;
            generateAnnotations(field, key, initFunction, annotationMap);
        }
    }
    for (BLangEndpoint globalEndpoint : pkgNode.globalEndpoints) {
        generateAnnotations(globalEndpoint, globalEndpoint.name.value, initFunction, annotationMap);
    }
    ASTBuilderUtil.createReturnStmt(pkgNode.pos, initFunction.body);
}
Also used : BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction)

Example 14 with BLangEndpoint

use of org.wso2.ballerinalang.compiler.tree.BLangEndpoint in project ballerina by ballerina-lang.

the class EndpointDesugar method rewriteEndpoint.

void rewriteEndpoint(BLangEndpoint endpoint, SymbolEnv env) {
    final BSymbol encSymbol, varSymbol;
    final BLangBlockStmt initBlock, startBlock;
    BLangBlockStmt stopBlock = null;
    if (env.enclInvokable != null) {
        // Function, Action, Resource. Code generate to its body directly.
        encSymbol = varSymbol = env.enclInvokable.symbol;
        initBlock = startBlock = ((BLangInvokableNode) env.node).body;
    } else if (env.enclService != null) {
        encSymbol = env.enclService.symbol;
        varSymbol = ((BLangService) env.node).initFunction.symbol;
        initBlock = startBlock = ((BLangService) env.node).initFunction.body;
    } else {
        // Pkg level endpoint.
        encSymbol = env.enclPkg.symbol;
        varSymbol = ((BLangPackage) env.node).initFunction.symbol;
        initBlock = ((BLangPackage) env.node).initFunction.body;
        startBlock = ((BLangPackage) env.node).startFunction.body;
        stopBlock = ((BLangPackage) env.node).stopFunction.body;
    }
    BLangBlockStmt genInit, genInitCall, genStartCall, genStopCall;
    genInit = generateEndpointInit(endpoint, env, encSymbol);
    genInitCall = generateEndpointInitFunctionCall(endpoint, env, encSymbol, varSymbol);
    genStartCall = generateEndpointStartOrStop(endpoint, endpoint.symbol.startFunction, env, encSymbol);
    genStopCall = generateEndpointStartOrStop(endpoint, endpoint.symbol.stopFunction, env, encSymbol);
    if (env.enclInvokable != null) {
        ASTBuilderUtil.prependStatements(genStartCall, startBlock);
        ASTBuilderUtil.prependStatements(genInitCall, initBlock);
        // TODO : Implement stop.
        ASTBuilderUtil.prependStatements(genInit, initBlock);
    } else if (env.enclService != null) {
        ASTBuilderUtil.appendStatements(genInit, initBlock);
        ASTBuilderUtil.appendStatements(genInitCall, initBlock);
        ASTBuilderUtil.appendStatements(genStartCall, startBlock);
    // TODO : Implement stop.
    } else {
        ASTBuilderUtil.appendStatements(genInit, initBlock);
        ASTBuilderUtil.appendStatements(genInitCall, initBlock);
        ASTBuilderUtil.appendStatements(genStartCall, startBlock);
        ASTBuilderUtil.appendStatements(genStopCall, Objects.requireNonNull(stopBlock));
    }
}
Also used : BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangInvokableNode(org.wso2.ballerinalang.compiler.tree.BLangInvokableNode)

Example 15 with BLangEndpoint

use of org.wso2.ballerinalang.compiler.tree.BLangEndpoint in project ballerina by ballerina-lang.

the class TreeVisitor method visit.

public void visit(BLangFunction funcNode) {
    // Check for native functions
    BSymbol funcSymbol = funcNode.symbol;
    if (Symbols.isNative(funcSymbol)) {
        return;
    }
    String functionName = funcNode.getName().getValue();
    SymbolEnv funcEnv = SymbolEnv.createFunctionEnv(funcNode, funcSymbol.scope, symbolEnv);
    if (isWithinParameterContext(functionName, NODE_TYPE_FUNCTION)) {
        this.populateSymbols(this.resolveAllVisibleSymbols(funcEnv), funcEnv);
        setTerminateVisitor(true);
    } else if (!ScopeResolverConstants.getResolverByClass(cursorPositionResolver).isCursorBeforeNode(funcNode.getPosition(), funcNode, this, this.documentServiceContext)) {
        // Visit the endpoints
        funcNode.endpoints.forEach(bLangEndpoint -> this.acceptNode(bLangEndpoint, funcEnv));
        this.blockOwnerStack.push(funcNode);
        // Cursor position is calculated against the Block statement scope resolver
        cursorPositionResolver = BlockStatementScopeResolver.class;
        this.acceptNode(funcNode.body, funcEnv);
        this.blockOwnerStack.pop();
        // Process workers
        if (terminateVisitor && !funcNode.workers.isEmpty()) {
            this.setTerminateVisitor(false);
        }
        funcNode.workers.forEach(e -> this.symbolEnter.defineNode(e, funcEnv));
        funcNode.workers.forEach(e -> this.acceptNode(e, funcEnv));
    }
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) Arrays(java.util.Arrays) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) TokenStream(org.antlr.v4.runtime.TokenStream) Token(org.antlr.v4.runtime.Token) BLangWorkerReceive(org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerReceive) BLangBreak(org.wso2.ballerinalang.compiler.tree.statements.BLangBreak) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) LSNodeVisitor(org.ballerinalang.langserver.common.LSNodeVisitor) BLangTryCatchFinally(org.wso2.ballerinalang.compiler.tree.statements.BLangTryCatchFinally) Map(java.util.Map) ServiceScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ServiceScopeResolver) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode) SymbolResolver(org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolResolver) BLangWhile(org.wso2.ballerinalang.compiler.tree.statements.BLangWhile) Names(org.wso2.ballerinalang.compiler.util.Names) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) ConnectorScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ConnectorScopeResolver) BLangLock(org.wso2.ballerinalang.compiler.tree.statements.BLangLock) ScopeResolverConstants(org.ballerinalang.langserver.completions.util.ScopeResolverConstants) BallerinaParser(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ObjectTypeScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ObjectTypeScopeResolver) BLangXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS) Collectors(java.util.stream.Collectors) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangCatch(org.wso2.ballerinalang.compiler.tree.statements.BLangCatch) List(java.util.List) Scope(org.wso2.ballerinalang.compiler.semantics.model.Scope) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) ResourceParamScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ResourceParamScopeResolver) BlockStatementScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.BlockStatementScopeResolver) StatementNode(org.ballerinalang.model.tree.statements.StatementNode) BLangWorker(org.wso2.ballerinalang.compiler.tree.BLangWorker) BLangBind(org.wso2.ballerinalang.compiler.tree.statements.BLangBind) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) SymbolEnter(org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolEnter) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) PackageNodeScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.PackageNodeScopeResolver) BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) BLangAbort(org.wso2.ballerinalang.compiler.tree.statements.BLangAbort) HashMap(java.util.HashMap) BLangTransaction(org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction) Stack(java.util.Stack) ArrayList(java.util.ArrayList) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) Position(org.eclipse.lsp4j.Position) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangForkJoin(org.wso2.ballerinalang.compiler.tree.statements.BLangForkJoin) BLangEndpointTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangEndpointTypeNode) MatchStatementScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.MatchStatementScopeResolver) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) BLangNext(org.wso2.ballerinalang.compiler.tree.statements.BLangNext) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) BLangThrow(org.wso2.ballerinalang.compiler.tree.statements.BLangThrow) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) Name(org.wso2.ballerinalang.compiler.util.Name) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) TopLevelNodeScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.TopLevelNodeScopeResolver) BLangWorkerSend(org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerSend) Node(org.ballerinalang.model.tree.Node) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) Collections(java.util.Collections) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BlockStatementScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.BlockStatementScopeResolver) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Aggregations

BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)10 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)9 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)8 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)8 ArrayList (java.util.ArrayList)7 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)6 HashMap (java.util.HashMap)5 List (java.util.List)5 Map (java.util.Map)5 DocumentServiceKeys (org.ballerinalang.langserver.DocumentServiceKeys)5 TextDocumentServiceContext (org.ballerinalang.langserver.TextDocumentServiceContext)5 Position (org.eclipse.lsp4j.Position)5 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Stack (java.util.Stack)4 Collectors (java.util.stream.Collectors)4 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)4 Token (org.antlr.v4.runtime.Token)4 TokenStream (org.antlr.v4.runtime.TokenStream)4 LSNodeVisitor (org.ballerinalang.langserver.common.LSNodeVisitor)4