Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.util.LinkedList;
- import org.antlr.runtime.CharStream;
- public class ANTLRUnbufferedFileStream implements CharStream {
- private static class Mark {
- public final long position;
- public final int indexInLine;
- public final int lineIndex;
- public Mark(long position, int indexInLine, int lineIndex) {
- this.position = position;
- this.indexInLine = indexInLine;
- this.lineIndex = lineIndex;
- }
- }
- private final File file;
- private final RandomAccessFile fileData;
- private final static char NL = String.format("%n").charAt(0);
- private final LinkedList<Mark> marks = new LinkedList<Mark>();
- private int indexInLine = 0;
- private int lineIndex = 1;
- public ANTLRUnbufferedFileStream(File file) throws FileNotFoundException {
- assert file.canRead();
- this.file = file;
- this.fileData = new RandomAccessFile(file, "r");
- }
- public ANTLRUnbufferedFileStream(String file) throws FileNotFoundException {
- this(new File(file));
- }
- @Override
- public int LA(int i) {
- try {
- if (i == 0)
- return 0;
- long mark = pos();
- long offset = mark + i - 1;
- if (offset < 0)
- return CharStream.EOF;
- else if (offset >= size())
- return CharStream.EOF;
- fseek(offset);
- int la = fileData.read(); // don't want to update counters
- fseek(mark);
- return la;
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- @Override
- public void consume() {
- try {
- int c = fileData.read();
- indexInLine++;
- if (c == NL) {
- lineIndex++;
- indexInLine = 0;
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- @Override
- public String getSourceName() {
- return file.toString();
- }
- @Override
- public int index() {
- return (int) pos();
- }
- @Override
- public int mark() {
- marks.push(new Mark(pos(), getCharPositionInLine(), getLine()));
- return marks.size();
- }
- @Override
- public void release(int m) {
- while(marks.size() >= m)
- marks.pop();
- }
- @Override
- public void rewind() {
- setToMark(marks.peek());
- }
- @Override
- public void rewind(int m) {
- while(marks.size() > m)
- marks.pop();
- setToMark(marks.pop());
- }
- private void setToMark(Mark mark) {
- fseek(mark.position);
- setCharPositionInLine(mark.indexInLine);
- setLine(mark.lineIndex);
- }
- @Override
- public void seek(int i) {
- long mark = pos();
- long offset = mark + i;
- fseek(offset);
- }
- @Override
- public int size() {
- try {
- return (int) fileData.length();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- @Override
- public int LT(int i) {
- return LA(i);
- }
- @Override
- public int getCharPositionInLine() {
- return indexInLine;
- }
- @Override
- public int getLine() {
- return lineIndex;
- }
- @Override
- public void setCharPositionInLine(int i) {
- indexInLine = i;
- }
- @Override
- public void setLine(int i) {
- lineIndex = i;
- }
- @Override
- public String substring(int start, int end) {
- return substring((long)start, (long)end);
- }
- public String substring(long start, long end) {
- try {
- long mark = pos();
- fseek(start);
- long len = end-start+1;
- StringBuilder sb = new StringBuilder();
- for (long i = 0; i < len; i++)
- sb.append((char)fileData.read());
- fseek(mark);
- return sb.toString();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- public long pos() {
- try {
- return fileData.getFilePointer();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- public void fseek(long off) {
- try {
- fileData.seek(off);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
Add Comment
Please, Sign In to add comment