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 --- .../java/bjc/rgens/newparser/RGrammarBuilder.java | 38 ++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 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 7549847..346e588 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarBuilder.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarBuilder.java @@ -4,7 +4,9 @@ import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.IList; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import static bjc.rgens.newparser.CaseElement.ElementType.*; import static bjc.rgens.newparser.RuleCase.CaseType.*; @@ -16,11 +18,13 @@ import static bjc.rgens.newparser.RuleCase.CaseType.*; * */ public class RGrammarBuilder { - private Map rules; + private IList currentCase; private Rule currRule; - private IList currentCase; + private Map rules; + + private Set exportedRules; private String initialRule; @@ -30,6 +34,8 @@ public class RGrammarBuilder { public RGrammarBuilder() { rules = new HashMap<>(); + exportedRules = new HashSet<>(); + currentCase = new FunctionalList<>(); } @@ -65,6 +71,8 @@ public class RGrammarBuilder { grammar.setInitialRule(initialRule); + grammar.setExportedRules(exportedRules); + return grammar; } @@ -139,6 +147,10 @@ public class RGrammarBuilder { * * @param init * The initial rule of the grammar. + * + * @throws IllegalArgumentException + * If the rule is either not valid or not defined in the + * grammar. */ public void setInitialRule(String init) { if(init == null) { @@ -151,4 +163,26 @@ public class RGrammarBuilder { initialRule = init; } + + /** + * Add an exported rule to this grammar. + * + * @param export + * The name of the rule to export. + * + * @throws IllegalArgumentException + * If the rule is either not valid or not defined in the + * grammar. + */ + public void addExport(String export) { + if(export == null) { + 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("No rule named '%s' found", export)); + } + + exportedRules.add(export); + } } -- cgit v1.2.3