1 package net.sourceforge.phpdt.internal.ui.text;
4 * (c) Copyright IBM Corp. 2000, 2001.
8 import java.io.IOException;
10 import net.sourceforge.phpdt.internal.corext.phpdoc.SingleCharReader;
12 import org.eclipse.jface.text.BadLocationException;
13 import org.eclipse.jface.text.IDocument;
16 * Reads from a document either forwards or backwards. May be configured to skip comments and strings.
18 public class PHPCodeReader extends SingleCharReader {
20 /** The EOF character */
21 public static final int EOF = -1;
23 private boolean fSkipComments = false;
25 private boolean fSkipStrings = false;
27 private boolean fForward = false;
29 private IDocument fDocument;
33 private int fEnd = -1;
35 private int fCachedLineNumber = -1;
37 private int fCachedLineOffset = -1;
39 public PHPCodeReader() {
43 * Returns the offset of the last read character. Should only be called after read has been called.
45 public int getOffset() {
46 return fForward ? fOffset - 1 : fOffset;
49 public void configureForwardReader(IDocument document, int offset, int length, boolean skipComments, boolean skipStrings)
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());
77 public void close() throws IOException {
82 * @see SingleCharReader#read()
84 public int read() throws IOException {
86 return fForward ? readForwards() : readBackwards();
87 } catch (BadLocationException x) {
88 throw new IOException(x.getMessage());
92 private void gotoCommentEnd() throws BadLocationException {
93 while (fOffset < fEnd) {
94 char current = fDocument.getChar(fOffset++);
96 if (fOffset < fEnd && fDocument.getChar(fOffset) == '/') {
104 private void gotoStringEnd(char delimiter) throws BadLocationException {
105 while (fOffset < fEnd) {
106 char current = fDocument.getChar(fOffset++);
107 if (current == '\\') {
108 // ignore escaped characters
110 } else if (current == delimiter) {
116 private void gotoLineEnd() throws BadLocationException {
117 int line = fDocument.getLineOfOffset(fOffset);
118 fOffset = fDocument.getLineOffset(line + 1);
121 private int readForwards() throws BadLocationException {
122 while (fOffset < fEnd) {
123 char current = fDocument.getChar(fOffset++);
130 gotoStringEnd(current);
137 if (fSkipComments && fOffset < fEnd) {
146 if (fSkipComments && fOffset < fEnd) {
147 char next = fDocument.getChar(fOffset);
149 // a comment starts, advance to the comment end
153 } else if (next == '/') {
154 // '//'-comment starts, advance to the line end
170 private void handleSingleLineComment() throws BadLocationException {
171 int line = fDocument.getLineOfOffset(fOffset);
172 if (line < fCachedLineNumber) {
173 fCachedLineNumber = line;
174 fCachedLineOffset = fDocument.getLineOffset(line);
175 int offset = fOffset;
176 while (fCachedLineOffset < offset) {
177 char current = fDocument.getChar(offset--);
179 if (current == '/' && fCachedLineOffset <= offset && fDocument.getChar(offset) == '/') {
184 if (current == '#' && fCachedLineOffset <= offset) {
192 private void gotoCommentStart() throws BadLocationException {
193 while (0 < fOffset) {
194 char current = fDocument.getChar(fOffset--);
195 if (current == '*' && 0 <= fOffset && fDocument.getChar(fOffset) == '/')
200 private void gotoStringStart(char delimiter) throws BadLocationException {
201 while (0 < fOffset) {
202 char current = fDocument.getChar(fOffset);
203 if (current == delimiter) {
204 if (!(0 <= fOffset && fDocument.getChar(fOffset - 1) == '\\'))
211 private int readBackwards() throws BadLocationException {
213 while (0 < fOffset) {
216 handleSingleLineComment();
218 char current = fDocument.getChar(fOffset);
222 if (fSkipComments && fOffset > 1) {
223 char next = fDocument.getChar(fOffset - 1);
225 // a comment ends, advance to the comment start
238 gotoStringStart(current);