summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2019-09-09 19:56:53 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2019-09-09 19:56:53 -0400
commitc6897211cb5da8c5bbbaf267db8ad020eb63a114 (patch)
treef46fa905845928f6d1ea59f2dcb182232e25d8cf
parente60131eb4ead92f3ab1caf29d9c89d5d507092d5 (diff)
Implement pattern guards
Pattern guards allow you to say that a particular pair should only be replaced when the input matches a particular regular expression. It is tied to the 'G' or 'Guard' control on the pair header, and is one of the 3 or so major features I want to implement before a 1.0 release As an aside, the other two features I want to add are: Multibody Support ================= This will allow a pair to replace to a few different things, based on various conditions (the two main ones I want are some sort of cyclic behavior, and some sort of guards for these) Sub-application ================ Allow the application of a stage to the result of applying a pair/parts of applying a pair. This will likely entail several sub-features such as: Named Stages ------------ A named stage is a collection of pairs that isn't applied by default, but is instead used for convenient grouping of related pages together. Custom Replacement ------------------ Instead of using replaceAll, I'll probably go for something using appendReplacement etc. on Matcher and roll some sort of custom thing, so as to provide for a convenient syntax for anything special I want to do while running the replacement. I'll also want to add some documentation, in addition to maybe doing some restructuring to make how things are laid out more obvious.
-rw-r--r--data/test/test10.rp3
-rw-r--r--src/main/java/bjc/everge/ReplPair.java48
-rw-r--r--src/test/java/bjc/everge/ReplPairTest.java5
3 files changed, 47 insertions, 9 deletions
diff --git a/data/test/test10.rp b/data/test/test10.rp
new file mode 100644
index 0000000..a647a3a
--- /dev/null
+++ b/data/test/test10.rp
@@ -0,0 +1,3 @@
+# Test pair guards
+//G/aaa//a
+b
diff --git a/src/main/java/bjc/everge/ReplPair.java b/src/main/java/bjc/everge/ReplPair.java
index 11ca3ef..4425da9 100644
--- a/src/main/java/bjc/everge/ReplPair.java
+++ b/src/main/java/bjc/everge/ReplPair.java
@@ -1,13 +1,10 @@
package bjc.everge;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Scanner;
+import java.util.*;
+import java.util.function.*;
+import java.util.regex.*;
-import java.util.function.UnaryOperator;
-
-import bjc.everge.ControlledString.Control;
-import bjc.everge.ControlledString.ParseStrings;
+import bjc.everge.ControlledString.*;
/**
* String pairs for replacements.
@@ -37,6 +34,14 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> {
public String name;
/**
+ * The guard for this replacement.
+ *
+ * The guard of the replacement is a regex that has to match before the pair will be considered.
+ * Defaults to being blank.
+ */
+ public String guard;
+
+ /**
* The string to look for.
*/
public String find;
@@ -448,6 +453,10 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> {
@Override
public String apply(String inp) {
+ if (guard != null) {
+ if (!inp.matches(guard)) return inp;
+ }
+
return inp.replaceAll(find, replace);
}
@@ -521,6 +530,28 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> {
rp.name = cont.get(0);
}
break;
+ case "GUARD":
+ case "G":
+ if (cont.count() != 1) {
+ String errMsg = String.format("One guard argument was expected (got %d)",
+ cont.count());
+
+ errs.add(new ReplError(lno, pno, errMsg, nam));
+ } else {
+ String pat = cont.get(0);
+
+ try {
+ Pattern.compile(pat);
+ } catch (PatternSyntaxException psex) {
+ String errMsg = String.format("Guard argument '%s' is not a valid regex (%s)",
+ pat, psex.getMessage());
+
+ errs.add(new ReplError(lno, pno, errMsg, nam));
+ }
+
+ rp.guard = cont.get(0);
+ }
+ break;
case "PRIORITY":
case "PRIOR":
case "P":
@@ -796,8 +827,7 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> {
}
private static ControlledString getControls(String lne, List<ReplError> errs,
- ReplOpts ropts, IntHolder lno, IntHolder pno, String type)
- {
+ ReplOpts ropts, IntHolder lno, IntHolder pno, String type) {
try {
return ControlledString.parse(lne, new ParseStrings("//", ";", "/", "|"));
} catch (IllegalArgumentException iaex) {
diff --git a/src/test/java/bjc/everge/ReplPairTest.java b/src/test/java/bjc/everge/ReplPairTest.java
index 8edb42e..e098605 100644
--- a/src/test/java/bjc/everge/ReplPairTest.java
+++ b/src/test/java/bjc/everge/ReplPairTest.java
@@ -86,4 +86,9 @@ public class ReplPairTest {
public void testGlobals() {
assertMultiReplace("data/test/test8.rp", "b1d\n1d\n1b1", "acca");
}
+
+ @Test
+ public void testGuards() {
+ assertMultiReplace("data/test/test10.rp", "a", "a", "bbb", "aaa");
+ }
}