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.23 $
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));
55 IPHPPartitions.PHP_MULTILINE_COMMENT,
56 new Token(IPHPPartitions.PHP_MULTILINE_COMMENT));
57 this.tokens.put(IPHPPartitions.HTML, new Token(IPHPPartitions.HTML));
59 IPHPPartitions.HTML_MULTILINE_COMMENT,
60 new Token(IPHPPartitions.HTML_MULTILINE_COMMENT));
62 this.tokens.put(IPHPPartitions.SMARTY, new Token(IPHPPartitions.SMARTY));
64 IPHPPartitions.SMARTY_MULTILINE_COMMENT,
65 new Token(IPHPPartitions.SMARTY_MULTILINE_COMMENT));
67 this.tokens.put(IDocument.DEFAULT_CONTENT_TYPE, new Token(IDocument.DEFAULT_CONTENT_TYPE));
71 private IToken getToken(String type) {
72 fLength = fCurrentLength;
77 int line = fDocument.getLineOfOffset(fOffset);
78 System.err.println("Error at " + line + " offset:" + String.valueOf(fOffset - fDocument.getLineOffset(line)));
80 } catch (BadLocationException e) { // should never happen
81 // TODO Write stacktrace to log
85 Assert.isTrue(fLength > 0, "Partition length <= 0!");
87 // String can never cross partition borders so reset string detection
89 fInDoubString = false;
90 IToken token = (IToken) this.tokens.get(type);
91 Assert.isNotNull(token, "Token for type \"" + type + "\" not found!");
93 System.out.println("Partition: fTokenOffset=" + fTokenOffset + " fContentType=" + type + " fLength=" + fLength);
99 * @see org.eclipse.jface.text.rules.IPartitionTokenScanner#setPartialRange(org.eclipse.jface.text.IDocument, int, int, java.lang.String, int)
101 public void setPartialRange(IDocument document, int offset, int length, String contentType, int partitionOffset) {
103 System.out.println("*****");
104 System.out.println("PartialRange: contentType=" + contentType + " partitionOffset=" + partitionOffset);
108 if (partitionOffset > -1) {
109 partitionBorder = false;
110 // because of strings we have to parse the whole partition
111 this.setRange(document, partitionOffset, offset - partitionOffset + length);
112 // sometimes we get a wrong partition so we retrieve the partition
113 // directly from the document
114 fContentType = fDocument.getContentType(partitionOffset);
116 this.setRange(document, offset, length);
118 } catch (BadLocationException e) {
119 // should never happen
120 // TODO print stack trace to log
121 // fall back just scan the whole document again
122 this.setRange(document, 0, fDocument.getLength());
128 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()
130 public int getTokenLength() {
135 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()
137 public int getTokenOffset() {
142 * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
144 public IToken nextToken() {
147 // check if we are not allready at the end of the
149 if ((c = read()) == ICharacterScanner.EOF) {
150 partitionBorder = false;
155 if (partitionBorder) {
156 fTokenOffset = fOffset;
157 partitionBorder = false;
160 while ((c = read()) != ICharacterScanner.EOF) {
163 if (!isInString(IPHPPartitions.PHP_PARTITIONING)
164 && fContentType != IPHPPartitions.PHP_MULTILINE_COMMENT
165 && checkPattern(new char[] { '?', 'p', 'h', 'p' }, true)) {
166 if (fContentType != IPHPPartitions.PHP_PARTITIONING && fCurrentLength > 5) {
168 IToken token = getToken(fContentType);
169 // save previouse contenttype
170 //TODO build stack for previouse contenttype
171 fPrevContentType = fContentType;
173 fContentType = IPHPPartitions.PHP_PARTITIONING;
177 fContentType = IPHPPartitions.PHP_PARTITIONING;
179 // remember offset of this partition
180 fTokenOffset = fOffset - 5;
183 !isInString(IPHPPartitions.PHP_PARTITIONING)
184 && fContentType != IPHPPartitions.PHP_MULTILINE_COMMENT
185 && checkPattern(new char[] { '?' }, false)) {
186 if (fContentType != IPHPPartitions.PHP_PARTITIONING && fCurrentLength > 2) {
188 IToken token = getToken(fContentType);
189 // save previouse contenttype
190 fPrevContentType = fContentType;
191 fContentType = IPHPPartitions.PHP_PARTITIONING;
194 fContentType = IPHPPartitions.PHP_PARTITIONING;
195 // remember offset of this partition
196 fTokenOffset = fOffset - 2;
199 !isInString(IPHPPartitions.PHP_PARTITIONING)
200 && (fContentType != IPHPPartitions.PHP_PARTITIONING) // BUG #769044
201 && (fContentType != IPHPPartitions.PHP_MULTILINE_COMMENT) // BUG #769044
202 && checkPattern(new char[] { '!', '-', '-' })) { // return previouse partition
203 if (fContentType != IPHPPartitions.HTML_MULTILINE_COMMENT && fCurrentLength > 4) {
205 IToken token = getToken(fContentType);
206 fContentType = IPHPPartitions.HTML_MULTILINE_COMMENT;
209 fContentType = IPHPPartitions.HTML_MULTILINE_COMMENT;
211 fTokenOffset = fOffset - 4;
216 if (!isInString(IPHPPartitions.PHP_PARTITIONING) && fContentType == IPHPPartitions.PHP_PARTITIONING) {
217 if ((c = read()) == '>') {
218 if (fPrevContentType != null)
219 fContentType = fPrevContentType;
221 fContentType = IPHPPartitions.HTML;
222 partitionBorder = true;
223 return getToken(IPHPPartitions.PHP_PARTITIONING);
224 } else if (c != ICharacterScanner.EOF)
229 if (!isInString(IPHPPartitions.PHP_PARTITIONING)
230 && fContentType == IPHPPartitions.HTML_MULTILINE_COMMENT
231 && checkPattern(new char[] { '-', '>' })) {
232 fContentType = IPHPPartitions.HTML;
233 partitionBorder = true;
234 return getToken(IPHPPartitions.HTML_MULTILINE_COMMENT);
237 case '{' : // SMARTY code starts here ?
238 if (fFileType == IPHPPartitions.SMARTY_FILE) {
239 if ((c = read()) == '*') {
242 "SMARTYDOC_TOKEN start "
253 if (fContentType != IPHPPartitions.SMARTY_MULTILINE_COMMENT && fCurrentLength > 2) {
254 // SMARTY doc code starts here
256 IToken token = getToken(fContentType);
257 fContentType = IPHPPartitions.SMARTY_MULTILINE_COMMENT;
259 // } else if (fContentType == IPHPPartitionScannerConstants.HTML && fOffset == 2) {
260 // fContentType = IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT;
261 } else { // if (fContentType == IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT) {
262 fContentType = IPHPPartitions.SMARTY_MULTILINE_COMMENT;
263 fTokenOffset = fOffset - 2;
270 "SMARTY_TOKEN start "
279 if (c != ICharacterScanner.EOF) {
282 if (fContentType != IPHPPartitions.SMARTY && fCurrentLength > 1) {
284 IToken token = getToken(fContentType);
285 fContentType = IPHPPartitions.SMARTY;
287 // } else if (fContentType == IPHPPartitionScannerConstants.HTML && fOffset==1) {
288 // fContentType = IPHPPartitionScannerConstants.SMARTY;
290 fContentType = IPHPPartitions.SMARTY;
291 fTokenOffset = fOffset - 1;
296 case '}' : // SMARTY code ends here ?
297 if (fFileType == IPHPPartitions.SMARTY_FILE && fContentType == IPHPPartitions.SMARTY) {
309 fContentType = IPHPPartitions.HTML;
310 partitionBorder = true;
311 return getToken(IPHPPartitions.SMARTY);
315 if (!isInString(IPHPPartitions.PHP_PARTITIONING) && (c = read()) == '*') { // MULTINE COMMENT JAVASCRIPT, CSS, PHP
316 if (fContentType == IPHPPartitions.PHP_PARTITIONING && fCurrentLength > 2) {
318 IToken token = getToken(fContentType);
319 fContentType = IPHPPartitions.PHP_MULTILINE_COMMENT;
321 } else if (fContentType == IPHPPartitions.PHP_MULTILINE_COMMENT) {
322 fTokenOffset = fOffset - 2;
326 } else if (!isInString(IPHPPartitions.PHP_PARTITIONING) && c != ICharacterScanner.EOF)
330 if (!isInString(IPHPPartitions.PHP_PARTITIONING) && (c = read()) == '/') {
331 if (fContentType == IPHPPartitions.PHP_MULTILINE_COMMENT) {
332 fContentType = IPHPPartitions.PHP_PARTITIONING;
333 partitionBorder = true;
334 return getToken(IPHPPartitions.PHP_MULTILINE_COMMENT);
335 } else if (fContentType == IPHPPartitions.CSS_MULTILINE_COMMENT) {
336 } else if (fContentType == IPHPPartitions.JS_MULTILINE_COMMENT) {
338 } else if (fFileType == IPHPPartitions.SMARTY_FILE && (c = read()) == '}') {
341 "SMARTYDOC_TOKEN end "
350 if (fContentType == IPHPPartitions.SMARTY_MULTILINE_COMMENT) {
351 fContentType = IPHPPartitions.HTML;
352 partitionBorder = true;
353 return getToken(IPHPPartitions.SMARTY_MULTILINE_COMMENT);
355 } else if (!isInString(IPHPPartitions.PHP_PARTITIONING) && c != ICharacterScanner.EOF) {
361 fInString = !fInString;
364 // toggle String mode
366 fInDoubString = !fInDoubString;
369 } // end of file reached but we have to return the
371 return getToken(fContentType);
374 * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(org.eclipse.jface.text.IDocument, int, int)
376 public void setRange(IDocument document, int offset, int length) {
378 System.out.println("SET RANGE: offset=" + offset + " length=" + length);
381 fDocument = document;
383 fTokenOffset = offset;
386 fEnd = fOffset + length;
388 fInDoubString = false;
389 fContentType = IPHPPartitions.HTML;
390 // String[] prev = getPartitionStack(offset);
395 if (fOffset < fEnd) {
397 return fDocument.getChar(fOffset++);
399 return ICharacterScanner.EOF;
400 } catch (BadLocationException e) {
401 // should never happen
402 // TODO write stacktrace to log
404 return ICharacterScanner.EOF;
408 private void unread() {
413 private void unread(int num) {
415 fCurrentLength -= num;
418 private boolean checkPattern(char[] pattern) {
419 return checkPattern(pattern, false);
423 * Check if next character sequence read from document is equals to
424 * the provided pattern. Pattern is read from left to right until the
425 * first character read doesn't match. If this happens all read characters are
427 * @param pattern The pattern to check.
428 * @return <code>true</code> if pattern is equals else returns <code>false</code>.
430 private boolean checkPattern(char[] pattern, boolean ignoreCase) {
431 int prevOffset = fOffset;
432 int prevLength = fCurrentLength;
433 for (int i = 0; i < pattern.length; i++) {
436 if (c == ICharacterScanner.EOF || !letterEquals(c, pattern[i], ignoreCase)) {
437 fOffset = prevOffset;
438 fCurrentLength = prevLength;
446 private boolean letterEquals(int test, char letter, boolean ignoreCase) {
449 else if (ignoreCase && Character.isLowerCase(letter) && test == Character.toUpperCase(letter))
451 else if (ignoreCase && Character.isUpperCase(letter) && test == Character.toLowerCase(letter))
458 * Checks wether the offset is in a <code>String</code> and the specified
459 * contenttype is the current content type.
460 * Strings are delimited, mutual exclusive, by a " or by a '.
462 * @param contentType The contenttype to check.
463 * @return <code>true</code> if the current offset is in a string else
466 private boolean isInString(String contentType) {
467 if (fContentType == contentType)
468 return (fInString || fInDoubString);
474 * Returns the previouse partition stack for the given offset.
476 * @param offset The offset to return the previouse partitionstack for.
478 * @return The stack as a string array.
480 private String[] getPartitionStack(int offset) {
481 ArrayList types = new ArrayList();
484 ITypedRegion region = fDocument.getPartition(offset);
485 tmpOffset = region.getOffset();
486 while (tmpOffset - 1 > 0) {
487 region = fDocument.getPartition(tmpOffset - 1);
488 tmpOffset = region.getOffset();
489 types.add(0, region.getType());
491 } catch (BadLocationException e) {
497 String[] retVal = new String[types.size()];
499 retVal = (String[]) types.toArray(retVal);