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.15 $
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
192 fTokenOffset = fOffset - 2;
194 else if (checkPattern(new char[] { '!', '-', '-' }))
195 { // return previouse partition
198 != IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT
202 IToken token = getToken(fContentType);
204 IPHPPartitionScannerConstants
205 .HTML_MULTILINE_COMMENT;
210 IPHPPartitionScannerConstants
211 .HTML_MULTILINE_COMMENT;
212 fTokenOffset = fOffset - 4;
215 if (fContentType == IPHPPartitionScannerConstants.PHP)
217 if ((c = read()) == '>')
218 { // TODO Actually calculate the previouse contenttype from the document
222 fContentType = fPrevContentType; else
224 IPHPPartitionScannerConstants.HTML;
225 partitionBorder = true;
227 IPHPPartitionScannerConstants.PHP);
229 else if (c != ICharacterScanner.EOF)
233 == IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT
234 && checkPattern(new char[] { '-', '>' }))
236 fContentType = IPHPPartitionScannerConstants.HTML;
237 partitionBorder = true;
239 IPHPPartitionScannerConstants
240 .HTML_MULTILINE_COMMENT);
243 if ((c = read()) == '*')
244 { // MULTINE COMMENT JAVASCRIPT, CSS, PHP
246 fContentType == IPHPPartitionScannerConstants.PHP
250 IToken token = getToken(fContentType);
252 IPHPPartitionScannerConstants
253 .PHP_MULTILINE_COMMENT;
258 == IPHPPartitionScannerConstants
259 .PHP_MULTILINE_COMMENT)
262 fTokenOffset = fOffset - 2; }
265 else if (c != ICharacterScanner.EOF)
266 unread(); break; case '*' :
267 if ((c = read()) == '/')
270 == IPHPPartitionScannerConstants
271 .PHP_MULTILINE_COMMENT)
274 IPHPPartitionScannerConstants.PHP;
275 partitionBorder = true;
277 IPHPPartitionScannerConstants
278 .PHP_MULTILINE_COMMENT);
282 == IPHPPartitionScannerConstants
283 .CSS_MULTILINE_COMMENT)
288 == IPHPPartitionScannerConstants
289 .JS_MULTILINE_COMMENT)
293 else if (c != ICharacterScanner.EOF)
295 } // end of file reached but we have to return the
297 return getToken(fContentType);
300 * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(org.eclipse.jface.text.IDocument, int, int)
302 public void setRange(IDocument document, int offset, int length) {
304 System.out.println("SET RANGE: offset=" + offset + " length=" + length);
307 fDocument = document;
309 fTokenOffset = offset;
311 fEnd = fOffset + length;
312 //partitionBorder = false;
317 if (fOffset < fEnd) {
318 return fDocument.getChar(fOffset++);
320 return ICharacterScanner.EOF;
321 } catch (BadLocationException e) {
322 // should never happen
323 // TODO write stacktrace to log
325 return ICharacterScanner.EOF;
329 private void unread() {
333 private boolean checkPattern(char[] pattern) {
334 return checkPattern(pattern, false);
338 * Check if next character sequence read from document is equals to
339 * the provided pattern. Pattern is read from left to right until the
340 * first character read doesn't match. If this happens all read characters are
342 * @param pattern The pattern to check.
343 * @return <code>true</code> if pattern is equals else returns <code>false</code>.
345 private boolean checkPattern(char[] pattern, boolean ignoreCase) {
346 int prevOffset = fOffset;
347 for (int i = 0; i < pattern.length; i++) {
350 if (c == ICharacterScanner.EOF
351 || !letterEquals(c, pattern[i], ignoreCase)) {
352 fOffset = prevOffset;
360 private boolean letterEquals(int test, char letter, boolean ignoreCase) {
365 && Character.isLowerCase(letter)
366 && test == Character.toUpperCase(letter))
370 && Character.isUpperCase(letter)
371 && test == Character.toLowerCase(letter))