From a41bdb237c730ced909936b647853dabeb1e5ced Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Sat, 20 Jul 2024 11:33:27 +0300 Subject: [PATCH] Add option to specify functionName for compiled regex circuit --- src/cli.ts | 14 +++++++++++++- src/compiler.ts | 5 +++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 7c01eb5..28ef979 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -15,10 +15,17 @@ program 'Partial state transitions to reveal' ) .option('-s, --revealSubpatterns ', 'Regex subpatterns to reveal') + .option( + '-n, --functionName ', + 'Function name to give to the regex circuit' + ) .action((rawRegex, options) => { // Extract and set the options const countEnabled = options.count || false; let revealEnabled = false; + const functionName = options.functionName + ? 'function ' + options.functionName + : ''; let revealInput: string[] | [number, number][][] | undefined = undefined; @@ -48,7 +55,12 @@ program } // Print the regex circuit based on the options - compiler.printRegexCircuit(countEnabled, revealEnabled, revealInput); + compiler.printRegexCircuit( + countEnabled, + revealEnabled, + revealInput, + functionName + ); }); // Parse the command-line arguments diff --git a/src/compiler.ts b/src/compiler.ts index 83b17f5..c46707c 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -494,7 +494,8 @@ export class RegexCompiler { printRegexCircuit( countEnabled: boolean, revealEnabled: boolean, - transitionInput?: string[] | [number, number][][] + transitionInput?: string[] | [number, number][][], + functionName?: string ) { let circuitLines: string[] = []; circuitLines = this.writeDeclarationLines() @@ -503,7 +504,7 @@ export class RegexCompiler { .concat(this.writeAcceptLines(countEnabled)); const stringRegexCircuit = - '\n(input: UInt8[]) {\n' + + `\n${functionName}(input: UInt8[]) {\n` + circuitLines.join('\n\t') + this.writeRevealLines(revealEnabled, transitionInput) + '\n}';