use of org.ballerinalang.model.tree.types.TypeNode in project ballerina by ballerina-lang.
the class BLangPackageBuilder method endTypeDefinition.
public void endTypeDefinition(DiagnosticPos pos, Set<Whitespace> ws, String identifier, boolean publicStruct) {
// TODO only adding object type for now
if (!this.objectStack.isEmpty()) {
BLangObject objectNode = (BLangObject) this.objectStack.pop();
objectNode.pos = pos;
objectNode.setName(this.createIdentifier(identifier));
if (publicStruct) {
objectNode.flagSet.add(Flag.PUBLIC);
}
objectNode.isAnonymous = false;
// Create an user defined type with object type
TypeNode objectType = createUserDefinedType(pos, ws, (BLangIdentifier) TreeBuilder.createIdentifierNode(), objectNode.name);
// Create and add receiver to attached functions
BLangVariable receiver = (BLangVariable) TreeBuilder.createVariableNode();
receiver.pos = pos;
IdentifierNode name = createIdentifier(Names.SELF.getValue());
receiver.setName(name);
receiver.addWS(ws);
receiver.docTag = DocTag.RECEIVER;
receiver.setTypeNode(objectType);
// Cache receiver to add to init function in symbolEnter
objectNode.receiver = receiver;
objectNode.functions.forEach(f -> f.setReceiver(receiver));
this.compUnit.addTopLevelNode(objectNode);
}
}
use of org.ballerinalang.model.tree.types.TypeNode in project ballerina by ballerina-lang.
the class Generator method generatePageForPrimitives.
/**
* Generate the page for primitive types.
* @param balPackage The ballerina.builtin package.
* @param packages List of available packages.
* @return A page model for the primitive types.
*/
public static Page generatePageForPrimitives(BLangPackage balPackage, List<Link> packages) {
ArrayList<Documentable> primitiveTypes = new ArrayList<>();
// Check for functions in the package
if (balPackage.getFunctions().size() > 0) {
for (BLangFunction function : balPackage.getFunctions()) {
if (function.getFlags().contains(Flag.PUBLIC) && function.getReceiver() != null) {
TypeNode langType = function.getReceiver().getTypeNode();
if (!(langType instanceof BLangUserDefinedType)) {
// Check for primitives in ballerina.builtin
Optional<PrimitiveTypeDoc> existingPrimitiveType = primitiveTypes.stream().filter((doc) -> doc instanceof PrimitiveTypeDoc && (((PrimitiveTypeDoc) doc)).name.equals(langType.toString())).map(doc -> (PrimitiveTypeDoc) doc).findFirst();
PrimitiveTypeDoc primitiveTypeDoc;
if (existingPrimitiveType.isPresent()) {
primitiveTypeDoc = existingPrimitiveType.get();
} else {
primitiveTypeDoc = new PrimitiveTypeDoc(langType.toString(), new ArrayList<>());
primitiveTypes.add(primitiveTypeDoc);
}
primitiveTypeDoc.children.add(createDocForNode(function));
}
}
}
}
// Create the links to select which page or package is active
List<Link> links = new ArrayList<>();
for (Link pkgLink : packages) {
if (BallerinaDocConstants.PRIMITIVE_TYPES_PAGE_NAME.equals(pkgLink.caption.value)) {
links.add(new Link(pkgLink.caption, pkgLink.href, true));
} else {
links.add(new Link(pkgLink.caption, pkgLink.href, false));
}
}
StaticCaption primitivesPageHeading = new StaticCaption(BallerinaDocConstants.PRIMITIVE_TYPES_PAGE_NAME);
return new Page(primitivesPageHeading, primitiveTypes, links);
}
use of org.ballerinalang.model.tree.types.TypeNode in project ballerina by ballerina-lang.
the class Generator method generatePage.
/**
* Generate the page when the bal package is passed.
* @param balPackage The current package that is being viewed.
* @param packages List of available packages.
* @return A page model for the current package.
*/
public static Page generatePage(BLangPackage balPackage, List<Link> packages) {
ArrayList<Documentable> documentables = new ArrayList<>();
String currentPackageName = (balPackage.symbol).pkgID.name.value;
// Check for structs in the package
if (balPackage.getStructs().size() > 0) {
for (BLangStruct struct : balPackage.getStructs()) {
if (struct.getFlags().contains(Flag.PUBLIC)) {
documentables.add(createDocForNode(struct));
}
}
}
// Check for functions in the package
if (balPackage.getFunctions().size() > 0) {
for (BLangFunction function : balPackage.getFunctions()) {
if (function.getFlags().contains(Flag.PUBLIC)) {
if (function.getReceiver() != null) {
if (documentables.size() > 0) {
for (Documentable parentDocumentable : documentables) {
TypeNode langType = function.getReceiver().getTypeNode();
String typeName = (langType instanceof BLangUserDefinedType ? ((BLangUserDefinedType) langType).typeName.value : langType.toString());
if (typeName.equals(parentDocumentable.name)) {
parentDocumentable.children.add(createDocForNode(function));
}
}
}
} else {
// If there's no receiver type i.e. no struct binding to the function
documentables.add(createDocForNode(function));
}
}
}
}
// Check for connectors in the package
for (BLangConnector connector : balPackage.getConnectors()) {
if (connector.getFlags().contains(Flag.PUBLIC)) {
documentables.add(createDocForNode(connector));
}
}
// Check for connectors in the package
for (EnumNode enumNode : balPackage.getEnums()) {
if (enumNode.getFlags().contains(Flag.PUBLIC)) {
documentables.add(createDocForNode(enumNode));
}
}
// Check for annotations
for (BLangAnnotation annotation : balPackage.getAnnotations()) {
if (annotation.getFlags().contains(Flag.PUBLIC)) {
documentables.add(createDocForNode(annotation));
}
}
// Check for global variables
for (BLangVariable var : balPackage.getGlobalVariables()) {
if (var.getFlags().contains(Flag.PUBLIC)) {
documentables.add(createDocForNode(var));
}
}
// Create the links to select which page or package is active
List<Link> links = new ArrayList<>();
PackageName packageNameHeading = null;
for (Link pkgLink : packages) {
if (pkgLink.caption.value.equals(currentPackageName)) {
packageNameHeading = (PackageName) pkgLink.caption;
links.add(new Link(pkgLink.caption, pkgLink.href, true));
} else {
links.add(new Link(pkgLink.caption, pkgLink.href, false));
}
}
return new Page(packageNameHeading, documentables, links);
}
use of org.ballerinalang.model.tree.types.TypeNode in project ballerina by ballerina-lang.
the class ParameterContextHolder method buildContext.
/**
* Build a readable parameter model from a Ballerina <code>VariableNode</code>.
*
* @param parameter {@code VariableNode} with parameter definition
* @return built Parameter context model
*/
public static ParameterContextHolder buildContext(VariableNode parameter) {
ParameterContextHolder context = new ParameterContextHolder();
TypeNode type = parameter.getTypeNode();
if (type instanceof BLangValueType) {
context.type = ((BLangValueType) parameter.getTypeNode()).getTypeKind().typeName();
} else if (type instanceof BLangUserDefinedType) {
context.type = ((BLangUserDefinedType) parameter.getTypeNode()).getTypeName().getValue();
}
// Ignore Connection and InRequest parameters
if (context.isIgnoredType(context.type)) {
return null;
}
context.name = parameter.getName().toString();
context.defaultValue = context.getDefaultValue(context.type, context.name);
// examples are not yet supported
context.example = "";
return context;
}
use of org.ballerinalang.model.tree.types.TypeNode in project ballerina by ballerina-lang.
the class ParserUtils method getReceiverType.
/**
* Extract receiverType from the function receiver.
*
* @param receiver receiver
* @return receiverType
*/
private static String getReceiverType(VariableNode receiver) {
if (receiver == null) {
return null;
}
TypeNode typeNode = receiver.getTypeNode();
String receiverType = null;
if (typeNode instanceof BLangUserDefinedType) {
receiverType = ((BLangUserDefinedType) typeNode).getTypeName().getValue();
} else if (typeNode instanceof BLangBuiltInRefTypeNode) {
receiverType = ((BLangBuiltInRefTypeNode) typeNode).getTypeKind().typeName();
} else if (typeNode instanceof BLangValueType) {
receiverType = ((BLangValueType) typeNode).getTypeKind().typeName();
} else {
return null;
}
return receiverType;
}
Aggregations