diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2019-06-12 21:48:59 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2019-06-12 21:48:59 -0400 |
| commit | 6be017f0b44dae0b43a127d82c9e8d3bc6637bce (patch) | |
| tree | 042f7ff591f27b9c0b4ed0fa661714f76be10094 /src/main/java | |
| parent | 5392663881d49fed1df32c58c99f63242b38733f (diff) | |
Tests & Debugging
Added another test, this time for staging.
Also added some debugging capabilities.
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/bjc/replpair/ReplOpts.java | 69 | ||||
| -rw-r--r-- | src/main/java/bjc/replpair/ReplPair.java | 97 |
2 files changed, 153 insertions, 13 deletions
diff --git a/src/main/java/bjc/replpair/ReplOpts.java b/src/main/java/bjc/replpair/ReplOpts.java new file mode 100644 index 0000000..a1645bb --- /dev/null +++ b/src/main/java/bjc/replpair/ReplOpts.java @@ -0,0 +1,69 @@ +package bjc.replpair; + +/** + * Options for processing ReplPairs. + * + * @author Ben Culkin. + */ +public class ReplOpts { + /** + * The default priority. + */ + public int defPrior; + /** + * The default stage. + */ + public int defStage; + + /** + * Whether to process multi-line defns. + */ + public boolean defMulti; + + /** + * Default status. + */ + public StageStatus defStatus; + + /** + * Enable debug info. + */ + public boolean isDebug; + + /** + * Create a default set of options. + */ + public ReplOpts() { + defPrior = 0; + defStage = 0; + + defMulti = false; + + defStatus = StageStatus.BOTH; + + isDebug = false; + } + + /** + * Create a new set of repl. opts + * + * @param p + * The default priority to use + * @param s + * The default stage to use + * @param m + * Whether to process multi-line defns. + * @param t + * The default status. + */ + public ReplOpts(int p, int s, boolean m, StageStatus t, boolean d) { + defPrior = p; + defStage = s; + + defMulti = m; + + defStatus = t; + + isDebug = d; + } +} 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<ReplPair>, UnaryOperator<String> { priority = p; } + /** * Read a list of replacement pairs from an input source. * @@ -159,15 +160,22 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { * The list of replacements. */ public static List<ReplPair> readList(List<ReplPair> detals, Scanner scn, List<ReplError> errs) { + return readList(detals, scn, errs, new ReplOpts()); + } + + public static List<ReplPair> readList(List<ReplPair> detals, Scanner scn, + List<ReplError> 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<List<ReplPair>> stages = new ArrayList<>(); stages.add(new ArrayList<ReplPair>()); @@ -240,6 +248,15 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { 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<ReplPair>, UnaryOperator<String> { rp.replace = body; - List<ReplPair> stageList; - if (stages.size() < stage) { + List<ReplPair> 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<ReplPair>, UnaryOperator<String> { // 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<ReplPair>, UnaryOperator<String> { List<ReplPair> tmpList = new ArrayList<>(); tmpList.addAll(detals); + if (isDebug) System.err.printf("\t[DEBUG] Stages: %s\n", stages); + + int procStg = 0; for (List<ReplPair> stageList : stages) { + procStg += 1; List<ReplPair> 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<ReplPair>, UnaryOperator<String> { // 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; } |
