Search in sources :

Example 1 with TypeNode

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);
    }
}
Also used : BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) IdentifierNode(org.ballerinalang.model.tree.IdentifierNode) BLangTupleTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangTupleTypeNode) TypeNode(org.ballerinalang.model.tree.types.TypeNode) BLangUnionTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangUnionTypeNode) BLangFunctionTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangFunctionTypeNode) BLangBuiltInRefTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangBuiltInRefTypeNode) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 2 with TypeNode

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);
}
Also used : AnnotationDoc(org.ballerinalang.docgen.model.AnnotationDoc) AnnotatableNode(org.ballerinalang.model.tree.AnnotatableNode) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) Link(org.ballerinalang.docgen.model.Link) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) Field(org.ballerinalang.docgen.model.Field) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) ArrayList(java.util.ArrayList) Page(org.ballerinalang.docgen.model.Page) Flag(org.ballerinalang.model.elements.Flag) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) EnumNode(org.ballerinalang.model.tree.EnumNode) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) FunctionDoc(org.ballerinalang.docgen.model.FunctionDoc) ConnectorDoc(org.ballerinalang.docgen.model.ConnectorDoc) EnumDoc(org.ballerinalang.docgen.model.EnumDoc) TypeNode(org.ballerinalang.model.tree.types.TypeNode) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangAnnotAttribute(org.wso2.ballerinalang.compiler.tree.BLangAnnotAttribute) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) ActionDoc(org.ballerinalang.docgen.model.ActionDoc) StaticCaption(org.ballerinalang.docgen.model.StaticCaption) List(java.util.List) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) Variable(org.ballerinalang.docgen.model.Variable) NodeKind(org.ballerinalang.model.tree.NodeKind) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) PackageName(org.ballerinalang.docgen.model.PackageName) Optional(java.util.Optional) Documentable(org.ballerinalang.docgen.model.Documentable) GlobalVariableDoc(org.ballerinalang.docgen.model.GlobalVariableDoc) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BallerinaDocConstants(org.ballerinalang.docgen.docs.BallerinaDocConstants) PrimitiveTypeDoc(org.ballerinalang.docgen.model.PrimitiveTypeDoc) StructDoc(org.ballerinalang.docgen.model.StructDoc) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) BLangType(org.wso2.ballerinalang.compiler.tree.types.BLangType) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) ArrayList(java.util.ArrayList) Page(org.ballerinalang.docgen.model.Page) StaticCaption(org.ballerinalang.docgen.model.StaticCaption) Documentable(org.ballerinalang.docgen.model.Documentable) TypeNode(org.ballerinalang.model.tree.types.TypeNode) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) PrimitiveTypeDoc(org.ballerinalang.docgen.model.PrimitiveTypeDoc) Link(org.ballerinalang.docgen.model.Link)

Example 3 with TypeNode

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);
}
Also used : BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) PackageName(org.ballerinalang.docgen.model.PackageName) ArrayList(java.util.ArrayList) EnumNode(org.ballerinalang.model.tree.EnumNode) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) Page(org.ballerinalang.docgen.model.Page) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) Documentable(org.ballerinalang.docgen.model.Documentable) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) TypeNode(org.ballerinalang.model.tree.types.TypeNode) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) Link(org.ballerinalang.docgen.model.Link)

Example 4 with TypeNode

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;
}
Also used : BLangValueType(org.wso2.ballerinalang.compiler.tree.types.BLangValueType) TypeNode(org.ballerinalang.model.tree.types.TypeNode) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType)

Example 5 with TypeNode

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;
}
Also used : BLangValueType(org.wso2.ballerinalang.compiler.tree.types.BLangValueType) BLangBuiltInRefTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangBuiltInRefTypeNode) BLangBuiltInRefTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangBuiltInRefTypeNode) TypeNode(org.ballerinalang.model.tree.types.TypeNode) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType)

Aggregations

TypeNode (org.ballerinalang.model.tree.types.TypeNode)5 BLangUserDefinedType (org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType)4 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)3 ArrayList (java.util.ArrayList)2 Documentable (org.ballerinalang.docgen.model.Documentable)2 Link (org.ballerinalang.docgen.model.Link)2 PackageName (org.ballerinalang.docgen.model.PackageName)2 Page (org.ballerinalang.docgen.model.Page)2 EnumNode (org.ballerinalang.model.tree.EnumNode)2 BLangAnnotation (org.wso2.ballerinalang.compiler.tree.BLangAnnotation)2 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)2 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)2 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)2 BLangValueType (org.wso2.ballerinalang.compiler.tree.types.BLangValueType)2 List (java.util.List)1 Optional (java.util.Optional)1 BallerinaDocConstants (org.ballerinalang.docgen.docs.BallerinaDocConstants)1 ActionDoc (org.ballerinalang.docgen.model.ActionDoc)1 AnnotationDoc (org.ballerinalang.docgen.model.AnnotationDoc)1 ConnectorDoc (org.ballerinalang.docgen.model.ConnectorDoc)1