1
package org.python.pydev.parser.jython;
3
import java.io.IOException;
6
* An implementation of interface CharStream, where the data is read from a Reader. Completely recreated so that we can read data directly from a String, as the
7
* initial implementation was highly inneficient when working only with a string (actually, if it was small, there would be no noticeable
8
* delays, but if it became big, then the improvement would be HUGE).
10
* It keeps the same semantics for line and column stuff (and shares the previous approach of keeping a buffer for this info).
11
* If we wanted we could optimize it for also taking less memory, but as there is usually not so many concurrent parses, this
12
* is probably not worth it -- and it would probably be a little slower)
15
public final class FastCharStream implements CharStream {
17
private char[] buffer;
19
private int bufline[];
21
private int bufcolumn[];
23
private boolean prevCharIsCR = false;
25
private boolean prevCharIsLF = false;
27
private int column = 0;
31
private int bufpos = -1;
33
private int updatePos;
35
private int tokenBegin;
37
private static IOException ioException;
39
private static final boolean DEBUG = false;
41
public FastCharStream(String initialDoc) {
42
this.buffer = initialDoc.toCharArray();
43
this.bufline = new int[initialDoc.length()];
44
this.bufcolumn = new int[initialDoc.length()];
47
public final char readChar() throws IOException {
50
char r = this.buffer[bufpos];
52
if(bufpos >= updatePos){
62
} else if (prevCharIsCR) {
80
bufline[bufpos] = line;
81
bufcolumn[bufpos] = column;
86
} catch (ArrayIndexOutOfBoundsException e) {
88
if (ioException == null){
89
ioException = new IOException();
100
public final int getColumn() {
101
return bufcolumn[bufpos];
109
public final int getLine() {
110
return bufline[bufpos];
113
public final int getEndColumn() {
114
return bufcolumn[bufpos];
117
public final int getEndLine() {
118
return bufline[bufpos];
121
public final int getBeginColumn() {
122
return bufcolumn[tokenBegin];
125
public final int getBeginLine() {
126
return bufline[tokenBegin];
129
public final void backup(int amount) {
131
System.out.println("FastCharStream: backup >>"+amount+"<<");
136
public final char BeginToken() throws IOException {
140
System.out.println("FastCharStream: BeginToken >>"+(int)c+"<<");
145
public final String GetImage() {
146
if (bufpos >= tokenBegin) {
147
return new String(buffer, tokenBegin, bufpos - tokenBegin+1);
149
return new String(buffer, tokenBegin, buffer.length - tokenBegin+1);
153
public final char[] GetSuffix(int len) {
155
char[] ret = new char[len];
158
int initial = bufpos - len +1;
160
int initial0 = initial;
163
System.arraycopy(buffer, initial, ret, -initial0, len);
165
System.arraycopy(buffer, initial, ret, 0, len);
167
} catch (Exception e) {
172
System.out.println("FastCharStream: GetSuffix:"+len+" >>"+new String(ret)+"<<");
177
public final void Done() {