Search in sources :

Example 66 with Node

use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.

the class BLangPackageBuilder method addMatchStmtPattern.

public void addMatchStmtPattern(DiagnosticPos pos, Set<Whitespace> ws, String identifier) {
    BLangMatchStmtPatternClause patternClause = (BLangMatchStmtPatternClause) TreeBuilder.createMatchStatementPattern();
    patternClause.pos = pos;
    Set<Whitespace> varDefWS = removeNthFromStart(ws, 0);
    patternClause.addWS(ws);
    // Create a variable node
    identifier = identifier == null ? Names.IGNORE.value : identifier;
    BLangVariable var = (BLangVariable) TreeBuilder.createVariableNode();
    var.pos = pos;
    var.setName(this.createIdentifier(identifier));
    var.setTypeNode(this.typeNodeStack.pop());
    var.addWS(varDefWS);
    patternClause.variable = var;
    patternClause.body = (BLangBlockStmt) blockNodeStack.pop();
    patternClause.body.pos = pos;
    this.matchStmtStack.peekFirst().patternClauses.add(patternClause);
}
Also used : BLangMatchStmtPatternClause(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause) Whitespace(org.ballerinalang.model.Whitespace) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 67 with Node

use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.

the class PackageLoader method addImportPkg.

// Private methods
private void addImportPkg(BLangPackage bLangPackage, String orgName, String sourcePkgName, String version) {
    List<Name> nameComps = getPackageNameComps(sourcePkgName);
    List<BLangIdentifier> pkgNameComps = new ArrayList<>();
    nameComps.forEach(comp -> {
        IdentifierNode node = TreeBuilder.createIdentifierNode();
        node.setValue(comp.value);
        pkgNameComps.add((BLangIdentifier) node);
    });
    BLangIdentifier orgNameNode = (BLangIdentifier) TreeBuilder.createIdentifierNode();
    orgNameNode.setValue(orgName);
    BLangIdentifier versionNode = (BLangIdentifier) TreeBuilder.createIdentifierNode();
    versionNode.setValue(version);
    BLangImportPackage importDcl = (BLangImportPackage) TreeBuilder.createImportPackageNode();
    importDcl.pos = bLangPackage.pos;
    importDcl.pkgNameComps = pkgNameComps;
    importDcl.orgName = orgNameNode;
    importDcl.version = versionNode;
    BLangIdentifier alias = (BLangIdentifier) TreeBuilder.createIdentifierNode();
    alias.setValue(names.merge(Names.DOT, nameComps.get(nameComps.size() - 1)).value);
    importDcl.alias = alias;
    bLangPackage.imports.add(importDcl);
}
Also used : IdentifierNode(org.ballerinalang.model.tree.IdentifierNode) ArrayList(java.util.ArrayList) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 68 with Node

use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.

the class PackageLoader method genRepoHierarchy.

