Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6e32c2b

Browse files
committedJul 17, 2024
First functional CLI same as the zk-regex script
1 parent 1a77232 commit 6e32c2b

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed
 

‎src/cli.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
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+
47
// Initialize the commander program
58
const program = new Command();
69

710
program
8-
.version('1.0.0')
9-
.description('CLI for Regex Compiler')
11+
.version('0.1.0')
12+
.description('CLI for ZK Regex Compiler in o1js')
1013
.argument('<rawRegex>', 'Raw regex pattern to compile')
1114
.option('-c, --count', 'Enable count')
15+
.option('-r, --reveal <inputs>', 'Reveal regex pattern substring')
1216
.action((rawRegex, options) => {
17+
// Extract and set the options
1318
const countEnabled = options.count || false;
19+
const revealEnabled = options.reveal ? true : false;
20+
const transitionInput = revealEnabled ? options.reveal : undefined;
1421

22+
// Initialize the RegexCompiler
1523
const compiler = RegexCompiler.initialize(rawRegex, true);
16-
compiler.printRegexCircuit(countEnabled, false);
24+
25+
// Print the regex circuit based on the options
26+
compiler.printRegexCircuit(countEnabled, revealEnabled, transitionInput);
1727
});
1828

1929
// Parse the command-line arguments
2030
program.parse(process.argv);
31+
32+
// node build/src/cli.js '[a-z]' -r '[[[0, 1]]]'
33+
// node build/src/cli.js '[a-z]' -r '["[a-z]"]'

‎src/compiler.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,11 @@ export class RegexCompiler {
465465
return revealLines;
466466
}
467467

468-
private writeRevealLines(revealEnabled: boolean) {
468+
private writeRevealLines(revealEnabled: boolean, transitionInput?: string) {
469469
let revealLines: string;
470470
if (revealEnabled) {
471471
const parsedInput: string[] | [number, number][][] = JSON.parse(
472-
process.argv[3]
472+
transitionInput ?? process.argv[3]
473473
);
474474

475475
let revealedTransitions: [number, number][][];
@@ -492,7 +492,11 @@ export class RegexCompiler {
492492
return revealLines;
493493
}
494494

495-
printRegexCircuit(countEnabled: boolean, revealEnabled: boolean) {
495+
printRegexCircuit(
496+
countEnabled: boolean,
497+
revealEnabled: boolean,
498+
transitionInput?: string
499+
) {
496500
let circuitLines: string[] = [];
497501
circuitLines = this.writeDeclarationLines()
498502
.concat(this.writeInitLines())
@@ -502,7 +506,7 @@ export class RegexCompiler {
502506
const stringRegexCircuit =
503507
'\n(input: UInt8[]) {\n' +
504508
circuitLines.join('\n\t') +
505-
this.writeRevealLines(revealEnabled) +
509+
this.writeRevealLines(revealEnabled, transitionInput) +
506510
'\n}';
507511

508512
const BOLD_GREEN = '\x1b[32;1m';

0 commit comments

Comments
 (0)
Failed to load comments.