forked from jasder/antlr
v4: Use generics for List and Map fields/locals to clean up several casts
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9325]
This commit is contained in:
parent
ee4a70cf16
commit
92bbdfbaea
|
@ -160,19 +160,19 @@ public class TokenRewriteStream extends CommonTokenStream {
|
|||
* I'm calling these things "programs."
|
||||
* Maps String (name) -> rewrite (List)
|
||||
*/
|
||||
protected Map programs = null;
|
||||
protected Map<String, List<RewriteOperation>> programs = null;
|
||||
|
||||
/** Map String (program name) -> Integer index */
|
||||
protected Map lastRewriteTokenIndexes = null;
|
||||
protected Map<String, Integer> lastRewriteTokenIndexes = null;
|
||||
|
||||
public TokenRewriteStream() {
|
||||
init();
|
||||
}
|
||||
|
||||
protected void init() {
|
||||
programs = new HashMap();
|
||||
programs.put(DEFAULT_PROGRAM_NAME, new ArrayList(PROGRAM_INIT_SIZE));
|
||||
lastRewriteTokenIndexes = new HashMap();
|
||||
programs = new HashMap<String, List<RewriteOperation>>();
|
||||
programs.put(DEFAULT_PROGRAM_NAME, new ArrayList<RewriteOperation>(PROGRAM_INIT_SIZE));
|
||||
lastRewriteTokenIndexes = new HashMap<String, Integer>();
|
||||
}
|
||||
|
||||
public TokenRewriteStream(TokenSource tokenSource) {
|
||||
|
@ -194,7 +194,7 @@ public class TokenRewriteStream extends CommonTokenStream {
|
|||
* longer in the stream. UNTESTED!
|
||||
*/
|
||||
public void rollback(String programName, int instructionIndex) {
|
||||
List is = (List)programs.get(programName);
|
||||
List<RewriteOperation> is = programs.get(programName);
|
||||
if ( is!=null ) {
|
||||
programs.put(programName, is.subList(MIN_TOKEN_INDEX,instructionIndex));
|
||||
}
|
||||
|
@ -240,7 +240,7 @@ public class TokenRewriteStream extends CommonTokenStream {
|
|||
|
||||
public void insertBefore(String programName, int index, Object text) {
|
||||
RewriteOperation op = new InsertBeforeOp(index,text);
|
||||
List rewrites = getProgram(programName);
|
||||
List<RewriteOperation> rewrites = getProgram(programName);
|
||||
op.instructionIndex = rewrites.size();
|
||||
rewrites.add(op);
|
||||
}
|
||||
|
@ -266,7 +266,7 @@ public class TokenRewriteStream extends CommonTokenStream {
|
|||
throw new IllegalArgumentException("replace: range invalid: "+from+".."+to+"(size="+tokens.size()+")");
|
||||
}
|
||||
RewriteOperation op = new ReplaceOp(from, to, text);
|
||||
List rewrites = getProgram(programName);
|
||||
List<RewriteOperation> rewrites = getProgram(programName);
|
||||
op.instructionIndex = rewrites.size();
|
||||
rewrites.add(op);
|
||||
}
|
||||
|
@ -318,16 +318,16 @@ public class TokenRewriteStream extends CommonTokenStream {
|
|||
lastRewriteTokenIndexes.put(programName, new Integer(i));
|
||||
}
|
||||
|
||||
protected List getProgram(String name) {
|
||||
List is = (List)programs.get(name);
|
||||
protected List<RewriteOperation> getProgram(String name) {
|
||||
List<RewriteOperation> is = programs.get(name);
|
||||
if ( is==null ) {
|
||||
is = initializeProgram(name);
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
private List initializeProgram(String name) {
|
||||
List is = new ArrayList(PROGRAM_INIT_SIZE);
|
||||
private List<RewriteOperation> initializeProgram(String name) {
|
||||
List<RewriteOperation> is = new ArrayList<RewriteOperation>(PROGRAM_INIT_SIZE);
|
||||
programs.put(name, is);
|
||||
return is;
|
||||
}
|
||||
|
@ -360,7 +360,7 @@ public class TokenRewriteStream extends CommonTokenStream {
|
|||
}
|
||||
|
||||
public String toString(String programName, int start, int end) {
|
||||
List rewrites = (List)programs.get(programName);
|
||||
List<RewriteOperation> rewrites = programs.get(programName);
|
||||
|
||||
// ensure start/end are in range
|
||||
if ( end>tokens.size()-1 ) end = tokens.size()-1;
|
||||
|
@ -372,14 +372,14 @@ public class TokenRewriteStream extends CommonTokenStream {
|
|||
StringBuilder buf = new StringBuilder();
|
||||
|
||||
// First, optimize instruction stream
|
||||
Map indexToOp = reduceToSingleOperationPerIndex(rewrites);
|
||||
Map<?, RewriteOperation> indexToOp = reduceToSingleOperationPerIndex(rewrites);
|
||||
|
||||
// Walk buffer, executing instructions and emitting tokens
|
||||
int i = start;
|
||||
while ( i <= end && i < tokens.size() ) {
|
||||
RewriteOperation op = (RewriteOperation)indexToOp.get(new Integer(i));
|
||||
RewriteOperation op = indexToOp.get(new Integer(i));
|
||||
indexToOp.remove(new Integer(i)); // remove so any left have index size-1
|
||||
Token t = (Token) tokens.get(i);
|
||||
Token t = tokens.get(i);
|
||||
if ( op==null ) {
|
||||
// no operation at that index, just dump token
|
||||
if ( t.getType()!=Token.EOF ) buf.append(t.getText());
|
||||
|
@ -396,9 +396,9 @@ public class TokenRewriteStream extends CommonTokenStream {
|
|||
if ( end==tokens.size()-1 ) {
|
||||
// Scan any remaining operations after last token
|
||||
// should be included (they will be inserts).
|
||||
Iterator it = indexToOp.values().iterator();
|
||||
Iterator<RewriteOperation> it = indexToOp.values().iterator();
|
||||
while (it.hasNext()) {
|
||||
RewriteOperation op = (RewriteOperation)it.next();
|
||||
RewriteOperation op = it.next();
|
||||
if ( op.index >= tokens.size()-1 ) buf.append(op.text);
|
||||
}
|
||||
}
|
||||
|
@ -454,19 +454,19 @@ public class TokenRewriteStream extends CommonTokenStream {
|
|||
*
|
||||
* Return a map from token index to operation.
|
||||
*/
|
||||
protected Map reduceToSingleOperationPerIndex(List rewrites) {
|
||||
protected Map<Integer, RewriteOperation> reduceToSingleOperationPerIndex(List<RewriteOperation> rewrites) {
|
||||
// System.out.println("rewrites="+rewrites);
|
||||
|
||||
// WALK REPLACES
|
||||
for (int i = 0; i < rewrites.size(); i++) {
|
||||
RewriteOperation op = (RewriteOperation)rewrites.get(i);
|
||||
RewriteOperation op = rewrites.get(i);
|
||||
if ( op==null ) continue;
|
||||
if ( !(op instanceof ReplaceOp) ) continue;
|
||||
ReplaceOp rop = (ReplaceOp)rewrites.get(i);
|
||||
// Wipe prior inserts within range
|
||||
List inserts = getKindOfOps(rewrites, InsertBeforeOp.class, i);
|
||||
List<InsertBeforeOp> inserts = getKindOfOps(rewrites, InsertBeforeOp.class, i);
|
||||
for (int j = 0; j < inserts.size(); j++) {
|
||||
InsertBeforeOp iop = (InsertBeforeOp) inserts.get(j);
|
||||
InsertBeforeOp iop = inserts.get(j);
|
||||
if ( iop.index == rop.index ) {
|
||||
// E.g., insert before 2, delete 2..2; update replace
|
||||
// text to include insert before, kill insert
|
||||
|
@ -479,9 +479,9 @@ public class TokenRewriteStream extends CommonTokenStream {
|
|||
}
|
||||
}
|
||||
// Drop any prior replaces contained within
|
||||
List prevReplaces = getKindOfOps(rewrites, ReplaceOp.class, i);
|
||||
List<ReplaceOp> prevReplaces = getKindOfOps(rewrites, ReplaceOp.class, i);
|
||||
for (int j = 0; j < prevReplaces.size(); j++) {
|
||||
ReplaceOp prevRop = (ReplaceOp) prevReplaces.get(j);
|
||||
ReplaceOp prevRop = prevReplaces.get(j);
|
||||
if ( prevRop.index>=rop.index && prevRop.lastIndex <= rop.lastIndex ) {
|
||||
// delete replace as it's a no-op.
|
||||
rewrites.set(prevRop.instructionIndex, null);
|
||||
|
@ -510,14 +510,14 @@ public class TokenRewriteStream extends CommonTokenStream {
|
|||
|
||||
// WALK INSERTS
|
||||
for (int i = 0; i < rewrites.size(); i++) {
|
||||
RewriteOperation op = (RewriteOperation)rewrites.get(i);
|
||||
RewriteOperation op = rewrites.get(i);
|
||||
if ( op==null ) continue;
|
||||
if ( !(op instanceof InsertBeforeOp) ) continue;
|
||||
InsertBeforeOp iop = (InsertBeforeOp)rewrites.get(i);
|
||||
// combine current insert with prior if any at same index
|
||||
List prevInserts = getKindOfOps(rewrites, InsertBeforeOp.class, i);
|
||||
List<InsertBeforeOp> prevInserts = getKindOfOps(rewrites, InsertBeforeOp.class, i);
|
||||
for (int j = 0; j < prevInserts.size(); j++) {
|
||||
InsertBeforeOp prevIop = (InsertBeforeOp) prevInserts.get(j);
|
||||
InsertBeforeOp prevIop = prevInserts.get(j);
|
||||
if ( prevIop.index == iop.index ) { // combine objects
|
||||
// convert to strings...we're in process of toString'ing
|
||||
// whole token buffer so no lazy eval issue with any templates
|
||||
|
@ -527,9 +527,9 @@ public class TokenRewriteStream extends CommonTokenStream {
|
|||
}
|
||||
}
|
||||
// look for replaces where iop.index is in range; error
|
||||
List prevReplaces = getKindOfOps(rewrites, ReplaceOp.class, i);
|
||||
List<ReplaceOp> prevReplaces = getKindOfOps(rewrites, ReplaceOp.class, i);
|
||||
for (int j = 0; j < prevReplaces.size(); j++) {
|
||||
ReplaceOp rop = (ReplaceOp) prevReplaces.get(j);
|
||||
ReplaceOp rop = prevReplaces.get(j);
|
||||
if ( iop.index == rop.index ) {
|
||||
rop.text = catOpText(iop.text,rop.text);
|
||||
rewrites.set(i, null); // delete current insert
|
||||
|
@ -542,9 +542,9 @@ public class TokenRewriteStream extends CommonTokenStream {
|
|||
}
|
||||
}
|
||||
// System.out.println("rewrites after="+rewrites);
|
||||
Map m = new HashMap();
|
||||
Map<Integer, RewriteOperation> m = new HashMap<Integer, RewriteOperation>();
|
||||
for (int i = 0; i < rewrites.size(); i++) {
|
||||
RewriteOperation op = (RewriteOperation)rewrites.get(i);
|
||||
RewriteOperation op = rewrites.get(i);
|
||||
if ( op==null ) continue; // ignore deleted ops
|
||||
if ( m.get(new Integer(op.index))!=null ) {
|
||||
throw new Error("should only be one op per index");
|
||||
|
@ -562,17 +562,21 @@ public class TokenRewriteStream extends CommonTokenStream {
|
|||
if ( b!=null ) y = b.toString();
|
||||
return x+y;
|
||||
}
|
||||
protected List getKindOfOps(List rewrites, Class kind) {
|
||||
|
||||
protected <T extends RewriteOperation> List<T> getKindOfOps(List<? extends RewriteOperation> rewrites, Class<T> kind) {
|
||||
return getKindOfOps(rewrites, kind, rewrites.size());
|
||||
}
|
||||
|
||||
/** Get all operations before an index of a particular kind */
|
||||
protected List getKindOfOps(List rewrites, Class kind, int before) {
|
||||
List ops = new ArrayList();
|
||||
protected <T extends RewriteOperation> List<T> getKindOfOps(List<? extends RewriteOperation> rewrites, Class<T> kind, int before) {
|
||||
List<T> ops = new ArrayList<T>();
|
||||
for (int i=0; i<before && i<rewrites.size(); i++) {
|
||||
RewriteOperation op = (RewriteOperation)rewrites.get(i);
|
||||
RewriteOperation op = rewrites.get(i);
|
||||
if ( op==null ) continue; // ignore deleted
|
||||
if ( op.getClass() == kind ) ops.add(op);
|
||||
if ( op.getClass() == kind ) {
|
||||
//noinspection unchecked
|
||||
ops.add((T)op);
|
||||
}
|
||||
}
|
||||
return ops;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue