001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.jexl2.parser;
018
019/**
020 * Token Manager Error.
021 */
022public class TokenMgrError extends Error {
023    /**
024     * The version identifier for this Serializable class.
025     * Increment only if the <i>serialized</i> form of the
026     * class changes.
027     */
028    private static final long serialVersionUID = 1L;
029
030    /*
031     * Ordinals for various reasons why an Error of this type can be thrown.
032     */
033    /**
034     * Lexical error occurred.
035     */
036    public static final int LEXICAL_ERROR = 0;
037    /**
038     * An attempt was made to create a second instance of a static token manager.
039     */
040    public static final int STATIC_LEXER_ERROR = 1;
041    /**
042     * Tried to change to an invalid lexical state.
043     */
044    public static final int INVALID_LEXICAL_STATE = 2;
045    /**
046     * Detected (and bailed out of) an infinite loop in the token manager.
047     */
048    public static final int LOOP_DETECTED = 3;
049    /**
050     * Indicates the reason why the exception is thrown. It will have
051     * one of the above 4 values.
052     */
053    private int errorCode;
054    /**
055     * The lexer state.
056     */
057    @SuppressWarnings("unused") // not read currently
058    private int state;
059    /**
060     * The current character.
061     */
062    private char current;
063    /**
064     * Last correct input before error occurs.
065     */
066    private String after;
067    /**
068     * 
069     */
070    private boolean eof;
071    /**
072     * Error line.
073     */
074    private int line;
075    /**
076     * Error column.
077     */
078    private int column;
079    
080    /**
081     * Gets the reason why the exception is thrown.
082     * @return one of the 4 lexical error codes
083     */
084    public int getErrorCode() {
085        return errorCode;
086    }
087
088    /**
089     * Gets the line number.
090     * @return line number.
091     */
092    public int getLine() {
093        return line;
094    }
095
096    /**
097     * Gets the column number.
098     * @return the column.
099     */
100    public int getColumn() {
101        return column;
102    }
103    
104    /**
105     * Gets the last correct input.
106     * @return the string after which the error occured
107     */
108    public String getAfter() {
109        return after;
110    }
111 
112
113    /**
114     * Returns a detailed message for the Error when it is thrown by the
115     * token manager to indicate a lexical error.
116     * @return the message
117     */
118    @Override
119    public String getMessage() {
120        return ("Lexical error at line "
121                + line + ", column "
122                + column + ".  Encountered: "
123                + (eof ? "<EOF> "
124                   : (StringParser.escapeString(String.valueOf(current), '"')) + " (" + (int) current + "), ")
125                + "after : " + StringParser.escapeString(after, '"'));
126    }
127
128
129    /** Constructor with message and reason. */
130    public TokenMgrError(String message, int reason) {
131        super(message);
132        errorCode = reason;
133    }
134
135    /** Full Constructor. */
136    public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) {
137        eof = EOFSeen;
138        state = lexState;
139        line = errorLine;
140        column = errorColumn;
141        after = errorAfter;
142        current = curChar;
143        errorCode = reason;
144    }
145
146    public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, int curChar, int reason) {
147        eof = EOFSeen;
148        state = lexState;
149        line = errorLine;
150        column = errorColumn;
151        after = errorAfter;
152        current = (char)curChar;
153        errorCode = reason;
154    }
155}