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