diff options
13 files changed, 160 insertions, 197 deletions
diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/CLParameters.java b/clformat/src/main/java/bjc/utils/ioutils/format/CLParameters.java index 427bc4e..4699059 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/CLParameters.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/CLParameters.java @@ -14,6 +14,11 @@ import bjc.utils.parserutils.TokenUtils; * @author Benjamin Culkin */ public class CLParameters { + private static String MSG_FMT = "Invalid %s (%s) \"%s\" to %s directive"; + + private static String RX_TRUE = "(?i)y(?:es)?|t(?:rue)?(?i)"; + private static String RX_FALSE = "(?i)no?|f(?:alse)?(?i)"; + private String[] params; private Map<String, String> namedParams; @@ -44,14 +49,20 @@ public class CLParameters { for (int i = 0; i < opts.length; i++) { String opt = opts[i]; - if (!opt.equals("")) nameIndices.put(opt, i); + if (!opt.equals("")) mapIndex(opt, i); } } public void mapIndex(String opt, int idx) { + if (params.length <= idx) System.err.printf("WARN: Mapping invalid index %d (max %d) to \"%s\"\n", idx, params.length, opt); nameIndices.put(opt, idx); } + public String getRaw(int idx) { + if (idx < 0 || idx >= params.length) return "Out of Bounds"; + + return params[idx]; + } /** * Get the length of the parameter list. * @@ -64,7 +75,8 @@ public class CLParameters { /** * Creates a set of parameters from an array of parameters. * - * Mostly, this just fills in V and # parameters. + * This handles things like quoted strings, named parameters and the + * other special parameter features. * * @param params * The parameters of the directive. @@ -101,10 +113,13 @@ public class CLParameters { prevChar = c; } + + // Add last parameter lParams.add(currParm.toString()); List<String> parameters = new ArrayList<>(); + // Special case empty blocks, so that we don't confuse people if (lParams.size() == 1 && lParams.get(0).equals("")) return new CLParameters(parameters.toArray(new String[0])); @@ -114,6 +129,7 @@ public class CLParameters { int currParamNo = 0; for(String param : lParams) { if (param.startsWith("#") && !param.equals("#")) { + // Named parameter boolean setIndex = false; int nameIdx = 0; @@ -121,6 +137,8 @@ public class CLParameters { char ch = param.charAt(i); if (ch == ':' || ch == ';') { + // Semicolon says to add as + // indexed parameter if (ch == ';') setIndex = true; nameIdx = i; @@ -148,6 +166,7 @@ public class CLParameters { private static String parseParam(String param, Tape<Object> dirParams) { if(param.equalsIgnoreCase("V")) { + // Read parameter from items Object par = dirParams.item(); boolean succ = dirParams.right(); @@ -185,186 +204,78 @@ public class CLParameters { return param; } - /** - * Get an optional character parameter with a default value. - * - * @param idx - * The index the parameter is at. - * @param paramName - * The name of the parameter. - * @param directive - * The directive this parameter belongs to. - * @param def - * The default value for the parameter. - * @return The value of the parameter if it exists, or the default - * otherwise. - */ - public boolean getBooleanDefault(int idx, String paramName, String directive, boolean def) { - if(!params[idx].equals("")) { - return getBoolean(idx, paramName, directive); - } + private String resolveKey(String key) { + if (nameIndices.containsKey(key)) return params[nameIndices.get(key)]; + else if (namedParams.containsKey(key)) return namedParams.get(key); - return def; + return ""; } - /** - * Get a mandatory character parameter. - * - * @param idx - * The index the parameter is at. - * @param paramName - * The name of the parameter. - * @param directive - * The directive this parameter belongs to. - * @return The value for the parameter. - */ - public boolean getBoolean(int idx, String paramName, String directive) { - String bol = params[idx]; - - if (bol.matches("[Yy](?:es)?|[Tt](?:rue)?")) return true; - else if (bol.matches("[Nn]o?|[Ff](?:alse)?")) return false; - else { - String msg = String.format("Invalid %s \"%s\" to %s directive", paramName, bol, directive); - throw new IllegalArgumentException(msg); - } - } - /** - * Get an optional character parameter with a default value. - * - * @param idx - * The index the parameter is at. - * @param paramName - * The name of the parameter. - * @param directive - * The directive this parameter belongs to. - * @param def - * The default value for the parameter. - * @return The value of the parameter if it exists, or the default - * otherwise. - */ - public String getStringDefault(int idx, String paramName, String directive, String def) { - if(!params[idx].equals("")) { - return getString(idx, paramName, directive); + public boolean getBoolean(String key, String paramName, String directive, boolean def) { + String bol = resolveKey(key); + + if(!bol.equals("")) { + if (bol.matches(RX_TRUE)) return true; + else if (bol.matches(RX_FALSE)) return false; + else { + String msg = String.format(MSG_FMT, paramName, key, bol, directive); + throw new IllegalArgumentException(msg); + } } return def; } - /** - * Get a mandatory character parameter. - * - * @param idx - * The index the parameter is at. - * @param paramName - * The name of the parameter. - * @param directive - * The directive this parameter belongs to. - * @return The value for the parameter. - */ - public String getString(int idx, String paramName, String directive) { - return params[idx]; - } + public String getString(String key, String paramName, String directive, String def) { + String vl = resolveKey(key); - /** - * Get an optional character parameter with a default value. - * - * @param idx - * The index the parameter is at. - * @param paramName - * The name of the parameter. - * @param directive - * The directive this parameter belongs to. - * @param def - * The default value for the parameter. - * @return The value of the parameter if it exists, or the default - * otherwise. - */ - public char getCharDefault(int idx, String paramName, String directive, char def) { - if(!params[idx].equals("")) { - return getChar(idx, paramName, directive); - } + // @NOTE 9/19/17 + // + // This raises the question of what to do if the empty string is a valid + // value for a parameter + if (!vl.equals("")) return vl; return def; } - /** - * Get a mandatory character parameter. - * - * @param idx - * The index the parameter is at. - * @param paramName - * The name of the parameter. - * @param directive - * The directive this parameter belongs to. - * @return The value for the parameter. - */ - public char getChar(int idx, String paramName, String directive) { - String param = params[idx]; - - if (param.length() == 1) { - // Punt in the case we have a slightly malformed - // character - return param.charAt(0); - } + public char getChar(String key, String paramName, String directive, char def) { + String param = resolveKey(key); - if(!param.startsWith("'")) { - throw new IllegalArgumentException( - String.format("Invalid %s \"%s\" to %s directive", paramName, param, directive)); - } - - return param.charAt(1); - } + if (!param.equals("")) { + if (param.length() == 1) { + // Punt in the case we have a slightly malformed + // character + return param.charAt(0); + } - // @TODO - // - // Add getString and getStringDefault + if(!param.startsWith("'")) { + throw new IllegalArgumentException( + String.format(MSG_FMT, paramName, key, param, directive)); + } - /** - * Get an optional integer parameter with a default value. - * - * @param idx - * The index the parameter is at. - * @param paramName - * The name of the parameter. - * @param directive - * The directive this parameter belongs to. - * @param def - * The default value for the parameter. - * @return The value of the parameter if it exists, or the default - * otherwise. - */ - public int getIntDefault(int idx, String paramName, String directive, int def) { - if(!params[idx].equals("")) { - return getInt(idx, paramName, directive); + return param.charAt(1); } return def; } - /** - * Get a mandatory integer parameter. - * - * @param idx - * The index the parameter is at. - * @param paramName - * The name of the parameter. - * @param directive - * The directive this parameter belongs to. - * @return The value for the parameter. - */ - public int getInt(int idx, String paramName, String directive) { - String param = params[idx]; + public int getInt(String key, String paramName, String directive, int def) { + String param = resolveKey(key); - try { - return Integer.parseInt(param); - } catch(NumberFormatException nfex) { - String msg = String.format("Invalid %s \"%s\" to %s directive", paramName, param, directive); + if (!param.equals("")) { + try { + return Integer.parseInt(param); + } catch(NumberFormatException nfex) { + String msg = String.format(MSG_FMT, paramName, key, param, directive); - IllegalArgumentException iaex = new IllegalArgumentException(msg); - iaex.initCause(nfex); + IllegalArgumentException iaex = new IllegalArgumentException(msg); + iaex.initCause(nfex); - throw iaex; + throw iaex; + } } + + return def; } @Override diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java index 2f57b9f..1767acd 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java @@ -24,13 +24,17 @@ public class AestheticDirective implements Directive { if (params.length() == 0) { // Zero parameters, use all defaults } else if (params.length() == 1) { - mincol = params.getIntDefault(0, "minimum column count", "A", 0); + params.mapIndices("mincol"); + + mincol = params.getInt("mincol", "minimum column count", "A", 0); } else if (params.length() < 4) { throw new IllegalArgumentException("Must provide either zero, one or four arguments to A directive"); } else { - colinc = params.getIntDefault(1, "padding increment", "A", 1); - minpad = params.getIntDefault(2, "minimum amount of padding", "A", 0); - padchar = params.getCharDefault(3, "padding character", "A", ' '); + params.mapIndices("colinc", "minpad", "padchar"); + + colinc = params.getInt( "colinc", "padding increment", "A", 1); + minpad = params.getInt( "minpad", "minimum amount of padding", "A", 0); + padchar = params.getChar("padchar", "padding character", "A", ' '); } StringBuilder work = new StringBuilder(); diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/ConditionalDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/ConditionalDirective.java index 61e2b70..8b952ec 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/ConditionalDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/ConditionalDirective.java @@ -141,7 +141,9 @@ public class ConditionalDirective implements Directive { } else { int res; if (dirParams.arrParams.length() >= 1) { - res = dirParams.arrParams.getInt(0, "conditional choice", "["); + dirParams.arrParams.mapIndices("choice"); + + res = dirParams.arrParams.getInt("choice", "conditional choice", "[", 0); } else { if (dirParams.item == null) { throw new IllegalArgumentException("No parameter provided for [ directive."); diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/Directive.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/Directive.java index 243bd43..ab66752 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/Directive.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/Directive.java @@ -43,4 +43,11 @@ public interface Directive { return false; } } + + // @TODO 9/19/18 Ben Culkin :ParseContained + // + // Implement something for parsing contained bodies, abstracting the + // stuff that Iteration/Conditional/CaseDirective do. + // + // The main issue is thinking of a good interface to it. } diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/EscapeDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/EscapeDirective.java index cb1fa29..233367f 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/EscapeDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/EscapeDirective.java @@ -21,26 +21,33 @@ public class EscapeDirective implements Directive { case 0: shouldExit = dirParams.tParams.atEnd(); break; - case 1: - int num = params.getInt(0, "condition count", "^"); + case 1: { + params.mapIndices("count"); + int num = params.getInt("count", "condition count", "^", 0); shouldExit = num == 0; break; - case 2: - int left = params.getInt(0, "left-hand condition", "^"); - int right = params.getInt(1, "right-hand condition", "^"); + } + case 2: { + params.mapIndices("lhand", "rhand"); + int left = params.getInt("lhand", "left-hand condition", "^", 0); + int right = params.getInt("rhand", "right-hand condition", "^", 0); shouldExit = left == right; break; + } case 3: - default: - int low = params.getInt(0, "lower-bound condition", "^"); - int mid = params.getInt(1, "interval condition", "^"); - int high = params.getInt(2, "upper-bound condition", "^"); + default: { + params.mapIndices("lower", "ival", "upper"); + + int low = params.getInt("lower", "lower-bound condition", "^", 0); + int mid = params.getInt("ival", "interval condition", "^", 0); + int high = params.getInt("upper", "upper-bound condition", "^", 0); shouldExit = (low <= mid) && (mid <= high); break; } + } if (dirParams.mods.dollarMod) dirParams.tParams.left(); diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/FreshlineDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/FreshlineDirective.java index dc9836b..710c3f5 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/FreshlineDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/FreshlineDirective.java @@ -14,7 +14,8 @@ public class FreshlineDirective implements Directive { int nTimes = 1; if(dirParams.arrParams.length() >= 1) { - nTimes = dirParams.arrParams.getInt(0, "occurance count", "&"); + dirParams.arrParams.mapIndices("count"); + nTimes = dirParams.arrParams.getInt("count", "occurance count", "&", 1); } if(dirParams.rw.isLastCharNL()) nTimes -= 1; diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/GeneralNumberDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/GeneralNumberDirective.java index b7a0f15..ee86a33 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/GeneralNumberDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/GeneralNumberDirective.java @@ -16,6 +16,7 @@ import java.io.IOException; public abstract class GeneralNumberDirective implements Directive { protected static void handleNumberDirective(ReportWriter rw, CLModifiers mods, CLParameters params, int argidx, long val, int radix) throws IOException { + /* * Initialize the two padding related parameters, and then fill them in from the * directive parameters if they are present. @@ -23,10 +24,13 @@ public abstract class GeneralNumberDirective implements Directive { int mincol = 0; char padchar = ' '; if (params.length() >= (argidx + 2)) { - mincol = params.getIntDefault(argidx + 1, "minimum column count", "R", 0); + params.mapIndex("mincol", argidx + 1); + mincol = params.getInt("mincol", "minimum column count", "R", 0); } + if (params.length() >= (argidx + 3)) { - padchar = params.getCharDefault(argidx + 2, "padding character", "R", ' '); + params.mapIndex("padchar", argidx + 2); + padchar = params.getChar("padchar", "padding character", "R", ' '); } String res; @@ -38,11 +42,16 @@ public abstract class GeneralNumberDirective implements Directive { */ int commaInterval = 0; char commaChar = ','; + + // System.err.printf("Comma params (idx %d, len %d): char \"%s\", int \"%s\"\n", argidx, params.length(), params.getRaw(argidx + 3), params.getRaw(argidx + 4)); + if (params.length() >= (argidx + 4)) { - commaChar = params.getCharDefault((argidx + 3), "comma character", "R", ','); + params.mapIndex("cchar", argidx + 3); + commaChar = params.getChar("cchar", "comma character", "R", ','); } if (params.length() >= (argidx + 5)) { - commaInterval = params.getIntDefault((argidx + 4), "comma interval", "R", 0); + params.mapIndex("cinterval", argidx + 4); + commaInterval = params.getInt("cinterval", "comma interval", "R", 0); } res = NumberUtils.toCommaString(val, mincol, padchar, commaInterval, commaChar, mods.atMod, radix); diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/GotoDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/GotoDirective.java index 7f140b5..5c457b0 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/GotoDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/GotoDirective.java @@ -12,26 +12,32 @@ public class GotoDirective implements Directive { @Override public void format(FormatParameters dirParams) { - CLParameters clParameters = dirParams.arrParams; + CLParameters params = dirParams.arrParams; if (dirParams.mods.colonMod) { int num = 1; - if (clParameters.length() >= 1) { - num = clParameters.getIntDefault(0, "number of arguments backward", "*", 1); + if (params.length() >= 1) { + params.mapIndices("numargs"); + + num = params.getInt("numargs", "number of arguments backward", "*", 1); } dirParams.tParams.left(num); } else if (dirParams.mods.atMod) { int num = 0; - if (clParameters.length() >= 1) { - num = clParameters.getIntDefault(0, "argument index", "*", 0); + if (params.length() >= 1) { + params.mapIndices("argidx"); + + num = params.getInt("argidx", "argument index", "*", 0); } dirParams.tParams.first(); dirParams.tParams.right(num); } else { int num = 1; - if (clParameters.length() >= 1) { - num = clParameters.getIntDefault(0, "number of arguments forward", "*", 1); + if (params.length() >= 1) { + params.mapIndices("numargs"); + + num = params.getInt("numargs", "number of arguments forward", "*", 1); } dirParams.tParams.right(num); diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/IndentDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/IndentDirective.java index 3a2f3fb..21e5535 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/IndentDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/IndentDirective.java @@ -20,7 +20,9 @@ public class IndentDirective implements Directive { int nIndents = 1; if(dirParams.arrParams.length() >= 1) { - nIndents = dirParams.arrParams.getInt(0, "indent count", "I"); + dirParams.arrParams.mapIndices("count"); + + nIndents = dirParams.arrParams.getInt("count", "indent count", "I", 1); } boolean dedent = false; diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/IterationDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/IterationDirective.java index fcdb263..c5e0fa0 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/IterationDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/IterationDirective.java @@ -62,9 +62,11 @@ public class IterationDirective implements Directive { int maxItr = Integer.MAX_VALUE; - CLParameters clParameters = dirParams.arrParams; - if (clParameters.length() > 0) { - maxItr = clParameters.getInt(0, "maximum iterations", "{"); + CLParameters params = dirParams.arrParams; + if (params.length() > 0) { + params.mapIndices("maxitr"); + + maxItr = params.getInt("maxitr", "maximum iterations", "{", Integer.MAX_VALUE); } int numItr = 0; diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/LiteralDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/LiteralDirective.java index 3a67f0c..1e71777 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/LiteralDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/LiteralDirective.java @@ -31,7 +31,9 @@ public class LiteralDirective implements Directive { int nTimes = 1; if (dirParams.arrParams.length() >= 1) { - nTimes = dirParams.arrParams.getInt(0, "occurance count", directive); + dirParams.arrParams.mapIndices("count"); + + nTimes = dirParams.arrParams.getInt("count", "occurance count", directive, 1); } for (int i = 0; i < nTimes; i++) { diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/RadixDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/RadixDirective.java index d6197da..b75154e 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/RadixDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/RadixDirective.java @@ -28,8 +28,8 @@ public class RadixDirective extends GeneralNumberDirective { */ long val = ((Number) dirParams.item).longValue(); - CLParameters clParameters = dirParams.arrParams; - if (clParameters.length() == 0) { + CLParameters params = dirParams.arrParams; + if (params.length() == 0) { if (dirParams.mods.atMod) { dirParams.rw.write(NumberUtils.toRoman(val, dirParams.mods.colonMod)); } else if (dirParams.mods.colonMod) { @@ -38,12 +38,14 @@ public class RadixDirective extends GeneralNumberDirective { dirParams.rw.write(NumberUtils.toCardinal(val)); } } else { - if (clParameters.length() < 1) + if (params.length() < 1) throw new IllegalArgumentException("R directive requires at least one parameter, the radix"); - int radix = clParameters.getInt(0, "radix", "R"); + params.mapIndex("radix", 0); - handleNumberDirective(dirParams.rw, dirParams.mods, clParameters, 0, val, radix); + int radix = params.getInt("radix", "radix", "R", 10); + + handleNumberDirective(dirParams.rw, dirParams.mods, params, 0, val, radix); } dirParams.tParams.right(); diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/TabulateDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/TabulateDirective.java index 332d7e2..eb8aa4f 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/TabulateDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/TabulateDirective.java @@ -20,11 +20,15 @@ public class TabulateDirective implements Directive { int colrel = 1, colinc = 1; if (params.length() > 2) { - colinc = params.getIntDefault(1, "column increment", "T", 1); + params.mapIndex("colinc", 1); + + colinc = params.getInt("colinc", "column increment", "T", 1); } if (params.length() > 1) { - colrel = params.getIntDefault(0, "relative column number", "T", 1); + params.mapIndices("colrel"); + + colrel = params.getInt("colrel", "relative column number", "T", 1); } for (int i = 0; i < colrel; i++) { @@ -42,11 +46,15 @@ public class TabulateDirective implements Directive { int colnum = 1, colinc = 1; if (params.length() > 2) { - colinc = params.getIntDefault(1, "column increment", "T", 1); + params.mapIndex("colinc", 1); + + colinc = params.getInt("colinc", "column increment", "T", 1); } if (params.length() > 1) { - colnum = params.getIntDefault(0, "column number", "T", 1); + params.mapIndices("colnum"); + + colnum = params.getInt("colnum", "column number", "T", 1); } if (currCol < colnum) { |
