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.13 $
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(
76 int partitionOffset) {
77 this.setRange(document, offset, length);
80 "PartialRange: contentType="
86 if (this.tokens.containsKey(contentType))
87 fContentType = contentType;
88 // TODO Calculate previouse contenttype
89 if (partitionOffset > -1) {
90 partitionBorder = false;
91 fTokenOffset = partitionOffset;
96 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()
98 public int getTokenLength() {
103 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()
105 public int getTokenOffset() {
110 * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
112 public IToken nextToken() {
115 // check if we are not allready at the end of the
117 if ((c = read()) == ICharacterScanner.EOF) {
118 partitionBorder = false;
123 if (partitionBorder) {
124 fTokenOffset = fOffset;
125 partitionBorder = false;
128 while ((c = read()) != ICharacterScanner.EOF) {
132 != IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT
133 && checkPattern(new char[] { '?', 'p', 'h', 'p' }, true)) {
134 if (fContentType != IPHPPartitionScannerConstants.PHP
135 && fOffset - 5 > 0) {
137 IToken token = getToken(fContentType);
138 // save previouse contenttype
139 fPrevContentType = fContentType;
141 fContentType = IPHPPartitionScannerConstants.PHP;
145 fContentType = IPHPPartitionScannerConstants.PHP;
147 // remember offset of this partition
148 fTokenOffset = fOffset - 5;
149 } else if (fContentType != IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT
150 && (checkPattern(new char[] { '?', ' ' }, false)
151 || checkPattern(new char[] { '?', '\r' }, false)
152 || checkPattern(new char[] { '?', '\n' }, false))) {
153 if (fContentType != IPHPPartitionScannerConstants.PHP
154 && fOffset - 3 > 0) {
156 IToken token = getToken(fContentType);
157 // save previouse contenttype
158 fPrevContentType = fContentType;
160 fContentType = IPHPPartitionScannerConstants.PHP;
164 fContentType = IPHPPartitionScannerConstants.PHP;
166 // remember offset of this partition
167 fTokenOffset = fOffset - 3;
168 } else if (checkPattern(new char[] { '!', '-', '-' })) {
169 // return previouse partition
171 != IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT
172 && fOffset - 4 > 0) {
174 IToken token = getToken(fContentType);
177 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT;
181 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT;
183 fTokenOffset = fOffset - 4;
187 if (fContentType == IPHPPartitionScannerConstants.PHP) {
188 if ((c = read()) == '>') {
189 // TODO Actually calculate the previouse contenttype from the document
190 if (fPrevContentType != null)
191 fContentType = fPrevContentType;
193 fContentType = IPHPPartitionScannerConstants.HTML;
194 partitionBorder = true;
195 return getToken(IPHPPartitionScannerConstants.PHP);
196 } else if (c != ICharacterScanner.EOF)
202 == IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT
203 && checkPattern(new char[] { '-', '>' })) {
204 fContentType = IPHPPartitionScannerConstants.HTML;
205 partitionBorder = true;
207 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
211 if ((c = read()) == '*') { // MULTINE COMMENT JAVASCRIPT, CSS, PHP
212 if (fContentType == IPHPPartitionScannerConstants.PHP
213 && fOffset - 2 > 0) {
215 IToken token = getToken(fContentType);
218 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT;
223 == IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT) {
225 fTokenOffset = fOffset - 2;
228 } else if (c != ICharacterScanner.EOF)
232 if ((c = read()) == '/') {
234 == IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT) {
235 fContentType = IPHPPartitionScannerConstants.PHP;
236 partitionBorder = true;
239 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
242 == IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT) {
245 == IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT) {
247 } else if (c != ICharacterScanner.EOF)
253 // end of file reached but we have to return the
255 return getToken(fContentType);
258 * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(org.eclipse.jface.text.IDocument, int, int)
260 public void setRange(IDocument document, int offset, int length) {
262 System.out.println("SET RANGE: offset=" + offset + " length=" + length);
265 fDocument = document;
267 fTokenOffset = offset;
269 fEnd = fOffset + length;
270 //partitionBorder = false;
275 if (fOffset < fEnd) {
276 return fDocument.getChar(fOffset++);
278 return ICharacterScanner.EOF;
279 } catch (BadLocationException e) {
280 // should never happen
281 // TODO write stacktrace to log
283 return ICharacterScanner.EOF;
287 private void unread() {
291 private boolean checkPattern(char[] pattern) {
292 return checkPattern(pattern, false);
296 * Check if next character sequence read from document is equals to
297 * the provided pattern. Pattern is read from left to right until the
298 * first character read doesn't match. If this happens all read characters are
300 * @param pattern The pattern to check.
301 * @return <code>true</code> if pattern is equals else returns <code>false</code>.
303 private boolean checkPattern(char[] pattern, boolean ignoreCase) {
304 int prevOffset = fOffset;
305 for (int i = 0; i < pattern.length; i++) {
308 if (c == ICharacterScanner.EOF
309 || !letterEquals(c, pattern[i], ignoreCase)) {
310 fOffset = prevOffset;
318 private boolean letterEquals(int test, char letter, boolean ignoreCase) {
323 && Character.isLowerCase(letter)
324 && test == Character.toUpperCase(letter))
328 && Character.isUpperCase(letter)
329 && test == Character.toLowerCase(letter))