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.17 $
11 package net.sourceforge.phpeclipse.phpeditor.php;
15 import org.eclipse.jface.text.*;
16 import org.eclipse.jface.text.rules.*;
21 public class PHPPartitionScanner implements IPartitionTokenScanner
23 private static final boolean DEBUG = true;
24 private boolean fInString = false;
25 private boolean fInDoubString = false;
26 private IDocument fDocument = null;
27 private int fOffset = -1;
28 private String fContentType = IPHPPartitionScannerConstants.HTML;
29 private String fPrevContentType = IPHPPartitionScannerConstants.HTML;
30 private boolean partitionBorder = false;
31 private int fTokenOffset;
32 private int fEnd = -1;
34 private int fCurrentLength;
35 private Map tokens = new HashMap();
37 public PHPPartitionScanner()
40 IPHPPartitionScannerConstants.PHP,
41 new Token(IPHPPartitionScannerConstants.PHP));
43 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
44 new Token(IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT));
46 IPHPPartitionScannerConstants.HTML,
47 new Token(IPHPPartitionScannerConstants.HTML));
49 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
50 new Token(IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT));
52 IDocument.DEFAULT_CONTENT_TYPE,
53 new Token(IDocument.DEFAULT_CONTENT_TYPE));
56 private IToken getToken(String type)
58 fLength = fCurrentLength;
66 int line = fDocument.getLineOfOffset(fOffset);
72 fOffset - fDocument.getLineOffset(line)));
75 catch (BadLocationException e)
76 { // should never happen
77 // TODO Write stacktrace to log
81 Assert.isTrue(fLength > 0, "Partition length <= 0!");
83 // String can never cross partition borders so reset string detection
85 fInDoubString = false;
86 IToken token = (IToken) this.tokens.get(type);
87 Assert.isNotNull(token, "Token for type \"" + type + "\" not found!");
91 "Partition: fTokenOffset="
102 * @see org.eclipse.jface.text.rules.IPartitionTokenScanner#setPartialRange(org.eclipse.jface.text.IDocument, int, int, java.lang.String, int)
104 public void setPartialRange(
114 "PartialRange: contentType="
116 + " partitionOffset="
122 if (partitionOffset > -1)
124 partitionBorder = false;
125 // because of strings we have to parse the whole partition
129 offset - partitionOffset + length);
130 // sometimes we get a wrong partition so we retrieve the partition
131 // directly from the document
132 fContentType = fDocument.getContentType(partitionOffset);
135 this.setRange(document, offset, length);
138 catch (BadLocationException e)
140 // should never happen
141 // TODO print stack trace to log
142 // fall back just scan the whole document again
143 this.setRange(document, 0, fDocument.getLength());
149 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()
151 public int getTokenLength()
157 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()
159 public int getTokenOffset()
165 * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
167 public IToken nextToken()
171 // check if we are not allready at the end of the
173 if ((c = read()) == ICharacterScanner.EOF)
175 partitionBorder = false;
183 fTokenOffset = fOffset;
184 partitionBorder = false;
187 while ((c = read()) != ICharacterScanner.EOF)
192 if (!isInString(IPHPPartitionScannerConstants.PHP)
194 != IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT
195 && checkPattern(new char[] { '?', 'p', 'h', 'p' }, true))
197 if (fContentType != IPHPPartitionScannerConstants.PHP
198 && fCurrentLength > 5)
201 IToken token = getToken(fContentType);
202 // save previouse contenttype
203 //TODO build stack for previouse contenttype
204 fPrevContentType = fContentType;
206 fContentType = IPHPPartitionScannerConstants.PHP;
211 fContentType = IPHPPartitionScannerConstants.PHP;
213 // remember offset of this partition
214 fTokenOffset = fOffset - 5;
218 !isInString(IPHPPartitionScannerConstants.PHP)
220 != IPHPPartitionScannerConstants
221 .PHP_MULTILINE_COMMENT
222 && checkPattern(new char[] { '?' }, false))
224 if (fContentType != IPHPPartitionScannerConstants.PHP
225 && fCurrentLength > 2)
228 IToken token = getToken(fContentType);
229 // save previouse contenttype
230 fPrevContentType = fContentType;
231 fContentType = IPHPPartitionScannerConstants.PHP;
235 fContentType = IPHPPartitionScannerConstants.PHP;
236 // remember offset of this partition
237 fTokenOffset = fOffset - 2;
241 !isInString(IPHPPartitionScannerConstants.PHP)
242 && checkPattern(new char[] { '!', '-', '-' }))
243 { // return previouse partition
245 != IPHPPartitionScannerConstants
246 .HTML_MULTILINE_COMMENT
247 && fCurrentLength > 4)
250 IToken token = getToken(fContentType);
252 IPHPPartitionScannerConstants
253 .HTML_MULTILINE_COMMENT;
258 IPHPPartitionScannerConstants
259 .HTML_MULTILINE_COMMENT;
261 fTokenOffset = fOffset - 4;
266 if (!isInString(IPHPPartitionScannerConstants.PHP)
267 && fContentType == IPHPPartitionScannerConstants.PHP)
269 if ((c = read()) == '>')
271 if (fPrevContentType != null)
272 fContentType = fPrevContentType;
275 IPHPPartitionScannerConstants.HTML;
276 partitionBorder = true;
277 return getToken(IPHPPartitionScannerConstants.PHP);
279 else if (c != ICharacterScanner.EOF)
284 if (!isInString(IPHPPartitionScannerConstants.PHP)
286 == IPHPPartitionScannerConstants
287 .HTML_MULTILINE_COMMENT
288 && checkPattern(new char[] { '-', '>' }))
290 fContentType = IPHPPartitionScannerConstants.HTML;
291 partitionBorder = true;
293 IPHPPartitionScannerConstants
294 .HTML_MULTILINE_COMMENT);
298 if (!isInString(IPHPPartitionScannerConstants.PHP) && (c = read()) == '*')
299 { // MULTINE COMMENT JAVASCRIPT, CSS, PHP
300 if (fContentType == IPHPPartitionScannerConstants.PHP
301 && fCurrentLength > 2)
304 IToken token = getToken(fContentType);
306 IPHPPartitionScannerConstants
307 .PHP_MULTILINE_COMMENT;
312 == IPHPPartitionScannerConstants
313 .PHP_MULTILINE_COMMENT)
316 fTokenOffset = fOffset - 2;
321 else if (!isInString(IPHPPartitionScannerConstants.PHP) && c != ICharacterScanner.EOF)
325 if (!isInString(IPHPPartitionScannerConstants.PHP) && (c = read()) == '/')
328 == IPHPPartitionScannerConstants
329 .PHP_MULTILINE_COMMENT)
331 fContentType = IPHPPartitionScannerConstants.PHP;
332 partitionBorder = true;
334 IPHPPartitionScannerConstants
335 .PHP_MULTILINE_COMMENT);
339 == IPHPPartitionScannerConstants
340 .CSS_MULTILINE_COMMENT)
345 == IPHPPartitionScannerConstants
346 .JS_MULTILINE_COMMENT)
350 else if (!isInString(IPHPPartitionScannerConstants.PHP) && c != ICharacterScanner.EOF)
355 fInString = !fInString;
358 // toggle String mode
360 fInDoubString = !fInDoubString;
363 } // end of file reached but we have to return the
365 return getToken(fContentType);
368 * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(org.eclipse.jface.text.IDocument, int, int)
370 public void setRange(IDocument document, int offset, int length)
375 "SET RANGE: offset=" + offset + " length=" + length);
378 fDocument = document;
380 fTokenOffset = offset;
383 fEnd = fOffset + length;
385 fInDoubString = false;
386 fContentType = IPHPPartitionScannerConstants.HTML;
387 // String[] prev = getPartitionStack(offset);
397 return fDocument.getChar(fOffset++);
399 return ICharacterScanner.EOF;
401 catch (BadLocationException e)
403 // should never happen
404 // TODO write stacktrace to log
406 return ICharacterScanner.EOF;
410 private void unread()
416 private void unread(int num)
419 fCurrentLength -= num;
422 private boolean checkPattern(char[] pattern)
424 return checkPattern(pattern, false);
428 * Check if next character sequence read from document is equals to
429 * the provided pattern. Pattern is read from left to right until the
430 * first character read doesn't match. If this happens all read characters are
432 * @param pattern The pattern to check.
433 * @return <code>true</code> if pattern is equals else returns <code>false</code>.
435 private boolean checkPattern(char[] pattern, boolean ignoreCase)
437 int prevOffset = fOffset;
438 int prevLength = fCurrentLength;
439 for (int i = 0; i < pattern.length; i++)
443 if (c == ICharacterScanner.EOF
444 || !letterEquals(c, pattern[i], ignoreCase))
446 fOffset = prevOffset;
447 fCurrentLength = prevLength;
455 private boolean letterEquals(int test, char letter, boolean ignoreCase)
461 && Character.isLowerCase(letter)
462 && test == Character.toUpperCase(letter))
466 && Character.isUpperCase(letter)
467 && test == Character.toLowerCase(letter))
474 * Checks wether the offset is in a <code>String</code> and the specified
475 * contenttype is the current content type.
476 * Strings are delimited, mutual exclusive, by a " or by a '.
478 * @param contentType The contenttype to check.
479 * @return <code>true</code> if the current offset is in a string else
482 private boolean isInString(String contentType)
484 if(fContentType == contentType)
485 return (fInString || fInDoubString);
491 * Returns the previouse partition stack for the given offset.
493 * @param offset The offset to return the previouse partitionstack for.
495 * @return The stack as a string array.
497 private String[] getPartitionStack(int offset)
499 ArrayList types = new ArrayList();
503 ITypedRegion region = fDocument.getPartition(offset);
504 tmpOffset = region.getOffset();
505 while(tmpOffset-1 > 0)
507 region = fDocument.getPartition(tmpOffset-1);
508 tmpOffset = region.getOffset();
509 types.add(0, region.getType());
512 catch (BadLocationException e)
520 String[] retVal = new String[types.size()];
522 retVal = (String[])types.toArray(retVal);