waisbrot

ANTLRUnbufferedFileStream.java

Apr 9th, 2012
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.io.RandomAccessFile;
  5. import java.util.LinkedList;
  6.  
  7. import org.antlr.runtime.CharStream;
  8.  
  9. public class ANTLRUnbufferedFileStream implements CharStream {
  10.     private static class Mark {
  11.         public final long position;
  12.         public final int indexInLine;
  13.         public final int lineIndex;
  14.         public Mark(long position, int indexInLine, int lineIndex) {
  15.             this.position = position;
  16.             this.indexInLine = indexInLine;
  17.             this.lineIndex = lineIndex;
  18.         }
  19.     }
  20.     private final File file;
  21.     private final RandomAccessFile fileData;
  22.     private final static char NL = String.format("%n").charAt(0);
  23.     private final LinkedList<Mark> marks = new LinkedList<Mark>();
  24.     private int indexInLine = 0;
  25.     private int lineIndex = 1;
  26.  
  27.     public ANTLRUnbufferedFileStream(File file) throws FileNotFoundException {
  28.         assert file.canRead();
  29.         this.file = file;
  30.         this.fileData = new RandomAccessFile(file, "r");
  31.     }
  32.     public ANTLRUnbufferedFileStream(String file) throws FileNotFoundException {
  33.         this(new File(file));
  34.     }
  35.  
  36.     @Override
  37.     public int LA(int i) {
  38.         try {
  39.             if (i == 0)
  40.                 return 0;
  41.             long mark = pos();
  42.             long offset = mark + i - 1;
  43.             if (offset < 0)
  44.                 return CharStream.EOF;
  45.             else if (offset >= size())
  46.                 return CharStream.EOF;
  47.             fseek(offset);
  48.             int la = fileData.read();  // don't want to update counters
  49.             fseek(mark);
  50.             return la;
  51.         } catch (IOException e) {
  52.             throw new RuntimeException(e);
  53.         }
  54.     }
  55.  
  56.     @Override
  57.     public void consume() {
  58.         try {
  59.             int c = fileData.read();
  60.             indexInLine++;
  61.             if (c == NL) {
  62.                 lineIndex++;
  63.                 indexInLine = 0;
  64.             }
  65.         } catch (IOException e) {
  66.             throw new RuntimeException(e);
  67.         }
  68.     }
  69.  
  70.     @Override
  71.     public String getSourceName() {
  72.         return file.toString();
  73.     }
  74.  
  75.     @Override
  76.     public int index() {
  77.         return (int) pos();
  78.     }
  79.  
  80.     @Override
  81.     public int mark() {
  82.         marks.push(new Mark(pos(), getCharPositionInLine(), getLine()));
  83.         return marks.size();
  84.     }
  85.  
  86.     @Override
  87.     public void release(int m) {
  88.         while(marks.size() >= m)
  89.             marks.pop();
  90.     }
  91.  
  92.     @Override
  93.     public void rewind() {
  94.         setToMark(marks.peek());
  95.     }
  96.  
  97.     @Override
  98.     public void rewind(int m) {
  99.         while(marks.size() > m)
  100.             marks.pop();
  101.         setToMark(marks.pop());
  102.     }
  103.    
  104.     private void setToMark(Mark mark) {
  105.         fseek(mark.position);
  106.         setCharPositionInLine(mark.indexInLine);
  107.         setLine(mark.lineIndex);
  108.     }
  109.  
  110.     @Override
  111.     public void seek(int i) {
  112.         long mark = pos();
  113.         long offset = mark + i;
  114.         fseek(offset);
  115.     }
  116.  
  117.     @Override
  118.     public int size() {
  119.         try {
  120.             return (int) fileData.length();
  121.         } catch (IOException e) {
  122.             throw new RuntimeException(e);
  123.         }
  124.     }
  125.  
  126.     @Override
  127.     public int LT(int i) {
  128.         return LA(i);
  129.     }
  130.  
  131.     @Override
  132.     public int getCharPositionInLine() {
  133.         return indexInLine;
  134.     }
  135.  
  136.     @Override
  137.     public int getLine() {
  138.         return lineIndex;
  139.     }
  140.  
  141.     @Override
  142.     public void setCharPositionInLine(int i) {
  143.         indexInLine = i;
  144.     }
  145.  
  146.     @Override
  147.     public void setLine(int i) {
  148.         lineIndex = i;
  149.     }
  150.  
  151.     @Override
  152.     public String substring(int start, int end) {
  153.         return substring((long)start, (long)end);
  154.     }
  155.     public String substring(long start, long end) {
  156.         try {
  157.             long mark = pos();
  158.             fseek(start);
  159.             long len = end-start+1;
  160.             StringBuilder sb = new StringBuilder();
  161.             for (long i = 0; i < len; i++)
  162.                 sb.append((char)fileData.read());
  163.             fseek(mark);
  164.             return sb.toString();
  165.         } catch (IOException e) {
  166.             throw new RuntimeException(e);
  167.         }
  168.     }
  169.  
  170.     public long pos() {
  171.         try {
  172.             return fileData.getFilePointer();
  173.         } catch (IOException e) {
  174.             throw new RuntimeException(e);
  175.         }
  176.     }
  177.  
  178.     public void fseek(long off) {
  179.         try {
  180.             fileData.seek(off);
  181.         } catch (IOException e) {
  182.             throw new RuntimeException(e);
  183.         }
  184.  
  185.     }
  186. }
Add Comment
Please, Sign In to add comment