summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/analysis/SourceIndex.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cuchaz/enigma/analysis/SourceIndex.java')
-rw-r--r--src/main/java/cuchaz/enigma/analysis/SourceIndex.java78
1 files changed, 37 insertions, 41 deletions
diff --git a/src/main/java/cuchaz/enigma/analysis/SourceIndex.java b/src/main/java/cuchaz/enigma/analysis/SourceIndex.java
index a20fbb4..cbc2945 100644
--- a/src/main/java/cuchaz/enigma/analysis/SourceIndex.java
+++ b/src/main/java/cuchaz/enigma/analysis/SourceIndex.java
@@ -28,36 +28,36 @@ import cuchaz.enigma.mapping.Entry;
28 28
29public class SourceIndex { 29public class SourceIndex {
30 30
31 private String m_source; 31 private String source;
32 private TreeMap<Token, EntryReference<Entry, Entry>> m_tokenToReference; 32 private TreeMap<Token, EntryReference<Entry, Entry>> tokenToReference;
33 private Multimap<EntryReference<Entry, Entry>, Token> m_referenceToTokens; 33 private Multimap<EntryReference<Entry, Entry>, Token> referenceToTokens;
34 private Map<Entry, Token> m_declarationToToken; 34 private Map<Entry, Token> declarationToToken;
35 private List<Integer> m_lineOffsets; 35 private List<Integer> lineOffsets;
36 private boolean m_ignoreBadTokens; 36 private boolean ignoreBadTokens;
37 37
38 public SourceIndex(String source) { 38 public SourceIndex(String source) {
39 this(source, true); 39 this(source, true);
40 } 40 }
41 41
42 public SourceIndex(String source, boolean ignoreBadTokens) { 42 public SourceIndex(String source, boolean ignoreBadTokens) {
43 m_source = source; 43 this.source = source;
44 m_ignoreBadTokens = ignoreBadTokens; 44 this.ignoreBadTokens = ignoreBadTokens;
45 m_tokenToReference = Maps.newTreeMap(); 45 this.tokenToReference = Maps.newTreeMap();
46 m_referenceToTokens = HashMultimap.create(); 46 this.referenceToTokens = HashMultimap.create();
47 m_declarationToToken = Maps.newHashMap(); 47 this.declarationToToken = Maps.newHashMap();
48 m_lineOffsets = Lists.newArrayList(); 48 this.lineOffsets = Lists.newArrayList();
49 49
50 // count the lines 50 // count the lines
51 m_lineOffsets.add(0); 51 this.lineOffsets.add(0);
52 for (int i = 0; i < source.length(); i++) { 52 for (int i = 0; i < source.length(); i++) {
53 if (source.charAt(i) == '\n') { 53 if (source.charAt(i) == '\n') {
54 m_lineOffsets.add(i + 1); 54 this.lineOffsets.add(i + 1);
55 } 55 }
56 } 56 }
57 } 57 }
58 58
59 public String getSource() { 59 public String getSource() {
60 return m_source; 60 return this.source;
61 } 61 }
62 62
63 public Token getToken(AstNode node) { 63 public Token getToken(AstNode node) {
@@ -75,11 +75,7 @@ public class SourceIndex {
75 System.err.println(String.format("WARNING: %s \"%s\" has invalid region: %s", node.getNodeType(), name, region)); 75 System.err.println(String.format("WARNING: %s \"%s\" has invalid region: %s", node.getNodeType(), name, region));
76 return null; 76 return null;
77 } 77 }
78 Token token = new Token( 78 Token token = new Token(toPos(region.getBeginLine(), region.getBeginColumn()), toPos(region.getEndLine(), region.getEndColumn()), this.source);
79 toPos(region.getBeginLine(), region.getBeginColumn()),
80 toPos(region.getEndLine(), region.getEndColumn()),
81 m_source
82 );
83 if (token.start == 0) { 79 if (token.start == 0) {
84 // DEBUG 80 // DEBUG
85 System.err.println(String.format("WARNING: %s \"%s\" has invalid start: %s", node.getNodeType(), name, region)); 81 System.err.println(String.format("WARNING: %s \"%s\" has invalid start: %s", node.getNodeType(), name, region));
@@ -90,7 +86,7 @@ public class SourceIndex {
90 // System.out.println( String.format( "%s \"%s\" region: %s", node.getNodeType(), name, region ) ); 86 // System.out.println( String.format( "%s \"%s\" region: %s", node.getNodeType(), name, region ) );
91 87
92 // if the token has a $ in it, something's wrong. Ignore this token 88 // if the token has a $ in it, something's wrong. Ignore this token
93 if (name.lastIndexOf('$') >= 0 && m_ignoreBadTokens) { 89 if (name.lastIndexOf('$') >= 0 && this.ignoreBadTokens) {
94 // DEBUG 90 // DEBUG
95 System.err.println(String.format("WARNING: %s \"%s\" is probably a bad token. It was ignored", node.getNodeType(), name)); 91 System.err.println(String.format("WARNING: %s \"%s\" is probably a bad token. It was ignored", node.getNodeType(), name));
96 return null; 92 return null;
@@ -103,8 +99,8 @@ public class SourceIndex {
103 Token token = getToken(node); 99 Token token = getToken(node);
104 if (token != null) { 100 if (token != null) {
105 EntryReference<Entry, Entry> deobfReference = new EntryReference<Entry, Entry>(deobfEntry, token.text, deobfContext); 101 EntryReference<Entry, Entry> deobfReference = new EntryReference<Entry, Entry>(deobfEntry, token.text, deobfContext);
106 m_tokenToReference.put(token, deobfReference); 102 this.tokenToReference.put(token, deobfReference);
107 m_referenceToTokens.put(deobfReference, token); 103 this.referenceToTokens.put(deobfReference, token);
108 } 104 }
109 } 105 }
110 106
@@ -112,14 +108,14 @@ public class SourceIndex {
112 Token token = getToken(node); 108 Token token = getToken(node);
113 if (token != null) { 109 if (token != null) {
114 EntryReference<Entry, Entry> reference = new EntryReference<Entry, Entry>(deobfEntry, token.text); 110 EntryReference<Entry, Entry> reference = new EntryReference<Entry, Entry>(deobfEntry, token.text);
115 m_tokenToReference.put(token, reference); 111 this.tokenToReference.put(token, reference);
116 m_referenceToTokens.put(reference, token); 112 this.referenceToTokens.put(reference, token);
117 m_declarationToToken.put(deobfEntry, token); 113 this.declarationToToken.put(deobfEntry, token);
118 } 114 }
119 } 115 }
120 116
121 public Token getReferenceToken(int pos) { 117 public Token getReferenceToken(int pos) {
122 Token token = m_tokenToReference.floorKey(new Token(pos, pos, null)); 118 Token token = this.tokenToReference.floorKey(new Token(pos, pos, null));
123 if (token != null && token.contains(pos)) { 119 if (token != null && token.contains(pos)) {
124 return token; 120 return token;
125 } 121 }
@@ -127,44 +123,44 @@ public class SourceIndex {
127 } 123 }
128 124
129 public Collection<Token> getReferenceTokens(EntryReference<Entry, Entry> deobfReference) { 125 public Collection<Token> getReferenceTokens(EntryReference<Entry, Entry> deobfReference) {
130 return m_referenceToTokens.get(deobfReference); 126 return this.referenceToTokens.get(deobfReference);
131 } 127 }
132 128
133 public EntryReference<Entry, Entry> getDeobfReference(Token token) { 129 public EntryReference<Entry, Entry> getDeobfReference(Token token) {
134 if (token == null) { 130 if (token == null) {
135 return null; 131 return null;
136 } 132 }
137 return m_tokenToReference.get(token); 133 return this.tokenToReference.get(token);
138 } 134 }
139 135
140 public void replaceDeobfReference(Token token, EntryReference<Entry, Entry> newDeobfReference) { 136 public void replaceDeobfReference(Token token, EntryReference<Entry, Entry> newDeobfReference) {
141 EntryReference<Entry, Entry> oldDeobfReference = m_tokenToReference.get(token); 137 EntryReference<Entry, Entry> oldDeobfReference = this.tokenToReference.get(token);
142 m_tokenToReference.put(token, newDeobfReference); 138 this.tokenToReference.put(token, newDeobfReference);
143 Collection<Token> tokens = m_referenceToTokens.get(oldDeobfReference); 139 Collection<Token> tokens = this.referenceToTokens.get(oldDeobfReference);
144 m_referenceToTokens.removeAll(oldDeobfReference); 140 this.referenceToTokens.removeAll(oldDeobfReference);
145 m_referenceToTokens.putAll(newDeobfReference, tokens); 141 this.referenceToTokens.putAll(newDeobfReference, tokens);
146 } 142 }
147 143
148 public Iterable<Token> referenceTokens() { 144 public Iterable<Token> referenceTokens() {
149 return m_tokenToReference.keySet(); 145 return this.tokenToReference.keySet();
150 } 146 }
151 147
152 public Iterable<Token> declarationTokens() { 148 public Iterable<Token> declarationTokens() {
153 return m_declarationToToken.values(); 149 return this.declarationToToken.values();
154 } 150 }
155 151
156 public Iterable<Entry> declarations() { 152 public Iterable<Entry> declarations() {
157 return m_declarationToToken.keySet(); 153 return this.declarationToToken.keySet();
158 } 154 }
159 155
160 public Token getDeclarationToken(Entry deobfEntry) { 156 public Token getDeclarationToken(Entry deobfEntry) {
161 return m_declarationToToken.get(deobfEntry); 157 return this.declarationToToken.get(deobfEntry);
162 } 158 }
163 159
164 public int getLineNumber(int pos) { 160 public int getLineNumber(int pos) {
165 // line number is 1-based 161 // line number is 1-based
166 int line = 0; 162 int line = 0;
167 for (Integer offset : m_lineOffsets) { 163 for (Integer offset : this.lineOffsets) {
168 if (offset > pos) { 164 if (offset > pos) {
169 break; 165 break;
170 } 166 }
@@ -175,11 +171,11 @@ public class SourceIndex {
175 171
176 public int getColumnNumber(int pos) { 172 public int getColumnNumber(int pos) {
177 // column number is 1-based 173 // column number is 1-based
178 return pos - m_lineOffsets.get(getLineNumber(pos) - 1) + 1; 174 return pos - this.lineOffsets.get(getLineNumber(pos) - 1) + 1;
179 } 175 }
180 176
181 private int toPos(int line, int col) { 177 private int toPos(int line, int col) {
182 // line and col are 1-based 178 // line and col are 1-based
183 return m_lineOffsets.get(line - 1) + col - 1; 179 return this.lineOffsets.get(line - 1) + col - 1;
184 } 180 }
185} 181}