@@ -141,10 +141,14 @@ private predicate hasDeviationCommentFileOrdering(
141
141
)
142
142
}
143
143
144
- private predicate mkBeginStack ( DeviationRecord record , File file , BeginStack stack , int index ) {
144
+ /**
145
+ * Calculate the stack of deviation begin markers related to the given deviation record, in the given file,
146
+ * at the given `markerRecordFileIndex` into the list of deviation markers for that record in that file.
147
+ */
148
+ private BeginStack calculateBeginStack ( DeviationRecord record , File file , int markerRecordFileIndex ) {
145
149
// Stack is empty at the start
146
- index = 0 and
147
- stack = TEmptyBeginStack ( ) and
150
+ markerRecordFileIndex = 0 and
151
+ result = TEmptyBeginStack ( ) and
148
152
// Only initialize when there is at least one such comment marker for this file and record
149
153
// pairing
150
154
exists ( CommentDeviationRangeMarker marker |
@@ -154,36 +158,42 @@ private predicate mkBeginStack(DeviationRecord record, File file, BeginStack sta
154
158
// Next token is begin, so push it to the stack
155
159
exists ( DeviationBegin begin , BeginStack prev |
156
160
record = begin .getRecord ( ) and
157
- hasDeviationCommentFileOrdering ( record , begin , file , index ) and
158
- mkBeginStack ( record , file , prev , index - 1 ) and
159
- stack = TConsBeginStack ( begin , prev )
161
+ hasDeviationCommentFileOrdering ( record , begin , file , markerRecordFileIndex ) and
162
+ prev = calculateBeginStack ( record , file , markerRecordFileIndex - 1 ) and
163
+ result = TConsBeginStack ( begin , prev )
160
164
)
161
165
or
162
166
// Next token is end
163
167
exists ( DeviationEnd end , BeginStack prevStack |
164
168
record = end .getRecord ( ) and
165
- hasDeviationCommentFileOrdering ( record , end , file , index ) and
166
- mkBeginStack ( record , file , prevStack , index - 1 )
169
+ hasDeviationCommentFileOrdering ( record , end , file , markerRecordFileIndex ) and
170
+ prevStack = calculateBeginStack ( record , file , markerRecordFileIndex - 1 )
167
171
|
168
172
// There is, so pop the most recent begin off the stack
169
- prevStack = TConsBeginStack ( _, stack )
173
+ prevStack = TConsBeginStack ( _, result )
170
174
or
171
- // Error, no begin on the stack, ignore and continue
175
+ // Error, no begin on the stack, ignore the end and continue
172
176
prevStack = TEmptyBeginStack ( ) and
173
- stack = TEmptyBeginStack ( )
177
+ result = TEmptyBeginStack ( )
174
178
)
175
179
}
176
180
177
181
newtype TBeginStack =
178
182
TConsBeginStack ( DeviationBegin begin , TBeginStack prev ) {
179
183
exists ( File file , int index |
180
184
hasDeviationCommentFileOrdering ( begin .getRecord ( ) , begin , file , index ) and
181
- mkBeginStack ( begin .getRecord ( ) , file , prev , index - 1 )
185
+ prev = calculateBeginStack ( begin .getRecord ( ) , file , index - 1 )
182
186
)
183
187
} or
184
188
TEmptyBeginStack ( )
185
189
190
+ /**
191
+ * A stack of begin markers that occur in the same file, referring to the same record.
192
+ */
186
193
private class BeginStack extends TBeginStack {
194
+ /** Gets the top begin marker on the stack. */
195
+ DeviationBegin peek ( ) { this = TConsBeginStack ( result , _) }
196
+
187
197
string toString ( ) {
188
198
exists ( DeviationBegin begin , BeginStack prev | this = TConsBeginStack ( begin , prev ) |
189
199
result = "(" + begin + ", " + prev .toString ( ) + ")"
@@ -198,7 +208,7 @@ predicate isDeviationRangePaired(DeviationRecord record, DeviationBegin begin, D
198
208
exists ( File file , int index |
199
209
record = end .getRecord ( ) and
200
210
hasDeviationCommentFileOrdering ( record , end , file , index ) and
201
- mkBeginStack ( record , file , TConsBeginStack ( begin , _ ) , index - 1 )
211
+ begin = calculateBeginStack ( record , file , index - 1 ) . peek ( )
202
212
)
203
213
}
204
214
0 commit comments