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.3 $
11 package net.sourceforge.phpeclipse.phpeditor.php;
13 import java.util.ArrayList;
14 import java.util.HashMap;
17 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
20 //import org.eclipse.jface.text.Assert;
21 import org.eclipse.core.runtime.Assert;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.ITypedRegion;
25 import org.eclipse.jface.text.rules.ICharacterScanner;
26 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
27 import org.eclipse.jface.text.rules.IToken;
28 import org.eclipse.jface.text.rules.Token;
33 public class HTMLPartitionScanner implements IPartitionTokenScanner {
34 private static final boolean DEBUG = false;
36 private boolean fInString = false;
38 private boolean fInDoubString = false;
40 private IDocument fDocument = null;
42 private int fOffset = -1;
44 private String fContentType = IPHPPartitions.HTML;
46 private String fPrevContentType = IPHPPartitions.HTML;
48 private boolean partitionBorder = false;
50 private int fTokenOffset;
52 private int fEnd = -1;
56 private int fCurrentLength;
58 private int fFileType;
60 private Map tokens = new HashMap();
62 public HTMLPartitionScanner() {
63 this(IPHPPartitions.PHP_FILE);
66 public HTMLPartitionScanner(int fileType) {
67 this.tokens.put(IPHPPartitions.HTML, new Token(IPHPPartitions.HTML));
68 this.tokens.put(IPHPPartitions.HTML_MULTILINE_COMMENT, new Token(
69 IPHPPartitions.HTML_MULTILINE_COMMENT));
72 .put(IPHPPartitions.SMARTY, new Token(IPHPPartitions.SMARTY));
73 this.tokens.put(IPHPPartitions.SMARTY_MULTILINE_COMMENT, new Token(
74 IPHPPartitions.SMARTY_MULTILINE_COMMENT));
76 this.tokens.put(IDocument.DEFAULT_CONTENT_TYPE, new Token(
77 IDocument.DEFAULT_CONTENT_TYPE));
81 private IToken getToken(String type) {
82 fLength = fCurrentLength;
87 int line = fDocument.getLineOfOffset(fOffset);
88 System.err.println("Error at "
91 + String.valueOf(fOffset
92 - fDocument.getLineOffset(line)));
94 } catch (BadLocationException e) { // should never happen
95 // TODO Write stacktrace to log
99 Assert.isTrue(fLength > 0, "Partition length <= 0!");
101 // String can never cross partition borders so reset string detection
103 fInDoubString = false;
104 IToken token = (IToken) this.tokens.get(type);
105 Assert.isNotNull(token, "Token for type \"" + type + "\" not found!");
107 System.out.println("Partition: fTokenOffset=" + fTokenOffset
108 + " fContentType=" + type + " fLength=" + fLength);
116 * @see org.eclipse.jface.text.rules.IPartitionTokenScanner#setPartialRange(org.eclipse.jface.text.IDocument,
117 * int, int, java.lang.String, int)
119 public void setPartialRange(IDocument document, int offset, int length,
120 String contentType, int partitionOffset) {
122 System.out.println("*****");
123 System.out.println("PartialRange: contentType=" + contentType
124 + " partitionOffset=" + partitionOffset);
128 if (partitionOffset > -1) {
129 partitionBorder = false;
130 // because of strings we have to parse the whole partition
131 this.setRange(document, partitionOffset, offset
132 - partitionOffset + length);
133 // sometimes we get a wrong partition so we retrieve the
135 // directly from the document
136 fContentType = fDocument.getContentType(partitionOffset);
138 this.setRange(document, offset, length);
140 } catch (BadLocationException e) {
141 // should never happen
142 // TODO print stack trace to log
143 // fall back just scan the whole document again
144 this.setRange(document, 0, fDocument.getLength());
152 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()
154 public int getTokenLength() {
161 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()
163 public int getTokenOffset() {
170 * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
172 public IToken nextToken() {
175 // check if we are not allready at the end of the
177 if ((c = read()) == ICharacterScanner.EOF) {
178 partitionBorder = false;
183 if (partitionBorder) {
184 fTokenOffset = fOffset;
185 partitionBorder = false;
188 while ((c = read()) != ICharacterScanner.EOF) {
191 if (checkPattern(new char[] { '!', '-', '-' })) { // return
194 if (fContentType != IPHPPartitions.HTML_MULTILINE_COMMENT
195 && fCurrentLength > 4) {
197 IToken token = getToken(fContentType);
198 fContentType = IPHPPartitions.HTML_MULTILINE_COMMENT;
201 fContentType = IPHPPartitions.HTML_MULTILINE_COMMENT;
203 fTokenOffset = fOffset - 4;
208 if (fContentType == IPHPPartitions.HTML_MULTILINE_COMMENT
209 && checkPattern(new char[] { '-', '>' })) {
210 fContentType = IPHPPartitions.HTML;
211 partitionBorder = true;
212 return getToken(IPHPPartitions.HTML_MULTILINE_COMMENT);
215 case '{': // SMARTY code starts here ?
216 if (fFileType == IPHPPartitions.SMARTY_FILE) {
217 if ((c = read()) == '*') {
219 System.out.println("SMARTYDOC_TOKEN start "
220 + fTokenOffset + " fContentType="
221 + fContentType + " fLength=" + fLength
222 + " fOffset=" + fOffset
223 + " fCurrentLength=" + fCurrentLength);
225 if (fContentType != IPHPPartitions.SMARTY_MULTILINE_COMMENT
226 && fCurrentLength > 2) {
227 // SMARTY doc code starts here
229 IToken token = getToken(fContentType);
230 fContentType = IPHPPartitions.SMARTY_MULTILINE_COMMENT;
232 // } else if (fContentType ==
233 // IPHPPartitionScannerConstants.HTML && fOffset ==
236 // IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT;
237 } else { // if (fContentType ==
238 // IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT)
240 fContentType = IPHPPartitions.SMARTY_MULTILINE_COMMENT;
241 fTokenOffset = fOffset - 2;
247 System.out.println("SMARTY_TOKEN start " + fTokenOffset
248 + " fContentType=" + fContentType + " fLength="
249 + fLength + " fOffset=" + fOffset);
251 if (c != ICharacterScanner.EOF) {
254 if (fContentType != IPHPPartitions.SMARTY
255 && fCurrentLength > 1) {
257 IToken token = getToken(fContentType);
258 fContentType = IPHPPartitions.SMARTY;
260 // } else if (fContentType ==
261 // IPHPPartitionScannerConstants.HTML && fOffset==1) {
262 // fContentType = IPHPPartitionScannerConstants.SMARTY;
264 fContentType = IPHPPartitions.SMARTY;
265 fTokenOffset = fOffset - 1;
270 case '}': // SMARTY code ends here ?
271 if (fFileType == IPHPPartitions.SMARTY_FILE
272 && fContentType == IPHPPartitions.SMARTY) {
274 System.out.println("SMARTY_TOKEN end " + fTokenOffset
275 + " fContentType=" + fContentType + " fLength="
276 + fLength + " fOffset=" + fOffset);
278 fContentType = IPHPPartitions.HTML;
279 partitionBorder = true;
280 return getToken(IPHPPartitions.SMARTY);
284 // if (!isInString(IPHPPartitions.PHP_PARTITIONING) && (c = read())
285 // == '*') { // MULTINE COMMENT JAVASCRIPT, CSS, PHP
286 // if (fContentType == IPHPPartitions.PHP_PARTITIONING &&
287 // fCurrentLength > 2) {
289 // IToken token = getToken(fContentType);
290 // fContentType = IPHPPartitions.PHP_PHPDOC_COMMENT;
292 // } else if (fContentType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
293 // fTokenOffset = fOffset - 2;
294 // fCurrentLength = 2;
297 // } else if (!isInString(IPHPPartitions.PHP_PARTITIONING) && c !=
298 // ICharacterScanner.EOF)
302 if (fFileType == IPHPPartitions.SMARTY_FILE
303 && (c = read()) == '}') {
305 System.out.println("SMARTYDOC_TOKEN end "
306 + fTokenOffset + " fContentType="
307 + fContentType + " fLength=" + fLength
308 + " fOffset=" + fOffset);
310 if (fContentType == IPHPPartitions.SMARTY_MULTILINE_COMMENT) {
311 fContentType = IPHPPartitions.HTML;
312 partitionBorder = true;
313 return getToken(IPHPPartitions.SMARTY_MULTILINE_COMMENT);
319 fInString = !fInString;
322 // toggle String mode
324 fInDoubString = !fInDoubString;
327 } // end of file reached but we have to return the
329 return getToken(fContentType);
335 * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(org.eclipse.jface.text.IDocument,
338 public void setRange(IDocument document, int offset, int length) {
340 System.out.println("SET RANGE: offset=" + offset + " length="
344 fDocument = document;
346 fTokenOffset = offset;
349 fEnd = fOffset + length;
351 fInDoubString = false;
352 fContentType = IPHPPartitions.HTML;
353 // String[] prev = getPartitionStack(offset);
358 if (fOffset < fEnd) {
360 return fDocument.getChar(fOffset++);
362 return ICharacterScanner.EOF;
363 } catch (BadLocationException e) {
364 // should never happen
365 // TODO write stacktrace to log
367 return ICharacterScanner.EOF;
371 private void unread() {
376 private void unread(int num) {
378 fCurrentLength -= num;
381 private boolean checkPattern(char[] pattern) {
382 return checkPattern(pattern, false);
386 * Check if next character sequence read from document is equals to the
387 * provided pattern. Pattern is read from left to right until the first
388 * character read doesn't match. If this happens all read characters are
392 * The pattern to check.
393 * @return <code>true</code> if pattern is equals else returns
394 * <code>false</code>.
396 private boolean checkPattern(char[] pattern, boolean ignoreCase) {
397 int prevOffset = fOffset;
398 int prevLength = fCurrentLength;
399 for (int i = 0; i < pattern.length; i++) {
402 if (c == ICharacterScanner.EOF
403 || !letterEquals(c, pattern[i], ignoreCase)) {
404 fOffset = prevOffset;
405 fCurrentLength = prevLength;
413 private boolean letterEquals(int test, char letter, boolean ignoreCase) {
416 else if (ignoreCase && Character.isLowerCase(letter)
417 && test == Character.toUpperCase(letter))
419 else if (ignoreCase && Character.isUpperCase(letter)
420 && test == Character.toLowerCase(letter))
427 * Checks wether the offset is in a <code>String</code> and the specified
428 * contenttype is the current content type. Strings are delimited, mutual
429 * exclusive, by a " or by a '.
432 * The contenttype to check.
433 * @return <code>true</code> if the current offset is in a string else
436 private boolean isInString(String contentType) {
437 if (fContentType == contentType)
438 return (fInString || fInDoubString);
444 * Returns the previouse partition stack for the given offset.
447 * The offset to return the previouse partitionstack for.
449 * @return The stack as a string array.
451 private String[] getPartitionStack(int offset) {
452 ArrayList types = new ArrayList();
455 ITypedRegion region = fDocument.getPartition(offset);
456 tmpOffset = region.getOffset();
457 while (tmpOffset - 1 > 0) {
458 region = fDocument.getPartition(tmpOffset - 1);
459 tmpOffset = region.getOffset();
460 types.add(0, region.getType());
462 } catch (BadLocationException e) {
468 String[] retVal = new String[types.size()];
470 retVal = (String[]) types.toArray(retVal);