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
24 * single line comments, Java strings.
26 public class FastJavaPartitionScanner implements IPartitionTokenScanner, IPHPPartitions {
29 private static final int PHP = 0;
31 private static final int SINGLE_LINE_COMMENT = 1;
33 private static final int MULTI_LINE_COMMENT = 2;
35 private static final int PHPDOC = 3;
37 private static final int STRING_DQ = 4;
39 private static final int STRING_SQ = 5;
41 private static final int STRING_HEREDOC = 6;
43 // beginning of prefixes and postfixes
44 private static final int NONE = 0;
46 private static final int BACKSLASH = 1; // postfix for STRING_DQ and CHARACTER
48 private static final int SLASH = 2; // prefix for SINGLE_LINE or MULTI_LINE or
51 private static final int SLASH_STAR = 3; // prefix for MULTI_LINE_COMMENT or
54 private static final int SLASH_STAR_STAR = 4; // prefix for MULTI_LINE_COMMENT
57 private static final int STAR = 5; // postfix for MULTI_LINE_COMMENT or
60 private static final int CARRIAGE_RETURN = 6; // postfix for STRING_DQ,
62 // SINGLE_LINE_COMMENT
64 // private static final int HEREDOC = 7;
67 private final BufferedDocumentScanner fScanner = new BufferedDocumentScanner(1000); // faster
70 /** The offset of the last returned token. */
71 private int fTokenOffset;
73 /** The length of the last returned token. */
74 private int fTokenLength;
76 /** The state of the scanner. */
79 /** The last significant characters read. */
82 /** The amount of characters already read on first call to nextToken(). */
83 private int fPrefixLength;
85 // emulate JavaPartitionScanner
86 private boolean fEmulate = false;
88 private int fJavaOffset;
90 private int fJavaLength;
92 private final IToken[] fTokens = new IToken[] { new Token(null), new Token(PHP_SINGLELINE_COMMENT),
93 new Token(PHP_MULTILINE_COMMENT), new Token(PHP_PHPDOC_COMMENT), new Token(PHP_STRING_DQ), new Token(PHP_STRING_SQ),
94 new Token(PHP_STRING_HEREDOC) };
96 public FastJavaPartitionScanner(boolean emulate) {
100 public FastJavaPartitionScanner() {
105 * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
107 public IToken nextToken() {
109 // emulate JavaPartitionScanner
111 if (fJavaOffset != -1 && fTokenOffset + fTokenLength != fJavaOffset + fJavaLength) {
112 fTokenOffset += fTokenLength;
120 fTokenOffset += fTokenLength;
121 fTokenLength = fPrefixLength;
124 final int ch = fScanner.read();
128 case ICharacterScanner.EOF:
129 if (fTokenLength > 0) {
130 fLast = NONE; // ignore last
131 return preFix(fState, PHP, NONE, 0);
140 // emulate JavaPartitionScanner
141 if (!fEmulate && fLast != CARRIAGE_RETURN) {
142 fLast = CARRIAGE_RETURN;
149 case SINGLE_LINE_COMMENT:
153 if (fTokenLength > 0) {
154 IToken token = fTokens[fState];
156 // emulate JavaPartitionScanner
162 fLast = CARRIAGE_RETURN;
182 case SINGLE_LINE_COMMENT:
186 // assert(fTokenLength > 0);
187 return postFix(fState);
195 if (fState == SINGLE_LINE_COMMENT) {
196 int nextch = fScanner.read();
198 // <h1>This is an <?php # echo 'simple' ?> example.</h1>
202 return postFix(fState);
205 // bug #1404228: Crash on <?php // comment ?>
211 if (!fEmulate && fLast == CARRIAGE_RETURN) {
213 case SINGLE_LINE_COMMENT:
232 newState = STRING_SQ;
237 newState = STRING_DQ;
241 last = CARRIAGE_RETURN;
256 fLast = NONE; // ignore fLast
257 return preFix(fState, newState, last, 1);
270 if (fTokenLength > 0) {
271 return preFix(PHP, SINGLE_LINE_COMMENT, NONE, 1);
273 preFix(PHP, SINGLE_LINE_COMMENT, NONE, 1);
274 fTokenOffset += fTokenLength;
275 fTokenLength = fPrefixLength;
279 if (fLast == SLASH) {
280 if (fTokenLength - getLastLength(fLast) > 0) {
281 return preFix(PHP, SINGLE_LINE_COMMENT, NONE, 2);
283 preFix(PHP, SINGLE_LINE_COMMENT, NONE, 2);
284 fTokenOffset += fTokenLength;
285 fTokenLength = fPrefixLength;
296 if (fLast == SLASH) {
297 if (fTokenLength - getLastLength(fLast) > 0)
298 return preFix(PHP, MULTI_LINE_COMMENT, SLASH_STAR, 2);
300 preFix(PHP, MULTI_LINE_COMMENT, SLASH_STAR, 2);
301 fTokenOffset += fTokenLength;
302 fTokenLength = fPrefixLength;
312 fLast = NONE; // ignore fLast
313 if (fTokenLength > 0)
314 return preFix(PHP, STRING_SQ, NONE, 1);
316 preFix(PHP, STRING_SQ, NONE, 1);
317 fTokenOffset += fTokenLength;
318 fTokenLength = fPrefixLength;
323 fLast = NONE; // ignore fLast
324 if (fTokenLength > 0)
325 return preFix(PHP, STRING_DQ, NONE, 1);
327 preFix(PHP, STRING_DQ, NONE, 1);
328 fTokenOffset += fTokenLength;
329 fTokenLength = fPrefixLength;
339 case SINGLE_LINE_COMMENT:
347 case SLASH_STAR_STAR:
348 return postFix(MULTI_LINE_COMMENT);
351 return postFix(PHPDOC);
370 case MULTI_LINE_COMMENT:
373 if (fLast == SLASH_STAR) {
374 fLast = SLASH_STAR_STAR;
385 return postFix(MULTI_LINE_COMMENT);
400 fLast = (fLast == BACKSLASH) ? NONE : BACKSLASH;
405 if (fLast != BACKSLASH) {
406 return postFix(STRING_DQ);
421 fLast = (fLast == BACKSLASH) ? NONE : BACKSLASH;
426 if (fLast != BACKSLASH) {
427 return postFix(STRING_SQ);
442 // fLast= (fLast == BACKSLASH) ? NONE : BACKSLASH;
447 // if (fLast != BACKSLASH) {
448 // return postFix(CHARACTER);
464 private static final int getLastLength(int last) {
472 case CARRIAGE_RETURN:
481 case SLASH_STAR_STAR:
486 private final void consume() {
491 private final IToken postFix(int state) {
496 return fTokens[state];
499 private final IToken preFix(int state, int newState, int last, int prefixLength) {
500 // emulate JavaPartitionScanner
501 if (fEmulate && state == PHP && (fTokenLength - getLastLength(fLast) > 0)) {
502 fTokenLength -= getLastLength(fLast);
503 fJavaOffset = fTokenOffset;
504 fJavaLength = fTokenLength;
507 fPrefixLength = prefixLength;
509 return fTokens[state];
512 fTokenLength -= getLastLength(fLast);
514 fPrefixLength = prefixLength;
515 IToken token = fTokens[state];
521 private static int getState(String contentType) {
523 if (contentType == null)
526 else if (contentType.equals(PHP_SINGLELINE_COMMENT))
527 return SINGLE_LINE_COMMENT;
529 else if (contentType.equals(PHP_MULTILINE_COMMENT))
530 return MULTI_LINE_COMMENT;
532 else if (contentType.equals(PHP_PHPDOC_COMMENT))
535 else if (contentType.equals(PHP_STRING_DQ))
538 else if (contentType.equals(PHP_STRING_SQ))
541 else if (contentType.equals(PHP_STRING_HEREDOC))
542 return STRING_HEREDOC;
544 // else if (contentType.equals(JAVA_CHARACTER))
552 * @see IPartitionTokenScanner#setPartialRange(IDocument, int, int, String,
555 public void setPartialRange(IDocument document, int offset, int length, String contentType, int partitionOffset) {
556 fScanner.setRange(document, offset, length);
557 setRange(document, offset, length);
558 fTokenOffset = partitionOffset;
560 fPrefixLength = offset - partitionOffset;
563 if (offset == partitionOffset) {
564 // restart at beginning of partition
567 fState = getState(contentType);
570 // emulate JavaPartitionScanner
578 * @see ITokenScanner#setRange(IDocument, int, int)
580 public void setRange(IDocument document, int offset, int length) {
581 fScanner.setRange(document, offset, length);
582 fTokenOffset = offset;
588 // emulate JavaPartitionScanner
596 * @see ITokenScanner#getTokenLength()
598 public int getTokenLength() {
603 * @see ITokenScanner#getTokenOffset()
605 public int getTokenOffset() {
606 if (AbstractPartitioner.DEBUG) {
607 Assert.isTrue(fTokenOffset >= 0, Integer.toString(fTokenOffset));