| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| GenericParseException |
|
| 0.0;0 |
| 1 | package org.webslinger.javacc.support; | |
| 2 | ||
| 3 | /** | |
| 4 | * This exception is thrown when parse errors are encountered. | |
| 5 | * You can explicitly create objects of this exception type by | |
| 6 | * calling the method generateParseException in the generated | |
| 7 | * parser. | |
| 8 | * | |
| 9 | * You can modify this class to customize your error reporting | |
| 10 | * mechanisms so long as you retain the public fields. | |
| 11 | */ | |
| 12 | public abstract class GenericParseException extends Exception { | |
| 13 | /** | |
| 14 | * The end of line string for this machine. | |
| 15 | */ | |
| 16 | 0 | public static final String eol = System.getProperty("line.separator", "\n"); |
| 17 | ||
| 18 | protected int getNextTokenKind() { | |
| 19 | 0 | throw new AbstractMethodError("getNextTokenKind"); |
| 20 | } | |
| 21 | ||
| 22 | protected int getTokenKind() { | |
| 23 | 0 | throw new AbstractMethodError("getTokenKind"); |
| 24 | } | |
| 25 | ||
| 26 | protected int getBeginColumn() { | |
| 27 | 0 | throw new AbstractMethodError("getBeginColumn"); |
| 28 | } | |
| 29 | ||
| 30 | protected int getBeginLine() { | |
| 31 | 0 | throw new AbstractMethodError("getBeginLine"); |
| 32 | } | |
| 33 | ||
| 34 | protected StringBuilder getEncountered(int maxSize) { | |
| 35 | 0 | throw new AbstractMethodError("getEncountered"); |
| 36 | } | |
| 37 | ||
| 38 | ||
| 39 | /** | |
| 40 | * This variable determines which constructor was used to create | |
| 41 | * this object and thereby affects the semantics of the | |
| 42 | * "getMessage" method (see below). | |
| 43 | */ | |
| 44 | protected boolean specialConstructor; | |
| 45 | ||
| 46 | ||
| 47 | /** | |
| 48 | * Each entry in this array is an array of integers. Each array | |
| 49 | * of integers represents a sequence of tokens (by their ordinal | |
| 50 | * values) that is expected at this point of the parse. | |
| 51 | */ | |
| 52 | public int[][] expectedTokenSequences; | |
| 53 | ||
| 54 | /** | |
| 55 | * This is a reference to the "tokenImage" array of the generated | |
| 56 | * parser within which the parse error occurred. This array is | |
| 57 | * defined in the generated ...Constants interface. | |
| 58 | */ | |
| 59 | public String[] tokenImage; | |
| 60 | ||
| 61 | /** | |
| 62 | * This constructor is used by the method "generateParseException" | |
| 63 | * in the generated parser. Calling this constructor generates | |
| 64 | * a new object of this type with the fields | |
| 65 | * "expectedTokenSequences", and "tokenImage" set. The boolean | |
| 66 | * flag "specialConstructor" is also set to true to indicate that | |
| 67 | * this constructor was used to create this object. | |
| 68 | * This constructor calls its super class with the empty string | |
| 69 | * to force the "toString" method of parent class "Throwable" to | |
| 70 | * print the error message in the form: | |
| 71 | * ParseException: <result of getMessage> | |
| 72 | */ | |
| 73 | public GenericParseException(int[][] expectedTokenSequencesVal, | |
| 74 | String[] tokenImageVal | |
| 75 | ) | |
| 76 | { | |
| 77 | 0 | super(""); |
| 78 | 0 | specialConstructor = true; |
| 79 | 0 | expectedTokenSequences = expectedTokenSequencesVal; |
| 80 | 0 | tokenImage = tokenImageVal; |
| 81 | 0 | } |
| 82 | ||
| 83 | /** | |
| 84 | * The following constructors are for use by you for whatever | |
| 85 | * purpose you can think of. Constructing the exception in this | |
| 86 | * manner makes the exception behave in the normal way - i.e., as | |
| 87 | * documented in the class "Throwable". The fields "errorToken", | |
| 88 | * "expectedTokenSequences", and "tokenImage" do not contain | |
| 89 | * relevant information. The JavaCC generated code does not use | |
| 90 | * these constructors. | |
| 91 | */ | |
| 92 | ||
| 93 | public GenericParseException() { | |
| 94 | 0 | super(); |
| 95 | 0 | specialConstructor = false; |
| 96 | 0 | } |
| 97 | ||
| 98 | public GenericParseException(String message) { | |
| 99 | 0 | super(message); |
| 100 | 0 | specialConstructor = false; |
| 101 | 0 | } |
| 102 | ||
| 103 | /** | |
| 104 | * This method has the standard behavior when this object has been | |
| 105 | * created using the standard constructors. Otherwise, it uses | |
| 106 | * "currentToken" and "expectedTokenSequences" to generate a parse | |
| 107 | * error message and returns it. If this object has been created | |
| 108 | * due to a parse error, and you do not catch it (it gets thrown | |
| 109 | * from the parser), then this method is called during the printing | |
| 110 | * of the final stack trace, and hence the correct error message | |
| 111 | * gets displayed. | |
| 112 | */ | |
| 113 | public String getMessage() { | |
| 114 | 0 | if (!specialConstructor) { |
| 115 | 0 | return super.getMessage(); |
| 116 | } | |
| 117 | 0 | StringBuilder expected = new StringBuilder(); |
| 118 | 0 | int maxSize = 0; |
| 119 | 0 | for (int[] expectedTokenSequence: expectedTokenSequences) { |
| 120 | 0 | if (maxSize < expectedTokenSequence.length) { |
| 121 | 0 | maxSize = expectedTokenSequence.length; |
| 122 | } | |
| 123 | 0 | for (int idx: expectedTokenSequence) { |
| 124 | 0 | expected.append(tokenImage[idx]).append(" "); |
| 125 | } | |
| 126 | 0 | if (expectedTokenSequence[expectedTokenSequence.length - 1] != 0) { |
| 127 | 0 | expected.append("..."); |
| 128 | } | |
| 129 | 0 | expected.append(eol).append(" "); |
| 130 | } | |
| 131 | 0 | StringBuilder retval = getEncountered(maxSize); |
| 132 | 0 | if (expectedTokenSequences.length == 1) { |
| 133 | 0 | retval.append("Was expecting:").append(eol).append(" "); |
| 134 | } else { | |
| 135 | 0 | retval.append("Was expecting one of:").append(eol).append(" "); |
| 136 | } | |
| 137 | 0 | retval.append(expected); |
| 138 | 0 | return retval.toString(); |
| 139 | } | |
| 140 | ||
| 141 | /** | |
| 142 | * Used to convert raw characters to their escaped version | |
| 143 | * when these raw version cannot be used as part of an ASCII | |
| 144 | * string literal. | |
| 145 | */ | |
| 146 | protected String add_escapes(String str) { | |
| 147 | 0 | StringBuilder retval = new StringBuilder(); |
| 148 | char ch; | |
| 149 | 0 | for (int i = 0; i < str.length(); i++) { |
| 150 | 0 | switch (str.charAt(i)) { |
| 151 | case 0 : | |
| 152 | 0 | continue; |
| 153 | case '\b': | |
| 154 | 0 | retval.append("\\b"); |
| 155 | 0 | continue; |
| 156 | case '\t': | |
| 157 | 0 | retval.append("\\t"); |
| 158 | 0 | continue; |
| 159 | case '\n': | |
| 160 | 0 | retval.append("\\n"); |
| 161 | 0 | continue; |
| 162 | case '\f': | |
| 163 | 0 | retval.append("\\f"); |
| 164 | 0 | continue; |
| 165 | case '\r': | |
| 166 | 0 | retval.append("\\r"); |
| 167 | 0 | continue; |
| 168 | case '\"': | |
| 169 | 0 | retval.append("\\\""); |
| 170 | 0 | continue; |
| 171 | case '\'': | |
| 172 | 0 | retval.append("\\\'"); |
| 173 | 0 | continue; |
| 174 | case '\\': | |
| 175 | 0 | retval.append("\\\\"); |
| 176 | 0 | continue; |
| 177 | default: | |
| 178 | 0 | if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { |
| 179 | 0 | String s = "0000" + Integer.toString(ch, 16); |
| 180 | 0 | retval.append("\\u" + s.substring(s.length() - 4, s.length())); |
| 181 | 0 | } else { |
| 182 | 0 | retval.append(ch); |
| 183 | } | |
| 184 | continue; | |
| 185 | } | |
| 186 | } | |
| 187 | 0 | return retval.toString(); |
| 188 | } | |
| 189 | } |