Search in sources :

Example 6 with BLangTransformer

use of org.wso2.ballerinalang.compiler.tree.BLangTransformer in project ballerina by ballerina-lang.

the class DocumentationTest method testDocTransformer.

@Test(description = "Test doc transformer.")
public void testDocTransformer() {
    CompileResult compileResult = BCompileUtil.compile("test-src/documentation/transformer.bal");
    Assert.assertEquals(0, compileResult.getWarnCount());
    PackageNode packageNode = compileResult.getAST();
    List<BLangDocumentation> docNodes = ((BLangTransformer) packageNode.getTransformers().get(0)).docAttachments;
    BLangDocumentation dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "\n" + " Transformer Foo Person -> Employee\n" + " ");
    Assert.assertEquals(dNode.getAttributes().size(), 3);
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "p");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " input struct Person source used for transformation\n ");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "e");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " output struct Employee struct which Person transformed to\n ");
    Assert.assertEquals(dNode.getAttributes().get(2).documentationField.getValue(), "defaultAddress");
    Assert.assertEquals(dNode.getAttributes().get(2).documentationText, " address which serves Eg: `POSTCODE 112`\n");
}
Also used : BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) CompileResult(org.ballerinalang.launcher.util.CompileResult) PackageNode(org.ballerinalang.model.tree.PackageNode) Test(org.testng.annotations.Test)

Example 7 with BLangTransformer

use of org.wso2.ballerinalang.compiler.tree.BLangTransformer in project ballerina by ballerina-lang.

the class SymbolEnter method defineTransformerMembers.

private void defineTransformerMembers(List<BLangTransformer> transformers, SymbolEnv pkgEnv) {
    transformers.forEach(transformer -> {
        SymbolEnv transformerEnv = SymbolEnv.createTransformerEnv(transformer, transformer.symbol.scope, pkgEnv);
        defineInvokableSymbolParams(transformer, transformer.symbol, transformerEnv);
    });
}
Also used : SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 8 with BLangTransformer

use of org.wso2.ballerinalang.compiler.tree.BLangTransformer in project ballerina by ballerina-lang.

the class SymbolEnter method validateTransformerMappingTypes.

private void validateTransformerMappingTypes(BLangTransformer transformerNode) {
    BType varType = symResolver.resolveTypeNode(transformerNode.source.typeNode, env);
    transformerNode.source.type = varType;
    transformerNode.retParams.forEach(returnParams -> {
        BType targetType = symResolver.resolveTypeNode(returnParams.typeNode, env);
        returnParams.type = targetType;
    });
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 9 with BLangTransformer

use of org.wso2.ballerinalang.compiler.tree.BLangTransformer in project ballerina by ballerina-lang.

the class CodeAnalyzer method visit.

public void visit(BLangTransformer transformerNode) {
    List<BVarSymbol> inputs = new ArrayList<>();
    inputs.add(transformerNode.source.symbol);
    transformerNode.requiredParams.forEach(param -> inputs.add(param.symbol));
    List<BVarSymbol> outputs = new ArrayList<>();
    transformerNode.retParams.forEach(param -> outputs.add(param.symbol));
    for (BLangStatement stmt : transformerNode.body.stmts) {
        switch(stmt.getKind()) {
            case VARIABLE_DEF:
                BLangVariableDef variableDefStmt = (BLangVariableDef) stmt;
                variableDefStmt.var.expr.accept(new TransformerVarRefValidator(outputs, DiagnosticCode.TRANSFORMER_INVALID_OUTPUT_USAGE));
                inputs.add(variableDefStmt.var.symbol);
                break;
            case ASSIGNMENT:
                BLangAssignment assignStmt = (BLangAssignment) stmt;
                assignStmt.varRefs.forEach(varRef -> {
                    varRef.accept(new TransformerVarRefValidator(inputs, DiagnosticCode.TRANSFORMER_INVALID_INPUT_UPDATE));
                    // If the stmt is declared using var, all the variable refs on lhs should be treated as inputs
                    if (assignStmt.declaredWithVar && varRef.getKind() == NodeKind.SIMPLE_VARIABLE_REF && !inputs.contains(((BLangSimpleVarRef) varRef).symbol)) {
                        inputs.add(((BLangSimpleVarRef) varRef).symbol);
                    }
                });
                assignStmt.expr.accept(new TransformerVarRefValidator(outputs, DiagnosticCode.TRANSFORMER_INVALID_OUTPUT_USAGE));
                break;
            case EXPRESSION_STATEMENT:
                // Here we have assumed that the invocation expression is the only expression-statement available.
                // TODO: support other types, once they are implemented.
                dlog.error(stmt.pos, DiagnosticCode.INVALID_STATEMENT_IN_TRANSFORMER, "invocation");
                break;
            default:
                dlog.error(stmt.pos, DiagnosticCode.INVALID_STATEMENT_IN_TRANSFORMER, stmt.getKind().name().toLowerCase().replace('_', ' '));
                break;
        }
    }
}
Also used : BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) ArrayList(java.util.ArrayList) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

Example 10 with BLangTransformer

use of org.wso2.ballerinalang.compiler.tree.BLangTransformer in project ballerina by ballerina-lang.

the class Desugar method addArgInitExpr.

private void addArgInitExpr(BLangTransformer transformerNode, BLangVariable var) {
    BLangSimpleVarRef varRef = new BLangLocalVarRef(var.symbol);
    varRef.lhsVar = true;
    varRef.pos = var.pos;
    varRef.type = var.type;
    BLangExpression initExpr = null;
    switch(var.type.tag) {
        case TypeTags.MAP:
            initExpr = new BLangMapLiteral(new ArrayList<>(), var.type);
            break;
        case TypeTags.JSON:
            initExpr = new BLangJSONLiteral(new ArrayList<>(), var.type);
            break;
        case TypeTags.STRUCT:
            initExpr = new BLangStructLiteral(new ArrayList<>(), var.type);
            break;
        case TypeTags.INT:
        case TypeTags.FLOAT:
        case TypeTags.STRING:
        case TypeTags.BOOLEAN:
        case TypeTags.BLOB:
        case TypeTags.XML:
            return;
        case TypeTags.TABLE:
        case TypeTags.STREAM:
            // TODO: add this once the able initializing is supported.
            return;
        default:
            return;
    }
    initExpr.pos = var.pos;
    BLangAssignment assignStmt = (BLangAssignment) TreeBuilder.createAssignmentNode();
    assignStmt.pos = var.pos;
    assignStmt.addVariable(varRef);
    assignStmt.expr = initExpr;
    transformerNode.body.stmts.add(0, assignStmt);
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangLocalVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangLocalVarRef) BLangStructLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStructLiteral) ArrayList(java.util.ArrayList) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangMapLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangMapLiteral) BLangJSONLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangJSONLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Aggregations

SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)7 BLangTransformer (org.wso2.ballerinalang.compiler.tree.BLangTransformer)6 ArrayList (java.util.ArrayList)4 BLangDocumentation (org.wso2.ballerinalang.compiler.tree.BLangDocumentation)4 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)4 CompileResult (org.ballerinalang.launcher.util.CompileResult)3 PackageNode (org.ballerinalang.model.tree.PackageNode)3 Test (org.testng.annotations.Test)3 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)3 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)2 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)2 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)2 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)2 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)2 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 DocumentServiceKeys (org.ballerinalang.langserver.DocumentServiceKeys)1