001/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 5.0 */ 002/* JavaCCOptions:KEEP_LINE_COL=null */ 003/* 004 005 Copyright (c) 1998-2008 The Regents of the University of California. 006 All rights reserved. 007 Permission is hereby granted, without written agreement and without 008 license or royalty fees, to use, copy, modify, and distribute this 009 software and its documentation for any purpose, provided that the above 010 copyright notice and the following two paragraphs appear in all copies 011 of this software. 012 013 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 014 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 015 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 016 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 017 SUCH DAMAGE. 018 019 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 020 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 021 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 022 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 023 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 024 ENHANCEMENTS, OR MODIFICATIONS. 025 026 PT_COPYRIGHT_VERSION_3 027 COPYRIGHTENDKEY 028 */ 029 030package ptolemy.moml.unit; 031 032/** 033 * This exception is thrown when parse errors are encountered. 034 * You can explicitly create objects of this exception type by 035 * calling the method generateParseException in the generated 036 * parser. 037 * 038 * You can modify this class to customize your error reporting 039 * mechanisms so long as you retain the public fields. 040 */ 041public class ParseException extends Exception { 042 043 /** 044 * The version identifier for this Serializable class. 045 * Increment only if the <i>serialized</i> form of the 046 * class changes. 047 */ 048 private static final long serialVersionUID = 1L; 049 050 /** 051 * This constructor is used by the method "generateParseException" 052 * in the generated parser. Calling this constructor generates 053 * a new object of this type with the fields "currentToken", 054 * "expectedTokenSequences", and "tokenImage" set. 055 */ 056 public ParseException(Token currentTokenVal, 057 int[][] expectedTokenSequencesVal, String[] tokenImageVal) { 058 super(initialise(currentTokenVal, expectedTokenSequencesVal, 059 tokenImageVal)); 060 currentToken = currentTokenVal; 061 expectedTokenSequences = expectedTokenSequencesVal; 062 tokenImage = tokenImageVal; 063 } 064 065 /** 066 * The following constructors are for use by you for whatever 067 * purpose you can think of. Constructing the exception in this 068 * manner makes the exception behave in the normal way - i.e., as 069 * documented in the class "Throwable". The fields "errorToken", 070 * "expectedTokenSequences", and "tokenImage" do not contain 071 * relevant information. The JavaCC generated code does not use 072 * these constructors. 073 */ 074 075 public ParseException() { 076 super(); 077 } 078 079 /** Constructor with message. */ 080 public ParseException(String message) { 081 super(message); 082 } 083 084 /** 085 * This is the last token that has been consumed successfully. If 086 * this object has been created due to a parse error, the token 087 * following this token will (therefore) be the first error token. 088 */ 089 public Token currentToken; 090 091 /** 092 * Each entry in this array is an array of integers. Each array 093 * of integers represents a sequence of tokens (by their ordinal 094 * values) that is expected at this point of the parse. 095 */ 096 public int[][] expectedTokenSequences; 097 098 /** 099 * This is a reference to the "tokenImage" array of the generated 100 * parser within which the parse error occurred. This array is 101 * defined in the generated ...Constants interface. 102 */ 103 public String[] tokenImage; 104 105 /** 106 * It uses "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) the correct error message 110 * gets displayed. 111 */ 112 private static String initialise(Token currentToken, 113 int[][] expectedTokenSequences, String[] tokenImage) { 114 String eol = System.getProperty("line.separator", "\n"); 115 StringBuffer expected = new StringBuffer(); 116 int maxSize = 0; 117 for (int[] expectedTokenSequence : expectedTokenSequences) { 118 if (maxSize < expectedTokenSequence.length) { 119 maxSize = expectedTokenSequence.length; 120 } 121 for (int j = 0; j < expectedTokenSequence.length; j++) { 122 expected.append(tokenImage[expectedTokenSequence[j]]) 123 .append(' '); 124 } 125 if (expectedTokenSequence[expectedTokenSequence.length - 1] != 0) { 126 expected.append("..."); 127 } 128 expected.append(eol).append(" "); 129 } 130 String retval = "Encountered \""; 131 Token tok = currentToken.next; 132 for (int i = 0; i < maxSize; i++) { 133 if (i != 0) { 134 retval += " "; 135 } 136 if (tok.kind == 0) { 137 retval += tokenImage[0]; 138 break; 139 } 140 retval += " " + tokenImage[tok.kind]; 141 retval += " \""; 142 retval += add_escapes(tok.image); 143 retval += " \""; 144 tok = tok.next; 145 } 146 retval += "\" at line " + currentToken.next.beginLine + ", column " 147 + currentToken.next.beginColumn; 148 retval += "." + eol; 149 if (expectedTokenSequences.length == 1) { 150 retval += "Was expecting:" + eol + " "; 151 } else { 152 retval += "Was expecting one of:" + eol + " "; 153 } 154 retval += expected.toString(); 155 return retval; 156 } 157 158 /** 159 * The end of line string for this machine. 160 */ 161 protected String eol = System.getProperty("line.separator", "\n"); 162 163 /** 164 * Used to convert raw characters to their escaped version 165 * when these raw version cannot be used as part of an ASCII 166 * string literal. 167 */ 168 static String add_escapes(String str) { 169 StringBuffer retval = new StringBuffer(); 170 char ch; 171 for (int i = 0; i < str.length(); i++) { 172 switch (str.charAt(i)) { 173 case 0: 174 continue; 175 case '\b': 176 retval.append("\\b"); 177 continue; 178 case '\t': 179 retval.append("\\t"); 180 continue; 181 case '\n': 182 retval.append("\\n"); 183 continue; 184 case '\f': 185 retval.append("\\f"); 186 continue; 187 case '\r': 188 retval.append("\\r"); 189 continue; 190 case '\"': 191 retval.append("\\\""); 192 continue; 193 case '\'': 194 retval.append("\\\'"); 195 continue; 196 case '\\': 197 retval.append("\\\\"); 198 continue; 199 default: 200 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 201 String s = "0000" + Integer.toString(ch, 16); 202 retval.append( 203 "\\u" + s.substring(s.length() - 4, s.length())); 204 } else { 205 retval.append(ch); 206 } 207 continue; 208 } 209 } 210 return retval.toString(); 211 } 212 213} 214/* JavaCC - OriginalChecksum=2333f322a92ee601dd8667ddea6f3541 (do not edit this line) */