11import { RegexCompiler } from './compiler.js' ;
22import { 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
85const program = new Command ( ) ;
96
107program
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
3048program . 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+ }
0 commit comments