From 2cc00686b50d5a28f0ab04ad52e5b075290698d9 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Tue, 21 Mar 2017 19:42:12 -0400 Subject: Add ranges Ranges can now be used with the syntax [start..end] inline to generate numbers instead of having to create a special rule for them. --- .../main/java/bjc/rgens/newparser/CaseElement.java | 112 ++++++++++++++++++++- 1 file changed, 107 insertions(+), 5 deletions(-) (limited to 'RGens/src/main/java/bjc/rgens/newparser/CaseElement.java') diff --git a/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java b/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java index a15cb9b..2eb2e3a 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java +++ b/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java @@ -14,13 +14,17 @@ public class CaseElement { */ public static enum ElementType { /** - * A element that represents a literal string. + * An element that represents a literal string. */ LITERAL, /** - * A element that represents a rule reference. + * An element that represents a rule reference. */ - RULEREF; + RULEREF, + /** + * An element that represents a random range. + */ + RANGE; } /** @@ -44,6 +48,28 @@ public class CaseElement { */ private String literalVal; + /** + * The starting integer value of this element. + * + *

Used For

+ *
+ *
RANGE
+ *
The inclusive start of the range
+ *
+ */ + private int start; + + /** + * The starting integer value of this element. + * + *

Used For

+ *
+ *
RANGE
+ *
The inclusive end of the range
+ *
+ */ + private int snd; + /** * Create a new case element. * @@ -53,11 +79,13 @@ public class CaseElement { * @throws IllegalArgumentException * If the specified type needs parameters. */ - public CaseElement(CaseElement.ElementType typ) { + public CaseElement(ElementType 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 int parameters."); default: break; } @@ -77,11 +105,13 @@ public class CaseElement { * If the specified type doesn't take a single string * parameter. */ - public CaseElement(CaseElement.ElementType typ, String val) { + public CaseElement(ElementType typ, String val) { switch(typ) { case LITERAL: case RULEREF: break; + case RANGE: + throw new IllegalArgumentException("This type requires two int parameters."); default: throw new IllegalArgumentException("This type doesn't have a string parameter."); } @@ -92,6 +122,35 @@ 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) { + case LITERAL: + case RULEREF: + throw new IllegalArgumentException("This type requires a string parameter."); + case RANGE: + break; + default: + throw new IllegalArgumentException("This type doesn't have two int parameters"); + } + + type = typ; + + this.start = first; + this.snd = second; + } /** * Get the literal string value for this element. @@ -113,12 +172,55 @@ public class CaseElement { return literalVal; } + /** + * 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) { + case RANGE: + break; + default: + throw new IllegalStateException( + 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) { + case RANGE: + break; + default: + throw new IllegalStateException( + String.format("Type '%s' doesn't have a ending integer value", type)); + + } + + return snd; + } + @Override public String toString() { switch(type) { case LITERAL: case RULEREF: return literalVal; + case RANGE: + return String.format("[%d..%d]", start, snd); default: return String.format("Unknown type '%s'", type); } -- cgit v1.2.3