private RepoHierarchy genRepoHierarchy(Path sourceRoot) {
    Path balHomeDir = HomeRepoUtils.createAndGetHomeReposPath();
    Path projectHiddenDir = sourceRoot.resolve(".ballerina");
    RepoHierarchyBuilder.RepoNode[] systemArr = loadSystemRepos();
    Converter<Path> converter = sourceDirectory.getConverter();
    Repo remote = new RemoteRepo(URI.create("https://staging.central.ballerina.io:9090/"));
    Repo homeCacheRepo = new CacheRepo(balHomeDir);
    Repo homeRepo = new ZipRepo(balHomeDir);
    Repo projectCacheRepo = new CacheRepo(projectHiddenDir);
    Repo projectRepo = new ZipRepo(projectHiddenDir);
    RepoHierarchyBuilder.RepoNode homeCacheNode;
    if (offline) {
        homeCacheNode = node(homeCacheRepo, systemArr);
    } else {
        homeCacheNode = node(homeCacheRepo, node(remote, systemArr));
    }
    RepoHierarchyBuilder.RepoNode nonLocalRepos = node(projectRepo, node(projectCacheRepo, homeCacheNode), node(homeRepo, homeCacheNode));
    RepoHierarchyBuilder.RepoNode fullRepoGraph;
    if (converter != null) {
        Repo programingSource = new ProgramingSourceRepo(converter);
        Repo projectSource = new ProjectSourceRepo(converter);
        fullRepoGraph = node(programingSource, node(projectSource, nonLocalRepos));
    } else {
        fullRepoGraph = nonLocalRepos;
    }
    return RepoHierarchyBuilder.build(fullRepoGraph);
}
Also used : Path(java.nio.file.Path) ProgramingSourceRepo(org.wso2.ballerinalang.compiler.packaging.repo.ProgramingSourceRepo) ProjectSourceRepo(org.wso2.ballerinalang.compiler.packaging.repo.ProjectSourceRepo) RemoteRepo(org.wso2.ballerinalang.compiler.packaging.repo.RemoteRepo) Repo(org.wso2.ballerinalang.compiler.packaging.repo.Repo) ProgramingSourceRepo(org.wso2.ballerinalang.compiler.packaging.repo.ProgramingSourceRepo) ZipRepo(org.wso2.ballerinalang.compiler.packaging.repo.ZipRepo) CacheRepo(org.wso2.ballerinalang.compiler.packaging.repo.CacheRepo) RepoHierarchyBuilder(org.wso2.ballerinalang.compiler.packaging.RepoHierarchyBuilder) CacheRepo(org.wso2.ballerinalang.compiler.packaging.repo.CacheRepo) RemoteRepo(org.wso2.ballerinalang.compiler.packaging.repo.RemoteRepo) ZipRepo(org.wso2.ballerinalang.compiler.packaging.repo.ZipRepo) ProjectSourceRepo(org.wso2.ballerinalang.compiler.packaging.repo.ProjectSourceRepo)

Example 69 with Node

use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.

the class BCompileUtil method compileAndGetPackage.

/**
 * Compile and return the compiled package node.
 *
 * @param sourceFilePath Path to source package/file
 * @return compiled package node
 */
public static BLangPackage compileAndGetPackage(String sourceFilePath) {
    Path sourcePath = Paths.get(sourceFilePath);
    String packageName = sourcePath.getFileName().toString();
    Path sourceRoot = resourceDir.resolve(sourcePath.getParent());
    CompilerContext context = new CompilerContext();
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(PROJECT_DIR, resourceDir.resolve(sourceRoot).toString());
    options.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString());
    options.put(PRESERVE_WHITESPACE, "false");
    CompileResult comResult = new CompileResult();
    // catch errors
    DiagnosticListener listener = comResult::addDiagnostic;
    context.put(DiagnosticListener.class, listener);
    // compile
    Compiler compiler = Compiler.getInstance(context);
    return compiler.compile(packageName);
}
Also used : Path(java.nio.file.Path) Compiler(org.wso2.ballerinalang.compiler.Compiler) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) DiagnosticListener(org.ballerinalang.util.diagnostic.DiagnosticListener)

Example 70 with Node

use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.

the class TestAnnotationProcessor method checkCyclicDependencies.

