Search in sources :

Example 41 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.

the class BLangPackageBuilder method addVarToStruct.

public void addVarToStruct(DiagnosticPos pos, Set<Whitespace> ws, String identifier, boolean exprAvailable, int annotCount, boolean isPrivate) {
    Set<Whitespace> wsForSemiColon = removeNthFromLast(ws, 0);
    BLangStruct structNode = (BLangStruct) this.structStack.peek();
    structNode.addWS(wsForSemiColon);
    BLangVariable field = addVar(pos, ws, identifier, exprAvailable, annotCount);
    if (!isPrivate) {
        field.flagSet.add(Flag.PUBLIC);
    }
}
Also used : BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) Whitespace(org.ballerinalang.model.Whitespace) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 42 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.

the class TreeVisitor method visit.

public void visit(BLangStruct structNode) {
    if (!ScopeResolverConstants.getResolverByClass(cursorPositionResolver).isCursorBeforeNode(structNode.getPosition(), structNode, this, this.documentServiceContext)) {
        BSymbol structSymbol = structNode.symbol;
        SymbolEnv structEnv = SymbolEnv.createPkgLevelSymbolEnv(structNode, structSymbol.scope, symbolEnv);
        if (structNode.fields.isEmpty() && isCursorWithinBlock(structNode.getPosition(), structEnv)) {
            symbolEnv = structEnv;
            Map<Name, Scope.ScopeEntry> visibleSymbolEntries = this.resolveAllVisibleSymbols(symbolEnv);
            this.populateSymbols(visibleSymbolEntries, null);
            this.setTerminateVisitor(true);
        } else if (!structNode.fields.isEmpty()) {
            // Since the struct definition do not have a block statement within, we push null
            this.blockStmtStack.push(null);
            this.blockOwnerStack.push(structNode);
            // Cursor position is calculated against the Block statement scope resolver
            this.cursorPositionResolver = BlockStatementScopeResolver.class;
            structNode.fields.forEach(field -> acceptNode(field, structEnv));
            this.blockStmtStack.pop();
            this.blockOwnerStack.pop();
        }
    }
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) Arrays(java.util.Arrays) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) TokenStream(org.antlr.v4.runtime.TokenStream) Token(org.antlr.v4.runtime.Token) BLangWorkerReceive(org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerReceive) BLangBreak(org.wso2.ballerinalang.compiler.tree.statements.BLangBreak) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) LSNodeVisitor(org.ballerinalang.langserver.common.LSNodeVisitor) BLangTryCatchFinally(org.wso2.ballerinalang.compiler.tree.statements.BLangTryCatchFinally) Map(java.util.Map) ServiceScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ServiceScopeResolver) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode) SymbolResolver(org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolResolver) BLangWhile(org.wso2.ballerinalang.compiler.tree.statements.BLangWhile) Names(org.wso2.ballerinalang.compiler.util.Names) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) ConnectorScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ConnectorScopeResolver) BLangLock(org.wso2.ballerinalang.compiler.tree.statements.BLangLock) ScopeResolverConstants(org.ballerinalang.langserver.completions.util.ScopeResolverConstants) BallerinaParser(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ObjectTypeScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ObjectTypeScopeResolver) BLangXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS) Collectors(java.util.stream.Collectors) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangCatch(org.wso2.ballerinalang.compiler.tree.statements.BLangCatch) List(java.util.List) Scope(org.wso2.ballerinalang.compiler.semantics.model.Scope) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) ResourceParamScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ResourceParamScopeResolver) BlockStatementScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.BlockStatementScopeResolver) StatementNode(org.ballerinalang.model.tree.statements.StatementNode) BLangWorker(org.wso2.ballerinalang.compiler.tree.BLangWorker) BLangBind(org.wso2.ballerinalang.compiler.tree.statements.BLangBind) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) SymbolEnter(org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolEnter) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) PackageNodeScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.PackageNodeScopeResolver) BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) BLangAbort(org.wso2.ballerinalang.compiler.tree.statements.BLangAbort) HashMap(java.util.HashMap) BLangTransaction(org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction) Stack(java.util.Stack) ArrayList(java.util.ArrayList) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) Position(org.eclipse.lsp4j.Position) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangForkJoin(org.wso2.ballerinalang.compiler.tree.statements.BLangForkJoin) BLangEndpointTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangEndpointTypeNode) MatchStatementScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.MatchStatementScopeResolver) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) BLangNext(org.wso2.ballerinalang.compiler.tree.statements.BLangNext) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) BLangThrow(org.wso2.ballerinalang.compiler.tree.statements.BLangThrow) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) Name(org.wso2.ballerinalang.compiler.util.Name) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) TopLevelNodeScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.TopLevelNodeScopeResolver) BLangWorkerSend(org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerSend) Node(org.ballerinalang.model.tree.Node) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) Collections(java.util.Collections) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BlockStatementScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.BlockStatementScopeResolver) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 43 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.

