use of org.wso2.ballerinalang.compiler.util.CompilerContext 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.util.CompilerContext 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.util.CompilerContext in project ballerina by ballerina-lang.
the class ParserUtils method getAllPackages.
/**
* Get All Native Packages.
*
* @return {@link Map} Package name, package functions and connectors
*/
public static Map<String, ModelPackage> getAllPackages() {
final Map<String, ModelPackage> modelPackage = new HashMap<>();
// TODO: remove once the packerina api for package listing is available
final String[] packageNames = { "net.http", "net.http.authadaptor", "net.http.endpoints", "net.http.mock", "net.http.swagger", "net.uri", "mime", "net.websub", "net.websub.hub", "net.grpc", "auth", "auth.authz", "auth.authz.permissionstore", "auth.basic", "auth.jwtAuth", "auth.userstore", "auth.utils", "caching", "collections", "config", "data.sql", "file", "internal", "io", "jwt", "jwt.signature", "log", "math", "os", "reflect", "runtime", "security.crypto", "task", "time", "transactions.coordinator", "user", "util" };
try {
List<BLangPackage> builtInPackages = LSPackageLoader.getBuiltinPackages();
for (BLangPackage bLangPackage : builtInPackages) {
loadPackageMap(bLangPackage.packageID.getName().getValue(), bLangPackage, modelPackage);
}
CompilerContext context = CommonUtil.prepareTempCompilerContext();
for (String packageName : packageNames) {
PackageID packageID = new PackageID(new Name("ballerina"), new Name(packageName), new Name("0.0.0"));
BLangPackage bLangPackage = LSPackageLoader.getPackageById(context, packageID);
loadPackageMap(bLangPackage.packageID.getName().getValue(), bLangPackage, modelPackage);
}
} catch (Exception e) {
// Above catch is to fail safe composer front end due to core errors.
logger.warn("Error while loading packages");
}
return modelPackage;
}
use of org.wso2.ballerinalang.compiler.util.CompilerContext 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.util.CompilerContext in project ballerina by ballerina-lang.
the class ParserUtils method getBuiltinTypes.
/**
* Get the builtin types.
*
* @return {@link List} list of builtin types
*/
public static List<SymbolInformation> getBuiltinTypes() {
CompilerContext context = prepareCompilerContext("", "");
SymbolTable symbolTable = SymbolTable.getInstance(context);
List<SymbolInformation> symbolInformationList = new ArrayList<>();
// TODO: Need to fill the default values
symbolTable.rootScope.entries.forEach((key, value) -> {
if (value.symbol instanceof BTypeSymbol) {
SymbolInformation symbolInfo = new SymbolInformation();
String symbolName = value.symbol.getName().getValue();
if (!symbolName.equals(BuiltInType.INVALID_TYPE)) {
symbolInfo.setName(symbolName);
setDefaultValuesForType(symbolName, symbolInfo);
symbolInformationList.add(symbolInfo);
}
}
});
return symbolInformationList;
}
Aggregations