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