use of org.ballerinalang.model.tree.Node in project ballerina by ballerina-lang.
the class ModelGenerator method getContext.
public static JsonObject getContext() {
// Set alias for the classes
/*
alias.put("ConnectorInitExprNode", "ConnectorInitNode");
alias.put("XmlCommentLiteralNode", "XMLCommentLiteralNode");
alias.put("XmlElementLiteralNode", "XMLElementLiteralNode");
alias.put("XmlPiLiteralNode", "XMLProcessingInstructionLiteralNode");
alias.put("XmlTextLiteralNode", "XMLTextLiteralNode");
*/
alias.put("EnumeratorNode", "EnumNode");
alias.put("ImportNode", "ImportPackageNode");
alias.put("RecordLiteralKeyValueNode", "RecordKeyValueNode");
alias.put("XmlnsNode", "XMLNSDeclarationNode");
alias.put("ArrayLiteralExprNode", "ArrayLiteralNode");
alias.put("BinaryExprNode", "BinaryExpressionNode");
alias.put("TypeInitExprNode", "TypeInitNode");
alias.put("FieldBasedAccessExprNode", "FieldBasedAccessNode");
alias.put("IndexBasedAccessExprNode", "IndexBasedAccessNode");
alias.put("IntRangeExprNode", "IntRangeExpression");
alias.put("ActionInvocationNode", "");
alias.put("LambdaNode", "LambdaFunctionNode");
alias.put("RecordLiteralExprNode", "RecordLiteralNode");
alias.put("SimpleVariableRefNode", "SimpleVariableReferenceNode");
alias.put("TernaryExprNode", "TernaryExpressionNode");
alias.put("TypeCastExprNode", "TypeCastNode");
alias.put("TypeConversionExprNode", "TypeConversionNode");
alias.put("UnaryExprNode", "UnaryExpressionNode");
alias.put("XmlQnameNode", "XMLQNameNode");
alias.put("XmlAttributeNode", "XMLAttributeNode");
alias.put("XmlAttributeAccessExprNode", "IndexBasedAccessNode");
alias.put("XmlQuotedStringNode", "XMLQuotedStringNode");
alias.put("XmlElementLiteralNode", "XmlElementLiteralNode");
alias.put("XmlTextLiteralNode", "XMLTextLiteralNode");
alias.put("XmlCommentLiteralNode", "XmlCommentLiteralNode");
alias.put("XmlPiLiteralNode", "XMLProcessingInstructionLiteralNode");
// alias.put("TransformNode", ""); -- not used
alias.put("TryNode", "TryCatchFinallyNode");
alias.put("VariableDefNode", "VariableDefinitionNode");
alias.put("BuiltInRefTypeNode", "BuiltInReferenceTypeNode");
alias.put("EndpointTypeNode", "UserDefinedTypeNode");
alias.put("StreamletInitExprNode", "");
// alias.put("ActionInvocationNode", ""); not used
alias.put("RestArgsExprNode", "");
alias.put("NamedArgsExprNode", "");
alias.put("XmlElementLiteralNode", "");
alias.put("XmlCommentLiteralNode", "");
alias.put("TableQueryExpressionNode", "");
alias.put("MatchPatternClauseNode", "MatchStatementPatternNode");
alias.put("TransformNode", "");
alias.put("StreamNode", "");
alias.put("UnionTypeNodeNode", "UnionTypeNode");
alias.put("StreamingInputNode", "");
alias.put("JoinStreamingInputNode", "");
alias.put("TableQueryNode", "");
alias.put("SetAssignmentClauseNode", "");
alias.put("SetNode", "");
alias.put("StreamingQueryNode", "");
alias.put("QueryNode", "");
alias.put("StreamingQueryDeclarationNode", "");
alias.put("WithinNode", "");
alias.put("PatternClauseNode", "");
List<Class<?>> list = ModelGenerator.find("org.ballerinalang.model.tree");
NodeKind[] nodeKinds = NodeKind.class.getEnumConstants();
JsonObject nodes = new JsonObject();
for (NodeKind node : nodeKinds) {
String nodeKind = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, node.toString());
String nodeClassName = nodeKind + "Node";
try {
String actualClassName = (alias.get(nodeClassName) != null) ? alias.get(nodeClassName) : nodeClassName;
Class<?> clazz = list.stream().filter(nodeClass -> nodeClass.getSimpleName().equals(actualClassName)).findFirst().get();
JsonObject nodeObj = new JsonObject();
nodeObj.addProperty("kind", nodeKind);
nodeObj.addProperty("name", nodeClassName);
nodeObj.addProperty("fileName", CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, nodeClassName));
JsonArray attr = new JsonArray();
JsonArray bools = new JsonArray();
JsonArray imports = new JsonArray();
List<String> parents = Arrays.asList(clazz.getInterfaces()).stream().map(parent -> parent.getSimpleName()).collect(Collectors.toList());
// tag object with supper type
if (parents.contains("StatementNode")) {
nodeObj.addProperty("isStatement", true);
JsonObject imp = new JsonObject();
imp.addProperty("returnType", "StatementNode");
imp.addProperty("returnTypeFile", "statement-node");
imports.add(imp);
} else {
nodeObj.addProperty("isStatement", false);
}
if (parents.contains("ExpressionNode")) {
nodeObj.addProperty("isExpression", true);
JsonObject imp = new JsonObject();
imp.addProperty("returnType", "ExpressionNode");
imp.addProperty("returnTypeFile", "expression-node");
imports.add(imp);
} else {
nodeObj.addProperty("isExpression", false);
}
if (!parents.contains("StatementNode") && !parents.contains("ExpressionNode")) {
JsonObject imp = new JsonObject();
imp.addProperty("returnType", "Node");
imp.addProperty("returnTypeFile", "node");
imports.add(imp);
}
Method[] methods = clazz.getMethods();
for (Method m : methods) {
String methodName = m.getName();
if ("getKind".equals(methodName) || "getWS".equals(methodName) || "getPosition".equals(methodName)) {
continue;
}
if (methodName.startsWith("get")) {
JsonObject attribute = new JsonObject();
JsonObject imp = new JsonObject();
attribute.addProperty("property", toJsonName(m.getName(), 3));
attribute.addProperty("methodSuffix", m.getName().substring(3));
attribute.addProperty("list", List.class.isAssignableFrom(m.getReturnType()));
attribute.addProperty("isNode", Node.class.isAssignableFrom(m.getReturnType()));
if (Node.class.isAssignableFrom(m.getReturnType())) {
String returnClass = m.getReturnType().getSimpleName();
if (alias.containsValue(m.getReturnType().getSimpleName())) {
returnClass = getKindForAliasClass(m.getReturnType().getSimpleName());
}
imp.addProperty("returnType", returnClass);
attribute.addProperty("returnType", returnClass);
imp.addProperty("returnTypeFile", CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, returnClass));
if (!imports.contains(imp)) {
imports.add(imp);
}
}
attr.add(attribute);
}
if (methodName.startsWith("is")) {
JsonObject attribute = new JsonObject();
JsonObject imp = new JsonObject();
attribute.addProperty("property", toJsonName(m.getName(), 2));
attribute.addProperty("methodSuffix", m.getName().substring(2));
attribute.addProperty("list", List.class.isAssignableFrom(m.getReturnType()));
attribute.addProperty("isNode", Node.class.isAssignableFrom(m.getReturnType()));
if (Node.class.isAssignableFrom(m.getReturnType())) {
String returnClass = m.getReturnType().getSimpleName();
if (alias.containsValue(m.getReturnType().getSimpleName())) {
returnClass = getKindForAliasClass(m.getReturnType().getSimpleName());
}
imp.addProperty("returnType", returnClass);
attribute.addProperty("returnType", returnClass);
imp.addProperty("returnTypeFile", CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, returnClass));
if (!imports.contains(imp)) {
imports.add(imp);
}
}
bools.add(attribute);
}
}
nodeObj.add("attributes", attr);
nodeObj.add("bools", bools);
nodeObj.add("imports", imports);
nodes.add(nodeClassName, nodeObj);
} catch (NoSuchElementException e) {
out.println("alias.put(\"" + nodeClassName + "\", \"\");");
}
}
out.println(nodes);
return nodes;
}
use of org.ballerinalang.model.tree.Node in project ballerina by ballerina-lang.
the class TreeVisitor method visit.
public void visit(BLangConnector connectorNode) {
String connectorName = connectorNode.getName().getValue();
BSymbol connectorSymbol = connectorNode.symbol;
SymbolEnv connectorEnv = SymbolEnv.createConnectorEnv(connectorNode, connectorSymbol.scope, symbolEnv);
if (isWithinParameterContext(connectorName, NODE_TYPE_CONNECTOR)) {
this.populateSymbols(this.resolveAllVisibleSymbols(connectorEnv), connectorEnv);
setTerminateVisitor(true);
} else if (!ScopeResolverConstants.getResolverByClass(cursorPositionResolver).isCursorBeforeNode(connectorNode.getPosition(), connectorNode, this, this.documentServiceContext)) {
// Reset the previous node
this.setPreviousNode(null);
// TODO: Handle Annotation attachments
if (!(connectorNode.actions.isEmpty() && connectorNode.varDefs.isEmpty() && connectorNode.endpoints.isEmpty())) {
// Visit the endpoints
connectorNode.endpoints.forEach(bLangEndpoint -> this.acceptNode(bLangEndpoint, connectorEnv));
// Since the connector def does not contains a block statement, we consider the block owner only.
// Here it is Connector Definition
this.blockOwnerStack.push(connectorNode);
connectorNode.varDefs.forEach(varDef -> {
// Cursor position is calculated against the Connector scope resolver
cursorPositionResolver = ConnectorScopeResolver.class;
this.acceptNode(varDef, connectorEnv);
});
connectorNode.actions.forEach(action -> {
// Cursor position is calculated against the Connector scope resolver
cursorPositionResolver = ConnectorScopeResolver.class;
this.acceptNode(action, connectorEnv);
});
if (terminateVisitor) {
this.acceptNode(null, null);
}
this.blockOwnerStack.pop();
} else {
this.isCursorWithinBlock(connectorNode.getPosition(), connectorEnv);
}
}
}
use of org.ballerinalang.model.tree.Node 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.ballerinalang.model.tree.Node in project ballerina by ballerina-lang.
the class TreeVisitor method visit.
// Visitor methods
public void visit(BLangPackage pkgNode) {
SymbolEnv pkgEnv = this.symTable.pkgEnvMap.get(pkgNode.symbol);
// Then visit each top-level element sorted using the compilation unit
String fileName = documentServiceContext.get(DocumentServiceKeys.FILE_NAME_KEY);
List<TopLevelNode> topLevelNodes = pkgNode.topLevelNodes.stream().filter(node -> node.getPosition().getSource().getCompilationUnitName().equals(fileName)).collect(Collectors.toList());
if (topLevelNodes.isEmpty()) {
this.setTerminateVisitor(true);
acceptNode(null, null);
} else {
cursorPositionResolver = PackageNodeScopeResolver.class;
topLevelNodes.forEach(topLevelNode -> {
cursorPositionResolver = TopLevelNodeScopeResolver.class;
this.blockOwnerStack.push(pkgNode);
acceptNode((BLangNode) topLevelNode, pkgEnv);
});
}
}
use of org.ballerinalang.model.tree.Node in project ballerina by ballerina-lang.
the class BlockStatementScopeResolver method isWithinScopeAfterLastChildNode.
private boolean isWithinScopeAfterLastChildNode(TreeVisitor treeVisitor, boolean lastChild, int nodeELine, int nodeECol, int line, int col, Node node) {
if (!lastChild) {
return false;
} else {
BLangBlockStmt bLangBlockStmt = treeVisitor.getBlockStmtStack().peek();
Node blockOwner = treeVisitor.getBlockOwnerStack().peek();
int blockOwnerELine = this.getBlockOwnerELine(blockOwner, bLangBlockStmt);
int blockOwnerECol = this.getBlockOwnerECol(blockOwner, bLangBlockStmt);
boolean isWithinScope = (line < blockOwnerELine || (line == blockOwnerELine && col <= blockOwnerECol)) && (line > nodeELine || (line == nodeELine && col > nodeECol));
if (isWithinScope) {
treeVisitor.setPreviousNode((BLangNode) node);
}
return isWithinScope;
}
}
Aggregations