From 8f6851b86bd5ab1dbc3f4bbdf4782ef00cfcc9ea Mon Sep 17 00:00:00 2001 From: dgelessus <dgelessus@users.noreply.github.com> Date: Tue, 5 Apr 2022 18:13:31 +0200 Subject: [PATCH] Remove TypedLinkedList in favor of LinkedList and real generics --- .../sablecc/sablecc/AddAstProductions.java | 10 +- .../java/org/sablecc/sablecc/GenAlts.java | 52 ++------ .../java/org/sablecc/sablecc/GenAnalyses.java | 68 +++------- .../java/org/sablecc/sablecc/GenLexer.java | 18 +-- .../java/org/sablecc/sablecc/Inlining.java | 31 +++-- .../InternalTransformationsToGrammar.java | 2 +- .../org/sablecc/sablecc/MacroExpander.java | 10 +- .../RecursiveProductionsDetections.java | 2 +- .../java/org/sablecc/sablecc/ResolveIds.java | 4 +- .../org/sablecc/sablecc/TypedLinkedList.java | 121 ------------------ 10 files changed, 67 insertions(+), 251 deletions(-) delete mode 100644 src/main/java/org/sablecc/sablecc/TypedLinkedList.java diff --git a/src/main/java/org/sablecc/sablecc/AddAstProductions.java b/src/main/java/org/sablecc/sablecc/AddAstProductions.java index 3238138..43adccb 100644 --- a/src/main/java/org/sablecc/sablecc/AddAstProductions.java +++ b/src/main/java/org/sablecc/sablecc/AddAstProductions.java @@ -26,7 +26,7 @@ import java.io.*; public class AddAstProductions extends DepthFirstAdapter { - LinkedList listAstProd = new TypedLinkedList(); + List<AAstProd> listAstProd = new LinkedList<>(); private boolean firstAlt; public AddAstProductions() @@ -35,7 +35,7 @@ public class AddAstProductions extends DepthFirstAdapter public void caseAProd(AProd node) { firstAlt = true; - listOfAstAlts = new TypedLinkedList(); + listOfAstAlts = new LinkedList<>(); /* * Here, we assume that if there is no Abstract Syntax Tree Section specified @@ -63,7 +63,7 @@ public class AddAstProductions extends DepthFirstAdapter public void inAAlt(AAlt node) { - listElems = new TypedLinkedList(); + listElems = new LinkedList<>(); processingParsedAlt = true; } @@ -95,8 +95,8 @@ public class AddAstProductions extends DepthFirstAdapter } } - LinkedList listElems; - LinkedList listOfAstAlts; + List<AElem> listElems; + List<AAstAlt> listOfAstAlts; public void error(Token token) { diff --git a/src/main/java/org/sablecc/sablecc/GenAlts.java b/src/main/java/org/sablecc/sablecc/GenAlts.java index dfd886a..2aedd3e 100644 --- a/src/main/java/org/sablecc/sablecc/GenAlts.java +++ b/src/main/java/org/sablecc/sablecc/GenAlts.java @@ -18,7 +18,7 @@ public class GenAlts extends DepthFirstAdapter private ResolveAstIds ast_ids; private File pkgDir; private String pkgName; - private List elemList; + private List<ElemInfo> elemList; private String currentProd; ElemInfo info; @@ -57,7 +57,7 @@ public class GenAlts extends DepthFirstAdapter public void inAAstAlt(AAstAlt node) { - elemList = new TypedLinkedList(ElemInfoCast.instance); + elemList = new LinkedList<>(); } public void caseAProductions(AProductions node) @@ -117,9 +117,7 @@ public class GenAlts extends DepthFirstAdapter boolean hasOperator = false; boolean hasList = false; - for(Iterator i = elemList.iterator(); i.hasNext();) - { - ElemInfo info = (ElemInfo) i.next(); + for(ElemInfo info : elemList) { if(info != null) switch(info.operator) { @@ -143,9 +141,8 @@ public class GenAlts extends DepthFirstAdapter ast_ids.astIds.pkgName.equals("") ? "analysis" : ast_ids.astIds.pkgName + ".analysis", name, currentProd}); - for(Iterator i = elemList.iterator(); i.hasNext();) + for(ElemInfo info : elemList) { - ElemInfo info = (ElemInfo) i.next(); if(info != null) switch(info.operator) { @@ -178,9 +175,9 @@ public class GenAlts extends DepthFirstAdapter macros.apply(file, "ConstructorHeader", new String[] {name}); - for(Iterator i = elemList.iterator(); i.hasNext();) + for(Iterator<ElemInfo> i = elemList.iterator(); i.hasNext();) { - ElemInfo info = (ElemInfo) i.next(); + ElemInfo info = i.next(); if(info != null) switch(info.operator) { @@ -203,10 +200,8 @@ public class GenAlts extends DepthFirstAdapter macros.apply(file, "ConstructorBodyHeader", null); - for(Iterator i = elemList.iterator(); i.hasNext();) + for(ElemInfo info : elemList) { - ElemInfo info = (ElemInfo) i.next(); - if(info != null ) switch(info.operator) { @@ -234,9 +229,9 @@ public class GenAlts extends DepthFirstAdapter macros.apply(file, "CloneHeader", new String[] {name}); - for(Iterator i = elemList.iterator(); i.hasNext();) + for(Iterator<ElemInfo> i = elemList.iterator(); i.hasNext();) { - ElemInfo info = (ElemInfo) i.next(); + ElemInfo info = i.next(); if(info != null) switch(info.operator) { @@ -261,10 +256,8 @@ public class GenAlts extends DepthFirstAdapter macros.apply(file, "Apply", new String[] {name}); - for(Iterator i = elemList.iterator(); i.hasNext();) + for(ElemInfo info : elemList) { - ElemInfo info = (ElemInfo) i.next(); - if(info != null) switch(info.operator) { @@ -286,10 +279,8 @@ public class GenAlts extends DepthFirstAdapter } macros.apply(file, "ToStringHeader", null); - for(Iterator i = elemList.iterator(); i.hasNext();) + for(ElemInfo info : elemList) { - ElemInfo info = (ElemInfo) i.next(); - if(info != null) switch(info.operator) { @@ -312,10 +303,8 @@ public class GenAlts extends DepthFirstAdapter macros.apply(file, "ToStringTail", null); macros.apply(file, "RemoveChildHeader", null); - for(Iterator i = elemList.iterator(); i.hasNext();) + for(ElemInfo info : elemList) { - ElemInfo info = (ElemInfo) i.next(); - if(info != null) switch(info.operator) { @@ -338,10 +327,8 @@ public class GenAlts extends DepthFirstAdapter macros.apply(file, "RemoveChildTail", null); macros.apply(file, "ReplaceChildHeader", null); - for(Iterator i = elemList.iterator(); i.hasNext();) + for(ElemInfo info : elemList) { - ElemInfo info = (ElemInfo) i.next(); - if(info != null) switch(info.operator) { @@ -404,17 +391,4 @@ public class GenAlts extends DepthFirstAdapter String type; int operator; } - - private static class ElemInfoCast implements Cast - { - public final static ElemInfoCast instance = new ElemInfoCast(); - - private ElemInfoCast() - {} - - public Object cast(Object o) - { - return (ElemInfo) o; - } - } } diff --git a/src/main/java/org/sablecc/sablecc/GenAnalyses.java b/src/main/java/org/sablecc/sablecc/GenAnalyses.java index f218304..9eeefa1 100644 --- a/src/main/java/org/sablecc/sablecc/GenAnalyses.java +++ b/src/main/java/org/sablecc/sablecc/GenAnalyses.java @@ -18,9 +18,9 @@ public class GenAnalyses extends DepthFirstAdapter private ResolveAstIds ast_ids; private File pkgDir; private String pkgName; - private List elemList; - private List altList = new TypedLinkedList(AltInfoCast.instance); - private List tokenList = new TypedLinkedList(StringCast.instance); + private List<ElemInfo> elemList; + private List<AltInfo> altList = new LinkedList<>(); + private List<String> tokenList = new LinkedList<>(); private String mainProduction; ElemInfo info; @@ -67,7 +67,7 @@ public class GenAnalyses extends DepthFirstAdapter public void inAAstAlt(AAstAlt node) { - elemList = new TypedLinkedList(ElemInfoCast.instance); + elemList = new LinkedList<>(); } public void caseAProductions(AProductions node) @@ -155,10 +155,8 @@ public class GenAnalyses extends DepthFirstAdapter { macros.apply(file, "AnalysisStart", null); - for(Iterator i = altList.iterator(); i.hasNext();) + for(AltInfo info : altList) { - AltInfo info = (AltInfo) i.next(); - macros.apply(file, "AnalysisBody", new String[] {info.name}); } @@ -166,10 +164,10 @@ public class GenAnalyses extends DepthFirstAdapter file.newLine(); } - for(Iterator i = tokenList.iterator(); i.hasNext();) + for(String token : tokenList) { macros.apply(file, "AnalysisBody", - new String[] {(String) i.next()}); + new String[] {token}); } macros.apply(file, "AnalysisTail", null); @@ -212,19 +210,17 @@ public class GenAnalyses extends DepthFirstAdapter { macros.apply(file, "AnalysisAdapterStart", null); - for(Iterator i = altList.iterator(); i.hasNext();) + for(AltInfo info : altList) { - AltInfo info = (AltInfo) i.next(); - macros.apply(file, "AnalysisAdapterBody", new String[] {info.name}); } } - for(Iterator i = tokenList.iterator(); i.hasNext();) + for(String token : tokenList) { macros.apply(file, "AnalysisAdapterBody", - new String[] {(String) i.next()}); + new String[] {token}); } macros.apply(file, "AnalysisAdapterTail", null); @@ -264,20 +260,16 @@ public class GenAnalyses extends DepthFirstAdapter ast_ids.astIds.pkgName.equals("") ? "node" : ast_ids.astIds.pkgName + ".node", mainProduction}); - for(Iterator i = altList.iterator(); i.hasNext();) + for(AltInfo info : altList) { - AltInfo info = (AltInfo) i.next(); - macros.apply(file, "DepthFirstAdapterInOut", new String[] {info.name}); macros.apply(file, "DepthFirstAdapterCaseHeader", new String[] {info.name}); - for(Iterator j = info.elems.iterator(); j.hasNext();) + for(ElemInfo eInfo : info.elems) { - ElemInfo eInfo = (ElemInfo) j.next(); - switch(eInfo.operator) { case ElemInfo.QMARK: @@ -339,19 +331,17 @@ public class GenAnalyses extends DepthFirstAdapter ast_ids.astIds.pkgName.equals("") ? "node" : ast_ids.astIds.pkgName + ".node", mainProduction}); - for(Iterator i = altList.iterator(); i.hasNext();) + for(AltInfo info : altList) { - AltInfo info = (AltInfo) i.next(); - macros.apply(file, "DepthFirstAdapterInOut", new String[] {info.name}); macros.apply(file, "DepthFirstAdapterCaseHeader", new String[] {info.name}); - for(ListIterator j = info.elems.listIterator(info.elems.size()); j.hasPrevious();) + for(ListIterator<ElemInfo> j = info.elems.listIterator(info.elems.size()); j.hasPrevious();) { - ElemInfo eInfo = (ElemInfo) j.previous(); + ElemInfo eInfo = j.previous(); switch(eInfo.operator) { @@ -405,35 +395,9 @@ public class GenAnalyses extends DepthFirstAdapter int operator; } - private static class ElemInfoCast implements Cast - { - final static ElemInfoCast instance = new ElemInfoCast(); - - private ElemInfoCast() - {} - - public Object cast(Object o) - { - return (ElemInfo) o; - } - } - private static class AltInfo { String name; - final List elems = new TypedLinkedList(ElemInfoCast.instance); - } - - private static class AltInfoCast implements Cast - { - final static AltInfoCast instance = new AltInfoCast(); - - private AltInfoCast() - {} - - public Object cast(Object o) - { - return (AltInfo) o; - } + final List<ElemInfo> elems = new LinkedList<>(); } } diff --git a/src/main/java/org/sablecc/sablecc/GenLexer.java b/src/main/java/org/sablecc/sablecc/GenLexer.java index 22f048c..5a22465 100644 --- a/src/main/java/org/sablecc/sablecc/GenLexer.java +++ b/src/main/java/org/sablecc/sablecc/GenLexer.java @@ -64,10 +64,10 @@ public class GenLexer extends AnalysisAdapter } else { - Iterator iter = ids.stateList.iterator(); + Iterator<String> iter = ids.stateList.iterator(); for(int i = 0; i < numStates; i++) { - names[i] = (String) iter.next(); + names[i] = iter.next(); } } @@ -152,16 +152,16 @@ public class GenLexer extends AnalysisAdapter String startState = "INITIAL"; if(ids.stateList.size() > 0) { - startState = (String) ids.stateList.getFirst(); + startState = ids.stateList.getFirst(); } macros.apply(file, "LexerHeader", new String[] {pkgName, ids.pkgName.equals("") ? "node" : ids.pkgName + ".node", startState}); - for(ListIterator i = ids.tokenList.listIterator(); i.hasNext();) + for(ListIterator<String> i = ids.tokenList.listIterator(); i.hasNext();) { - String name = (String) i.next(); + String name = i.next(); ATokenDef node = ids.tokens.get(name); boolean fixed = ids.fixedTokens.get(node); @@ -195,9 +195,9 @@ public class GenLexer extends AnalysisAdapter macros.apply(file, "LexerBody1"); - for(ListIterator i = ids.tokenList.listIterator(); i.hasNext();) + for(ListIterator<String> i = ids.tokenList.listIterator(); i.hasNext();) { - String name = (String) i.next(); + String name = i.next(); ATokenDef node = ids.tokens.get(name); boolean fixed = ids.fixedTokens.get(node); @@ -321,9 +321,9 @@ public class GenLexer extends AnalysisAdapter if(ids.stateList.size() > 0) { - for(ListIterator i = ids.stateList.listIterator(); i.hasNext();) + for(ListIterator<String> i = ids.stateList.listIterator(); i.hasNext();) { - String s = (String) i.next(); + String s = i.next(); macros.apply(file, "LexerStateBody", new String[] {s, "" + i.previousIndex()}); diff --git a/src/main/java/org/sablecc/sablecc/Inlining.java b/src/main/java/org/sablecc/sablecc/Inlining.java index 79393bf..12ac903 100644 --- a/src/main/java/org/sablecc/sablecc/Inlining.java +++ b/src/main/java/org/sablecc/sablecc/Inlining.java @@ -79,7 +79,7 @@ public class Inlining Once we detect that the production can be inline, we try to inline each of its alternatives. */ - LinkedList listOfAlts = new TypedLinkedList(NodeCast.instance); + List<AAlt> listOfAlts = new LinkedList<>(); for(int i=0; i<alts.length; i++) { listOfAlts.addAll( inlineAlternative(alts[i]) ); @@ -95,7 +95,7 @@ public class Inlining } return false; /**************************************************************************/ - listOfAlts = (LinkedList)removeAlternativeDoubloonsFromInlinedProduction(listOfAlts); + listOfAlts = removeAlternativeDoubloonsFromInlinedProduction(listOfAlts); //list of productions whose inlining was a success. productionsToBeRemoved.add("P" + ResolveIds.name(prod_to_inline.getName())); @@ -107,32 +107,32 @@ public class Inlining return false; } - List removeAlternativeDoubloonsFromInlinedProduction(List inlinedAlternatives) + List<AAlt> removeAlternativeDoubloonsFromInlinedProduction(List<AAlt> inlinedAlternatives) { - AAlt[] alts = (AAlt [])inlinedAlternatives.toArray(new AAlt[0]); - LinkedList[] theWhole = new LinkedList[alts.length]; + AAlt[] alts = inlinedAlternatives.toArray(new AAlt[0]); + List<?>[] theWhole = new List<?>[alts.length]; - TreeSet indexOfDoublonsAlternatives = new TreeSet(); + Set<Integer> indexOfDoublonsAlternatives = new TreeSet<>(); for(int i=0; i<alts.length; i++) { LinkedList elems = alts[i].getElems(); AElem[] arrayOfElems = (AElem []) elems.toArray(new AElem[0]); - LinkedList listOfElems = new TypedLinkedList(StringCast.instance); + List<String> listOfElems = new LinkedList<>(); for(int j=0; j<arrayOfElems.length; j++) { listOfElems.add(arrayOfElems[j].getId().getText()); } theWhole[i] = listOfElems; - LinkedList currentList = listOfElems; + List<String> currentList = listOfElems; for(int k=0; k<i; k++) { if( currentList.equals(theWhole[k]) ) { //theWhole[k] = null; - indexOfDoublonsAlternatives.add(new Integer(k)); + indexOfDoublonsAlternatives.add(k); } } } @@ -140,9 +140,8 @@ public class Inlining { int i = 0; - for(Iterator iter = indexOfDoublonsAlternatives.iterator(); iter.hasNext();) + for(int index : indexOfDoublonsAlternatives) { - int index = ((Integer)iter.next()).intValue(); //System.out.println("(" + index + "," + i + ")"); inlinedAlternatives.remove( index - i++ ); } @@ -184,7 +183,7 @@ public class Inlining * Inlining of an alternative * */ - public LinkedList inlineAlternative(AAlt alt) + public List<AAlt> inlineAlternative(AAlt alt) { AElem[] elems = (AElem[])alt.getElems().toArray(new AElem[0]); String elem_name ; @@ -219,7 +218,7 @@ public class Inlining } } - LinkedList resultingListOfAlts = new TypedLinkedList(); + List<AAlt> resultingListOfAlts = new LinkedList<>(); resultingListOfAlts.add(alt); for(int i=0; i<occurenceOfProductionToInlineWithinTheAlternative; i++) { @@ -233,10 +232,10 @@ public class Inlining /* * whichOccurence is used to number an element within the alternative */ - public LinkedList inline(LinkedList altsList, int whichOccurence) + public List<AAlt> inline(List<AAlt> altsList, int whichOccurence) { - LinkedList resultList = new LinkedList(); - AAlt[] alts = (AAlt[])altsList.toArray(new AAlt[0]); + List<AAlt> resultList = new LinkedList<>(); + AAlt[] alts = altsList.toArray(new AAlt[0]); AAlt aParsed_alt; Map<String, String> mapOfNewTermNames; diff --git a/src/main/java/org/sablecc/sablecc/InternalTransformationsToGrammar.java b/src/main/java/org/sablecc/sablecc/InternalTransformationsToGrammar.java index 6865bad..8ced274 100644 --- a/src/main/java/org/sablecc/sablecc/InternalTransformationsToGrammar.java +++ b/src/main/java/org/sablecc/sablecc/InternalTransformationsToGrammar.java @@ -258,7 +258,7 @@ public class InternalTransformationsToGrammar extends DepthFirstAdapter } } - LinkedList checkCreationOfXElem = new TypedLinkedList(StringCast.instance); + List<String> checkCreationOfXElem = new LinkedList<>(); //It's also available for Ignored alternatives public void caseAElem(AElem node) diff --git a/src/main/java/org/sablecc/sablecc/MacroExpander.java b/src/main/java/org/sablecc/sablecc/MacroExpander.java index 9f15a0e..76c7514 100644 --- a/src/main/java/org/sablecc/sablecc/MacroExpander.java +++ b/src/main/java/org/sablecc/sablecc/MacroExpander.java @@ -15,7 +15,7 @@ public class MacroExpander private static final String MACRO = "Macro:"; private static final String lineSeparator = System.getProperty("line.separator"); - private Map<String, List> macros = new TreeMap<>(); + private Map<String, List<String>> macros = new TreeMap<>(); public MacroExpander(Reader in) throws IOException { @@ -33,7 +33,7 @@ public class MacroExpander if(line.startsWith(MACRO)) { String name = line.substring(MACRO.length()); - List macro = new TypedLinkedList(StringCast.instance); + List<String> macro = new LinkedList<>(); while((line = in.readLine()) != null) { @@ -66,16 +66,16 @@ public class MacroExpander public void apply(BufferedWriter out, String macroName, String[] arguments) throws IOException { - List macro = macros.get(macroName); + List<String> macro = macros.get(macroName); - for(ListIterator li = macro.listIterator(); li.hasNext();) + for(ListIterator<String> li = macro.listIterator(); li.hasNext();) { if(li.nextIndex() != 0) { out.newLine(); } - String line = (String) li.next(); + String line = li.next(); char c; for(int i = 0; i < line.length(); i++) diff --git a/src/main/java/org/sablecc/sablecc/RecursiveProductionsDetections.java b/src/main/java/org/sablecc/sablecc/RecursiveProductionsDetections.java index c29276e..c6e04b8 100644 --- a/src/main/java/org/sablecc/sablecc/RecursiveProductionsDetections.java +++ b/src/main/java/org/sablecc/sablecc/RecursiveProductionsDetections.java @@ -13,7 +13,7 @@ import org.sablecc.sablecc.node.*; public class RecursiveProductionsDetections extends DepthFirstAdapter { - public LinkedList listOfRecursiveProds = new TypedLinkedList(StringCast.instance); + public List<String> listOfRecursiveProds = new LinkedList<>(); private String currentProd; public void caseAProd(AProd node) diff --git a/src/main/java/org/sablecc/sablecc/ResolveIds.java b/src/main/java/org/sablecc/sablecc/ResolveIds.java index 99a4fc2..e37b86b 100644 --- a/src/main/java/org/sablecc/sablecc/ResolveIds.java +++ b/src/main/java/org/sablecc/sablecc/ResolveIds.java @@ -37,8 +37,8 @@ public class ResolveIds extends DepthFirstAdapter public final Map<ATokenDef, Boolean> fixedTokens = new HashMap<>(); - public final List tokenList = new TypedLinkedList(StringCast.instance); - public final LinkedList stateList = new TypedLinkedList(StringCast.instance); + public final List<String> tokenList = new LinkedList<>(); + public final LinkedList<String> stateList = new LinkedList<>(); public File pkgDir; public String pkgName = ""; diff --git a/src/main/java/org/sablecc/sablecc/TypedLinkedList.java b/src/main/java/org/sablecc/sablecc/TypedLinkedList.java deleted file mode 100644 index 2e1494b..0000000 --- a/src/main/java/org/sablecc/sablecc/TypedLinkedList.java +++ /dev/null @@ -1,121 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * This file is part of SableCC. * - * See the file "LICENSE" for copyright information and the * - * terms and conditions for copying, distribution and * - * modification of SableCC. * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -package org.sablecc.sablecc; - -import java.util.*; - -public class TypedLinkedList extends LinkedList -{ - Cast cast; - - public TypedLinkedList() - { - super(); - - cast = NoCast.instance; - } - - public TypedLinkedList(Collection c) - { - super(c); - - cast = NoCast.instance; - } - - public TypedLinkedList(Cast cast) - { - super(); - - this.cast = cast; - } - - public TypedLinkedList(Collection c, Cast cast) - { - super(c); - - this.cast = cast; - } - - public Cast getCast() - { - return cast; - } - - public void addFirst(Object o) - { - super.addFirst(cast.cast(o)); - } - - public void addLast(Object o) - { - super.addLast(cast.cast(o)); - } - - public ListIterator listIterator(int index) - { - return new TypedLinkedListIterator(super.listIterator(index)); - } - - private class TypedLinkedListIterator implements ListIterator - { - ListIterator iterator; - - TypedLinkedListIterator(ListIterator iterator) - { - this.iterator = iterator; - } - - public boolean hasNext() - { - return iterator.hasNext(); - } - - public Object next() - { - return iterator.next(); - } - - public boolean hasPrevious() - { - return iterator.hasPrevious(); - } - - public Object previous() - { - return iterator.previous(); - } - - public int nextIndex() - { - return iterator.nextIndex(); - } - - public int previousIndex() - { - return iterator.previousIndex(); - } - - public void remove - () - { - iterator.remove(); - } - - public void set - (Object o) - { - iterator.set(cast.cast(o)); - } - - public void add - (Object o) - { - iterator.add(cast.cast(o)); - } - } -} -- GitLab