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.14 $
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 {
22 private static final boolean DEBUG = false;
23 private IDocument fDocument = null;
24 private int fOffset = -1;
25 private String fContentType = IPHPPartitionScannerConstants.HTML;
26 private String fPrevContentType;
28 private boolean partitionBorder = false;
29 private int fTokenOffset;
30 private int fEnd = -1;
32 private Map tokens = new HashMap();
34 public PHPPartitionScanner() {
36 IPHPPartitionScannerConstants.PHP,
37 new Token(IPHPPartitionScannerConstants.PHP));
39 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
40 new Token(IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT));
42 IPHPPartitionScannerConstants.HTML,
43 new Token(IPHPPartitionScannerConstants.HTML));
45 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
46 new Token(IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT));
48 IDocument.DEFAULT_CONTENT_TYPE,
49 new Token(IDocument.DEFAULT_CONTENT_TYPE));
52 private IToken getToken(String type) {
53 fLength = fOffset - fTokenOffset;
54 IToken token = (IToken) this.tokens.get(type);
55 Assert.isNotNull(token, "Token for type \"" + type + "\" not found!");
58 "Partition: fTokenOffset="
69 * @see org.eclipse.jface.text.rules.IPartitionTokenScanner#setPartialRange(org.eclipse.jface.text.IDocument, int, int, java.lang.String, int)
71 public void setPartialRange(
78 this.setRange(document, offset, length);
82 "PartialRange: contentType="
88 if(offset == partitionOffset)
92 fContentType = fDocument.getContentType(offset);
94 catch (BadLocationException e)
99 else if (this.tokens.containsKey(contentType))
100 fContentType = contentType;
101 // TODO Calculate previouse contenttype
102 if (partitionOffset > -1)
104 partitionBorder = false;
105 fTokenOffset = partitionOffset;
110 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()
112 public int getTokenLength() {
117 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()
119 public int getTokenOffset() {
124 * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
126 public IToken nextToken()
130 // check if we are not allready at the end of the
132 if ((c = read()) == ICharacterScanner.EOF)
134 partitionBorder = false;
142 fTokenOffset = fOffset;
143 partitionBorder = false;
146 while ((c = read()) != ICharacterScanner.EOF)
152 != IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT
153 && checkPattern(new char[] { '?', 'p', 'h', 'p' }, true))
155 if (fContentType != IPHPPartitionScannerConstants.PHP
159 IToken token = getToken(fContentType);
160 // save previouse contenttype
161 fPrevContentType = fContentType;
163 fContentType = IPHPPartitionScannerConstants.PHP;
168 fContentType = IPHPPartitionScannerConstants.PHP;
170 // remember offset of this partition
171 fTokenOffset = fOffset - 5;
175 != IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT
176 && checkPattern(new char[] { '?' }, false))
178 if (fContentType != IPHPPartitionScannerConstants.PHP
182 IToken token = getToken(fContentType);
183 // save previouse contenttype
186 fContentType = IPHPPartitionScannerConstants.PHP;
190 fContentType = IPHPPartitionScannerConstants.PHP;
191 // remember offset of this partition
195 else if (checkPattern(new char[] { '!', '-', '-' }))
196 { // return previouse partition
199 != IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT
203 IToken token = getToken(fContentType);
205 IPHPPartitionScannerConstants
206 .HTML_MULTILINE_COMMENT;
211 IPHPPartitionScannerConstants
212 .HTML_MULTILINE_COMMENT;
213 fTokenOffset = fOffset - 4;
216 if (fContentType == IPHPPartitionScannerConstants.PHP)
218 if ((c = read()) == '>')
219 { // TODO Actually calculate the previouse contenttype from the document
223 fContentType = fPrevContentType; else
225 IPHPPartitionScannerConstants.HTML;
226 partitionBorder = true;
228 IPHPPartitionScannerConstants.PHP);
230 else if (c != ICharacterScanner.EOF)
234 == IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT
235 && checkPattern(new char[] { '-', '>' }))
237 fContentType = IPHPPartitionScannerConstants.HTML;
238 partitionBorder = true;
240 IPHPPartitionScannerConstants
241 .HTML_MULTILINE_COMMENT);
244 if ((c = read()) == '*')
245 { // MULTINE COMMENT JAVASCRIPT, CSS, PHP
247 fContentType == IPHPPartitionScannerConstants.PHP
251 IToken token = getToken(fContentType);
253 IPHPPartitionScannerConstants
254 .PHP_MULTILINE_COMMENT;
259 == IPHPPartitionScannerConstants
260 .PHP_MULTILINE_COMMENT)
263 fTokenOffset = fOffset - 2; }
266 else if (c != ICharacterScanner.EOF)
267 unread(); break; case '*' :
268 if ((c = read()) == '/')
271 == IPHPPartitionScannerConstants
272 .PHP_MULTILINE_COMMENT)
275 IPHPPartitionScannerConstants.PHP;
276 partitionBorder = true;
278 IPHPPartitionScannerConstants
279 .PHP_MULTILINE_COMMENT);
283 == IPHPPartitionScannerConstants
284 .CSS_MULTILINE_COMMENT)
289 == IPHPPartitionScannerConstants
290 .JS_MULTILINE_COMMENT)
294 else if (c != ICharacterScanner.EOF)
296 } // end of file reached but we have to return the
298 return getToken(fContentType);
301 * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(org.eclipse.jface.text.IDocument, int, int)
303 public void setRange(IDocument document, int offset, int length) {
305 System.out.println("SET RANGE: offset=" + offset + " length=" + length);
308 fDocument = document;
310 fTokenOffset = offset;
312 fEnd = fOffset + length;
313 //partitionBorder = false;
318 if (fOffset < fEnd) {
319 return fDocument.getChar(fOffset++);
321 return ICharacterScanner.EOF;
322 } catch (BadLocationException e) {
323 // should never happen
324 // TODO write stacktrace to log
326 return ICharacterScanner.EOF;
330 private void unread() {
334 private boolean checkPattern(char[] pattern) {
335 return checkPattern(pattern, false);
339 * Check if next character sequence read from document is equals to
340 * the provided pattern. Pattern is read from left to right until the
341 * first character read doesn't match. If this happens all read characters are
343 * @param pattern The pattern to check.
344 * @return <code>true</code> if pattern is equals else returns <code>false</code>.
346 private boolean checkPattern(char[] pattern, boolean ignoreCase) {
347 int prevOffset = fOffset;
348 for (int i = 0; i < pattern.length; i++) {
351 if (c == ICharacterScanner.EOF
352 || !letterEquals(c, pattern[i], ignoreCase)) {
353 fOffset = prevOffset;
361 private boolean letterEquals(int test, char letter, boolean ignoreCase) {
366 && Character.isLowerCase(letter)
367 && test == Character.toUpperCase(letter))
371 && Character.isUpperCase(letter)
372 && test == Character.toLowerCase(letter))