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;
}
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;
}
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);
}
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;
}
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;
}
}
Aggregations