1
- /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
1
+ /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2
2
* Use of this file is governed by the BSD 3-clause license that
3
3
* can be found in the LICENSE.txt file in the project root.
4
4
*/
@@ -313,6 +313,10 @@ std::string TokenStreamRewriter::getText(const std::string &programName, const I
313
313
std::unordered_map<size_t , TokenStreamRewriter::RewriteOperation*> TokenStreamRewriter::reduceToSingleOperationPerIndex (
314
314
std::vector<TokenStreamRewriter::RewriteOperation*> &rewrites) {
315
315
316
+ // Reset the instructionIndex
317
+ for (size_t i = 0 ; i < rewrites.size (); ++i) {
318
+ rewrites[i]->instructionIndex = i;
319
+ }
316
320
317
321
// WALK REPLACES
318
322
for (size_t i = 0 ; i < rewrites.size (); ++i) {
@@ -327,35 +331,34 @@ std::unordered_map<size_t, TokenStreamRewriter::RewriteOperation*> TokenStreamRe
327
331
if (iop->index == rop->index ) {
328
332
// E.g., insert before 2, delete 2..2; update replace
329
333
// text to include insert before, kill insert
330
- delete rewrites[iop->instructionIndex ];
331
- rewrites[iop->instructionIndex ] = nullptr ;
332
334
rop->text = iop->text + (!rop->text .empty () ? rop->text : " " );
335
+ rewrites[iop->instructionIndex ] = nullptr ;
336
+ delete iop;
333
337
}
334
338
else if (iop->index > rop->index && iop->index <= rop->lastIndex ) {
335
339
// delete insert as it's a no-op.
336
- delete rewrites[iop->instructionIndex ];
337
340
rewrites[iop->instructionIndex ] = nullptr ;
341
+ delete iop;
338
342
}
339
343
}
340
344
// Drop any prior replaces contained within
341
345
std::vector<ReplaceOp*> prevReplaces = getKindOfOps<ReplaceOp>(rewrites, i);
342
346
for (auto *prevRop : prevReplaces) {
343
347
if (prevRop->index >= rop->index && prevRop->lastIndex <= rop->lastIndex ) {
344
348
// delete replace as it's a no-op.
345
- delete rewrites[prevRop->instructionIndex ];
346
349
rewrites[prevRop->instructionIndex ] = nullptr ;
350
+ delete prevRop;
347
351
continue ;
348
352
}
349
353
// throw exception unless disjoint or identical
350
354
bool disjoint = prevRop->lastIndex < rop->index || prevRop->index > rop->lastIndex ;
351
355
// Delete special case of replace (text==null):
352
356
// D.i-j.u D.x-y.v | boundaries overlap combine to max(min)..max(right)
353
357
if (prevRop->text .empty () && rop->text .empty () && !disjoint) {
354
- delete rewrites[prevRop->instructionIndex ];
355
- rewrites[prevRop->instructionIndex ] = nullptr ; // kill first delete
356
358
rop->index = std::min (prevRop->index , rop->index );
357
359
rop->lastIndex = std::max (prevRop->lastIndex , rop->lastIndex );
358
- std::cout << " new rop " << rop << std::endl;
360
+ rewrites[prevRop->instructionIndex ] = nullptr ; // kill first delete
361
+ delete prevRop;
359
362
}
360
363
else if (!disjoint) {
361
364
throw IllegalArgumentException (" replace op boundaries of " + rop->toString () +
@@ -379,8 +382,8 @@ std::unordered_map<size_t, TokenStreamRewriter::RewriteOperation*> TokenStreamRe
379
382
// whole token buffer so no lazy eval issue with any templates
380
383
iop->text = catOpText (&iop->text , &prevIop->text );
381
384
// delete redundant prior insert
382
- delete rewrites[prevIop->instructionIndex ];
383
385
rewrites[prevIop->instructionIndex ] = nullptr ;
386
+ delete prevIop;
384
387
}
385
388
}
386
389
// look for replaces where iop.index is in range; error
0 commit comments