use of org.ballerinalang.util.diagnostic.DiagnosticListener in project ballerina by ballerina-lang.
the class BCompileUtil method compile.
/**
* Compile and return the semantic errors.
*
* @param sourceRoot root path of the source packages
* @param packageName name of the package to compile
* @param compilerPhase Compiler phase
* @return Semantic errors
*/
public static CompileResult compile(String sourceRoot, String packageName, CompilerPhase compilerPhase) {
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");
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);
if (comResult.getErrorCount() > 0 || CompilerPhase.CODE_GEN.compareTo(compilerPhase) > 0) {
return comResult;
}
CompiledBinaryFile.ProgramFile programFile = compiler.getExecutableProgram(packageNode);
if (programFile != null) {
ProgramFile pFile = LauncherUtils.getExecutableProgram(programFile);
comResult.setProgFile(pFile);
if (pFile != null) {
boolean distributedTxEnabled = CompilerUtils.isDistributedTransactionsEnabled();
pFile.setDistributedTransactionEnabled(distributedTxEnabled);
}
}
return comResult;
}
use of org.ballerinalang.util.diagnostic.DiagnosticListener in project ballerina by ballerina-lang.
the class BCompileUtil method getDiagnostics.
/**
* Used by IntelliJ IDEA plugin to provide semantic analyzing capability.
*
* @param classLoader a {@link ClassLoader} to be set as thread context class loader. This is used by {@link
* java.util.ServiceLoader}. Otherwise semantic analyzing capability providing wont work since it
* cant find core package.
* @param sourceRoot source root of a project
* @param fileName either the file name (if in project root) or the package name
* @return list of diagnostics
*/
public static List<Diagnostic> getDiagnostics(ClassLoader classLoader, String sourceRoot, String fileName) {
Thread.currentThread().setContextClassLoader(classLoader);
CompilerContext context = new CompilerContext();
CompilerOptions options = CompilerOptions.getInstance(context);
options.put(PROJECT_DIR, sourceRoot);
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);
BLangPackage entryPackageNode = compiler.compile(fileName);
CompiledBinaryFile.ProgramFile programFile = compiler.getExecutableProgram(entryPackageNode);
if (programFile != null) {
comResult.setProgFile(LauncherUtils.getExecutableProgram(programFile));
}
Diagnostic[] diagnostics = comResult.getDiagnostics();
return Arrays.stream(diagnostics).collect(Collectors.toList());
}
use of org.ballerinalang.util.diagnostic.DiagnosticListener 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.ballerinalang.util.diagnostic.DiagnosticListener 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;
}
Aggregations