2 * This program and the accompanying materials
3 * are made available under the terms of the Common Public License v1.0
4 * which accompanies this distribution, and is available at
5 * http://www.eclipse.org/legal/cpl-v10.html
6 * Created on 05.03.2003
8 * @author Stefan Langer (musk)
9 * @version $Revision: 1.24 $
11 package net.sourceforge.phpeclipse.phpeditor.php;
13 import java.util.ArrayList;
14 import java.util.HashMap;
17 import net.sourceforge.phpdt.internal.ui.text.*;
19 import org.eclipse.jface.text.Assert;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.ITypedRegion;
23 import org.eclipse.jface.text.rules.ICharacterScanner;
24 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
25 import org.eclipse.jface.text.rules.IToken;
26 import org.eclipse.jface.text.rules.Token;
31 public class PHPPartitionScanner implements IPartitionTokenScanner {
32 private static final boolean DEBUG = false;
34 private boolean fInString = false;
35 private boolean fInDoubString = false;
36 private IDocument fDocument = null;
37 private int fOffset = -1;
38 private String fContentType = IPHPPartitions.HTML;
39 private String fPrevContentType = IPHPPartitions.HTML;
40 private boolean partitionBorder = false;
41 private int fTokenOffset;
42 private int fEnd = -1;
44 private int fCurrentLength;
45 private int fFileType;
46 private Map tokens = new HashMap();
48 public PHPPartitionScanner() {
49 this(IPHPPartitions.PHP_FILE);
52 public PHPPartitionScanner(int fileType) {
53 this.tokens.put(IPHPPartitions.PHP_PARTITIONING, new Token(IPHPPartitions.PHP_PARTITIONING));
54 this.tokens.put(IPHPPartitions.PHP_STRING_DQ, new Token(IPHPPartitions.PHP_STRING_DQ));
56 IPHPPartitions.PHP_PHPDOC_COMMENT,
57 new Token(IPHPPartitions.PHP_PHPDOC_COMMENT));
58 this.tokens.put(IPHPPartitions.HTML, new Token(IPHPPartitions.HTML));
60 IPHPPartitions.HTML_MULTILINE_COMMENT,
61 new Token(IPHPPartitions.HTML_MULTILINE_COMMENT));
63 this.tokens.put(IPHPPartitions.SMARTY, new Token(IPHPPartitions.SMARTY));
65 IPHPPartitions.SMARTY_MULTILINE_COMMENT,
66 new Token(IPHPPartitions.SMARTY_MULTILINE_COMMENT));
68 this.tokens.put(IDocument.DEFAULT_CONTENT_TYPE, new Token(IDocument.DEFAULT_CONTENT_TYPE));
72 private IToken getToken(String type) {
73 fLength = fCurrentLength;
78 int line = fDocument.getLineOfOffset(fOffset);
79 System.err.println("Error at " + line + " offset:" + String.valueOf(fOffset - fDocument.getLineOffset(line)));
81 } catch (BadLocationException e) { // should never happen
82 // TODO Write stacktrace to log
86 Assert.isTrue(fLength > 0, "Partition length <= 0!");
88 // String can never cross partition borders so reset string detection
90 fInDoubString = false;
91 IToken token = (IToken) this.tokens.get(type);
92 Assert.isNotNull(token, "Token for type \"" + type + "\" not found!");
94 System.out.println("Partition: fTokenOffset=" + fTokenOffset + " fContentType=" + type + " fLength=" + fLength);
100 * @see org.eclipse.jface.text.rules.IPartitionTokenScanner#setPartialRange(org.eclipse.jface.text.IDocument, int, int, java.lang.String, int)
102 public void setPartialRange(IDocument document, int offset, int length, String contentType, int partitionOffset) {
104 System.out.println("*****");
105 System.out.println("PartialRange: contentType=" + contentType + " partitionOffset=" + partitionOffset);
109 if (partitionOffset > -1) {
110 partitionBorder = false;
111 // because of strings we have to parse the whole partition
112 this.setRange(document, partitionOffset, offset - partitionOffset + length);
113 // sometimes we get a wrong partition so we retrieve the partition
114 // directly from the document
115 fContentType = fDocument.getContentType(partitionOffset);
117 this.setRange(document, offset, length);
119 } catch (BadLocationException e) {
120 // should never happen
121 // TODO print stack trace to log
122 // fall back just scan the whole document again
123 this.setRange(document, 0, fDocument.getLength());
129 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()
131 public int getTokenLength() {
136 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()
138 public int getTokenOffset() {
143 * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
145 public IToken nextToken() {
148 // check if we are not allready at the end of the
150 if ((c = read()) == ICharacterScanner.EOF) {
151 partitionBorder = false;
156 if (partitionBorder) {
157 fTokenOffset = fOffset;
158 partitionBorder = false;
161 while ((c = read()) != ICharacterScanner.EOF) {
164 if (!isInString(IPHPPartitions.PHP_PARTITIONING)
165 && fContentType != IPHPPartitions.PHP_PHPDOC_COMMENT
166 && checkPattern(new char[] { '?', 'p', 'h', 'p' }, true)) {
167 if (fContentType != IPHPPartitions.PHP_PARTITIONING && fCurrentLength > 5) {
169 IToken token = getToken(fContentType);
170 // save previouse contenttype
171 //TODO build stack for previouse contenttype
172 fPrevContentType = fContentType;
174 fContentType = IPHPPartitions.PHP_PARTITIONING;
178 fContentType = IPHPPartitions.PHP_PARTITIONING;
180 // remember offset of this partition
181 fTokenOffset = fOffset - 5;
184 !isInString(IPHPPartitions.PHP_PARTITIONING)
185 && fContentType != IPHPPartitions.PHP_PHPDOC_COMMENT
186 && checkPattern(new char[] { '?' }, false)) {
187 if (fContentType != IPHPPartitions.PHP_PARTITIONING && fCurrentLength > 2) {
189 IToken token = getToken(fContentType);
190 // save previouse contenttype
191 fPrevContentType = fContentType;
192 fContentType = IPHPPartitions.PHP_PARTITIONING;
195 fContentType = IPHPPartitions.PHP_PARTITIONING;
196 // remember offset of this partition
197 fTokenOffset = fOffset - 2;
200 !isInString(IPHPPartitions.PHP_PARTITIONING)
201 && (fContentType != IPHPPartitions.PHP_PARTITIONING) // BUG #769044
202 && (fContentType != IPHPPartitions.PHP_PHPDOC_COMMENT) // BUG #769044
203 && checkPattern(new char[] { '!', '-', '-' })) { // return previouse partition
204 if (fContentType != IPHPPartitions.HTML_MULTILINE_COMMENT && fCurrentLength > 4) {
206 IToken token = getToken(fContentType);
207 fContentType = IPHPPartitions.HTML_MULTILINE_COMMENT;
210 fContentType = IPHPPartitions.HTML_MULTILINE_COMMENT;
212 fTokenOffset = fOffset - 4;
217 if (!isInString(IPHPPartitions.PHP_PARTITIONING) && fContentType == IPHPPartitions.PHP_PARTITIONING) {
218 if ((c = read()) == '>') {
219 if (fPrevContentType != null)
220 fContentType = fPrevContentType;
222 fContentType = IPHPPartitions.HTML;
223 partitionBorder = true;
224 return getToken(IPHPPartitions.PHP_PARTITIONING);
225 } else if (c != ICharacterScanner.EOF)
230 if (!isInString(IPHPPartitions.PHP_PARTITIONING)
231 && fContentType == IPHPPartitions.HTML_MULTILINE_COMMENT
232 && checkPattern(new char[] { '-', '>' })) {
233 fContentType = IPHPPartitions.HTML;
234 partitionBorder = true;
235 return getToken(IPHPPartitions.HTML_MULTILINE_COMMENT);
238 case '{' : // SMARTY code starts here ?
239 if (fFileType == IPHPPartitions.SMARTY_FILE) {
240 if ((c = read()) == '*') {
243 "SMARTYDOC_TOKEN start "
254 if (fContentType != IPHPPartitions.SMARTY_MULTILINE_COMMENT && fCurrentLength > 2) {
255 // SMARTY doc code starts here
257 IToken token = getToken(fContentType);
258 fContentType = IPHPPartitions.SMARTY_MULTILINE_COMMENT;
260 // } else if (fContentType == IPHPPartitionScannerConstants.HTML && fOffset == 2) {
261 // fContentType = IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT;
262 } else { // if (fContentType == IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT) {
263 fContentType = IPHPPartitions.SMARTY_MULTILINE_COMMENT;
264 fTokenOffset = fOffset - 2;
271 "SMARTY_TOKEN start "
280 if (c != ICharacterScanner.EOF) {
283 if (fContentType != IPHPPartitions.SMARTY && fCurrentLength > 1) {
285 IToken token = getToken(fContentType);
286 fContentType = IPHPPartitions.SMARTY;
288 // } else if (fContentType == IPHPPartitionScannerConstants.HTML && fOffset==1) {
289 // fContentType = IPHPPartitionScannerConstants.SMARTY;
291 fContentType = IPHPPartitions.SMARTY;
292 fTokenOffset = fOffset - 1;
297 case '}' : // SMARTY code ends here ?
298 if (fFileType == IPHPPartitions.SMARTY_FILE && fContentType == IPHPPartitions.SMARTY) {
310 fContentType = IPHPPartitions.HTML;
311 partitionBorder = true;
312 return getToken(IPHPPartitions.SMARTY);
316 if (!isInString(IPHPPartitions.PHP_PARTITIONING) && (c = read()) == '*') { // MULTINE COMMENT JAVASCRIPT, CSS, PHP
317 if (fContentType == IPHPPartitions.PHP_PARTITIONING && fCurrentLength > 2) {
319 IToken token = getToken(fContentType);
320 fContentType = IPHPPartitions.PHP_PHPDOC_COMMENT;
322 } else if (fContentType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
323 fTokenOffset = fOffset - 2;
327 } else if (!isInString(IPHPPartitions.PHP_PARTITIONING) && c != ICharacterScanner.EOF)
331 if (!isInString(IPHPPartitions.PHP_PARTITIONING) && (c = read()) == '/') {
332 if (fContentType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
333 fContentType = IPHPPartitions.PHP_PARTITIONING;
334 partitionBorder = true;
335 return getToken(IPHPPartitions.PHP_PHPDOC_COMMENT);
336 } else if (fContentType == IPHPPartitions.CSS_MULTILINE_COMMENT) {
337 } else if (fContentType == IPHPPartitions.JS_MULTILINE_COMMENT) {
339 } else if (fFileType == IPHPPartitions.SMARTY_FILE && (c = read()) == '}') {
342 "SMARTYDOC_TOKEN end "
351 if (fContentType == IPHPPartitions.SMARTY_MULTILINE_COMMENT) {
352 fContentType = IPHPPartitions.HTML;
353 partitionBorder = true;
354 return getToken(IPHPPartitions.SMARTY_MULTILINE_COMMENT);
356 } else if (!isInString(IPHPPartitions.PHP_PARTITIONING) && c != ICharacterScanner.EOF) {
362 fInString = !fInString;
365 // toggle String mode
367 fInDoubString = !fInDoubString;
370 } // end of file reached but we have to return the
372 return getToken(fContentType);
375 * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(org.eclipse.jface.text.IDocument, int, int)
377 public void setRange(IDocument document, int offset, int length) {
379 System.out.println("SET RANGE: offset=" + offset + " length=" + length);
382 fDocument = document;
384 fTokenOffset = offset;
387 fEnd = fOffset + length;
389 fInDoubString = false;
390 fContentType = IPHPPartitions.HTML;
391 // String[] prev = getPartitionStack(offset);
396 if (fOffset < fEnd) {
398 return fDocument.getChar(fOffset++);
400 return ICharacterScanner.EOF;
401 } catch (BadLocationException e) {
402 // should never happen
403 // TODO write stacktrace to log
405 return ICharacterScanner.EOF;
409 private void unread() {
414 private void unread(int num) {
416 fCurrentLength -= num;
419 private boolean checkPattern(char[] pattern) {
420 return checkPattern(pattern, false);
424 * Check if next character sequence read from document is equals to
425 * the provided pattern. Pattern is read from left to right until the
426 * first character read doesn't match. If this happens all read characters are
428 * @param pattern The pattern to check.
429 * @return <code>true</code> if pattern is equals else returns <code>false</code>.
431 private boolean checkPattern(char[] pattern, boolean ignoreCase) {
432 int prevOffset = fOffset;
433 int prevLength = fCurrentLength;
434 for (int i = 0; i < pattern.length; i++) {
437 if (c == ICharacterScanner.EOF || !letterEquals(c, pattern[i], ignoreCase)) {
438 fOffset = prevOffset;
439 fCurrentLength = prevLength;
447 private boolean letterEquals(int test, char letter, boolean ignoreCase) {
450 else if (ignoreCase && Character.isLowerCase(letter) && test == Character.toUpperCase(letter))
452 else if (ignoreCase && Character.isUpperCase(letter) && test == Character.toLowerCase(letter))
459 * Checks wether the offset is in a <code>String</code> and the specified
460 * contenttype is the current content type.
461 * Strings are delimited, mutual exclusive, by a " or by a '.
463 * @param contentType The contenttype to check.
464 * @return <code>true</code> if the current offset is in a string else
467 private boolean isInString(String contentType) {
468 if (fContentType == contentType)
469 return (fInString || fInDoubString);
475 * Returns the previouse partition stack for the given offset.
477 * @param offset The offset to return the previouse partitionstack for.
479 * @return The stack as a string array.
481 private String[] getPartitionStack(int offset) {
482 ArrayList types = new ArrayList();
485 ITypedRegion region = fDocument.getPartition(offset);
486 tmpOffset = region.getOffset();
487 while (tmpOffset - 1 > 0) {
488 region = fDocument.getPartition(tmpOffset - 1);
489 tmpOffset = region.getOffset();
490 types.add(0, region.getType());
492 } catch (BadLocationException e) {
498 String[] retVal = new String[types.size()];
500 retVal = (String[]) types.toArray(retVal);