Fixed the licensing statements - I had erroneously marked the files as covered by...
[lwes-dotnet/github-mirror.git] / Org.Lwes / ESF / Exceptions.cs
blobc331fe74b37708ba2e1d501fe2f023844190c05e
1 //
2 // This file is part of the LWES .NET Binding (LWES.net)
3 //
4 // COPYRIGHT (C) 2009, Phillip Clark (cerebralkungfu[at*g mail[dot*com)
5 // original .NET implementation
6 //
7 // LWES.net is free software: you can redistribute it and/or modify
8 // it under the terms of the Lesser GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
12 // LWES.net is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the Lesser GNU General Public License
18 // along with LWES.net. If not, see <http://www.gnu.org/licenses/>.
20 namespace Org.Lwes.ESF
22 using System;
23 using System.Text;
25 /// <summary>
26 /// Exception indicating a parse error has occurred.
27 /// </summary>
28 [Serializable]
29 public class ParseException : System.FormatException
31 #region Fields
33 /// <summary>
34 /// Value indicating an ErrorPosition is unknown or invalid.
35 /// </summary>
36 public static readonly int InvalidErrorPosition = -1;
38 private int _position = InvalidErrorPosition;
40 #endregion Fields
42 #region Constructors
44 /// <summary>
45 /// Creates a new instance.
46 /// </summary>
47 public ParseException()
51 /// <summary>
52 /// Creates a new instance initializing the error's message.
53 /// </summary>
54 /// <param name="msg">the error message</param>
55 public ParseException(string msg)
56 : base(msg)
60 /// <summary>
61 /// Creates a new instance initializing the error's message
62 /// and indicating the cursor position of the error.
63 /// </summary>
64 /// <param name="msg">the error message</param>
65 /// <param name="cursor">the cursor position of the error</param>
66 public ParseException(string msg, Cursor cursor)
67 : base(ParseException.MakeErrorMessage(msg, cursor))
69 _position = cursor;
72 /// <summary>
73 /// Creates a new instance indicating the cursor position of the error
74 /// and the values that were expected at that position.
75 /// </summary>
76 /// <param name="curs">the cursor position of the error</param>
77 /// <param name="expected">expected values</param>
78 public ParseException(Cursor curs, params string[] expected)
79 : base(ParseException.MakeErrorMessage(curs, expected))
83 #endregion Constructors
85 #region Properties
87 /// <summary>
88 /// Position of the parse error within the parse input.
89 /// </summary>
90 /// <value>Offset from the beginning parse input where the error occurred.</value>
91 public int ErrorPosition
93 get { return _position; }
96 #endregion Properties
98 #region Methods
100 private static string MakeErrorMessage(string message, Cursor curs)
102 return String.Concat(message, " - at ", curs);
105 private static string MakeErrorMessage(Cursor curs, params string[] expected)
107 StringBuilder buffer = new StringBuilder(400)
108 .Append("Input cannot be parsed at ").Append(curs.ToString()).Append(": expected ");
109 if (expected.Length == 1)
110 buffer.Append(expected[0]);
111 else
113 for (int i = 0; i < expected.Length; ++i)
115 if (i > 0)
117 if (i == expected.Length - 1) buffer.Append(" or ");
118 else buffer.Append(", ");
120 buffer.Append(expected[i]);
123 return buffer.ToString();
126 #endregion Methods