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