5 namespace Lucene
.Net
.Analysis
.Standard
7 public class TokenMgrError
: Exception
9 // Ordinals for various reasons why an Exception of this type can be thrown.
12 /// Lexical error occured.
14 internal const int LEXICAL_ERROR
= 0;
17 /// An attempt wass made to create a second instance of a static token manager.
19 internal const int STATIC_LEXER_ERROR
= 1;
22 * Tried to change to an invalid lexical state.
24 internal const int INVALID_LEXICAL_STATE
= 2;
27 /// Detected (and bailed out of) an infinite loop in the token manager.
29 internal const int LOOP_DETECTED
= 3;
32 /// Indicates the reason why the exception is thrown. It will have
33 /// one of the above 4 values.
35 internal int errorCode
;
38 /// Replaces unprintable characters by their espaced (or unicode escaped)
39 /// equivalents in the given string
41 /// <param name="str"></param>
42 /// <returns></returns>
43 protected static String
AddEscapes(String str
)
45 StringBuilder retval
= new StringBuilder();
47 for (int i
= 0; i
< str
.Length
; i
++)
69 retval
.Append("\\\"");
72 retval
.Append("\\\'");
75 retval
.Append("\\\\");
78 if ((ch
= str
[i
]) < 0x20 || ch
> 0x7e)
80 String s
= "0000" + Number
.ToString(ch
, 16);
81 retval
.Append("\\u" + s
.Substring(s
.Length
- 4, 4));
90 return retval
.ToString();
94 /// Returns a detailed message for the Error when it is thrown by the
95 /// token manager to indicate a lexical error.
97 /// EOFSeen : indicates if EOF caused the lexicl error
98 /// curLexState : lexical state in which this error occured
99 /// errorLine : line number when the error occured
100 /// errorColumn : column number when the error occured
101 /// errorAfter : prefix that was seen before this error occured
102 /// curchar : the offending character
103 /// Note: You can customize the lexical error message by modifying this method.
105 /// <param name="EOFSeen"></param>
106 /// <param name="lexState"></param>
107 /// <param name="errorLine"></param>
108 /// <param name="errorColumn"></param>
109 /// <param name="errorAfter"></param>
110 /// <param name="curChar"></param>
111 /// <returns></returns>
112 protected static String
LexicalError(bool EOFSeen
, int lexState
, int errorLine
, int errorColumn
, String errorAfter
, char curChar
)
114 return("Lexical error at line " +
115 errorLine
+ ", column " +
116 errorColumn
+ ". Encountered: " +
117 (EOFSeen
? "<EOF> " : ("\"" +
118 AddEscapes(curChar
+ "\"") + " (" + (int)curChar
+ "), ") +
119 "after : \"" + AddEscapes(errorAfter
) + "\""));
123 /// You can also modify the body of this method to customize your error messages.
124 /// For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not
125 /// of end-users concern, so you can return something like :
127 /// "Internal Error : Please file a bug report .... "
129 /// from this method for such cases in the release version of your parser.
131 /// <returns></returns>
132 public String
GetMessage()
138 /// Constructors of various flavors follow.
140 public TokenMgrError()
144 public TokenMgrError(String message
, int reason
) : base(message
)
149 public TokenMgrError(bool EOFSeen
, int lexState
, int errorLine
, int errorColumn
, String errorAfter
, char curChar
, int reason
)
150 : this(LexicalError(EOFSeen
, lexState
, errorLine
, errorColumn
, errorAfter
, curChar
), reason
)