summaryrefslogtreecommitdiff
path: root/RGens/src/main/java/bjc/rgens/newparser/Rule.java
diff options
context:
space:
mode:
authorbjculkin <bjculkin@mix.wvu.edu>2017-03-21 19:29:27 -0400
committerbjculkin <bjculkin@mix.wvu.edu>2017-03-21 19:38:42 -0400
commit5444cd4db8a0fa41d25cd303c1145cadd112e12f (patch)
tree779f205becc1e1cded6ed1c307f295a2404ce22d /RGens/src/main/java/bjc/rgens/newparser/Rule.java
parentccb2510fadf19e5e1cda63d948fd482e25fc799d (diff)
Add formatter
Adds a formatter capable of taking in a parsed grammar and printing it out in a formatted form, capable of being reparsed.
Diffstat (limited to 'RGens/src/main/java/bjc/rgens/newparser/Rule.java')
-rw-r--r--RGens/src/main/java/bjc/rgens/newparser/Rule.java57
1 files changed, 57 insertions, 0 deletions
diff --git a/RGens/src/main/java/bjc/rgens/newparser/Rule.java b/RGens/src/main/java/bjc/rgens/newparser/Rule.java
index c479c87..a65f1ce 100644
--- a/RGens/src/main/java/bjc/rgens/newparser/Rule.java
+++ b/RGens/src/main/java/bjc/rgens/newparser/Rule.java
@@ -22,8 +22,17 @@ public class Rule {
*
* @param ruleName
* The name of the grammar rule.
+ *
+ * @throws IllegalArgumentException
+ * If the rule name is invalid.
*/
public Rule(String ruleName) {
+ 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");
+ }
+
this.ruleName = ruleName;
ruleCases = new FunctionalList<>();
@@ -36,6 +45,10 @@ public class Rule {
* The case to add.
*/
public void addCase(RuleCase cse) {
+ if(cse == null) {
+ throw new NullPointerException("Case must not be null");
+ }
+
ruleCases.add(cse);
}
@@ -47,4 +60,48 @@ public class Rule {
public RuleCase getCase() {
return ruleCases.randItem();
}
+
+ /**
+ * Get all the cases of this rule.
+ *
+ * @return All the cases in this rule.
+ */
+ public IList<RuleCase> getCases() {
+ return ruleCases;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+
+ int result = 1;
+ result = prime * result + ((ruleCases == null) ? 0 : ruleCases.hashCode());
+ result = prime * result + ((ruleName == null) ? 0 : ruleName.hashCode());
+
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if(this == obj) return true;
+ if(obj == null) return false;
+ if(!(obj instanceof Rule)) return false;
+
+ Rule other = (Rule) obj;
+
+ if(ruleCases == null) {
+ if(other.ruleCases != null) return false;
+ } else if(!ruleCases.equals(other.ruleCases)) return false;
+
+ if(ruleName == null) {
+ if(other.ruleName != null) return false;
+ } else if(!ruleName.equals(other.ruleName)) return false;
+
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("Rule [ruleName='%s', ruleCases=%s]", ruleName, ruleCases);
+ }
} \ No newline at end of file