diff --git a/src/core/FlowControl.js b/src/core/FlowControl.js index 9e01ef0a..aba64ab1 100755 --- a/src/core/FlowControl.js +++ b/src/core/FlowControl.js @@ -90,6 +90,66 @@ const FlowControl = { }, + /** + * Register operation. + * + * @param {Object} state - The current state of the recipe. + * @param {number} state.progress - The current position in the recipe. + * @param {Dish} state.dish - The Dish being operated on. + * @param {Operation[]} state.opList - The list of operations in the recipe. + * @returns {Object} The updated state of the recipe. + */ + runRegister: function(state) { + const ings = state.opList[state.progress].getIngValues(), + extractorStr = ings[0], + i = ings[1], + m = ings[2]; + + let modifiers = ""; + if (i) modifiers += "i"; + if (m) modifiers += "m"; + + const extractor = new RegExp(extractorStr, modifiers), + input = state.dish.get(Dish.STRING), + registers = input.match(extractor); + + /** + * Replaces references to registers (e.g. $R0) with the contents of those registers. + * + * @param {string} str + * @returns {string} + */ + function replaceRegister(str) { + // Replace references to registers ($Rn) with contents of registers + str = str.replace(/((?:^|[^\\])(?:\\.|[^\\])*?)\$R(\d{1,2})/g, (match, pre, regNum) => { + const index = parseInt(regNum, 10) + 1; + return (index >= registers.length) ? match : pre + registers[index]; + }); + + // Unescape remaining register references + return str.replace(/\\\$R(\d{1,2})/, "$R$1"); + } + + // Step through all subsequent ops and replace registers in args with extracted content + for (let i = state.progress + 1; i < state.opList.length; i++) { + let args = state.opList[i].getIngValues(); + args = args.map(arg => { + if (typeof arg !== "string" && typeof arg !== "object") return arg; + + if (typeof arg === "object" && arg.string) { + arg.string = replaceRegister(arg.string); + return arg; + } + + return replaceRegister(arg); + }); + state.opList[i].setIngValues(args); + } + + return state; + }, + + /** * Jump operation. * diff --git a/src/core/config/Categories.js b/src/core/config/Categories.js index d1ed8132..234f6919 100755 --- a/src/core/config/Categories.js +++ b/src/core/config/Categories.js @@ -317,6 +317,7 @@ const Categories = [ ops: [ "Fork", "Merge", + "Register", "Jump", "Conditional Jump", "Return", diff --git a/src/core/config/OperationConfig.js b/src/core/config/OperationConfig.js index d302abee..8f24e5f2 100755 --- a/src/core/config/OperationConfig.js +++ b/src/core/config/OperationConfig.js @@ -116,6 +116,30 @@ const OperationConfig = { flowControl: true, args: [] }, + "Register": { + module: "Default", + description: "Extract data from the input and store it in registers which can then be passed into subsequent operations as arguments. Regular expression capture groups are used to select the data to extract.

To use registers in arguments, refer to them using the notation $Rn where n is the register number, starting at 0.

For example:
Input: Test
Extractor: (.*)
Argument: $R0 becomes Test

Registers can be escaped in arguments using a backslash. e.g. \\$R0 would become $R0 rather than Test.", + inputType: "string", + outputType: "string", + flowControl: true, + args: [ + { + name: "Extractor", + type: "binaryString", + value: "([\\s\\S]*)" + }, + { + name: "Case insensitive", + type: "boolean", + value: true + }, + { + name: "Multiline matching", + type: "boolean", + value: false + }, + ] + }, "Jump": { module: "Default", description: "Jump forwards or backwards over the specified number of operations.", diff --git a/src/core/config/modules/Default.js b/src/core/config/modules/Default.js index 9af165d0..6e51367b 100644 --- a/src/core/config/modules/Default.js +++ b/src/core/config/modules/Default.js @@ -154,6 +154,7 @@ OpModules.Default = { "Generate HOTP": OTP.runHOTP, "Fork": FlowControl.runFork, "Merge": FlowControl.runMerge, + "Register": FlowControl.runRegister, "Jump": FlowControl.runJump, "Conditional Jump": FlowControl.runCondJump, "Return": FlowControl.runReturn,