the class Generator method createDocForNode.

/**
 * Create documentation for structs.
 * @param structNode ballerina struct node.
 * @return documentation for structs.
 */
public static StructDoc createDocForNode(BLangStruct structNode) {
    String structName = structNode.getName().value;
    // Check if its an anonymous struct
    if (structName.contains(ANONYMOUS_STRUCT)) {
        structName = "Anonymous Struct";
    }
    List<Field> fields = new ArrayList<>();
    // Iterate through the struct fields
    if (structNode.getFields().size() > 0) {
        for (BLangVariable param : structNode.getFields()) {
            String dataType = type(param);
            String desc = fieldAnnotation(structNode, param);
            String defaultValue = "";
            if (null != param.getInitialExpression()) {
                defaultValue = param.getInitialExpression().toString();
            }
            Field variable = new Field(param.getName().value, dataType, desc, defaultValue);
            fields.add(variable);
        }
    }
    return new StructDoc(structName, description(structNode), new ArrayList<>(), fields);
}
Also used : Field(org.ballerinalang.docgen.model.Field) ArrayList(java.util.ArrayList) StructDoc(org.ballerinalang.docgen.model.StructDoc) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 44 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.

the class HtmlDocTest method testEnumPropertiesExtracted.

@Test(description = "Enum properties should be available via construct")
public void testEnumPropertiesExtracted() throws Exception {
    BLangPackage bLangPackage = createPackage("package x.y; " + "@Description { value:\"The direction of the parameter\"}\n" + "@Field { value:\"IN: IN parameters are used to send values to stored procedures\"}\n" + "@Field { value:\"OUT: OUT parameters are used to get values from stored procedures\"}\n" + "public enum Direction { IN,OUT}");
    EnumDoc enumDoc = Generator.createDocForNode(bLangPackage.getEnums().get(0));
    Assert.assertEquals(enumDoc.name, "Direction", "Enum name should be extracted");
    Assert.assertEquals(enumDoc.description, "The direction of the parameter", "Description of the " + "enum should be extracted");
    // Enumerators inside the enum
    Assert.assertEquals(enumDoc.enumerators.get(0).name, "IN", "Enumerator name should be extracted");
    Assert.assertEquals(enumDoc.enumerators.get(0).description, "IN parameters are used to send values to " + "stored procedures", "Description of the enumerator should be extracted");
}
Also used : EnumDoc(org.ballerinalang.docgen.model.EnumDoc) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Test(org.testng.annotations.Test)

Example 45 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.

the class HtmlDocTest method testStructPropertiesExtracted.

@Test(description = "Struct properties should be available via construct")
public void testStructPropertiesExtracted() throws Exception {
    BLangPackage bLangPackage = createPackage("package x.y; " + "@Description { value:\"Message sent by the client\"}" + "@Field {value:\"count: Number of retries\"}\n" + "@Field {value:\"interval: Retry interval in millisecond\"}" + "struct Message {int interval;int count;}");
    StructDoc structDoc = Generator.createDocForNode(bLangPackage.getStructs().get(0));
    Assert.assertEquals(structDoc.name, "Message", "Struct name should be extracted");
    Assert.assertEquals(structDoc.description, "Message sent by the client", "Description of the " + "struct should be extracted");
    // Struct fields
    Assert.assertEquals(structDoc.fields.get(0).name, "interval", "Struct field name should be extracted");
    Assert.assertEquals(structDoc.fields.get(0).dataType, "int", "Struct field type should be extracted");
    Assert.assertEquals(structDoc.fields.get(0).description, "Retry interval in millisecond", "Description of the struct field should be extracted");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) StructDoc(org.ballerinalang.docgen.model.StructDoc) Test(org.testng.annotations.Test)

Aggregations

BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)14 Test (org.testng.annotations.Test)11 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)10 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)10 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)10 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)9 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)8 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)8 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)8 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)8 Name (org.wso2.ballerinalang.compiler.util.Name)8 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)7 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)7 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)7 CompileResult (org.ballerinalang.launcher.util.CompileResult)6 PackageNode (org.ballerinalang.model.tree.PackageNode)6 BLangDocumentation (org.wso2.ballerinalang.compiler.tree.BLangDocumentation)6 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)5 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)5 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)5