X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fchr%2FCharInput.java;h=a03f940820c20c43ce600432f3ffbe165d37a366;hb=ea7e036150d05484ee7867e73d80b5789972edbc;hp=15f128953d149f99617ac13d7c96efec390478b1;hpb=cfa62077ae26cc704390719af1063fcc3dcc21d2;p=sbp.git diff --git a/src/edu/berkeley/sbp/chr/CharInput.java b/src/edu/berkeley/sbp/chr/CharInput.java index 15f1289..a03f940 100644 --- a/src/edu/berkeley/sbp/chr/CharInput.java +++ b/src/edu/berkeley/sbp/chr/CharInput.java @@ -11,15 +11,73 @@ import edu.berkeley.sbp.misc.*; import edu.berkeley.sbp.Input.Location; public class CharInput extends Cartesian.Input { - private final Reader r; - - public CharInput(String s) { this(new StringReader(s)); } - public CharInput(Reader r) { this(r, null); } - public CharInput(Reader r, String s) { this.r = new BufferedReader(r); } - public CharInput(InputStream i) { this(i, null); } - public CharInput(InputStream i, String s) { this(new InputStreamReader(i), s); } + + private static class RollbackReader extends Reader { + private final Reader r; + public RollbackReader(Reader r) { this.r = r; } + + private char[] queue = new char[1024]; + private int head = 0; + private int tail = 0; + + private void unread(char c) { + if (tail >= queue.length) { + if (tail - head > queue.length/2) { + char[] queue2 = new char[queue.length * 2]; + System.arraycopy(queue, head, queue2, 0, tail-head); + } else { + System.arraycopy(queue, head, queue, 0, tail-head); + } + tail = tail-head; + head = 0; + } + queue[tail++] = c; + } + + public void close() throws IOException { r.close(); } + public int read() throws IOException { + if (tail>head) + return queue[head++]; + return r.read(); + } + public int read(char cbuf[]) throws IOException { return read(cbuf, 0, cbuf.length); } + public int read(char cbuf[], int off, int len) throws IOException { + if (tail>head) { + int count = Math.min(len, tail-head); + System.arraycopy(queue, head, cbuf, off, count); + head += count; + return count; + } + return r.read(cbuf, off, len); + } + public long skip(long n) throws IOException { return r.skip(n); } + public boolean ready() throws IOException { return true; } + public boolean markSupported() { return false; } + public void mark(int readAheadLimit) throws IOException { throw new IOException("not supported"); } + public void reset() throws IOException { throw new IOException("not supported"); } + } + + private final RollbackReader r; + public CharInput(Reader r, String s) { + this.name = s; + this.r = new RollbackReader(new BufferedReader(r)); + } + public CharInput(String s) { this(new StringReader(s)); } + public CharInput(Reader r) { this(r, null); } + public CharInput(InputStream i) { this(i, null); } + public CharInput(InputStream i, String s) { this(new InputStreamReader(i), s); } + public CharInput(File f) throws IOException { this(new FileInputStream(f), f.getName()); } + + public CharInput(InputStream i, String s, boolean indent) { + this(new InputStreamReader(i), s); + this.indent = indent; + } + public String getName() { return name; } + + private String name; boolean cr = false; + boolean indent = false; private int count = 0; private StringBuilder cache = new StringBuilder(); @@ -28,21 +86,77 @@ public class CharInput extends Cartesian.Input { else if (cache == null) cache = new StringBuilder(); } + int indentation = -1; + int lastIndentation = 0; + int delta = 0; + public boolean isCR() { return cr; } public Character _next() throws IOException { + Character ret = __next(); + if (ret==null) return null; + char c = ret.charValue(); + if (indent) { + if (ret==CharAtom.left) System.out.print("\033[31m{\033[0m"); + else if (ret==CharAtom.right) System.out.print("\033[31m}\033[0m"); + else System.out.print(c+""); + } + return ret; + } + public Character __next() throws IOException { + cr = false; + int i = r.read(); - if (i==-1) { /*System.err.print("\r...done \r"); */return null; } + if (i==-1) { + if (indent && indentation >= 0) { + redent(indentation - lastIndentation); + lastIndentation = indentation; + indentation = -1; + return __next(); + } + return null; + } char c = (char)i; if (cache != null) cache.append(c); cr = c=='\n'; - /* - if ((count++) % 100 == 0) - System.err.print(" " + count + "\r"); - */ + + if (indent) + if (cr && ignore) { + ignore = false; + } else if (cr) { + while(true) { + indentation = 0; + do { i = r.read(); if (i==' ') indentation++; } while (i==' '); + if (i=='\n') { /* FIXME */ continue; } + if (i==-1) { /* FIXME */ } + if (indentation - lastIndentation > 0) { + r.unread('\n'); + for(int j=0; j0) { r.unread(CharAtom.left); redent(i-1); return; } + } + public String showRegion(Region rc) { if (cache == null) return null; Cartesian.Region r = (Cartesian.Region)rc;