diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-09-17 16:49:44 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-09-17 16:49:44 -0300 |
| commit | a77a3f9094f4c11cfa9ba3463814eb61d880486f (patch) | |
| tree | e25fa762e15c8d6fda736245df4e419d15cda8d2 /clformat/src/main/java/bjc/utils | |
| parent | 3d6076ead0d2b9b3543482469502a84a675160dd (diff) | |
General updates to FORMAT
Diffstat (limited to 'clformat/src/main/java/bjc/utils')
13 files changed, 198 insertions, 47 deletions
diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/CLFormatter.java b/clformat/src/main/java/bjc/utils/ioutils/format/CLFormatter.java index f614d9a..2a8f443 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/CLFormatter.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/CLFormatter.java @@ -173,6 +173,21 @@ public class CLFormatter { * The format string to use. * @param params * The parameters for the string. + */ + public void formatString(ReportWriter target, String format, Object... params) throws IOException { + /* Put the parameters where we can easily handle them. */ + Tape<Object> tParams = new SingleTape<>(params); + + doFormatString(format, target, tParams, true); + } + + /** + * Format a string in the style of CL's FORMAT. + * + * @param format + * The format string to use. + * @param params + * The parameters for the string. * @return The formatted string. */ public void formatString(Writer target, String format, Iterable<Object> params) throws IOException { 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 bde7a7d..29494b2 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/CLParameters.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/CLParameters.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import bjc.utils.esodata.Tape; +import bjc.utils.parserutils.TokenUtils; /** * Represents a set of parameters to a CL format directive. @@ -49,14 +50,23 @@ public class CLParameters { StringBuilder currParm = new StringBuilder(); char prevChar = ' '; + boolean inStr = false; for (int i = 0; i < unsplit.length(); i++) { char c = unsplit.charAt(i); - if (c == ',' && prevChar != '\'') { + if (c == ',' && prevChar != '\'' && !inStr) { lParams.add(currParm.toString()); currParm = new StringBuilder(); + } else if (c == '"' && prevChar != '\'') { + inStr = true; + + currParm.append(c); + } else if (inStr && c == '"' && prevChar != '\'') { + inStr = false; + + currParm.append(c); } else { currParm.append(c); } @@ -92,6 +102,8 @@ public class CLParameters { char ch = ((Character) par); parameters.add(Character.toString(ch)); + } else if (par instanceof String) { + parameters.add((String)par); } else { throw new IllegalArgumentException( "Incorrect type of parameter for V inline parameter"); @@ -100,6 +112,10 @@ public class CLParameters { parameters.add(Integer.toString(dirParams.size() - dirParams.position())); } else if (param.equals("%")) { parameters.add(Integer.toString(dirParams.position())); + } else if (param.startsWith("\"")) { + String dquote = param.substring(1, param.length() - 1); + + parameters.add(TokenUtils.descapeString(dquote)); } else { parameters.add(param); } @@ -122,7 +138,87 @@ public class CLParameters { * @return The value of the parameter if it exists, or the default * otherwise. */ - public char getCharDefault(int idx, String paramName, char directive, char def) { + public boolean getBooleanDefault(int idx, String paramName, String directive, boolean def) { + if(!params[idx].equals("")) { + return getBoolean(idx, paramName, directive); + } + + 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 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); + } + + 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]; + } + + /** + * 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); } @@ -141,22 +237,27 @@ public class CLParameters { * The directive this parameter belongs to. * @return The value for the parameter. */ - public char getChar(int idx, String paramName, char directive) { + 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 issue + // Punt in the case we have a slightly malformed + // character return param.charAt(0); } if(!param.startsWith("'")) { throw new IllegalArgumentException( - String.format("Invalid %s \"%s\" to %c directive", paramName, param, directive)); + String.format("Invalid %s \"%s\" to %s directive", paramName, param, directive)); } return param.charAt(1); } + // @TODO + // + // Add getString and getStringDefault + /** * Get an optional integer parameter with a default value. * @@ -171,7 +272,7 @@ public class CLParameters { * @return The value of the parameter if it exists, or the default * otherwise. */ - public int getIntDefault(int idx, String paramName, char directive, int def) { + public int getIntDefault(int idx, String paramName, String directive, int def) { if(!params[idx].equals("")) { return getInt(idx, paramName, directive); } @@ -190,13 +291,13 @@ public class CLParameters { * The directive this parameter belongs to. * @return The value for the parameter. */ - public int getInt(int idx, String paramName, char directive) { + public int getInt(int idx, String paramName, String directive) { String param = params[idx]; try { return Integer.parseInt(param); } catch(NumberFormatException nfex) { - String msg = String.format("Invalid %s \"%s\" to %c directive", paramName, param, directive); + String msg = String.format("Invalid %s \"%s\" to %s directive", paramName, param, directive); IllegalArgumentException iaex = new IllegalArgumentException(msg); iaex.initCause(nfex); 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 87a6d6b..428e488 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 @@ -23,13 +23,13 @@ public class AestheticDirective implements Directive { if (dirParams.arrParams.length() == 0) { // Zero parameters, use all defaults } else if (dirParams.arrParams.length() == 1) { - mincol = dirParams.arrParams.getIntDefault(0, "minimum column count", 'A', 0); + mincol = dirParams.arrParams.getIntDefault(0, "minimum column count", "A", 0); } else if (dirParams.arrParams.length() < 4) { throw new IllegalArgumentException("Must provide either zero, one or four arguments to A directive"); } else { - colinc = dirParams.arrParams.getIntDefault(1, "padding increment", 'A', 1); - minpad = dirParams.arrParams.getIntDefault(2, "minimum amount of padding", 'A', 0); - padchar = dirParams.arrParams.getCharDefault(3, "padding character", 'A', ' '); + colinc = dirParams.arrParams.getIntDefault(1, "padding increment", "A", 1); + minpad = dirParams.arrParams.getIntDefault(2, "minimum amount of padding", "A", 0); + padchar = dirParams.arrParams.getCharDefault(3, "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 c4bde66..61e2b70 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,7 @@ public class ConditionalDirective implements Directive { } else { int res; if (dirParams.arrParams.length() >= 1) { - res = dirParams.arrParams.getInt(0, "conditional choice", '['); + res = dirParams.arrParams.getInt(0, "conditional choice", "["); } 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/EscapeDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/EscapeDirective.java index f0aa6ec..f983a5a 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 @@ -20,21 +20,21 @@ public class EscapeDirective implements Directive { shouldExit = dirParams.tParams.atEnd(); break; case 1: - int num = dirParams.arrParams.getInt(0, "condition count", '^'); + int num = dirParams.arrParams.getInt(0, "condition count", "^"); shouldExit = num == 0; break; case 2: - int left = dirParams.arrParams.getInt(0, "left-hand condition", '^'); - int right = dirParams.arrParams.getInt(1, "right-hand condition", '^'); + int left = dirParams.arrParams.getInt(0, "left-hand condition", "^"); + int right = dirParams.arrParams.getInt(1, "right-hand condition", "^"); shouldExit = left == right; break; case 3: default: - int low = dirParams.arrParams.getInt(0, "lower-bound condition", '^'); - int mid = dirParams.arrParams.getInt(1, "interval condition", '^'); - int high = dirParams.arrParams.getInt(2, "upper-bound condition", '^'); + int low = dirParams.arrParams.getInt(0, "lower-bound condition", "^"); + int mid = dirParams.arrParams.getInt(1, "interval condition", "^"); + int high = dirParams.arrParams.getInt(2, "upper-bound condition", "^"); shouldExit = (low <= mid) && (mid <= high); break; 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 afd4c5a..dc9836b 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,7 @@ public class FreshlineDirective implements Directive { int nTimes = 1; if(dirParams.arrParams.length() >= 1) { - nTimes = dirParams.arrParams.getInt(0, "occurance count", '&'); + nTimes = dirParams.arrParams.getInt(0, "occurance count", "&"); } 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 3eb741e..b7a0f15 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 @@ -23,10 +23,10 @@ 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); + mincol = params.getIntDefault(argidx + 1, "minimum column count", "R", 0); } if (params.length() >= (argidx + 3)) { - padchar = params.getCharDefault(argidx + 2, "padding character", 'R', ' '); + padchar = params.getCharDefault(argidx + 2, "padding character", "R", ' '); } String res; @@ -39,10 +39,10 @@ public abstract class GeneralNumberDirective implements Directive { int commaInterval = 0; char commaChar = ','; if (params.length() >= (argidx + 4)) { - commaChar = params.getCharDefault((argidx + 3), "comma character", 'R', ','); + commaChar = params.getCharDefault((argidx + 3), "comma character", "R", ','); } if (params.length() >= (argidx + 5)) { - commaInterval = params.getIntDefault((argidx + 4), "comma interval", 'R', 0); + commaInterval = params.getIntDefault((argidx + 4), "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 9a8d416..7fe4557 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 @@ -13,14 +13,14 @@ public class GotoDirective implements Directive { if (dirParams.mods.colonMod) { int num = 1; if (dirParams.arrParams.length() >= 1) { - num = dirParams.arrParams.getIntDefault(0, "number of arguments backward", '*', 1); + num = dirParams.arrParams.getIntDefault(0, "number of arguments backward", "*", 1); } dirParams.tParams.left(num); } else if (dirParams.mods.atMod) { int num = 0; if (dirParams.arrParams.length() >= 1) { - num = dirParams.arrParams.getIntDefault(0, "argument index", '*', 0); + num = dirParams.arrParams.getIntDefault(0, "argument index", "*", 0); } dirParams.tParams.first(); @@ -28,7 +28,7 @@ public class GotoDirective implements Directive { } else { int num = 1; if (dirParams.arrParams.length() >= 1) { - num = dirParams.arrParams.getIntDefault(0, "number of arguments forward", '*', 1); + num = dirParams.arrParams.getIntDefault(0, "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 new file mode 100644 index 0000000..3a2f3fb --- /dev/null +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/IndentDirective.java @@ -0,0 +1,42 @@ +package bjc.utils.ioutils.format.directives; + +import java.io.IOException; + +/** + * Implement the I directive. + * @author student + * + */ +public class IndentDirective implements Directive { + + @Override + public void format(FormatParameters dirParams) throws IOException { + // Dollar mod is indent configuration + if (dirParams.mods.dollarMod) { + + return; + } + + int nIndents = 1; + + if(dirParams.arrParams.length() >= 1) { + nIndents = dirParams.arrParams.getInt(0, "indent count", "I"); + } + + boolean dedent = false; + if (nIndents < 0) { + nIndents = -nIndents; + + dedent = true; + } + + if (dirParams.mods.colonMod) { + if (dedent) dirParams.rw.dedent(nIndents); + else dirParams.rw.indent(nIndents); + } else { + if (dedent) throw new IllegalArgumentException("Cannot have negative indent level"); + + dirParams.rw.setLevel(nIndents); + } + } +} 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 304f991..fb77a34 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 @@ -63,7 +63,7 @@ public class IterationDirective implements Directive { int maxItr = Integer.MAX_VALUE; if (dirParams.arrParams.length() > 0) { - maxItr = dirParams.arrParams.getInt(0, "maximum iterations", '{'); + maxItr = dirParams.arrParams.getInt(0, "maximum iterations", "{"); } 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 9ad647f..3a67f0c 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 @@ -10,7 +10,7 @@ import java.io.IOException; */ public class LiteralDirective implements Directive { - private char directive; + private String directive; private String lit; /** @@ -22,7 +22,7 @@ public class LiteralDirective implements Directive { * The character for the directive. */ public LiteralDirective(String lit, char directive) { - this.directive = directive; + this.directive = Character.toString(directive); this.lit = lit; } 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 da41276..7de8fb0 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 @@ -39,7 +39,7 @@ public class RadixDirective extends GeneralNumberDirective { if (dirParams.arrParams.length() < 1) throw new IllegalArgumentException("R directive requires at least one parameter, the radix"); - int radix = dirParams.arrParams.getInt(0, "radix", 'R'); + int radix = dirParams.arrParams.getInt(0, "radix", "R"); handleNumberDirective(dirParams.rw, dirParams.mods, dirParams.arrParams, 0, val, radix); } 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 1fd95ff..2a42158 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 @@ -6,35 +6,30 @@ import bjc.utils.ioutils.format.*; public class TabulateDirective implements Directive { public void format(FormatParameters dirParams) throws IOException { - // Unsupported feature. - // - // I can't really make out what this is supposed to do from the - // documentation, but I suspect that it depends on font glyph - // size, not character positions + // Support for a possible future feature + char padchar = ' '; + + int currCol = dirParams.rw.getLinePos(); if (dirParams.mods.colonMod) { - throw new UnsupportedOperationException("Colon mod is not supported for T directive"); + currCol = dirParams.rw.getIndentPos(); } - // Support for a possible future feature - char padchar = ' '; if (dirParams.mods.atMod) { int colrel = 1, colinc = 1; if (dirParams.arrParams.length() > 2) { - colinc = dirParams.arrParams.getIntDefault(1, "column increment", 'T', 1); + colinc = dirParams.arrParams.getIntDefault(1, "column increment", "T", 1); } if (dirParams.arrParams.length() > 1) { - colrel = dirParams.arrParams.getIntDefault(0, "relative column number", 'T', 1); + colrel = dirParams.arrParams.getIntDefault(0, "relative column number", "T", 1); } for (int i = 0; i < colrel; i++) { dirParams.rw.write(padchar); } - int currCol = dirParams.rw.getLinePos(); - int nSpaces = 0; while ((currCol + nSpaces) % colinc != 0) nSpaces++; @@ -46,15 +41,13 @@ public class TabulateDirective implements Directive { int colnum = 1, colinc = 1; if (dirParams.arrParams.length() > 2) { - colinc = dirParams.arrParams.getIntDefault(1, "column increment", 'T', 1); + colinc = dirParams.arrParams.getIntDefault(1, "column increment", "T", 1); } if (dirParams.arrParams.length() > 1) { - colnum = dirParams.arrParams.getIntDefault(0, "column number", 'T', 1); + colnum = dirParams.arrParams.getIntDefault(0, "column number", "T", 1); } - int currCol = dirParams.rw.getLinePos(); - if (currCol < colnum) { for (int i = currCol; i < colnum; i++) { dirParams.rw.write(padchar); |
