Search in sources :

Example 1 with ModelPackage

use of org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage in project ballerina by ballerina-lang.

the class BallerinaParserService method validateAndParse.

/**
 * Validates a given ballerina input.
 *
 * @param bFileRequest - Object which holds data about Ballerina content.
 * @return List of errors if any
 */
private JsonObject validateAndParse(BFile bFileRequest) throws InvocationTargetException, IllegalAccessException {
    final java.nio.file.Path filePath = Paths.get(bFileRequest.getFilePath(), bFileRequest.getFileName());
    final String fileName = bFileRequest.getFileName();
    final String content = bFileRequest.getContent();
    BallerinaFile bFile = ParserUtils.compile(content, filePath);
    String programDir = TextDocumentServiceUtil.getSourceRoot(filePath);
    final BLangPackage model = bFile.getBLangPackage();
    final List<Diagnostic> diagnostics = bFile.getDiagnostics();
    ErrorCategory errorCategory = ErrorCategory.NONE;
    if (!diagnostics.isEmpty()) {
        if (model == null) {
            errorCategory = ErrorCategory.SYNTAX;
        } else {
            errorCategory = ErrorCategory.SEMANTIC;
        }
    }
    JsonArray errors = new JsonArray();
    final String errorCategoryName = errorCategory.name();
    diagnostics.forEach(diagnostic -> {
        JsonObject error = new JsonObject();
        Diagnostic.DiagnosticPosition position = diagnostic.getPosition();
        if (position != null) {
            if (!diagnostic.getSource().getCompilationUnitName().equals(fileName)) {
                return;
            }
            error.addProperty("row", position.getStartLine());
            error.addProperty("column", position.getStartColumn());
            error.addProperty("type", "error");
            error.addProperty("category", errorCategoryName);
        } else {
            // position == null means it's a bug in core side.
            error.addProperty("category", ErrorCategory.RUNTIME.name());
        }
        error.addProperty("text", diagnostic.getMessage());
        errors.add(error);
    });
    JsonObject result = new JsonObject();
    result.add("errors", errors);
    Gson gson = new Gson();
    JsonElement diagnosticsJson = gson.toJsonTree(diagnostics);
    result.add("diagnostics", diagnosticsJson);
    if (model != null && bFileRequest.needTree()) {
        BLangCompilationUnit compilationUnit = model.getCompilationUnits().stream().filter(compUnit -> fileName.equals(compUnit.getName())).findFirst().get();
        JsonElement modelElement = generateJSON(compilationUnit, new HashMap<>());
        result.add("model", modelElement);
    }
    final Map<String, ModelPackage> modelPackage = new HashMap<>();
    ParserUtils.loadPackageMap("Current Package", bFile.getBLangPackage(), modelPackage);
    Optional<ModelPackage> packageInfoJson = modelPackage.values().stream().findFirst();
    if (packageInfoJson.isPresent() && bFileRequest.needPackageInfo()) {
        JsonElement packageInfo = gson.toJsonTree(packageInfoJson.get());
        result.add("packageInfo", packageInfo);
    }
    if (programDir != null) {
        result.addProperty("programDirPath", programDir);
    }
    return result;
}
Also used : BallerinaFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile) HashMap(java.util.HashMap) Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) JsonArray(com.google.gson.JsonArray) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) JsonElement(com.google.gson.JsonElement) ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit)

Example 2 with ModelPackage

use of org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage in project ballerina by ballerina-lang.

the class ParserUtils method extractEnums.

/**
 * Extract Enums from ballerina lang.
 *
 * @param packages    packages to send.
 * @param packagePath packagePath.
 * @param bLangEnum   enum.
 */
