| 1 | |
|
| 2 | |
|
| 3 | |
package org.apache.commons.ognl; |
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
public class ParseException extends Exception { |
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
private static final long serialVersionUID = 1L; |
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
public ParseException(Token currentTokenVal, |
| 30 | |
int[][] expectedTokenSequencesVal, |
| 31 | |
String[] tokenImageVal |
| 32 | |
) |
| 33 | |
{ |
| 34 | 1 | super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal)); |
| 35 | 1 | currentToken = currentTokenVal; |
| 36 | 1 | expectedTokenSequences = expectedTokenSequencesVal; |
| 37 | 1 | tokenImage = tokenImageVal; |
| 38 | 1 | } |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public ParseException() { |
| 51 | 0 | super(); |
| 52 | 0 | } |
| 53 | |
|
| 54 | |
|
| 55 | |
public ParseException(String message) { |
| 56 | 0 | super(message); |
| 57 | 0 | } |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public Token currentToken; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
public int[][] expectedTokenSequences; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public String[] tokenImage; |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
private static String initialise(Token currentToken, |
| 89 | |
int[][] expectedTokenSequences, |
| 90 | |
String[] tokenImage) { |
| 91 | 1 | String eol = System.getProperty("line.separator", "\n"); |
| 92 | 1 | StringBuffer expected = new StringBuffer(); |
| 93 | 1 | int maxSize = 0; |
| 94 | 45 | for (int i = 0; i < expectedTokenSequences.length; i++) { |
| 95 | 44 | if (maxSize < expectedTokenSequences[i].length) { |
| 96 | 1 | maxSize = expectedTokenSequences[i].length; |
| 97 | |
} |
| 98 | 88 | for (int j = 0; j < expectedTokenSequences[i].length; j++) { |
| 99 | 44 | expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' '); |
| 100 | |
} |
| 101 | 44 | if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { |
| 102 | 43 | expected.append("..."); |
| 103 | |
} |
| 104 | 44 | expected.append(eol).append(" "); |
| 105 | |
} |
| 106 | 1 | String retval = "Encountered \""; |
| 107 | 1 | Token tok = currentToken.next; |
| 108 | 2 | for (int i = 0; i < maxSize; i++) { |
| 109 | 1 | if (i != 0) retval += " "; |
| 110 | 1 | if (tok.kind == 0) { |
| 111 | 0 | retval += tokenImage[0]; |
| 112 | 0 | break; |
| 113 | |
} |
| 114 | 1 | retval += " " + tokenImage[tok.kind]; |
| 115 | 1 | retval += " \""; |
| 116 | 1 | retval += add_escapes(tok.image); |
| 117 | 1 | retval += " \""; |
| 118 | 1 | tok = tok.next; |
| 119 | |
} |
| 120 | 1 | retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; |
| 121 | 1 | retval += "." + eol; |
| 122 | 1 | if (expectedTokenSequences.length == 1) { |
| 123 | 0 | retval += "Was expecting:" + eol + " "; |
| 124 | |
} else { |
| 125 | 1 | retval += "Was expecting one of:" + eol + " "; |
| 126 | |
} |
| 127 | 1 | retval += expected.toString(); |
| 128 | 1 | return retval; |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | 1 | protected String eol = System.getProperty("line.separator", "\n"); |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
static String add_escapes(String str) { |
| 142 | 1 | StringBuffer retval = new StringBuffer(); |
| 143 | |
char ch; |
| 144 | 2 | for (int i = 0; i < str.length(); i++) { |
| 145 | 1 | switch (str.charAt(i)) |
| 146 | |
{ |
| 147 | |
case 0 : |
| 148 | 0 | continue; |
| 149 | |
case '\b': |
| 150 | 0 | retval.append("\\b"); |
| 151 | 0 | continue; |
| 152 | |
case '\t': |
| 153 | 0 | retval.append("\\t"); |
| 154 | 0 | continue; |
| 155 | |
case '\n': |
| 156 | 0 | retval.append("\\n"); |
| 157 | 0 | continue; |
| 158 | |
case '\f': |
| 159 | 0 | retval.append("\\f"); |
| 160 | 0 | continue; |
| 161 | |
case '\r': |
| 162 | 0 | retval.append("\\r"); |
| 163 | 0 | continue; |
| 164 | |
case '\"': |
| 165 | 0 | retval.append("\\\""); |
| 166 | 0 | continue; |
| 167 | |
case '\'': |
| 168 | 0 | retval.append("\\\'"); |
| 169 | 0 | continue; |
| 170 | |
case '\\': |
| 171 | 0 | retval.append("\\\\"); |
| 172 | 0 | continue; |
| 173 | |
default: |
| 174 | 1 | if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { |
| 175 | 0 | String s = "0000" + Integer.toString(ch, 16); |
| 176 | 0 | retval.append("\\u" + s.substring(s.length() - 4, s.length())); |
| 177 | 0 | } else { |
| 178 | 1 | retval.append(ch); |
| 179 | |
} |
| 180 | |
continue; |
| 181 | |
} |
| 182 | |
} |
| 183 | 1 | return retval.toString(); |
| 184 | |
} |
| 185 | |
|
| 186 | |
} |
| 187 | |
/* JavaCC - OriginalChecksum=8b86d6ca771ad72d019c9024e473d350 (do not edit this line) */ |