Search in sources :

Example 1 with Compiler

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

the class WorkspaceUtils method getBallerinaPackage.

private static BLangPackage getBallerinaPackage(String fileName, CompilerContext context) {
    Compiler compiler = Compiler.getInstance(context);
    BLangPackage balPkg = null;
    try {
        balPkg = compiler.compile(fileName);
    } catch (Exception ex) {
        BDiagnostic catastrophic = new BDiagnostic();
        catastrophic.msg = "Failed in the runtime parse/analyze. " + ex.getMessage();
    }
    return balPkg;
}
Also used : Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BDiagnostic(org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnostic)

Example 2 with Compiler

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

the class ParserUtils method compile.

/**
 * Compile a Ballerina file.
 *
 * @param content       file content
 * @param path          file path
 * @param compilerPhase {CompilerPhase} set phase for the compiler.
 * @return
 */
public static BallerinaFile compile(String content, Path path, CompilerPhase compilerPhase) {
    if (documentManager.isFileOpen(path)) {
        documentManager.updateFile(path, content);
    } else {
        documentManager.openFile(path, content);
    }
    String sourceRoot = TextDocumentServiceUtil.getSourceRoot(path);
    String pkgName = TextDocumentServiceUtil.getPackageNameForGivenFile(sourceRoot, path.toString());
    LSDocument sourceDocument = new LSDocument();
    sourceDocument.setUri(path.toUri().toString());
    sourceDocument.setSourceRoot(sourceRoot);
    PackageRepository packageRepository = new WorkspacePackageRepository(sourceRoot, documentManager);
    CompilerContext context = TextDocumentServiceUtil.prepareCompilerContext(packageRepository, sourceDocument, true, documentManager, CompilerPhase.DEFINE);
    List<org.ballerinalang.util.diagnostic.Diagnostic> balDiagnostics = new ArrayList<>();
    CollectDiagnosticListener diagnosticListener = new CollectDiagnosticListener(balDiagnostics);
    context.put(DiagnosticListener.class, diagnosticListener);
    BLangPackage bLangPackage = null;
    try {
        Compiler compiler = Compiler.getInstance(context);
        if ("".equals(pkgName)) {
            Path filePath = path.getFileName();
            if (filePath != null) {
                bLangPackage = compiler.compile(filePath.toString());
            }
        } else {
            bLangPackage = compiler.compile(pkgName);
        }
    } catch (Exception e) {
    // Ignore.
    }
    BallerinaFile bfile = new BallerinaFile();
    bfile.setBLangPackage(bLangPackage);
    bfile.setDiagnostics(balDiagnostics);
    return bfile;
}
Also used : Path(java.nio.file.Path) Compiler(org.wso2.ballerinalang.compiler.Compiler) BallerinaFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic) BDiagnostic(org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnostic) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) PackageRepository(org.ballerinalang.repository.PackageRepository) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) IOException(java.io.IOException) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) LSDocument(org.ballerinalang.langserver.common.LSDocument) CollectDiagnosticListener(org.ballerinalang.langserver.CollectDiagnosticListener)

Example 3 with Compiler

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

the class ParserUtils method getBallerinaFileForContent.

/**
 * This method is designed to generate the Ballerina model and Diagnostic information for a given Ballerina content.
 * Ideal use case is generating Ballerina model and Diagnostic information for unsaved Ballerina files.
 *
 * @param fileName      - File name. This can be any arbitrary name as as we haven't save the file yet.
 * @param source        - Ballerina source content that needs to be parsed.
 * @param compilerPhase - This will tell up to which point(compiler phase) we should process the model
 * @return BallerinaFile - Object which contains Ballerina model and Diagnostic information
 */
public static BallerinaFile getBallerinaFileForContent(Path filePath, String fileName, String source, CompilerPhase compilerPhase) {
    CompilerContext context = prepareCompilerContext(fileName, source);
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(COMPILER_PHASE, compilerPhase.toString());
    options.put(PRESERVE_WHITESPACE, Boolean.TRUE.toString());
    return getBallerinaFile(filePath, fileName, context);
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions)

Example 4 with Compiler

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

the class ParserUtils method getBallerinaFile.

/**
 * Returns an object which contains Ballerina model and Diagnostic information.
 *
 * @param fileName - File name
 * @param context  - CompilerContext
 * @return BallerinaFile - Object which contains Ballerina model and Diagnostic information
 */
private static BallerinaFile getBallerinaFile(Path packagePath, String fileName, CompilerContext context) {
    List<Diagnostic> diagnostics = new ArrayList<>();
    ComposerDiagnosticListener composerDiagnosticListener = new ComposerDiagnosticListener(diagnostics);
    context.put(DiagnosticListener.class, composerDiagnosticListener);
    BallerinaFile ballerinaFile = new BallerinaFile();
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(PROJECT_DIR, packagePath.toString());
    options.put(COMPILER_PHASE, CompilerPhase.DEFINE.toString());
    options.put(PRESERVE_WHITESPACE, "true");
    context.put(SourceDirectory.class, new FileSystemProjectDirectory(packagePath));
    Compiler compiler = Compiler.getInstance(context);
    // compile
    try {
        ballerinaFile.setBLangPackage(compiler.compile(fileName));
    } catch (Exception ex) {
        BDiagnostic catastrophic = new BDiagnostic();
        catastrophic.msg = "Failed in the runtime parse/analyze. " + ex.getMessage();
        diagnostics.add(catastrophic);
    }
    ballerinaFile.setDiagnostics(diagnostics);
    return ballerinaFile;
}
Also used : Compiler(org.wso2.ballerinalang.compiler.Compiler) FileSystemProjectDirectory(org.wso2.ballerinalang.compiler.FileSystemProjectDirectory) BallerinaFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile) ArrayList(java.util.ArrayList) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic) BDiagnostic(org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnostic) BDiagnostic(org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnostic) IOException(java.io.IOException)

Example 5 with Compiler

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

the class LSPackageCache method findPackage.

/**
 * Find the package by Package ID.
 * @param compilerContext       compiler context
 * @param pkgId                 Package Id to lookup
 * @return {@link BLangPackage} BLang Package resolved
 */
public BLangPackage findPackage(CompilerContext compilerContext, PackageID pkgId) {
    if (containsPackage(pkgId.getName().getValue())) {
        return packageMap.get(pkgId.getName().getValue());
    } else {
        BLangPackage bLangPackage = LSPackageLoader.getPackageById(compilerContext, pkgId);
        addPackage(bLangPackage);
        return bLangPackage;
    }
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage)

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