From b0d27faf67ec23b3d55786e00d4fd3b0d07567ee Mon Sep 17 00:00:00 2001 From: bjculkin Date: Thu, 23 Mar 2017 10:46:02 -0400 Subject: Add forgetful groups. Forgetful groups are groups which reset the currently allowed nested openers/closers from enclosing groups. This is useful for things like quoted strings, where you don't want groups to open inside them. As a consequence, this also adds nested openers. However, predicated openers/closers cannot be nested. --- .../java/bjc/utils/parserutils/TokenUtils.java | 2 - .../utils/parserutils/delims/DelimiterGroup.java | 111 ++++++++++++++++++++- .../delims/SequenceCharacteristics.java | 12 +-- .../parserutils/delims/SequenceDelimiter.java | 81 ++++++++++++--- .../utils/parserutils/delims/StringDelimiter.java | 2 +- 5 files changed, 181 insertions(+), 27 deletions(-) (limited to 'BJC-Utils2/src') diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenUtils.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenUtils.java index 69315cb..4b8cde1 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenUtils.java @@ -5,8 +5,6 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.commons.lang3.StringUtils; - /** * Utilities useful for operating on PL tokens. * diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java index 23a3b9f..652b8f6 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java @@ -14,7 +14,6 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Set; import java.util.function.BiPredicate; import java.util.function.Function; @@ -209,6 +208,17 @@ public class DelimiterGroup { return groupExclusions; } + /** + * Get the groups that are allowed to open anywhere inside this + * group. + * + * @return The groups allowed to open anywhere inside this + * group. + */ + public Map getNestingOpeners() { + return nestedOpenDelimiters; + } + /** * Checks if a given token marks a subgroup. * @@ -245,6 +255,15 @@ public class DelimiterGroup { return new Pair<>(null, null); } + + /** + * Check if this group starts a new nesting scope. + * + * @return Whether this group starts a new nesting scope. + */ + public boolean isForgetful() { + return forgetful; + } } /** @@ -257,6 +276,11 @@ public class DelimiterGroup { */ private Map openDelimiters; + /* + * The delimiters that open groups inside of this group. + */ + private Map nestedOpenDelimiters; + /* * The delimiters that close this group. */ @@ -293,6 +317,11 @@ public class DelimiterGroup { */ private List> predClosers; + /* + * Whether or not this group starts a new nesting set. + */ + private boolean forgetful; + /** * Create a new empty delimiter group. * @@ -305,6 +334,8 @@ public class DelimiterGroup { groupName = name; openDelimiters = new HashMap<>(); + nestedOpenDelimiters = new HashMap<>(); + closingDelimiters = new HashSet<>(); topLevelExclusions = new HashSet<>(); @@ -412,11 +443,64 @@ public class DelimiterGroup { subgroups.put(subgroup, priority); } + /** + * Adds a marker that opens a group at the top level of this group. + * + * @param opener + * The marker that opens the group. + * + * @param group + * The group opened by the marker. + */ public void addOpener(T opener, T group) { + if(opener == null) { + throw new NullPointerException("Opener must not be null"); + } else if(group == null) { + throw new NullPointerException("Group to open must not be null"); + } + openDelimiters.put(opener, group); } + /** + * Adds a marker that opens a group inside of this group. + * + * @param opener + * The marker that opens the group. + * + * @param group + * The group opened by the marker. + */ + public void addNestedOpener(T opener, T group) { + if(opener == null) { + throw new NullPointerException("Opener must not be null"); + } else if(group == null) { + throw new NullPointerException("Group to open must not be null"); + } + + nestedOpenDelimiters.put(opener, group); + } + + /** + * Mark a closing delimiter as implying a subgroup. + * + * @param closer + * The closing delimiter. + * + * @param subgroup + * The subgroup to imply. + */ public void implySubgroup(T closer, T subgroup) { + if(closer == null) { + throw new NullPointerException("Closer must not be null"); + } else if(subgroup == null) { + throw new NullPointerException("Subgroup must not be null"); + } else if(!closingDelimiters.contains(closer)) { + throw new IllegalArgumentException(String.format("No closing delimiter '%s' defined", closer)); + } else if(!subgroups.containsKey(subgroup)) { + throw new IllegalArgumentException(String.format("No subgroup '%s' defined", subgroup)); + } + impliedSubgroups.put(closer, subgroup); } @@ -468,18 +552,43 @@ public class DelimiterGroup { * @param opener * The item that opened this group. * + * @param parms + * The parameters that opened this group + * * @return An opened instance of this group. */ public OpenGroup open(T opener, T[] parms) { return new OpenGroup(opener, parms); } + /** + * Adds a predicated opener to the top level of this group. + * + * @param pred + * The predicate that defines the opener and its + * parameters. + */ public void addPredOpener(Function> pred) { predOpeners.add(pred); } + /** + * Adds a predicated closer to the top level of this group. + * + * @param pred + * The predicate that defines the closer. + */ public void addPredCloser(BiPredicate pred) { predClosers.add(pred); } + /** + * Set whether or not this group starts a new nesting set. + * + * @param forgetful + * Whether this group starts a new nesting set. + */ + public void setForgetful(boolean forgetful) { + this.forgetful = forgetful; + } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceCharacteristics.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceCharacteristics.java index f053ef6..5dcda2d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceCharacteristics.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceCharacteristics.java @@ -41,26 +41,18 @@ public class SequenceCharacteristics { this.subgroup = subgroup; } - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ @Override public int hashCode() { final int prime = 31; int result = 1; + result = prime * result + ((contents == null) ? 0 : contents.hashCode()); result = prime * result + ((root == null) ? 0 : root.hashCode()); result = prime * result + ((subgroup == null) ? 0 : subgroup.hashCode()); + return result; } - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ @Override public boolean equals(Object obj) { if(this == obj) return true; diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java index dee5034..e723123 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java @@ -9,11 +9,14 @@ import bjc.utils.esodata.Stack; import bjc.utils.funcdata.IMap; import bjc.utils.funcutils.StringUtils; +import com.google.common.collect.HashMultimap; import com.google.common.collect.HashMultiset; +import com.google.common.collect.Multimap; import com.google.common.collect.Multiset; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; /** * Convert linear sequences into trees that represent group structure. @@ -24,10 +27,6 @@ import java.util.Map; * The type of items in the sequence. */ public class SequenceDelimiter { - /* - * Mapping from opening delimiters to the names of the groups they open - */ - /* * Mapping from group names to actual groups. */ @@ -47,7 +46,7 @@ public class SequenceDelimiter { * structure. * * Essentially, creates a parse tree of the expression against the - * following grammar while obeying the defined group rules. + * following grammar while obeying the defined grouping rules. * *
 	 *              -> ( |  | )*
