From cb5a0f731db04a0ed0712975dd00c1b27ee4d6f9 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Wed, 5 Sep 2018 12:15:32 -0300 Subject: Cleanup suffixWith/prefixWith in builder --- .../java/bjc/rgens/parser/RGrammarBuilder.java | 86 +++++++++------------- 1 file changed, 36 insertions(+), 50 deletions(-) (limited to 'src') diff --git a/src/main/java/bjc/rgens/parser/RGrammarBuilder.java b/src/main/java/bjc/rgens/parser/RGrammarBuilder.java index c94af9a..8939b03 100755 --- a/src/main/java/bjc/rgens/parser/RGrammarBuilder.java +++ b/src/main/java/bjc/rgens/parser/RGrammarBuilder.java @@ -145,50 +145,10 @@ public class RGrammarBuilder { * grammar, or if the suffix is invalid. */ public void suffixWith(String ruleName, IList suffixes) { - if (ruleName == null) { - throw new NullPointerException("Rule name must not be null"); - } else if (ruleName.equals("")) { - throw new IllegalArgumentException("The empty string is not a valid rule name"); - } else if(!rules.containsKey(ruleName)) { - String msg = String.format("Rule '%s' is not a valid rule name"); - - throw new IllegalArgumentException(msg); - } - - Set elements = new HashSet<>(suffixes.getSize()); - for(CaseElement suffix : suffixes) { - elements.add(suffix); - } - - List> suffixLists = powerList(elements); - - FunctionalList> newCases = new FunctionalList<>(); - - IList> caseList = rules.get(ruleName).getCases(); - for (IPair ruleCase : caseList) { - RuleCase cas = ruleCase.getRight(); - - for(List suffixList : suffixLists) { - FunctionalList newCase = new FunctionalList<>(); - - for(CaseElement elm : cas.elementList) { - newCase.add(elm); - } - - for(CaseElement element : suffixList) { - newCase.add(element); - } - - newCases.add(new Pair<>(ruleCase.getLeft(), cas.withElements(newCase))); - } - } - - - for (IPair newCase : newCases) { - rules.get(ruleName).addCase(newCase.getRight(), newCase.getLeft()); - } + affixWith(ruleName, suffixes, AffixType.SUFFIX); } + /** * Prefix a given case element to every case of a specific rule. * @@ -203,6 +163,23 @@ public class RGrammarBuilder { * grammar, or if the prefix is invalid. */ public void prefixWith(String ruleName, IList prefixes) { + affixWith(ruleName, prefixes, AffixType.PREFIX); + } + + private static enum AffixType { + SUFFIX, + PREFIX; + + public boolean isSuffix() { + return this == SUFFIX; + } + + public boolean isPrefix() { + return this == PREFIX; + } + } + + private void affixWith(String ruleName, IList affixes, AffixType type) { if (ruleName == null) { throw new NullPointerException("Rule name must not be null"); } else if (ruleName.equals("")) { @@ -213,12 +190,12 @@ public class RGrammarBuilder { throw new IllegalArgumentException(msg); } - Set elements = new HashSet<>(prefixes.getSize()); - for(CaseElement prefix : prefixes) { - elements.add(prefix); + Set elements = new HashSet<>(affixes.getSize()); + for(CaseElement affix : affixes) { + elements.add(affix); } - List> prefixLists = powerList(elements); + List> affixLists = powerList(elements); FunctionalList> newCases = new FunctionalList<>(); @@ -226,16 +203,25 @@ public class RGrammarBuilder { for (IPair ruleCase : caseList) { RuleCase cas = ruleCase.getRight(); - for(List prefixList : prefixLists) { + for(List affixList : affixLists) { FunctionalList newCase = new FunctionalList<>(); - for(CaseElement elm: prefixList) { - newCase.add(elm); + if(type.isPrefix()) { + for(CaseElement element : affixList) { + newCase.add(element); + } } - for(CaseElement elm :cas.elementList) { + for(CaseElement elm : cas.elementList) { newCase.add(elm); } + + if(type.isSuffix()) { + for(CaseElement element : affixList) { + newCase.add(element); + } + } + newCases.add(new Pair<>(ruleCase.getLeft(), cas.withElements(newCase))); } -- cgit v1.2.3