diff --git a/tool/src/org/antlr/v4/automata/ATNSerializer.java b/tool/src/org/antlr/v4/automata/ATNSerializer.java index 5d727854b..bb3fa6653 100644 --- a/tool/src/org/antlr/v4/automata/ATNSerializer.java +++ b/tool/src/org/antlr/v4/automata/ATNSerializer.java @@ -32,6 +32,7 @@ package org.antlr.v4.automata; import org.antlr.v4.misc.Utils; import org.antlr.v4.parse.ANTLRParser; import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.misc.IntegerList; import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.runtime.misc.IntervalSet; import org.antlr.v4.tool.Grammar; @@ -71,8 +72,8 @@ public class ATNSerializer { * * Convenient to pack into unsigned shorts to make as Java string. */ - public List serialize() { - List data = new ArrayList(); + public IntegerList serialize() { + IntegerList data = new IntegerList(); // convert grammar type to ATN const to avoid dependence on ANTLRParser if ( g.getType()== ANTLRParser.LEXER ) data.add(ATN.LEXER); else if ( g.getType()== ANTLRParser.PARSER ) data.add(ATN.PARSER); @@ -277,7 +278,7 @@ public class ATNSerializer { return new String(Utils.toCharArray(getSerialized(g, atn))); } - public static List getSerialized(Grammar g, ATN atn) { + public static IntegerList getSerialized(Grammar g, ATN atn) { return new ATNSerializer(g, atn).serialize(); } @@ -286,7 +287,7 @@ public class ATNSerializer { } public static String getDecoded(Grammar g, ATN atn) { - List serialized = getSerialized(g, atn); + IntegerList serialized = getSerialized(g, atn); char[] data = Utils.toCharArray(serialized); return new ATNSerializer(g, atn).decode(data); } diff --git a/tool/src/org/antlr/v4/codegen/model/SerializedATN.java b/tool/src/org/antlr/v4/codegen/model/SerializedATN.java index c8706a76d..e58c3c511 100644 --- a/tool/src/org/antlr/v4/codegen/model/SerializedATN.java +++ b/tool/src/org/antlr/v4/codegen/model/SerializedATN.java @@ -32,6 +32,7 @@ package org.antlr.v4.codegen.model; import org.antlr.v4.automata.ATNSerializer; import org.antlr.v4.codegen.OutputModelFactory; import org.antlr.v4.runtime.atn.ATN; +import org.antlr.v4.runtime.misc.IntegerList; import java.util.*; @@ -40,9 +41,9 @@ public class SerializedATN extends OutputModelObject { public List serialized; public SerializedATN(OutputModelFactory factory, ATN atn) { super(factory); - List data = ATNSerializer.getSerialized(factory.getGrammar(), atn); + IntegerList data = ATNSerializer.getSerialized(factory.getGrammar(), atn); serialized = new ArrayList(data.size()); - for (int c : data) { + for (int c : data.toArray()) { String encoded = factory.getGenerator().target.encodeIntAsCharEscape(c == -1 ? Character.MAX_VALUE : c); serialized.add(encoded); } diff --git a/tool/src/org/antlr/v4/misc/Utils.java b/tool/src/org/antlr/v4/misc/Utils.java index 7dd29e7dc..3006d7da0 100644 --- a/tool/src/org/antlr/v4/misc/Utils.java +++ b/tool/src/org/antlr/v4/misc/Utils.java @@ -29,6 +29,7 @@ package org.antlr.v4.misc; +import org.antlr.v4.runtime.misc.IntegerList; import org.antlr.v4.tool.ast.GrammarAST; import java.util.*; @@ -137,13 +138,6 @@ public class Utils { // return x; // } - public static int[] toIntArray(List list) { - if ( list==null ) return null; - int[] a = new int[list.size()]; - for (int i=0; i data) { + public static char[] toCharArray(IntegerList data) { if ( data==null ) return null; char[] cdata = new char[data.size()]; for (int i=0; i altList = new ArrayList(); + IntegerList altList = new IntegerList(); altList.addAll(alts); - Collections.sort(altList); + altList.sort(); Set configurations = s.configset; for (int altIndex = 0; altIndex < altList.size(); altIndex++) { int alt = altList.get(altIndex); diff --git a/tool/src/org/antlr/v4/tool/Grammar.java b/tool/src/org/antlr/v4/tool/Grammar.java index 7a644f7c5..27540169e 100644 --- a/tool/src/org/antlr/v4/tool/Grammar.java +++ b/tool/src/org/antlr/v4/tool/Grammar.java @@ -46,6 +46,7 @@ import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.atn.ATN; import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.misc.IntSet; +import org.antlr.v4.runtime.misc.IntegerList; import org.antlr.v4.runtime.misc.IntervalSet; import org.antlr.v4.runtime.misc.NotNull; import org.antlr.v4.runtime.misc.Nullable; @@ -58,7 +59,6 @@ import org.antlr.v4.tool.ast.TerminalAST; import java.io.IOException; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; @@ -466,9 +466,9 @@ public class Grammar implements AttributeResolver { return tokenName; } - public List getTokenDisplayNames(Collection types) { + public List getTokenDisplayNames(IntegerList types) { List names = new ArrayList(); - for (int t : types) names.add(getTokenDisplayName(t)); + for (int t : types.toArray()) names.add(getTokenDisplayName(t)); return names; } diff --git a/tool/test/org/antlr/v4/test/BaseTest.java b/tool/test/org/antlr/v4/test/BaseTest.java index 8009a1254..ad6c7e13f 100644 --- a/tool/test/org/antlr/v4/test/BaseTest.java +++ b/tool/test/org/antlr/v4/test/BaseTest.java @@ -50,6 +50,7 @@ import org.antlr.v4.runtime.atn.ATNState; import org.antlr.v4.runtime.atn.DecisionState; import org.antlr.v4.runtime.atn.LexerATNSimulator; import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.IntegerList; import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.runtime.misc.Nullable; import org.antlr.v4.semantics.SemanticPipeline; @@ -184,8 +185,8 @@ public abstract class BaseTest { // dfa.minimized = dmin.minimize(); // } - List getTypesFromString(Grammar g, String expecting) { - List expectingTokenTypes = new ArrayList(); + IntegerList getTypesFromString(Grammar g, String expecting) { + IntegerList expectingTokenTypes = new IntegerList(); if ( expecting!=null && !expecting.trim().isEmpty() ) { for (String tname : expecting.replace(" ", "").split(",")) { int ttype = g.getTokenType(tname); @@ -195,9 +196,9 @@ public abstract class BaseTest { return expectingTokenTypes; } - public List getTokenTypesViaATN(String input, LexerATNSimulator lexerATN) { + public IntegerList getTokenTypesViaATN(String input, LexerATNSimulator lexerATN) { ANTLRInputStream in = new ANTLRInputStream(input); - List tokenTypes = new ArrayList(); + IntegerList tokenTypes = new IntegerList(); int ttype; do { ttype = lexerATN.matchATN(in); @@ -1065,9 +1066,9 @@ public abstract class BaseTest { public void assertNull(Object object) { try {Assert.assertNull(object);} catch (Error e) {lastTestFailed=true; throw e;} } public static class IntTokenStream implements TokenStream { - List types; + IntegerList types; int p=0; - public IntTokenStream(List types) { this.types = types; } + public IntTokenStream(IntegerList types) { this.types = types; } @Override public void consume() { p++; } diff --git a/tool/test/org/antlr/v4/test/TestATNInterpreter.java b/tool/test/org/antlr/v4/test/TestATNInterpreter.java index f7c120430..32fab5105 100644 --- a/tool/test/org/antlr/v4/test/TestATNInterpreter.java +++ b/tool/test/org/antlr/v4/test/TestATNInterpreter.java @@ -7,6 +7,7 @@ import org.antlr.v4.runtime.atn.ATN; import org.antlr.v4.runtime.atn.ATNState; import org.antlr.v4.runtime.atn.BlockStartState; import org.antlr.v4.runtime.atn.LexerATNSimulator; +import org.antlr.v4.runtime.misc.IntegerList; import org.antlr.v4.tool.DOTGenerator; import org.antlr.v4.tool.Grammar; import org.antlr.v4.tool.LexerGrammar; @@ -14,9 +15,6 @@ import org.antlr.v4.tool.Rule; import org.antlr.v4.tool.interp.ParserInterpreter; import org.junit.Test; -import java.util.List; - - // NOTICE: TOKENS IN LEXER, PARSER MUST BE SAME OR TOKEN TYPE MISMATCH // NOTICE: TOKENS IN LEXER, PARSER MUST BE SAME OR TOKEN TYPE MISMATCH // NOTICE: TOKENS IN LEXER, PARSER MUST BE SAME OR TOKEN TYPE MISMATCH @@ -275,7 +273,7 @@ public class TestATNInterpreter extends BaseTest { { ATN lexatn = createATN(lg); LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn); - List types = getTokenTypesViaATN(inputString, lexInterp); + IntegerList types = getTokenTypesViaATN(inputString, lexInterp); System.out.println(types); semanticProcess(lg); diff --git a/tool/test/org/antlr/v4/test/TestATNParserPrediction.java b/tool/test/org/antlr/v4/test/TestATNParserPrediction.java index 4393a5a1b..9ef7c684b 100644 --- a/tool/test/org/antlr/v4/test/TestATNParserPrediction.java +++ b/tool/test/org/antlr/v4/test/TestATNParserPrediction.java @@ -37,6 +37,7 @@ import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.TokenStream; import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.IntegerList; import org.antlr.v4.tool.DOTGenerator; import org.antlr.v4.tool.Grammar; import org.antlr.v4.tool.LexerGrammar; @@ -44,8 +45,6 @@ import org.antlr.v4.tool.Rule; import org.antlr.v4.tool.interp.ParserInterpreter; import org.junit.Test; -import java.util.List; - // NOTICE: TOKENS IN LEXER, PARSER MUST BE SAME OR TOKEN TYPE MISMATCH // NOTICE: TOKENS IN LEXER, PARSER MUST BE SAME OR TOKEN TYPE MISMATCH // NOTICE: TOKENS IN LEXER, PARSER MUST BE SAME OR TOKEN TYPE MISMATCH @@ -477,7 +476,7 @@ public class TestATNParserPrediction extends BaseTest { Tool.internalOption_ShowATNConfigsInDFA = true; ATN lexatn = createATN(lg); LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn); - List types = getTokenTypesViaATN(inputString, lexInterp); + IntegerList types = getTokenTypesViaATN(inputString, lexInterp); System.out.println(types); semanticProcess(lg); @@ -542,7 +541,7 @@ public class TestATNParserPrediction extends BaseTest { // System.out.println(dot.getDOT(atn.ruleToStartState.get(g.getRule("e")))); ParserATNSimulator interp = new ParserATNSimulator(atn); - List types = getTokenTypesViaATN(inputString, lexInterp); + IntegerList types = getTokenTypesViaATN(inputString, lexInterp); System.out.println(types); TokenStream input = new IntTokenStream(types); try { @@ -572,7 +571,7 @@ public class TestATNParserPrediction extends BaseTest { ParserInterpreter interp = new ParserInterpreter(g, null); for (int i=0; i types = getTokenTypesViaATN(inputString[i], lexInterp); + IntegerList types = getTokenTypesViaATN(inputString[i], lexInterp); System.out.println(types); TokenStream input = new IntTokenStream(types); try {