diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2018-11-15 20:16:55 -0500 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2018-11-15 20:16:55 -0500 |
| commit | 2f3d7c21008510e240cfbeaa8179b7f03b432cce (patch) | |
| tree | 0e155709f7ce8694597f8a8c00c57066b68bfd5a /src/main/java/bjc/inflexion/InflectionString.java | |
| parent | e3bc4bcf923665555aa587a82a7eba803799e423 (diff) | |
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.
Diffstat (limited to 'src/main/java/bjc/inflexion/InflectionString.java')
| -rw-r--r-- | src/main/java/bjc/inflexion/InflectionString.java | 89 |
1 files changed, 79 insertions, 10 deletions
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; @@ -42,6 +43,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<String> 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<String> 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. * * @author bjculkin @@ -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 |
