Search in sources :

Example 31 with BLangBlockStmt

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

the class TreeVisitor method visit.

// Statements
@Override
public void visit(BLangBlockStmt blockNode) {
    SymbolEnv blockEnv = SymbolEnv.createBlockEnv(blockNode, symbolEnv);
    this.blockStmtStack.push(blockNode);
    // Cursor position is calculated against the Block statement scope resolver
    this.cursorPositionResolver = BlockStatementScopeResolver.class;
    // Reset the previous node to null
    this.setPreviousNode(null);
    if (blockNode.stmts.isEmpty()) {
        this.isCursorWithinBlock((DiagnosticPos) (this.blockOwnerStack.peek()).getPosition(), blockEnv);
    } else {
        blockNode.stmts.forEach(stmt -> this.acceptNode(stmt, blockEnv));
    }
    this.blockStmtStack.pop();
}
Also used : SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 32 with BLangBlockStmt

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

the class BlockStatementScopeResolver method isCursorBeforeNode.

/**
 * Check whether the cursor position is located before the evaluating statement node.
 * @param nodePosition position of the node
 * @param node statement being evaluated
 * @return true|false
 */
@Override
public boolean isCursorBeforeNode(DiagnosticPos nodePosition, Node node, TreeVisitor treeVisitor, TextDocumentServiceContext completionContext) {
    int line = completionContext.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine();
    int col = completionContext.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter();
    DiagnosticPos zeroBasedPos = CommonUtil.toZeroBasedPosition(nodePosition);
    int nodeSLine = zeroBasedPos.sLine;
    int nodeSCol = zeroBasedPos.sCol;
    // node endLine for the BLangIf node has to calculate by considering the else node. End line of the BLangIf
    // node is the endLine of the else node.
    int nodeELine = node instanceof BLangIf ? getIfElseNodeEndLine((BLangIf) node) : zeroBasedPos.eLine;
    int nodeECol = zeroBasedPos.eCol;
    BLangBlockStmt bLangBlockStmt = treeVisitor.getBlockStmtStack().peek();
    Node blockOwner = treeVisitor.getBlockOwnerStack().peek();
    boolean isLastStatement = this.isNodeLastStatement(bLangBlockStmt, blockOwner, node);
    boolean isWithinScopeAfterLastChild = this.isWithinScopeAfterLastChildNode(treeVisitor, isLastStatement, nodeELine, nodeECol, line, col, node);
    if (line < nodeSLine || (line == nodeSLine && col < nodeSCol) || isWithinScopeAfterLastChild) {
        Map<Name, Scope.ScopeEntry> visibleSymbolEntries = treeVisitor.resolveAllVisibleSymbols(treeVisitor.getSymbolEnv());
        treeVisitor.populateSymbols(visibleSymbolEntries, null);
        treeVisitor.setTerminateVisitor(true);
        return true;
    }
    return false;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) Node(org.ballerinalang.model.tree.Node) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 33 with BLangBlockStmt

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

the class ServiceProtoUtils method getInvocationExpression.

private static BLangInvocation getInvocationExpression(BlockNode body) {
    if (body == null) {
        return null;
    }
    for (StatementNode statementNode : body.getStatements()) {
        BLangExpression expression = null;
        // example : conn.send inside while block.
        if (statementNode instanceof BLangWhile) {
            BLangWhile langWhile = (BLangWhile) statementNode;
            BLangInvocation invocExp = getInvocationExpression(langWhile.getBody());
            if (invocExp != null) {
                return invocExp;
            }
        }
        // example : conn.send inside for block.
        if (statementNode instanceof BLangForeach) {
            BLangForeach langForeach = (BLangForeach) statementNode;
            BLangInvocation invocExp = getInvocationExpression(langForeach.getBody());
            if (invocExp != null) {
                return invocExp;
            }
        }
        // example : conn.send inside if block.
        if (statementNode instanceof BLangIf) {
            BLangIf langIf = (BLangIf) statementNode;
            BLangInvocation invocExp = getInvocationExpression(langIf.getBody());
            if (invocExp != null) {
                return invocExp;
            }
            invocExp = getInvocationExpression((BLangBlockStmt) langIf.getElseStatement());
            if (invocExp != null) {
                return invocExp;
            }
        }
        // example : _ = conn.send(msg);
        if (statementNode instanceof BLangAssignment) {
            BLangAssignment assignment = (BLangAssignment) statementNode;
            expression = assignment.getExpression();
        }
        // example : grpc:HttpConnectorError err = conn.send(msg);
        if (statementNode instanceof BLangVariableDef) {
            BLangVariableDef variableDef = (BLangVariableDef) statementNode;
            BLangVariable variable = variableDef.getVariable();
            expression = variable.getInitialExpression();
        }
        if (expression != null && expression instanceof BLangInvocation) {
            BLangInvocation invocation = (BLangInvocation) expression;
            if ("send".equals(invocation.getName().getValue())) {
                return invocation;
            }
        }
    }
    return null;
}
Also used : BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) StatementNode(org.ballerinalang.model.tree.statements.StatementNode) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) BLangWhile(org.wso2.ballerinalang.compiler.tree.statements.BLangWhile) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 34 with BLangBlockStmt

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

the class SemanticAnalyzer method visit.

// Statements
public void visit(BLangBlockStmt blockNode) {
    SymbolEnv blockEnv = SymbolEnv.createBlockEnv(blockNode, env);
    blockNode.stmts.forEach(stmt -> analyzeStmt(stmt, blockEnv));
}
Also used : SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 35 with BLangBlockStmt

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

the class SemanticAnalyzer method visit.

public void visit(BLangMatchStmtPatternClause patternClause) {
    // If the variable is not equal to '_', then define the variable in the block scope
    if (!patternClause.variable.name.value.endsWith(Names.IGNORE.value)) {
        SymbolEnv blockEnv = SymbolEnv.createBlockEnv((BLangBlockStmt) patternClause.body, env);
        symbolEnter.defineNode(patternClause.variable, blockEnv);
        analyzeStmt(patternClause.body, blockEnv);
        return;
    }
    symbolEnter.defineNode(patternClause.variable, this.env);
    analyzeStmt(patternClause.body, this.env);
}
Also used : SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Aggregations

BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)33 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)22 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)19 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)16 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)12 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)9 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)8 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)8 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)7 BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)6 BLangIndexBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess)6 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)6 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)6 Name (org.wso2.ballerinalang.compiler.util.Name)6 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)5 BLangMatchStmtPatternClause (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause)5 ArrayList (java.util.ArrayList)4 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)4 BLangForeach (org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)4 Whitespace (org.ballerinalang.model.Whitespace)3