1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.cdt.internal.ui.text;
14 import java.io.IOException;
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
20 * Reads from a document either forwards or backwards. May be configured to
21 * skip comments and strings.
23 public class CCodeReader extends SingleCharReader {
25 /** The EOF character */
26 public static final int EOF= -1;
28 private boolean fSkipComments= false;
29 private boolean fSkipStrings= false;
30 private boolean fForward= false;
32 private IDocument fDocument;
36 private int fCachedLineNumber= -1;
37 private int fCachedLineOffset= -1;
40 public CCodeReader() {
44 * Returns the offset of the last read character. Should only be called after read has been called.
46 public int getOffset() {
47 return fForward ? fOffset -1 : fOffset;
50 public void configureForwardReader(IDocument document, int offset, int length, boolean skipComments, boolean skipStrings) throws IOException {
53 fSkipComments= skipComments;
54 fSkipStrings= skipStrings;
57 fEnd= Math.min(fDocument.getLength(), fOffset + length);
60 public void configureBackwardReader(IDocument document, int offset, boolean skipComments, boolean skipStrings) throws IOException {
63 fSkipComments= skipComments;
64 fSkipStrings= skipStrings;
68 fCachedLineNumber= fDocument.getLineOfOffset(fOffset);
69 } catch (BadLocationException x) {
70 throw new IOException(x.getMessage());
78 public void close() throws IOException {
83 * @see SingleCharReader#read()
86 public int read() throws IOException {
88 return fForward ? readForwards() : readBackwards();
89 } catch (BadLocationException x) {
90 throw new IOException(x.getMessage());
94 private void gotoCommentEnd() throws BadLocationException {
95 while (fOffset < fEnd) {
96 char current= fDocument.getChar(fOffset++);
98 if (fOffset < fEnd && fDocument.getChar(fOffset) == '/') {
106 private void gotoStringEnd(char delimiter) throws BadLocationException {
107 while (fOffset < fEnd) {
108 char current= fDocument.getChar(fOffset++);
109 if (current == '\\') {
110 // ignore escaped characters
112 } else if (current == delimiter) {
118 private void gotoLineEnd() throws BadLocationException {
119 int line= fDocument.getLineOfOffset(fOffset);
120 fOffset= fDocument.getLineOffset(line + 1);
123 private int readForwards() throws BadLocationException {
124 while (fOffset < fEnd) {
125 char current= fDocument.getChar(fOffset++);
130 if (fSkipComments && fOffset < fEnd) {
131 char next= fDocument.getChar(fOffset);
133 // a comment starts, advance to the comment end
137 } else if (next == '/') {
138 // '//'-comment starts, advance to the line end
150 gotoStringEnd(current);
163 private void handleSingleLineComment() throws BadLocationException {
164 int line= fDocument.getLineOfOffset(fOffset);
165 if (line < fCachedLineNumber) {
166 fCachedLineNumber= line;
167 fCachedLineOffset= fDocument.getLineOffset(line);
169 while (fCachedLineOffset < offset) {
170 char current= fDocument.getChar(offset--);
171 if (current == '/' && fCachedLineOffset <= offset && fDocument.getChar(offset) == '/') {
179 private void gotoCommentStart() throws BadLocationException {
180 while (0 < fOffset) {
181 char current= fDocument.getChar(fOffset--);
182 if (current == '*' && 0 <= fOffset && fDocument.getChar(fOffset) == '/')
187 private void gotoStringStart(char delimiter) throws BadLocationException {
188 while (0 < fOffset) {
189 char current= fDocument.getChar(fOffset);
190 if (current == delimiter) {
191 if ( !(0 <= fOffset && fDocument.getChar(fOffset -1) == '\\'))
198 private int readBackwards() throws BadLocationException {
200 while (0 < fOffset) {
203 handleSingleLineComment();
205 char current= fDocument.getChar(fOffset);
209 if (fSkipComments && fOffset > 1) {
210 char next= fDocument.getChar(fOffset - 1);
212 // a comment ends, advance to the comment start
226 gotoStringStart(current);