1 /**********************************************************************
2 Copyright (c) 2002 Widespace, OU and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://solareclipse.sourceforge.net/legal/cpl-v10.html
9 Igor Malinin - initial contribution
11 $Id: PHPPartitionScanner.java,v 1.25 2004-09-02 18:32:34 jsurfer Exp $
12 **********************************************************************/
13 package net.sourceforge.phpeclipse.phpeditor.php;
15 import java.util.HashMap;
18 import net.sourceforge.phpeclipse.ui.text.rules.AbstractPartitioner;
20 import org.eclipse.core.internal.indexing.AbstractPagePolicy;
21 import org.eclipse.jface.text.Assert;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.rules.ICharacterScanner;
25 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
26 import org.eclipse.jface.text.rules.IToken;
27 import org.eclipse.jface.text.rules.Token;
32 * @author Igor Malinin
34 public class PHPPartitionScanner implements IPartitionTokenScanner {
35 // public static final String JSP_DIRECTIVE = "__jsp_directive";
36 // public static final String JSP_COMMENT = "__jsp_comment";
37 //// public static final String JSP_TAG = "__jsp_tag";
38 // public static final String JSP_DECLARATION = "__jsp_declaration";
39 public static final String PHP_SCRIPTING_AREA = "__php_scripting_area ";
41 // public static final String JSP_EXPRESSION = "__jsp_expression";
43 public static final int STATE_DEFAULT = 0;
45 // public static final int STATE_TAG = 1;
46 // public static final int STATE_SCRIPT = 2;
48 private IDocument document;
62 private Map tokens = new HashMap();
64 public PHPPartitionScanner() {
68 * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
70 public IToken nextToken() {
74 * switch (state) { case STATE_TAG: return nextTagToken(); }
78 case ICharacterScanner.EOF:
79 state = STATE_DEFAULT;
80 return getToken(null);
84 case ICharacterScanner.EOF:
85 state = STATE_DEFAULT;
86 return getToken(null);
88 case '?': // <%SCRIPLET <%@DIRECTIVE <%!DECLARATION <%=EXPRESSION <%--COMMENT
90 // if (Character.isWhitespace((char)ch)) {
91 // return nextJSPToken(PHP_SCRIPTING_AREA);
94 case ICharacterScanner.EOF:
95 state = STATE_DEFAULT;
96 return getToken(PHP_SCRIPTING_AREA);
98 // case '-': // <%- <%--COMMENT
100 // case ICharacterScanner.EOF:
102 // return nextCommentToken();
108 return scanUntilPHPEndToken(PHP_SCRIPTING_AREA);
116 case ICharacterScanner.EOF:
117 state = STATE_DEFAULT;
118 return getToken(null);
122 case ICharacterScanner.EOF:
123 state = STATE_DEFAULT;
124 return getToken(null);
139 state = STATE_DEFAULT;
140 return getToken(null);
145 private IToken scanUntilPHPEndToken(String token) {
149 case ICharacterScanner.EOF:
150 state = STATE_DEFAULT;
151 return getToken(token);
152 case '"': // double quoted string
153 // read until end of double quoted string
154 readUntilEscaped('"');
156 case '\'': // single quoted string
157 // read until end of single quoted string
158 readUntilEscaped('\'');
160 case '/': // comment start?
163 case ICharacterScanner.EOF:
166 // read until end of line
170 // read until end of comment
171 readMultiLineComment();
177 case '#': // line comment
178 // read until end of line
184 case ICharacterScanner.EOF:
186 state = STATE_DEFAULT;
187 return getToken(token);
198 // private IToken nextCommentToken() {
200 // loop: while (true) {
202 // case ICharacterScanner.EOF:
205 // case '-': // - --%>
208 // case ICharacterScanner.EOF:
211 // case '-': // -- --%>
214 // case ICharacterScanner.EOF:
217 // case '%': // --% --%>
220 // case ICharacterScanner.EOF:
227 // case '-': // --- ---%>
240 // return getToken(JSP_COMMENT);
243 private IToken getToken(String type) {
244 length = position - offset;
251 return Token.UNDEFINED;
254 IToken token = (IToken) tokens.get(type);
256 token = new Token(type);
257 tokens.put(type, token);
264 if (position >= end) {
265 return ICharacterScanner.EOF;
269 return document.getChar(position++);
270 } catch (BadLocationException e) {
272 return ICharacterScanner.EOF;
276 private void readUntilEscaped(char ch) {
277 if (position >= end) {
282 if (document.getChar(position++) == ch) {
283 if (position < 2 || document.getChar(position - 2) != '\\') {
288 } catch (BadLocationException e) {
294 private void readSingleLine() {
295 if (position >= end) {
299 while (document.getChar(position++) != '\n') {
302 } catch (BadLocationException e) {
308 private void readMultiLineComment() {
309 if (position >= end) {
315 ch = document.getChar(position++);
317 if (document.getChar(position) == '/') {
323 } catch (BadLocationException e) {
329 private void unread() {
334 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()
336 public int getTokenOffset() {
337 if (AbstractPartitioner.DEBUG) {
338 Assert.isTrue(offset >= 0, Integer.toString(offset));
344 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()
346 public int getTokenLength() {
351 * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(IDocument, int, int)
353 public void setRange(IDocument document, int offset, int length) {
354 this.document = document;
356 this.end = offset + length;
358 this.offset = offset;
359 this.position = offset;
364 * @see org.eclipse.jface.text.rules.IPartitionTokenScanner
366 public void setPartialRange(IDocument document, int offset, int length, String contentType, int partitionOffset) {
367 state = STATE_DEFAULT;
368 // if (partitionOffset > -1) {
369 // int delta= offset - partitionOffset;
371 // this.setRange(document, partitionOffset, length + delta);
375 setRange(document, partitionOffset, length);
378 // private boolean isContinuationPartition(IDocument document, int offset) {
380 // String type = document.getContentType(offset - 1);
382 // if (type != IDocument.DEFAULT_CONTENT_TYPE) {
385 // } catch (BadLocationException e) {}