001 /* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 0.7pre2 */ 002 003 /* 004 * Cobertura - http://cobertura.sourceforge.net/ 005 * 006 * This file was taken from JavaNCSS 007 * http://www.kclee.com/clemens/java/javancss/ 008 * Copyright (C) 2000 Chr. Clemens Lee <clemens a.t kclee d.o.t com> 009 * 010 * Cobertura is free software; you can redistribute it and/or modify 011 * it under the terms of the GNU General Public License as published 012 * by the Free Software Foundation; either version 2 of the License, 013 * or (at your option) any later version. 014 * 015 * Cobertura is distributed in the hope that it will be useful, but 016 * WITHOUT ANY WARRANTY; without even the implied warranty of 017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 018 * General Public License for more details. 019 * 020 * You should have received a copy of the GNU General Public License 021 * along with Cobertura; if not, write to the Free Software 022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 023 * USA 024 */ 025 026 package net.sourceforge.cobertura.javancss; 027 028 class TokenMgrError extends Error 029 { 030 031 private static final long serialVersionUID = 0L; 032 033 /* 034 * Ordinals for various reasons why an Error of this type can be thrown. 035 */ 036 037 /** 038 * Lexical error occured. 039 */ 040 static final int LEXICAL_ERROR = 0; 041 042 /** 043 * An attempt wass made to create a second instance of a static token manager. 044 */ 045 static final int STATIC_LEXER_ERROR = 1; 046 047 /** 048 * Tried to change to an invalid lexical state. 049 */ 050 static final int INVALID_LEXICAL_STATE = 2; 051 052 /** 053 * Detected (and bailed out of) an infinite loop in the token manager. 054 */ 055 static final int LOOP_DETECTED = 3; 056 057 /** 058 * Indicates the reason why the exception is thrown. It will have 059 * one of the above 4 values. 060 */ 061 int errorCode; 062 063 /** 064 * Replaces unprintable characters by their espaced (or unicode escaped) 065 * equivalents in the given string 066 */ 067 static final String addEscapes(String str) 068 { 069 StringBuffer retval = new StringBuffer(); 070 char ch; 071 for (int i = 0; i < str.length(); i++) 072 { 073 switch (str.charAt(i)) 074 { 075 case 0: 076 continue; 077 case '\b': 078 retval.append("\\b"); 079 continue; 080 case '\t': 081 retval.append("\\t"); 082 continue; 083 case '\n': 084 retval.append("\\n"); 085 continue; 086 case '\f': 087 retval.append("\\f"); 088 continue; 089 case '\r': 090 retval.append("\\r"); 091 continue; 092 case '\"': 093 retval.append("\\\""); 094 continue; 095 case '\'': 096 retval.append("\\\'"); 097 continue; 098 case '\\': 099 retval.append("\\\\"); 100 continue; 101 default: 102 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) 103 { 104 String s = "0000" + Integer.toString(ch, 16); 105 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 106 } 107 else 108 { 109 retval.append(ch); 110 } 111 continue; 112 } 113 } 114 return retval.toString(); 115 } 116 117 /** 118 * Returns a detailed message for the Error when it is thrown by the 119 * token manager to indicate a lexical error. 120 * Parameters : 121 * EOFSeen : indicates if EOF caused the lexicl error 122 * curLexState : lexical state in which this error occured 123 * errorLine : line number when the error occured 124 * errorColumn : column number when the error occured 125 * errorAfter : prefix that was seen before this error occured 126 * curchar : the offending character 127 * Note: You can customize the lexical error message by modifying this method. 128 */ 129 private static final String LexicalError(boolean EOFSeen, int errorLine, int errorColumn, 130 String errorAfter, char curChar) 131 { 132 return ("Lexical error at line " 133 + errorLine 134 + ", column " 135 + errorColumn 136 + ". Encountered: " 137 + (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" 138 + (int)curChar + "), ") + "after : \"" + addEscapes(errorAfter) + "\""); 139 } 140 141 /** 142 * You can also modify the body of this method to customize your error messages. 143 * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not 144 * of end-users concern, so you can return something like : 145 * 146 * "Internal Error : Please file a bug report .... " 147 * 148 * from this method for such cases in the release version of your parser. 149 */ 150 public String getMessage() 151 { 152 return super.getMessage(); 153 } 154 155 /* 156 * Constructors of various flavors follow. 157 */ 158 159 TokenMgrError() 160 { 161 } 162 163 TokenMgrError(String message, int reason) 164 { 165 super(message); 166 errorCode = reason; 167 } 168 169 TokenMgrError(boolean EOFSeen, int errorLine, int errorColumn, String errorAfter, char curChar, 170 int reason) 171 { 172 this(LexicalError(EOFSeen, errorLine, errorColumn, errorAfter, curChar), reason); 173 } 174 }