View Javadoc

1   /* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 5.0 */
2   /* JavaCCOptions: */
3   /*
4    *    Copyright 2010-2011 The 99 Software Foundation
5    *
6    *    Licensed under the Apache License, Version 2.0 (the "License");
7    *    you may not use this file except in compliance with the License.
8    *    You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *    Unless required by applicable law or agreed to in writing, software
13   *    distributed under the License is distributed on an "AS IS" BASIS,
14   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *    See the License for the specific language governing permissions and
16   *    limitations under the License.
17   */
18  package org.nnsoft.commons.bspi;
19  
20  /** Token Manager Error. */
21  public class TokenMgrError extends Error
22  {
23  
24    /**
25     * The version identifier for this Serializable class.
26     * Increment only if the <i>serialized</i> form of the
27     * class changes.
28     */
29    private static final long serialVersionUID = 1L;
30  
31    /*
32     * Ordinals for various reasons why an Error of this type can be thrown.
33     */
34  
35    /**
36     * Lexical error occurred.
37     */
38    static final int LEXICAL_ERROR = 0;
39  
40    /**
41     * An attempt was made to create a second instance of a static token manager.
42     */
43    static final int STATIC_LEXER_ERROR = 1;
44  
45    /**
46     * Tried to change to an invalid lexical state.
47     */
48    static final int INVALID_LEXICAL_STATE = 2;
49  
50    /**
51     * Detected (and bailed out of) an infinite loop in the token manager.
52     */
53    static final int LOOP_DETECTED = 3;
54  
55    /**
56     * Indicates the reason why the exception is thrown. It will have
57     * one of the above 4 values.
58     */
59    int errorCode;
60  
61    /**
62     * Replaces unprintable characters by their escaped (or unicode escaped)
63     * equivalents in the given string
64     */
65    protected static final String addEscapes(String str) {
66      StringBuffer retval = new StringBuffer();
67      char ch;
68      for (int i = 0; i < str.length(); i++) {
69        switch (str.charAt(i))
70        {
71          case 0 :
72            continue;
73          case '\b':
74            retval.append("\\b");
75            continue;
76          case '\t':
77            retval.append("\\t");
78            continue;
79          case '\n':
80            retval.append("\\n");
81            continue;
82          case '\f':
83            retval.append("\\f");
84            continue;
85          case '\r':
86            retval.append("\\r");
87            continue;
88          case '\"':
89            retval.append("\\\"");
90            continue;
91          case '\'':
92            retval.append("\\\'");
93            continue;
94          case '\\':
95            retval.append("\\\\");
96            continue;
97          default:
98            if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
99              String s = "0000" + Integer.toString(ch, 16);
100             retval.append("\\u" + s.substring(s.length() - 4, s.length()));
101           } else {
102             retval.append(ch);
103           }
104           continue;
105       }
106     }
107     return retval.toString();
108   }
109 
110   /**
111    * Returns a detailed message for the Error when it is thrown by the
112    * token manager to indicate a lexical error.
113    * Parameters :
114    *    EOFSeen     : indicates if EOF caused the lexical error
115    *    curLexState : lexical state in which this error occurred
116    *    errorLine   : line number when the error occurred
117    *    errorColumn : column number when the error occurred
118    *    errorAfter  : prefix that was seen before this error occurred
119    *    curchar     : the offending character
120    * Note: You can customize the lexical error message by modifying this method.
121    */
122   protected static String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar) {
123     return("Lexical error at line " +
124           errorLine + ", column " +
125           errorColumn + ".  Encountered: " +
126           (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") +
127           "after : \"" + addEscapes(errorAfter) + "\"");
128   }
129 
130   /**
131    * You can also modify the body of this method to customize your error messages.
132    * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not
133    * of end-users concern, so you can return something like :
134    *
135    *     "Internal Error : Please file a bug report .... "
136    *
137    * from this method for such cases in the release version of your parser.
138    */
139   public String getMessage() {
140     return super.getMessage();
141   }
142 
143   /*
144    * Constructors of various flavors follow.
145    */
146 
147   /** No arg constructor. */
148   public TokenMgrError() {
149   }
150 
151   /** Constructor with message and reason. */
152   public TokenMgrError(String message, int reason) {
153     super(message);
154     errorCode = reason;
155   }
156 
157   /** Full Constructor. */
158   public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) {
159     this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
160   }
161 }
162 /* JavaCC - OriginalChecksum=5515ebf01b6bdd81e790c8bad40ccd1b (do not edit this line) */