1
+ import { assert } from 'o1js' ;
1
2
import { RegexCompiler } from './compiler.js' ;
2
3
import { Command } from 'commander' ;
3
4
@@ -9,16 +10,17 @@ program
9
10
. description ( 'CLI for ZK Regex Compiler in o1js' )
10
11
. argument ( '<rawRegex>' , 'Raw regex pattern to compile' )
11
12
. option ( '-c, --count' , 'Enable count for match regex pattern' )
12
- . option ( '-t, --revealTransitions <values...>' , 'Transitions to reveal' )
13
+ . option (
14
+ '-t, --revealTransitions <values...>' ,
15
+ 'Partial state transitions to reveal'
16
+ )
13
17
. option ( '-s, --revealSubpatterns <values...>' , 'Regex subpatterns to reveal' )
14
18
. action ( ( rawRegex , options ) => {
15
19
// Extract and set the options
16
20
const countEnabled = options . count || false ;
17
21
let revealEnabled = false ;
18
22
19
- // Initialize transitionInput to undefined
20
- let transitionInput : string [ ] | [ number , number ] [ ] [ ] | undefined =
21
- undefined ;
23
+ let revealInput : string [ ] | [ number , number ] [ ] [ ] | undefined = undefined ;
22
24
23
25
// Ensure only one of --revealTransitions or --revealSubpatterns is provided
24
26
if ( options . revealTransitions && options . revealSubpatterns ) {
@@ -28,20 +30,25 @@ program
28
30
process . exit ( 1 ) ;
29
31
}
30
32
33
+ // Initialize the RegexCompiler
34
+ const compiler = RegexCompiler . initialize ( rawRegex , true ) ;
35
+
31
36
// Set transitionInput and revealEnabled based on the provided option
32
37
if ( options . revealTransitions ) {
33
38
revealEnabled = true ;
34
- transitionInput = parseTransitions ( options . revealTransitions ) ;
39
+ revealInput = parseTransitions ( options . revealTransitions ) ;
40
+
41
+ assertTransitionsIncluded (
42
+ revealInput ,
43
+ compiler . extractSubPatternTransitions ( [ rawRegex ] ) . flat ( )
44
+ ) ;
35
45
} else if ( options . revealSubpatterns ) {
36
46
revealEnabled = true ;
37
- transitionInput = options . revealSubpatterns ;
47
+ revealInput = options . revealSubpatterns ;
38
48
}
39
49
40
- // Initialize the RegexCompiler
41
- const compiler = RegexCompiler . initialize ( rawRegex , true ) ;
42
-
43
50
// Print the regex circuit based on the options
44
- compiler . printRegexCircuit ( countEnabled , revealEnabled , transitionInput ) ;
51
+ compiler . printRegexCircuit ( countEnabled , revealEnabled , revealInput ) ;
45
52
} ) ;
46
53
47
54
// Parse the command-line arguments
@@ -60,6 +67,8 @@ function parseTransitions(inputArray: string[]): [number, number][][] {
60
67
61
68
// Extract pairs of numbers from the cleaned string
62
69
const pairs = cleanedString . match ( / \[ \d + , \d + \] / g) ;
70
+
71
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
63
72
return pairs ! . map ( ( pair ) => {
64
73
const [ a , b ] = pair . replace ( / [ [ \] ] / g, '' ) . split ( ',' ) . map ( Number ) ;
65
74
if ( isNaN ( a ) || isNaN ( b ) ) {
@@ -69,3 +78,32 @@ function parseTransitions(inputArray: string[]): [number, number][][] {
69
78
} ) ;
70
79
} ) ;
71
80
}
81
+
82
+ /**
83
+ * Assert that all transitions to reveal are included in the full transition array.
84
+ *
85
+ * @param transitionsToReveal - The array of transitions to reveal.
86
+ * @param fullTransitions - The full array of transitions.
87
+ */
88
+ function assertTransitionsIncluded (
89
+ transitionsToReveal : [ number , number ] [ ] [ ] ,
90
+ fullTransitions : [ number , number ] [ ]
91
+ ) : void {
92
+ // Convert fullTransitions to a Set of strings for quick lookup
93
+ const fullTransitionsSet = new Set (
94
+ fullTransitions . map ( ( transition ) => JSON . stringify ( transition ) )
95
+ ) ;
96
+
97
+ // Check each transition in transitionsToReveal
98
+ for ( const group of transitionsToReveal ) {
99
+ for ( const transition of group ) {
100
+ const isIncluded = fullTransitionsSet . has ( JSON . stringify ( transition ) ) ;
101
+ assert (
102
+ isIncluded ,
103
+ `Transition ${ JSON . stringify ( transition ) } not found!\n` +
104
+ `Please enter transitions that are part of your regex pattern transitions: ` +
105
+ `[${ Array . from ( fullTransitionsSet ) . join ( ', ' ) } ]`
106
+ ) ;
107
+ }
108
+ }
109
+ }
0 commit comments