jinaga
Version:
Data management for web and mobile applications.
108 lines • 5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.describeSpecification = exports.describeDeclaration = void 0;
function describeDeclaration(references, labels) {
const declaration = references.map((reference, index) => {
const label = labels[index];
return `let ${label.name}: ${label.type} = #${reference.hash}\n`;
}).join("");
return declaration;
}
exports.describeDeclaration = describeDeclaration;
function describeSpecification(specification, depth) {
const indent = " ".repeat(depth);
const given = specification.given.map(given => describeGiven(given, depth)).join(", ");
const matches = specification.matches.map(match => describeMatch(match, depth + 1)).join("");
const projection = (specification.projection.type === "composite" && specification.projection.components.length === 0) ? "" :
" => " + describeProjection(specification.projection, depth);
return `${indent}(${given}) {\n${matches}${indent}}${projection}\n`;
}
exports.describeSpecification = describeSpecification;
function describeGiven(given, depth) {
if (given.conditions.length === 0) {
return `${given.label.name}: ${given.label.type}`;
}
const indent = " ".repeat(depth);
const conditions = given.conditions.map(condition => describeExistentialCondition(condition, depth + 1)).join("");
return `${given.label.name}: ${given.label.type} [\n${conditions}${indent}]`;
}
function describeExistentialCondition(condition, depth) {
const indent = " ".repeat(depth);
const matches = condition.matches.map(match => describeMatch(match, depth + 1)).join("");
const op = condition.exists ? "" : "!";
return `${indent}${op}E {\n${matches}${indent}}\n`;
}
function describeMatch(match, depth) {
const indent = " ".repeat(depth);
const conditions = match.conditions.map(condition => describeCondition(condition, match.unknown.name, depth + 1)).join("");
return `${indent}${match.unknown.name}: ${match.unknown.type} [\n${conditions}${indent}]\n`;
}
function describeCondition(condition, unknown, depth) {
const indent = " ".repeat(depth);
if (condition.type === "path") {
const rolesLeft = condition.rolesLeft.map(r => `->${r.name}: ${r.predecessorType}`).join("");
const rolesRight = condition.rolesRight.map(r => `->${r.name}: ${r.predecessorType}`).join("");
return `${indent}${unknown}${rolesLeft} = ${condition.labelRight}${rolesRight}\n`;
}
else if (condition.type === "existential") {
const matches = condition.matches.map(match => describeMatch(match, depth + 1)).join("");
const op = condition.exists ? "" : "!";
return `${indent}${op}E {\n${matches}${indent}}\n`;
}
else {
throw new Error("Not implemented");
}
}
function describeProjection(projection, depth) {
if (projection.type === "composite") {
const indent = " ".repeat(depth);
const orderedProjections = projection.components.sort((a, b) => a.name.localeCompare(b.name));
const projectionDescriptions = orderedProjections.map(projection => ` ${indent}${projection.name} = ${describeComponentProjection(projection, depth + 1)}\n`).join("");
return `{\n${projectionDescriptions}${indent}}`;
}
else if (projection.type === "field") {
return `${projection.label}.${projection.field}`;
}
else if (projection.type === "fact") {
return projection.label;
}
else if (projection.type === "hash") {
return `#${projection.label}`;
}
else if (projection.type === "time") {
return `@${projection.label}`;
}
else {
const _exhaustiveCheck = projection;
throw new Error(`Unknown projection type: ${_exhaustiveCheck.type}`);
}
}
function describeComponentProjection(projection, depth) {
if (projection.type === "specification") {
return describeChildSpecification(projection, depth);
}
else if (projection.type === "field") {
return `${projection.label}.${projection.field}`;
}
else if (projection.type === "fact") {
return projection.label;
}
else if (projection.type === "hash") {
return `#${projection.label}`;
}
else if (projection.type === "time") {
return `@${projection.label}`;
}
else {
const _exhaustiveCheck = projection;
throw new Error(`Unknown projection type: ${_exhaustiveCheck.type}`);
}
}
function describeChildSpecification(specification, depth) {
const indent = " ".repeat(depth);
const matches = specification.matches.map(match => describeMatch(match, depth + 1)).join("");
const projection = (specification.projection.type === "composite" && specification.projection.components.length === 0) ? "" :
" => " + describeProjection(specification.projection, depth);
return `{\n${matches}${indent}}${projection}`;
}
//# sourceMappingURL=description.js.map