001/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 0.7pre6 */ 002 003package jtb.cparser; 004 005/** 006 * This exception is thrown when parse errors are encountered. You can explicitly create objects of 007 * this exception type by calling the method generateParseException in the generated parser. 008 * 009 * <p>You can modify this class to customize your error reporting mechanisms so long as you retain 010 * the public fields. 011 */ 012@SuppressWarnings("serial") 013public class ParseException extends Exception { 014 static final long serialVersionUID = 20050923L; 015 016 /** 017 * This constructor is used by the method "generateParseException" in the generated parser. 018 * Calling this constructor generates a new object of this type with the fields "currentToken", 019 * "expectedTokenSequences", and "tokenImage" set. The boolean flag "specialConstructor" is also 020 * set to true to indicate that this constructor was used to create this object. This constructor 021 * calls its superclass with the empty string to force the "toString" method of parent class 022 * "Throwable" to print the error message in the form: ParseException: result_of_getMessage 023 */ 024 public ParseException( 025 Token currentTokenVal, int[][] expectedTokenSequencesVal, String[] tokenImageVal) { 026 super(""); 027 specialConstructor = true; 028 currentToken = currentTokenVal; 029 expectedTokenSequences = expectedTokenSequencesVal; 030 tokenImage = tokenImageVal; 031 } 032 033 /** 034 * The following constructors are for use by you for whatever purpose you can think of. 035 * Constructing the exception in this manner makes the exception behave in the normal way -- i.e., 036 * as documented in the class "Throwable". The fields "errorToken", "expectedTokenSequences", and 037 * "tokenImage" do not contain relevant information. The JavaCC generated code does not use these 038 * constructors. 039 */ 040 public ParseException() { 041 super(); 042 specialConstructor = false; 043 } 044 045 public ParseException(String message) { 046 super(message); 047 specialConstructor = false; 048 } 049 050 /** 051 * This variable determines which constructor was used to create this object and thereby affects 052 * the semantics of the "getMessage" method (see below). 053 */ 054 protected boolean specialConstructor; 055 056 /** 057 * This is the last token that has been consumed successfully. If this object has been created due 058 * to a parse error, the token followng this token will (therefore) be the first error token. 059 */ 060 public Token currentToken; 061 062 /** 063 * Each entry in this array is an array of integers. Each array of integers represents a sequence 064 * of tokens (by their ordinal values) that is expected at this point of the parse. 065 */ 066 public int[][] expectedTokenSequences; 067 068 /** 069 * This is a reference to the "tokenImage" array of the generated parser within which the parse 070 * error occurred. This array is defined in the generated ...Constants interface. 071 */ 072 public String[] tokenImage; 073 074 /** 075 * This method has the standard behavior when this object has been created using the standard 076 * constructors. Otherwise, it uses "currentToken" and "expectedTokenSequences" to generate a 077 * parse error message and returns it. If this object has been created due to a parse error, and 078 * you do not catch it (it gets thrown from the parser), then this method is called during the 079 * printing of the final stack trace, and hence the correct error message gets displayed. 080 */ 081 public String getMessage() { 082 if (!specialConstructor) { 083 return super.getMessage(); 084 } 085 String expected = ""; 086 int maxSize = 0; 087 for (int i = 0; i < expectedTokenSequences.length; i++) { 088 if (maxSize < expectedTokenSequences[i].length) { 089 maxSize = expectedTokenSequences[i].length; 090 } 091 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 092 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 093 } 094 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 095 expected += "..."; 096 } 097 expected += eol + " "; 098 } 099 String retval = "Encountered \""; 100 Token tok = currentToken.next; 101 for (int i = 0; i < maxSize; i++) { 102 if (i != 0) retval += " "; 103 if (tok.kind == 0) { 104 retval += tokenImage[0]; 105 break; 106 } 107 retval += add_escapes(tok.image); 108 tok = tok.next; 109 } 110 retval += 111 "\" at line " 112 + currentToken.next.beginLine 113 + ", column " 114 + currentToken.next.beginColumn 115 + "." 116 + eol; 117 if (expectedTokenSequences.length == 1) { 118 retval += "Was expecting:" + eol + " "; 119 } else { 120 retval += "Was expecting one of:" + eol + " "; 121 } 122 retval += expected; 123 return retval; 124 } 125 126 /** The end of line string for this machine. */ 127 protected String eol = System.getProperty("line.separator", "\n"); 128 129 /** 130 * Used to convert raw characters to their escaped version when these raw version cannot be used 131 * as part of an ASCII string literal. 132 */ 133 protected String add_escapes(String str) { 134 StringBuffer retval = new StringBuffer(); 135 char ch; 136 for (int i = 0; i < str.length(); i++) { 137 switch (str.charAt(i)) { 138 case 0: 139 continue; 140 case '\b': 141 retval.append("\\b"); 142 continue; 143 case '\t': 144 retval.append("\\t"); 145 continue; 146 case '\n': 147 retval.append("\\n"); 148 continue; 149 case '\f': 150 retval.append("\\f"); 151 continue; 152 case '\r': 153 retval.append("\\r"); 154 continue; 155 case '\"': 156 retval.append("\\\""); 157 continue; 158 case '\'': 159 retval.append("\\\'"); 160 continue; 161 case '\\': 162 retval.append("\\\\"); 163 continue; 164 default: 165 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 166 String s = "0000" + Integer.toString(ch, 16); 167 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 168 } else { 169 retval.append(ch); 170 } 171 continue; 172 } 173 } 174 return retval.toString(); 175 } 176}