~vil/pydev/upstream

« back to all changes in this revision

Viewing changes to org.python.pydev.parser/src/org/python/pydev/parser/jython/FastCharStream.java

  • Committer: Vladimír Lapáček
  • Date: 2006-08-30 18:38:44 UTC
  • Revision ID: vladimir.lapacek@gmail.com-20060830183844-f4d82c1239a7770a
Initial import of upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.python.pydev.parser.jython;
 
2
 
 
3
import java.io.IOException;
 
4
 
 
5
/**
 
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).
 
9
 * 
 
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)
 
13
 */
 
14
 
 
15
public final class FastCharStream implements CharStream {
 
16
 
 
17
        private char[] buffer;
 
18
 
 
19
        private int bufline[];
 
20
 
 
21
        private int bufcolumn[];
 
22
 
 
23
        private boolean prevCharIsCR = false;
 
24
 
 
25
        private boolean prevCharIsLF = false;
 
26
 
 
27
        private int column = 0;
 
28
 
 
29
        private int line = 1;
 
30
 
 
31
        private int bufpos = -1;
 
32
 
 
33
        private int updatePos;
 
34
        
 
35
        private int tokenBegin;
 
36
        
 
37
        private static IOException ioException;
 
38
 
 
39
        private static final boolean DEBUG = false;
 
40
 
 
41
        public FastCharStream(String initialDoc) {
 
42
                this.buffer = initialDoc.toCharArray();
 
43
                this.bufline = new int[initialDoc.length()];
 
44
                this.bufcolumn = new int[initialDoc.length()];
 
45
        }
 
46
 
 
47
        public final char readChar() throws IOException {
 
48
                try {
 
49
                    bufpos++;
 
50
                        char r = this.buffer[bufpos];
 
51
                        
 
52
                        if(bufpos >= updatePos){
 
53
                                updatePos++;
 
54
                                
 
55
                                //start UpdateLineCol
 
56
                                column++;
 
57
                                
 
58
                                if (prevCharIsLF) {
 
59
                                        prevCharIsLF = false;
 
60
                                        line += (column = 1);
 
61
                                        
 
62
                                } else if (prevCharIsCR) {
 
63
                                        
 
64
                                        prevCharIsCR = false;
 
65
                                        if (r == '\n') {
 
66
                                                prevCharIsLF = true;
 
67
                                        } else {
 
68
                                                line += (column = 1);
 
69
                                        }
 
70
                                }
 
71
                                
 
72
                                if(r == '\r'){
 
73
                                        prevCharIsCR = true;
 
74
                                        
 
75
                                }else if(r == '\n'){
 
76
                                        prevCharIsLF = true;
 
77
                                        
 
78
                                }
 
79
                                
 
80
                                bufline[bufpos] = line;
 
81
                                bufcolumn[bufpos] = column;
 
82
                                //end UpdateLineCol
 
83
                        }
 
84
                        
 
85
                        return r;
 
86
                } catch (ArrayIndexOutOfBoundsException e) {
 
87
                    bufpos--;
 
88
                    if (ioException == null){
 
89
                        ioException = new IOException();
 
90
                    }
 
91
                        throw ioException;
 
92
                }
 
93
        }
 
94
 
 
95
        /**
 
96
         * @deprecated
 
97
         * @see #getEndColumn
 
98
         */
 
99
 
 
100
        public final int getColumn() {
 
101
                return bufcolumn[bufpos];
 
102
        }
 
103
 
 
104
        /**
 
105
         * @deprecated
 
106
         * @see #getEndLine
 
107
         */
 
108
 
 
109
        public final int getLine() {
 
110
                return bufline[bufpos];
 
111
        }
 
112
 
 
113
        public final int getEndColumn() {
 
114
                return bufcolumn[bufpos];
 
115
        }
 
116
 
 
117
        public final int getEndLine() {
 
118
                return bufline[bufpos];
 
119
        }
 
120
 
 
121
        public final int getBeginColumn() {
 
122
                return bufcolumn[tokenBegin];
 
123
        }
 
124
 
 
125
        public final int getBeginLine() {
 
126
                return bufline[tokenBegin];
 
127
        }
 
128
 
 
129
        public final void backup(int amount) {
 
130
                if(DEBUG){
 
131
                        System.out.println("FastCharStream: backup >>"+amount+"<<");
 
132
                }
 
133
                bufpos -= amount;
 
134
        }
 
135
 
 
136
        public final char BeginToken() throws IOException {
 
137
                char c = readChar();
 
138
                tokenBegin = bufpos;
 
139
                if(DEBUG){
 
140
                        System.out.println("FastCharStream: BeginToken >>"+(int)c+"<<");
 
141
                }
 
142
                return c;
 
143
        }
 
144
 
 
145
        public final String GetImage() {
 
146
                if (bufpos >= tokenBegin) {
 
147
                        return new String(buffer, tokenBegin, bufpos - tokenBegin+1);
 
148
                } else {
 
149
                        return new String(buffer, tokenBegin, buffer.length - tokenBegin+1);
 
150
                }
 
151
        }
 
152
 
 
153
        public final char[] GetSuffix(int len) {
 
154
 
 
155
                char[] ret = new char[len];
 
156
                if (len > 0) {
 
157
                        try {
 
158
                int initial = bufpos - len +1;
 
159
                if(initial < 0){
 
160
                    int initial0 = initial;
 
161
                    len += initial;
 
162
                    initial = 0;
 
163
                    System.arraycopy(buffer, initial, ret, -initial0, len);
 
164
                }else{
 
165
                    System.arraycopy(buffer, initial, ret, 0, len);
 
166
                }
 
167
                        } catch (Exception e) {
 
168
                                e.printStackTrace();
 
169
                        }
 
170
                }
 
171
                if(DEBUG){
 
172
                        System.out.println("FastCharStream: GetSuffix:"+len+" >>"+new String(ret)+"<<");
 
173
                }
 
174
                return ret;
 
175
        }
 
176
 
 
177
        public final void Done() {
 
178
                buffer = null;
 
179
                bufline = null;
 
180
                bufcolumn = null;
 
181
        }
 
182
 
 
183
}