summaryrefslogtreecommitdiff
path: root/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java
diff options
context:
space:
mode:
Diffstat (limited to 'RGens/src/main/java/bjc/rgens/newparser/CaseElement.java')
-rw-r--r--RGens/src/main/java/bjc/rgens/newparser/CaseElement.java139
1 files changed, 81 insertions, 58 deletions
diff --git a/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java b/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java
index 5f798aa..8a5454b 100644
--- a/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java
+++ b/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java
@@ -4,13 +4,13 @@ import static bjc.rgens.newparser.CaseElement.ElementType.*;
/**
* A element in a rule case.
- *
+ *
* @author EVE
*/
public class CaseElement {
/**
* The possible types of an element.
- *
+ *
* @author EVE
*
*/
@@ -38,9 +38,9 @@ public class CaseElement {
EXPVARDEF;
}
- private static final String SPECIAL_CASELEM = "\\{[^}]+\\}";
- private static final String REFER_CASELEM = "\\[[^\\]]+\\]";
- private static final String RANGE_CASELM = "\\[\\d+\\.\\.\\d+\\]";
+ private static final String SPECIAL_CASELEM = "\\{[^}]+\\}";
+ private static final String REFER_CASELEM = "\\[[^\\]]+\\]";
+ private static final String RANGE_CASELM = "\\[\\d+\\.\\.\\d+\\]";
/**
* The type of this element.
@@ -49,10 +49,10 @@ public class CaseElement {
/**
* The literal string value of this element.
- *
+ *
* This means that it is a string whose value should always mean the
* same thing.
- *
+ *
* <h2>Used For</h2>
* <dl>
* <dt>LITERAL</dt>
@@ -65,7 +65,7 @@ public class CaseElement {
/**
* The starting integer value of this element.
- *
+ *
* <h2>Used For</h2>
* <dl>
* <dt>RANGE</dt>
@@ -76,7 +76,7 @@ public class CaseElement {
/**
* The starting integer value of this element.
- *
+ *
* <h2>Used For</h2>
* <dl>
* <dt>RANGE</dt>
@@ -87,7 +87,7 @@ public class CaseElement {
/**
* The name of the variable this element defines.
- *
+ *
* <h2>Used For</h2>
* <dl>
* <dt>VARDEF</dt>
@@ -100,7 +100,7 @@ public class CaseElement {
/**
* The definition of the variable this element defines.
- *
+ *
* <h2>Used For</h2>
* <dl>
* <dt>VARDEF</dt>
@@ -113,23 +113,26 @@ public class CaseElement {
/**
* Create a new case element.
- *
+ *
* @param typ
* The type of this element.
- *
+ *
* @throws IllegalArgumentException
* If the specified type needs parameters.
*/
public CaseElement(ElementType typ) {
- switch(typ) {
+ switch (typ) {
case LITERAL:
case RULEREF:
throw new IllegalArgumentException("This type requires a string parameter");
+
case RANGE:
throw new IllegalArgumentException("This type requires two integer parameters");
+
case VARDEF:
case EXPVARDEF:
throw new IllegalArgumentException("This type requires two string parameters");
+
default:
break;
}
@@ -139,27 +142,30 @@ public class CaseElement {
/**
* Create a new case element that has a single string value.
- *
+ *
* @param typ
* The type of this element.
- *
+ *
* @param val
* The string value of this element.
- *
+ *
* @throws IllegalArgumentException
* If the specified type doesn't take a single string
* parameter.
*/
public CaseElement(ElementType typ, String val) {
- switch(typ) {
+ switch (typ) {
case LITERAL:
case RULEREF:
break;
+
case RANGE:
throw new IllegalArgumentException("This type requires two integer parameters");
+
case VARDEF:
case EXPVARDEF:
throw new IllegalArgumentException("This type requires two string parameters");
+
default:
throw new IllegalArgumentException("This type doesn't have a string parameter");
}
@@ -171,28 +177,31 @@ public class CaseElement {
/**
* Create a new case element that has two integer values.
- *
+ *
* @param typ
* The type of this element.
* @param first
* The first integer value for this element.
* @param second
* The second integer value for this element.
- *
+ *
* @throws IllegalArgumentException
* If the specified type doesn't take two integer
* parameters.
*/
public CaseElement(ElementType typ, int first, int second) {
- switch(typ) {
+ switch (typ) {
case LITERAL:
case RULEREF:
throw new IllegalArgumentException("This type requires a string parameter");
+
case RANGE:
break;
+
case VARDEF:
case EXPVARDEF:
throw new IllegalArgumentException("This type requires two string parameters");
+
default:
throw new IllegalArgumentException("This type doesn't have two integer parameters");
}
@@ -205,28 +214,31 @@ public class CaseElement {
/**
* Create a new case element that has two string values.
- *
+ *
* @param typ
* The type of this element.
* @param name
* The first string value for this element.
* @param def
* The second string value for this element.
- *
+ *
* @throws IllegalArgumentException
* If the specified type doesn't take two string
* parameters.
*/
public CaseElement(ElementType typ, String name, String def) {
- switch(typ) {
+ switch (typ) {
case LITERAL:
case RULEREF:
throw new IllegalArgumentException("This type requires a string parameter");
+
case RANGE:
throw new IllegalArgumentException("This type requires two integer parameters");
+
case VARDEF:
case EXPVARDEF:
break;
+
default:
throw new IllegalArgumentException("This type doesn't have two string parameters");
}
@@ -239,19 +251,21 @@ public class CaseElement {
/**
* Get the literal string value for this element.
- *
+ *
* @return The literal string value for this element.
- *
+ *
* @throws IllegalStateException
* If this type doesn't have a literal string value.
*/
public String getLiteral() {
- switch(type) {
+ switch (type) {
case LITERAL:
case RULEREF:
break;
+
default:
- throw new IllegalStateException(String.format("Type '%s' doesn't have a literal string value"));
+ throw new IllegalStateException(
+ String.format("Type '%s' doesn't have a literal string value"));
}
return literalVal;
@@ -259,39 +273,42 @@ public class CaseElement {
/**
* Get the starting integer value for this element.
- *
+ *
* @return The starting integer value for this element.
- *
+ *
* @throws IllegalStateException
* If this type doesn't have a starting integer value.
*/
public int getStart() {
- switch(type) {
+ switch (type) {
case RANGE:
break;
+
default:
throw new IllegalStateException(
- String.format("Type '%s' doesn't have a starting integer value", type));
+ String.format("Type '%s' doesn't have a starting integer value", type));
}
+
return start;
}
/**
* Get the ending integer value for this element.
- *
+ *
* @return The ending integer value for this element.
- *
+ *
* @throws IllegalStateException
* If this type doesn't have a ending integer value.
*/
public int getEnd() {
- switch(type) {
+ switch (type) {
case RANGE:
break;
+
default:
throw new IllegalStateException(
- String.format("Type '%s' doesn't have a ending integer value", type));
+ String.format("Type '%s' doesn't have a ending integer value", type));
}
return end;
@@ -299,17 +316,18 @@ public class CaseElement {
/**
* Get the variable name for this element.
- *
+ *
* @return The variable name of this element.
- *
+ *
* @throws IllegalStateException
* If the type doesn't have a variable name.
*/
public String getName() {
- switch(type) {
+ switch (type) {
case VARDEF:
case EXPVARDEF:
break;
+
default:
throw new IllegalStateException(String.format("Type '%s' doesn't have a name", type));
}
@@ -319,17 +337,18 @@ public class CaseElement {
/**
* Get the variable definition for this element.
- *
+ *
* @return The variable definition of this element.
- *
+ *
* @throws IllegalStateException
* If the type doesn't have a variable definition.
*/
public String getDefn() {
- switch(type) {
+ switch (type) {
case VARDEF:
case EXPVARDEF:
break;
+
default:
throw new IllegalStateException(String.format("Type '%s' doesn't have a name", type));
}
@@ -339,16 +358,20 @@ public class CaseElement {
@Override
public String toString() {
- switch(type) {
+ switch (type) {
case LITERAL:
case RULEREF:
return literalVal;
+
case RANGE:
return String.format("[%d..%d]", start, end);
+
case VARDEF:
return String.format("{%s:=%s}", varName, varDef);
+
case EXPVARDEF:
return String.format("{%s=%s}", varName, varDef);
+
default:
return String.format("Unknown type '%s'", type);
}
@@ -356,36 +379,36 @@ public class CaseElement {
/**
* Create a case element from a string.
- *
+ *
* @param csepart
* The string to convert.
- *
+ *
* @return A case element representing the string.
*/
public static CaseElement createElement(String csepart) {
- if(csepart == null) {
+ if (csepart == null) {
throw new NullPointerException("Case part cannot be null");
}
- if(csepart.matches(SPECIAL_CASELEM)) {
+ if (csepart.matches(SPECIAL_CASELEM)) {
/*
* Handle special cases.
*/
String specialBody = csepart.substring(1, csepart.length() - 1);
- if(specialBody.matches("\\S+:=\\S+")) {
+ if (specialBody.matches("\\S+:=\\S+")) {
/*
* Handle expanding variable definitions.
*/
String[] parts = specialBody.split(":=");
- if(parts.length != 2) {
+ if (parts.length != 2) {
throw new GrammarException("Expanded variables must be a name and a definition,"
- + " seperated by :=");
+ + " seperated by :=");
}
return new CaseElement(EXPVARDEF, parts[0], parts[1]);
- } else if(specialBody.matches("\\S+=\\S+")) {
+ } else if (specialBody.matches("\\S+=\\S+")) {
/*
* Handle regular variable definitions.
*/
@@ -394,23 +417,23 @@ public class CaseElement {
*/
String[] parts = specialBody.split("=");
- if(parts.length != 2) {
+ if (parts.length != 2) {
throw new GrammarException("Variables must be a name and a definition,"
- + " seperated by =");
+ + " seperated by =");
}
return new CaseElement(VARDEF, parts[0], parts[1]);
- } else if(specialBody.matches("{empty}")) {
+ } else if (specialBody.matches("{empty}")) {
/*
* Literal blank, for empty cases.
*/
return new CaseElement(LITERAL, "");
} else {
throw new IllegalArgumentException(
- String.format("Unknown special case part '%s'", specialBody));
+ String.format("Unknown special case part '%s'", specialBody));
}
- } else if(csepart.matches(REFER_CASELEM)) {
- if(csepart.matches(RANGE_CASELM)) {
+ } else if (csepart.matches(REFER_CASELEM)) {
+ if (csepart.matches(RANGE_CASELM)) {
/*
* Handle ranges
*/
@@ -421,7 +444,7 @@ public class CaseElement {
return new CaseElement(RANGE, firstNum, secondNum);
}
-
+
return new CaseElement(RULEREF, csepart);
} else {
return new CaseElement(LITERAL, csepart);