diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-09-06 18:00:12 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-09-06 18:00:12 -0300 |
| commit | cc1cbe38c25c5d4b1577a825e6f6cab332d03e89 (patch) | |
| tree | bfbf2b6257cd3196d079eef09a5c770178933d0e /base/src/main | |
| parent | 6d781f3b203397196d200f0b80a8fe358ec8f53c (diff) | |
Actually handle EscapeException
Diffstat (limited to 'base/src/main')
3 files changed, 135 insertions, 99 deletions
diff --git a/base/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java b/base/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java index cd0dbd1..4276f40 100644 --- a/base/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java +++ b/base/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java @@ -26,18 +26,18 @@ public class AestheticDirective implements Directive { int mincol = 0, colinc = 1, minpad = 0; char padchar = ' '; - if (params.length() >= 1) { + if (params.length() == 0) { + // Zero parameters, use all defaults + } else if (params.length() == 1) { mincol = params.getIntDefault(0, "minimum column count", 'A', 0); - } - - if (params.length() < 4) { + } 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', ' '); } - 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', ' '); - StringBuilder work = new StringBuilder(); if (mods.atMod) { diff --git a/base/src/main/java/bjc/utils/ioutils/format/directives/ConditionalDirective.java b/base/src/main/java/bjc/utils/ioutils/format/directives/ConditionalDirective.java index 837730f..71d9e7d 100644 --- a/base/src/main/java/bjc/utils/ioutils/format/directives/ConditionalDirective.java +++ b/base/src/main/java/bjc/utils/ioutils/format/directives/ConditionalDirective.java @@ -1,9 +1,7 @@ package bjc.utils.ioutils.format.directives; import bjc.utils.esodata.Tape; -import bjc.utils.ioutils.format.CLFormatter; -import bjc.utils.ioutils.format.CLModifiers; -import bjc.utils.ioutils.format.CLParameters; +import bjc.utils.ioutils.format.*; import bjc.utils.ioutils.ReportWriter; import java.io.IOException; @@ -71,57 +69,68 @@ public class ConditionalDirective implements Directive { } Object par = formatParams.item(); - if (mods.colonMod) { - formatParams.right(); - - if (par == null) { - throw new IllegalArgumentException("No parameter provided for [ directive."); - } else if (!(par instanceof Boolean)) { - throw new IllegalFormatConversionException('[', par.getClass()); - } - boolean res = (Boolean) par; - - String frmt; - if (res) - frmt = clauses.get(1); - else - frmt = clauses.get(0); - - fmt.doFormatString(frmt, rw, formatParams); - } else if (mods.atMod) { - if (par == null) { - throw new IllegalArgumentException("No parameter provided for [ directive."); - } else if (!(par instanceof Boolean)) { - throw new IllegalFormatConversionException('[', par.getClass()); - } - boolean res = (Boolean) par; - - if (res) { - fmt.doFormatString(clauses.get(0), rw, formatParams); - } else { + try { + if (mods.colonMod) { formatParams.right(); - } - } else { - int res; - if (arrParams.length() >= 1) { - res = arrParams.getInt(0, "conditional choice", '['); - } else { + if (par == null) { throw new IllegalArgumentException("No parameter provided for [ directive."); - } else if (!(par instanceof Number)) { + } else if (!(par instanceof Boolean)) { throw new IllegalFormatConversionException('[', par.getClass()); } - res = ((Number) par).intValue(); + boolean res = (Boolean) par; - formatParams.right(); - } + String frmt; + if (res) + frmt = clauses.get(1); + else + frmt = clauses.get(0); + + fmt.doFormatString(frmt, rw, formatParams); + } else if (mods.atMod) { + if (par == null) { + throw new IllegalArgumentException("No parameter provided for [ directive."); + } else if (!(par instanceof Boolean)) { + throw new IllegalFormatConversionException('[', par.getClass()); + } + boolean res = (Boolean) par; - if (res < 0 || res > clauses.size()) { - if (defClause != null) - fmt.doFormatString(defClause, rw, formatParams); + if (res) { + fmt.doFormatString(clauses.get(0), rw, formatParams); + } else { + formatParams.right(); + } } else { - fmt.doFormatString(clauses.get(res), rw, formatParams); + int res; + if (arrParams.length() >= 1) { + res = arrParams.getInt(0, "conditional choice", '['); + } else { + if (par == null) { + throw new IllegalArgumentException("No parameter provided for [ directive."); + } else if (!(par instanceof Number)) { + throw new IllegalFormatConversionException('[', par.getClass()); + } + res = ((Number) par).intValue(); + + formatParams.right(); + } + + if (res < 0 || res > clauses.size()) { + if (defClause != null) + fmt.doFormatString(defClause, rw, formatParams); + } else { + fmt.doFormatString(clauses.get(res), rw, formatParams); + } } + } catch (EscapeException eex) { + // @NOTE 9/5/18 + // + // I am not sure if it is valid to error here. I'm not + // even sure that we need to handle this here, but I + // dunno + //if (eex.endIteration) + // throw new UnsupportedOperationException("Colon mod not allowed on escape marker without colon mod on iteration"); + throw eex; } return; diff --git a/base/src/main/java/bjc/utils/ioutils/format/directives/IterationDirective.java b/base/src/main/java/bjc/utils/ioutils/format/directives/IterationDirective.java index 081d5e6..97e392e 100644 --- a/base/src/main/java/bjc/utils/ioutils/format/directives/IterationDirective.java +++ b/base/src/main/java/bjc/utils/ioutils/format/directives/IterationDirective.java @@ -2,13 +2,13 @@ package bjc.utils.ioutils.format.directives; import bjc.utils.esodata.SingleTape; import bjc.utils.esodata.Tape; -import bjc.utils.ioutils.format.CLFormatter; -import bjc.utils.ioutils.format.CLModifiers; -import bjc.utils.ioutils.format.CLParameters; +import bjc.utils.ioutils.format.*; import bjc.utils.ioutils.ReportWriter; import java.io.IOException; +import java.util.Iterator; + import java.util.IllegalFormatConversionException; import java.util.regex.Matcher; @@ -72,70 +72,97 @@ public class IterationDirective implements Directive { int numItr = 0; if (mods.atMod && mods.colonMod) { - do { - if (numItr > maxItr) - break; - numItr += 1; - - if (!(iter instanceof Iterable<?>)) { - throw new IllegalFormatConversionException('{', iter.getClass()); - } - - @SuppressWarnings("unchecked") - Iterable<Object> nitr = (Iterable<Object>) iter; - Tape<Object> nParams = new SingleTape<>(nitr); - - fmt.doFormatString(frmt, rw, nParams); - - iter = tParams.right(); - } while (tParams.position() < tParams.size()); + try { + do { + if (numItr > maxItr) break; + numItr += 1; + + if (!(iter instanceof Iterable<?>)) { + throw new IllegalFormatConversionException('{', iter.getClass()); + } + + @SuppressWarnings("unchecked") + Iterable<Object> nitr = (Iterable<Object>) iter; + Tape<Object> nParams = new SingleTape<>(nitr); + + try { + fmt.doFormatString(frmt, rw, nParams); + } catch (EscapeException eex) { + if (eex.endIteration) { + if (tParams.atEnd()) throw eex; + } + } + + iter = tParams.right(); + } while (tParams.position() < tParams.size()); + } catch (EscapeException eex) { + } } else if (mods.atMod) { + try { while (tParams.position() < tParams.size()) { - if (numItr > maxItr) - break; + if (numItr > maxItr) break; numItr += 1; fmt.doFormatString(frmt, rw, tParams); } + } catch (EscapeException eex) { + if (eex.endIteration) + throw new UnsupportedOperationException("Colon mod not allowed on escape marker without colon mod on iteration"); + } } else if (mods.colonMod) { if (!(item instanceof Iterable<?>)) { throw new IllegalFormatConversionException('{', item.getClass()); } - @SuppressWarnings("unchecked") - Iterable<Object> itr = (Iterable<Object>) item; - - for (Object obj : itr) { - if (numItr > maxItr) - break; - numItr += 1; - - if (!(obj instanceof Iterable<?>)) { - throw new IllegalFormatConversionException('{', obj.getClass()); - } - + try { @SuppressWarnings("unchecked") - Iterable<Object> nitr = (Iterable<Object>) obj; - Tape<Object> nParams = new SingleTape<>(nitr); - - fmt.doFormatString(frmt, rw, nParams); + Iterable<Object> itb = (Iterable<Object>) item; + Iterator<Object> itr = itb.iterator(); + while (itr.hasNext()) { + Object obj = itr.next(); + + if (numItr > maxItr) break; + numItr += 1; + + if (!(obj instanceof Iterable<?>)) { + throw new IllegalFormatConversionException('{', obj.getClass()); + } + + @SuppressWarnings("unchecked") + Iterable<Object> nitr = (Iterable<Object>) obj; + Tape<Object> nParams = new SingleTape<>(nitr); + + try { + fmt.doFormatString(frmt, rw, nParams); + } catch (EscapeException eex) { + if(eex.endIteration && !itr.hasNext()) throw eex; + } + } + } + catch (EscapeException eex) { } } else { if (!(item instanceof Iterable<?>)) { throw new IllegalFormatConversionException('{', item.getClass()); } - @SuppressWarnings("unchecked") - Iterable<Object> itr = (Iterable<Object>) item; + try { + @SuppressWarnings("unchecked") + Iterable<Object> itr = (Iterable<Object>) item; - Tape<Object> nParams = new SingleTape<>(itr); + Tape<Object> nParams = new SingleTape<>(itr); - while (nParams.position() < nParams.size()) { - if (numItr > maxItr) - break; - numItr += 1; + while (nParams.position() < nParams.size()) { + if (numItr > maxItr) + break; + numItr += 1; + + fmt.doFormatString(frmt, rw, nParams); - fmt.doFormatString(frmt, rw, nParams); + } + } catch (EscapeException eex) { + if (eex.endIteration) + throw new UnsupportedOperationException("Colon mod not allowed on escape marker without colon mod on iteration"); } } |
