Added initial documentation tree using doxygen. More tweaks on the license text ensur...
[lwes-dotnet/github-mirror.git] / Org.Lwes / ESF / Cursor.cs
bloba5fd54cd5b05858cbb6929e533c741dba4fbd977
1 //
2 // This file is part of the LWES .NET Binding (LWES.net)
3 //
4 // COPYRIGHT© 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 // 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
22 using System.Text;
24 /// <summary>
25 /// Cursor used by parsing methods. Tracks offset, line, and line-position.
26 /// </summary>
27 public struct Cursor
29 #region Fields
31 private int _line;
32 private int _linepos;
33 private int _offs;
35 #endregion Fields
37 #region Constructors
39 /// <summary>
40 /// Creates a new instance initialized to the offset given.
41 /// </summary>
42 /// <param name="ofs"></param>
43 public Cursor(int ofs)
45 _offs = _linepos = ofs;
46 _line = 0;
49 /// <summary>
50 /// Creates a new instance with initialized values for offset,
51 /// line, and line-position.
52 /// </summary>
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)
58 _offs = offs;
59 _line = line;
60 _linepos = linepos;
63 /// <summary>
64 /// Copy constructor.
65 /// </summary>
66 /// <param name="c">Copy cursor</param>
67 public Cursor(Cursor c)
69 _offs = c._offs;
70 _line = c._line;
71 _linepos = c._linepos;
74 #endregion Constructors
76 #region Properties
78 /// <summary>
79 /// Zero based line number where offset occurs.
80 /// </summary>
81 public int Line
83 get { return _line; }
86 /// <summary>
87 /// Zero based character position within the line.
88 /// </summary>
89 public int LinePos
91 get { return _linepos; }
94 /// <summary>
95 /// Zero based offset from beginning of input.
96 /// </summary>
97 public int Offset
99 get { return _offs; }
102 #endregion Properties
104 #region Methods
106 /// <summary>
107 /// Implicit conversion operator to Int32
108 /// </summary>
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)
113 return c._offs;
116 /// <summary>
117 /// Implicit + operator for incrementing a cursor by an integer value.
118 /// </summary>
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);
127 /// <summary>
128 /// Implicit ++ operator for incrementing a cursor's position.
129 /// </summary>
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);
137 /// <summary>
138 /// Increments the cursor.
139 /// </summary>
140 /// <returns>Cursor representing the incremented position</returns>
141 public Cursor Increment()
143 return new Cursor(_offs+1, _line, _linepos+1);
146 /// <summary>
147 /// Increments the cursor.
148 /// </summary>
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);
156 /// <summary>
157 /// Increments the cursor to reflect a new line. (single character newline)
158 /// </summary>
159 /// <returns>Cursor reflecting the new position</returns>
160 public Cursor Newline()
162 return new Cursor(_offs + 1, _line + 1, 0);
165 /// <summary>
166 /// Increments the cursor to reflect a new line. (multiple character newline)
167 /// </summary>
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);
175 /// <summary>
176 ///
177 /// </summary>
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();
187 #endregion Methods