Initial revision
[beagle.git] / Lucene.Net / Analysis / Standard / TokenMgrError.cs
blobe8ac908143aa8bdf6e1912893fd549df13f23fa6
1 using System;
2 using System.Text;
3 using Lucene.Net.Util;
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.
11 /// <summary>
12 /// Lexical error occured.
13 /// </summary>
14 internal const int LEXICAL_ERROR = 0;
16 /// <summary>
17 /// An attempt wass made to create a second instance of a static token manager.
18 /// </summary>
19 internal const int STATIC_LEXER_ERROR = 1;
21 /**
22 * Tried to change to an invalid lexical state.
24 internal const int INVALID_LEXICAL_STATE = 2;
26 /// <summary>
27 /// Detected (and bailed out of) an infinite loop in the token manager.
28 /// </summary>
29 internal const int LOOP_DETECTED = 3;
31 /// <summary>
32 /// Indicates the reason why the exception is thrown. It will have
33 /// one of the above 4 values.
34 /// </summary>
35 internal int errorCode;
37 /// <summary>
38 /// Replaces unprintable characters by their espaced (or unicode escaped)
39 /// equivalents in the given string
40 /// </summary>
41 /// <param name="str"></param>
42 /// <returns></returns>
43 protected static String AddEscapes(String str)
45 StringBuilder retval = new StringBuilder();
46 char ch;
47 for (int i = 0; i < str.Length; i++)
49 switch (str[i])
51 case (char)0 :
52 continue;
53 case '\b':
54 retval.Append("\\b");
55 continue;
56 case '\t':
57 retval.Append("\\t");
58 continue;
59 case '\n':
60 retval.Append("\\n");
61 continue;
62 case '\f':
63 retval.Append("\\f");
64 continue;
65 case '\r':
66 retval.Append("\\r");
67 continue;
68 case '\"':
69 retval.Append("\\\"");
70 continue;
71 case '\'':
72 retval.Append("\\\'");
73 continue;
74 case '\\':
75 retval.Append("\\\\");
76 continue;
77 default:
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));
83 else
85 retval.Append(ch);
87 continue;
90 return retval.ToString();
93 /// <summary>
94 /// Returns a detailed message for the Error when it is thrown by the
95 /// token manager to indicate a lexical error.
96 /// Parameters :
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.
104 /// </summary>
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) + "\""));
122 /// <summary>
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.
130 /// </summary>
131 /// <returns></returns>
132 public String GetMessage()
134 return base.Message;
137 /// <summary>
138 /// Constructors of various flavors follow.
139 /// </summary>
140 public TokenMgrError()
144 public TokenMgrError(String message, int reason) : base(message)
146 errorCode = reason;
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)