KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > expression > ast > TokenMgrError


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.expression.ast;
5
6 public class TokenMgrError extends Error JavaDoc {
7   /*
8   * Ordinals for various reasons why an Error of this type can be thrown.
9   */

10
11   /**
12    * Lexical error occured.
13    */

14   static final int LEXICAL_ERROR = 0;
15
16   /**
17    * An attempt wass made to create a second instance of a static token manager.
18    */

19   static final int STATIC_LEXER_ERROR = 1;
20
21   /**
22    * Tried to change to an invalid lexical state.
23    */

24   static final int INVALID_LEXICAL_STATE = 2;
25
26   /**
27    * Detected (and bailed out of) an infinite loop in the token manager.
28    */

29   static final int LOOP_DETECTED = 3;
30
31   /**
32    * Indicates the reason why the exception is thrown. It will have one of the above 4 values.
33    */

34   int errorCode;
35
36   /**
37    * Replaces unprintable characters by their espaced (or unicode escaped) equivalents in the given string
38    */

39   protected static final String JavaDoc addEscapes(String JavaDoc str) {
40     StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
41     char ch;
42     for (int i = 0; i < str.length(); i++) {
43       switch (str.charAt(i)) {
44         case 0:
45           continue;
46         case '\b':
47           retval.append("\\b");
48           continue;
49         case '\t':
50           retval.append("\\t");
51           continue;
52         case '\n':
53           retval.append("\\n");
54           continue;
55         case '\f':
56           retval.append("\\f");
57           continue;
58         case '\r':
59           retval.append("\\r");
60           continue;
61         case '\"':
62           retval.append("\\\"");
63           continue;
64         case '\'':
65           retval.append("\\\'");
66           continue;
67         case '\\':
68           retval.append("\\\\");
69           continue;
70         default:
71           if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
72             String JavaDoc s = "0000" + Integer.toString(ch, 16);
73             retval.append("\\u" + s.substring(s.length() - 4, s.length()));
74           } else {
75             retval.append(ch);
76           }
77           continue;
78       }
79     }
80     return retval.toString();
81   }
82
83   /**
84    * Returns a detailed message for the Error when it is thrown by the token manager to indicate a lexical error.
85    * Parameters : EOFSeen : indicates if EOF caused the lexicl error curLexState : lexical state in which this error
86    * occured errorLine : line number when the error occured errorColumn : column number when the error occured
87    * errorAfter : prefix that was seen before this error occured curchar : the offending character Note: You can
88    * customize the lexical error message by modifying this method.
89    */

90   protected static String JavaDoc LexicalError(boolean EOFSeen,
91                                        int lexState,
92                                        int errorLine,
93                                        int errorColumn,
94                                        String JavaDoc errorAfter,
95                                        char curChar) {
96     return ("Lexical error at line "
97             + errorLine
98             + ", column "
99             + errorColumn
100             + ". Encountered: "
101             +
102             (EOFSeen ?
103                     "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int) curChar + "), ")
104             + "after : \""
105             + addEscapes(errorAfter) + "\"");
106   }
107
108   /**
109    * You can also modify the body of this method to customize your error messages. For example, cases like
110    * LOOP_DETECTED and INVALID_LEXICAL_STATE are not of end-users concern, so you can return something like :
111    * "Internal Error : Please file a bug report .... " from this method for such cases in the release version of your
112    * parser.
113    */

114   public String JavaDoc getMessage() {
115     return super.getMessage();
116   }
117
118   /*
119   * Constructors of various flavors follow.
120   */

121
122   public TokenMgrError() {
123   }
124
125   public TokenMgrError(String JavaDoc message, int reason) {
126     super(message);
127     errorCode = reason;
128   }
129
130   public TokenMgrError(boolean EOFSeen,
131                        int lexState,
132                        int errorLine,
133                        int errorColumn,
134                        String JavaDoc errorAfter,
135                        char curChar,
136                        int reason) {
137     this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
138   }
139 }
Popular Tags