1 35 37 package com.google.gwt.dev.js.rhino; 38 39 import java.io.Reader ; 40 import java.io.IOException ; 41 42 55 final class LineBuffer { 56 65 static final int BUFLEN = 256; 66 67 LineBuffer(Reader in, int lineno) { 68 this.in = in; 69 this.lineno = lineno; 70 } 71 72 int read() throws IOException { 73 for(;;) { 74 if (end == offset && !fill()) 75 return -1; 76 77 int c = buffer[offset]; 78 ++offset; 79 80 if ((c & EOL_HINT_MASK) == 0) { 81 switch (c) { 82 case '\r': 83 if (offset != end) { 85 if (buffer[offset] == '\n') 86 ++offset; 87 } else { 88 lastWasCR = true; 91 } 92 case '\n': case '\u2028': case '\u2029': 94 prevStart = lineStart; 95 lineStart = offset; 96 lineno++; 97 return '\n'; 98 } 99 } 100 101 if (c < 128 || !formatChar(c)) { 102 return c; 103 } 104 } 105 } 106 107 void unread() { 108 111 if (offset == 0 && !hitEOF) Context.codeBug(); 118 119 if (offset == 0) return; 121 offset--; 122 int c = buffer[offset]; 123 if ((c & EOL_HINT_MASK) == 0 && eolChar(c)) { 124 lineStart = prevStart; 125 lineno--; 126 } 127 } 128 129 private void skipFormatChar() { 130 if (checkSelf && !formatChar(buffer[offset])) Context.codeBug(); 131 132 if (offset != 0) { 138 char tmp = buffer[offset]; 139 buffer[offset] = buffer[offset - 1]; 140 buffer[offset - 1] = tmp; 141 } 142 else if (otherEnd != 0) { 143 char tmp = buffer[offset]; 144 buffer[offset] = otherBuffer[otherEnd - 1]; 145 otherBuffer[otherEnd - 1] = tmp; 146 } 147 148 ++offset; 149 } 150 151 int peek() throws IOException { 152 for (;;) { 153 if (end == offset && !fill()) { 154 return -1; 155 } 156 157 int c = buffer[offset]; 158 if ((c & EOL_HINT_MASK) == 0 && eolChar(c)) { 159 return '\n'; 160 } 161 if (c < 128 || !formatChar(c)) { 162 return c; 163 } 164 165 skipFormatChar(); 166 } 167 } 168 169 boolean match(int test) throws IOException { 170 if ((test & EOL_HINT_MASK) == 0 && eolChar(test)) 172 Context.codeBug(); 173 if (test >= 128 && formatChar(test)) 175 Context.codeBug(); 176 177 for (;;) { 178 if (end == offset && !fill()) 179 return false; 180 181 int c = buffer[offset]; 182 if (test == c) { 183 ++offset; 184 return true; 185 } 186 if (c < 128 || !formatChar(c)) { 187 return false; 188 } 189 skipFormatChar(); 190 } 191 } 192 193 String getLine() { 195 int i = offset; 197 while(true) { 198 if (i == end) { 199 if (end == buffer.length) { 203 char[] tmp = new char[buffer.length * 2]; 204 System.arraycopy(buffer, 0, tmp, 0, end); 205 buffer = tmp; 206 } 207 int charsRead = 0; 208 try { 209 charsRead = in.read(buffer, end, buffer.length - end); 210 } catch (IOException ioe) { 211 break; 213 } 214 if (charsRead < 0) 215 break; 216 end += charsRead; 217 } 218 int c = buffer[i]; 219 if ((c & EOL_HINT_MASK) == 0 && eolChar(c)) 220 break; 221 i++; 222 } 223 224 int start = lineStart; 225 if (lineStart < 0) { 226 StringBuffer sb = new StringBuffer (otherEnd - otherStart + i); 228 sb.append(otherBuffer, otherStart, otherEnd - otherStart); 229 sb.append(buffer, 0, i); 230 return sb.toString(); 231 } else { 232 return new String (buffer, lineStart, i - lineStart); 233 } 234 } 235 236 int getOffset() { 239 if (lineStart < 0) 240 return offset + (otherEnd - otherStart); 242 else 243 return offset - lineStart; 244 } 245 246 private boolean fill() throws IOException { 247 if (checkSelf && !(end == offset)) Context.codeBug(); 249 250 char[] tempBuffer = buffer; 252 buffer = otherBuffer; 253 otherBuffer = tempBuffer; 254 255 if (buffer == null) { 257 buffer = new char[BUFLEN]; 258 } 259 260 if (lineStart >= 0) { 262 otherStart = lineStart; 263 } else { 264 otherStart = 0; 266 } 267 268 otherEnd = end; 269 270 prevStart = lineStart = (otherBuffer == null) ? 0 : -1; 273 274 offset = 0; 275 end = in.read(buffer, 0, buffer.length); 276 if (end < 0) { 277 end = 0; 278 279 283 hitEOF = true; 284 return false; 285 } 286 287 290 if (lastWasCR) { 297 if (buffer[0] == '\n') { 298 offset++; 299 if (end == 1) 300 return fill(); 301 } 302 lineStart = offset; 303 lastWasCR = false; 304 } 305 return true; 306 } 307 308 int getLineno() { return lineno; } 309 boolean eof() { return hitEOF; } 310 311 private static boolean formatChar(int c) { 312 return Character.getType((char)c) == Character.FORMAT; 313 } 314 315 private static boolean eolChar(int c) { 316 return c == '\r' || c == '\n' || c == '\u2028' || c == '\u2029'; 317 } 318 319 private static final int EOL_HINT_MASK = 0xdfd0; 322 323 private Reader in; 324 private char[] otherBuffer = null; 325 private char[] buffer = null; 326 327 private int offset = 0; 329 private int end = 0; 330 private int otherEnd; 331 private int lineno; 332 333 private int lineStart = 0; 334 private int otherStart = 0; 335 private int prevStart = 0; 336 337 private boolean lastWasCR = false; 338 private boolean hitEOF = false; 339 340 private static final boolean checkSelf = true; 342 } 343 | Popular Tags |