/// <summary> /// Read the next char /// </summary> public CharInfo Read() { CharInfo res = CharInfo.EOF; bool moveNextPosition = true; // If unread char get the first if (_UnreadBuffer.Count > 0) { res = _UnreadBuffer.Pop(); // We calculate the next position if the buffer is empty moveNextPosition = _UnreadBuffer.Count == 0; } else { res = new CharInfo(Reader.Read(), Position); // Add in buffer only if new char if (_RegisteredBuffers != null) { foreach (var buffer in _RegisteredBuffers) { buffer.Push(res); } } } // Move the position ? if (res != CharInfo.EOF && moveNextPosition) { _Position = res.Position; if (res == '\n') { if (!_LastWasCR) { _Position = _Position.NextLine(true); } else { _Position = _Position.AddPosition(1); } } else if (res == '\r') { _Position = _Position.NextLine(true); } else { _Position++; } } _LastWasCR = res == '\r'; // Returns result return(res); }
public void TestNexLine() { var pos = new ParsePosition(17, 2, 7); var pos2 = pos.NextLine(); var pos3 = pos2.NextLine(false); Assert.Equal(17, pos.Position); Assert.Equal(2, pos.Line); Assert.Equal(7, pos.Column); Assert.Equal(18, pos2.Position); Assert.Equal(3, pos2.Line); Assert.Equal(0, pos2.Column); Assert.Equal(18, pos3.Position); Assert.Equal(4, pos3.Line); Assert.Equal(0, pos3.Column); }