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.22 $
11 package net.sourceforge.phpeclipse.phpeditor.php;
13 import java.util.ArrayList;
14 import java.util.HashMap;
17 import org.eclipse.jface.text.Assert;
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.ITypedRegion;
21 import org.eclipse.jface.text.rules.ICharacterScanner;
22 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
23 import org.eclipse.jface.text.rules.IToken;
24 import org.eclipse.jface.text.rules.Token;
29 public class PHPPartitionScanner implements IPartitionTokenScanner {
30 private static final boolean DEBUG = false;
32 private boolean fInString = false;
33 private boolean fInDoubString = false;
34 private IDocument fDocument = null;
35 private int fOffset = -1;
36 private String fContentType = IPHPPartitionScannerConstants.HTML;
37 private String fPrevContentType = IPHPPartitionScannerConstants.HTML;
38 private boolean partitionBorder = false;
39 private int fTokenOffset;
40 private int fEnd = -1;
42 private int fCurrentLength;
43 private int fFileType;
44 private Map tokens = new HashMap();
46 public PHPPartitionScanner() {
47 this(IPHPPartitionScannerConstants.PHP_FILE);
50 public PHPPartitionScanner(int fileType) {
51 this.tokens.put(IPHPPartitionScannerConstants.PHP, new Token(IPHPPartitionScannerConstants.PHP));
53 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
54 new Token(IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT));
55 this.tokens.put(IPHPPartitionScannerConstants.HTML, new Token(IPHPPartitionScannerConstants.HTML));
57 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
58 new Token(IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT));
60 this.tokens.put(IPHPPartitionScannerConstants.SMARTY, new Token(IPHPPartitionScannerConstants.SMARTY));
62 IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT,
63 new Token(IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT));
65 this.tokens.put(IDocument.DEFAULT_CONTENT_TYPE, new Token(IDocument.DEFAULT_CONTENT_TYPE));
69 private IToken getToken(String type) {
70 fLength = fCurrentLength;
75 int line = fDocument.getLineOfOffset(fOffset);
76 System.err.println("Error at " + line + " offset:" + String.valueOf(fOffset - fDocument.getLineOffset(line)));
78 } catch (BadLocationException e) { // should never happen
79 // TODO Write stacktrace to log
83 Assert.isTrue(fLength > 0, "Partition length <= 0!");
85 // String can never cross partition borders so reset string detection
87 fInDoubString = false;
88 IToken token = (IToken) this.tokens.get(type);
89 Assert.isNotNull(token, "Token for type \"" + type + "\" not found!");
91 System.out.println("Partition: fTokenOffset=" + fTokenOffset + " fContentType=" + type + " fLength=" + fLength);
97 * @see org.eclipse.jface.text.rules.IPartitionTokenScanner#setPartialRange(org.eclipse.jface.text.IDocument, int, int, java.lang.String, int)
99 public void setPartialRange(IDocument document, int offset, int length, String contentType, int partitionOffset) {
101 System.out.println("*****");
102 System.out.println("PartialRange: contentType=" + contentType + " partitionOffset=" + partitionOffset);
106 if (partitionOffset > -1) {
107 partitionBorder = false;
108 // because of strings we have to parse the whole partition
109 this.setRange(document, partitionOffset, offset - partitionOffset + length);
110 // sometimes we get a wrong partition so we retrieve the partition
111 // directly from the document
112 fContentType = fDocument.getContentType(partitionOffset);
114 this.setRange(document, offset, length);
116 } catch (BadLocationException e) {
117 // should never happen
118 // TODO print stack trace to log
119 // fall back just scan the whole document again
120 this.setRange(document, 0, fDocument.getLength());
126 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()
128 public int getTokenLength() {
133 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()
135 public int getTokenOffset() {
140 * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
142 public IToken nextToken() {
145 // check if we are not allready at the end of the
147 if ((c = read()) == ICharacterScanner.EOF) {
148 partitionBorder = false;
153 if (partitionBorder) {
154 fTokenOffset = fOffset;
155 partitionBorder = false;
158 while ((c = read()) != ICharacterScanner.EOF) {
161 if (!isInString(IPHPPartitionScannerConstants.PHP)
162 && fContentType != IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT
163 && checkPattern(new char[] { '?', 'p', 'h', 'p' }, true)) {
164 if (fContentType != IPHPPartitionScannerConstants.PHP && fCurrentLength > 5) {
166 IToken token = getToken(fContentType);
167 // save previouse contenttype
168 //TODO build stack for previouse contenttype
169 fPrevContentType = fContentType;
171 fContentType = IPHPPartitionScannerConstants.PHP;
175 fContentType = IPHPPartitionScannerConstants.PHP;
177 // remember offset of this partition
178 fTokenOffset = fOffset - 5;
181 !isInString(IPHPPartitionScannerConstants.PHP)
182 && fContentType != IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT
183 && checkPattern(new char[] { '?' }, false)) {
184 if (fContentType != IPHPPartitionScannerConstants.PHP && fCurrentLength > 2) {
186 IToken token = getToken(fContentType);
187 // save previouse contenttype
188 fPrevContentType = fContentType;
189 fContentType = IPHPPartitionScannerConstants.PHP;
192 fContentType = IPHPPartitionScannerConstants.PHP;
193 // remember offset of this partition
194 fTokenOffset = fOffset - 2;
197 !isInString(IPHPPartitionScannerConstants.PHP)
198 && (fContentType != IPHPPartitionScannerConstants.PHP) // BUG #769044
199 && (fContentType != IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT) // BUG #769044
200 && checkPattern(new char[] { '!', '-', '-' })) { // return previouse partition
201 if (fContentType != IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT && fCurrentLength > 4) {
203 IToken token = getToken(fContentType);
204 fContentType = IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT;
207 fContentType = IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT;
209 fTokenOffset = fOffset - 4;
214 if (!isInString(IPHPPartitionScannerConstants.PHP) && fContentType == IPHPPartitionScannerConstants.PHP) {
215 if ((c = read()) == '>') {
216 if (fPrevContentType != null)
217 fContentType = fPrevContentType;
219 fContentType = IPHPPartitionScannerConstants.HTML;
220 partitionBorder = true;
221 return getToken(IPHPPartitionScannerConstants.PHP);
222 } else if (c != ICharacterScanner.EOF)
227 if (!isInString(IPHPPartitionScannerConstants.PHP)
228 && fContentType == IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT
229 && checkPattern(new char[] { '-', '>' })) {
230 fContentType = IPHPPartitionScannerConstants.HTML;
231 partitionBorder = true;
232 return getToken(IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
235 case '{' : // SMARTY code starts here ?
236 if (fFileType == IPHPPartitionScannerConstants.SMARTY_FILE) {
237 if ((c = read()) == '*') {
240 "SMARTYDOC_TOKEN start "
251 if (fContentType != IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT && fCurrentLength > 2) {
252 // SMARTY doc code starts here
254 IToken token = getToken(fContentType);
255 fContentType = IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT;
257 // } else if (fContentType == IPHPPartitionScannerConstants.HTML && fOffset == 2) {
258 // fContentType = IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT;
259 } else { // if (fContentType == IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT) {
260 fContentType = IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT;
261 fTokenOffset = fOffset - 2;
268 "SMARTY_TOKEN start "
277 if (c != ICharacterScanner.EOF) {
280 if (fContentType != IPHPPartitionScannerConstants.SMARTY && fCurrentLength > 1) {
282 IToken token = getToken(fContentType);
283 fContentType = IPHPPartitionScannerConstants.SMARTY;
285 // } else if (fContentType == IPHPPartitionScannerConstants.HTML && fOffset==1) {
286 // fContentType = IPHPPartitionScannerConstants.SMARTY;
288 fContentType = IPHPPartitionScannerConstants.SMARTY;
289 fTokenOffset = fOffset - 1;
294 case '}' : // SMARTY code ends here ?
295 if (fFileType == IPHPPartitionScannerConstants.SMARTY_FILE && fContentType == IPHPPartitionScannerConstants.SMARTY) {
307 fContentType = IPHPPartitionScannerConstants.HTML;
308 partitionBorder = true;
309 return getToken(IPHPPartitionScannerConstants.SMARTY);
313 if (!isInString(IPHPPartitionScannerConstants.PHP) && (c = read()) == '*') { // MULTINE COMMENT JAVASCRIPT, CSS, PHP
314 if (fContentType == IPHPPartitionScannerConstants.PHP && fCurrentLength > 2) {
316 IToken token = getToken(fContentType);
317 fContentType = IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT;
319 } else if (fContentType == IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT) {
320 fTokenOffset = fOffset - 2;
324 } else if (!isInString(IPHPPartitionScannerConstants.PHP) && c != ICharacterScanner.EOF)
328 if (!isInString(IPHPPartitionScannerConstants.PHP) && (c = read()) == '/') {
329 if (fContentType == IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT) {
330 fContentType = IPHPPartitionScannerConstants.PHP;
331 partitionBorder = true;
332 return getToken(IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
333 } else if (fContentType == IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT) {
334 } else if (fContentType == IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT) {
336 } else if (fFileType == IPHPPartitionScannerConstants.SMARTY_FILE && (c = read()) == '}') {
339 "SMARTYDOC_TOKEN end "
348 if (fContentType == IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT) {
349 fContentType = IPHPPartitionScannerConstants.HTML;
350 partitionBorder = true;
351 return getToken(IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT);
353 } else if (!isInString(IPHPPartitionScannerConstants.PHP) && c != ICharacterScanner.EOF) {
359 fInString = !fInString;
362 // toggle String mode
364 fInDoubString = !fInDoubString;
367 } // end of file reached but we have to return the
369 return getToken(fContentType);
372 * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(org.eclipse.jface.text.IDocument, int, int)
374 public void setRange(IDocument document, int offset, int length) {
376 System.out.println("SET RANGE: offset=" + offset + " length=" + length);
379 fDocument = document;
381 fTokenOffset = offset;
384 fEnd = fOffset + length;
386 fInDoubString = false;
387 fContentType = IPHPPartitionScannerConstants.HTML;
388 // String[] prev = getPartitionStack(offset);
393 if (fOffset < fEnd) {
395 return fDocument.getChar(fOffset++);
397 return ICharacterScanner.EOF;
398 } catch (BadLocationException e) {
399 // should never happen
400 // TODO write stacktrace to log
402 return ICharacterScanner.EOF;
406 private void unread() {
411 private void unread(int num) {
413 fCurrentLength -= num;
416 private boolean checkPattern(char[] pattern) {
417 return checkPattern(pattern, false);
421 * Check if next character sequence read from document is equals to
422 * the provided pattern. Pattern is read from left to right until the
423 * first character read doesn't match. If this happens all read characters are
425 * @param pattern The pattern to check.
426 * @return <code>true</code> if pattern is equals else returns <code>false</code>.
428 private boolean checkPattern(char[] pattern, boolean ignoreCase) {
429 int prevOffset = fOffset;
430 int prevLength = fCurrentLength;
431 for (int i = 0; i < pattern.length; i++) {
434 if (c == ICharacterScanner.EOF || !letterEquals(c, pattern[i], ignoreCase)) {
435 fOffset = prevOffset;
436 fCurrentLength = prevLength;
444 private boolean letterEquals(int test, char letter, boolean ignoreCase) {
447 else if (ignoreCase && Character.isLowerCase(letter) && test == Character.toUpperCase(letter))
449 else if (ignoreCase && Character.isUpperCase(letter) && test == Character.toLowerCase(letter))
456 * Checks wether the offset is in a <code>String</code> and the specified
457 * contenttype is the current content type.
458 * Strings are delimited, mutual exclusive, by a " or by a '.
460 * @param contentType The contenttype to check.
461 * @return <code>true</code> if the current offset is in a string else
464 private boolean isInString(String contentType) {
465 if (fContentType == contentType)
466 return (fInString || fInDoubString);
472 * Returns the previouse partition stack for the given offset.
474 * @param offset The offset to return the previouse partitionstack for.
476 * @return The stack as a string array.
478 private String[] getPartitionStack(int offset) {
479 ArrayList types = new ArrayList();
482 ITypedRegion region = fDocument.getPartition(offset);
483 tmpOffset = region.getOffset();
484 while (tmpOffset - 1 > 0) {
485 region = fDocument.getPartition(tmpOffset - 1);
486 tmpOffset = region.getOffset();
487 types.add(0, region.getType());
489 } catch (BadLocationException e) {
495 String[] retVal = new String[types.size()];
497 retVal = (String[]) types.toArray(retVal);