2 using System
.Configuration
;
6 namespace Lucene
.Net
.Analysis
.Standard
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
14 /// You can modify this class to customize your error reporting
15 /// mechanisms so long as you retain the public fields.
17 public class ParseException
: System
.IO
.IOException
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: <result of Message>
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
38 specialConstructor
= true;
39 currentToken
= currentTokenVal
;
40 expectedTokenSequences
= expectedTokenSequencesVal
;
41 tokenImage
= tokenImageVal
;
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.
53 public ParseException()
55 specialConstructor
= false;
58 public ParseException(String message
) : base(message
)
60 specialConstructor
= false;
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).
68 protected bool specialConstructor
;
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.
75 public Token currentToken
;
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.
82 public int[][] expectedTokenSequences
;
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.
89 public String
[] tokenImage
;
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
101 /// <returns></returns>
102 public String
GetMessage()
104 if (!specialConstructor
)
108 String expected
= "";
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)
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
+= " ";
133 retval
+= tokenImage
[0];
136 retval
+= AddEscapes(tok
.image
);
139 retval
+= "\" at line " + currentToken
.next
.beginLine
+ ", column " + currentToken
.next
.beginColumn
+ "." + eol
;
140 if (expectedTokenSequences
.Length
== 1)
142 retval
+= "Was expecting:" + eol
+ " ";
146 retval
+= "Was expecting one of:" + eol
+ " ";
153 /// The end of line string for this machine.
155 static protected String possible_eol
= ConfigurationSettings
.AppSettings
.Get("line.separator");
156 protected String eol
= (possible_eol
== null) ? "\r\n" : possible_eol
;
159 /// Used to convert raw characters to their escaped version
160 /// when these raw version cannot be used as part of an ASCII
163 /// <param name="str"></param>
164 /// <returns></returns>
165 protected String
AddEscapes(String str
)
167 StringBuilder retval
= new StringBuilder();
169 for (int i
= 0; i
< str
.Length
; i
++)
176 retval
.Append("\\b");
179 retval
.Append("\\t");
182 retval
.Append("\\n");
185 retval
.Append("\\f");
188 retval
.Append("\\r");
191 retval
.Append("\\\"");
194 retval
.Append("\\\'");
197 retval
.Append("\\\\");
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));
212 return retval
.ToString();