2 // This file is part of the LWES .NET Binding (LWES.net)
4 // COPYRIGHT© 2009, Phillip Clark (cerebralkungfu[at*g mail[dot*com)
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
25 /// Cursor used by parsing methods. Tracks offset, line, and line-position.
40 /// Creates a new instance initialized to the offset given.
42 /// <param name="ofs"></param>
43 public Cursor(int ofs
)
45 _offs
= _linepos
= ofs
;
50 /// Creates a new instance with initialized values for offset,
51 /// line, and line-position.
53 /// <param name="offs">Offset from the beginning of the parse input.</param>
54 /// <param name="line">Zero based line number where the offset occurs.</param>
55 /// <param name="linepos">Zero based character position within the line.</param>
56 public Cursor(int offs
, int line
, int linepos
)
66 /// <param name="c">Copy cursor</param>
67 public Cursor(Cursor c
)
71 _linepos
= c
._linepos
;
74 #endregion Constructors
79 /// Zero based line number where offset occurs.
87 /// Zero based character position within the line.
91 get { return _linepos; }
95 /// Zero based offset from beginning of input.
102 #endregion Properties
107 /// Implicit conversion operator to Int32
109 /// <param name="c">cursor to convert</param>
110 /// <returns>an integer representing the cursor position</returns>
111 public static implicit operator int(Cursor c
)
117 /// Implicit + operator for incrementing a cursor by an integer value.
119 /// <param name="c">cursor to be incremented</param>
120 /// <param name="inc">number by which the cursor is incremented</param>
121 /// <returns>an incremented cursor</returns>
122 public static Cursor
operator +(Cursor c
, int inc
)
124 return new Cursor(c
._offs
+ inc
, c
._line
, c
._linepos
+ inc
);
128 /// Implicit ++ operator for incrementing a cursor's position.
130 /// <param name="c">cursor being incremented</param>
131 /// <returns>the incremented cursor</returns>
132 public static Cursor
operator ++(Cursor c
)
134 return new Cursor(c
._offs
+ 1, c
._line
, c
._linepos
+ 1);
138 /// Increments the cursor.
140 /// <returns>Cursor representing the incremented position</returns>
141 public Cursor
Increment()
143 return new Cursor(_offs
+1, _line
, _linepos
+1);
147 /// Increments the cursor.
149 /// <param name="count">indicates the number to increment the cursor by</param>
150 /// <returns>Cursor representing the incremented position</returns>
151 public Cursor
Increment(int count
)
153 return new Cursor(_offs
+ count
, _line
, _linepos
+ count
);
157 /// Increments the cursor to reflect a new line. (single character newline)
159 /// <returns>Cursor reflecting the new position</returns>
160 public Cursor
Newline()
162 return new Cursor(_offs
+ 1, _line
+ 1, 0);
166 /// Increments the cursor to reflect a new line. (multiple character newline)
168 /// <param name="charCount">number of characters reflecting the new line</param>
169 /// <returns>Cursor reflecting the new position</returns>
170 public Cursor
Newline(int charCount
)
172 return new Cursor(_offs
+ charCount
, _line
+ 1, 0);
178 /// <returns></returns>
179 public override string ToString()
181 return new StringBuilder(65).Append("Cursor: { Offset: ").Append(_offs
)
182 .Append(", Line: ").Append(_line
)
183 .Append(", Character: ").Append(_linepos
)
184 .Append("}").ToString();