Search in sources :

Example 6 with CompilerOptions

use of org.wso2.ballerinalang.compiler.util.CompilerOptions 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;
}
Also used : Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) CompiledBinaryFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) DiagnosticListener(org.ballerinalang.util.diagnostic.DiagnosticListener) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Example 7 with CompilerOptions

use of org.wso2.ballerinalang.compiler.util.CompilerOptions 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());
}
Also used : Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) CompiledBinaryFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic) DiagnosticListener(org.ballerinalang.util.diagnostic.DiagnosticListener)

Example 8 with CompilerOptions

use of org.wso2.ballerinalang.compiler.util.CompilerOptions in project ballerina by ballerina-lang.

the class CommonUtil method prepareTempCompilerContext.

/**
 * Prepare a new compiler context.
 * @return {@link CompilerContext} Prepared compiler context
 */
public static CompilerContext prepareTempCompilerContext() {
    CompilerContext context = new CompilerContext();
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(PROJECT_DIR, "");
    options.put(COMPILER_PHASE, CompilerPhase.DESUGAR.toString());
    options.put(PRESERVE_WHITESPACE, "false");
    context.put(SourceDirectory.class, new NullSourceDirectory());
    return context;
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) NullSourceDirectory(org.ballerinalang.langserver.workspace.repository.NullSourceDirectory)

Example 9 with CompilerOptions

use of org.wso2.ballerinalang.compiler.util.CompilerOptions 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;
}
Also used : Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) CompiledBinaryFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Example 10 with CompilerOptions

use of org.wso2.ballerinalang.compiler.util.CompilerOptions 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)

Aggregations

CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)15 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)14 Compiler (org.wso2.ballerinalang.compiler.Compiler)9 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)5 DiagnosticListener (org.ballerinalang.util.diagnostic.DiagnosticListener)4 CompiledBinaryFile (org.wso2.ballerinalang.programfile.CompiledBinaryFile)4 Path (java.nio.file.Path)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)2 Diagnostic (org.ballerinalang.util.diagnostic.Diagnostic)2 CopyOption (java.nio.file.CopyOption)1 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)1 Files (java.nio.file.Files)1 LinkOption (java.nio.file.LinkOption)1 Paths (java.nio.file.Paths)1 StandardCopyOption (java.nio.file.StandardCopyOption)1 COMPILER_PHASE (org.ballerinalang.compiler.CompilerOptionName.COMPILER_PHASE)1 PRESERVE_WHITESPACE (org.ballerinalang.compiler.CompilerOptionName.PRESERVE_WHITESPACE)1 PROJECT_DIR (org.ballerinalang.compiler.CompilerOptionName.PROJECT_DIR)1