Search in sources :

Example 16 with Compiler

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

the class CompilerPluginRunner method handleAnnotationProcesses.

private void handleAnnotationProcesses(CompilerPlugin plugin) {
    // Get the list of packages of annotations that this particular compiler plugin is interested in.
    SupportedAnnotationPackages supportedAnnotationPackages = plugin.getClass().getAnnotation(SupportedAnnotationPackages.class);
    if (supportedAnnotationPackages == null) {
        return;
    }
    String[] annotationPkgs = supportedAnnotationPackages.value();
    if (annotationPkgs.length == 0) {
        return;
    }
    for (String annPackage : annotationPkgs) {
        // Check whether each annotation type definition is available in the AST.
        List<BAnnotationSymbol> annotationSymbols = getAnnotationSymbols(annPackage);
        annotationSymbols.forEach(annSymbol -> {
            DefinitionID definitionID = new DefinitionID(annSymbol.pkgID.name.value, annSymbol.name.value);
            List<CompilerPlugin> processorList = processorMap.computeIfAbsent(definitionID, k -> new ArrayList<>());
            processorList.add(plugin);
        });
    }
}
Also used : CompilerPlugin(org.ballerinalang.compiler.plugins.CompilerPlugin) SupportedAnnotationPackages(org.ballerinalang.compiler.plugins.SupportedAnnotationPackages) BAnnotationSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationSymbol)

Example 17 with Compiler

use of org.wso2.ballerinalang.compiler.Compiler 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 18 with Compiler

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

Example 19 with Compiler

use of org.wso2.ballerinalang.compiler.Compiler 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 20 with Compiler

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

Aggregations

CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)15 Compiler (org.wso2.ballerinalang.compiler.Compiler)14 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)13 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)12 ArrayList (java.util.ArrayList)8 Path (java.nio.file.Path)7 DiagnosticListener (org.ballerinalang.util.diagnostic.DiagnosticListener)4 CompiledBinaryFile (org.wso2.ballerinalang.programfile.CompiledBinaryFile)4 IOException (java.io.IOException)3 LSDocument (org.ballerinalang.langserver.common.LSDocument)3 WorkspacePackageRepository (org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository)3 Diagnostic (org.ballerinalang.util.diagnostic.Diagnostic)3 BDiagnostic (org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnostic)3 CompilerPhase (org.ballerinalang.compiler.CompilerPhase)2 CompilerPlugin (org.ballerinalang.compiler.plugins.CompilerPlugin)2 SupportedAnnotationPackages (org.ballerinalang.compiler.plugins.SupportedAnnotationPackages)2 BallerinaFile (org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile)2 PackageRepository (org.ballerinalang.repository.PackageRepository)2 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 File (java.io.File)1