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.10 $
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 final int HTML = 0;
24 // private final int PHP = 1;
25 // private final int JS = 2;
26 // private final int CSS = 4;
27 // private final int COMMENT = 5;
28 // private final int HTMLCOMMENT = 6;
30 private IDocument fDocument = null;
31 private int fOffset = -1;
32 // private int fLastOffset = -1;
33 private String fContentType = IPHPPartitionScannerConstants.HTML;
35 private boolean partitionBorder = false;
36 private int fTokenOffset;
37 private int fEnd = -1;
39 //private int fState = HTML;
40 private Map tokens = new HashMap();
42 public PHPPartitionScanner()
45 IPHPPartitionScannerConstants.PHP,
46 new Token(IPHPPartitionScannerConstants.PHP));
48 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
49 new Token(IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT));
51 IPHPPartitionScannerConstants.HTML,
52 new Token(IPHPPartitionScannerConstants.HTML));
54 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
55 new Token(IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT));
58 private IToken getToken(String type)
60 fLength = fOffset-fTokenOffset;
61 IToken token = (IToken)this.tokens.get(type);
62 Assert.isNotNull(token, "Token for type \"" + type + "\" not found!");
67 * @see org.eclipse.jface.text.rules.IPartitionTokenScanner#setPartialRange(org.eclipse.jface.text.IDocument, int, int, java.lang.String, int)
69 public void setPartialRange(
76 this.setRange(document, offset, length);
77 if(this.tokens.containsKey(contentType))
78 fContentType = contentType;
79 if (partitionOffset > -1)
81 partitionBorder = false;
82 fTokenOffset = partitionOffset;
87 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()
89 public int getTokenLength()
95 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()
97 public int getTokenOffset()
103 * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
105 public IToken nextToken()
109 // check if we are not allready at the end of the
111 if ((c = read()) == ICharacterScanner.EOF)
113 partitionBorder = false;
121 fTokenOffset = fOffset;
122 partitionBorder = false;
125 while ((c = read()) != ICharacterScanner.EOF)
130 if (fContentType != IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT && checkPattern(new char[] { '?', 'p', 'h', 'p' }, true))
133 if (fContentType != IPHPPartitionScannerConstants.PHP
137 IToken token = getToken(fContentType);
138 fContentType = IPHPPartitionScannerConstants.PHP;
143 fContentType = IPHPPartitionScannerConstants.PHP;
145 // remember offset of this partition
146 fTokenOffset = fOffset - 5;
148 else if (checkPattern(new char[] { '!', '-', '-' }))
150 // return previouse partition
152 != IPHPPartitionScannerConstants
153 .HTML_MULTILINE_COMMENT
157 IToken token = getToken(fContentType);
159 IPHPPartitionScannerConstants
160 .HTML_MULTILINE_COMMENT;
165 IPHPPartitionScannerConstants
166 .HTML_MULTILINE_COMMENT;
168 fTokenOffset = fOffset - 4;
172 if (fContentType == IPHPPartitionScannerConstants.PHP)
174 if ((c = read()) == '>')
176 fContentType = IPHPPartitionScannerConstants.HTML;
177 partitionBorder = true;
178 return getToken(IPHPPartitionScannerConstants.PHP);
180 else if(c != ICharacterScanner.EOF)
186 == IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT
187 && checkPattern(new char[] { '-', '>' }))
189 fContentType = IPHPPartitionScannerConstants.HTML;
190 partitionBorder = true;
192 IPHPPartitionScannerConstants
193 .HTML_MULTILINE_COMMENT);
197 if ((c=read()) == '*')
198 { // MULTINE COMMENT JAVASCRIPT, CSS, PHP
199 if (fContentType == IPHPPartitionScannerConstants.PHP
203 IToken token = getToken(fContentType);
205 IPHPPartitionScannerConstants
206 .PHP_MULTILINE_COMMENT;
212 == IPHPPartitionScannerConstants
213 .PHP_MULTILINE_COMMENT)
216 fTokenOffset = fOffset - 2;
220 else if(c != ICharacterScanner.EOF)
224 if ((c = read()) == '/')
227 == IPHPPartitionScannerConstants
228 .PHP_MULTILINE_COMMENT)
230 fContentType = IPHPPartitionScannerConstants.PHP;
231 partitionBorder = true;
233 IPHPPartitionScannerConstants
234 .PHP_MULTILINE_COMMENT);
238 == IPHPPartitionScannerConstants
239 .CSS_MULTILINE_COMMENT)
244 == IPHPPartitionScannerConstants
245 .JS_MULTILINE_COMMENT)
249 else if(c != ICharacterScanner.EOF)
255 // end of file reached but we have to return the
257 return getToken(fContentType);
260 * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(org.eclipse.jface.text.IDocument, int, int)
262 public void setRange(IDocument document, int offset, int length)
264 fDocument = document;
266 fTokenOffset = offset;
268 fEnd = fOffset + length;
269 //partitionBorder = false;
278 return fDocument.getChar(fOffset++);
280 return ICharacterScanner.EOF;
282 catch (BadLocationException e)
284 // should never happen
285 // TODO write stacktrace to log
287 return ICharacterScanner.EOF;
291 private void unread()
296 private boolean checkPattern(char[] pattern)
298 return checkPattern(pattern, false);
302 * Check if next character sequence read from document is equals to
303 * the provided pattern. Pattern is read from left to right until the
304 * first character read doesn't match. If this happens all read characters are
306 * @param pattern The pattern to check.
307 * @return <code>true</code> if pattern is equals else returns <code>false</code>.
309 private boolean checkPattern(char[] pattern, boolean ignoreCase)
311 int prevOffset = fOffset;
312 for (int i = 0; i < pattern.length; i++)
316 if(c == ICharacterScanner.EOF || !letterEquals(c, pattern[i], ignoreCase))
318 fOffset = prevOffset;
326 private boolean letterEquals(int test, char letter, boolean ignoreCase)
330 else if(ignoreCase && Character.isLowerCase(letter) && test == Character.toUpperCase(letter))
332 else if(ignoreCase && Character.isUpperCase(letter) && test == Character.toLowerCase(letter))