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();
}
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;
}
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;
}
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));
}
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);
}
Aggregations