strong-tie/inbound-calls
0
1"use strict";2Object.defineProperty(exports, "__esModule", { value: true });3exports.reconstruct = void 0;4const types_1 = require("./types");5const write_set_tokens_1 = require("./write-set-tokens");6const reduceStack = (stack) => stack.map(exports.reconstruct).join('');7const createAlternate = (token) => {8 if ('options' in token) {9 return token.options.map(reduceStack).join('|');10 }11 else if ('stack' in token) {12 return reduceStack(token.stack);13 }14 else {15 throw new Error(`options or stack must be Root or Group token`);16 }17};18exports.reconstruct = (token) => {19 switch (token.type) {20 case types_1.types.ROOT:21 return createAlternate(token);22 case types_1.types.CHAR: {23 const c = String.fromCharCode(token.value);24 // Note that the escaping for characters inside classes is handled25 // in the write-set-tokens module so '-' and ']' are not escaped here26 return (/[[\\{}$^.|?*+()]/.test(c) ? '\\' : '') + c;27 }28 case types_1.types.POSITION:29 if (token.value === '^' || token.value === '$') {30 return token.value;31 }32 else {33 return `\\${token.value}`;34 }35 case types_1.types.REFERENCE:36 return `\\${token.value}`;37 case types_1.types.SET:38 return write_set_tokens_1.writeSetTokens(token);39 case types_1.types.GROUP: {40 // Check token.remember41 const prefix = token.name ? `?<${token.name}>` :42 token.remember ? '' :43 token.followedBy ? '?=' :44 token.notFollowedBy ? '?!' :45 '?:';46 return `(${prefix}${createAlternate(token)})`;47 }48 case types_1.types.REPETITION: {49 const { min, max } = token;50 let endWith;51 if (min === 0 && max === 1) {52 endWith = '?';53 }54 else if (min === 1 && max === Infinity) {55 endWith = '+';56 }57 else if (min === 0 && max === Infinity) {58 endWith = '*';59 }60 else if (max === Infinity) {61 endWith = `{${min},}`;62 }63 else if (min === max) {64 endWith = `{${min}}`;65 }66 else {67 endWith = `{${min},${max}}`;68 }69 return `${exports.reconstruct(token.value)}${endWith}`;70 }71 case types_1.types.RANGE:72 return `${write_set_tokens_1.setChar(token.from)}-${write_set_tokens_1.setChar(token.to)}`;73 default:74 throw new Error(`Invalid token type ${token}`);75 }76};77//# sourceMappingURL=reconstruct.js.map