summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/rgens/parser/RGrammarBuilder.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2019-07-21 15:52:00 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2019-07-21 15:52:00 -0400
commit6b810efa8765047a669be0e9ff6501ddda5c49a9 (patch)
tree0e65344afe3a58efdc662a22867bc8ab2948f3e6 /src/main/java/bjc/rgens/parser/RGrammarBuilder.java
parentdd4bdab11c715fa2dcc438f287806b50f8467fd3 (diff)
parenta4d1507727e9aeeea0982ebbda785a961902d46c (diff)
Merge cleanups
Diffstat (limited to 'src/main/java/bjc/rgens/parser/RGrammarBuilder.java')
-rwxr-xr-xsrc/main/java/bjc/rgens/parser/RGrammarBuilder.java232
1 files changed, 180 insertions, 52 deletions
diff --git a/src/main/java/bjc/rgens/parser/RGrammarBuilder.java b/src/main/java/bjc/rgens/parser/RGrammarBuilder.java
index 70b738b..7296801 100755
--- a/src/main/java/bjc/rgens/parser/RGrammarBuilder.java
+++ b/src/main/java/bjc/rgens/parser/RGrammarBuilder.java
@@ -12,9 +12,13 @@ import java.util.Set;
import bjc.rgens.parser.elements.CaseElement;
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;
+
import bjc.utils.funcutils.ListUtils;
import bjc.utils.funcutils.SetUtils;
@@ -57,10 +61,19 @@ public class RGrammarBuilder {
* The rule by that name, or a new one if none existed.
*/
public Rule getOrCreateRule(String rName) {
- if(rName == null)
- throw new NullPointerException("Rule name must not be null");
- else if(rName.equals(""))
- throw new IllegalArgumentException("The empty string is not a valid rule name");
+ return getOrCreateRule(rName, new Tree<>());
+ }
+
+ public Rule getOrCreateRule(String rName, ITree<String> errs) {
+ if(rName == null) {
+ errs.addChild("ERROR: Rule name must not be null");
+
+ return null;
+ } else if(rName.equals("")) {
+ errs.addChild("ERROR: The empty string is not a valid rule name");
+
+ return null;
+ }
if(rules.containsKey(rName)) {
return rules.get(rName);
@@ -114,10 +127,17 @@ public class RGrammarBuilder {
* If the rule is either not valid or not defined in the grammar.
*/
public void setInitialRule(String init) {
+ }
+
+ public void setInitialRule(String init, ITree<String> errs) {
if (init == null) {
- throw new NullPointerException("init must not be null");
+ errs.addChild("init must not be null");
+
+ return;
} else if (init.equals("")) {
- throw new IllegalArgumentException("The empty string is not a valid rule name");
+ errs.addChild("The empty string is not a valid rule name");
+
+ return;
}
initialRule = init;
@@ -133,10 +153,18 @@ public class RGrammarBuilder {
* If the rule is either not valid or not defined in the grammar.
*/
public void addExport(String export) {
+ addExport(export, new Tree<>());
+ }
+
+ public void addExport(String export, ITree<String> errs) {
if (export == null) {
- throw new NullPointerException("Export name must not be null");
+ errs.addChild("ERROR: Export name must not be null");
+
+ return;
} else if (export.equals("")) {
- throw new NullPointerException("The empty string is not a valid rule name");
+ errs.addChild("ERROR: The empty string is not a valid rule name");
+
+ return;
}
exportedRules.add(export);
@@ -155,8 +183,8 @@ public class RGrammarBuilder {
* If the rule name is either invalid or not defined by this
* grammar, or if the suffix is invalid.
*/
- public void suffixWith(String ruleName, IList<CaseElement> suffixes) {
- affixWith(ruleName, suffixes, AffixType.SUFFIX);
+ public void suffixWith(String ruleName, List<CaseElement> suffixes) {
+ affixWith(ruleName, suffixes, AffixType.SUFFIX, new Tree<>());
}
@@ -173,8 +201,8 @@ public class RGrammarBuilder {
* If the rule name is either invalid or not defined by this
* grammar, or if the prefix is invalid.
*/
- public void prefixWith(String ruleName, IList<CaseElement> prefixes) {
- affixWith(ruleName, prefixes, AffixType.PREFIX);
+ public void prefixWith(String ruleName, List<CaseElement> prefixes) {
+ affixWith(ruleName, prefixes, AffixType.PREFIX, new Tree<>());
}
/**
@@ -190,8 +218,8 @@ public class RGrammarBuilder {
* If the rule name is either invalid or not defined by this
* grammar, or if the prefix/suffix is invalid.
*/
- public void circumfixWith(String ruleName, IList<CaseElement> prefixes) {
- affixWith(ruleName, prefixes, AffixType.CIRCUMFIX);
+ public void circumfixWith(String ruleName, List<CaseElement> prefixes) {
+ affixWith(ruleName, prefixes, AffixType.CIRCUMFIX, new Tree<>());
}
public static enum AffixType {
@@ -208,32 +236,39 @@ public class RGrammarBuilder {
}
}
- public void affixWith(String ruleName, IList<CaseElement> affixes, AffixType type) {
+ public void affixWith(String ruleName, List<CaseElement> affixes, AffixType type) {
+ affixWith(ruleName, affixes, type, new Tree<>());
+ }
+
+ public void affixWith(String ruleName, List<CaseElement> affixes, AffixType type, ITree<String> errs) {
if (ruleName == null) {
- throw new NullPointerException("Rule name must not be null");
+ errs.addChild("ERROR: Rule name must not be null");
+
+ return;
} else if (ruleName.equals("")) {
- throw new IllegalArgumentException("The empty string is not a valid rule name");
+ errs.addChild("ERROR: The empty string is not a valid rule name");
+
+ return;
} else if(!rules.containsKey(ruleName)) {
- String msg = String.format("Rule '%s' is not a valid rule name");
+ String msg = String.format("ERROR: Rule '%s' is not a valid rule name");
- throw new IllegalArgumentException(msg);
- }
+ errs.addChild(msg);
- Set<CaseElement> elements = new HashSet<>(affixes.getSize());
- for(CaseElement affix : affixes) {
- elements.add(affix);
+ return;
}
+
+ Set<CaseElement> elements = new HashSet<>(affixes);
List<List<CaseElement>> affixLists = powerList(elements);
FunctionalList<IPair<Integer, RuleCase>> newCases = new FunctionalList<>();
-
IList<IPair<Integer, RuleCase>> caseList = rules.get(ruleName).getCases();
+
for (IPair<Integer, RuleCase> ruleCase : caseList) {
RuleCase cas = ruleCase.getRight();
for(List<CaseElement> affixList : affixLists) {
- FunctionalList<CaseElement> newCase = new FunctionalList<>();
+ List<CaseElement> newCase = new ArrayList<>();
if(type.isPrefix()) {
for(CaseElement element : affixList) {
@@ -263,12 +298,24 @@ public class RGrammarBuilder {
}
public void despaceRule(String ruleName) {
+ despaceRule(ruleName, new Tree<>(), false);
+ }
+
+ public void despaceRule(String ruleName, ITree<String> errs, boolean doTrace) {
if (ruleName == null) {
- throw new NullPointerException("ruleName must not be null");
+ errs.addChild("ERROR: Rule name must not be null");
+
+ return;
} else if (ruleName.equals("")) {
- throw new IllegalArgumentException("The empty string is not a valid rule name");
+ errs.addChild("ERROR: The empty string is not a valid rule name");
+
+ return;
} else if (!rules.containsKey(ruleName)) {
- throw new IllegalArgumentException(String.format("The rule '%s' doesn't exist", ruleName));
+ String msg = String.format("ERROR: The rule '%s' doesn't exist", ruleName);
+
+ errs.addChild(msg);
+
+ return;
}
IList<IPair<Integer, RuleCase>> caseList = rules.get(ruleName).getCases();
@@ -279,42 +326,82 @@ public class RGrammarBuilder {
newCaseList.add(new Pair<>(cse.getLeft(), new FlatRuleCase(cse.getRight().elementList)));
}
- trace("Despacing %d cases of rule %s", caseList.getSize(), ruleName);
+ if (doTrace) {
+ String msg = String.format("TRACE: Despacing %d cases of rule %s", caseList.getSize(), ruleName);
+
+ errs.addChild(msg);
+ }
rules.get(ruleName).replaceCases(newCaseList);
}
public void setWeight(String ruleName) {
+ setWeight(ruleName, new Tree<>());
+ }
+
+ public void setWeight(String ruleName, ITree<String> errs) {
if (ruleName == null) {
- throw new NullPointerException("ruleName must not be null");
+ errs.addChild("ERROR: Rule name must not be null");
+
+ return;
} else if (ruleName.equals("")) {
- throw new IllegalArgumentException("The empty string is not a valid rule name");
+ errs.addChild("ERROR: The empty string is not a valid rule name");
+
+ return;
} else if (!rules.containsKey(ruleName)) {
- throw new IllegalArgumentException(String.format("The rule '%s' doesn't exist", ruleName));
+ String msg = String.format("ERROR: The rule '%s' doesn't exist", ruleName);
+
+ errs.addChild(msg);
+
+ return;
}
rules.get(ruleName).prob = Rule.ProbType.NORMAL;
}
public void setRuleRecur(String ruleName, int recurLimit) {
+ setRuleRecur(ruleName, recurLimit, new Tree<>());
+ }
+
+ public void setRuleRecur(String ruleName, int recurLimit, ITree<String> errs) {
if (ruleName == null) {
- throw new NullPointerException("ruleName must not be null");
+ errs.addChild("ERROR: Rule name must not be null");
+
+ return;
} else if (ruleName.equals("")) {
- throw new IllegalArgumentException("The empty string is not a valid rule name");
+ errs.addChild("ERROR: The empty string is not a valid rule name");
+
+ return;
} else if (!rules.containsKey(ruleName)) {
- throw new IllegalArgumentException(String.format("The rule '%s' doesn't exist", ruleName));
+ String msg = String.format("ERROR: The rule '%s' doesn't exist", ruleName);
+
+ errs.addChild(msg);
+
+ return;
}
rules.get(ruleName).recurLimit = recurLimit;
}
public void setDescent(String ruleName, int descentFactor) {
+ setDescent(ruleName, descentFactor, new Tree<>());
+ }
+
+ public void setDescent(String ruleName, int descentFactor, ITree<String> errs) {
if (ruleName == null) {
- throw new NullPointerException("ruleName must not be null");
+ errs.addChild("ERROR: Rule name must not be null");
+
+ return;
} else if (ruleName.equals("")) {
- throw new IllegalArgumentException("The empty string is not a valid rule name");
+ errs.addChild("ERROR: The empty string is not a valid rule name");
+
+ return;
} else if (!rules.containsKey(ruleName)) {
- throw new IllegalArgumentException(String.format("The rule '%s' doesn't exist", ruleName));
+ String msg = String.format("ERROR: The rule '%s' doesn't exist", ruleName);
+
+ errs.addChild(msg);
+
+ return;
}
Rule rl = rules.get(ruleName);
@@ -324,12 +411,23 @@ public class RGrammarBuilder {
}
public void setBinomial(String ruleName, int target, int bound, int trials) {
+ setBinomial(ruleName, target, bound, trials, new Tree<>());
+ }
+
+ public void setBinomial(String ruleName, int target, int bound, int trials, ITree<String> errs) {
if (ruleName == null) {
- throw new NullPointerException("ruleName must not be null");
+ errs.addChild("ERROR: Rule name must not be null");
+
+ return;
} else if (ruleName.equals("")) {
- throw new IllegalArgumentException("The empty string is not a valid rule name");
+ errs.addChild("ERROR: The empty string is not a valid rule name");
+
+ return;
} else if (!rules.containsKey(ruleName)) {
- throw new IllegalArgumentException(String.format("The rule '%s' doesn't exist", ruleName));
+ String msg = String.format("ERROR: The rule '%s' doesn't exist", ruleName);
+ errs.addChild(msg);
+
+ return;
}
Rule rl = rules.get(ruleName);
@@ -350,37 +448,67 @@ public class RGrammarBuilder {
}
public void rejectRule(String rule, String reject) {
+ rejectRule(rule, reject, new Tree<>());
+ }
+
+ public void rejectRule(String rule, String reject, ITree<String> errs) {
if (rule == null) {
- throw new NullPointerException("rule must not be null");
+ errs.addChild("ERROR: Rule must not be null");
+
+ return;
} else if(reject == null) {
- throw new NullPointerException("reject must not be null");
+ errs.addChild("ERROR: Reject must not be null");
+
+ return;
} else if (rule.equals("")) {
- throw new IllegalArgumentException("The empty string is not a valid rule name");
+ errs.addChild("ERROR: The empty string is not a valid rule name");
+
+ return;
} else if(!rules.containsKey(rule)) {
- throw new IllegalArgumentException(String.format("The rule '%s' doesn't exist", rule));
+ String msg = String.format("ERROR: The rule '%s' doesn't exist", rule);
+
+ errs.addChild(msg);
+
+ return;
}
Rule rl = rules.get(rule);
- rl.addRejection(reject);
+ rl.addRejection(reject, errs);
}
public void findReplaceRule(String rule, String find, String replace) {
+ findReplaceRule(rule, find, replace, new Tree<>());
+ }
+
+ public void findReplaceRule(String rule, String find, String replace, ITree<String> errs) {
if (rule == null) {
- throw new NullPointerException("rule must not be null");
+ errs.addChild("ERROR: Rule must not be null");
+
+ return;
} else if(find == null) {
- throw new NullPointerException("find must not be null");
+ errs.addChild("ERROR: Find must not be null");
+
+ return;
} else if(replace == null) {
- throw new NullPointerException("replace must not be null");
+ errs.addChild("ERROR: Replace must not be null");
+
+ return;
} else if (rule.equals("")) {
- throw new IllegalArgumentException("The empty string is not a valid rule name");
+ errs.addChild("ERROR: The empty string is not a valid rule name");
+
+ return;
} else if(!rules.containsKey(rule)) {
- throw new IllegalArgumentException(String.format("The rule '%s' doesn't exist", rule));
+ String msg = String.format("ERROR: The rule '%s' doesn't exist", rule);
+
+ errs.addChild(msg);
+
+ return;
}
Rule rl = rules.get(rule);
- rl.addFindReplace(find, replace);
+ rl.addFindReplace(find, replace, errs);
}
/*