From c82e3b3b2de0633317ec8fc85925e91422820597 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Sun, 8 Oct 2017 22:39:59 -0300 Subject: Start splitting into maven modules --- .../parserutils/delims/DelimiterException.java | 21 + .../utils/parserutils/delims/DelimiterGroup.java | 593 +++++++++++++++++++++ .../bjc/utils/parserutils/delims/RegexCloser.java | 33 ++ .../bjc/utils/parserutils/delims/RegexOpener.java | 54 ++ .../delims/SequenceCharacteristics.java | 93 ++++ .../parserutils/delims/SequenceDelimiter.java | 371 +++++++++++++ .../utils/parserutils/delims/StringDelimiter.java | 31 ++ 7 files changed, 1196 insertions(+) create mode 100644 base/src/main/java/bjc/utils/parserutils/delims/DelimiterException.java create mode 100644 base/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java create mode 100644 base/src/main/java/bjc/utils/parserutils/delims/RegexCloser.java create mode 100644 base/src/main/java/bjc/utils/parserutils/delims/RegexOpener.java create mode 100644 base/src/main/java/bjc/utils/parserutils/delims/SequenceCharacteristics.java create mode 100644 base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java create mode 100644 base/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java (limited to 'base/src/main/java/bjc/utils/parserutils/delims') diff --git a/base/src/main/java/bjc/utils/parserutils/delims/DelimiterException.java b/base/src/main/java/bjc/utils/parserutils/delims/DelimiterException.java new file mode 100644 index 0000000..071afb4 --- /dev/null +++ b/base/src/main/java/bjc/utils/parserutils/delims/DelimiterException.java @@ -0,0 +1,21 @@ +package bjc.utils.parserutils.delims; + +/** + * The superclass for exceptions thrown during sequence delimitation. + */ +public class DelimiterException extends RuntimeException { + /** + * + */ + private static final long serialVersionUID = 2079514406049040888L; + + /** + * Create a new generic delimiter exception. + * + * @param res + * The reason for this exception. + */ + 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 new file mode 100644 index 0000000..b1d8597 --- /dev/null +++ b/base/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java @@ -0,0 +1,593 @@ +package bjc.utils.parserutils.delims; + +import java.util.Arrays; +import java.util.Deque; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.BiPredicate; +import java.util.function.Function; + +import bjc.utils.data.IPair; +import bjc.utils.data.ITree; +import bjc.utils.data.Pair; +import bjc.utils.data.Tree; +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IList; + +/** + * Represents a possible delimiter group to match. + * + * @author EVE + * + * @param + * The type of items in the sequence. + */ +public class DelimiterGroup { + /** + * Represents an instance of a delimiter group. + * + * @author EVE + * + */ + public class OpenGroup { + /* + * The contents of this group. + */ + private final Deque> contents; + + /* + * The contents of the current subgroup. + */ + private IList> currentGroup; + + /* + * The token that opened the group, and any opening parameters. + */ + private final T opener; + private final T[] params; + + /** + * Create a new instance of a delimiter group. + * + * @param open + * The item that opened this group. + * + * @param parms + * Any parameters from the opener. + */ + public OpenGroup(final T open, final T[] parms) { + opener = open; + params = parms; + + contents = new LinkedList<>(); + + currentGroup = new FunctionalList<>(); + } + + /** + * Add an item to this group instance. + * + * @param itm + * The item to add to this group instance. + */ + public void addItem(final ITree itm) { + currentGroup.add(itm); + } + + /** + * Mark a subgroup. + * + * @param marker + * The item that indicated this subgroup. + * + * @param chars + * The characteristics for building the tree. + */ + public void markSubgroup(final T marker, final SequenceCharacteristics chars) { + /* + * Add all of the contents to the subgroup. + */ + final ITree subgroupContents = new Tree<>(chars.contents); + for (final ITree itm : currentGroup) { + subgroupContents.addChild(itm); + } + + /* + * Handle subordinate sub-groups. + */ + while (!contents.isEmpty()) { + final ITree possibleSubordinate = contents.peek(); + + /* + * Subordinate lower priority subgroups. + */ + if (possibleSubordinate.getHead().equals(chars.subgroup)) { + final T otherMarker = possibleSubordinate.getChild(1).getHead(); + + if (subgroups.get(marker) > subgroups.get(otherMarker)) { + subgroupContents.prependChild(contents.pop()); + } else { + break; + } + } else { + subgroupContents.prependChild(contents.pop()); + } + } + + final Tree subgroup = new Tree<>(chars.subgroup, subgroupContents, new Tree<>(marker)); + + contents.push(subgroup); + + currentGroup = new FunctionalList<>(); + } + + /** + * Convert this group into a tree. + * + * @param closer + * The item that closed this group. + * + * @param chars + * The characteristics for building the tree. + * + * @return This group as a tree. + */ + public ITree toTree(final T closer, final SequenceCharacteristics chars) { + /* + * Mark any implied subgroups. + */ + if (impliedSubgroups.containsKey(closer)) { + markSubgroup(impliedSubgroups.get(closer), chars); + } + + final ITree res = new Tree<>(chars.contents); + + /* + * Add either the contents of the current group, + * or subgroups if they're their. + */ + if (contents.isEmpty()) { + currentGroup.forEach(res::addChild); + } else { + while (!contents.isEmpty()) { + res.prependChild(contents.poll()); + } + + currentGroup.forEach(res::addChild); + } + + return new Tree<>(groupName, new Tree<>(opener), res, new Tree<>(closer)); + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + + builder.append("OpenGroup [contents="); + builder.append(contents); + builder.append(", currentGroup="); + builder.append(currentGroup); + builder.append(", opener="); + builder.append(opener); + builder.append("]"); + + return builder.toString(); + } + + /** + * Check if a group is excluded at the top level of this group. + * + * @param groupName + * The group to check. + * + * @return Whether or not the provided group is excluded. + */ + public boolean excludes(final T groupName) { + return topLevelExclusions.contains(groupName); + } + + /** + * Check if the provided delimiter would close this group. + * + * @param del + * The string to check as a closing delimiter. + * + * @return Whether or not the provided delimiter closes this + * group. + */ + public boolean isClosing(final T del) { + if (closingDelimiters.contains(del)) return true; + + for (final BiPredicate pred : predClosers) { + if (pred.test(del, params)) return true; + } + + return closingDelimiters.contains(del); + } + + /** + * Get the name of the group this is an instance of. + * + * @return The name of the group this is an instance of. + */ + public T getName() { + return groupName; + } + + /** + * Get the groups that aren't allowed at all in this group. + * + * @return The groups that aren't allowed at all in this group. + */ + public Set getNestingExclusions() { + 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. + * + * @param tok + * The token to check. + * + * @return Whether or not the token marks a subgroup. + */ + public boolean marksSubgroup(final T tok) { + return subgroups.containsKey(tok); + } + + /** + * Checks if a given token opens a group. + * + * @param marker + * The token to check. + * + * @return The name of the group T opens, or null if it doesn't + * open one. + */ + public IPair doesOpen(final T marker) { + if (openDelimiters.containsKey(marker)) return new Pair<>(openDelimiters.get(marker), null); + + for (final Function> pred : predOpeners) { + final IPair par = pred.apply(marker); + + if (par.getLeft() != null) return par; + } + + 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; + } + } + + /** + * The name of this delimiter group. + */ + public final T groupName; + + /* + * The delimiters that open groups at the top level of this group. + */ + private final Map openDelimiters; + + /* + * The delimiters that open groups inside of this group. + */ + private final Map nestedOpenDelimiters; + + /* + * The delimiters that close this group. + */ + private final Set closingDelimiters; + + /* + * The groups that can't occur in the top level of this group. + */ + private final Set topLevelExclusions; + + /* + * The groups that can't occur anywhere inside this group. + */ + private final Set groupExclusions; + + /* + * Mapping from sub-group delimiters, to any sub-groups enclosed in + * them. + */ + private final Map subgroups; + + /* + * Subgroups implied by a particular closing delimiter + */ + private final Map impliedSubgroups; + + /* + * Allows more complex openings + */ + private final List>> predOpeners; + + /* + * Allow more complex closings + */ + private final List> predClosers; + + /* + * Whether or not this group starts a new nesting set. + */ + private boolean forgetful; + + /** + * Create a new empty delimiter group. + * + * @param name + * The name of the delimiter group + */ + public DelimiterGroup(final T name) { + if (name == null) throw new NullPointerException("Group name must not be null"); + + groupName = name; + + openDelimiters = new HashMap<>(); + nestedOpenDelimiters = new HashMap<>(); + + closingDelimiters = new HashSet<>(); + + topLevelExclusions = new HashSet<>(); + groupExclusions = new HashSet<>(); + + subgroups = new HashMap<>(); + impliedSubgroups = new HashMap<>(); + + predOpeners = new LinkedList<>(); + predClosers = new LinkedList<>(); + } + + /** + * Adds one or more delimiters that close this group. + * + * @param closers + * Delimiters that close this group. + */ + @SafeVarargs + public final void addClosing(final T... closers) { + final List closerList = Arrays.asList(closers); + + for (final T closer : closerList) { + if (closer == null) + throw new NullPointerException("Closing delimiter must not be null"); + 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 { + closingDelimiters.add(closer); + } + } + } + + /** + * Adds one or more groups that cannot occur in the top level of this + * group. + * + * @param exclusions + * The groups forbidden in the top level of this group. + */ + @SafeVarargs + public final void addTopLevelForbid(final T... exclusions) { + for (final T exclusion : exclusions) { + if (exclusion == null) + throw new NullPointerException("Exclusion must not be null"); + 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 { + topLevelExclusions.add(exclusion); + } + } + } + + /** + * Adds one or more groups that cannot occur at all in this group. + * + * @param exclusions + * The groups forbidden inside this group. + */ + @SafeVarargs + public final void addGroupForbid(final T... exclusions) { + for (final T exclusion : exclusions) { + if (exclusion == null) + throw new NullPointerException("Exclusion must not be null"); + 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 { + groupExclusions.add(exclusion); + } + } + } + + /** + * Adds sub-group markers to this group. + * + * @param subgroup + * The token to mark a sub-group. + * + * @param priority + * 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"); + + 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(final T opener, final 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(final T opener, final 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(final T closer, final 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); + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + + builder.append("("); + + builder.append("groupName=["); + builder.append(groupName); + builder.append("], "); + + builder.append("closingDelimiters=["); + for (final T closer : closingDelimiters) { + builder.append(closer + ","); + } + builder.deleteCharAt(builder.length() - 1); + builder.append("]"); + + if (topLevelExclusions != null && !topLevelExclusions.isEmpty()) { + builder.append(", "); + builder.append("topLevelExclusions=["); + for (final T exclusion : topLevelExclusions) { + builder.append(exclusion + ","); + } + builder.deleteCharAt(builder.length() - 1); + builder.append("]"); + } + + if (groupExclusions != null && !groupExclusions.isEmpty()) { + builder.append(", "); + builder.append("groupExclusions=["); + for (final T exclusion : groupExclusions) { + builder.append(exclusion + ","); + } + builder.deleteCharAt(builder.length() - 1); + builder.append("]"); + } + + builder.append(" )"); + + return builder.toString(); + } + + /** + * Open an instance of this group. + * + * @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(final T opener, final 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(final 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(final 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(final boolean forgetful) { + this.forgetful = forgetful; + } +} diff --git a/base/src/main/java/bjc/utils/parserutils/delims/RegexCloser.java b/base/src/main/java/bjc/utils/parserutils/delims/RegexCloser.java new file mode 100644 index 0000000..4b29949 --- /dev/null +++ b/base/src/main/java/bjc/utils/parserutils/delims/RegexCloser.java @@ -0,0 +1,33 @@ +package bjc.utils.parserutils.delims; + +import java.util.function.BiPredicate; + +/** + * A predicated closer for use with {@link RegexOpener}. + * + * @author bjculkin + * + */ +public class RegexCloser implements BiPredicate { + private final String rep; + + /** + * Create a new regex closer. + * + * @param closer + * The format string to use for closing. + */ + public RegexCloser(final String closer) { + rep = closer; + } + + @Override + public boolean test(final String closer, final String[] params) { + /* + * Confirm passing an array instead of a single var-arg. + */ + final String work = String.format(rep, (Object[]) params); + + 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 new file mode 100644 index 0000000..ee93b73 --- /dev/null +++ b/base/src/main/java/bjc/utils/parserutils/delims/RegexOpener.java @@ -0,0 +1,54 @@ +package bjc.utils.parserutils.delims; + +import java.util.function.Function; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import bjc.utils.data.IPair; +import bjc.utils.data.Pair; + +/** + * A predicated opener for use with {@link RegexCloser} + * + * @author bjculkin + * + */ +public class RegexOpener implements Function> { + private final String name; + + private final Pattern patt; + + /** + * Create a new regex opener. + * + * @param groupName + * The name of the opened group. + * + * @param groupRegex + * The regex that matches the opener. + */ + public RegexOpener(final String groupName, final String groupRegex) { + name = groupName; + + patt = Pattern.compile(groupRegex); + } + + @Override + public IPair apply(final String str) { + final Matcher m = patt.matcher(str); + + if (m.matches()) { + final int numGroups = m.groupCount(); + + final String[] parms = new String[numGroups + 1]; + + for (int i = 0; i <= numGroups; i++) { + parms[i] = m.group(i); + } + + return new Pair<>(name, parms); + } + + return new Pair<>(null, null); + } +} diff --git a/base/src/main/java/bjc/utils/parserutils/delims/SequenceCharacteristics.java b/base/src/main/java/bjc/utils/parserutils/delims/SequenceCharacteristics.java new file mode 100644 index 0000000..882b4c5 --- /dev/null +++ b/base/src/main/java/bjc/utils/parserutils/delims/SequenceCharacteristics.java @@ -0,0 +1,93 @@ +package bjc.utils.parserutils.delims; + +/** + * Marks the parameters for building a sequence tree. + * + * @author EVE + * + * @param + * The type of item in the tree. + */ +public class SequenceCharacteristics { + /** + * The item to mark the root of the tree. + */ + public final T root; + + /** + * The item to mark the contents of a group/subgroup. + */ + + public final T contents; + + /** + * The item to mark a subgroup. + */ + public final T subgroup; + + /** + * Create a new set of parameters for building a tree. + * + * @param root + * The root marker. + * @param contents + * The group/subgroup contents marker. + * @param subgroup + * The subgroup marker. + */ + public SequenceCharacteristics(final T root, final T contents, final T subgroup) { + this.root = root; + this.contents = contents; + this.subgroup = subgroup; + } + + @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; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof SequenceCharacteristics)) return false; + + final SequenceCharacteristics other = (SequenceCharacteristics) obj; + + if (contents == null) { + if (other.contents != null) return false; + } else if (!contents.equals(other.contents)) return false; + + if (root == null) { + if (other.root != null) return false; + } else if (!root.equals(other.root)) return false; + + if (subgroup == null) { + if (other.subgroup != null) return false; + } else if (!subgroup.equals(other.subgroup)) return false; + + return true; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + + builder.append("SequenceCharacteristics [root="); + builder.append(root == null ? "(null)" : root); + builder.append(", contents="); + builder.append(contents == null ? "(null)" : contents); + builder.append(", subgroup="); + builder.append(subgroup == null ? "(null)" : subgroup); + builder.append("]"); + + return builder.toString(); + } +} \ No newline at end of file diff --git a/base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java b/base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java new file mode 100644 index 0000000..ccfaffb --- /dev/null +++ b/base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java @@ -0,0 +1,371 @@ +package bjc.utils.parserutils.delims; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +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 bjc.utils.data.IPair; +import bjc.utils.data.ITree; +import bjc.utils.data.Tree; +import bjc.utils.esodata.PushdownMap; +import bjc.utils.esodata.SimpleStack; +import bjc.utils.esodata.Stack; +import bjc.utils.funcdata.IMap; +import bjc.utils.funcutils.StringUtils; + +/** + * Convert linear sequences into trees that represent group structure. + * + * @author EVE + * + * @param + * The type of items in the sequence. + */ +public class SequenceDelimiter { + /* + * Mapping from group names to actual groups. + */ + private final Map> groups; + + /* + * The initial group to start with. + */ + private DelimiterGroup initialGroup; + + /** + * Create a new sequence delimiter. + */ + public SequenceDelimiter() { + groups = new HashMap<>(); + } + + /** + * Convert a linear sequence into a tree that matches the delimiter + * structure. + * + * Essentially, creates a parse tree of the expression against the + * following grammar while obeying the defined grouping rules. + * + *
+	 *              → ( |  | )*
+	 *          
+	 *           
+	 *
+	 *              → STRING
+	 *              → STRING
+	 *             → STRING
+	 *            → STRING
+	 * 
+ * + * @param chars + * The parameters on how to mark certain portions of the + * tree. + * @param seq + * The sequence to delimit. + * + * @return The sequence as a tree that matches its group structure. Each + * node in the tree is either a data node, a subgroup node, or a + * group node. + * + * A data node is a leaf node whose data is the string it + * represents. + * + * A subgroup node is a node with two children, and the name of + * the sub-group as its label. The first child is the contents + * of the sub-group, and the second is the marker that started + * the subgroup. The marker is a leaf node labeled with its + * contents, and the contents contains a recursive tree. + * + * A group node is a node with three children, and the name of + * the group as its label. The first child is the opening + * delimiter, the second is the group contents, and the third is + * the closing delimiter. The delimiters are leaf nodes labeled + * with their contents, while the group node contains a + * recursive tree. + * + * @throws DelimiterException + * Thrown if something went wrong during sequence + * delimitation. + * + */ + public ITree delimitSequence(final SequenceCharacteristics chars, + @SuppressWarnings("unchecked") final T... seq) 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"); + + /* + * The stack of opened and not yet closed groups. + */ + final Stack.OpenGroup> groupStack = new SimpleStack<>(); + + /* + * Open initial group. + */ + groupStack.push(initialGroup.open(chars.root, null)); + + /* + * Groups that aren't allowed to be opened at the moment. + */ + final Stack> forbiddenDelimiters = new SimpleStack<>(); + forbiddenDelimiters.push(HashMultiset.create()); + + /* + * Groups that are allowed to be opened at the moment. + */ + final Stack> allowedDelimiters = new SimpleStack<>(); + allowedDelimiters.push(HashMultimap.create()); + + /* + * Map of who forbid what for debugging purposes. + */ + final IMap whoForbid = new PushdownMap<>(); + + /* + * Process each member of the sequence. + */ + for (int i = 0; i < seq.length; i++) { + final T tok = seq[i]; + + /* + * Check if this token could open a group. + */ + final 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. + */ + if (possibleOpen != null) { + final DelimiterGroup group = groups.get(possibleOpen); + + /* + * Error on groups that can't open in this + * context. + * + * This means groups that can't occur at the + * top-level of this group, as well as nested + * exclusions from all enclosing groups. + */ + if (isForbidden(groupStack, forbiddenDelimiters, possibleOpen)) { + T forbiddenBy; + + if (whoForbid.containsKey(tok)) { + forbiddenBy = whoForbid.get(tok); + } else { + forbiddenBy = groupStack.top().getName(); + } + + final String ctxList = StringUtils.toEnglishList(groupStack.toArray(), "then"); + + final String fmt = "Group '%s' can't be opened in this context. (forbidden by '%s')\nContext Stack: %s"; + + throw new DelimiterException(String.format(fmt, group, forbiddenBy, ctxList)); + } + + /* + * Add an open group. + */ + final 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. + */ + final Multimap currentAllowed = allowedDelimiters.top(); + for (final Entry opener : open.getNestingOpeners().entrySet()) { + currentAllowed.put(opener.getKey(), opener.getValue()); + } + + /* + * Add the nested exclusions from this group + */ + final Multiset currentForbidden = forbiddenDelimiters.top(); + for (final T exclusion : open.getNestingExclusions()) { + currentForbidden.add(exclusion); + + whoForbid.put(exclusion, possibleOpen); + } + } else if (!groupStack.empty() && groupStack.top().isClosing(tok)) { + /* + * Close the group. + */ + final DelimiterGroup.OpenGroup closed = groupStack.pop(); + + groupStack.top().addItem(closed.toTree(tok, chars)); + + /* + * Remove nested exclusions from this group. + */ + final Multiset currentForbidden = forbiddenDelimiters.top(); + for (final T excludedGroup : closed.getNestingExclusions()) { + currentForbidden.remove(excludedGroup); + + whoForbid.remove(excludedGroup); + } + + /* + * Remove the nested opens from this group. + */ + final Multimap currentAllowed = allowedDelimiters.top(); + for (final 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)) { + /* + * Mark a subgroup. + */ + groupStack.top().markSubgroup(tok, chars); + } else { + /* + * Add an item to the group. + */ + groupStack.top().addItem(new Tree<>(tok)); + } + } + + /* + * Error if not all groups were closed. + */ + if (groupStack.size() > 1) { + final DelimiterGroup.OpenGroup group = groupStack.top(); + + final StringBuilder msgBuilder = new StringBuilder(); + + 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)); + } + + return groupStack.pop().toTree(chars.root, chars); + } + + /* + * Check if a group is forbidden to open in a context. + */ + private boolean isForbidden(final Stack.OpenGroup> groupStack, + final Stack> forbiddenDelimiters, final T groupName) { + boolean localForbid; + + /* + * Check if a delimiter is locally forbidden. + */ + if (groupStack.empty()) { + localForbid = false; + } else { + localForbid = groupStack.top().excludes(groupName); + } + + return localForbid || forbiddenDelimiters.top().contains(groupName); + } + + /** + * Add a delimiter group. + * + * @param group + * The delimiter group. + */ + public void addGroup(final DelimiterGroup group) { + if (group == null) throw new NullPointerException("Group must not be null"); + + groups.put(group.groupName, group); + } + + /** + * Creates and adds a delimiter group using the provided settings. + * + * @param openers + * The tokens that open this group + * @param groupName + * The name of the group + * @param closers + * The tokens that close this group + */ + public void addGroup(final T[] openers, final T groupName, @SuppressWarnings("unchecked") final T... closers) { + final DelimiterGroup group = new DelimiterGroup<>(groupName); + + group.addClosing(closers); + + addGroup(group); + + for (final T open : openers) { + group.addOpener(open, groupName); + } + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + + builder.append("SequenceDelimiter ["); + + if (groups != null) { + builder.append("groups="); + builder.append(groups); + builder.append(","); + } + + if (initialGroup != null) { + builder.append("initialGroup="); + builder.append(initialGroup); + } + + builder.append("]"); + + return builder.toString(); + } + + /** + * Set the initial group of this delimiter. + * + * @param initialGroup + * The initial group of this delimiter. + */ + public void setInitialGroup(final DelimiterGroup initialGroup) { + this.initialGroup = initialGroup; + } +} diff --git a/base/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java b/base/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java new file mode 100644 index 0000000..e3eeea5 --- /dev/null +++ b/base/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java @@ -0,0 +1,31 @@ +package bjc.utils.parserutils.delims; + +import bjc.utils.data.ITree; + +/** + * A sequence delimiter specialized for strings. + * + * @author EVE + * + */ +public class StringDelimiter extends SequenceDelimiter { + + /** + * Override of + * {@link SequenceDelimiter#delimitSequence(SequenceCharacteristics, Object...)} + * for ease of use for strings. + * + * @param seq + * The sequence to delimit. + * + * @return The sequence as a tree. + * + * @throws DelimiterException + * if something went wrong with delimiting the sequence. + * + * @see SequenceDelimiter + */ + public ITree delimitSequence(final String... seq) throws DelimiterException { + return super.delimitSequence(new SequenceCharacteristics<>("root", "contents", "subgroup"), seq); + } +} -- cgit v1.2.3