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