private static void extractEnums(Map<String, ModelPackage> packages, String packagePath, EnumNode bLangEnum) {
    String fileName = bLangEnum.getPosition().getSource().getCompilationUnitName();
    if (packages.containsKey(packagePath)) {
        ModelPackage modelPackage = packages.get(packagePath);
        modelPackage.addEnumItem(createNewEnum(bLangEnum.getName().getValue(), bLangEnum.getEnumerators(), fileName));
    } else {
        ModelPackage modelPackage = new ModelPackage();
        modelPackage.setName(packagePath);
        modelPackage.addEnumItem(createNewEnum(bLangEnum.getName().getValue(), bLangEnum.getEnumerators(), fileName));
        packages.put(packagePath, modelPackage);
    }
}
Also used : ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage)

Example 3 with ModelPackage

use of org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage in project ballerina by ballerina-lang.

the class ParserUtils method extractStruct.

/**
 * Extract Structs from ballerina lang.
 *
 * @param packages    packages to send.
 * @param packagePath packagePath.
 * @param struct      struct.
 */
private static void extractStruct(Map<String, ModelPackage> packages, String packagePath, BLangStruct struct) {
    String fileName = struct.getPosition().getSource().getCompilationUnitName();
    if (packages.containsKey(packagePath)) {
        ModelPackage modelPackage = packages.get(packagePath);
        modelPackage.addStructsItem(createNewStruct(struct.getName().getValue(), struct.getFields(), fileName));
    } else {
        ModelPackage modelPackage = new ModelPackage();
        modelPackage.setName(packagePath);
        modelPackage.addStructsItem(createNewStruct(struct.getName().getValue(), struct.getFields(), fileName));
        packages.put(packagePath, modelPackage);
    }
}
Also used : ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage)

Example 4 with ModelPackage

use of org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage 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;
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) HashMap(java.util.HashMap) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage) PackageID(org.ballerinalang.model.elements.PackageID) IOException(java.io.IOException) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 5 with ModelPackage

use of org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage in project ballerina by ballerina-lang.

the class ParserUtils method removeConstructsOfFile.

/**
 * Remove constructs from given file in given package from given package map.
 *
 * @param pkgName    Name of the package
 * @param fileName   Name of the File
 * @param packageMap Package constructs map
 */
public static void removeConstructsOfFile(String pkgName, String fileName, Map<String, ModelPackage> packageMap) {
    ModelPackage newPkg = new ModelPackage();
    newPkg.setName(pkgName);
    if (packageMap.containsKey(pkgName)) {
        ModelPackage currentPkg = packageMap.get(pkgName);
        currentPkg.getFunctions().forEach((Function func) -> {
            if (!func.getFileName().equals(fileName)) {
                newPkg.addFunctionsItem(func);
            }
        });
        currentPkg.getStructs().forEach((Struct struct) -> {
            if (!struct.getFileName().equals(fileName)) {
                newPkg.addStructsItem(struct);
            }
        });
        currentPkg.getAnnotations().forEach((AnnotationDef annotation) -> {
            if (!annotation.getFileName().equals(fileName)) {
                newPkg.addAnnotationsItem(annotation);
            }
        });
        currentPkg.getConnectors().forEach((Connector connector) -> {
            if (!connector.getFileName().equals(fileName)) {
                newPkg.addConnectorsItem(connector);
            }
        });
        packageMap.remove(pkgName);
        packageMap.put(pkgName, newPkg);
    }
}
Also used : Function(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Function) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) Connector(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Connector) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage) Struct(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Struct) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) AnnotationDef(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.AnnotationDef)

Aggregations

ModelPackage (org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage)9 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 BallerinaFile (org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile)2 AnnotationAttachment (org.ballerinalang.composer.service.ballerina.parser.service.model.lang.AnnotationAttachment)2 Parameter (org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Parameter)2 BLangAnnotationAttachment (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment)2 BLangCompilationUnit (org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit)2 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)2 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Swagger (io.swagger.models.Swagger)1 Yaml (io.swagger.v3.core.util.Yaml)1 SwaggerConverter (io.swagger.v3.parser.converter.SwaggerConverter)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1