001    /* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 4.1 */
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 402 2009-08-25 13:51:07Z schulte2005 $
033     *
034     */
035    package org.jomc.util;
036    
037    /** Token Manager Error. */
038    public class TokenMgrError extends Error
039    {
040    
041       /*
042        * Ordinals for various reasons why an Error of this type can be thrown.
043        */
044    
045       /**
046        * Lexical error occurred.
047        */
048       static final int LEXICAL_ERROR = 0;
049    
050       /**
051        * An attempt was made to create a second instance of a static token manager.
052        */
053       static final int STATIC_LEXER_ERROR = 1;
054    
055       /**
056        * Tried to change to an invalid lexical state.
057        */
058       static final int INVALID_LEXICAL_STATE = 2;
059    
060       /**
061        * Detected (and bailed out of) an infinite loop in the token manager.
062        */
063       static final int LOOP_DETECTED = 3;
064    
065       /**
066        * Indicates the reason why the exception is thrown. It will have
067        * one of the above 4 values.
068        */
069       int errorCode;
070    
071       /**
072        * Replaces unprintable characters by their escaped (or unicode escaped)
073        * equivalents in the given string
074        */
075       protected static final String addEscapes(String str) {
076          StringBuffer retval = new StringBuffer();
077          char ch;
078          for (int i = 0; i < str.length(); i++) {
079            switch (str.charAt(i))
080            {
081               case 0 :
082                  continue;
083               case '\b':
084                  retval.append("\\b");
085                  continue;
086               case '\t':
087                  retval.append("\\t");
088                  continue;
089               case '\n':
090                  retval.append("\\n");
091                  continue;
092               case '\f':
093                  retval.append("\\f");
094                  continue;
095               case '\r':
096                  retval.append("\\r");
097                  continue;
098               case '\"':
099                  retval.append("\\\"");
100                  continue;
101               case '\'':
102                  retval.append("\\\'");
103                  continue;
104               case '\\':
105                  retval.append("\\\\");
106                  continue;
107               default:
108                  if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
109                     String s = "0000" + Integer.toString(ch, 16);
110                     retval.append("\\u" + s.substring(s.length() - 4, s.length()));
111                  } else {
112                     retval.append(ch);
113                  }
114                  continue;
115            }
116          }
117          return retval.toString();
118       }
119    
120       /**
121        * Returns a detailed message for the Error when it is thrown by the
122        * token manager to indicate a lexical error.
123        * Parameters :
124        *    EOFSeen     : indicates if EOF caused the lexical error
125        *    curLexState : lexical state in which this error occurred
126        *    errorLine   : line number when the error occurred
127        *    errorColumn : column number when the error occurred
128        *    errorAfter  : prefix that was seen before this error occurred
129        *    curchar     : the offending character
130        * Note: You can customize the lexical error message by modifying this method.
131        */
132       protected static String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar) {
133          return("Lexical error at line " +
134               errorLine + ", column " +
135               errorColumn + ".  Encountered: " +
136               (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") +
137               "after : \"" + addEscapes(errorAfter) + "\"");
138       }
139    
140       /**
141        * You can also modify the body of this method to customize your error messages.
142        * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not
143        * of end-users concern, so you can return something like :
144        *
145        *     "Internal Error : Please file a bug report .... "
146        *
147        * from this method for such cases in the release version of your parser.
148        */
149       public String getMessage() {
150          return super.getMessage();
151       }
152    
153       /*
154        * Constructors of various flavors follow.
155        */
156    
157       /** No arg constructor. */
158       public TokenMgrError() {
159       }
160    
161       /** Constructor with message and reason. */
162       public TokenMgrError(String message, int reason) {
163          super(message);
164          errorCode = reason;
165       }
166    
167       /** Full Constructor. */
168       public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) {
169          this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
170       }
171    }
172    /* JavaCC - OriginalChecksum=b4bd559961d67684bc0bda3998be536f (do not edit this line) */