Search in sources :

Example 11 with CompilerOptions

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

Example 12 with CompilerOptions

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

the class BallerinaDocGenerator method generatePackageDocsFromBallerina.

/**
 * Generates {@link BLangPackage} objects for each Ballerina package from the given ballerina files.
 *
 * @param sourceRoot    points to the folder relative to which package path is given
 * @param packagePath   a {@link Path} object pointing either to a ballerina file or a folder with ballerina files.
 * @param packageFilter comma separated list of package names/patterns to be filtered from the documentation.
 * @param isNative      whether the given packages are native or not.
 * @return a map of {@link BLangPackage} objects. Key - Ballerina package name Value - {@link BLangPackage}
 */
protected static Map<String, BLangPackage> generatePackageDocsFromBallerina(String sourceRoot, Path packagePath, String packageFilter, boolean isNative) throws IOException {
    final List<Path> packagePaths = new ArrayList<>();
    if (Files.isDirectory(packagePath)) {
        BallerinaSubPackageVisitor subPackageVisitor = new BallerinaSubPackageVisitor(packagePath, packagePaths);
        Files.walkFileTree(packagePath, subPackageVisitor);
    } else {
        packagePaths.add(packagePath);
    }
    BallerinaDocDataHolder dataHolder = BallerinaDocDataHolder.getInstance();
    if (!isNative) {
        // This is necessary to be true in order to Ballerina to work properly
        System.setProperty("skipNatives", "true");
    }
    BLangPackage bLangPackage;
    for (Path path : packagePaths) {
        CompilerContext context = new CompilerContext();
        CompilerOptions options = CompilerOptions.getInstance(context);
        options.put(CompilerOptionName.PROJECT_DIR, sourceRoot);
        options.put(CompilerOptionName.COMPILER_PHASE, CompilerPhase.DESUGAR.toString());
        options.put(CompilerOptionName.PRESERVE_WHITESPACE, "false");
        Compiler compiler = Compiler.getInstance(context);
        // TODO: Remove this and the related constants once these are properly handled in the core
        if (BAL_BUILTIN.equals(path) || BAL_BUILTIN_CORE.equals(path)) {
            bLangPackage = loadBuiltInPackage(context);
        } else {
            // compile the given file
            bLangPackage = compiler.compile(getPackageNameFromPath(path));
        }
        if (bLangPackage == null) {
            out.println(String.format("docerina: invalid Ballerina package: %s", packagePath));
        } else {
            String packageName = bLangPackage.symbol.pkgID.name.value;
            if (isFilteredPackage(packageName, packageFilter)) {
                if (BallerinaDocUtils.isDebugEnabled()) {
                    out.println("Package " + packageName + " excluded");
                }
                continue;
            }
            dataHolder.getPackageMap().put(packageName, bLangPackage);
        }
    }
    return dataHolder.getPackageMap();
}
Also used : Path(java.nio.file.Path) Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions)

Example 13 with CompilerOptions

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

the class WorkspaceUtils method getBallerinaFileForContent.

public static BLangPackage getBallerinaFileForContent(String fileName, String source) {
    CompilerContext context = prepareCompilerContext(fileName, source);
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(CompilerOptionName.COMPILER_PHASE, CompilerPhase.DEFINE.toString());
    options.put(CompilerOptionName.PRESERVE_WHITESPACE, Boolean.TRUE.toString());
    return getBallerinaPackage(fileName, context);
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions)

Example 14 with CompilerOptions

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

the class TextDocumentServiceUtil method prepareCompilerContext.

/**
 * Prepare the compiler context.
 *
 * @param packageRepository  Package Repository
 * @param sourceRoot         LSDocument for Source Root
 * @param preserveWhitespace Preserve Whitespace
 * @return {@link CompilerContext}     Compiler context
 */
public static CompilerContext prepareCompilerContext(PackageRepository packageRepository, LSDocument sourceRoot, boolean preserveWhitespace, WorkspaceDocumentManager documentManager, CompilerPhase compilerPhase) {
    org.wso2.ballerinalang.compiler.util.CompilerContext context = new CompilerContext();
    context.put(PackageRepository.class, packageRepository);
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(PROJECT_DIR, sourceRoot.getSourceRoot());
    if (null == compilerPhase) {
        throw new AssertionError("Compiler Phase can not be null.");
    }
    options.put(COMPILER_PHASE, compilerPhase.toString());
    options.put(PRESERVE_WHITESPACE, Boolean.valueOf(preserveWhitespace).toString());
    if (isProjectDir(sourceRoot.getSourceRoot(), sourceRoot.getURIString())) {
        context.put(SourceDirectory.class, new LangServerFSProjectDirectory(sourceRoot.getSourceRootPath(), documentManager));
    } else {
        context.put(SourceDirectory.class, new LangServerFSProgramDirectory(sourceRoot.getSourceRootPath(), documentManager));
    }
    return context;
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) LangServerFSProgramDirectory(org.ballerinalang.langserver.workspace.repository.LangServerFSProgramDirectory) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) LangServerFSProjectDirectory(org.ballerinalang.langserver.workspace.repository.LangServerFSProjectDirectory)

Example 15 with CompilerOptions

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

the class ParserUtils method getBallerinaFile.

/**
 * This method is designed to generate the Ballerina model and Diagnostic information for a given Ballerina file.
 * saved in the file-system.
 *
 * @param programDir          - Path of the program directory.
 * @param compilationUnitName - compilationUnitName name.
 * @return BallerinaFile - Object which contains Ballerina model and Diagnostic information
 */
public static BallerinaFile getBallerinaFile(String programDir, String compilationUnitName) {
    CompilerContext context = new CompilerContext();
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(PROJECT_DIR, programDir);
    options.put(COMPILER_PHASE, CompilerPhase.CODE_ANALYZE.toString());
    options.put(PRESERVE_WHITESPACE, Boolean.TRUE.toString());
    return getBallerinaFile(Paths.get(programDir), compilationUnitName, context);
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions)

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