From 0050863e9aa77d8963ecc70b6e13783051fdfb2d Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sat, 4 Jan 2020 12:16:19 -0500 Subject: Implement use of GroupDecree for CaseDirective This begins the work of using GroupDecree for reading in groups. --- .../java/bjc/utils/ioutils/format/CLTokenizer.java | 22 ++++++++++- .../java/bjc/utils/ioutils/format/GroupDecree.java | 30 +++++++++++++++ .../ioutils/format/directives/CaseDirective.java | 45 ++-------------------- 3 files changed, 53 insertions(+), 44 deletions(-) (limited to 'clformat/src/main/java/bjc/utils/ioutils/format') 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 1624692..47deefc 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/CLTokenizer.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/CLTokenizer.java @@ -145,6 +145,21 @@ public class CLTokenizer implements Iterator { return new Decree(sb.toString()); } + /** + * Read in a group decree. + * + * @param openedWith + * The decree that started the group. + * + * @param desiredClosing + * The name of the decree that will close the group. + * + * @return A group decree with the given properties. + */ + public GroupDecree nextGroup(Decree openedWith, String desiredClosing) { + return nextGroup(openedWith, desiredClosing, null); + } + /** * Read in a group decree. * @@ -169,8 +184,10 @@ public class CLTokenizer implements Iterator { int nestingLevel = 1; + Decree curDecree; + do { - Decree curDecree = next(); + curDecree = next(); // @TODO handle nesting & such if (curDecree.isLiteral) { @@ -204,7 +221,8 @@ public class CLTokenizer implements Iterator { } while (hasNext()); if (newGroup.closing == null) { - String msg = String.format("Did not find closing directive for group (wanted %s)", desiredClosing); + String msg = String.format("Did not find closing directive for group (wanted %s, last decree was %s)", + desiredClosing, curDecree.name); throw new NoSuchElementException(msg); } diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/GroupDecree.java b/clformat/src/main/java/bjc/utils/ioutils/format/GroupDecree.java index 0680a15..cdbe586 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/GroupDecree.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/GroupDecree.java @@ -74,4 +74,34 @@ public class GroupDecree { public void addChild(ClauseDecree child) { body.add(child); } + + /** + * Get the first clause from the group. + * + * @return The first clause in the group + */ + public ClauseDecree clause() { + return body.get(0); + } + + /** + * Get a specific clause from the group. + * + * @param idx + * The index of the clause to get. + * + * @return The clause at that index. + */ + public ClauseDecree clause(int idx) { + return body.get(idx); + } + + /** + * Get the body of the first clause. + * + * @return The decrees that make up the body of the first clause. + */ + public List unwrap() { + return body.get(0).body; + } } diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/CaseDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/CaseDirective.java index 28ce2db..9861864 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/CaseDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/CaseDirective.java @@ -20,46 +20,7 @@ public class CaseDirective implements Directive { public Edict compile(CompileContext compCTX) { CLModifiers mods = compCTX.decr.modifiers; - List condBody = new ArrayList<>(); - - int nestLevel = 1; - - Iterator dirIter = compCTX.directives; - while (dirIter.hasNext()) { - Decree decr = dirIter.next(); - - if (decr.isLiteral) { - condBody.add(decr); - - continue; - } - - String dirName = decr.name; - - if (dirName != null) { - if (dirName.equals("(")) { - if (nestLevel > 0) { - condBody.add(decr); - } - - nestLevel += 1; - } else if (Directive.isOpening(dirName)) { - nestLevel += 1; - - condBody.add(decr); - } else if (dirName.equals(")")) { - nestLevel = Math.max(0, nestLevel - 1); - - /* End the iteration. */ - if (nestLevel == 0) break; - } else if (Directive.isClosing(dirName)) { - nestLevel = Math.max(0, nestLevel - 1); - } else { - /* Not a special directive. */ - condBody.add(decr); - } - } - } + GroupDecree condBody = compCTX.directives.nextGroup(compCTX.decr, ")"); CaseEdict.Mode mode; @@ -93,8 +54,8 @@ class CaseEdict implements Edict { private CLFormatter formatter; - public CaseEdict(List body, Mode caseMode, CLFormatter fmt) { - this.body = new CLString(fmt.compile(body)); + public CaseEdict(GroupDecree body, Mode caseMode, CLFormatter fmt) { + this.body = new CLString(fmt.compile(body.unwrap())); this.caseMode = caseMode; -- cgit v1.2.3