diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/MappingsReaderOld.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MappingsReaderOld.java | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsReaderOld.java b/src/main/java/cuchaz/enigma/mapping/MappingsReaderOld.java new file mode 100644 index 0000000..1a93604 --- /dev/null +++ b/src/main/java/cuchaz/enigma/mapping/MappingsReaderOld.java | |||
| @@ -0,0 +1,122 @@ | |||
| 1 | package cuchaz.enigma.mapping; | ||
| 2 | |||
| 3 | import com.google.common.collect.Queues; | ||
| 4 | |||
| 5 | import java.io.BufferedReader; | ||
| 6 | import java.io.IOException; | ||
| 7 | import java.io.Reader; | ||
| 8 | import java.util.Deque; | ||
| 9 | |||
| 10 | public class MappingsReaderOld { | ||
| 11 | |||
| 12 | public Mappings read(Reader in) throws IOException, MappingParseException { | ||
| 13 | return read(new BufferedReader(in)); | ||
| 14 | } | ||
| 15 | |||
| 16 | public Mappings read(BufferedReader in) throws IOException, MappingParseException { | ||
| 17 | Mappings mappings = new Mappings(); | ||
| 18 | Deque<Object> mappingStack = Queues.newArrayDeque(); | ||
| 19 | |||
| 20 | int lineNumber = 0; | ||
| 21 | String line = null; | ||
| 22 | while ((line = in.readLine()) != null) { | ||
| 23 | lineNumber++; | ||
| 24 | |||
| 25 | // strip comments | ||
| 26 | int commentPos = line.indexOf('#'); | ||
| 27 | if (commentPos >= 0) { | ||
| 28 | line = line.substring(0, commentPos); | ||
| 29 | } | ||
| 30 | |||
| 31 | // skip blank lines | ||
| 32 | if (line.trim().length() <= 0) { | ||
| 33 | continue; | ||
| 34 | } | ||
| 35 | |||
| 36 | // get the indent of this line | ||
| 37 | int indent = 0; | ||
| 38 | for (int i = 0; i < line.length(); i++) { | ||
| 39 | if (line.charAt(i) != '\t') { | ||
| 40 | break; | ||
| 41 | } | ||
| 42 | indent++; | ||
| 43 | } | ||
| 44 | |||
| 45 | // handle stack pops | ||
| 46 | while (indent < mappingStack.size()) { | ||
| 47 | mappingStack.pop(); | ||
| 48 | } | ||
| 49 | |||
| 50 | String[] parts = line.trim().split("\\s"); | ||
| 51 | try { | ||
| 52 | // read the first token | ||
| 53 | String token = parts[0]; | ||
| 54 | |||
| 55 | if (token.equalsIgnoreCase("CLASS")) { | ||
| 56 | ClassMapping classMapping; | ||
| 57 | if (indent <= 0) { | ||
| 58 | // outer class | ||
| 59 | classMapping = readClass(parts, false); | ||
| 60 | mappings.addClassMapping(classMapping); | ||
| 61 | } else { | ||
| 62 | |||
| 63 | // inner class | ||
| 64 | if (!(mappingStack.peek() instanceof ClassMapping)) { | ||
| 65 | throw new MappingParseException(lineNumber, "Unexpected CLASS entry here!"); | ||
| 66 | } | ||
| 67 | |||
| 68 | classMapping = readClass(parts, true); | ||
| 69 | ((ClassMapping) mappingStack.peek()).addInnerClassMapping(classMapping); | ||
| 70 | } | ||
| 71 | mappingStack.push(classMapping); | ||
| 72 | } else if (token.equalsIgnoreCase("FIELD")) { | ||
| 73 | if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { | ||
| 74 | throw new MappingParseException(lineNumber, "Unexpected FIELD entry here!"); | ||
| 75 | } | ||
| 76 | ((ClassMapping) mappingStack.peek()).addFieldMapping(readField(parts)); | ||
| 77 | } else if (token.equalsIgnoreCase("METHOD")) { | ||
| 78 | if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { | ||
| 79 | throw new MappingParseException(lineNumber, "Unexpected METHOD entry here!"); | ||
| 80 | } | ||
| 81 | MethodMapping methodMapping = readMethod(parts); | ||
| 82 | ((ClassMapping) mappingStack.peek()).addMethodMapping(methodMapping); | ||
| 83 | mappingStack.push(methodMapping); | ||
| 84 | } else if (token.equalsIgnoreCase("ARG")) { | ||
| 85 | if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof MethodMapping)) { | ||
| 86 | throw new MappingParseException(lineNumber, "Unexpected ARG entry here!"); | ||
| 87 | } | ||
| 88 | ((MethodMapping) mappingStack.peek()).addArgumentMapping(readArgument(parts)); | ||
| 89 | } | ||
| 90 | } catch (ArrayIndexOutOfBoundsException | IllegalArgumentException ex) { | ||
| 91 | throw new MappingParseException(lineNumber, "Malformed line:\n" + line); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | return mappings; | ||
| 96 | } | ||
| 97 | |||
| 98 | private ArgumentMapping readArgument(String[] parts) { | ||
| 99 | return new ArgumentMapping(Integer.parseInt(parts[1]), parts[2]); | ||
| 100 | } | ||
| 101 | |||
| 102 | private ClassMapping readClass(String[] parts, boolean makeSimple) { | ||
| 103 | if (parts.length == 2) { | ||
| 104 | return new ClassMapping(parts[1]); | ||
| 105 | } else { | ||
| 106 | return new ClassMapping(parts[1], parts[2]); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | /* TEMP */ | ||
| 111 | protected FieldMapping readField(String[] parts) { | ||
| 112 | return new FieldMapping(parts[1], new Type(parts[3]), parts[2]); | ||
| 113 | } | ||
| 114 | |||
| 115 | private MethodMapping readMethod(String[] parts) { | ||
| 116 | if (parts.length == 3) { | ||
| 117 | return new MethodMapping(parts[1], new Signature(parts[2])); | ||
| 118 | } else { | ||
| 119 | return new MethodMapping(parts[1], new Signature(parts[3]), parts[2]); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | } | ||