summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbjculkin <bjculkin@mix.wvu.edu>2017-03-23 10:46:02 -0400
committerbjculkin <bjculkin@mix.wvu.edu>2017-03-23 10:46:02 -0400
commitb0d27faf67ec23b3d55786e00d4fd3b0d07567ee (patch)
tree9abfde359f93cdd85c4178754273349f3f329881
parent73bdb274931e735bb958f73cc5414e50fed572c4 (diff)
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.
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenUtils.java2
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java111
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceCharacteristics.java12
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java81
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java2
5 files changed, 181 insertions, 27 deletions
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;
@@ -210,6 +209,17 @@ public class DelimiterGroup<T> {
}
/**
+ * Get the groups that are allowed to open anywhere inside this
+ * group.
+ *
+ * @return The groups allowed to open anywhere inside this
+ * group.
+ */
+ public Map<T, T> getNestingOpeners() {
+ return nestedOpenDelimiters;
+ }
+
+ /**
* Checks if a given token marks a subgroup.
*
* @param tok
@@ -245,6 +255,15 @@ public class DelimiterGroup<T> {
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;
+ }
}
/**
@@ -258,6 +277,11 @@ public class DelimiterGroup<T> {
private Map<T, T> openDelimiters;
/*
+ * The delimiters that open groups inside of this group.
+ */
+ private Map<T, T> nestedOpenDelimiters;
+
+ /*
* The delimiters that close this group.
*/
private Set<T> closingDelimiters;
@@ -293,6 +317,11 @@ public class DelimiterGroup<T> {
*/
private List<BiPredicate<T, T[]>> 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<T> {
groupName = name;
openDelimiters = new HashMap<>();
+ nestedOpenDelimiters = new HashMap<>();
+
closingDelimiters = new HashSet<>();
topLevelExclusions = new HashSet<>();
@@ -412,11 +443,64 @@ public class DelimiterGroup<T> {
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<T> {
* @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<T, IPair<T, T[]>> 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<T, T[]> 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<T> {
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.
@@ -25,10 +28,6 @@ import java.util.Map;
*/
public class SequenceDelimiter<T> {
/*
- * Mapping from opening delimiters to the names of the groups they open
- */
-
- /*
* Mapping from group names to actual groups.
*/
private Map<T, DelimiterGroup<T>> groups;
@@ -47,7 +46,7 @@ public class SequenceDelimiter<T> {
* 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.
*
* <pre>
* <tree> -> (<data> | <subgroup> | <group>)*
@@ -95,6 +94,8 @@ public class SequenceDelimiter<T> {
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<T> {
* Open initial group.
*/
groupStack.push(initialGroup.open(chars.root, null));
-
+
/*
* Groups that aren't allowed to be opened at the moment.
*/
- Multiset<T> forbiddenDelimiters = HashMultiset.create();
+ Stack<Multiset<T>> forbiddenDelimiters = new SimpleStack<>();
+ forbiddenDelimiters.push(HashMultiset.create());
+
+ /*
+ * Groups that are allowed to be opened at the moment.
+ */
+ Stack<Multimap<T, T>> allowedDelimiters = new SimpleStack<>();
+ allowedDelimiters.push(HashMultimap.create());
/*
* Map of who forbid what for debugging purposes.
@@ -122,7 +130,19 @@ public class SequenceDelimiter<T> {
IPair<T, T[]> 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.
*/
@@ -168,10 +188,27 @@ public class SequenceDelimiter<T> {
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<T, T> currentAllowed = allowedDelimiters.top();
+ for(Entry<T, T> opener : open.getNestingOpeners().entrySet()) {
+ currentAllowed.put(opener.getKey(), opener.getValue());
+ }
+
+ /*
* Add the nested exclusions from this group
*/
+ Multiset<T> 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<T> {
/*
* Remove nested exclusions from this group.
*/
+ Multiset<T> 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<T, T> currentAllowed = allowedDelimiters.top();
+ for(Entry<T, T> 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<T> {
return groupStack.pop().toTree(chars.root, chars);
}
- private boolean isForbidden(Stack<DelimiterGroup<T>.OpenGroup> groupStack, Multiset<T> forbiddenDelimiters,
- T groupName) {
+ private boolean isForbidden(Stack<DelimiterGroup<T>.OpenGroup> groupStack,
+ Stack<Multiset<T>> 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<String> {
* @see SequenceDelimiter
*/
public ITree<String> delimitSequence(String... seq) throws DelimiterException {
- return super.delimitSequence(new SequenceCharacteristics("root", "contents", "subgroup"), seq);
+ return super.delimitSequence(new SequenceCharacteristics<>("root", "contents", "subgroup"), seq);
}
}