diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2019-07-21 16:24:47 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2019-07-21 16:24:47 -0300 |
| commit | 8c289f05ca36c3def6a4e4ab2414b7469c03339e (patch) | |
| tree | 9e97b6ea73b94ad831258b9bede0136a43530a39 /src/main/java/bjc/rgens/parser/RGrammarBuilder.java | |
| parent | 89668d36167846e002d0f6dcdc1034b5fee44ce3 (diff) | |
Refactor front-end error-handling
This refactors the front-end to use a tree for capturing errors, instead
of throwing exceptions.
This has the benefit that you will receive notifications about all of
the error messages you have, instead of only the first.
I'm a bit fuzzy on the details, since it's been a while since I wrote
these changes.
Diffstat (limited to 'src/main/java/bjc/rgens/parser/RGrammarBuilder.java')
| -rwxr-xr-x | src/main/java/bjc/rgens/parser/RGrammarBuilder.java | 232 |
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 535d818..358c6a1 100755 --- a/src/main/java/bjc/rgens/parser/RGrammarBuilder.java +++ b/src/main/java/bjc/rgens/parser/RGrammarBuilder.java @@ -4,9 +4,13 @@ import bjc.rgens.parser.elements.CaseElement; import bjc.rgens.parser.elements.VariableDefCaseElement; 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; @@ -59,10 +63,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); @@ -116,10 +129,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; @@ -135,10 +155,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); @@ -157,8 +185,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<>()); } @@ -175,8 +203,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<>()); } /** @@ -192,8 +220,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 { @@ -210,32 +238,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) { @@ -265,12 +300,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(); @@ -281,42 +328,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); @@ -326,12 +413,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); @@ -352,37 +450,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); } /* |
