1 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 5.0 */ 2 /* JavaCCOptions:KEEP_LINE_COL=null */ 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 /** 36 * This constructor is used by the method "generateParseException" 37 * in the generated parser. Calling this constructor generates 38 * a new object of this type with the fields "currentToken", 39 * "expectedTokenSequences", and "tokenImage" set. 40 */ 41 public ParseException(Token currentTokenVal, 42 int[][] expectedTokenSequencesVal, 43 String[] tokenImageVal 44 ) 45 { 46 super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal)); 47 currentToken = currentTokenVal; 48 expectedTokenSequences = expectedTokenSequencesVal; 49 tokenImage = tokenImageVal; 50 } 51 52 /** 53 * The following constructors are for use by you for whatever 54 * purpose you can think of. Constructing the exception in this 55 * manner makes the exception behave in the normal way - i.e., as 56 * documented in the class "Throwable". The fields "errorToken", 57 * "expectedTokenSequences", and "tokenImage" do not contain 58 * relevant information. The JavaCC generated code does not use 59 * these constructors. 60 */ 61 62 public ParseException() { 63 super(); 64 } 65 66 /** Constructor with message. */ 67 public ParseException(String message) { 68 super(message); 69 } 70 71 72 /** 73 * This is the last token that has been consumed successfully. If 74 * this object has been created due to a parse error, the token 75 * followng this token will (therefore) be the first error token. 76 */ 77 public Token currentToken; 78 79 /** 80 * Each entry in this array is an array of integers. Each array 81 * of integers represents a sequence of tokens (by their ordinal 82 * values) that is expected at this point of the parse. 83 */ 84 public int[][] expectedTokenSequences; 85 86 /** 87 * This is a reference to the "tokenImage" array of the generated 88 * parser within which the parse error occurred. This array is 89 * defined in the generated ...Constants interface. 90 */ 91 public String[] tokenImage; 92 93 /** 94 * It uses "currentToken" and "expectedTokenSequences" to generate a parse 95 * error message and returns it. If this object has been created 96 * due to a parse error, and you do not catch it (it gets thrown 97 * from the parser) the correct error message 98 * gets displayed. 99 */ 100 private static String initialise(Token currentToken, 101 int[][] expectedTokenSequences, 102 String[] tokenImage) { 103 String eol = System.getProperty("line.separator", "\n"); 104 StringBuffer expected = new StringBuffer(); 105 int maxSize = 0; 106 for (int i = 0; i < expectedTokenSequences.length; i++) { 107 if (maxSize < expectedTokenSequences[i].length) { 108 maxSize = expectedTokenSequences[i].length; 109 } 110 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 111 expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' '); 112 } 113 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 114 expected.append("..."); 115 } 116 expected.append(eol).append(" "); 117 } 118 String retval = "Encountered \""; 119 Token tok = currentToken.next; 120 for (int i = 0; i < maxSize; i++) { 121 if (i != 0) retval += " "; 122 if (tok.kind == 0) { 123 retval += tokenImage[0]; 124 break; 125 } 126 retval += " " + tokenImage[tok.kind]; 127 retval += " \""; 128 retval += add_escapes(tok.image); 129 retval += " \""; 130 tok = tok.next; 131 } 132 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 133 retval += "." + eol; 134 if (expectedTokenSequences.length == 1) { 135 retval += "Was expecting:" + eol + " "; 136 } else { 137 retval += "Was expecting one of:" + eol + " "; 138 } 139 retval += expected.toString(); 140 return retval; 141 } 142 143 /** 144 * The end of line string for this machine. 145 */ 146 protected String eol = System.getProperty("line.separator", "\n"); 147 148 /** 149 * Used to convert raw characters to their escaped version 150 * when these raw version cannot be used as part of an ASCII 151 * string literal. 152 */ 153 static String add_escapes(String str) { 154 StringBuffer retval = new StringBuffer(); 155 char ch; 156 for (int i = 0; i < str.length(); i++) { 157 switch (str.charAt(i)) 158 { 159 case 0 : 160 continue; 161 case '\b': 162 retval.append("\\b"); 163 continue; 164 case '\t': 165 retval.append("\\t"); 166 continue; 167 case '\n': 168 retval.append("\\n"); 169 continue; 170 case '\f': 171 retval.append("\\f"); 172 continue; 173 case '\r': 174 retval.append("\\r"); 175 continue; 176 case '\"': 177 retval.append("\\\""); 178 continue; 179 case '\'': 180 retval.append("\\\'"); 181 continue; 182 case '\\': 183 retval.append("\\\\"); 184 continue; 185 default: 186 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 187 String s = "0000" + Integer.toString(ch, 16); 188 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 189 } else { 190 retval.append(ch); 191 } 192 continue; 193 } 194 } 195 return retval.toString(); 196 } 197 198 } 199 /* JavaCC - OriginalChecksum=62ba72b2159703420d5ce7232a0226fb (do not edit this line) */ 200