Skip to content

Commit

Permalink
Split reveal into two different variadic CLI options
Browse files Browse the repository at this point in the history
  • Loading branch information
Shigoto-dev19 committed Jul 19, 2024
1 parent 6e32c2b commit 580636f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
56 changes: 47 additions & 9 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
import { RegexCompiler } from './compiler.js';
import { Command } from 'commander';

//TODO edit to add more flexibility removing JSON.parse()
//TODO take options into considersations --> split reveal transitions/subpattern

// Initialize the commander program
const program = new Command();

program
.version('0.1.0')
.description('CLI for ZK Regex Compiler in o1js')
.argument('<rawRegex>', 'Raw regex pattern to compile')
.option('-c, --count', 'Enable count')
.option('-r, --reveal <inputs>', 'Reveal regex pattern substring')
.option('-c, --count', 'Enable count for match regex pattern')
.option('-t, --revealTransitions <values...>', 'Transitions to reveal')
.option('-s, --revealSubpatterns <values...>', 'Regex subpatterns to reveal')
.action((rawRegex, options) => {
// Extract and set the options
const countEnabled = options.count || false;
const revealEnabled = options.reveal ? true : false;
const transitionInput = revealEnabled ? options.reveal : undefined;
let revealEnabled = false;

// Initialize transitionInput to undefined
let transitionInput: string[] | [number, number][][] | undefined =
undefined;

// Ensure only one of --revealTransitions or --revealSubpatterns is provided
if (options.revealTransitions && options.revealSubpatterns) {
console.error(
'Error: You can only use either --revealTransitions or --revealSubpatterns, not both!'
);
process.exit(1);
}

// Set transitionInput and revealEnabled based on the provided option
if (options.revealTransitions) {
revealEnabled = true;
transitionInput = parseTransitions(options.revealTransitions);
} else if (options.revealSubpatterns) {
revealEnabled = true;
transitionInput = options.revealSubpatterns;
}

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

// node build/src/cli.js '[a-z]' -r '[[[0, 1]]]'
// node build/src/cli.js '[a-z]' -r '["[a-z]"]'
// Function to parse input strings into an array of arrays of number pairs
function parseTransitions(inputArray: string[]): [number, number][][] {
return inputArray.map((str: string) => {
// Remove spaces and ensure the string matches the expected format
const cleanedString = str.replace(/\s/g, '');
const isValidFormat = /^\[\d+,\d+\](,\[\d+,\d+\])*$/.test(cleanedString);

if (!isValidFormat) {
throw new Error(`Invalid format: ${str}`);
}

// Extract pairs of numbers from the cleaned string
const pairs = cleanedString.match(/\[\d+,\d+\]/g);
return pairs!.map((pair) => {
const [a, b] = pair.replace(/[[\]]/g, '').split(',').map(Number);
if (isNaN(a) || isNaN(b)) {
throw new Error(`Invalid number in pair: ${pair}`);
}
return [a, b] as [number, number];
});
});
}
15 changes: 7 additions & 8 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,21 +465,20 @@ export class RegexCompiler {
return revealLines;
}

private writeRevealLines(revealEnabled: boolean, transitionInput?: string) {
private writeRevealLines(
revealEnabled: boolean,
transitionInput?: string[] | [number, number][][]
) {
let revealLines: string;
if (revealEnabled) {
const parsedInput: string[] | [number, number][][] = JSON.parse(
transitionInput ?? process.argv[3]
);

let revealedTransitions: [number, number][][];
// Type guard to check if parsedInput is an array of strings
try {
revealedTransitions = this.extractSubPatternTransitions(
parsedInput as string[]
transitionInput as string[]
);
} catch (error) {
revealedTransitions = parsedInput as [number, number][][];
revealedTransitions = transitionInput as [number, number][][];
}

revealLines =
Expand All @@ -495,7 +494,7 @@ export class RegexCompiler {
printRegexCircuit(
countEnabled: boolean,
revealEnabled: boolean,
transitionInput?: string
transitionInput?: string[] | [number, number][][]
) {
let circuitLines: string[] = [];
circuitLines = this.writeDeclarationLines()
Expand Down

0 comments on commit 580636f

Please sign in to comment.