jspurefix
Version:
pure node js fix engine
165 lines • 7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeParser = void 0;
const contained_1 = require("../../contained");
const definition_1 = require("../../definition");
const parse_context_1 = require("./parse-context");
class NodeParser {
constructor(progress) {
this.progress = progress;
this.parseContexts = [];
}
addto(context, containedField) {
if (context.set != null) {
this.progress.newAdds++;
const builder = new contained_1.ContainedSetBuilder(context.set);
builder.add(containedField);
}
else {
this.progress.cacheMisses++;
}
}
fullContextName() {
return this.parseContexts.map(c => c.name).join('.');
}
addSimple(node) {
if (node.isSelfClosing) {
const parent = this.peek();
if (!parent) {
throw new Error(`simple field ${node.name} has no parent on which to add.`);
}
const fieldName = node.attributes.name;
const fieldDefinition = this.progress.definitions.simple.get(fieldName);
if (!fieldDefinition) {
throw new Error(`simple field ${fieldName} has no declaration in dictionary.`);
}
const containedField = this.makeContainedSimple(fieldDefinition, parent, node.attributes.required === 'Y');
this.addto(parent, containedField);
}
}
makeContainedSimple(fieldDefinition, context, required) {
var _a, _b, _c;
return new contained_1.ContainedSimpleField(fieldDefinition, (_c = (_b = (_a = context === null || context === void 0 ? void 0 : context.set) === null || _a === void 0 ? void 0 : _a.fields) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0, required, false);
}
makeContainedComponent(fieldDef, context) {
var _a, _b, _c;
return new contained_1.ContainedComponentField(fieldDef, (_c = (_b = (_a = context === null || context === void 0 ? void 0 : context.set) === null || _a === void 0 ? void 0 : _a.fields) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0, context.required);
}
makeContainedGroup(fieldDef, context, required) {
var _a, _b, _c;
return new contained_1.ContainedGroupField(fieldDef, (_c = (_b = (_a = context === null || context === void 0 ? void 0 : context.set) === null || _a === void 0 ? void 0 : _a.fields) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0, required);
}
addComponentField(componentName, node) {
const parent = this.peek();
if (!parent) {
throw new Error(`component ${node.name} has no parent on which to add.`);
}
const fieldDef = this.progress.definitions.component.get(componentName);
if (fieldDef) {
const containedField = this.makeContainedComponent(fieldDef, parent);
this.addto(parent, containedField);
}
else {
if (this.progress.numberPasses >= this.progress.maxIterations) {
throw new Error(`field ${node.name} includes unknown component ${componentName}.`);
}
else {
this.progress.cacheMisses++;
}
}
}
addComponentDefinition(name) {
var _a, _b;
const latest = (_a = this.parseContexts.pop()) !== null && _a !== void 0 ? _a : null;
if (!latest) {
throw new Error(`component field ${name} closes yet does not exist.`);
}
if (!latest.defining) {
return;
}
const asComponent = (_b = latest.asComponent()) !== null && _b !== void 0 ? _b : null;
if (asComponent) {
this.progress.newDefines++;
this.progress.definitions.addComponentFieldDef(asComponent);
}
else {
throw new Error(`latest not instance of component field ${latest.name} `);
}
}
addGroupField(name) {
var _a;
const group = (_a = this.parseContexts.pop()) !== null && _a !== void 0 ? _a : null;
if (!group) {
throw new Error(`group field ${name} closes yet does not exist.`);
}
const parent = this.peek();
if (parent) {
const asGroup = group.asGroup();
if (asGroup) {
const containedField = this.makeContainedGroup(asGroup, parent, group.required);
this.addto(parent, containedField);
}
}
else {
throw new Error(`group field ${group.name} has no parent on which to add.`);
}
}
expandName(componentName) {
switch (componentName) {
case 'header': {
return 'StandardHeader';
}
case 'trailer': {
return 'StandardTrailer';
}
default:
return componentName;
}
}
beginComponentDefinition(node) {
const componentName = node.attributes.name || node.name;
const fullName = this.expandName(componentName);
if (!node.isSelfClosing) {
const set = new definition_1.ComponentFieldDefinition(fullName, componentName, null, null);
const context = new parse_context_1.ParseContext(fullName, true, set);
this.startContext(context);
}
else {
this.addComponentField(fullName, node);
const context = new parse_context_1.ParseContext(fullName, false, null);
this.startContext(context);
}
}
beginGroupDefinition(node) {
var _a;
if (node.isSelfClosing)
return;
const groupName = node.attributes.name;
const noOfField = (_a = this.progress.definitions.simple.get(groupName)) !== null && _a !== void 0 ? _a : null;
if (!noOfField) {
const msg = `group ${groupName} has no field defined.`;
throw new Error(msg);
}
const fullQualifiedName = `${this.fullContextName()}.${groupName}`;
let cached = this.progress.groupDefinitionCache.get(fullQualifiedName);
if (!cached) {
cached = new definition_1.GroupFieldDefinition(groupName, groupName, null, noOfField, null);
this.progress.groupDefinitionCache.set(fullQualifiedName, cached);
}
else {
cached.reset();
}
const context = new parse_context_1.ParseContext(node.attributes.name, true, cached);
context.required = node.attributes.required === 'Y';
this.startContext(context);
}
startContext(context) {
this.progress.newContexts++;
this.parseContexts.push(context);
}
peek() {
return this.parseContexts.length > 0 ? this.parseContexts[this.parseContexts.length - 1] : null;
}
}
exports.NodeParser = NodeParser;
//# sourceMappingURL=node-parser.js.map