2 // This file is part of the LWES .NET Binding (LWES.net)
4 // COPYRIGHT© 2009, Phillip Clark (phillip[at*flitbit[dot*org)
5 // original .NET implementation
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 // Lesser 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
26 /// Exception indicating a parse error has occurred.
29 public class ParseException
: System
.FormatException
34 /// Value indicating an ErrorPosition is unknown or invalid.
36 public static readonly int InvalidErrorPosition
= -1;
38 private int _position
= InvalidErrorPosition
;
45 /// Creates a new instance.
47 public ParseException()
52 /// Creates a new instance initializing the error's message.
54 /// <param name="msg">the error message</param>
55 public ParseException(string msg
)
61 /// Creates a new instance initializing the error's message
62 /// and indicating the cursor position of the error.
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
))
73 /// Creates a new instance indicating the cursor position of the error
74 /// and the values that were expected at that position.
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
88 /// Position of the parse error within the parse input.
90 /// <value>Offset from the beginning parse input where the error occurred.</value>
91 public int ErrorPosition
93 get { return _position; }
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]);
113 for (int i
= 0; i
< expected
.Length
; ++i
)
117 if (i
== expected
.Length
- 1) buffer
.Append(" or ");
118 else buffer
.Append(", ");
120 buffer
.Append(expected
[i
]);
123 return buffer
.ToString();