View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jexl2.parser;
18  
19  /**
20   * Token Manager Error.
21   */
22  public class TokenMgrError extends Error {
23      /**
24       * The version identifier for this Serializable class.
25       * Increment only if the <i>serialized</i> form of the
26       * class changes.
27       */
28      private static final long serialVersionUID = 1L;
29  
30      /*
31       * Ordinals for various reasons why an Error of this type can be thrown.
32       */
33      /**
34       * Lexical error occurred.
35       */
36      public static final int LEXICAL_ERROR = 0;
37      /**
38       * An attempt was made to create a second instance of a static token manager.
39       */
40      public static final int STATIC_LEXER_ERROR = 1;
41      /**
42       * Tried to change to an invalid lexical state.
43       */
44      public static final int INVALID_LEXICAL_STATE = 2;
45      /**
46       * Detected (and bailed out of) an infinite loop in the token manager.
47       */
48      public static final int LOOP_DETECTED = 3;
49      /**
50       * Indicates the reason why the exception is thrown. It will have
51       * one of the above 4 values.
52       */
53      private int errorCode;
54      /**
55       * The lexer state.
56       */
57      @SuppressWarnings("unused") // not read currently
58      private int state;
59      /**
60       * The current character.
61       */
62      private char current;
63      /**
64       * Last correct input before error occurs.
65       */
66      private String after;
67      /**
68       * 
69       */
70      private boolean eof;
71      /**
72       * Error line.
73       */
74      private int line;
75      /**
76       * Error column.
77       */
78      private int column;
79      
80      /**
81       * Gets the reason why the exception is thrown.
82       * @return one of the 4 lexical error codes
83       */
84      public int getErrorCode() {
85          return errorCode;
86      }
87  
88      /**
89       * Gets the line number.
90       * @return line number.
91       */
92      public int getLine() {
93          return line;
94      }
95  
96      /**
97       * Gets the column number.
98       * @return the column.
99       */
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 }