|
@@ -1,15 +1,16 @@
|
1
|
1
|
/* Licensed under GPLv3 - https://opensource.org/licenses/GPL-3.0 */
|
2
|
2
|
package org.openzen.zenscript.lexer;
|
3
|
3
|
|
4
|
|
-import gnu.trove.iterator.TIntIterator;
|
5
|
|
-import gnu.trove.map.hash.TIntIntHashMap;
|
6
|
|
-import gnu.trove.map.hash.TIntObjectHashMap;
|
7
|
|
-import gnu.trove.set.hash.TIntHashSet;
|
|
4
|
+
|
8
|
5
|
import java.lang.reflect.Array;
|
9
|
6
|
import java.util.ArrayList;
|
10
|
7
|
import java.util.HashMap;
|
|
8
|
+import java.util.HashSet;
|
|
9
|
+import java.util.Iterator;
|
11
|
10
|
import java.util.LinkedList;
|
|
11
|
+import java.util.Map;
|
12
|
12
|
import java.util.Queue;
|
|
13
|
+import java.util.Set;
|
13
|
14
|
|
14
|
15
|
/**
|
15
|
16
|
* Implements a DFA. Used as intermediate form when compiling an NFA to a
|
|
@@ -56,8 +57,8 @@ public class DFA<T>
|
56
|
57
|
|
57
|
58
|
while (!todo.isEmpty()) {
|
58
|
59
|
DFAState<T> current = todo.poll();
|
59
|
|
-
|
60
|
|
- TIntIterator it = current.transitions.keySet().iterator();
|
|
60
|
+
|
|
61
|
+ Iterator<Integer> it = current.transitions.keySet().iterator();
|
61
|
62
|
while (it.hasNext()) {
|
62
|
63
|
int k = it.next();
|
63
|
64
|
DFAState<T> next = current.transitions.get(k);
|
|
@@ -70,16 +71,16 @@ public class DFA<T>
|
70
|
71
|
}
|
71
|
72
|
|
72
|
73
|
/* Compile */
|
73
|
|
- TIntIntHashMap[] transitions = new TIntIntHashMap[counter];
|
|
74
|
+ Map<Integer, Integer>[] transitions = new HashMap[counter];
|
74
|
75
|
@SuppressWarnings("unchecked")
|
75
|
76
|
T[] finals2 = (T[]) Array.newInstance(tokenClass, counter);
|
76
|
77
|
|
77
|
78
|
for (DFAState<T> node : nodeList) {
|
78
|
79
|
int index = nodes.get(node);
|
79
|
80
|
finals2[index] = node.finalCode;
|
80
|
|
-
|
81
|
|
- transitions[index] = new TIntIntHashMap();
|
82
|
|
- TIntIterator it = node.transitions.keySet().iterator();
|
|
81
|
+
|
|
82
|
+ transitions[index] = new HashMap<>();
|
|
83
|
+ Iterator<Integer> it = node.transitions.keySet().iterator();
|
83
|
84
|
while (it.hasNext()) {
|
84
|
85
|
int k = it.next();
|
85
|
86
|
DFAState<T> next = node.transitions.get(k);
|
|
@@ -98,13 +99,13 @@ public class DFA<T>
|
98
|
99
|
public DFA<T> optimize()
|
99
|
100
|
{
|
100
|
101
|
CompiledDFA<T> compiled = compile();
|
101
|
|
- TIntIntHashMap[] transitions = compiled.transitions;
|
|
102
|
+ Map<Integer, Integer>[] transitions = compiled.transitions;
|
102
|
103
|
int size = transitions.length;
|
103
|
104
|
|
104
|
105
|
/* Collect all edges and determine alphabet */
|
105
|
|
- TIntHashSet alphabet = new TIntHashSet();
|
|
106
|
+ Set<Integer> alphabet = new HashSet<>();
|
106
|
107
|
for (int i = 0; i < size; i++) {
|
107
|
|
- TIntIterator it = transitions[i].keySet().iterator();
|
|
108
|
+ Iterator<Integer> it = transitions[i].keySet().iterator();
|
108
|
109
|
while (it.hasNext()) {
|
109
|
110
|
int k = it.next();
|
110
|
111
|
alphabet.add(k);
|
|
@@ -127,7 +128,7 @@ public class DFA<T>
|
127
|
128
|
boolean changed;
|
128
|
129
|
do {
|
129
|
130
|
changed = false;
|
130
|
|
- TIntIterator ita = alphabet.iterator();
|
|
131
|
+ Iterator<Integer> ita = alphabet.iterator();
|
131
|
132
|
while (ita.hasNext()) {
|
132
|
133
|
int x = ita.next();
|
133
|
134
|
for (int i = 0; i < size; i++) {
|
|
@@ -146,7 +147,7 @@ public class DFA<T>
|
146
|
147
|
} while (changed);
|
147
|
148
|
|
148
|
149
|
/* Group nodes */
|
149
|
|
- TIntObjectHashMap<DFAState<T>> nodeMap = new TIntObjectHashMap<>();
|
|
150
|
+ Map<Integer, DFAState<T>> nodeMap = new HashMap<>();
|
150
|
151
|
outer: for (int i = 0; i < size; i++) {
|
151
|
152
|
for (int j = 0; j < size; j++) {
|
152
|
153
|
if (!distinguishable[i][j] && nodeMap.containsKey(j)) {
|
|
@@ -165,7 +166,7 @@ public class DFA<T>
|
165
|
166
|
}
|
166
|
167
|
|
167
|
168
|
for (int i = 0; i < compiled.transitions.length; i++) {
|
168
|
|
- TIntIterator iter = transitions[i].keySet().iterator();
|
|
169
|
+ Iterator<Integer> iter = transitions[i].keySet().iterator();
|
169
|
170
|
while (iter.hasNext()) {
|
170
|
171
|
int k = iter.next();
|
171
|
172
|
|
|
@@ -182,9 +183,9 @@ public class DFA<T>
|
182
|
183
|
StringBuilder result = new StringBuilder();
|
183
|
184
|
CompiledDFA<T> dfs = compile();
|
184
|
185
|
for (int i = 0; i < dfs.transitions.length; i++) {
|
185
|
|
- TIntIntHashMap map = dfs.transitions[i];
|
186
|
|
-
|
187
|
|
- TIntIterator it = map.keySet().iterator();
|
|
186
|
+ Map<Integer, Integer> map = dfs.transitions[i];
|
|
187
|
+
|
|
188
|
+ Iterator<Integer> it = map.keySet().iterator();
|
188
|
189
|
while (it.hasNext()) {
|
189
|
190
|
int v = it.next();
|
190
|
191
|
result.append("edge(");
|
|
@@ -217,14 +218,15 @@ public class DFA<T>
|
217
|
218
|
*/
|
218
|
219
|
public static class DFAState<T>
|
219
|
220
|
{
|
220
|
|
- private TIntObjectHashMap<DFAState<T>> transitions;
|
|
221
|
+
|
|
222
|
+ private Map<Integer, DFAState<T>> transitions;
|
221
|
223
|
private T finalCode = null;
|
222
|
224
|
|
223
|
225
|
/**
|
224
|
226
|
* Creates a new DFA state.
|
225
|
227
|
*/
|
226
|
228
|
public DFAState() {
|
227
|
|
- transitions = new TIntObjectHashMap<>();
|
|
229
|
+ transitions = new HashMap<>();
|
228
|
230
|
}
|
229
|
231
|
|
230
|
232
|
/**
|