1 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 6.1 */ 2 /* JavaCCOptions:KEEP_LINE_COLUMN=true */ 3 /* 4 * 5 * This file is part of Java 1.8 parser and Abstract Syntax Tree. 6 * 7 * Java 1.8 parser and Abstract Syntax Tree is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU Lesser General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * You should have received a copy of the GNU Lesser General Public License 13 * along with Java 1.8 parser and Abstract Syntax Tree. If not, see <http://www.gnu.org/licenses/>. 14 */ 15 package com.github.javaparser; 16 17 /** 18 * This exception is thrown when parse errors are encountered. 19 * You can explicitly create objects of this exception type by 20 * calling the method generateParseException in the generated 21 * parser. 22 * 23 * You can modify this class to customize your error reporting 24 * mechanisms so long as you retain the public fields. 25 */ 26 public class ParseException extends Exception { 27 28 /** 29 * The version identifier for this Serializable class. 30 * Increment only if the <i>serialized</i> form of the 31 * class changes. 32 */ 33 private static final long serialVersionUID = 1L; 34 35 private static final String INDENT = " "; 36 37 /** 38 * The end of line string (we do not use System.getProperty("") so that we are compatible with Android/GWT); 39 */ 40 protected static String EOL = "\n"; 41 42 43 public ParseException(Token currentTokenVal, 44 int[][] expectedTokenSequencesVal, 45 String[] tokenImageVal 46 ) 47 { 48 this (currentTokenVal, expectedTokenSequencesVal, tokenImageVal, null); 49 } 50 51 52 /** 53 * This constructor is used by the method "generateParseException" 54 * in the generated parser. Calling this constructor generates 55 * a new object of this type with the fields "currentToken", 56 * "expectedTokenSequences", and "tokenImage" set. 57 */ 58 public ParseException(Token currentTokenVal, 59 int[][] expectedTokenSequencesVal, 60 String[] tokenImageVal, 61 String lexicalStateName 62 ) 63 { 64 super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal, lexicalStateName)); 65 currentToken = currentTokenVal; 66 expectedTokenSequences = expectedTokenSequencesVal; 67 tokenImage = tokenImageVal; 68 } 69 70 /** 71 * The following constructors are for use by you for whatever 72 * purpose you can think of. Constructing the exception in this 73 * manner makes the exception behave in the normal way - i.e., as 74 * documented in the class "Throwable". The fields "errorToken", 75 * "expectedTokenSequences", and "tokenImage" do not contain 76 * relevant information. The JavaCC generated code does not use 77 * these constructors. 78 */ 79 80 public ParseException() { 81 super(); 82 } 83 84 /** Constructor with message. */ 85 public ParseException(String message) { 86 super(message); 87 } 88 89 90 /** 91 * This is the last token that has been consumed successfully. If 92 * this object has been created due to a parse error, the token 93 * followng this token will (therefore) be the first error token. 94 */ 95 public Token currentToken; 96 97 /** 98 * Each entry in this array is an array of integers. Each array 99 * of integers represents a sequence of tokens (by their ordinal 100 * values) that is expected at this point of the parse. 101 */ 102 public int[][] expectedTokenSequences; 103 104 /** 105 * This is a reference to the "tokenImage" array of the generated 106 * parser within which the parse error occurred. This array is 107 * defined in the generated ...Constants interface. 108 */ 109 public String[] tokenImage; 110 111 /** 112 * It uses "currentToken" and "expectedTokenSequences" to generate a parse 113 * error message and returns it. If this object has been created 114 * due to a parse error, and you do not catch it (it gets thrown 115 * from the parser) the correct error message 116 * gets displayed. 117 */ 118 private static String initialise(Token currentToken, 119 int[][] expectedTokenSequences, 120 String[] tokenImage, 121 String lexicalStateName) { 122 StringBuilder sb = new StringBuilder(); 123 StringBuffer expected = new StringBuffer(); 124 125 int maxSize = 0; 126 java.util.TreeSet<String> sortedOptions = new java.util.TreeSet<String>(); 127 for (int i = 0; i < expectedTokenSequences.length; i++) { 128 if (maxSize < expectedTokenSequences[i].length) { 129 maxSize = expectedTokenSequences[i].length; 130 } 131 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 132 sortedOptions.add(tokenImage[expectedTokenSequences[i][j]]); 133 } 134 } 135 136 for (String option : sortedOptions) { 137 expected.append(INDENT).append(option).append(EOL); 138 } 139 140 sb.append("Encountered unexpected token:"); 141 142 Token tok = currentToken.next; 143 for (int i = 0; i < maxSize; i++) { 144 String tokenText = tok.image; 145 String escapedTokenText = add_escapes(tokenText); 146 if (i != 0) { 147 sb.append(" "); 148 } 149 if (tok.kind == 0) { 150 sb.append(tokenImage[0]); 151 break; 152 } 153 sb.append(" \""); 154 sb.append(escapedTokenText); 155 sb.append("\""); 156 sb.append(" " + tokenImage[tok.kind]); 157 tok = tok.next; 158 } 159 sb.append(EOL).append(INDENT).append("at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn); 160 sb.append(".").append(EOL); 161 162 if (expectedTokenSequences.length == 0) { 163 // Nothing to add here 164 } else { 165 int numExpectedTokens = expectedTokenSequences.length; 166 sb.append(EOL).append("Was expecting"+ (numExpectedTokens == 1 ? ":" : " one of:") + EOL + EOL); 167 sb.append(expected.toString()); 168 } 169 // 2013/07/30 --> Seems to be inaccurate as represents the readahead state, not the lexical state BEFORE the unknown token 170 // if (lexicalStateName != null) { 171 // sb.append(EOL).append("** Lexical State : ").append(lexicalStateName).append(EOL).append(EOL); 172 // } 173 174 return sb.toString(); 175 } 176 177 178 /** 179 * Used to convert raw characters to their escaped version 180 * when these raw version cannot be used as part of an ASCII 181 * string literal. 182 */ 183 static String add_escapes(String str) { 184 StringBuffer retval = new StringBuffer(); 185 char ch; 186 for (int i = 0; i < str.length(); i++) { 187 switch (str.charAt(i)) 188 { 189 case '\b': 190 retval.append("\\b"); 191 continue; 192 case '\t': 193 retval.append("\\t"); 194 continue; 195 case '\n': 196 retval.append("\\n"); 197 continue; 198 case '\f': 199 retval.append("\\f"); 200 continue; 201 case '\r': 202 retval.append("\\r"); 203 continue; 204 case '\"': 205 retval.append("\\\""); 206 continue; 207 case '\'': 208 retval.append("\\\'"); 209 continue; 210 case '\\': 211 retval.append("\\\\"); 212 continue; 213 default: 214 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 215 String s = "0000" + Integer.toString(ch, 16); 216 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 217 } else { 218 retval.append(ch); 219 } 220 continue; 221 } 222 } 223 return retval.toString(); 224 } 225 226 } 227 /* JavaCC - OriginalChecksum=c0da6a86ac0d0ec8b633aeafa43e6e37 (do not edit this line) */ 228