From c6897211cb5da8c5bbbaf267db8ad020eb63a114 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 9 Sep 2019 19:56:53 -0400 Subject: 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. --- src/main/java/bjc/everge/ReplPair.java | 48 +++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 9 deletions(-) (limited to 'src/main/java/bjc') 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. @@ -36,6 +33,14 @@ public class ReplPair implements Comparable, UnaryOperator { */ 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. */ @@ -448,6 +453,10 @@ public class ReplPair implements Comparable, UnaryOperator { @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, UnaryOperator { 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, UnaryOperator { } private static ControlledString getControls(String lne, List 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) { -- cgit v1.2.3