use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.
the class BallerinaTextDocumentService method definition.
@Override
public CompletableFuture<List<? extends Location>> definition(TextDocumentPositionParams position) {
return CompletableFuture.supplyAsync(() -> {
TextDocumentServiceContext definitionContext = new TextDocumentServiceContext();
definitionContext.put(DocumentServiceKeys.FILE_URI_KEY, position.getTextDocument().getUri());
definitionContext.put(DocumentServiceKeys.POSITION_KEY, position);
BLangPackage currentBLangPackage = TextDocumentServiceUtil.getBLangPackage(definitionContext, documentManager, false, LSCustomErrorStrategy.class, false).get(0);
definitionContext.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, currentBLangPackage.symbol.getName().getValue());
lSPackageCache.addPackage(currentBLangPackage);
List<Location> contents;
try {
PositionTreeVisitor positionTreeVisitor = new PositionTreeVisitor(definitionContext);
currentBLangPackage.accept(positionTreeVisitor);
contents = DefinitionUtil.getDefinitionPosition(definitionContext, lSPackageCache);
} catch (Exception e) {
contents = new ArrayList<>();
}
return contents;
});
}
use of org.wso2.ballerinalang.compiler.tree.BLangPackage 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;
}
}
use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.
the class BCompileUtil method compile.
/**
* Compile and return the semantic errors.
*
* @param sourceRoot root path of the source packages
* @param packageName name of the package to compile
* @param compilerPhase Compiler phase
* @return Semantic errors
*/
public static CompileResult compile(String sourceRoot, String packageName, CompilerPhase compilerPhase) {
CompilerContext context = new CompilerContext();
CompilerOptions options = CompilerOptions.getInstance(context);
options.put(PROJECT_DIR, sourceRoot);
options.put(COMPILER_PHASE, compilerPhase.toString());
options.put(PRESERVE_WHITESPACE, "false");
CompileResult comResult = new CompileResult();
// catch errors
DiagnosticListener listener = comResult::addDiagnostic;
context.put(DiagnosticListener.class, listener);
// compile
Compiler compiler = Compiler.getInstance(context);
BLangPackage packageNode = compiler.compile(packageName);
comResult.setAST(packageNode);
if (comResult.getErrorCount() > 0 || CompilerPhase.CODE_GEN.compareTo(compilerPhase) > 0) {
return comResult;
}
CompiledBinaryFile.ProgramFile programFile = compiler.getExecutableProgram(packageNode);
if (programFile != null) {
ProgramFile pFile = LauncherUtils.getExecutableProgram(programFile);
comResult.setProgFile(pFile);
if (pFile != null) {
boolean distributedTxEnabled = CompilerUtils.isDistributedTransactionsEnabled();
pFile.setDistributedTransactionEnabled(distributedTxEnabled);
}
}
return comResult;
}
use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.
the class BCompileUtil method getDiagnostics.
/**
* Used by IntelliJ IDEA plugin to provide semantic analyzing capability.
*
* @param classLoader a {@link ClassLoader} to be set as thread context class loader. This is used by {@link
* java.util.ServiceLoader}. Otherwise semantic analyzing capability providing wont work since it
* cant find core package.
* @param sourceRoot source root of a project
* @param fileName either the file name (if in project root) or the package name
* @return list of diagnostics
*/
public static List<Diagnostic> getDiagnostics(ClassLoader classLoader, String sourceRoot, String fileName) {
Thread.currentThread().setContextClassLoader(classLoader);
CompilerContext context = new CompilerContext();
CompilerOptions options = CompilerOptions.getInstance(context);
options.put(PROJECT_DIR, sourceRoot);
options.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString());
options.put(PRESERVE_WHITESPACE, "false");
CompileResult comResult = new CompileResult();
// catch errors
DiagnosticListener listener = comResult::addDiagnostic;
context.put(DiagnosticListener.class, listener);
// compile
Compiler compiler = Compiler.getInstance(context);
BLangPackage entryPackageNode = compiler.compile(fileName);
CompiledBinaryFile.ProgramFile programFile = compiler.getExecutableProgram(entryPackageNode);
if (programFile != null) {
comResult.setProgFile(LauncherUtils.getExecutableProgram(programFile));
}
Diagnostic[] diagnostics = comResult.getDiagnostics();
return Arrays.stream(diagnostics).collect(Collectors.toList());
}
use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.
the class SymbolEnter method visit.
// Visitor methods
@Override
public void visit(BLangPackage pkgNode) {
if (pkgNode.completedPhases.contains(CompilerPhase.DEFINE)) {
return;
}
// Create PackageSymbol.
BPackageSymbol pSymbol = createPackageSymbol(pkgNode);
SymbolEnv builtinEnv = this.symTable.pkgEnvMap.get(symTable.builtInPackageSymbol);
SymbolEnv pkgEnv = SymbolEnv.createPkgEnv(pkgNode, pSymbol.scope, builtinEnv);
this.symTable.pkgEnvMap.put(pSymbol, pkgEnv);
createPackageInitFunctions(pkgNode);
// visit the package node recursively and define all package level symbols.
// And maintain a list of created package symbols.
pkgNode.imports.forEach(importNode -> defineNode(importNode, pkgEnv));
// Define struct nodes.
pkgNode.enums.forEach(enumNode -> defineNode(enumNode, pkgEnv));
// Define struct nodes.
pkgNode.structs.forEach(struct -> defineNode(struct, pkgEnv));
// Define object nodes
pkgNode.objects.forEach(object -> defineNode(object, pkgEnv));
// Define connector nodes.
pkgNode.connectors.forEach(con -> defineNode(con, pkgEnv));
// Define connector params and type.
defineConnectorParams(pkgNode.connectors, pkgEnv);
// Define transformer nodes.
pkgNode.transformers.forEach(tansformer -> defineNode(tansformer, pkgEnv));
// Define service and resource nodes.
pkgNode.services.forEach(service -> defineNode(service, pkgEnv));
// Define struct field nodes.
defineStructFields(pkgNode.structs, pkgEnv);
// Define object field nodes.
defineObjectFields(pkgNode.objects, pkgEnv);
// Define connector action nodes.
defineConnectorMembers(pkgNode.connectors, pkgEnv);
// Define object functions
defineObjectMembers(pkgNode.objects, pkgEnv);
// Define function nodes.
pkgNode.functions.forEach(func -> defineNode(func, pkgEnv));
// Define transformer params
defineTransformerMembers(pkgNode.transformers, pkgEnv);
// Define service resource nodes.
defineServiceMembers(pkgNode.services, pkgEnv);
// Define annotation nodes.
pkgNode.annotations.forEach(annot -> defineNode(annot, pkgEnv));
resolveAnnotationAttributeTypes(pkgNode.annotations, pkgEnv);
pkgNode.globalVars.forEach(var -> defineNode(var, pkgEnv));
pkgNode.globalEndpoints.forEach(ep -> defineNode(ep, pkgEnv));
definePackageInitFunctions(pkgNode, pkgEnv);
pkgNode.completedPhases.add(CompilerPhase.DEFINE);
}
Aggregations