diff options
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<String, Directive> builtinDirectives; // Extra directives specific to this formatter - private Map<String, Directive> extraDirectives; + private Map<String, Directive> 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<Decree> cltok, ReportWriter rw, Tape<Object> 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<Decree> cltok, ReportWriter rw, Tape<Object> tParams, boolean isToplevel) throws IOException { + public void doFormatString(CLTokenizer cltok, ReportWriter rw, Tape<Object> 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<Decree> it = cltok.iterator(); - + CLTokenizer it = CLTokenizer.fromTokens(cltok); return compile(it); } - public List<Edict> compile(Iterator<Decree> cltok) { + public List<Edict> compile(CLTokenizer cltok) { List<Edict> 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<Decree> { + /* + * Internal class for a tokenizer that returns a specific set of tokens. + */ + private static class SetCLTokenizer extends CLTokenizer { + private Iterator<Decree> body; + + public SetCLTokenizer(Iterator<Decree> bod) { + body = bod; + } + + public SetCLTokenizer(Iterable<Decree> 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<Decree> 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<Decree> 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<Decree> directives; + public CLTokenizer directives; /** * The configured formatter instance we are using. @@ -26,7 +26,7 @@ public class CompileContext { */ public Decree decr; - public CompileContext(Iterator<Decree> 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<Object> tParams; - public Iterator<Decree> dirIter; + public CLTokenizer dirIter; public CLFormatter fmt; public FormatParameters(ReportWriter rw, Object item, Decree decr, Tape<Object> tParams, - Iterator<Decree> dirIter, CLFormatter fmt) { + CLTokenizer dirIter, CLFormatter fmt) { this.rw = rw; this.item = item; |
