From faf3e39fee32226ee72c6d43b2ba8a0f4e4bd837 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Tue, 29 May 2018 18:02:46 -0300 Subject: Refactor case element generation Case elements are now responsible for generating themselves. --- src/main/java/bjc/rgens/parser/RGrammarBuilder.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/main/java/bjc/rgens/parser/RGrammarBuilder.java') diff --git a/src/main/java/bjc/rgens/parser/RGrammarBuilder.java b/src/main/java/bjc/rgens/parser/RGrammarBuilder.java index 284d1a2..f34a418 100755 --- a/src/main/java/bjc/rgens/parser/RGrammarBuilder.java +++ b/src/main/java/bjc/rgens/parser/RGrammarBuilder.java @@ -66,8 +66,20 @@ public class RGrammarBuilder { public RGrammar toRGrammar() { RGrammar grammar = new RGrammar(rules); + if(initialRule != null) { + if(!rules.containsKey(initialRule)) { + throw new GrammarException(String.format("Rule '%s' doesn't exist\n", initialRule)); + } + } + grammar.setInitialRule(initialRule); + for(String export : exportedRules) { + if(!rules.containsKey(export)) { + throw new GrammarException(String.format("Rule '%s' doesn't exist\n", export)); + } + } + grammar.setExportedRules(exportedRules); return grammar; @@ -87,8 +99,6 @@ public class RGrammarBuilder { throw new NullPointerException("init must not be null"); } else if (init.equals("")) { throw new IllegalArgumentException("The empty string is not a valid rule name"); - } else if (!rules.containsKey(init)) { - throw new IllegalArgumentException(String.format("The rule '%s' doesn't exist", init)); } initialRule = init; @@ -108,8 +118,6 @@ public class RGrammarBuilder { throw new NullPointerException("Export name must not be null"); } else if (export.equals("")) { throw new NullPointerException("The empty string is not a valid rule name"); - } else if(!rules.containsKey(export)) { - throw new IllegalArgumentException(String.format("The rule '%s' doesn't exist", rules)); } exportedRules.add(export); -- cgit v1.2.3