update metagrammar
[sbp.git] / src / edu / berkeley / sbp / chr / CharInput.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp.chr;
4 import java.io.*;
5 import java.util.*;
6 import java.lang.reflect.*;
7 import java.lang.ref.*;
8 import edu.berkeley.sbp.*;
9 import edu.berkeley.sbp.util.*;
10 import edu.berkeley.sbp.misc.*;
11 import edu.berkeley.sbp.Input.Location;
12
13 public class CharInput extends Cartesian.Input<Character> {
14
15     private static class RollbackReader extends Reader {
16         private final Reader r;
17         public RollbackReader(Reader r) { this.r = r; }
18         
19         private char[] queue = new char[1024];
20         private int head = 0;
21         private int tail = 0;
22
23         private void unread(char c) {
24             if (tail >= queue.length) {
25                 if (tail - head > queue.length/2) {
26                     char[] queue2 = new char[queue.length * 2];
27                     System.arraycopy(queue, head, queue2, 0, tail-head);
28                 } else {
29                     System.arraycopy(queue, head, queue, 0, tail-head);
30                 }
31                 tail = tail-head;
32                 head = 0;
33             }
34             queue[tail++] = c;
35         }
36
37         public void close() throws IOException { r.close(); }
38         public int read() throws IOException {
39             if (tail>head)
40                 return queue[head++];
41             return r.read();
42         }
43         public int read(char cbuf[]) throws IOException { return read(cbuf, 0, cbuf.length); }
44         public int read(char cbuf[], int off, int len) throws IOException {
45             if (tail>head) {
46                 int count = Math.min(len, tail-head);
47                 System.arraycopy(queue, head, cbuf, off, count);
48                 head += count;
49                 return count;
50             }
51             return r.read(cbuf, off, len);
52         }
53         public long skip(long n) throws IOException { return r.skip(n); }
54         public boolean ready() throws IOException { return true; }
55         public boolean markSupported() { return false; }
56         public void mark(int readAheadLimit) throws IOException { throw new IOException("not supported"); }
57         public void reset() throws IOException { throw new IOException("not supported"); }
58     }
59
60     private final RollbackReader r;
61     
62     public CharInput(Reader r,      String s) {
63         this.name = s;
64         this.r = new RollbackReader(new BufferedReader(r));
65     }
66     public CharInput(String s)                  { this(new StringReader(s)); }
67     public CharInput(Reader r)                  { this(r, null); }
68     public CharInput(InputStream i)             { this(i, null); }
69     public CharInput(InputStream i, String s)   { this(new InputStreamReader(i), s); }
70     public CharInput(File f) throws IOException { this(new FileInputStream(f), f.getName()); }
71
72     public CharInput(InputStream i, String s, boolean indent) {
73         this(new InputStreamReader(i), s);
74         this.indent = indent;
75     }
76     public String getName() { return name; }
77
78     private String name;
79     boolean cr = false;
80     boolean indent = false;
81     private int count = 0;
82     private StringBuilder cache = new StringBuilder();
83     
84     public void setCacheEnabled(boolean enabled) {
85         if (!enabled) cache = null;
86         else if (cache == null) cache = new StringBuilder();
87     }
88
89     int indentation = -1;
90     int lastIndentation = 0;
91     int delta = 0;
92
93     public boolean   isCR() { return cr; }
94     public Character _next() throws IOException {
95         Character ret = __next();
96         if (ret==null) return null;
97         char c = ret.charValue();
98         if (indent) {
99             if (ret==CharAtom.left)  System.out.print("\033[31m{\033[0m");
100             else if (ret==CharAtom.right) System.out.print("\033[31m}\033[0m");
101             else System.out.print(c+"");
102         }
103         return ret;
104     }
105     public Character __next() throws IOException {
106
107         cr = false;
108
109         int i = r.read();
110         if (i==-1) {
111             if (indent && indentation >= 0) {
112                 redent(indentation - lastIndentation);
113                 lastIndentation = indentation;
114                 indentation = -1;
115                 return __next();
116             }
117             return null;
118         }
119         char c = (char)i;
120         if (cache != null) cache.append(c);
121         cr = c=='\n';
122
123         if (indent)
124             if (cr && ignore) {
125                 ignore = false;
126             } else if (cr) {
127                 while(true) {
128                     indentation = 0;
129                     do { i = r.read(); if (i==' ') indentation++; } while (i==' ');
130                     if (i=='\n') { /* FIXME */ continue; }
131                     if (i==-1) { /* FIXME */ }
132                      if (indentation - lastIndentation > 0) {
133                         r.unread('\n');
134                         for(int j=0; j<indentation; j++) r.unread(' ');
135                         redent(indentation - lastIndentation);
136                     } else {
137                         redent(indentation - lastIndentation);
138                         r.unread('\n');
139                         for(int j=0; j<indentation; j++) r.unread(' ');
140                     }
141                     if (i != -1) r.unread((char)i);
142                     ignore = true;
143                     break;
144                 }
145                 lastIndentation = indentation;
146                 indentation = -1;
147                 return __next();
148             }
149
150         return c;
151     }
152
153     private boolean ignore = false;
154
155     private void redent(int i) {
156         if (i<0) { r.unread(CharAtom.right); redent(i+1); return; }
157         if (i>0) { r.unread(CharAtom.left); redent(i-1); return; }
158     }
159
160     public String showRegion(Region<Character> rc) {
161         if (cache == null) return null;
162         Cartesian.Region r = (Cartesian.Region)rc;
163         int start = r.getStart().getScalar()+1;
164         int end = r.getEnd().getScalar()+1;
165         if (start < 0) start = 0;
166         if (end < start) end = start;
167         if (end > cache.length()) end = cache.length();
168         String ret;
169         if (end-start < 60) ret = cachesubstring(start, end);
170         else ret = cachesubstring(start, start+25) +
171                  "..." +
172                  cachesubstring(end-25, end);
173         return StringUtil.escapify(ret, "\n\r");
174     }
175
176     private String cachesubstring(int start, int end) {
177         if (start < 0) start = 0;
178         if (end < 0)   end = 0;
179         if (start >= cache.length()) start = cache.length();
180         if (end >= cache.length()) end = cache.length();
181         return cache.substring(start, end);
182     }
183
184 }