001 /* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 5.0 */ 002 /* JavaCCOptions: */ 003 /* 004 * Copyright (c) 2009 The JOMC Project 005 * Copyright (c) 2005 Christian Schulte <cs@jomc.org> 006 * All rights reserved. 007 * 008 * Redistribution and use in source and binary forms, with or without 009 * modification, are permitted provided that the following conditions 010 * are met: 011 * 012 * o Redistributions of source code must retain the above copyright 013 * notice, this list of conditions and the following disclaimer. 014 * 015 * o Redistributions in binary form must reproduce the above copyright 016 * notice, this list of conditions and the following disclaimer in 017 * the documentation and/or other materials provided with the 018 * distribution. 019 * 020 * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS" 021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 022 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 023 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR 024 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 027 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 028 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 029 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 030 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 031 * 032 * $Id: VersionParser.jj 733 2009-10-05 17:22:57Z schulte2005 $ 033 * 034 */ 035 package org.jomc.util; 036 037 /** Token Manager Error. */ 038 public class TokenMgrError extends Error 039 { 040 041 /** 042 * The version identifier for this Serializable class. 043 * Increment only if the <i>serialized</i> form of the 044 * class changes. 045 */ 046 private static final long serialVersionUID = 1L; 047 048 /* 049 * Ordinals for various reasons why an Error of this type can be thrown. 050 */ 051 052 /** 053 * Lexical error occurred. 054 */ 055 static final int LEXICAL_ERROR = 0; 056 057 /** 058 * An attempt was made to create a second instance of a static token manager. 059 */ 060 static final int STATIC_LEXER_ERROR = 1; 061 062 /** 063 * Tried to change to an invalid lexical state. 064 */ 065 static final int INVALID_LEXICAL_STATE = 2; 066 067 /** 068 * Detected (and bailed out of) an infinite loop in the token manager. 069 */ 070 static final int LOOP_DETECTED = 3; 071 072 /** 073 * Indicates the reason why the exception is thrown. It will have 074 * one of the above 4 values. 075 */ 076 int errorCode; 077 078 /** 079 * Replaces unprintable characters by their escaped (or unicode escaped) 080 * equivalents in the given string 081 */ 082 protected static final String addEscapes(String str) { 083 StringBuffer retval = new StringBuffer(); 084 char ch; 085 for (int i = 0; i < str.length(); i++) { 086 switch (str.charAt(i)) 087 { 088 case 0 : 089 continue; 090 case '\b': 091 retval.append("\\b"); 092 continue; 093 case '\t': 094 retval.append("\\t"); 095 continue; 096 case '\n': 097 retval.append("\\n"); 098 continue; 099 case '\f': 100 retval.append("\\f"); 101 continue; 102 case '\r': 103 retval.append("\\r"); 104 continue; 105 case '\"': 106 retval.append("\\\""); 107 continue; 108 case '\'': 109 retval.append("\\\'"); 110 continue; 111 case '\\': 112 retval.append("\\\\"); 113 continue; 114 default: 115 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 116 String s = "0000" + Integer.toString(ch, 16); 117 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 118 } else { 119 retval.append(ch); 120 } 121 continue; 122 } 123 } 124 return retval.toString(); 125 } 126 127 /** 128 * Returns a detailed message for the Error when it is thrown by the 129 * token manager to indicate a lexical error. 130 * Parameters : 131 * EOFSeen : indicates if EOF caused the lexical error 132 * curLexState : lexical state in which this error occurred 133 * errorLine : line number when the error occurred 134 * errorColumn : column number when the error occurred 135 * errorAfter : prefix that was seen before this error occurred 136 * curchar : the offending character 137 * Note: You can customize the lexical error message by modifying this method. 138 */ 139 protected static String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar) { 140 return("Lexical error at line " + 141 errorLine + ", column " + 142 errorColumn + ". Encountered: " + 143 (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") + 144 "after : \"" + addEscapes(errorAfter) + "\""); 145 } 146 147 /** 148 * You can also modify the body of this method to customize your error messages. 149 * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not 150 * of end-users concern, so you can return something like : 151 * 152 * "Internal Error : Please file a bug report .... " 153 * 154 * from this method for such cases in the release version of your parser. 155 */ 156 public String getMessage() { 157 return super.getMessage(); 158 } 159 160 /* 161 * Constructors of various flavors follow. 162 */ 163 164 /** No arg constructor. */ 165 public TokenMgrError() { 166 } 167 168 /** Constructor with message and reason. */ 169 public TokenMgrError(String message, int reason) { 170 super(message); 171 errorCode = reason; 172 } 173 174 /** Full Constructor. */ 175 public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) { 176 this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason); 177 } 178 } 179 /* JavaCC - OriginalChecksum=98b448fe49b26fa161375304fd2dafe3 (do not edit this line) */