feat: added error handling and generally tweaked tsrg parsing

This commit is contained in:
zaaarf 2023-08-26 18:25:31 +02:00
parent 7898882bc5
commit f03bb3c932
No known key found for this signature in database
GPG key ID: 6445A5CD15E5B40C
2 changed files with 27 additions and 19 deletions

View file

@ -1,7 +1,17 @@
package ftbsc.lll.exceptions; package ftbsc.lll.exceptions;
/**
* Thrown when something goes wrong while parsing a mappings file.
*/
public class MalformedMappingsException extends Exception { public class MalformedMappingsException extends Exception {
public MalformedMappingsException(String mapping, String type) {
super(String.format("Unexpected token at line %s for mapper type %s!", mapping, type)); /**
* Constructs a new {@link MalformedMappingsException} given the line number
* and an error message.
* @param lineNumber the line the error occurred at
* @param error the error message
*/
public MalformedMappingsException(int lineNumber, String error) {
super(String.format("Unexpected token at line %d: %s!", lineNumber, error));
} }
} }

View file

@ -6,7 +6,6 @@ import ftbsc.lll.mapper.AbstractMapper;
import ftbsc.lll.mapper.IMapper; import ftbsc.lll.mapper.IMapper;
import ftbsc.lll.mapper.tools.data.ClassData; import ftbsc.lll.mapper.tools.data.ClassData;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -36,24 +35,23 @@ public class TSRGMapper extends AbstractMapper {
*/ */
@Override @Override
protected void processLines(List<String> lines, boolean ignoreErrors) throws MalformedMappingsException { protected void processLines(List<String> lines, boolean ignoreErrors) throws MalformedMappingsException {
//skip the first line ("tsrg2 left right")
lines = new ArrayList<>(lines);
lines.remove(0);
String currentClass = ""; String currentClass = "";
for(String l : lines) { for(int i = 1; i < lines.size(); i++) { //start from 1 to skip header
if(l == null) continue; String currentLine = lines.get(i);
if(l.startsWith("\t") || l.startsWith(" ")) { boolean isMember = currentLine.startsWith("\t") || currentLine.startsWith(" ");
String[] split = l.trim().split(" "); String[] tokens = currentLine.trim().split(" ");
if(split.length == 2) //field if(isMember) {
this.mappings.get(currentClass).addField(split[0], split[1]); if(tokens.length == 2) //field
else if (split.length == 3)//method this.mappings.get(currentClass).addField(tokens[0], tokens[1]);
this.mappings.get(currentClass).addMethod(split[0], split[2], split[1]); //add child else if(tokens.length == 3)//method
this.mappings.get(currentClass).addMethod(tokens[0], tokens[2], tokens[1]); //add child
else if(!ignoreErrors) throw new MalformedMappingsException(i, "wrong number of space-separated tokens");
} else { } else {
String[] sp = l.split(" "); if(tokens.length == 2) {
ClassData s = new ClassData(sp[0], sp[1]); ClassData s = new ClassData(tokens[0], tokens[1]);
currentClass = s.name; currentClass = s.name;
this.mappings.put(s.name, s); this.mappings.put(s.name, s);
} else if(!ignoreErrors) throw new MalformedMappingsException(i, "wrong number of space-separated tokens");
} }
} }
} }