summaryrefslogtreecommitdiff
path: root/clformat/src/main/java/bjc
diff options
context:
space:
mode:
Diffstat (limited to 'clformat/src/main/java/bjc')
-rw-r--r--clformat/src/main/java/bjc/utils/ioutils/format/CLTokenizer.java22
-rw-r--r--clformat/src/main/java/bjc/utils/ioutils/format/GroupDecree.java30
-rw-r--r--clformat/src/main/java/bjc/utils/ioutils/format/directives/CaseDirective.java45
3 files changed, 53 insertions, 44 deletions
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
@@ -154,6 +154,21 @@ public class CLTokenizer implements Iterator<Decree> {
* @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.
+ *
+ * @param openedWith
+ * The decree that started the group.
+ *
+ * @param desiredClosing
+ * The name of the decree that will close the group.
+ *
* @param clauseSep
* The name of the decree that will separate clauses in the group. Pass 'null' if this group
* doesn't have separate clauses.
@@ -169,8 +184,10 @@ public class CLTokenizer implements Iterator<Decree> {
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<Decree> {
} 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<Decree> 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<Decree> condBody = new ArrayList<>();
-
- int nestLevel = 1;
-
- Iterator<Decree> 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<Decree> 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;