private static int[] checkCyclicDependencies(List<Test> tests) {
    int numberOfNodes = tests.size();
    int[] indegrees = new int[numberOfNodes];
    int[] sortedElts = new int[numberOfNodes];
    List<Integer>[] dependencyMatrix = new ArrayList[numberOfNodes];
    for (int i = 0; i < numberOfNodes; i++) {
        dependencyMatrix[i] = new ArrayList<>();
    }
    List<String> testNames = tests.stream().map(k -> k.getTestName()).collect(Collectors.toList());
    int i = 0;
    for (Test test : tests) {
        if (!test.getDependsOnTestFunctions().isEmpty()) {
            for (String dependsOnFn : test.getDependsOnTestFunctions()) {
                int idx = testNames.indexOf(dependsOnFn);
                if (idx == -1) {
                    String message = String.format("Test [%s] depends on function [%s], but it couldn't be found" + ".", test.getTestFunction().getName(), dependsOnFn);
                    throw new BallerinaException(message);
                }
                dependencyMatrix[i].add(idx);
            }
        }
        i++;
    }
    // fill in degrees
    for (int j = 0; j < numberOfNodes; j++) {
        List<Integer> dependencies = dependencyMatrix[j];
        for (int node : dependencies) {
            indegrees[node]++;
        }
    }
    // Create a queue and enqueue all vertices with indegree 0
    Queue<Integer> q = new LinkedList<Integer>();
    for (i = 0; i < numberOfNodes; i++) {
        if (indegrees[i] == 0) {
            q.add(i);
        }
    }
    // Initialize count of visited vertices
    int cnt = 0;
    // Create a vector to store result (A topological ordering of the vertices)
    Vector<Integer> topOrder = new Vector<Integer>();
    while (!q.isEmpty()) {
        // Extract front of queue (or perform dequeue) and add it to topological order
        int u = q.poll();
        topOrder.add(u);
        // Iterate through all its neighbouring nodes of dequeued node u and decrease their in-degree by 1
        for (int node : dependencyMatrix[u]) {
            // If in-degree becomes zero, add it to queue
            if (--indegrees[node] == 0) {
                q.add(node);
            }
        }
        cnt++;
    }
    // Check if there was a cycle
    if (cnt != numberOfNodes) {
        String message = "Cyclic test dependency detected";
        throw new BallerinaException(message);
    }
    i = numberOfNodes - 1;
    for (int elt : topOrder) {
        sortedElts[i] = elt;
        i--;
    }
    return sortedElts;
}
Also used : Arrays(java.util.Arrays) PackageNode(org.ballerinalang.model.tree.PackageNode) BType(org.ballerinalang.model.types.BType) ProgramFile(org.ballerinalang.util.codegen.ProgramFile) TestSuite(org.ballerinalang.testerina.core.entity.TestSuite) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) DiagnosticLog(org.ballerinalang.util.diagnostic.DiagnosticLog) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) ArrayList(java.util.ArrayList) SupportedAnnotationPackages(org.ballerinalang.compiler.plugins.SupportedAnnotationPackages) AbstractCompilerPlugin(org.ballerinalang.compiler.plugins.AbstractCompilerPlugin) Vector(java.util.Vector) Map(java.util.Map) TesterinaFunction(org.ballerinalang.testerina.core.entity.TesterinaFunction) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) LinkedList(java.util.LinkedList) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) FunctionNode(org.ballerinalang.model.tree.FunctionNode) TypeTags(org.ballerinalang.model.types.TypeTags) Collectors(java.util.stream.Collectors) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) BArrayType(org.ballerinalang.model.types.BArrayType) List(java.util.List) Instruction(org.ballerinalang.util.codegen.Instruction) Test(org.ballerinalang.testerina.core.entity.Test) Queue(java.util.Queue) FunctionInfo(org.ballerinalang.util.codegen.FunctionInfo) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Test(org.ballerinalang.testerina.core.entity.Test) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) Vector(java.util.Vector)

Aggregations

ArrayList (java.util.ArrayList)19 Node (org.w3c.dom.Node)13 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)13 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)13 List (java.util.List)12 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)11 Map (java.util.Map)10 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)10 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)10 QName (javax.xml.namespace.QName)8 NodeList (org.w3c.dom.NodeList)8 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)8 Name (org.wso2.ballerinalang.compiler.util.Name)8 Arrays (java.util.Arrays)7 HashMap (java.util.HashMap)7 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)7 Node (org.ballerinalang.model.tree.Node)7 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)7 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)7 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)7