@@ -125,31 +125,47 @@ abstract class RegexString extends StringLiteral {
125125 }
126126
127127 /**
128- * Gets the nesting depth of character classes at position `pos`
128+ * Holds if the character at `pos` starts a character set delimiter.
129+ * Result is 1 for `[` and 0 for `]`.
129130 */
130- private int charSetDepth ( int pos ) {
131- pos = - 1 and result = 0
131+ private int charSetDelimiter ( int pos ) {
132+ result = 1 and this . charSetStart0 ( pos , _ )
132133 or
133- exists ( this .getChar ( pos ) ) and
134- result =
135- max ( int j |
136- j = 0 or
137- j =
138- count ( int i | i < pos and this .charSetStart0 ( i , _) ) -
139- count ( int i | i < pos and this .charSetEnd0 ( i ) )
140- )
134+ result = - 1 and this .charSetEnd0 ( pos )
135+ }
136+
137+ /**
138+ * Holds if the char at `pos` is the one-based `index`th occourence of a character set delimiter (`[` or `]`).
139+ * Result is 1 for `[` and -1 for `]`.
140+ */
141+ private int charSetDelimiter ( int index , int pos ) {
142+ result = this .charSetDelimiter ( pos ) and
143+ pos = rank [ index ] ( int p | exists ( this .charSetDelimiter ( p ) ) )
144+ }
145+
146+ bindingset [ x]
147+ int max_zero ( int x ) { result = max ( [ x , 0 ] ) }
148+
149+ /**
150+ * Gets the nesting depth of character classes after position `pos`,
151+ * where `pos` is the position of a character set delimiter.
152+ */
153+ private int charSetDepth ( int index , int pos ) {
154+ index = 1 and result = max_zero ( charSetDelimiter ( index , pos ) )
155+ or
156+ result = max_zero ( charSetDelimiter ( index , pos ) + charSetDepth ( index - 1 , _) )
141157 }
142158
143159 /** Hold if a top-level character set starts between `start` and `end`. */
144160 predicate charSetStart ( int start , int end ) {
145161 this .charSetStart0 ( start , end ) and
146- this .charSetDepth ( start ) = 0
162+ this .charSetDepth ( _ , start ) = 1
147163 }
148164
149165 /** Holds if a top-level character set ends at `pos`. */
150166 predicate charSetEnd ( int pos ) {
151167 this .charSetEnd0 ( pos ) and
152- this .charSetDepth ( pos ) = 1
168+ this .charSetDepth ( _ , pos ) = 0
153169 }
154170
155171 /**
0 commit comments