Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const i = numbers.indexOf(value);
rules.push([RuleType.NORMAL, inst, i]);
} else if (/^\d+(\/\d+)?$/.test(value)) {
// Normal rule with explicit radix
const p = value.split('/');
const i = numbers.indexOf(p[0]);
const j = numbers.indexOf(p[1]);
rules.push([RuleType.NORMAL_RADIX, inst, i, j]);
} else {
// Atom rule
switch (value) {
case '-x':
rules.push([RuleType.MINUS, inst]);
break;
case '0.x':
rules.push([RuleType.PROPER_FRACTION, inst, 0]);
if (!comma) {
rules.push([RuleType.PROPER_FRACTION, inst, 1]);
}
break;
case '0,x':
rules.push([RuleType.PROPER_FRACTION, inst, 1]);
if (!period) {
rules.push([RuleType.PROPER_FRACTION, inst, 0]);
}
break;
break;
}
}
for (const raw of ruleset[1].rules) {
// Decode the instruction array
const inst = this.encodeRule(raw.rule, symbols);
// Decode the rule descriptor.
const { value } = raw;
if (/^\d+$/.test(value)) {
// Normal rule without an explicit radix.
const i = numbers.indexOf(value);
rules.push([RuleType.NORMAL, inst, i]);
} else if (/^\d+(\/\d+)?$/.test(value)) {
// Normal rule with explicit radix
const p = value.split('/');
const i = numbers.indexOf(p[0]);
const j = numbers.indexOf(p[1]);
rules.push([RuleType.NORMAL_RADIX, inst, i, j]);
} else {
// Atom rule
switch (value) {
case '-x':
rules.push([RuleType.MINUS, inst]);
break;
// Decode the rule descriptor.
const { value } = raw;
if (/^\d+$/.test(value)) {
// Normal rule without an explicit radix.
const i = numbers.indexOf(value);
rules.push([RuleType.NORMAL, inst, i]);
} else if (/^\d+(\/\d+)?$/.test(value)) {
// Normal rule with explicit radix
const p = value.split('/');
const i = numbers.indexOf(p[0]);
const j = numbers.indexOf(p[1]);
rules.push([RuleType.NORMAL_RADIX, inst, i, j]);
} else {
// Atom rule
switch (value) {
case '-x':
rules.push([RuleType.MINUS, inst]);
break;
case '0.x':
rules.push([RuleType.PROPER_FRACTION, inst, 0]);
if (!comma) {
rules.push([RuleType.PROPER_FRACTION, inst, 1]);
}
break;
case '0,x':
return [Opcode.APPLY_LEFT_RULE, i];
}
case 'apply-left-numfmt': {
const i = this.index.get(node.n);
return [Opcode.APPLY_LEFT_NUM_FORMAT, i];
}
case 'apply-left-2-rule': {
const i = this.ruleindex(node.n);
return [Opcode.APPLY_LEFT_2_RULE, i];
}
case 'apply-left-2-numfmt': {
const i = symbols.indexOf(node.n);
return [Opcode.APPLY_LEFT_2_NUM_FORMAT, i];
}
case 'apply-right-rule': {
const i = this.ruleindex(node.n);
return [Opcode.APPLY_RIGHT_RULE, i];
}
case 'apply-right-numfmt': {
const i = symbols.indexOf(node.n);
return [Opcode.APPLY_RIGHT_NUM_FORMAT, i];
}
case 'apply-unch-rule': {
const i = this.ruleindex(node.n);
return [Opcode.UNCHANGED_RULE, i];
}
return [Opcode.LITERAL, i];
}
case 'apply-left-rule': {
const i = this.ruleindex(node.n);
return [Opcode.APPLY_LEFT_RULE, i];
}
case 'apply-left-numfmt': {
const i = this.index.get(node.n);
return [Opcode.APPLY_LEFT_NUM_FORMAT, i];
}
case 'apply-left-2-rule': {
const i = this.ruleindex(node.n);
return [Opcode.APPLY_LEFT_2_RULE, i];
}
case 'apply-left-2-numfmt': {
const i = symbols.indexOf(node.n);
return [Opcode.APPLY_LEFT_2_NUM_FORMAT, i];
}
case 'apply-right-rule': {
const i = this.ruleindex(node.n);
return [Opcode.APPLY_RIGHT_RULE, i];
}
case 'apply-right-numfmt': {
const i = symbols.indexOf(node.n);
return [Opcode.APPLY_RIGHT_NUM_FORMAT, i];
}
private encodeNode(node: RBNFNode, symbols: string[]): any {
switch (node.kind) {
case 'literal': {
const i = symbols.indexOf(node.n);
return [Opcode.LITERAL, i];
}
case 'apply-left-rule': {
const i = this.ruleindex(node.n);
return [Opcode.APPLY_LEFT_RULE, i];
}
case 'apply-left-numfmt': {
const i = this.index.get(node.n);
return [Opcode.APPLY_LEFT_NUM_FORMAT, i];
}
case 'apply-left-2-rule': {
const i = this.ruleindex(node.n);
return [Opcode.APPLY_LEFT_2_RULE, i];
}
case 'apply-left-2-numfmt': {
const i = symbols.indexOf(node.n);
return [Opcode.APPLY_LEFT_2_NUM_FORMAT, i];
}
case 'apply-right-rule': {
const i = this.ruleindex(node.n);
return [Opcode.APPLY_RIGHT_RULE, i];
}
private encodeNode(node: RBNFNode, symbols: string[]): any {
switch (node.kind) {
case 'literal': {
const i = symbols.indexOf(node.n);
return [Opcode.LITERAL, i];
}
case 'apply-left-rule': {
const i = this.ruleindex(node.n);
return [Opcode.APPLY_LEFT_RULE, i];
}
case 'apply-left-numfmt': {
const i = this.index.get(node.n);
return [Opcode.APPLY_LEFT_NUM_FORMAT, i];
}
case 'apply-left-2-rule': {
const i = this.ruleindex(node.n);
return [Opcode.APPLY_LEFT_2_RULE, i];
}
case 'apply-left-2-numfmt': {
const i = symbols.indexOf(node.n);
return [Opcode.APPLY_LEFT_2_NUM_FORMAT, i];
}
return [Opcode.APPLY_LEFT_2_RULE, i];
}
case 'apply-left-2-numfmt': {
const i = symbols.indexOf(node.n);
return [Opcode.APPLY_LEFT_2_NUM_FORMAT, i];
}
case 'apply-right-rule': {
const i = this.ruleindex(node.n);
return [Opcode.APPLY_RIGHT_RULE, i];
}
case 'apply-right-numfmt': {
const i = symbols.indexOf(node.n);
return [Opcode.APPLY_RIGHT_NUM_FORMAT, i];
}
case 'apply-unch-rule': {
const i = this.ruleindex(node.n);
return [Opcode.UNCHANGED_RULE, i];
}
case 'apply-unch-numfmt': {
const i = symbols.indexOf(node.n);
return [Opcode.UNCHANGED_NUM_FORMAT, i];
}
case 'cardinal':
case 'ordinal': {
const block: any[] = [];
for (const n of node.n) {
return [Opcode.APPLY_LEFT_NUM_FORMAT, i];
}
case 'apply-left-2-rule': {
const i = this.ruleindex(node.n);
return [Opcode.APPLY_LEFT_2_RULE, i];
}
case 'apply-left-2-numfmt': {
const i = symbols.indexOf(node.n);
return [Opcode.APPLY_LEFT_2_NUM_FORMAT, i];
}
case 'apply-right-rule': {
const i = this.ruleindex(node.n);
return [Opcode.APPLY_RIGHT_RULE, i];
}
case 'apply-right-numfmt': {
const i = symbols.indexOf(node.n);
return [Opcode.APPLY_RIGHT_NUM_FORMAT, i];
}
case 'apply-unch-rule': {
const i = this.ruleindex(node.n);
return [Opcode.UNCHANGED_RULE, i];
}
case 'apply-unch-numfmt': {
const i = symbols.indexOf(node.n);
return [Opcode.UNCHANGED_NUM_FORMAT, i];
}
}
case 'apply-unch-numfmt': {
const i = symbols.indexOf(node.n);
return [Opcode.UNCHANGED_NUM_FORMAT, i];
}
case 'cardinal':
case 'ordinal': {
const block: any[] = [];
for (const n of node.n) {
const cat = PLURALS.indexOf(n.category);
const i = symbols.indexOf(n.n);
block.push([cat, i]);
}
return node.kind === 'cardinal' ? [Opcode.CARDINAL, block] : [Opcode.ORDINAL, block];
}
case 'optional': {
const block = node.n.map(n => this.encodeNode(n, symbols));
return [Opcode.OPTIONAL, block];
}
case 'sub-left':
return [Opcode.SUB_LEFT];
case 'sub-right':
return [Opcode.SUB_RIGHT];
case 'sub-right-3':
return [Opcode.SUB_RIGHT_3];
}