From 6d781f3b203397196d200f0b80a8fe358ec8f53c Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Thu, 6 Sep 2018 17:59:51 -0300 Subject: Implement tabulate directive --- .../java/bjc/utils/ioutils/format/CLFormatter.java | 15 +--- .../format/directives/TabulateDirective.java | 80 ++++++++++++++++++++++ 2 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 base/src/main/java/bjc/utils/ioutils/format/directives/TabulateDirective.java (limited to 'base/src/main/java') diff --git a/base/src/main/java/bjc/utils/ioutils/format/CLFormatter.java b/base/src/main/java/bjc/utils/ioutils/format/CLFormatter.java index f0fd404..1605a75 100644 --- a/base/src/main/java/bjc/utils/ioutils/format/CLFormatter.java +++ b/base/src/main/java/bjc/utils/ioutils/format/CLFormatter.java @@ -65,6 +65,8 @@ public class CLFormatter { builtinDirectives.put("^", new EscapeDirective()); builtinDirectives.put("[", new ConditionalDirective()); builtinDirectives.put("{", new IterationDirective()); + + builtinDirectives.put("T", new TabulateDirective()); } /** @@ -229,22 +231,11 @@ public class CLFormatter { throw new IllegalArgumentException("Found conditional-end outside of conditional."); case ";": throw new IllegalArgumentException( - "Found conditional-seperator outside of conditional."); + "Found seperator outside of block."); case "}": throw new IllegalArgumentException("Found iteration-end outside of iteration"); - case "T": case "<": case ">": - /* - * @TODO Figure out how to implement - * tabulation/justification in a reasonable - * manner. - * - * 9/5/18 - * - * We did, but the rest of the code needs to be - * converted to use ReportWriter instead - */ throw new IllegalArgumentException("Layout-control directives aren't implemented yet."); case "F": case "E": diff --git a/base/src/main/java/bjc/utils/ioutils/format/directives/TabulateDirective.java b/base/src/main/java/bjc/utils/ioutils/format/directives/TabulateDirective.java new file mode 100644 index 0000000..7775904 --- /dev/null +++ b/base/src/main/java/bjc/utils/ioutils/format/directives/TabulateDirective.java @@ -0,0 +1,80 @@ +package bjc.utils.ioutils.format.directives; + +import java.io.IOException; +import java.util.regex.Matcher; + +import bjc.utils.esodata.Tape; + +import bjc.utils.ioutils.ReportWriter; +import bjc.utils.ioutils.format.*; + +public class TabulateDirective implements Directive { + public void format(ReportWriter rw, Object item, CLModifiers mods, CLParameters arrParams, Tape tParams, + Matcher dirMatcher, CLFormatter fmt) 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 of font glyph + // size, not character positions + if (mods.colonMod) { + throw new UnsupportedOperationException("Colon mod is not supported for T directive"); + } + + // Support for a possible future feature + char padchar = ' '; + + if (mods.atMod) { + int colrel = 1, colinc = 1; + + if (arrParams.length() > 2) { + colinc = arrParams.getIntDefault(1, "column increment", 'T', 1); + } + + if (arrParams.length() > 1) { + colrel = arrParams.getIntDefault(0, "relative column number", 'T', 1); + } + + for (int i = 0; i < colrel; i++) { + rw.write(padchar); + } + + int currCol = rw.getLinePos(); + + int nSpaces = 0; + + while ((currCol + nSpaces) % colinc != 0) nSpaces++; + + for (int i = 0; i < nSpaces; i++) { + rw.write(padchar); + } + } else { + int colnum = 1, colinc = 1; + + if (arrParams.length() > 2) { + colinc = arrParams.getIntDefault(1, "column increment", 'T', 1); + } + + if (arrParams.length() > 1) { + colnum = arrParams.getIntDefault(0, "column number", 'T', 1); + } + + int currCol = rw.getLinePos(); + + if (currCol < colnum) { + for (int i = currCol; i < colnum; i++) { + rw.write(padchar); + } + } else { + if (colinc == 0) return; + + int k = 0; + + while (colnum > (currCol + (k * colinc))) k++; + + for (int i = currCol; i < colnum; i++) { + rw.write(padchar); + } + } + } + } +} -- cgit v1.2.3