TokenStreamRewriter.reduceToSingleOperationPerIndex

We need to combine operations and report invalid operations (like overlapping replaces that are not completed nested). Inserts to same index need to be combined etc.

Here are the cases:

I.i.u I.j.v leave alone, nonoverlapping<br> I.i.u I.i.v combine: Iivu

R.i-j.u R.x-y.v | i-j in x-y delete first R<br> R.i-j.u R.i-j.v delete first R<br> R.i-j.u R.x-y.v | x-y in i-j ERROR<br> R.i-j.u R.x-y.v | boundaries overlap ERROR

Delete special case of replace (text==null):<br> D.i-j.u D.x-y.v | boundaries overlap combine to max(min)..max(right)

I.i.u R.x-y.v | i in (x+1)-y delete I (since insert before<br> we're not deleting i)<br> I.i.u R.x-y.v | i not in (x+1)-y leave alone, nonoverlapping<br> R.x-y.v I.i.u | i in x-y ERROR<br> R.x-y.v I.x.u R.x-y.uv (combine, delete I)<br> R.x-y.v I.i.u | i not in x-y leave alone, nonoverlapping

I.i.u = insert u before op @ index i<br> R.x-y.u = replace x-y indexed tokens with u

First we need to examine replaces. For any replace op:

1. wipe out any insertions before op within that range.<br> 2. Drop any replace op before that is contained completely within that range.<br> 3. Throw exception upon boundary overlap with any previous replace.

Then we can deal with inserts:

1. for any inserts to same index, combine even if not adjacent.<br> 2. for any prior replace with same left boundary, combine this insert with replace and delete this replace.<br> 3. throw exception if index in same range as previous replace

Don't actually delete; make op null in list. Easier to walk list. Later we can throw as we add to index &rarr; op map.

Note that I.2 R.2-2 will wipe out I.2 even though, technically, the inserted stuff would be before the replace range. But, if you add tokens in front of a method body '{' and then delete the method body, I think the stuff before the '{' you added should disappear too.

Return: a map from token index to operation.

class TokenStreamRewriter
protected
reduceToSingleOperationPerIndex

Meta