UNPKG

yogeshs-utilities

Version:
204 lines 9.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CobolVariable = exports.StatementProcessor = void 0; class CobolVariable { constructor(variableName, defaultValue) { this.variableName = variableName; this.defaultValue = defaultValue; } } exports.CobolVariable = CobolVariable; class StatementProcessor { static performVarying(allLines, methodNameList, cobolVariableList) { const mainMethodList = []; if (allLines.length === 0) return mainMethodList; const regex = new RegExp("^PERFORM", "i"); allLines.forEach(line => { if (!regex.test(line.modifiedLine)) { mainMethodList.push(line); } else { const mainBlock = this.conversionDoWhile(line.modifiedLine, methodNameList, cobolVariableList); mainBlock.forEach((d) => { mainMethodList.push({ ...line, modifiedLine: d }); }); } }); return mainMethodList; } static conversionDoWhile(modifiedLine, methodNameList, cobolVariableList) { const mainBlockList = []; let performStmtList = []; const methodList = methodNameList.map(list => list.replace(".", "")); let strDeclare = ""; let strWhile = ""; let variable = ""; if (modifiedLine.startsWith("PERFORM") && modifiedLine.includes("UNTIL") && modifiedLine.includes("VARYING")) { const arrLast = modifiedLine.split("UNTIL"); if (arrLast.length > 1) { strWhile = this.whileConversion(arrLast[1]); strWhile = "WHILE (" + strWhile + ")"; strWhile = strWhile.replace(".", ""); } const newLine = arrLast[0]; const regexSet = /VARYING(\s+[A-z0-9\-]+\s+)FROM /; if (regexSet.test(newLine)) { const match = newLine.match(regexSet); strDeclare = match ? match[1] : ""; variable = "SET " + strDeclare; } const regexPerform = /(PERFORM.*)VARYING/; if (regexPerform.test(newLine)) { const match = newLine.match(regexPerform); const strPerform = match ? match[1] : ""; performStmtList = this.performThruConversion(strPerform, methodList); if (performStmtList.length === 0) { performStmtList.push(strPerform); } } const regexFrom = /(FROM.*)UNTIL/; if (regexFrom.test(modifiedLine)) { const match = modifiedLine.match(regexFrom); const strFrom = match ? match[1] : ""; const regexFm = /FROM\s+(.*)\s+BY\s+(.*)/; if (regexFm.test(strFrom)) { const matchFrom = strFrom.match(regexFm); if (matchFrom) { variable += "= " + matchFrom[1]; strDeclare = strDeclare + "=" + strDeclare + " " + matchFrom[2]; } } } mainBlockList.push(variable); mainBlockList.push("DO"); mainBlockList.push(...performStmtList); mainBlockList.push(strDeclare); mainBlockList.push(strWhile); } else if (modifiedLine.startsWith("PERFORM") && modifiedLine.includes("UNTIL") && modifiedLine.includes("THRU")) { const arrLastPerform = modifiedLine.split("UNTIL"); if (arrLastPerform.length > 1) { strWhile = this.whileConversion(arrLastPerform[1]); strWhile = "WHILE (" + strWhile + ")"; strWhile = strWhile.replace(".", ""); } const regexDeclare = /(.*\s)/; if (regexDeclare.test(arrLastPerform[1])) { const match = arrLastPerform[1].match(regexDeclare); strDeclare = match ? match[1] : ""; } const arrDeclare = strDeclare.split(' ').filter(arr => arr.trim() !== ""); if (arrDeclare.length > 0) { const variableValue = cobolVariableList.find(f => f.variableName === arrDeclare[0]); if (variableValue) { variable = `SET ${strDeclare} ${variableValue.defaultValue}`; } } const perFormList = this.performThruConversion(arrLastPerform[0], methodList); mainBlockList.push(variable); mainBlockList.push("DO"); mainBlockList.push(...perFormList); mainBlockList.push(strWhile); } else if (modifiedLine.startsWith("PERFORM ") && modifiedLine.includes("THRU ")) { const perFormList = this.performThruConversion(modifiedLine, methodList); mainBlockList.push(...perFormList); } else if (!(modifiedLine.startsWith("PERFORM UNTIL"))) { if (modifiedLine.startsWith("PERFORM") && modifiedLine.includes("UNTIL") && !modifiedLine.includes("VARYING") && !modifiedLine.includes("THRU") && !modifiedLine.startsWith("PERFORM UNTIL")) { const arrLast = modifiedLine.split("UNTIL"); const strPerform = arrLast[0].trim(); const regexDeclare = /(.*\s)/; if (arrLast.length > 1) { if (regexDeclare.test(arrLast[1])) { const match = arrLast[1].match(regexDeclare); strDeclare = match ? match[1] : ""; } const arrDeclare = strDeclare.split(' ').filter(arr => arr.trim() !== ""); const varName = arrDeclare.length > 0 ? arrDeclare[0] : arrLast[1]; const variableValue = cobolVariableList.find(f => f.variableName === varName); if (variableValue) { const variableDefaultValue = variableValue.defaultValue; const nArr = arrDeclare.length > 1 ? arrDeclare[1] : ""; const variableName = `${arrDeclare[0]} ${nArr}`; strWhile = `WHILE (${variableName} ${variableDefaultValue})`; } else { strWhile = `WHILE (${varName})`; } mainBlockList.push("DO"); mainBlockList.push(strPerform); mainBlockList.push(strWhile); } } else { mainBlockList.push(modifiedLine); } } return mainBlockList; } static whileConversion(text) { let currentLine = text; if (currentLine.includes(">=")) { currentLine = currentLine.replace(">=", "<="); } else if (currentLine.includes("<=")) { currentLine = currentLine.replace("<=", ">="); } else if (currentLine.includes("<")) { currentLine = currentLine.replace("<", ">"); } else if (currentLine.includes(">")) { currentLine = currentLine.replace(">", "<"); } else if (currentLine.includes("=")) { currentLine = currentLine.replace("=", "<>"); } else { currentLine = currentLine + " is equal to TRUE"; } return currentLine; } // previously the name of this function was performThruConversion in C# static performThruConversion(text, methodList) { const performStmtList = []; const arrList = text.split("THRU"); if (!text.includes("THRU ")) return performStmtList; const sLine = arrList[0].replace("PERFORM", "").trim(); const eLine = arrList[1].replace(".", "").trim(); let indexPosition = -1; for (const list of methodList) { indexPosition++; if (list !== sLine) continue; for (let i = indexPosition; i < methodList.length; i++) { if (i >= methodList.length) break; let nLine = methodList[i].trim(); nLine = nLine.replace("PERFORM", ""); performStmtList.push("PERFORM " + nLine); if (nLine === eLine) { break; } } } return performStmtList; } static removeDotFromLines(allLines) { const lineDetails = []; allLines.forEach(cLine => { const trimmedLine = cLine.modifiedLine.trim(); if (!trimmedLine.endsWith(".")) { lineDetails.push({ ...cLine, modifiedLine: trimmedLine }); } else { const dotIndex = trimmedLine.lastIndexOf("."); const nLine = trimmedLine.substring(0, dotIndex).trim(); lineDetails.push({ ...cLine, modifiedLine: nLine }); } }); return lineDetails; } } exports.StatementProcessor = StatementProcessor; //# sourceMappingURL=statement-processor.js.map