1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
13 import net.sourceforge.phpeclipse.ui.text.rules.AbstractPartitioner;
15 import org.eclipse.jface.text.Assert;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.rules.ICharacterScanner;
18 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
19 import org.eclipse.jface.text.rules.IToken;
20 import org.eclipse.jface.text.rules.Token;
23 * This scanner recognizes the JavaDoc comments, Java multi line comments, Java single line comments, Java strings.
25 public class FastJavaPartitionScanner implements IPartitionTokenScanner, IPHPPartitions {
28 private static final int PHP = 0;
30 private static final int SINGLE_LINE_COMMENT = 1;
32 private static final int MULTI_LINE_COMMENT = 2;
34 private static final int PHPDOC = 3;
36 private static final int STRING_DQ = 4;
38 private static final int STRING_SQ = 5;
40 // private static final int CHARACTER= 4;
42 // beginning of prefixes and postfixes
43 private static final int NONE = 0;
45 private static final int BACKSLASH = 1; // postfix for STRING and CHARACTER
47 private static final int SLASH = 2; // prefix for SINGLE_LINE or MULTI_LINE or JAVADOC
49 private static final int SLASH_STAR = 3; // prefix for MULTI_LINE_COMMENT or JAVADOC
51 private static final int SLASH_STAR_STAR = 4; // prefix for MULTI_LINE_COMMENT or JAVADOC
53 private static final int STAR = 5; // postfix for MULTI_LINE_COMMENT or JAVADOC
55 private static final int CARRIAGE_RETURN = 6; // postfix for STRING, CHARACTER and SINGLE_LINE_COMMENT
58 private final BufferedDocumentScanner fScanner = new BufferedDocumentScanner(1000); // faster implementation
60 /** The offset of the last returned token. */
61 private int fTokenOffset;
63 /** The length of the last returned token. */
64 private int fTokenLength;
66 /** The state of the scanner. */
69 /** The last significant characters read. */
72 /** The amount of characters already read on first call to nextToken(). */
73 private int fPrefixLength;
75 // emulate JavaPartitionScanner
76 private boolean fEmulate = false;
78 private int fJavaOffset;
80 private int fJavaLength;
82 private final IToken[] fTokens = new IToken[] { new Token(null), new Token(PHP_SINGLELINE_COMMENT),
83 new Token(PHP_MULTILINE_COMMENT), new Token(PHP_PHPDOC_COMMENT), new Token(PHP_STRING_DQ), new Token(PHP_STRING_SQ) };
85 public FastJavaPartitionScanner(boolean emulate) {
89 public FastJavaPartitionScanner() {
94 * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
96 public IToken nextToken() {
98 // emulate JavaPartitionScanner
100 if (fJavaOffset != -1 && fTokenOffset + fTokenLength != fJavaOffset + fJavaLength) {
101 fTokenOffset += fTokenLength;
109 fTokenOffset += fTokenLength;
110 fTokenLength = fPrefixLength;
113 final int ch = fScanner.read();
117 case ICharacterScanner.EOF:
118 if (fTokenLength > 0) {
119 fLast = NONE; // ignore last
120 return preFix(fState, PHP, NONE, 0);
129 // emulate JavaPartitionScanner
130 if (!fEmulate && fLast != CARRIAGE_RETURN) {
131 fLast = CARRIAGE_RETURN;
138 case SINGLE_LINE_COMMENT:
142 if (fTokenLength > 0) {
143 IToken token = fTokens[fState];
145 // emulate JavaPartitionScanner
151 fLast = CARRIAGE_RETURN;
171 case SINGLE_LINE_COMMENT:
175 // assert(fTokenLength > 0);
176 return postFix(fState);
184 if (!fEmulate && fLast == CARRIAGE_RETURN) {
186 case SINGLE_LINE_COMMENT:
205 newState = STRING_SQ;
210 newState = STRING_DQ;
214 last = CARRIAGE_RETURN;
229 fLast = NONE; // ignore fLast
230 return preFix(fState, newState, last, 1);
243 if (fTokenLength > 0) {
244 return preFix(PHP, SINGLE_LINE_COMMENT, NONE, 1);
246 preFix(PHP, SINGLE_LINE_COMMENT, NONE, 1);
247 fTokenOffset += fTokenLength;
248 fTokenLength = fPrefixLength;
252 if (fLast == SLASH) {
253 if (fTokenLength - getLastLength(fLast) > 0) {
254 return preFix(PHP, SINGLE_LINE_COMMENT, NONE, 2);
256 preFix(PHP, SINGLE_LINE_COMMENT, NONE, 2);
257 fTokenOffset += fTokenLength;
258 fTokenLength = fPrefixLength;
269 if (fLast == SLASH) {
270 if (fTokenLength - getLastLength(fLast) > 0)
271 return preFix(PHP, MULTI_LINE_COMMENT, SLASH_STAR, 2);
273 preFix(PHP, MULTI_LINE_COMMENT, SLASH_STAR, 2);
274 fTokenOffset += fTokenLength;
275 fTokenLength = fPrefixLength;
285 fLast = NONE; // ignore fLast
286 if (fTokenLength > 0)
287 return preFix(PHP, STRING_SQ, NONE, 1);
289 preFix(PHP, STRING_SQ, NONE, 1);
290 fTokenOffset += fTokenLength;
291 fTokenLength = fPrefixLength;
296 fLast = NONE; // ignore fLast
297 if (fTokenLength > 0)
298 return preFix(PHP, STRING_DQ, NONE, 1);
300 preFix(PHP, STRING_DQ, NONE, 1);
301 fTokenOffset += fTokenLength;
302 fTokenLength = fPrefixLength;
312 case SINGLE_LINE_COMMENT:
320 case SLASH_STAR_STAR:
321 return postFix(MULTI_LINE_COMMENT);
324 return postFix(PHPDOC);
343 case MULTI_LINE_COMMENT:
346 if (fLast == SLASH_STAR) {
347 fLast = SLASH_STAR_STAR;
358 return postFix(MULTI_LINE_COMMENT);
373 fLast = (fLast == BACKSLASH) ? NONE : BACKSLASH;
378 if (fLast != BACKSLASH) {
379 return postFix(STRING_DQ);
394 fLast = (fLast == BACKSLASH) ? NONE : BACKSLASH;
399 if (fLast != BACKSLASH) {
400 return postFix(STRING_SQ);
415 // fLast= (fLast == BACKSLASH) ? NONE : BACKSLASH;
420 // if (fLast != BACKSLASH) {
421 // return postFix(CHARACTER);
437 private static final int getLastLength(int last) {
445 case CARRIAGE_RETURN:
454 case SLASH_STAR_STAR:
459 private final void consume() {
464 private final IToken postFix(int state) {
469 return fTokens[state];
472 private final IToken preFix(int state, int newState, int last, int prefixLength) {
473 // emulate JavaPartitionScanner
474 if (fEmulate && state == PHP && (fTokenLength - getLastLength(fLast) > 0)) {
475 fTokenLength -= getLastLength(fLast);
476 fJavaOffset = fTokenOffset;
477 fJavaLength = fTokenLength;
480 fPrefixLength = prefixLength;
482 return fTokens[state];
485 fTokenLength -= getLastLength(fLast);
487 fPrefixLength = prefixLength;
488 IToken token = fTokens[state];
494 private static int getState(String contentType) {
496 if (contentType == null)
499 else if (contentType.equals(PHP_SINGLELINE_COMMENT))
500 return SINGLE_LINE_COMMENT;
502 else if (contentType.equals(PHP_MULTILINE_COMMENT))
503 return MULTI_LINE_COMMENT;
505 else if (contentType.equals(PHP_PHPDOC_COMMENT))
508 else if (contentType.equals(PHP_STRING_DQ))
510 else if (contentType.equals(PHP_STRING_SQ))
512 // else if (contentType.equals(JAVA_CHARACTER))
520 * @see IPartitionTokenScanner#setPartialRange(IDocument, int, int, String, int)
522 public void setPartialRange(IDocument document, int offset, int length, String contentType, int partitionOffset) {
523 fScanner.setRange(document, offset, length);
524 setRange(document, offset, length);
525 fTokenOffset = partitionOffset;
527 fPrefixLength = offset - partitionOffset;
530 if (offset == partitionOffset) {
531 // restart at beginning of partition
534 fState = getState(contentType);
537 // emulate JavaPartitionScanner
545 * @see ITokenScanner#setRange(IDocument, int, int)
547 public void setRange(IDocument document, int offset, int length) {
548 fScanner.setRange(document, offset, length);
549 fTokenOffset = offset;
555 // emulate JavaPartitionScanner
563 * @see ITokenScanner#getTokenLength()
565 public int getTokenLength() {
570 * @see ITokenScanner#getTokenOffset()
572 public int getTokenOffset() {
573 if (AbstractPartitioner.DEBUG) {
574 Assert.isTrue(fTokenOffset >= 0, Integer.toString(fTokenOffset));