From 6be017f0b44dae0b43a127d82c9e8d3bc6637bce Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Wed, 12 Jun 2019 21:48:59 -0400 Subject: Tests & Debugging Added another test, this time for staging. Also added some debugging capabilities. --- src/main/java/bjc/replpair/ReplPair.java | 97 +++++++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 13 deletions(-) (limited to 'src/main/java/bjc/replpair/ReplPair.java') diff --git a/src/main/java/bjc/replpair/ReplPair.java b/src/main/java/bjc/replpair/ReplPair.java index 14ecb49..651113b 100644 --- a/src/main/java/bjc/replpair/ReplPair.java +++ b/src/main/java/bjc/replpair/ReplPair.java @@ -104,6 +104,7 @@ public class ReplPair implements Comparable, UnaryOperator { priority = p; } + /** * Read a list of replacement pairs from an input source. * @@ -159,15 +160,22 @@ public class ReplPair implements Comparable, UnaryOperator { * The list of replacements. */ public static List readList(List detals, Scanner scn, List errs) { + return readList(detals, scn, errs, new ReplOpts()); + } + + public static List readList(List detals, Scanner scn, + List errs, ReplOpts ropts) { int lno = 0; int pno = 0; - int defPrior = 1; - int defStage = 1; + int defPrior = ropts.defPrior; + int defStage = ropts.defStage; - boolean defMulti = false; + boolean defMulti = ropts.defMulti; - StageStatus defStatus = StageStatus.BOTH; + StageStatus defStatus = ropts.defStatus; + + boolean isDebug = ropts.isDebug; List> stages = new ArrayList<>(); stages.add(new ArrayList()); @@ -240,6 +248,15 @@ public class ReplPair implements Comparable, UnaryOperator { case "B": defStatus = StageStatus.BOTH; break; + case "DT": + isDebug = true; + break; + case "DF": + isDebug = false; + break; + case "D": + isDebug = Boolean.parseBoolean(bitBody); + break; default: errs.add(new ReplError(lno, pno, String.format("Invalid control name '%s'", bitHead), name)); break; @@ -448,17 +465,26 @@ public class ReplPair implements Comparable, UnaryOperator { rp.replace = body; - List stageList; - if (stages.size() < stage) { + List stageList = null; + if (stage == 0 || stages.size() < (stage - 1)) { stageList = stages.get(stage); if (stageList == null) { stageList = new ArrayList<>(); + stages.add(stage, stageList); } } else { - stageList = new ArrayList<>(); - stages.add(stage, stageList); + for (int i = stages.size(); i <= stage; i++) { + stages.add(new ArrayList<>()); + } + + stageList = stages.get(stage); + } + + if (isDebug) { + System.err.printf("\t[DEBUG] Stage %d: Added %s\n\t\tContents: %s\n", + stage, rp, stageList); } stageList.add(rp); @@ -466,7 +492,17 @@ public class ReplPair implements Comparable, UnaryOperator { // Special-case one-stage processing. if (stages.size() == 1) { - detals.addAll(stages.iterator().next()); + if (isDebug) System.err.printf("\t[DEBUG] Executing single-stage bypass\n"); + + for (ReplPair rp : stages.iterator().next()) { + if (rp.stat == StageStatus.INTERNAL) { + if (isDebug) System.err.printf("\t[DEBUG] Excluding internal RP %s\n", rp); + + continue; + } + + detals.add(rp); + } detals.sort(null); @@ -477,19 +513,46 @@ public class ReplPair implements Comparable, UnaryOperator { List tmpList = new ArrayList<>(); tmpList.addAll(detals); + if (isDebug) System.err.printf("\t[DEBUG] Stages: %s\n", stages); + + int procStg = 0; for (List stageList : stages) { + procStg += 1; List curStage = new ArrayList<>(); + if (isDebug) System.err.printf("\t[DEBUG] Staging stage %d of %d: %s\n", + procStg, stageList.size(), stageList); + for (ReplPair rp : stageList) { // Process through every pair in the previous // stages for (ReplPair curPar : tmpList) { - rp.replace = rp.replace.replaceAll(curPar.find, curPar.replace); + String tmp = rp.replace.replaceAll(curPar.find, curPar.replace); + + if (isDebug && !rp.replace.equals(tmp)) { + System.err.printf("\t[DEBUG] Staged '%s' -> '%s'\t%s\n", + rp.replace, tmp, curPar); + } + + rp.replace = tmp; } // If we're external; add straight to the output - if (rp.stat == StageStatus.EXTERNAL) detals.add(rp); - else curStage.add(rp); + if (rp.stat == StageStatus.EXTERNAL) { + if (isDebug) { + System.err.printf("\t[DEBUG] Skipped external for staging: %s\n", + rp); + } + + detals.add(rp); + } else { + if (isDebug) { + System.err.printf("\t[DEBUG] Added to stage %d: %s\n\t\tContents: %s\n", + procStg, rp, curStage); + } + + curStage.add(rp); + } } tmpList.addAll(curStage); @@ -498,13 +561,21 @@ public class ReplPair implements Comparable, UnaryOperator { // Copy over to output, excluding internals for (ReplPair rp : tmpList) { - if (rp.stat == StageStatus.INTERNAL) continue; + if (rp.stat == StageStatus.INTERNAL) { + if (isDebug) System.err.printf("\t[DEBUG] Excluded internal: %s\n", rp); + + continue; + } detals.add(rp); } detals.sort(null); + if (isDebug) { + System.err.printf("\t[DEBUG] Final output: %s\n", detals); + } + return detals; } -- cgit v1.2.3