summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/parserutils/delims
diff options
context:
space:
mode:
authorBenjamin J. Culkin <bjculkin@mix.wvu.edu>2018-02-13 20:52:37 -0400
committerBenjamin J. Culkin <bjculkin@mix.wvu.edu>2018-02-13 20:52:37 -0400
commit2d8f0aba5565b292f17695afd276143a4f71c72b (patch)
tree2e855252038af0b15275958b64caaee9e72f8847 /base/src/main/java/bjc/utils/parserutils/delims
parentcb45a62d8629e079ae403489efab392d7208cb62 (diff)
Update
Diffstat (limited to 'base/src/main/java/bjc/utils/parserutils/delims')
-rw-r--r--base/src/main/java/bjc/utils/parserutils/delims/DelimiterException.java5
-rw-r--r--base/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java103
-rw-r--r--base/src/main/java/bjc/utils/parserutils/delims/RegexCloser.java3
-rw-r--r--base/src/main/java/bjc/utils/parserutils/delims/RegexOpener.java3
-rw-r--r--base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java91
5 files changed, 79 insertions, 126 deletions
diff --git a/base/src/main/java/bjc/utils/parserutils/delims/DelimiterException.java b/base/src/main/java/bjc/utils/parserutils/delims/DelimiterException.java
index 4172d32..427b4cf 100644
--- a/base/src/main/java/bjc/utils/parserutils/delims/DelimiterException.java
+++ b/base/src/main/java/bjc/utils/parserutils/delims/DelimiterException.java
@@ -4,9 +4,6 @@ package bjc.utils.parserutils.delims;
* The superclass for exceptions thrown during sequence delimitation.
*/
public class DelimiterException extends RuntimeException {
- /**
- *
- */
private static final long serialVersionUID = 2079514406049040888L;
/**
@@ -18,4 +15,4 @@ public class DelimiterException extends RuntimeException {
public DelimiterException(final String res) {
super(res);
}
-} \ No newline at end of file
+}
diff --git a/base/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java b/base/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java
index 5d020a6..0fe998d 100644
--- a/base/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java
+++ b/base/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java
@@ -144,11 +144,12 @@ public class DelimiterGroup<T> {
markSubgroup(impliedSubgroups.get(closer), chars);
}
+ /* The resulting tree. */
final ITree<T> res = new Tree<>(chars.contents);
/*
- * Add either the contents of the current group, or
- * subgroups if they're their.
+ * Add either the contents of the current group,
+ * or subgroups if they're there.
*/
if(contents.isEmpty()) {
currentGroup.forEach(res::addChild);
@@ -260,12 +261,14 @@ public class DelimiterGroup<T> {
* open one.
*/
public IPair<T, T[]> doesOpen(final T marker) {
- if(openDelimiters.containsKey(marker)) return new Pair<>(openDelimiters.get(marker), null);
+ if (openDelimiters.containsKey(marker))
+ return new Pair<>(openDelimiters.get(marker), null);
for(final Function<T, IPair<T, T[]>> pred : predOpeners) {
final IPair<T, T[]> par = pred.apply(marker);
- if(par.getLeft() != null) return par;
+ if (par.getLeft() != null)
+ return par;
}
return new Pair<>(null, null);
@@ -286,29 +289,19 @@ public class DelimiterGroup<T> {
*/
public final T groupName;
- /*
- * The delimiters that open groups at the top level of this group.
- */
+ /* The delimiters that open groups at the top level of this group. */
private final Map<T, T> openDelimiters;
- /*
- * The delimiters that open groups inside of this group.
- */
+ /* The delimiters that open groups inside of this group. */
private final Map<T, T> nestedOpenDelimiters;
- /*
- * The delimiters that close this group.
- */
+ /* The delimiters that close this group. */
private final Set<T> closingDelimiters;
- /*
- * The groups that can't occur in the top level of this group.
- */
+ /* The groups that can't occur in the top level of this group. */
private final Set<T> topLevelExclusions;
- /*
- * The groups that can't occur anywhere inside this group.
- */
+ /* The groups that can't occur anywhere inside this group. */
private final Set<T> groupExclusions;
/*
@@ -317,24 +310,16 @@ public class DelimiterGroup<T> {
*/
private final Map<T, Integer> subgroups;
- /*
- * Subgroups implied by a particular closing delimiter
- */
+ /* Subgroups implied by a particular closing delimiter */
private final Map<T, T> impliedSubgroups;
- /*
- * Allows more complex openings
- */
+ /* Allows more complex openings */
private final List<Function<T, IPair<T, T[]>>> predOpeners;
- /*
- * Allow more complex closings
- */
+ /* Allow more complex closings */
private final List<BiPredicate<T, T[]>> predClosers;
- /*
- * Whether or not this group starts a new nesting set.
- */
+ /* Whether or not this group starts a new nesting set. */
private boolean forgetful;
/**
@@ -344,19 +329,20 @@ public class DelimiterGroup<T> {
* The name of the delimiter group
*/
public DelimiterGroup(final T name) {
- if(name == null) throw new NullPointerException("Group name must not be null");
+ if (name == null)
+ throw new NullPointerException("Group name must not be null");
groupName = name;
- openDelimiters = new HashMap<>();
+ openDelimiters = new HashMap<>();
nestedOpenDelimiters = new HashMap<>();
closingDelimiters = new HashSet<>();
topLevelExclusions = new HashSet<>();
- groupExclusions = new HashSet<>();
+ groupExclusions = new HashSet<>();
- subgroups = new HashMap<>();
+ subgroups = new HashMap<>();
impliedSubgroups = new HashMap<>();
predOpeners = new LinkedList<>();
@@ -373,17 +359,17 @@ public class DelimiterGroup<T> {
public final void addClosing(final T... closers) {
final List<T> closerList = Arrays.asList(closers);
- for(final T closer : closerList) {
- if(closer == null)
+ for (final T closer : closerList) {
+ if (closer == null) {
throw new NullPointerException("Closing delimiter must not be null");
- else if(closer.equals(""))
+ } else if (closer.equals("")) {
/*
* We can do this because equals works on
* arbitrary objects, not just those of the same
* type.
*/
throw new IllegalArgumentException("Empty string is not a valid exclusion");
- else {
+ } else {
closingDelimiters.add(closer);
}
}
@@ -398,17 +384,17 @@ public class DelimiterGroup<T> {
*/
@SafeVarargs
public final void addTopLevelForbid(final T... exclusions) {
- for(final T exclusion : exclusions) {
- if(exclusion == null)
+ for (final T exclusion : exclusions) {
+ if (exclusion == null) {
throw new NullPointerException("Exclusion must not be null");
- else if(exclusion.equals(""))
+ } else if (exclusion.equals("")) {
/*
* We can do this because equals works on
* arbitrary objects, not just those of the same
* type.
*/
throw new IllegalArgumentException("Empty string is not a valid exclusion");
- else {
+ } else {
topLevelExclusions.add(exclusion);
}
}
@@ -422,17 +408,17 @@ public class DelimiterGroup<T> {
*/
@SafeVarargs
public final void addGroupForbid(final T... exclusions) {
- for(final T exclusion : exclusions) {
- if(exclusion == null)
+ for (final T exclusion : exclusions) {
+ if (exclusion == null) {
throw new NullPointerException("Exclusion must not be null");
- else if(exclusion.equals(""))
+ } else if (exclusion.equals("")) {
/*
* We can do this because equals works on
* arbitrary objects, not just those of the same
* type.
*/
throw new IllegalArgumentException("Empty string is not a valid exclusion");
- else {
+ } else {
groupExclusions.add(exclusion);
}
}
@@ -448,7 +434,8 @@ public class DelimiterGroup<T> {
* The priority of this sub-group.
*/
public void addSubgroup(final T subgroup, final int priority) {
- if(subgroup == null) throw new NullPointerException("Subgroup marker must not be null");
+ if (subgroup == null)
+ throw new NullPointerException("Subgroup marker must not be null");
subgroups.put(subgroup, priority);
}
@@ -463,9 +450,10 @@ public class DelimiterGroup<T> {
* The group opened by the marker.
*/
public void addOpener(final T opener, final T group) {
- if(opener == null)
+ 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");
+ else if (group == null)
+ throw new NullPointerException("Group to open must not be null");
openDelimiters.put(opener, group);
}
@@ -480,9 +468,11 @@ public class DelimiterGroup<T> {
* The group opened by the marker.
*/
public void addNestedOpener(final T opener, final T group) {
- if(opener == null)
+ 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");
+ } else if (group == null) {
+ throw new NullPointerException("Group to open must not be null");
+ }
nestedOpenDelimiters.put(opener, group);
}
@@ -497,14 +487,15 @@ public class DelimiterGroup<T> {
* The subgroup to imply.
*/
public void implySubgroup(final T closer, final T subgroup) {
- if(closer == null)
+ if (closer == null) {
throw new NullPointerException("Closer must not be null");
- else if(subgroup == null)
+ } else if (subgroup == null) {
throw new NullPointerException("Subgroup must not be null");
- else if(!closingDelimiters.contains(closer))
+ } else if (!closingDelimiters.contains(closer)) {
throw new IllegalArgumentException(String.format("No closing delimiter '%s' defined", closer));
- else if(!subgroups.containsKey(subgroup))
+ } else if (!subgroups.containsKey(subgroup)) {
throw new IllegalArgumentException(String.format("No subgroup '%s' defined", subgroup));
+ }
impliedSubgroups.put(closer, subgroup);
}
diff --git a/base/src/main/java/bjc/utils/parserutils/delims/RegexCloser.java b/base/src/main/java/bjc/utils/parserutils/delims/RegexCloser.java
index ac257d1..a3c831e 100644
--- a/base/src/main/java/bjc/utils/parserutils/delims/RegexCloser.java
+++ b/base/src/main/java/bjc/utils/parserutils/delims/RegexCloser.java
@@ -9,6 +9,7 @@ import java.util.function.BiPredicate;
*
*/
public class RegexCloser implements BiPredicate<String, String[]> {
+ /* Closing string. */
private final String rep;
/**
@@ -30,4 +31,4 @@ public class RegexCloser implements BiPredicate<String, String[]> {
return work.equals(closer);
}
-} \ No newline at end of file
+}
diff --git a/base/src/main/java/bjc/utils/parserutils/delims/RegexOpener.java b/base/src/main/java/bjc/utils/parserutils/delims/RegexOpener.java
index 15f11e3..a84b148 100644
--- a/base/src/main/java/bjc/utils/parserutils/delims/RegexOpener.java
+++ b/base/src/main/java/bjc/utils/parserutils/delims/RegexOpener.java
@@ -14,8 +14,9 @@ import bjc.utils.data.Pair;
*
*/
public class RegexOpener implements Function<String, IPair<String, String[]>> {
+ /* The name of the group. */
private final String name;
-
+ /* The pattern that marks an opening group. */
private final Pattern patt;
/**
diff --git a/base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java b/base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java
index 0eddebe..6aa3cc9 100644
--- a/base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java
+++ b/base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java
@@ -27,19 +27,13 @@ import bjc.utils.funcutils.StringUtils;
* The type of items in the sequence.
*/
public class SequenceDelimiter<T> {
- /*
- * Mapping from group names to actual groups.
- */
+ /* Mapping from group names to actual groups. */
private final Map<T, DelimiterGroup<T>> groups;
- /*
- * The initial group to start with.
- */
+ /* The initial group to start with. */
private DelimiterGroup<T> initialGroup;
- /**
- * Create a new sequence delimiter.
- */
+ /** Create a new sequence delimiter. */
public SequenceDelimiter() {
groups = new HashMap<>();
}
@@ -93,46 +87,36 @@ public class SequenceDelimiter<T> {
*/
public ITree<T> delimitSequence(final SequenceCharacteristics<T> chars,
@SuppressWarnings("unchecked") final T... seq) throws DelimiterException {
- if(initialGroup == null)
+ if (initialGroup == null) {
throw new NullPointerException("Initial group must be specified.");
- else if(chars == null) throw new NullPointerException("Sequence characteristics must not be null");
+ } else if (chars == null) {
+ throw new NullPointerException("Sequence characteristics must not be null");
+ }
- /*
- * The stack of opened and not yet closed groups.
- */
+ /* The stack of opened and not yet closed groups. */
final Stack<DelimiterGroup<T>.OpenGroup> groupStack = new SimpleStack<>();
- /*
- * Open initial group.
- */
+ /* Open initial group. */
groupStack.push(initialGroup.open(chars.root, null));
- /*
- * Groups that aren't allowed to be opened at the moment.
- */
+ /* Groups that aren't allowed to be opened at the moment. */
final Stack<Multiset<T>> forbiddenDelimiters = new SimpleStack<>();
forbiddenDelimiters.push(HashMultiset.create());
- /*
- * Groups that are allowed to be opened at the moment.
- */
+ /* Groups that are allowed to be opened at the moment. */
final Stack<Multimap<T, T>> allowedDelimiters = new SimpleStack<>();
allowedDelimiters.push(HashMultimap.create());
- /*
- * Map of who forbid what for debugging purposes.
- */
+ /* Map of who forbid what for debugging purposes. */
final IMap<T, T> whoForbid = new PushdownMap<>();
- /*
+ /*
* Process each member of the sequence.
*/
- for(int i = 0; i < seq.length; i++) {
+ for (int i = 0; i < seq.length; i++) {
final T tok = seq[i];
- /*
- * Check if this token could open a group.
- */
+ /* Check if this token could open a group. */
final IPair<T, T[]> possibleOpenPar = groupStack.top().doesOpen(tok);
T possibleOpen = possibleOpenPar.getLeft();
@@ -178,9 +162,7 @@ public class SequenceDelimiter<T> {
throw new DelimiterException(String.format(fmt, group, forbiddenBy, ctxList));
}
- /*
- * Add an open group.
- */
+ /* Add an open group. */
final DelimiterGroup<T>.OpenGroup open = group.open(tok, possibleOpenPar.getRight());
groupStack.push(open);
@@ -192,17 +174,13 @@ public class SequenceDelimiter<T> {
forbiddenDelimiters.push(HashMultiset.create());
}
- /*
- * Add the nested opens from this group.
- */
+ /* Add the nested opens from this group. */
final Multimap<T, T> currentAllowed = allowedDelimiters.top();
for(final Entry<T, T> opener : open.getNestingOpeners().entrySet()) {
currentAllowed.put(opener.getKey(), opener.getValue());
}
- /*
- * Add the nested exclusions from this group
- */
+ /* Add the nested exclusions from this group */
final Multiset<T> currentForbidden = forbiddenDelimiters.top();
for(final T exclusion : open.getNestingExclusions()) {
currentForbidden.add(exclusion);
@@ -217,9 +195,7 @@ public class SequenceDelimiter<T> {
groupStack.top().addItem(closed.toTree(tok, chars));
- /*
- * Remove nested exclusions from this group.
- */
+ /* Remove nested exclusions from this group. */
final Multiset<T> currentForbidden = forbiddenDelimiters.top();
for(final T excludedGroup : closed.getNestingExclusions()) {
currentForbidden.remove(excludedGroup);
@@ -227,9 +203,7 @@ public class SequenceDelimiter<T> {
whoForbid.remove(excludedGroup);
}
- /*
- * Remove the nested opens from this group.
- */
+ /* Remove the nested opens from this group. */
final Multimap<T, T> currentAllowed = allowedDelimiters.top();
for(final Entry<T, T> closer : closed.getNestingOpeners().entrySet()) {
currentAllowed.remove(closer.getKey(), closer.getValue());
@@ -248,9 +222,7 @@ public class SequenceDelimiter<T> {
*/
groupStack.top().markSubgroup(tok, chars);
} else {
- /*
- * Add an item to the group.
- */
+ /* Add an item to the group. */
groupStack.top().addItem(new Tree<>(tok));
}
}
@@ -261,20 +233,11 @@ public class SequenceDelimiter<T> {
if(groupStack.size() > 1) {
final DelimiterGroup<T>.OpenGroup group = groupStack.top();
- final StringBuilder msgBuilder = new StringBuilder();
-
- final String closingDelims = StringUtils.toEnglishList(group.getNestingExclusions().toArray(),
- false);
+ final String closingDelims = StringUtils.toEnglishList(
+ group.getNestingExclusions().toArray(), false);
final String ctxList = StringUtils.toEnglishList(groupStack.toArray(), "then");
- msgBuilder.append("Unclosed group '");
- msgBuilder.append(group.getName());
- msgBuilder.append("'. Expected one of ");
- msgBuilder.append(closingDelims);
- msgBuilder.append(" to close it\nOpen groups: ");
- msgBuilder.append(ctxList);
-
final String fmt = "Unclosed group '%s'. Expected one of %s to close it.\nOpen groups: %n";
throw new DelimiterException(String.format(fmt, group.getName(), closingDelims, ctxList));
@@ -283,9 +246,7 @@ public class SequenceDelimiter<T> {
return groupStack.pop().toTree(chars.root, chars);
}
- /*
- * Check if a group is forbidden to open in a context.
- */
+ /* Check if a group is forbidden to open in a context. */
private boolean isForbidden(final Stack<DelimiterGroup<T>.OpenGroup> groupStack,
final Stack<Multiset<T>> forbiddenDelimiters, final T groupName) {
boolean localForbid;
@@ -309,7 +270,9 @@ public class SequenceDelimiter<T> {
* The delimiter group.
*/
public void addGroup(final DelimiterGroup<T> group) {
- if(group == null) throw new NullPointerException("Group must not be null");
+ if (group == null) {
+ throw new NullPointerException("Group must not be null");
+ }
groups.put(group.groupName, group);
}