[TranslationUnitPatcher] ErrorPatcher now uses a Map instead of a switch
package pt.up.fe.specs.tupatcher;
import java.util.HashMap;
+import java.util.Map;
+import java.util.function.BiConsumer;
import pt.up.fe.specs.tupatcher.parser.TUErrorData;
import pt.up.fe.specs.tupatcher.parser.TUErrorsData;
+import pt.up.fe.specs.util.SpecsLogs;
public class ErrorPatcher {
+ private final static Map<ErrorKind, BiConsumer<TUErrorData, PatchData>> ERROR_PATCHERS;
+ static {
+ ERROR_PATCHERS = new HashMap<>();
+
+ ERROR_PATCHERS.put(ErrorKind.UNKNOWN_TYPE, ErrorPatcher::unknownType);
+ }
// final public static int UNKNOWN_TYPE = 3822;
private final PatchData patchData;
@@ -28,11 +37,34 @@ public class ErrorPatcher {
this.patchData = patchData;
}
- public void patch(TUErrorsData errorsData) {
- for (TUErrorData data : errorsData.get(TUErrorsData.ERRORS)) {
+ /**
+ *
+ * @param errorsData
+ * @return true if patchData was updated, false otherwise
+ */
+ public boolean patch(TUErrorsData errorsData) {
+ var errors = errorsData.get(TUErrorsData.ERRORS);
+ if (errors.isEmpty()) {
+ SpecsLogs.info("No errors found!");
+ return false;
+ }
+
+ // Try to correct only the first error
+ var data = errors.get(0);
+
+ // for (TUErrorData data : errorsData.get(TUErrorsData.ERRORS)) {
int errorNumber = (int) data.getValue("errorNumber");
var error = ErrorKind.getKind(errorNumber);
+
+ var errorPatcher = ERROR_PATCHERS.get(error);
+ if (errorPatcher == null) {
+ throw new RuntimeException("Error kind not supported yet: " + error);
+ }
+
+ errorPatcher.accept(data, patchData);
+ return true;
+ /*
switch (error) {
case UNKNOWN_TYPE:
// unknown type identifier
@@ -46,10 +78,17 @@ public class ErrorPatcher {
// PatchData.addType(typeName);
//
// }
- }
+ */
+ // }
// else if (...) other errors
- return;
+ // return;
+
+ }
+
+ public static void unknownType(TUErrorData data, PatchData patchData) {
+ String typeName = data.get(TUErrorData.MAP).get("identifier_name");
+ patchData.addType(typeName);
}
}
