Search in sources :

Example 21 with Compiler

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

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

the class TextDocumentServiceUtil method getCompiler.

/**
 * Get compiler for the given context and file.
 *
 * @param context             Language server context
 * @param fileName            File name which is currently open
 * @param packageRepository   package repository
 * @param sourceRoot          LSDocument for root path of the source
 * @param preserveWhitespace  enable/disable preserve white space in compiler
 * @param customErrorStrategy custom error strategy class
 * @return {@link Compiler} ballerina compiler
 */
private static Compiler getCompiler(LanguageServerContext context, String fileName, PackageRepository packageRepository, LSDocument sourceRoot, boolean preserveWhitespace, Class customErrorStrategy, WorkspaceDocumentManager documentManager) {
    CompilerContext compilerContext = TextDocumentServiceUtil.prepareCompilerContext(packageRepository, sourceRoot, preserveWhitespace, documentManager);
    context.put(DocumentServiceKeys.FILE_NAME_KEY, fileName);
    context.put(DocumentServiceKeys.COMPILER_CONTEXT_KEY, compilerContext);
    context.put(DocumentServiceKeys.OPERATION_META_CONTEXT_KEY, new TextDocumentServiceContext());
    List<org.ballerinalang.util.diagnostic.Diagnostic> balDiagnostics = new ArrayList<>();
    CollectDiagnosticListener diagnosticListener = new CollectDiagnosticListener(balDiagnostics);
    compilerContext.put(DiagnosticListener.class, diagnosticListener);
    compilerContext.put(DefaultErrorStrategy.class, CustomErrorStrategyFactory.getCustomErrorStrategy(customErrorStrategy, context));
    return Compiler.getInstance(compilerContext);
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList)

Example 23 with Compiler

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

the class TextDocumentServiceUtil method getBLangPackage.

/**
 * Get the BLangPackage for a given program.
 *
 * @param context             Language Server Context
 * @param docManager          Document manager
 * @param preserveWhitespace  Enable preserve whitespace
 * @param customErrorStrategy custom error strategy class
 * @param compileFullProject  compile full project from the source root
 * @return {@link BLangPackage} BLang Package
 */
public static List<BLangPackage> getBLangPackage(LanguageServerContext context, WorkspaceDocumentManager docManager, boolean preserveWhitespace, Class customErrorStrategy, boolean compileFullProject) {
    String uri = context.get(DocumentServiceKeys.FILE_URI_KEY);
    LSDocument document = new LSDocument(uri);
    Path filePath = CommonUtil.getPath(document);
    Path fileNamePath = filePath.getFileName();
    String fileName = "";
    if (fileNamePath != null) {
        fileName = fileNamePath.toString();
    }
    String sourceRoot = TextDocumentServiceUtil.getSourceRoot(filePath);
    String pkgName = TextDocumentServiceUtil.getPackageNameForGivenFile(sourceRoot, filePath.toString());
    LSDocument sourceDocument = new LSDocument();
    sourceDocument.setUri(uri);
    sourceDocument.setSourceRoot(sourceRoot);
    PackageRepository packageRepository = new WorkspacePackageRepository(sourceRoot, docManager);
    List<BLangPackage> packages = new ArrayList<>();
    if (compileFullProject) {
        if (!sourceRoot.isEmpty()) {
            File projectDir = new File(sourceRoot);
            File[] files = projectDir.listFiles();
            if (files != null) {
                for (File file : files) {
                    if ((file.isDirectory() && !file.getName().startsWith(".")) || (!file.isDirectory() && file.getName().endsWith(".bal"))) {
                        Compiler compiler = getCompiler(context, fileName, packageRepository, sourceDocument, preserveWhitespace, customErrorStrategy, docManager);
                        packages.add(compiler.compile(file.getName()));
                    }
                }
            }
        }
    } else {
        Compiler compiler = getCompiler(context, fileName, packageRepository, sourceDocument, preserveWhitespace, customErrorStrategy, docManager);
        if ("".equals(pkgName)) {
            packages.add(compiler.compile(fileName));
        } else {
            packages.add(compiler.compile(pkgName));
        }
    }
    return packages;
}
Also used : Path(java.nio.file.Path) Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) LSDocument(org.ballerinalang.langserver.common.LSDocument) ArrayList(java.util.ArrayList) PackageRepository(org.ballerinalang.repository.PackageRepository) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) File(java.io.File) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository)

Example 24 with Compiler

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

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