@@ -95,6 +94,8 @@ public class SequenceDelimiter {
 			throws DelimiterException {
 		if(initialGroup == null) {
 			throw new NullPointerException("Initial group must be specified.");
+		} else if(chars == null) {
+			throw new NullPointerException("Sequence characteristics must not be null");
 		}
 
 		/*
@@ -106,11 +107,18 @@ public class SequenceDelimiter {
 		 * Open initial group.
 		 */
 		groupStack.push(initialGroup.open(chars.root, null));
-		
+
 		/*
 		 * Groups that aren't allowed to be opened at the moment.
 		 */
-		Multiset forbiddenDelimiters = HashMultiset.create();
+		Stack> forbiddenDelimiters = new SimpleStack<>();
+		forbiddenDelimiters.push(HashMultiset.create());
+
+		/*
+		 * Groups that are allowed to be opened at the moment.
+		 */
+		Stack> allowedDelimiters = new SimpleStack<>();
+		allowedDelimiters.push(HashMultimap.create());
 
 		/*
 		 * Map of who forbid what for debugging purposes.
@@ -122,7 +130,19 @@ public class SequenceDelimiter {
 
 			IPair possibleOpenPar = groupStack.top().doesOpen(tok);
 			T possibleOpen = possibleOpenPar.getLeft();
-			
+
+			if(possibleOpen == null) {
+				/*
+				 * Handle nested openers.
+				 * 
+				 * Local openers take priority over nested ones
+				 * if they overlap.
+				 */
+				if(allowedDelimiters.top().containsKey(tok)) {
+					possibleOpen = allowedDelimiters.top().get(tok).iterator().next();
+				}
+			}
+
 			/*
 			 * If we have an opening delimiter, handle it.
 			 */
@@ -167,11 +187,28 @@ public class SequenceDelimiter {
 				DelimiterGroup.OpenGroup open = group.open(tok, possibleOpenPar.getRight());
 				groupStack.push(open);
 
+				/*
+				 * Handle 'forgetful' groups that reset nesting
+				 */
+				if(open.isForgetful()) {
+					allowedDelimiters.push(HashMultimap.create());
+					forbiddenDelimiters.push(HashMultiset.create());
+				}
+
+				/*
+				 * Add the nested opens from this group.
+				 */
+				Multimap currentAllowed = allowedDelimiters.top();
+				for(Entry opener : open.getNestingOpeners().entrySet()) {
+					currentAllowed.put(opener.getKey(), opener.getValue());
+				}
+
 				/*
 				 * Add the nested exclusions from this group
 				 */
+				Multiset currentForbidden = forbiddenDelimiters.top();
 				for(T exclusion : open.getNestingExclusions()) {
-					forbiddenDelimiters.add(exclusion);
+					currentForbidden.add(exclusion);
 
 					whoForbid.put(exclusion, possibleOpen);
 				}
@@ -186,11 +223,28 @@ public class SequenceDelimiter {
 				/*
 				 * Remove nested exclusions from this group.
 				 */
+				Multiset currentForbidden = forbiddenDelimiters.top();
 				for(T excludedGroup : closed.getNestingExclusions()) {
-					forbiddenDelimiters.remove(excludedGroup);
+					currentForbidden.remove(excludedGroup);
 
 					whoForbid.remove(excludedGroup);
 				}
+
+				/*
+				 * Remove the nested opens from this group.
+				 */
+				Multimap currentAllowed = allowedDelimiters.top();
+				for(Entry closer : closed.getNestingOpeners().entrySet()) {
+					currentAllowed.remove(closer.getKey(), closer.getValue());
+				}
+
+				/*
+				 * Handle 'forgetful' groups that reset nesting.
+				 */
+				if(closed.isForgetful()) {
+					allowedDelimiters.drop();
+					forbiddenDelimiters.drop();
+				}
 			} else if(!groupStack.empty() && groupStack.top().marksSubgroup(tok)) {
 				groupStack.top().markSubgroup(tok, chars);
 			} else {
@@ -223,15 +277,16 @@ public class SequenceDelimiter {
 		return groupStack.pop().toTree(chars.root, chars);
 	}
 
-	private boolean isForbidden(Stack.OpenGroup> groupStack, Multiset forbiddenDelimiters,
-			T groupName) {
+	private boolean isForbidden(Stack.OpenGroup> groupStack,
+			Stack> forbiddenDelimiters, T groupName) {
 		boolean localForbid;
+
 		if(groupStack.empty())
 			localForbid = false;
 		else
 			localForbid = groupStack.top().excludes(groupName);
 
-		return localForbid || forbiddenDelimiters.contains(groupName);
+		return localForbid || forbiddenDelimiters.top().contains(groupName);
 	}
 
 	/**
diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java
index 9799ea9..cbf99ee 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java
@@ -26,6 +26,6 @@ public class StringDelimiter extends SequenceDelimiter {
 	 * @see SequenceDelimiter
 	 */
 	public ITree delimitSequence(String... seq) throws DelimiterException {
-		return super.delimitSequence(new SequenceCharacteristics("root", "contents", "subgroup"), seq);
+		return super.delimitSequence(new SequenceCharacteristics<>("root", "contents", "subgroup"), seq);
 	}
 }
-- 
cgit v1.2.3