Search in sources :

Example 26 with CompilerContext

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

the class BallerinaTextDocumentService method compileAndSendDiagnostics.

private void compileAndSendDiagnostics(String content, LSDocument document, Path path) {
    String sourceRoot = TextDocumentServiceUtil.getSourceRoot(path);
    String pkgName = TextDocumentServiceUtil.getPackageNameForGivenFile(sourceRoot, path.toString());
    LSDocument sourceDocument = new LSDocument();
    sourceDocument.setUri(document.getURIString());
    sourceDocument.setSourceRoot(sourceRoot);
    PackageRepository packageRepository = new WorkspacePackageRepository(sourceRoot, documentManager);
    CompilerContext context = TextDocumentServiceUtil.prepareCompilerContext(packageRepository, sourceDocument, false, documentManager);
    List<org.ballerinalang.util.diagnostic.Diagnostic> balDiagnostics = new ArrayList<>();
    CollectDiagnosticListener diagnosticListener = new CollectDiagnosticListener(balDiagnostics);
    context.put(DiagnosticListener.class, diagnosticListener);
    Compiler compiler = Compiler.getInstance(context);
    if ("".equals(pkgName)) {
        Path filePath = path.getFileName();
        if (filePath != null) {
            compiler.compile(filePath.toString());
        }
    } else {
        compiler.compile(pkgName);
    }
    publishDiagnostics(balDiagnostics, path);
}
Also used : Path(java.nio.file.Path) Compiler(org.wso2.ballerinalang.compiler.Compiler) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) PackageRepository(org.ballerinalang.repository.PackageRepository) MarkedString(org.eclipse.lsp4j.MarkedString) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) LSDocument(org.ballerinalang.langserver.common.LSDocument)

Example 27 with CompilerContext

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

the class LSPackageCache method loadPackagesMap.

private void loadPackagesMap() {
    CompilerContext tempCompilerContext = CommonUtil.prepareTempCompilerContext();
    for (String staticPkgName : staticPkgNames) {
        PackageID packageID = new PackageID(new org.wso2.ballerinalang.compiler.util.Name("ballerina"), new org.wso2.ballerinalang.compiler.util.Name(staticPkgName), new org.wso2.ballerinalang.compiler.util.Name("0.0.0"));
        this.packageMap.put(getPackageName(packageID), LSPackageLoader.getPackageById(tempCompilerContext, packageID));
    }
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) PackageID(org.ballerinalang.model.elements.PackageID)

Example 28 with CompilerContext

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

the class LSPackageLoader method getBuiltinPackages.

/**
 * Get the Builtin Package.
 * @return {@link BLangPackage} Builtin BLang package
 */
public static List<BLangPackage> getBuiltinPackages() {
    List<BLangPackage> builtins = new ArrayList<>();
    CompilerContext context = CommonUtil.prepareTempCompilerContext();
    PackageLoader pkgLoader = PackageLoader.getInstance(context);
    SemanticAnalyzer semAnalyzer = SemanticAnalyzer.getInstance(context);
    CodeAnalyzer codeAnalyzer = CodeAnalyzer.getInstance(context);
    BLangPackage builtInPkg = codeAnalyzer.analyze(semAnalyzer.analyze(pkgLoader.loadAndDefinePackage(Names.BUILTIN_ORG.value, Names.BUILTIN_PACKAGE.getValue())));
    builtins.add(builtInPkg);
    return builtins;
}
Also used : CodeAnalyzer(org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) PackageLoader(org.wso2.ballerinalang.compiler.PackageLoader) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) SemanticAnalyzer(org.wso2.ballerinalang.compiler.semantics.analyzer.SemanticAnalyzer)

Example 29 with CompilerContext

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

Example 30 with CompilerContext

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

the class ParserUtils method loadBuiltInPackage.

/**
 * Loading builtin packages.
 *
 * @param context compiler context
 * @return {BLangPackage} builtIn package
 */
private static BLangPackage loadBuiltInPackage(CompilerContext context) {
    PackageLoader pkgLoader = PackageLoader.getInstance(context);
    SymbolTable symbolTable = SymbolTable.getInstance(context);
    SemanticAnalyzer semAnalyzer = SemanticAnalyzer.getInstance(context);
    CodeAnalyzer codeAnalyzer = CodeAnalyzer.getInstance(context);
    Desugar desugar = Desugar.getInstance(context);
    BLangPackage builtInPkg = desugar.perform(codeAnalyzer.analyze(semAnalyzer.analyze(pkgLoader.loadAndDefinePackage(Names.BUILTIN_ORG.getValue(), Names.BUILTIN_PACKAGE.getValue()))));
    symbolTable.builtInPackageSymbol = builtInPkg.symbol;
    return builtInPkg;
}
Also used : CodeAnalyzer(org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer) LSPackageLoader(org.ballerinalang.langserver.LSPackageLoader) PackageLoader(org.wso2.ballerinalang.compiler.PackageLoader) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) Desugar(org.wso2.ballerinalang.compiler.desugar.Desugar) SemanticAnalyzer(org.wso2.ballerinalang.compiler.semantics.analyzer.SemanticAnalyzer)

Aggregations

CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)24 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)15 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)13 Compiler (org.wso2.ballerinalang.compiler.Compiler)12 ArrayList (java.util.ArrayList)10 Path (java.nio.file.Path)5 IOException (java.io.IOException)4 DiagnosticListener (org.ballerinalang.util.diagnostic.DiagnosticListener)4 CompiledBinaryFile (org.wso2.ballerinalang.programfile.CompiledBinaryFile)4 PackageID (org.ballerinalang.model.elements.PackageID)3 Diagnostic (org.ballerinalang.util.diagnostic.Diagnostic)3 Name (org.wso2.ballerinalang.compiler.util.Name)3 BDiagnostic (org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnostic)3 HashMap (java.util.HashMap)2 BallerinaFile (org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile)2 LSDocument (org.ballerinalang.langserver.common.LSDocument)2 WorkspacePackageRepository (org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository)2 PackageRepository (org.ballerinalang.repository.PackageRepository)2 PackageLoader (org.wso2.ballerinalang.compiler.PackageLoader)2 CodeAnalyzer (org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer)2