use of org.wso2.ballerinalang.compiler.util.CompilerContext in project ballerina by ballerina-lang.
the class BallerinaDocGenerator method generatePackageDocsFromBallerina.
/**
* Generates {@link BLangPackage} objects for each Ballerina package from the given ballerina files.
*
* @param sourceRoot points to the folder relative to which package path is given
* @param packagePath a {@link Path} object pointing either to a ballerina file or a folder with ballerina files.
* @param packageFilter comma separated list of package names/patterns to be filtered from the documentation.
* @param isNative whether the given packages are native or not.
* @return a map of {@link BLangPackage} objects. Key - Ballerina package name Value - {@link BLangPackage}
*/
protected static Map<String, BLangPackage> generatePackageDocsFromBallerina(String sourceRoot, Path packagePath, String packageFilter, boolean isNative) throws IOException {
final List<Path> packagePaths = new ArrayList<>();
if (Files.isDirectory(packagePath)) {
BallerinaSubPackageVisitor subPackageVisitor = new BallerinaSubPackageVisitor(packagePath, packagePaths);
Files.walkFileTree(packagePath, subPackageVisitor);
} else {
packagePaths.add(packagePath);
}
BallerinaDocDataHolder dataHolder = BallerinaDocDataHolder.getInstance();
if (!isNative) {
// This is necessary to be true in order to Ballerina to work properly
System.setProperty("skipNatives", "true");
}
BLangPackage bLangPackage;
for (Path path : packagePaths) {
CompilerContext context = new CompilerContext();
CompilerOptions options = CompilerOptions.getInstance(context);
options.put(CompilerOptionName.PROJECT_DIR, sourceRoot);
options.put(CompilerOptionName.COMPILER_PHASE, CompilerPhase.DESUGAR.toString());
options.put(CompilerOptionName.PRESERVE_WHITESPACE, "false");
Compiler compiler = Compiler.getInstance(context);
// TODO: Remove this and the related constants once these are properly handled in the core
if (BAL_BUILTIN.equals(path) || BAL_BUILTIN_CORE.equals(path)) {
bLangPackage = loadBuiltInPackage(context);
} else {
// compile the given file
bLangPackage = compiler.compile(getPackageNameFromPath(path));
}
if (bLangPackage == null) {
out.println(String.format("docerina: invalid Ballerina package: %s", packagePath));
} else {
String packageName = bLangPackage.symbol.pkgID.name.value;
if (isFilteredPackage(packageName, packageFilter)) {
if (BallerinaDocUtils.isDebugEnabled()) {
out.println("Package " + packageName + " excluded");
}
continue;
}
dataHolder.getPackageMap().put(packageName, bLangPackage);
}
}
return dataHolder.getPackageMap();
}
use of org.wso2.ballerinalang.compiler.util.CompilerContext in project ballerina by ballerina-lang.
the class BallerinaDocGenerator method loadBuiltInPackage.
private static BLangPackage loadBuiltInPackage(CompilerContext context) {
SymbolTable symbolTable = SymbolTable.getInstance(context);
// Load built-in packages.
BLangPackage builtInPkg = getBuiltInPackage(context);
symbolTable.builtInPackageSymbol = builtInPkg.symbol;
return builtInPkg;
}
use of org.wso2.ballerinalang.compiler.util.CompilerContext in project ballerina by ballerina-lang.
the class WorkspaceUtils method getBallerinaFileForContent.
public static BLangPackage getBallerinaFileForContent(String fileName, String source) {
CompilerContext context = prepareCompilerContext(fileName, source);
CompilerOptions options = CompilerOptions.getInstance(context);
options.put(CompilerOptionName.COMPILER_PHASE, CompilerPhase.DEFINE.toString());
options.put(CompilerOptionName.PRESERVE_WHITESPACE, Boolean.TRUE.toString());
return getBallerinaPackage(fileName, context);
}
use of org.wso2.ballerinalang.compiler.util.CompilerContext 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.util.CompilerContext 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);
}
Aggregations