Skip to main content
summaryrefslogtreecommitdiffstats
blob: 13de07bfd4803327ede07283430534d734bb24e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package org.eclipse.cdt.internal.ui.text;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */

import java.io.IOException;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;

/**
 * Reads from a document either forwards or backwards. May be configured to
 * skip comments and strings.
 */
public class CCodeReader extends SingleCharReader {
	
	/** The EOF character */
	public static final int EOF= -1;
	
	private boolean fSkipComments= false;
	private boolean fSkipStrings= false;
	private boolean fForward= false;
	
	private IDocument fDocument;
	private int fOffset;
	
	private int fEnd= -1;
	private int fCachedLineNumber= -1;
	private int fCachedLineOffset= -1;
	
	
	public CCodeReader() {
	}
	
	/**
	 * Returns the offset of the last read character. Should only be called after read has been called.
	 */
	public int getOffset() {
		return fForward ? fOffset -1 : fOffset;
	}
	
	public void configureForwardReader(IDocument document, int offset, int length, boolean skipComments, boolean skipStrings) throws IOException {
		fDocument= document;
		fOffset= offset;
		fSkipComments= skipComments;
		fSkipStrings= skipStrings;
		
		fForward= true;
		fEnd= Math.min(fDocument.getLength(), fOffset + length);		
	}
	
	public void configureBackwardReader(IDocument document, int offset, boolean skipComments, boolean skipStrings) throws IOException {
		fDocument= document;
		fOffset= offset;
		fSkipComments= skipComments;
		fSkipStrings= skipStrings;
		
		fForward= false;
		try {
			fCachedLineNumber= fDocument.getLineOfOffset(fOffset);
		} catch (BadLocationException x) {
			throw new IOException(x.getMessage());
		}
	}
	
	/*
	 * @see Reader#close()
	 */
	public void close() throws IOException {
		fDocument= null;
	}
	
	/*
	 * @see SingleCharReader#read()
	 */
	public int read() throws IOException {
		try {
			return fForward ? readForwards() : readBackwards();
		} catch (BadLocationException x) {
			throw new IOException(x.getMessage());
		}
	}
	
	private void gotoCommentEnd() throws BadLocationException {
		while (fOffset < fEnd) {
			char current= fDocument.getChar(fOffset++);
			if (current == '*') {
				if (fOffset < fEnd && fDocument.getChar(fOffset) == '/') {
					++ fOffset;
					return;
				}
			}
		}
	}
	
	private void gotoStringEnd(char delimiter) throws BadLocationException {
		while (fOffset < fEnd) {
			char current= fDocument.getChar(fOffset++);
			if (current == '\\') {
				// ignore escaped characters
				++ fOffset;
			} else if (current == delimiter) {
				return;
			}
		}
	}
	
	private void gotoLineEnd() throws BadLocationException {
		int line= fDocument.getLineOfOffset(fOffset);
		fOffset= fDocument.getLineOffset(line + 1);
	}
	
	private int readForwards() throws BadLocationException {
		while (fOffset < fEnd) {
			char current= fDocument.getChar(fOffset++);
			
			switch (current) {
				case '/':
					
					if (fSkipComments && fOffset < fEnd) {
						char next= fDocument.getChar(fOffset);
						if (next == '*') {
							// a comment starts, advance to the comment end
							++ fOffset;
							gotoCommentEnd();
							continue;
						} else if (next == '/') {
							// '//'-comment starts, advance to the line end
							gotoLineEnd();
							continue;
						}
					}
					
					return current;
					
				case '"':
				case '\'':
				
					if (fSkipStrings) {
						gotoStringEnd(current);
						continue;
					}
					
					return current;
			}
			
			return current;
		}
		
		return EOF;
	}
	
	private void handleSingleLineComment() throws BadLocationException {
		int line= fDocument.getLineOfOffset(fOffset);
		if (line < fCachedLineNumber) {
			fCachedLineNumber= line;
			fCachedLineOffset= fDocument.getLineOffset(line);
			int offset= fOffset;
			while (fCachedLineOffset < offset) {
				char current= fDocument.getChar(offset--);
				if (current == '/' && fCachedLineOffset <= offset && fDocument.getChar(offset) == '/') {
					fOffset= offset;
					return;
				}
			}
		}
	}
	
	private void gotoCommentStart() throws BadLocationException {
		while (0 < fOffset) {
			char current= fDocument.getChar(fOffset--);
			if (current == '*' && 0 <= fOffset && fDocument.getChar(fOffset) == '/')
				return;
		}
	}
	
	private void gotoStringStart(char delimiter) throws BadLocationException {
		while (0 < fOffset) {
			char current= fDocument.getChar(fOffset);
			if (current == delimiter) {
				if ( !(0 <= fOffset && fDocument.getChar(fOffset -1) == '\\'))
					return;
			}
			-- fOffset;
		}
	}
		
	private int readBackwards() throws BadLocationException {
		
		while (0 < fOffset) {
			-- fOffset;
			
			handleSingleLineComment();
			
			char current= fDocument.getChar(fOffset);
			switch (current) {
				case '/':
					
					if (fSkipComments && fOffset > 1) {
						char next= fDocument.getChar(fOffset - 1);
						if (next == '*') {
							// a comment ends, advance to the comment start
							fOffset -= 2;
							gotoCommentStart();
							continue;
						}
					}
					
					return current;
					
				case '"':
				case '\'':
				
					if (fSkipStrings) {
						-- fOffset;
						gotoStringStart(current);
						continue;
					}
					
					return current;
			}
			
			return current;
		}
		
		return EOF;
	}
}

Back to the top