001/*
002 * Copyright 2006 - 2013
003 *     Stefan Balev     <stefan.balev@graphstream-project.org>
004 *     Julien Baudry    <julien.baudry@graphstream-project.org>
005 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
006 *     Yoann Pigné      <yoann.pigne@graphstream-project.org>
007 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
008 * 
009 * This file is part of GraphStream <http://graphstream-project.org>.
010 * 
011 * GraphStream is a library whose purpose is to handle static or dynamic
012 * graph, create them from scratch, file or any source and display them.
013 * 
014 * This program is free software distributed under the terms of two licenses, the
015 * CeCILL-C license that fits European law, and the GNU Lesser General Public
016 * License. You can  use, modify and/ or redistribute the software under the terms
017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
019 * the Free Software Foundation, either version 3 of the License, or (at your
020 * option) any later version.
021 * 
022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
024 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
025 * 
026 * You should have received a copy of the GNU Lesser General Public License
027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
028 * 
029 * The fact that you are presently reading this means that you have had
030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
031 */
032package org.graphstream.util.parser;
033
034/** Token Manager Error. */
035public class TokenMgrError extends Error {
036
037        /**
038         * The version identifier for this Serializable class. Increment only if the
039         * <i>serialized</i> form of the class changes.
040         */
041        private static final long serialVersionUID = 1L;
042
043        /*
044         * Ordinals for various reasons why an Error of this type can be thrown.
045         */
046
047        /**
048         * Lexical error occurred.
049         */
050        public static final int LEXICAL_ERROR = 0;
051
052        /**
053         * An attempt was made to create a second instance of a static token
054         * manager.
055         */
056        public static final int STATIC_LEXER_ERROR = 1;
057
058        /**
059         * Tried to change to an invalid lexical state.
060         */
061        public static final int INVALID_LEXICAL_STATE = 2;
062
063        /**
064         * Detected (and bailed out of) an infinite loop in the token manager.
065         */
066        public static final int LOOP_DETECTED = 3;
067
068        /**
069         * Indicates the reason why the exception is thrown. It will have one of the
070         * above 4 values.
071         */
072        int errorCode;
073
074        /**
075         * Replaces unprintable characters by their escaped (or unicode escaped)
076         * equivalents in the given string
077         */
078        protected static final String addEscapes(String str) {
079                StringBuffer retval = new StringBuffer();
080                char ch;
081                for (int i = 0; i < str.length(); i++) {
082                        switch (str.charAt(i)) {
083                        case 0:
084                                continue;
085                        case '\b':
086                                retval.append("\\b");
087                                continue;
088                        case '\t':
089                                retval.append("\\t");
090                                continue;
091                        case '\n':
092                                retval.append("\\n");
093                                continue;
094                        case '\f':
095                                retval.append("\\f");
096                                continue;
097                        case '\r':
098                                retval.append("\\r");
099                                continue;
100                        case '\"':
101                                retval.append("\\\"");
102                                continue;
103                        case '\'':
104                                retval.append("\\\'");
105                                continue;
106                        case '\\':
107                                retval.append("\\\\");
108                                continue;
109                        default:
110                                if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
111                                        String s = "0000" + Integer.toString(ch, 16);
112                                        retval.append("\\u"
113                                                        + s.substring(s.length() - 4, s.length()));
114                                } else {
115                                        retval.append(ch);
116                                }
117                                continue;
118                        }
119                }
120                return retval.toString();
121        }
122
123        /**
124         * Returns a detailed message for the Error when it is thrown by the token
125         * manager to indicate a lexical error. Parameters : EOFSeen : indicates if
126         * EOF caused the lexical error curLexState : lexical state in which this
127         * error occurred errorLine : line number when the error occurred
128         * errorColumn : column number when the error occurred errorAfter : prefix
129         * that was seen before this error occurred curchar : the offending
130         * character Note: You can customize the lexical error message by modifying
131         * this method.
132         */
133        protected static String LexicalError(boolean EOFSeen, int lexState,
134                        int errorLine, int errorColumn, String errorAfter, char curChar) {
135                return ("Lexical error at line "
136                                + errorLine
137                                + ", column "
138                                + errorColumn
139                                + ".  Encountered: "
140                                + (EOFSeen ? "<EOF> " : ("\""
141                                                + addEscapes(String.valueOf(curChar)) + "\"")
142                                                + " (" + (int) curChar + "), ") + "after : \""
143                                + addEscapes(errorAfter) + "\"");
144        }
145
146        /**
147         * You can also modify the body of this method to customize your error
148         * messages. For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE
149         * are not of end-users concern, so you can return something like :
150         * 
151         * "Internal Error : Please file a bug report .... "
152         * 
153         * from this method for such cases in the release version of your parser.
154         */
155        public String getMessage() {
156                return super.getMessage();
157        }
158
159        /*
160         * Constructors of various flavors follow.
161         */
162
163        /** No arg constructor. */
164        public TokenMgrError() {
165        }
166
167        /** Constructor with message and reason. */
168        public TokenMgrError(String message, int reason) {
169                super(message);
170                errorCode = reason;
171        }
172
173        /** Full Constructor. */
174        public TokenMgrError(boolean EOFSeen, int lexState, int errorLine,
175                        int errorColumn, String errorAfter, char curChar, int reason) {
176                this(LexicalError(EOFSeen, lexState, errorLine, errorColumn,
177                                errorAfter, curChar), reason);
178        }
179}
180/*
181 * JavaCC - OriginalChecksum=43ce61356e7f4e29ca35693bd6385bae (do not edit this
182 * line)
183 */