From ccb2510fadf19e5e1cda63d948fd482e25fc799d Mon Sep 17 00:00:00 2001 From: bjculkin Date: Tue, 21 Mar 2017 15:42:22 -0400 Subject: Add export rules to grammars --- .../main/java/bjc/rgens/newparser/RGrammar.java | 61 +++++++++++++++++++--- 1 file changed, 54 insertions(+), 7 deletions(-) (limited to 'RGens/src/main/java/bjc/rgens/newparser/RGrammar.java') diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java index 4adcbe8..35fc356 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java @@ -1,7 +1,8 @@ package bjc.rgens.newparser; -import java.util.List; +import java.util.HashSet; import java.util.Map; +import java.util.Set; /** * Represents a randomized grammar. @@ -20,7 +21,9 @@ public class RGrammar { private Map rules; - private Map importRules; + private Map importRules; + + private Set exportRules; private String initialRule; @@ -40,11 +43,11 @@ public class RGrammar { * Imported rules are checked for rule definitions after local * definitions are checked. * - * @param importedRules + * @param exportedRules * The set of imported rules to use. */ - public void setImportedRules(Map importedRules) { - importRules = importedRules; + public void setImportedRules(Map exportedRules) { + importRules = exportedRules; } /** @@ -116,9 +119,9 @@ public class RGrammar { generateCase(cse, state); } else if(importRules.containsKey(elm.getLiteral())) { - RuleCase cse = importRules.get(elm.getLiteral()).getCase(); + RGrammar dst = importRules.get(elm.getLiteral()); - generateCase(cse, state); + state.contents.append(dst.generate(elm.getLiteral())); } else { throw new GrammarException( String.format("No rule by name '%s' found", elm.getLiteral())); @@ -149,6 +152,14 @@ public class RGrammar { * is no initial rule. */ public void setInitialRule(String initialRule) { + /* + * Passing null nulls our initial rule. + */ + if(initialRule == null) { + this.initialRule = null; + return; + } + if(initialRule.equals("")) { throw new GrammarException("The empty string is not a valid rule name"); } else if(!rules.containsKey(initialRule)) { @@ -158,4 +169,40 @@ public class RGrammar { this.initialRule = initialRule; } + + /** + * Gets the rules exported by this grammar. + * + * The initial rule is exported by default if specified. + * + * @return The rules exported by this grammar. + */ + public Set getExportedRules() { + Set res = new HashSet<>(); + + for(String rname : exportRules) { + if(!rules.containsKey(rname)) { + throw new GrammarException(String + .format("No rule named '%s' local to this grammar found", initialRule)); + } + + res.add(rules.get(rname)); + } + + if(initialRule != null) { + res.add(rules.get(initialRule)); + } + + return res; + } + + /** + * Set the rules exported by this grammar. + * + * @param exportRules + * The rules exported by this grammar. + */ + public void setExportedRules(Set exportRules) { + this.exportRules = exportRules; + } } -- cgit v1.2.3