ErrorManager.cs revision 324c4644fee44b9898524c09511bd33c3f12e2df
1/*
2[The "BSD licence"]
3Copyright (c) 2005-2007 Kunle Odutola
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions
8are met:
91. Redistributions of source code MUST RETAIN the above copyright
10   notice, this list of conditions and the following disclaimer.
112. Redistributions in binary form MUST REPRODUCE the above copyright
12   notice, this list of conditions and the following disclaimer in
13   the documentation and/or other materials provided with the
14   distribution.
153. The name of the author may not be used to endorse or promote products
16   derived from this software without specific prior WRITTEN permission.
174. Unless explicitly state otherwise, any contribution intentionally
18   submitted for inclusion in this work to the copyright owner or licensor
19   shall be under the terms and conditions of this license, without any
20   additional terms or conditions.
21
22THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*/
33
34
35namespace Antlr.Runtime.Misc
36{
37	using System;
38	using StringBuilder = System.Text.StringBuilder;
39	using StackTrace = System.Diagnostics.StackTrace;
40	using StackFrame = System.Diagnostics.StackFrame;
41	using IList = System.Collections.IList;
42
43	/// <summary>A minimal ANTLR3 error [message] manager with the ST bits</summary>
44	public class ErrorManager
45	{
46		public static void InternalError(object error, Exception e)
47		{
48			StackFrame location = GetLastNonErrorManagerCodeLocation(e);
49			string msg = "Exception " + e + "@" + location + ": " + error;
50			//Error(MSG_INTERNAL_ERROR, msg);
51			Error(msg);
52		}
53
54		public static void InternalError(object error)
55		{
56			StackFrame location = GetLastNonErrorManagerCodeLocation(new Exception());
57			string msg = location + ": " + error;
58			//Error(MSG_INTERNAL_ERROR, msg);
59			Error(msg);
60		}
61
62		/// <summary>
63		/// Return first non ErrorManager code location for generating messages
64		/// </summary>
65		/// <param name="e">Current exception</param>
66		/// <returns></returns>
67		private static StackFrame GetLastNonErrorManagerCodeLocation(Exception e)
68		{
69			StackTrace stackTrace = new StackTrace(e);
70			int i = 0;
71			for (; i < stackTrace.FrameCount; i++)
72			{
73				StackFrame f = stackTrace.GetFrame(i);
74				if (f.ToString().IndexOf("ErrorManager") < 0)
75				{
76					break;
77				}
78			}
79			StackFrame location = stackTrace.GetFrame(i);
80
81			return location;
82		}
83
84		public static void Error(/*int msgID,*/ object arg)
85		{
86			//getErrorCount().errors++;
87			//getErrorListener().error(new ToolMessage(msgID, arg));
88
89			StringBuilder sb = new StringBuilder();
90			//sb.AppendFormat("internal error: {0} {1}", arg);
91			sb.AppendFormat("internal error: {0} ", arg);
92		}
93
94		/*
95			INTERNAL_ERROR(arg,arg2,exception,stackTrace) ::= <<
96			internal error: <arg> <arg2><if(exception)>: <exception><endif>
97			<stackTrace; separator="\n">
98			>>
99		 */
100	}
101}