First post!
[beagle.git] / Lucene.Net / Analysis / Standard / ParseException.cs
blob58b067491b9c886ebf58e4058151c8381aa1cc57
1 using System;
2 using System.Configuration;
3 using System.Text;
4 using Lucene.Net.Util;
6 namespace Lucene.Net.Analysis.Standard
8 /// <summary>
9 /// This exception is thrown when parse errors are encountered.
10 /// You can explicitly create objects of this exception type by
11 /// calling the method generateParseException in the generated
12 /// parser.
13 ///
14 /// You can modify this class to customize your error reporting
15 /// mechanisms so long as you retain the public fields.
16 /// </summary>
17 public class ParseException : System.IO.IOException
19 /// <summary>
20 /// This constructor is used by the method "GenerateParseException"
21 /// in the generated parser. Calling this constructor generates
22 /// a new object of this type with the fields "CurrentToken",
23 /// "ExpectedTokenSequences", and "TokenImage" set. The bool
24 /// flag "SpecialConstructor" is also set to true to indicate that
25 /// this constructor was used to create this object.
26 /// This constructor calls its super class with the empty string
27 /// to force the "ToString" method of parent class "Exception" to
28 /// print the error message in the form:
29 /// ParseException: &lt;result of Message&gt;
30 /// </summary>
31 /// <param name="currentTokenVal"></param>
32 /// <param name="expectedTokenSequencesVal"></param>
33 /// <param name="tokenImageVal"></param>
34 public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal,
35 String[] tokenImageVal
36 ) : base("")
38 specialConstructor = true;
39 currentToken = currentTokenVal;
40 expectedTokenSequences = expectedTokenSequencesVal;
41 tokenImage = tokenImageVal;
44 /// <summary>
45 /// The following constructors are for use by you for whatever
46 /// purpose you can think of. Constructing the exception in this
47 /// manner makes the exception behave in the normal way - i.e., as
48 /// documented in the class "Throwable". The fields "errorToken",
49 /// "expectedTokenSequences", and "tokenImage" do not contain
50 /// relevant information. The JavaCC generated code does not use
51 /// these constructors.
52 /// </summary>
53 public ParseException()
55 specialConstructor = false;
58 public ParseException(String message) : base(message)
60 specialConstructor = false;
63 /// <summary>
64 /// This variable determines which constructor was used to create
65 /// this object and thereby affects the semantics of the
66 /// "GetMessage" method (see below).
67 /// </summary>
68 protected bool specialConstructor;
70 /// <summary>
71 /// This is the last token that has been consumed successfully. If
72 /// this object has been created due to a parse error, the token
73 /// followng this token will (therefore) be the first error token.
74 /// </summary>
75 public Token currentToken;
77 /// <summary>
78 /// Each entry in this array is an array of integers. Each array
79 /// of integers represents a sequence of tokens (by their ordinal
80 /// values) that is expected at this point of the parse.
81 /// </summary>
82 public int[][] expectedTokenSequences;
84 /// <summary>
85 /// This is a reference to the "tokenImage" array of the generated
86 /// parser within which the parse error occurred. This array is
87 /// defined in the generated ...Constants interface.
88 /// </summary>
89 public String[] tokenImage;
91 /// <summary>
92 /// This method has the standard behavior when this object has been
93 /// created using the standard constructors. Otherwise, it uses
94 /// "currentToken" and "expectedTokenSequences" to generate a parse
95 /// error message and returns it. If this object has been created
96 /// due to a parse error, and you do not catch it (it gets thrown
97 /// from the parser), then this method is called during the printing
98 /// of the final stack trace, and hence the correct error message
99 /// gets displayed.
100 /// </summary>
101 /// <returns></returns>
102 public String GetMessage()
104 if (!specialConstructor)
106 return base.Message;
108 String expected = "";
109 int maxSize = 0;
110 for (int i = 0; i < expectedTokenSequences.Length; i++)
112 if (maxSize < expectedTokenSequences[i].Length)
114 maxSize = expectedTokenSequences[i].Length;
116 for (int j = 0; j < expectedTokenSequences[i].Length; j++)
118 expected += tokenImage[expectedTokenSequences[i][j]] + " ";
120 if (expectedTokenSequences[i][expectedTokenSequences[i].Length - 1] != 0)
122 expected += "...";
124 expected += eol + " ";
126 String retval = "Encountered \"";
127 Token tok = currentToken.next;
128 for (int i = 0; i < maxSize; i++)
130 if (i != 0) retval += " ";
131 if (tok.kind == 0)
133 retval += tokenImage[0];
134 break;
136 retval += AddEscapes(tok.image);
137 tok = tok.next;
139 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn + "." + eol;
140 if (expectedTokenSequences.Length == 1)
142 retval += "Was expecting:" + eol + " ";
144 else
146 retval += "Was expecting one of:" + eol + " ";
148 retval += expected;
149 return retval;
152 /// <summary>
153 /// The end of line string for this machine.
154 /// </summary>
155 static protected String possible_eol = ConfigurationSettings.AppSettings.Get("line.separator");
156 protected String eol = (possible_eol == null) ? "\r\n" : possible_eol;
158 /// <summary>
159 /// Used to convert raw characters to their escaped version
160 /// when these raw version cannot be used as part of an ASCII
161 /// string literal.
162 /// </summary>
163 /// <param name="str"></param>
164 /// <returns></returns>
165 protected String AddEscapes(String str)
167 StringBuilder retval = new StringBuilder();
168 char ch;
169 for (int i = 0; i < str.Length; i++)
171 switch (str[i])
173 case (char)0 :
174 continue;
175 case '\b':
176 retval.Append("\\b");
177 continue;
178 case '\t':
179 retval.Append("\\t");
180 continue;
181 case '\n':
182 retval.Append("\\n");
183 continue;
184 case '\f':
185 retval.Append("\\f");
186 continue;
187 case '\r':
188 retval.Append("\\r");
189 continue;
190 case '\"':
191 retval.Append("\\\"");
192 continue;
193 case '\'':
194 retval.Append("\\\'");
195 continue;
196 case '\\':
197 retval.Append("\\\\");
198 continue;
199 default:
200 if ((ch = str[i]) < 0x20 || ch > 0x7e)
202 String s = "0000" + Number.ToString(ch, 16);
203 retval.Append("\\u" + s.Substring(s.Length - 4, 4));
205 else
207 retval.Append(ch);
209 continue;
212 return retval.ToString();