summaryrefslogtreecommitdiff
path: root/clformat/src/main/java/bjc/utils/ioutils
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2020-01-17 18:49:41 -0500
committerbculkin2442 <bjculkin@mix.wvu.edu>2020-01-17 18:49:41 -0500
commite99f6c758c9e1ea5601e1076845912db5153e38c (patch)
tree93a3effceae60fabc6671b914be6b0d848be62b6 /clformat/src/main/java/bjc/utils/ioutils
parentd8aade895eac0d1c971825f1e46e868568053bd3 (diff)
Revert "Revert "Re-apply implementation of GroupDecree for ConditionalDirective""
This reverts commit c7103ed995bef77b6645947c9a8820af2933dd90.
Diffstat (limited to 'clformat/src/main/java/bjc/utils/ioutils')
-rw-r--r--clformat/src/main/java/bjc/utils/ioutils/format/CLFormatter.java44
-rw-r--r--clformat/src/main/java/bjc/utils/ioutils/format/CLTokenizer.java2
-rw-r--r--clformat/src/main/java/bjc/utils/ioutils/format/GroupDecree.java16
-rw-r--r--clformat/src/main/java/bjc/utils/ioutils/format/directives/ConditionalDirective.java231
4 files changed, 133 insertions, 160 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 73bb5fa..56c64f6 100644
--- a/clformat/src/main/java/bjc/utils/ioutils/format/CLFormatter.java
+++ b/clformat/src/main/java/bjc/utils/ioutils/format/CLFormatter.java
@@ -369,11 +369,11 @@ public class CLFormatter {
/**
* Compile a CLString from a string.
- *
+ *
* @param inp
- * The string to compile.
- *
- * @return The compiled string.
+ * The string to compile.
+ *
+ * @return A CLString compiled from the input.
*/
public CLString compile(String inp) {
CLTokenizer tokenzer = new CLTokenizer(inp);
@@ -384,15 +384,15 @@ public class CLFormatter {
}
/**
- * Compile a set of decrees into a set of edicts.
- *
+ * Compile a set of edicts from a list of decrees.
+ *
* @param decrees
- * The decrees to compile.
- *
- * @return The edicts compiled from the decrees.
+ * The decrees to compile.
+ *
+ * @return A set of edicts compiled from the decrees.
*/
public List<Edict> compile(Iterable<Decree> decrees) {
- // Not 100% sure this is correct, but the tests are passing
+ // If we have no decrees, there are no edicts.
if (decrees == null) return new ArrayList<>();
CLTokenizer it = CLTokenizer.fromTokens(decrees);
@@ -400,12 +400,26 @@ public class CLFormatter {
}
/**
- * Compile a set of edicts from a tokenizer.
- *
+ * Compile a set of edicts from a clause.
+ *
+ * @param clause
+ * The clause to compile.
+ *
+ * @return The set of edicts compiled from the clause.
+ */
+ public List<Edict> compile(ClauseDecree clause) {
+ if (clause == null) return new ArrayList<>();
+
+ return compile(clause.body);
+ }
+
+ /**
+ * Compile a set of edicts from a set of tokens.
+ *
* @param cltok
- * The tokenizer to get decrees from.
- *
- * @return The set of edicts compiled from the tokenizer.
+ * The tokenizer providing us with our tokens.
+ *
+ * @return The edicts compiled from those tokens.
*/
public List<Edict> compile(CLTokenizer cltok) {
List<Edict> result = new ArrayList<>();
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 1b9c90f..14eebac 100644
--- a/clformat/src/main/java/bjc/utils/ioutils/format/CLTokenizer.java
+++ b/clformat/src/main/java/bjc/utils/ioutils/format/CLTokenizer.java
@@ -239,7 +239,7 @@ 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, last decree was %s)",
+ 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 f6a46ae..b3ae69b 100644
--- a/clformat/src/main/java/bjc/utils/ioutils/format/GroupDecree.java
+++ b/clformat/src/main/java/bjc/utils/ioutils/format/GroupDecree.java
@@ -12,7 +12,7 @@ import java.util.*;
*
* @author Ben Culkin
*/
-public class GroupDecree {
+public class GroupDecree implements Iterable<ClauseDecree> {
/**
* The decree that opened this group.
*/
@@ -134,4 +134,18 @@ public class GroupDecree {
}
// return String.format("GroupDecree [opening=%s, closing=%s, body=%s]", opening, closing, body);
}
+
+ @Override
+ public Iterator<ClauseDecree> iterator() {
+ return body.iterator();
+ }
+
+ /**
+ * Get the number of clauses in this group.
+ *
+ * @return The number of clauses in the group.
+ */
+ public int size() {
+ return body.size();
+ }
}
diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/ConditionalDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/ConditionalDirective.java
index 099c793..2eb5e4c 100644
--- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/ConditionalDirective.java
+++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/ConditionalDirective.java
@@ -2,6 +2,8 @@ package bjc.utils.ioutils.format.directives;
import java.io.*;
import java.util.*;
+import java.util.regex.*;
+
import bjc.utils.esodata.*;
import bjc.utils.ioutils.format.*;
@@ -20,85 +22,20 @@ public class ConditionalDirective implements Directive {
CLModifiers mods = compCTX.decr.modifiers;
CLParameters params = compCTX.decr.parameters;
- List<Decree> condBody = new ArrayList<>();
- List<List<Decree>> clauses = new ArrayList<>();
+ GroupDecree clauses = compCTX.directives.nextGroup(compCTX.decr, "]", ";");
- List<Decree> defClause = null;
+ ClauseDecree defClause = null;
boolean isDefault = false;
- int nestLevel = 1;
+ for (ClauseDecree clause : clauses) {
+ if (isDefault) defClause = clause;
- 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);
-
- if (nestLevel == 0) {
- /* End the conditional. */
- List<Decree> clause = condBody;
-
- if (isDefault) {
- defClause = clause;
- }
- clauses.add(clause);
-
- break;
- } else {
- /* Not a special directive. */
- condBody.add(decr);
- }
- } else if (Directive.isClosing(dirName)) {
- nestLevel = Math.max(0, nestLevel - 1);
-
- condBody.add(decr);
- } else if (dirName.equals(";")) {
- if (nestLevel == 1) {
- /* End the clause. */
- List<Decree> clause = condBody;
-
- condBody = new ArrayList<>();
-
- if (isDefault) {
- defClause = clause;
- }
- clauses.add(clause);
-
- /*
- * Mark the next clause as the
- * default.
- */
- if (decr.modifiers.colonMod) {
- isDefault = true;
- }
- } else {
- /* Not a special directive. */
- condBody.add(decr);
- }
- } else {
- /* Not a special directive. */
- condBody.add(decr);
- }
+ if (clause.terminator != null && clause.terminator.modifiers.colonMod) {
+ isDefault = true;
}
}
- if (mods.starMod && clauses.size() > 0) defClause = clauses.get(0);
+ if (mods.starMod && clauses.size() > 0) defClause = clauses.clause();
CLValue index = null;
@@ -118,13 +55,16 @@ public class ConditionalDirective implements Directive {
mode = ConditionalEdict.Mode.INDEX_CLAUSE;
}
- return new ConditionalEdict(mode, mods.dollarMod, index, clauses, defClause, compCTX.formatter);
+ return new ConditionalEdict(mode, mods.dollarMod, index, clauses,
+ defClause, compCTX.formatter);
}
}
class ConditionalEdict implements Edict {
public static enum Mode {
- FIRST_SECOND, OUTPUT_TRUE, INDEX_CLAUSE
+ FIRST_SECOND,
+ OUTPUT_TRUE,
+ INDEX_CLAUSE
}
private Mode condMode;
@@ -135,18 +75,23 @@ class ConditionalEdict implements Edict {
private List<CLString> clauses;
private CLString defClause;
- public ConditionalEdict(Mode condMode, boolean decrementIndex, CLValue index, List<List<Decree>> clauses,
- List<Decree> defClause, CLFormatter fmt) {
+ private CLFormatter formatter;
+
+ public ConditionalEdict(Mode condMode, boolean decrementIndex,
+ CLValue index, GroupDecree clauses, ClauseDecree defClause,
+ CLFormatter fmt) {
this.condMode = condMode;
this.decrementIndex = decrementIndex;
this.index = index;
this.clauses = new ArrayList<>();
- for (List<Decree> clause : clauses) {
+ for (ClauseDecree clause : clauses) {
this.clauses.add(new CLString(fmt.compile(clause)));
}
this.defClause = new CLString(fmt.compile(defClause));
+
+ this.formatter = fmt;
}
@Override
@@ -155,91 +100,91 @@ class ConditionalEdict implements Edict {
try {
switch (condMode) {
- case FIRST_SECOND: {
- Object o = items.item();
- items.right();
-
- boolean res = false;
- if (o == null) {
- //throw new IllegalArgumentException("No parameter provided for [ directive.");
- } else if (!(o instanceof Boolean)) {
- throw new IllegalFormatConversionException('[', o.getClass());
- } else {
- res = (Boolean) o;
- }
+ case FIRST_SECOND:
+ {
+ Object o = items.item();
+ items.right();
- CLString frmt;
- if (res) {
- frmt = clauses.get(1);
- } else {
- frmt = clauses.get(0);
- }
+ boolean res = false;
+ if (o == null) {
+ //throw new IllegalArgumentException("No parameter provided for [ directive.");
+ } else if (!(o instanceof Boolean)) {
+ throw new IllegalFormatConversionException('[', o.getClass());
+ } else {
+ res = (Boolean) o;
+ }
- frmt.format(formCTX);
- }
- break;
- case OUTPUT_TRUE: {
- boolean res = false;
- Object o = items.item();
-
- if (o == null) {
- // throw new IllegalArgumentException("No parameter provided for [ directive.");
- } else if (o instanceof Integer) {
- if ((Integer) o != 0) {
- res = true;
+ CLString frmt;
+ if (res) {
+ frmt = clauses.get(1);
+ } else {
+ frmt = clauses.get(0);
}
- } else if (o instanceof Boolean) {
- res = (Boolean) o;
- } else {
- throw new IllegalFormatConversionException('[', o.getClass());
- }
- if (res) {
- clauses.get(0).format(formCTX);
- } else {
- items.right();
+ frmt.format(formCTX);
}
- }
break;
- case INDEX_CLAUSE: {
- int res;
-
- if (index != null) {
- res = index.asInt(items, "conditional choice", "[", 0);
- } else {
+ case OUTPUT_TRUE:
+ {
+ boolean res = false;
Object o = items.item();
if (o == null) {
- throw new IllegalArgumentException(
- "No parameter provided for [ directive.");
- } else if (!(o instanceof Number)) { throw new IllegalFormatConversionException(
- '[', o.getClass()); }
-
- res = ((Number) o).intValue();
+ // throw new IllegalArgumentException("No parameter provided for [ directive.");
+ } else if (o instanceof Integer) {
+ if ((Integer)o != 0) {
+ res = true;
+ }
+ } else if (o instanceof Boolean) {
+ res = (Boolean) o;
+ } else {
+ throw new IllegalFormatConversionException('[', o.getClass());
+ }
- items.right();
+ if (res) {
+ clauses.get(0).format(formCTX);
+ } else {
+ items.right();
+ }
}
+ break;
+ case INDEX_CLAUSE:
+ {
+ int res;
+
+ if (index != null) {
+ res = index.asInt(items, "conditional choice", "[", 0);
+ } else {
+ Object o = items.item();
+
+ if (o == null) {
+ throw new IllegalArgumentException("No parameter provided for [ directive.");
+ } else if (!(o instanceof Number)) {
+ throw new IllegalFormatConversionException('[', o.getClass());
+ }
- if (decrementIndex) res -= 1;
+ res = ((Number) o).intValue();
- if (clauses.size() == 0 || res < 0 || res >= clauses.size()) {
- if (defClause != null) {
- defClause.format(formCTX.writer, items);
+ items.right();
}
- } else {
- CLString frmt = clauses.get(res);
- frmt.format(formCTX.writer, items);
+ if (decrementIndex) res -= 1;
+
+ if (clauses.size() == 0 || res < 0 || res >= clauses.size()) {
+ if (defClause != null) {
+ defClause.format(formCTX.writer, items);
+ }
+ } else {
+ CLString frmt = clauses.get(res);
+
+ frmt.format(formCTX.writer, items);
+ }
}
- }
break;
- default:
- throw new IllegalArgumentException("INTERNAL ERROR: ConditionalEdict mode " + condMode
- + " is not supported. This is a bug.");
}
- } catch (DirectiveEscape eex) {
+ } catch (DirectiveEscape dex) {
// Conditionals are transparent to iteration-escapes
- throw eex;
+ throw dex;
}
}
}