namespace XDLCompiler; /// Provides functionality for tracking a position within text. public struct TextPosition { private int _offset; private int _line; private int _column; /// Gets the current offset. public readonly int Offset => _offset; /// Gets the current position's line number. public readonly int Line => _line; /// Gets the current positions' column number. public readonly int Column => _column; /// Initializes a new instance. public TextPosition(int offset, int line, int column) { ArgumentOutOfRangeException.ThrowIfLessThan(offset, 0); ArgumentOutOfRangeException.ThrowIfLessThan(line, 0); ArgumentOutOfRangeException.ThrowIfLessThan(column, 0); _offset = offset; _line = line; _column = column; } /// Advances the position by the provided text. public void Advance(ReadOnlySpan text) { _offset += text.Length; var lines = text.Count('\n'); if (lines == 0) { _column += text.Length; } else { _column = text.Length - text.LastIndexOf('\n') - 1; _line += lines; } } /// Advances the position by the provided text. public void Advance(char text) { _offset++; if (text != '\n') { _column++; } else { _line++; _column = 1; } } public readonly override string ToString() { return $"{_line + 1},{_column + 1}"; } }