use of org.wso2.ballerinalang.compiler.tree.BLangPackage 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.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.
the class LauncherUtils method compile.
/**
* Compile and get the executable program file.
*
* @param sourceRootPath Path to the source root
* @param sourcePath Path to the source from the source root
* @param offline Should the build call remote repos
* @return Executable program
*/
public static ProgramFile compile(Path sourceRootPath, Path sourcePath, boolean offline) {
CompilerContext context = new CompilerContext();
CompilerOptions options = CompilerOptions.getInstance(context);
options.put(PROJECT_DIR, sourceRootPath.toString());
options.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString());
options.put(PRESERVE_WHITESPACE, "false");
options.put(OFFLINE, Boolean.toString(offline));
// compile
Compiler compiler = Compiler.getInstance(context);
BLangPackage entryPkgNode = compiler.compile(sourcePath.toString());
CompiledBinaryFile.ProgramFile programFile = compiler.getExecutableProgram(entryPkgNode);
if (programFile == null) {
throw createLauncherException("compilation contains errors");
}
ProgramFile progFile = getExecutableProgram(programFile);
progFile.setProgramFilePath(sourcePath);
return progFile;
}
use of org.wso2.ballerinalang.compiler.tree.BLangPackage 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.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.
the class BCompileUtil method compile.
public static CompileResult compile(String sourceRoot, String packageName, CompilerPhase compilerPhase, SourceDirectory sourceDirectory) {
CompilerContext context = new CompilerContext();
CompilerOptions options = CompilerOptions.getInstance(context);
options.put(PROJECT_DIR, sourceRoot);
options.put(COMPILER_PHASE, compilerPhase.toString());
options.put(PRESERVE_WHITESPACE, "false");
context.put(SourceDirectory.class, sourceDirectory);
CompileResult comResult = new CompileResult();
// catch errors
DiagnosticListener listener = comResult::addDiagnostic;
context.put(DiagnosticListener.class, listener);
// compile
Compiler compiler = Compiler.getInstance(context);
BLangPackage packageNode = compiler.compile(packageName);
comResult.setAST(packageNode);
CompiledBinaryFile.ProgramFile programFile = compiler.getExecutableProgram(packageNode);
if (programFile != null) {
comResult.setProgFile(LauncherUtils.getExecutableProgram(programFile));
}
return comResult;
}
use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.
the class TreeVisitor method visit.
// Visitor methods
public void visit(BLangPackage pkgNode) {
SymbolEnv pkgEnv = this.symTable.pkgEnvMap.get(pkgNode.symbol);
// Then visit each top-level element sorted using the compilation unit
String fileName = documentServiceContext.get(DocumentServiceKeys.FILE_NAME_KEY);
List<TopLevelNode> topLevelNodes = pkgNode.topLevelNodes.stream().filter(node -> node.getPosition().getSource().getCompilationUnitName().equals(fileName)).collect(Collectors.toList());
if (topLevelNodes.isEmpty()) {
this.setTerminateVisitor(true);
acceptNode(null, null);
} else {
cursorPositionResolver = PackageNodeScopeResolver.class;
topLevelNodes.forEach(topLevelNode -> {
cursorPositionResolver = TopLevelNodeScopeResolver.class;
this.blockOwnerStack.push(pkgNode);
acceptNode((BLangNode) topLevelNode, pkgEnv);
});
}
}
Aggregations