Skip to content

Commit 580636f

Browse files
committed
Split reveal into two different variadic CLI options
1 parent 6e32c2b commit 580636f

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

src/cli.ts

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
11
import { RegexCompiler } from './compiler.js';
22
import { Command } from 'commander';
33

4-
//TODO edit to add more flexibility removing JSON.parse()
5-
//TODO take options into considersations --> split reveal transitions/subpattern
6-
74
// Initialize the commander program
85
const program = new Command();
96

107
program
118
.version('0.1.0')
129
.description('CLI for ZK Regex Compiler in o1js')
1310
.argument('<rawRegex>', 'Raw regex pattern to compile')
14-
.option('-c, --count', 'Enable count')
15-
.option('-r, --reveal <inputs>', 'Reveal regex pattern substring')
11+
.option('-c, --count', 'Enable count for match regex pattern')
12+
.option('-t, --revealTransitions <values...>', 'Transitions to reveal')
13+
.option('-s, --revealSubpatterns <values...>', 'Regex subpatterns to reveal')
1614
.action((rawRegex, options) => {
1715
// Extract and set the options
1816
const countEnabled = options.count || false;
19-
const revealEnabled = options.reveal ? true : false;
20-
const transitionInput = revealEnabled ? options.reveal : undefined;
17+
let revealEnabled = false;
18+
19+
// Initialize transitionInput to undefined
20+
let transitionInput: string[] | [number, number][][] | undefined =
21+
undefined;
22+
23+
// Ensure only one of --revealTransitions or --revealSubpatterns is provided
24+
if (options.revealTransitions && options.revealSubpatterns) {
25+
console.error(
26+
'Error: You can only use either --revealTransitions or --revealSubpatterns, not both!'
27+
);
28+
process.exit(1);
29+
}
30+
31+
// Set transitionInput and revealEnabled based on the provided option
32+
if (options.revealTransitions) {
33+
revealEnabled = true;
34+
transitionInput = parseTransitions(options.revealTransitions);
35+
} else if (options.revealSubpatterns) {
36+
revealEnabled = true;
37+
transitionInput = options.revealSubpatterns;
38+
}
2139

2240
// Initialize the RegexCompiler
2341
const compiler = RegexCompiler.initialize(rawRegex, true);
@@ -29,5 +47,25 @@ program
2947
// Parse the command-line arguments
3048
program.parse(process.argv);
3149

32-
// node build/src/cli.js '[a-z]' -r '[[[0, 1]]]'
33-
// node build/src/cli.js '[a-z]' -r '["[a-z]"]'
50+
// Function to parse input strings into an array of arrays of number pairs
51+
function parseTransitions(inputArray: string[]): [number, number][][] {
52+
return inputArray.map((str: string) => {
53+
// Remove spaces and ensure the string matches the expected format
54+
const cleanedString = str.replace(/\s/g, '');
55+
const isValidFormat = /^\[\d+,\d+\](,\[\d+,\d+\])*$/.test(cleanedString);
56+
57+
if (!isValidFormat) {
58+
throw new Error(`Invalid format: ${str}`);
59+
}
60+
61+
// Extract pairs of numbers from the cleaned string
62+
const pairs = cleanedString.match(/\[\d+,\d+\]/g);
63+
return pairs!.map((pair) => {
64+
const [a, b] = pair.replace(/[[\]]/g, '').split(',').map(Number);
65+
if (isNaN(a) || isNaN(b)) {
66+
throw new Error(`Invalid number in pair: ${pair}`);
67+
}
68+
return [a, b] as [number, number];
69+
});
70+
});
71+
}

src/compiler.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -465,21 +465,20 @@ export class RegexCompiler {
465465
return revealLines;
466466
}
467467

468-
private writeRevealLines(revealEnabled: boolean, transitionInput?: string) {
468+
private writeRevealLines(
469+
revealEnabled: boolean,
470+
transitionInput?: string[] | [number, number][][]
471+
) {
469472
let revealLines: string;
470473
if (revealEnabled) {
471-
const parsedInput: string[] | [number, number][][] = JSON.parse(
472-
transitionInput ?? process.argv[3]
473-
);
474-
475474
let revealedTransitions: [number, number][][];
476475
// Type guard to check if parsedInput is an array of strings
477476
try {
478477
revealedTransitions = this.extractSubPatternTransitions(
479-
parsedInput as string[]
478+
transitionInput as string[]
480479
);
481480
} catch (error) {
482-
revealedTransitions = parsedInput as [number, number][][];
481+
revealedTransitions = transitionInput as [number, number][][];
483482
}
484483

485484
revealLines =
@@ -495,7 +494,7 @@ export class RegexCompiler {
495494
printRegexCircuit(
496495
countEnabled: boolean,
497496
revealEnabled: boolean,
498-
transitionInput?: string
497+
transitionInput?: string[] | [number, number][][]
499498
) {
500499
let circuitLines: string[] = [];
501500
circuitLines = this.writeDeclarationLines()

0 commit comments

Comments
 (0)