From 2f3d7c21008510e240cfbeaa8179b7f03b432cce Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 15 Nov 2018 20:16:55 -0500 Subject: Fix up error handling (throw when parse errors happen) Error handling for InflectionStrings will now make itself known, as an exception will be thrown upon there being errors parsing the string. --- src/main/java/bjc/inflexion/InflectionString.java | 89 ++++++++++++++++++++--- 1 file changed, 79 insertions(+), 10 deletions(-) (limited to 'src/main/java/bjc/inflexion') diff --git a/src/main/java/bjc/inflexion/InflectionString.java b/src/main/java/bjc/inflexion/InflectionString.java index 4bb024a..b901704 100644 --- a/src/main/java/bjc/inflexion/InflectionString.java +++ b/src/main/java/bjc/inflexion/InflectionString.java @@ -21,6 +21,7 @@ import static bjc.inflexion.InflectionString.InflectionDirective.variable; import static java.util.Arrays.asList; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -41,6 +42,68 @@ import bjc.inflexion.nouns.Prepositions; * */ public class InflectionString { + /** + * Exception thrown if the string we are attempting to compile has + * invalid syntax. + * + * @author bjculkin + * + */ + public class InflectionFormatException extends RuntimeException { + private static final long serialVersionUID = -5306003088746525691L; + + /** + * The string we attempted to parse. + */ + public final String inp; + /** + * The errors we encountered parsing the string. + */ + public final List parseErrors; + + /** + * Create a new format exception. + * + * @param inp + * The string we are attempting to compile + * @param parseErrors + * The errors we encountered parsing the string. + */ + public InflectionFormatException(String inp, List parseErrors) { + this.inp = inp; + // Can't modify the list of parse errors. + this.parseErrors = Collections.unmodifiableList(parseErrors); + } + + /* + * (non-Javadoc) + * + * @see java.lang.Throwable#toString() + */ + @Override + public String toString() { + boolean doBrief = false; + + if (doBrief) return String.format("Encountered errors attempting to parse string %s", + parseErrors.size(), inp); + + StringBuilder sb = new StringBuilder(parseErrors.size()); + sb.append("Encountered errors attempting to parse the following string:\n\t"); + sb.append(inp); + sb.append("\nErrors:"); + for (int i = 0; i < parseErrors.size(); i++) { + String msg = parseErrors.get(i); + sb.append("\n\t"); + sb.append(msg); + } + sb.append("\n(total of "); + sb.append(parseErrors.size()); + sb.append(" errors)"); + + return sb.toString(); + } + } + /** * Represents a directive in a inflection string. * @@ -166,18 +229,13 @@ public class InflectionString { * Corresponds to the 'f' option. */ public boolean summarize; + /** * Mark the summarization as occurring at the end of the * string, regardless of its current position. */ public boolean atEnd = false; - // Emit error message - private static String error(int curPos, int i, String msg, Object... props) { - return String.format("%s (at position %d in # directive starting at %d)", - String.format(msg, props), curPos + i, curPos); - } - /** * Create a new set of numeric options from a string. * @@ -266,7 +324,7 @@ public class InflectionString { } /** - * + * Create a blank set of numeric options. */ public NumericOptions() { } @@ -308,6 +366,11 @@ public class InflectionString { prevOption, currNum)); } } + + // Emit error message + private static String error(int curPos, int i, String msg, Object... props) { + return InflectionDirective.error("#", curPos, i, msg, props); + } } /** @@ -375,18 +438,23 @@ public class InflectionString { } /** - * + * Create an empty set of noun options. */ public NounOptions() { } // Emit error message private static String error(int curPos, int i, String msg, Object... props) { - return String.format("%s (at position %d in N directive starting at %d)", - String.format(msg, props), curPos + i, curPos); + return InflectionDirective.error("N", curPos, i, msg, props); } } + // Emit error message + private static String error(String dir, int curPos, int i, String msg, Object... props) { + return String.format("%s (at position %d in %s directive starting at position %d)", + String.format(msg, props), curPos + i, dir, curPos); + } + /** * The type of the directive. */ @@ -740,6 +808,7 @@ public class InflectionString { curPos += strang.length(); } + if (!parseErrors.isEmpty()) throw new InflectionFormatException(inp, parseErrors); } // Emit an error message -- cgit v1.2.3