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);
204 // bug #1404228: Crash on <?php // comment ?>
205 // fScanner.unread();
209 if (!fEmulate && fLast == CARRIAGE_RETURN) {
211 case SINGLE_LINE_COMMENT:
230 newState = STRING_SQ;
235 newState = STRING_DQ;
239 last = CARRIAGE_RETURN;
254 fLast = NONE; // ignore fLast
255 return preFix(fState, newState, last, 1);
268 if (fTokenLength > 0) {
269 return preFix(PHP, SINGLE_LINE_COMMENT, NONE, 1);
271 preFix(PHP, SINGLE_LINE_COMMENT, NONE, 1);
272 fTokenOffset += fTokenLength;
273 fTokenLength = fPrefixLength;
277 if (fLast == SLASH) {
278 if (fTokenLength - getLastLength(fLast) > 0) {
279 return preFix(PHP, SINGLE_LINE_COMMENT, NONE, 2);
281 preFix(PHP, SINGLE_LINE_COMMENT, NONE, 2);
282 fTokenOffset += fTokenLength;
283 fTokenLength = fPrefixLength;
294 if (fLast == SLASH) {
295 if (fTokenLength - getLastLength(fLast) > 0)
296 return preFix(PHP, MULTI_LINE_COMMENT, SLASH_STAR, 2);
298 preFix(PHP, MULTI_LINE_COMMENT, SLASH_STAR, 2);
299 fTokenOffset += fTokenLength;
300 fTokenLength = fPrefixLength;
310 fLast = NONE; // ignore fLast
311 if (fTokenLength > 0)
312 return preFix(PHP, STRING_SQ, NONE, 1);
314 preFix(PHP, STRING_SQ, NONE, 1);
315 fTokenOffset += fTokenLength;
316 fTokenLength = fPrefixLength;
321 fLast = NONE; // ignore fLast
322 if (fTokenLength > 0)
323 return preFix(PHP, STRING_DQ, NONE, 1);
325 preFix(PHP, STRING_DQ, NONE, 1);
326 fTokenOffset += fTokenLength;
327 fTokenLength = fPrefixLength;
337 case SINGLE_LINE_COMMENT:
345 case SLASH_STAR_STAR:
346 return postFix(MULTI_LINE_COMMENT);
349 return postFix(PHPDOC);
368 case MULTI_LINE_COMMENT:
371 if (fLast == SLASH_STAR) {
372 fLast = SLASH_STAR_STAR;
383 return postFix(MULTI_LINE_COMMENT);
398 fLast = (fLast == BACKSLASH) ? NONE : BACKSLASH;
403 if (fLast != BACKSLASH) {
404 return postFix(STRING_DQ);
419 fLast = (fLast == BACKSLASH) ? NONE : BACKSLASH;
424 if (fLast != BACKSLASH) {
425 return postFix(STRING_SQ);
440 // fLast= (fLast == BACKSLASH) ? NONE : BACKSLASH;
445 // if (fLast != BACKSLASH) {
446 // return postFix(CHARACTER);
462 private static final int getLastLength(int last) {
470 case CARRIAGE_RETURN:
479 case SLASH_STAR_STAR:
484 private final void consume() {
489 private final IToken postFix(int state) {
494 return fTokens[state];
497 private final IToken preFix(int state, int newState, int last, int prefixLength) {
498 // emulate JavaPartitionScanner
499 if (fEmulate && state == PHP && (fTokenLength - getLastLength(fLast) > 0)) {
500 fTokenLength -= getLastLength(fLast);
501 fJavaOffset = fTokenOffset;
502 fJavaLength = fTokenLength;
505 fPrefixLength = prefixLength;
507 return fTokens[state];
510 fTokenLength -= getLastLength(fLast);
512 fPrefixLength = prefixLength;
513 IToken token = fTokens[state];
519 private static int getState(String contentType) {
521 if (contentType == null)
524 else if (contentType.equals(PHP_SINGLELINE_COMMENT))
525 return SINGLE_LINE_COMMENT;
527 else if (contentType.equals(PHP_MULTILINE_COMMENT))
528 return MULTI_LINE_COMMENT;
530 else if (contentType.equals(PHP_PHPDOC_COMMENT))
533 else if (contentType.equals(PHP_STRING_DQ))
536 else if (contentType.equals(PHP_STRING_SQ))
539 else if (contentType.equals(PHP_STRING_HEREDOC))
540 return STRING_HEREDOC;
542 // else if (contentType.equals(JAVA_CHARACTER))
550 * @see IPartitionTokenScanner#setPartialRange(IDocument, int, int, String,
553 public void setPartialRange(IDocument document, int offset, int length, String contentType, int partitionOffset) {
554 fScanner.setRange(document, offset, length);
555 setRange(document, offset, length);
556 fTokenOffset = partitionOffset;
558 fPrefixLength = offset - partitionOffset;
561 if (offset == partitionOffset) {
562 // restart at beginning of partition
565 fState = getState(contentType);
568 // emulate JavaPartitionScanner
576 * @see ITokenScanner#setRange(IDocument, int, int)
578 public void setRange(IDocument document, int offset, int length) {
579 fScanner.setRange(document, offset, length);
580 fTokenOffset = offset;
586 // emulate JavaPartitionScanner
594 * @see ITokenScanner#getTokenLength()
596 public int getTokenLength() {
601 * @see ITokenScanner#getTokenOffset()
603 public int getTokenOffset() {
604 if (AbstractPartitioner.DEBUG) {
605 Assert.isTrue(fTokenOffset >= 0, Integer.toString(fTokenOffset));