summaryrefslogtreecommitdiff
path: root/RGens/src/main
diff options
context:
space:
mode:
authorstudent <student@192.168.1.186>2017-03-22 11:49:58 -0400
committerstudent <student@192.168.1.186>2017-03-22 11:49:58 -0400
commit66a92de7920535cf76f3b1d9b9cd1984820cc75f (patch)
treea33d333b9c58dfa07d1be06c2da3ba058c3434a5 /RGens/src/main
parent9c1c8bba103d35eb9abeeba6b3ce8633ec10f9e7 (diff)
Work on implementing variables
Diffstat (limited to 'RGens/src/main')
-rw-r--r--RGens/src/main/java/bjc/rgens/newparser/CaseElement.java224
-rw-r--r--RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java1
2 files changed, 177 insertions, 48 deletions
diff --git a/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java b/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java
index 7d04156..29cf6f7 100644
--- a/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java
+++ b/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java
@@ -28,12 +28,21 @@ public class CaseElement {
/**
* An element that represents a random range.
*/
- RANGE;
+ RANGE,
+ /**
+ * An element that represents a variable that stores a string.
+ */
+ VARDEF,
+ /**
+ * An element that represents a variable that stores the result of
+ * generating a rule.
+ */
+ 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.
@@ -43,8 +52,8 @@ 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.
+ * This means that it is a string whose value should always mean the same
+ * thing.
*
* <h2>Used For</h2>
* <dl>
@@ -76,27 +85,57 @@ public class CaseElement {
* <dd>The inclusive end of the range</dd>
* </dl>
*/
- private int snd;
+ private int end;
+
+ /**
+ * The name of the variable this element defines.
+ *
+ * <h2>Used For</h2>
+ * <dl>
+ * <dt>VARDEF</dt>
+ * <dd>The name of the variable</dd>
+ * <dt>EXPVARDEF</dt>
+ * <dd>The name of the variable</dd>
+ * </dl>
+ */
+ private String varName;
+
+ /**
+ * The definition of the variable this element defines.
+ *
+ * <h2>Used For</h2>
+ * <dl>
+ * <dt>VARDEF</dt>
+ * <dd>The value of the variable</dd>
+ * <dt>EXPVARDEF</dt>
+ * <dd>The rule to expand for the value of this variable</dd>
+ * </dl>
+ */
+ private String varDef;
/**
* Create a new case element.
*
* @param typ
- * The type of this element.
+ * The type of this element.
*
* @throws IllegalArgumentException
- * If the specified type needs parameters.
+ * 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.");
+ throw new IllegalArgumentException("This type requires a string parameter");
case RANGE:
- throw new IllegalArgumentException("This type requires two int parameters.");
+ throw new IllegalArgumentException("This type requires two integer parameters");
+ case VARDEF:
+ case EXPVARDEF:
+ throw new IllegalArgumentException("This type requires two string parameters");
default:
break;
}
+
type = typ;
}
@@ -104,24 +143,26 @@ public class CaseElement {
* Create a new case element that has a single string value.
*
* @param typ
- * The type of this element.
+ * The type of this element.
*
* @param val
- * The string value of this element.
+ * The string value of this element.
*
* @throws IllegalArgumentException
- * If the specified type doesn't take a single string
- * parameter.
+ * 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 int parameters.");
+ 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.");
+ throw new IllegalArgumentException("This type doesn't have a string parameter");
}
type = typ;
@@ -133,31 +174,66 @@ public class CaseElement {
* Create a new case element that has two integer values.
*
* @param typ
- * The type of this element.
+ * The type of this element.
* @param first
- * The first integer value for this element.
+ * The first integer value for this element.
* @param second
- * The second integer value for this element.
+ * The second integer value for this element.
*
* @throws IllegalArgumentException
- * If the specified type doesn't take two integer
- * parameters.
+ * 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.");
+ 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 int parameters");
+ throw new IllegalArgumentException("This type doesn't have two integer parameters");
}
type = typ;
this.start = first;
- this.snd = second;
+ this.end = second;
+ }
+
+ /**
+ * 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) {
+ 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");
+ }
+
+ type = typ;
+
+ this.varName = name;
+ this.varDef = def;
}
/**
@@ -166,10 +242,10 @@ public class CaseElement {
* @return The literal string value for this element.
*
* @throws IllegalStateException
- * If this type doesn't have a literal string value.
+ * If this type doesn't have a literal string value.
*/
public String getLiteral() {
- switch(type) {
+ switch (type) {
case LITERAL:
case RULEREF:
break;
@@ -186,15 +262,14 @@ public class CaseElement {
* @return The starting integer value for this element.
*
* @throws IllegalStateException
- * If this type doesn't have a starting integer value.
+ * 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));
+ throw new IllegalStateException(String.format("Type '%s' doesn't have a starting integer value", type));
}
return start;
@@ -206,29 +281,71 @@ public class CaseElement {
* @return The ending integer value for this element.
*
* @throws IllegalStateException
- * If this type doesn't have a ending integer value.
+ * 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));
+ throw new IllegalStateException(String.format("Type '%s' doesn't have a ending integer value", type));
+ }
+
+ return end;
+ }
+
+ /**
+ * 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) {
+ case VARDEF:
+ case EXPVARDEF:
+ break;
+ default:
+ throw new IllegalStateException(String.format("Type '%s' doesn't have a name", type));
+ }
+ return varName;
+ }
+
+ /**
+ * 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) {
+ case VARDEF:
+ case EXPVARDEF:
+ break;
+ default:
+ throw new IllegalStateException(String.format("Type '%s' doesn't have a name", type));
}
- return snd;
+ return varDef;
}
@Override
public String toString() {
- switch(type) {
+ switch (type) {
case LITERAL:
case RULEREF:
return literalVal;
case RANGE:
- return String.format("[%d..%d]", start, snd);
+ 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);
}
@@ -238,21 +355,34 @@ public class CaseElement {
* Create a case element from a string.
*
* @param csepart
- * The string to convert.
+ * 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(CaseElement.SPECIAL_CASELEM)) {
+ if (csepart.matches(SPECIAL_CASELEM)) {
/*
- * Handle other cases.
+ * Handle special cases.
*/
- } else if(csepart.matches(CaseElement.REFER_CASELEM)) {
- if(csepart.matches(CaseElement.RANGE_CASELM)) {
+ String specialBody = csepart.substring(1, csepart.length() - 1);
+
+ if (specialBody.matches("\\S+:=\\S+")) {
+ /*
+ * Handle expanding variable definitions.
+ */
+ } else if (specialBody.matches("\\S+=\\S+")) {
+ /*
+ * Handle regular variable definitions.
+ */
+ } else {
+ throw new IllegalArgumentException(String.format("Unknown special case part '%s'", specialBody));
+ }
+ } else if (csepart.matches(REFER_CASELEM)) {
+ if (csepart.matches(RANGE_CASELM)) {
/*
* Handle ranges
*/
diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java
index 82c52ee..dac397e 100644
--- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java
+++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java
@@ -294,7 +294,6 @@ public class RGrammarParser {
/*
* Handle a where block (a block with local rules).
*/
- @SuppressWarnings("unused")
private void handleWhereBlock(String block, RGrammarBuilder build, int level) throws GrammarException {
try(BlockReader whereReader = new BlockReader("", new StringReader(block))) {
try {