diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/java/bjc/replpair/ReplOpts.java | 69 | ||||
| -rw-r--r-- | src/main/java/bjc/replpair/ReplPair.java | 97 | ||||
| -rw-r--r-- | src/test/java/bjc/replpair/ReplPairTest.java | 8 |
3 files changed, 160 insertions, 14 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; } diff --git a/src/test/java/bjc/replpair/ReplPairTest.java b/src/test/java/bjc/replpair/ReplPairTest.java index 4ce918f..ec7d8a5 100644 --- a/src/test/java/bjc/replpair/ReplPairTest.java +++ b/src/test/java/bjc/replpair/ReplPairTest.java @@ -16,7 +16,6 @@ import org.junit.Test; * @author Ben Culkin */ public class ReplPairTest { - // Test that we load empty files fine @Test public void testLoadFile() { List<ReplPair> lrp = null; @@ -51,6 +50,11 @@ public class ReplPairTest { assertMultiReplace("data/test/test5.rp", "a", "a", "aa", "ab"); } + @Test + public void testStaging() { + assertMultiReplace(true, "data/test/test6.rp", "c", "a", "y2", "x"); + } + private void assertMultiReplace(String fle, String... inps) { assertMultiReplace(false, fle, inps); } @@ -89,6 +93,8 @@ public class ReplPairTest { } private void assertReplacesTo(boolean logRep, String right, List<ReplPair> rps, String inp) { + if (logRep) System.err.printf("\t[LOG] Checking '%s' -> '%s'\n", inp, right); + String tmp = inp; for (ReplPair rp : rps) { |
