Search in sources :

Example 26 with BLangPackage

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;
    });
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) ArrayList(java.util.ArrayList) PositionTreeVisitor(org.ballerinalang.langserver.common.position.PositionTreeVisitor) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy) Location(org.eclipse.lsp4j.Location)

Example 27 with BLangPackage

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;
    }
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage)

Example 28 with 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;
}
Also used : Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) CompiledBinaryFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) DiagnosticListener(org.ballerinalang.util.diagnostic.DiagnosticListener) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Example 29 with BLangPackage

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());
}
Also used : Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) CompiledBinaryFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic) DiagnosticListener(org.ballerinalang.util.diagnostic.DiagnosticListener)

Example 30 with BLangPackage

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);
}
Also used : BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Aggregations

BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)78 Test (org.testng.annotations.Test)29 ArrayList (java.util.ArrayList)28 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)19 Page (org.ballerinalang.docgen.model.Page)18 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)16 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)15 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)15 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)14 IOException (java.io.IOException)13 List (java.util.List)13 Path (java.nio.file.Path)12 Compiler (org.wso2.ballerinalang.compiler.Compiler)12 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)12 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)11 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)10 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)10 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)9 BLangAction (org.wso2.ballerinalang.compiler.tree.BLangAction)9 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)9