From 12fd9362751871711fad12fea01f39a38c2f32e8 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Sun, 23 Jul 2017 20:10:44 -0300 Subject: Simplify parser. The parser no longer uses a modal builder, so grammar blocks could now be done in a parallel manner --- .../java/bjc/rgens/newparser/RGrammarBuilder.java | 99 +++------------------- 1 file changed, 12 insertions(+), 87 deletions(-) (limited to 'RGens/src/main/java/bjc/rgens/newparser/RGrammarBuilder.java') diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarBuilder.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarBuilder.java index b4f9f45..bd8ba84 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarBuilder.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarBuilder.java @@ -17,10 +17,6 @@ import static bjc.rgens.newparser.RuleCase.CaseType.*; * */ public class RGrammarBuilder { - private IList currentCase; - - private Rule currRule; - private Map rules; private Set exportedRules; @@ -34,30 +30,23 @@ public class RGrammarBuilder { rules = new HashMap<>(); exportedRules = new HashSet<>(); - - currentCase = new FunctionalList<>(); } - /** - * Starts a rule with the provided name. - * - * If the rule already exists, it will be opened for adding additional - * cases instead. - * - * @param rName - * The name of the rule currently being built. - * - * @throws IllegalArgumentException - * If the rule name is the empty string. - */ - public void startRule(String rName) { - if (rName == null) { + public Rule getOrCreateRule(String rName) { + if(rName == null) throw new NullPointerException("Rule name must not be null"); - } else if (rName.equals("")) { + else if(rName.equals("")) throw new IllegalArgumentException("The empty string is not a valid rule name"); - } - currRule = new Rule(rName); + if(rules.containsKey(rName)) + return rules.get(rName); + else { + Rule ret = new Rule(rName); + + rules.put(rName, ret); + + return ret; + } } /** @@ -75,70 +64,6 @@ public class RGrammarBuilder { return grammar; } - /** - * Adds a case part to this rule. - * - *

Case part formatting

- *
- *
Rule Reference
- *
Rule references are marked by being surrounded with square - * brackets (the square brackets are part of the rule's name)
- *
Literal Strings
- *
Literal strings are the default case part type.
- *
- * - * @param csepart - */ - public void addCasePart(String csepart) { - CaseElement element = CaseElement.createElement(csepart); - - currentCase.add(element); - } - - /** - * Finalizes editing a rule. - * - * Saves the rule to the rule map. - * - * @throws IllegalStateException - * Must be invoked while a rule is being edited. - */ - public void finishRule() { - if (currRule == null) { - throw new IllegalStateException("Must start a rule before finishing one"); - } - - rules.put(currRule.name, currRule); - } - - /** - * Finishes the current case being edited. - * - * @throws IllegalStateException - * Must be invoked while a rule is being edited. - */ - public void finishCase() { - if (currRule == null) { - throw new IllegalStateException("Must start a rule before finishing a case"); - } - - currRule.addCase(new RuleCase(NORMAL, currentCase)); - - currentCase = new FunctionalList<>(); - } - - /** - * Begins a case for the current rule. - * - * @throws IllegalStateException - * Must be invoked while a rule is being edited. - */ - public void beginCase() { - if (currRule == null) { - throw new IllegalStateException("Must start a rule before adding cases"); - } - } - /** * Set the initial rule of the grammar. * -- cgit v1.2.3