From 3f99fcbdb98dfd6da75ce8a8227477345b6b1d02 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sat, 4 Jan 2020 11:58:34 -0500 Subject: More work on group decree refactor The previous version had some places where it wasn't right. This does the rest of the ground work that it seemed needed to be done. --- .../java/bjc/utils/ioutils/format/CLFormatter.java | 33 ++---------- .../java/bjc/utils/ioutils/format/CLTokenizer.java | 63 ++++++++++++++++++++++ .../ioutils/format/directives/CompileContext.java | 4 +- .../format/directives/FormatParameters.java | 4 +- 4 files changed, 71 insertions(+), 33 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 92a7a63..df01238 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/CLFormatter.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/CLFormatter.java @@ -29,7 +29,7 @@ public class CLFormatter { private static Map builtinDirectives; // Extra directives specific to this formatter - private Map extraDirectives; + private Map extraDirectives; static { // Set up the built-in directives @@ -253,31 +253,7 @@ public class CLFormatter { * * @throws IOException If something goes wrong */ - public void doFormatString(Iterable cltok, ReportWriter rw, Tape tParams, boolean isToplevel) throws IOException { - doFormatString(cltok.iterator(), rw, tParams, isToplevel); - } - - /** - * Fill in a partially started format string. - * - * Used mostly for directives that require formatting again with a - * different string. - * - * @param cltok - * The place to get tokens from. - * - * @param rw - * The buffer to file output into. - * - * @param tParams - * The parameters to use. - * - * @param isToplevel - * Whether or not this is a top-level format - * - * @throws IOException If something goes wrong - */ - public void doFormatString(Iterator cltok, ReportWriter rw, Tape tParams, boolean isToplevel) throws IOException { + public void doFormatString(CLTokenizer cltok, ReportWriter rw, Tape tParams, boolean isToplevel) throws IOException { boolean doTail = true; try { @@ -383,12 +359,11 @@ public class CLFormatter { // Not 100% sure this is correct, but the tests are passing if (cltok == null) return new ArrayList<>(); - Iterator it = cltok.iterator(); - + CLTokenizer it = CLTokenizer.fromTokens(cltok); return compile(it); } - public List compile(Iterator cltok) { + public List compile(CLTokenizer cltok) { List result = new ArrayList<>(); while (cltok.hasNext()) { diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/CLTokenizer.java b/clformat/src/main/java/bjc/utils/ioutils/format/CLTokenizer.java index f19fcf8..1624692 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/CLTokenizer.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/CLTokenizer.java @@ -7,14 +7,77 @@ import bjc.utils.ioutils.*; import bjc.utils.ioutils.format.directives.*; public class CLTokenizer implements Iterator { + /* + * Internal class for a tokenizer that returns a specific set of tokens. + */ + private static class SetCLTokenizer extends CLTokenizer { + private Iterator body; + + public SetCLTokenizer(Iterator bod) { + body = bod; + } + + public SetCLTokenizer(Iterable bod) { + body = bod.iterator(); + } + + @Override + public boolean hasNext() { + return body.hasNext(); + } + + @Override + public Decree next() { + return body.next(); + } + } + private Matcher mat; private Decree dir; + /** + * Empty constructor that should only be invoked if you are a subclass who overrides + * hasNext()/next(). + */ + protected CLTokenizer() { + + } + + /** + * Create a new tokenizer, tokenizing from a given string. + * + * @param strang + * The string to tokenize from. + */ public CLTokenizer(String strang) { this.mat = CLPattern.getDirectiveMatcher(strang); } + /** + * Create a CLTokenizer yielding a given set of decrees. + * + * @param bod + * The decrees to yield. + * + * @return A tokenizer yielding the given set of decrees. + */ + public static CLTokenizer fromTokens(Iterator bod) { + return new SetCLTokenizer(bod); + } + + /** + * Create a CLTokenizer yielding a given set of decrees. + * + * @param bod + * The decrees to yield. + * + * @return A tokenizer yielding the given set of decrees. + */ + public static CLTokenizer fromTokens(Iterable bod) { + return new SetCLTokenizer(bod); + } + @Override public boolean hasNext() { return !mat.hitEnd(); diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/CompileContext.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/CompileContext.java index 1cfae14..4fa2fcd 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/CompileContext.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/CompileContext.java @@ -14,7 +14,7 @@ public class CompileContext { /** * The stream of parsed directives. */ - public Iterator directives; + public CLTokenizer directives; /** * The configured formatter instance we are using. @@ -26,7 +26,7 @@ public class CompileContext { */ public Decree decr; - public CompileContext(Iterator dirs, CLFormatter fmt, Decree dcr) { + public CompileContext(CLTokenizer dirs, CLFormatter fmt, Decree dcr) { directives = dirs; formatter = fmt; diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/FormatParameters.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/FormatParameters.java index 7604c77..82fafbc 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/FormatParameters.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/FormatParameters.java @@ -15,12 +15,12 @@ public class FormatParameters { public Tape tParams; - public Iterator dirIter; + public CLTokenizer dirIter; public CLFormatter fmt; public FormatParameters(ReportWriter rw, Object item, Decree decr, Tape tParams, - Iterator dirIter, CLFormatter fmt) { + CLTokenizer dirIter, CLFormatter fmt) { this.rw = rw; this.item = item; -- cgit v1.2.3