summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src')
-rw-r--r--BJC-Utils2/src/examples/java/bjc/utils/examples/DelimSplitterTest.java13
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/DelimiterGroup.java18
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/SequenceDelimiter.java59
3 files changed, 33 insertions, 57 deletions
diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/DelimSplitterTest.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/DelimSplitterTest.java
index a127caa..cebe024 100644
--- a/BJC-Utils2/src/examples/java/bjc/utils/examples/DelimSplitterTest.java
+++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/DelimSplitterTest.java
@@ -176,12 +176,7 @@ public class DelimSplitterTest {
System.out.println("Reset splitter");
}
break;
- case "delims-addopen":
- dlm.addOpener(argArray[0], argArray[1]);
- if(verbose) {
- System.out.printf("Added opener '%s' for group '%s'\n", argArray[0], argArray[1]);
- }
- break;
+
case "delims-addgroup":
for(String arg : argArray) {
dlm.addGroup(groups.get(arg));
@@ -336,6 +331,12 @@ public class DelimSplitterTest {
argArray[0], argArray[1]));
}
break;
+ case "add-opener":
+ group.addOpener(argArray[0], argArray[1]);
+ if(verbose) {
+ System.out.printf("Added opener '%s' for group '%s'\n", argArray[0], argArray[1]);
+ }
+ break;
case "debug":
System.out.println(group.toString());
break;
diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/DelimiterGroup.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/DelimiterGroup.java
index 37a8726..f3d7c3f 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/DelimiterGroup.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/DelimiterGroup.java
@@ -195,6 +195,14 @@ public class DelimiterGroup<T> {
public boolean marksSubgroup(T tok) {
return subgroups.containsKey(tok);
}
+
+ public T doesOpen(T name) {
+ if(openDelimiters.containsKey(name)) {
+ return openDelimiters.get(name);
+ }
+
+ return null;
+ }
}
/**
@@ -203,6 +211,11 @@ public class DelimiterGroup<T> {
public final T groupName;
/*
+ * The delimiters that open groups in this group,
+ */
+ private Map<T, T> openDelimiters;
+
+ /*
* The delimiters that close this group.
*/
private Set<T> closingDelimiters;
@@ -234,6 +247,7 @@ public class DelimiterGroup<T> {
groupName = name;
+ openDelimiters = new HashMap<>();
closingDelimiters = new HashSet<>();
topLevelExclusions = new HashSet<>();
groupExclusions = new HashSet<>();
@@ -334,6 +348,10 @@ public class DelimiterGroup<T> {
subgroups.put(subgroup, priority);
}
+
+ public void addOpener(T opener, T group) {
+ openDelimiters.put(opener, group);
+ }
@Override
public String toString() {
diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/SequenceDelimiter.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/SequenceDelimiter.java
index 55e3dd1..9a3bac6 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/SequenceDelimiter.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/SequenceDelimiter.java
@@ -7,7 +7,6 @@ import bjc.utils.esodata.SimpleStack;
import bjc.utils.esodata.Stack;
import bjc.utils.funcdata.IMap;
import bjc.utils.funcutils.StringUtils;
-import bjc.utils.parserutils.DelimiterGroup.OpenGroup;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
@@ -27,7 +26,6 @@ public class SequenceDelimiter<T> {
/*
* Mapping from opening delimiters to the names of the groups they open
*/
- private Map<T, T> openDelimiters;
/*
* Mapping from group names to actual groups.
@@ -40,8 +38,6 @@ public class SequenceDelimiter<T> {
* Create a new sequence delimiter.
*/
public SequenceDelimiter() {
- openDelimiters = new HashMap<>();
-
groups = new HashMap<>();
}
@@ -65,7 +61,7 @@ public class SequenceDelimiter<T> {
*
* @param chars
* The parameters on how to mark certain portions of the
- * tre.
+ * tree.
* @param seq
* The sequence to delimit.
*
@@ -109,18 +105,7 @@ public class SequenceDelimiter<T> {
* Open initial group.
*/
groupStack.push(initialGroup.open(chars.root));
-
- /*
- * Handle the trivial case where there are no groups.
- */
- if(openDelimiters.isEmpty()) {
- for(T tok : seq) {
- groupStack.top().addItem(new Tree<>(tok));
- }
-
- return groupStack.pop().toTree(chars.root, chars);
- }
-
+
/*
* Groups that aren't allowed to be opened at the moment.
*/
@@ -134,12 +119,12 @@ public class SequenceDelimiter<T> {
for(int i = 0; i < seq.length; i++) {
T tok = seq[i];
+ T possibleOpen = groupStack.top().doesOpen(tok);
/*
* If we have an opening delimiter, handle it.
*/
- if(openDelimiters.containsKey(tok)) {
- T groupName = openDelimiters.get(tok);
- DelimiterGroup<T> group = groups.get(groupName);
+ if(possibleOpen != null) {
+ DelimiterGroup<T> group = groups.get(possibleOpen);
/*
* Error on groups that can't open in this
@@ -149,7 +134,7 @@ public class SequenceDelimiter<T> {
* top-level of this group, as well as nested
* exclusions from all enclosing groups.
*/
- if(isForbidden(groupStack, forbiddenDelimiters, groupName)) {
+ if(isForbidden(groupStack, forbiddenDelimiters, possibleOpen)) {
StringBuilder msgBuilder = new StringBuilder();
T forbiddenBy;
@@ -185,7 +170,7 @@ public class SequenceDelimiter<T> {
for(T exclusion : open.getNestingExclusions()) {
forbiddenDelimiters.add(exclusion);
- whoForbid.put(exclusion, groupName);
+ whoForbid.put(exclusion, possibleOpen);
}
} else if(!groupStack.empty() && groupStack.top().isClosing(tok)) {
/*
@@ -247,28 +232,6 @@ public class SequenceDelimiter<T> {
}
/**
- * Add a open delimiter for the specified group.
- *
- * @param open
- * The open delimiter.
- * @param groupName
- * The name of the group it opens.
- */
- public void addOpener(T open, T groupName) {
- if(open == null) {
- throw new NullPointerException("Opener must not be null");
- } else if(open.equals("")) {
- throw new IllegalArgumentException("Empty string is not a valid opening delimiter");
- } else if(groupName == null) {
- throw new NullPointerException("Group name must not be null");
- } else if(!groups.containsKey(groupName)) {
- throw new IllegalArgumentException("Group " + groupName + " doesn't exist.");
- }
-
- openDelimiters.put(open, groupName);
- }
-
- /**
* Add a delimiter group.
*
* @param group
@@ -300,7 +263,7 @@ public class SequenceDelimiter<T> {
addGroup(group);
for(T open : openers) {
- addOpener(open, groupName);
+ group.addOpener(open, groupName);
}
}
@@ -310,12 +273,6 @@ public class SequenceDelimiter<T> {
builder.append("SequenceDelimiter [");
- if(openDelimiters != null) {
- builder.append("openDelimiters=");
- builder.append(openDelimiters);
- builder.append(", ");
- }
-
if(groups != null) {
builder.append("groups=");
builder.append(groups);