use of org.wso2.ballerinalang.programfile.PackageInfo 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.wso2.ballerinalang.programfile.PackageInfo in project ballerina by ballerina-lang.
the class ErrorTableEntry method setPackageInfo.
public void setPackageInfo(PackageInfo packageInfo) {
this.packageInfo = packageInfo;
// Load Cache values.
if (errorStructCPIndex < 0) {
return;
}
StructureRefCPEntry structureRefCPEntry = (StructureRefCPEntry) packageInfo.getCPEntry(errorStructCPIndex);
// this.error = (StructInfo) structureRefCPEntry.getStructureTypeInfo();
}
use of org.wso2.ballerinalang.programfile.PackageInfo in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
public void visit(BLangPackage pkgNode) {
if (pkgNode.completedPhases.contains(CompilerPhase.CODE_GEN)) {
if (!buildCompiledPackage) {
programFile.packageInfoMap.put(pkgNode.symbol.pkgID.bvmAlias(), pkgNode.symbol.packageInfo);
}
return;
}
// TODO Improve this design without if/else
PackageInfo packageInfo = new PackageInfo();
pkgNode.symbol.packageInfo = packageInfo;
if (buildCompiledPackage) {
// Generating the BALO
pkgNode.imports.forEach(impPkgNode -> {
int impPkgNameCPIndex = addUTF8CPEntry(packageInfo, impPkgNode.symbol.name.value);
// TODO Improve the import package version once it is available
int impPkgVersionCPIndex = addUTF8CPEntry(packageInfo, PackageID.DEFAULT.version.value);
ImportPackageInfo importPkgInfo = new ImportPackageInfo(impPkgNameCPIndex, impPkgVersionCPIndex);
packageInfo.importPkgInfoSet.add(importPkgInfo);
packageFile.packageInfo = packageInfo;
});
} else {
// Generating a BALX
// first visit all the imports
pkgNode.imports.forEach(impPkgNode -> genNode(impPkgNode, this.env));
// TODO We need to create identifier for both name and the version
programFile.packageInfoMap.put(pkgNode.symbol.pkgID.bvmAlias(), packageInfo);
}
// Add the current package to the program file
BPackageSymbol pkgSymbol = pkgNode.symbol;
currentPkgID = pkgSymbol.pkgID;
currentPkgInfo = packageInfo;
currentPkgInfo.nameCPIndex = addUTF8CPEntry(currentPkgInfo, currentPkgID.bvmAlias());
currentPkgInfo.versionCPIndex = addUTF8CPEntry(currentPkgInfo, currentPkgID.version.value);
// Insert the package reference to the constant pool of the current package
currentPackageRefCPIndex = addPackageRefCPEntry(currentPkgInfo, currentPkgID);
// This attribute keep track of line numbers
int lineNoAttrNameIndex = addUTF8CPEntry(currentPkgInfo, AttributeInfo.Kind.LINE_NUMBER_TABLE_ATTRIBUTE.value());
lineNoAttrInfo = new LineNumberTableAttributeInfo(lineNoAttrNameIndex);
// This attribute keep package-level variable information
int pkgVarAttrNameIndex = addUTF8CPEntry(currentPkgInfo, AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE.value());
currentPkgInfo.addAttributeInfo(AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE, new LocalVariableAttributeInfo(pkgVarAttrNameIndex));
pkgNode.globalVars.forEach(this::createPackageVarInfo);
pkgNode.structs.forEach(this::createStructInfoEntry);
pkgNode.enums.forEach(this::createEnumInfoEntry);
pkgNode.connectors.forEach(this::createConnectorInfoEntry);
pkgNode.functions.forEach(this::createFunctionInfoEntry);
pkgNode.services.forEach(this::createServiceInfoEntry);
pkgNode.functions.forEach(this::createFunctionInfoEntry);
pkgNode.transformers.forEach(this::createTransformerInfoEntry);
// Visit package builtin function
visitBuiltinFunctions(pkgNode.initFunction);
visitBuiltinFunctions(pkgNode.startFunction);
visitBuiltinFunctions(pkgNode.stopFunction);
pkgNode.topLevelNodes.stream().filter(pkgLevelNode -> pkgLevelNode.getKind() != NodeKind.VARIABLE && pkgLevelNode.getKind() != NodeKind.XMLNS).forEach(pkgLevelNode -> genNode((BLangNode) pkgLevelNode, this.env));
currentPkgInfo.addAttributeInfo(AttributeInfo.Kind.LINE_NUMBER_TABLE_ATTRIBUTE, lineNoAttrInfo);
currentPackageRefCPIndex = -1;
currentPkgID = null;
pkgNode.completedPhases.add(CompilerPhase.CODE_GEN);
}
use of org.wso2.ballerinalang.programfile.PackageInfo in project carbon-business-process by wso2.
the class BPELPackageManagementServiceSkeleton method getPackageInfo.
private PackageType getPackageInfo(BPELPackageInfo packageInfo, int maxRemainingPackages) throws PackageManagementException {
PackageType bpelPackage = new PackageType();
bpelPackage.setName(packageInfo.getName());
bpelPackage.setState(convertToPackageStatusType(packageInfo.getStatus()));
bpelPackage.setVersions(getAllVersionsOfPackage(packageInfo, maxRemainingPackages));
bpelPackage.setErrorLog(packageInfo.getCauseForDeploymentFailure());
return bpelPackage;
}
use of org.wso2.ballerinalang.programfile.PackageInfo in project carbon-business-process by wso2.
the class BPELPackageManagementServiceSkeleton method getAllVersionsOfPackage.
private Versions_type0 getAllVersionsOfPackage(BPELPackageInfo packageInfo, int maxRemainingPackages) throws PackageManagementException {
Versions_type0 versionsList = new Versions_type0();
List<String> versions = packageInfo.getAvailableVersions();
Collections.reverse(versions);
int count = 0;
int startIndex = maxRemainingPackages < 0 ? versions.size() + maxRemainingPackages : 0;
int endIndex = versions.size() - 1;
for (int i = startIndex; i <= endIndex && Math.abs(maxRemainingPackages) > count; i++) {
String version = versions.get(i);
Version_type0 packageVersion = new Version_type0();
packageVersion.setName(version);
packageVersion.setProcesses(getProcessesForPackage(version));
if (version.equals(packageInfo.getName() + "-" + packageInfo.getLatestVersion())) {
packageVersion.setIsLatest(true);
} else {
packageVersion.setIsLatest(false);
}
versionsList.addVersion(packageVersion);
count++;
}
return versionsList;
}
Aggregations