From 415f5689fe900a04bf64d41878cfa225905b6617 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Sat, 18 Mar 2017 19:58:22 -0400 Subject: Attempt to get subgroups working --- .../bjc/utils/parserutils/SequenceDelimiter.java | 70 ++++++++++++++++------ 1 file changed, 53 insertions(+), 17 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/SequenceDelimiter.java') 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 96a6c65..af6ba81 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/SequenceDelimiter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/SequenceDelimiter.java @@ -76,6 +76,7 @@ public class SequenceDelimiter { closingDelimiters = new HashSet<>(); topLevelExclusions = new HashSet<>(); groupExclusions = new HashSet<>(); + subgroups = new HashMap<>(); } /** @@ -473,60 +474,60 @@ public class SequenceDelimiter { whoForbid.remove(excludedGroup); } - } else if(!groupStack.empty() && groupStack.top().subgroups.containsKey(tok)){ + } else if(!groupStack.empty() && groupStack.top().subgroups.containsKey(tok)) { /* * Parse a sub-group. */ - + /* * The set of enclosed groups. */ Set enclosed = groupStack.top().subgroups.get(tok); - + /* * The current contents of this group. */ ITree contentTree = trees.pop(); - + /* - * Find the first element to enclose in the subgroup. + * Find the first element to enclose in the + * subgroup. */ int ind = contentTree.revFind((chd) -> { - if(chd.getHead().equals(subgroup)) { - return !enclosed.contains(chd.getChild(1)); - } else { - return false; - } + return checkChild(subgroup, enclosed, chd); }); - + + if(ind == -1) ind = 0; + ITree newContentTree = new Tree<>(contentTree.getHead()); ITree subgroupContents = new Tree<>(contents); - + /* - * Split content tree into an untouched tree, and the subgroup. + * Split content tree into an untouched tree, + * and the subgroup. */ for(int j = 0; j < contentTree.getChildrenCount(); j++) { ITree child = contentTree.getChild(j); - + if(j < ind) { newContentTree.addChild(child); } else { subgroupContents.addChild(child); } } - + /* * Construct the subgroup. */ ITree subgroupTree = new Tree<>(subgroup); subgroupTree.addChild(subgroupContents); subgroupTree.addChild(new Tree<>(tok)); - + /* * Add the subgroup to the group. */ newContentTree.addChild(subgroupTree); - + /* * Add the group contents. */ @@ -560,6 +561,19 @@ public class SequenceDelimiter { return res; } + private boolean checkChild(T subgroup, Set enclosed, ITree chd) { + System.out.println("Checking child '" + chd.getHead() + "' for subgroups."); + + if(chd.getHead().equals(subgroup)) { + System.out.println("Checking if '" + chd.getChild(1) + "' is a subordinate group."); + boolean contains = enclosed.contains(chd.getChild(1)); + System.out.println("It " + (contains ? "was" : "wasn't")); + return contains; + } else { + return false; + } + } + private boolean isForbidden(Stack> groupStack, Multiset forbiddenDelimiters, T groupName) { boolean localForbid; if(groupStack.empty()) @@ -627,4 +641,26 @@ public class SequenceDelimiter { addOpener(open, groupName); } } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + builder.append("SequenceDelimiter ["); + + if(openDelimiters != null) { + builder.append("openDelimiters="); + builder.append(openDelimiters); + builder.append(", "); + } + + if(groups != null) { + builder.append("groups="); + builder.append(groups); + } + + builder.append("]"); + + return builder.toString(); + } } -- cgit v1.2.3