@Test void testRollback() throws IOException { var stream = new BufferedCharStream(new ByteArrayInputStream("abcdef\t\r\nabcdef".getBytes())); assertEquals('a', stream.peek()); assertEquals(new LineColumn(1, 1), stream.position()); for (int index = 0; index < 3; index++) { stream.take(); } assertEquals(new LineColumn(1, 4), stream.position()); stream.rollback(3); assertEquals(new LineColumn(1, 1), stream.position()); assertEquals('a', stream.peek()); for (int index = 0; index < 14; index++) { stream.take(); } assertEquals(new LineColumn(2, 7), stream.position()); stream.rollback(7); assertEquals(new LineColumn(1, 9), stream.position()); stream = new BufferedCharStream(new ByteArrayInputStream("\r\na\r\n\tb".getBytes())); for (int index = 0; index < 5; index++) { stream.take(); } assertEquals(new LineColumn(3, 6), stream.position()); stream.rollback(3); assertEquals(new LineColumn(2, 2), stream.position()); }
@Override public void rollback(int count) { while (count-- > 0) { if (!hasRemaining()) { pos--; } else { var ch = buffer[--pos]; if (ch == '\n') { count++; } else if (ch == '\r') { line--; column = calculateLineColumn(); } else if (ch == '\t') { column -= tabSize - (column - 1) % tabSize; } else { column--; } } } }
BufferedCharStream implements CharStream { @Override public void rollback(int count) { while (count-- > 0) { if (!hasRemaining()) { pos--; } else { var ch = buffer[--pos]; if (ch == '\n') { count++; } else if (ch == '\r') { line--; column = calculateLineColumn(); } else if (ch == '\t') { column -= tabSize - (column - 1) % tabSize; } else { column--; } } } } }
BufferedCharStream implements CharStream { @Override public void rollback(int count) { while (count-- > 0) { if (!hasRemaining()) { pos--; } else { var ch = buffer[--pos]; if (ch == '\n') { count++; } else if (ch == '\r') { line--; column = calculateLineColumn(); } else if (ch == '\t') { column -= tabSize - (column - 1) % tabSize; } else { column--; } } } } BufferedCharStream(InputStream stream);  BufferedCharStream(InputStream stream, int line, int column); private  BufferedCharStream(InputStream stream, int tabSize); }
BufferedCharStream implements CharStream { @Override public void rollback(int count) { while (count-- > 0) { if (!hasRemaining()) { pos--; } else { var ch = buffer[--pos]; if (ch == '\n') { count++; } else if (ch == '\r') { line--; column = calculateLineColumn(); } else if (ch == '\t') { column -= tabSize - (column - 1) % tabSize; } else { column--; } } } } BufferedCharStream(InputStream stream);  BufferedCharStream(InputStream stream, int line, int column); private  BufferedCharStream(InputStream stream, int tabSize); @Override char take(); @Override char peek(); @Override void mark(); @Override void reset(); @Override void rollback(int count); @Override boolean hasRemaining(); @Override LineColumn position(); }
BufferedCharStream implements CharStream { @Override public void rollback(int count) { while (count-- > 0) { if (!hasRemaining()) { pos--; } else { var ch = buffer[--pos]; if (ch == '\n') { count++; } else if (ch == '\r') { line--; column = calculateLineColumn(); } else if (ch == '\t') { column -= tabSize - (column - 1) % tabSize; } else { column--; } } } } BufferedCharStream(InputStream stream);  BufferedCharStream(InputStream stream, int line, int column); private  BufferedCharStream(InputStream stream, int tabSize); @Override char take(); @Override char peek(); @Override void mark(); @Override void reset(); @Override void rollback(int count); @Override boolean hasRemaining(); @Override LineColumn position();  }