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");
}
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);
});
}
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;
});
}
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;
}
}
}
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);
}
Aggregations