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.27 2005-05-06 00:57:28 stefanbjarni 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.jface.text.Assert;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.rules.ICharacterScanner;
24 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
25 import org.eclipse.jface.text.rules.IToken;
26 import org.eclipse.jface.text.rules.Token;
31 * @author Igor Malinin
33 public class PHPPartitionScanner implements IPartitionTokenScanner {
34 // public static final String JSP_DIRECTIVE = "__jsp_directive";
35 // public static final String JSP_COMMENT = "__jsp_comment";
36 //// public static final String JSP_TAG = "__jsp_tag";
37 // public static final String JSP_DECLARATION = "__jsp_declaration";
38 public static final String PHP_SCRIPTING_AREA = "__php_scripting_area ";
40 // public static final String JSP_EXPRESSION = "__jsp_expression";
42 public static final int STATE_DEFAULT = 0;
44 // public static final int STATE_TAG = 1;
45 // public static final int STATE_SCRIPT = 2;
47 private IDocument document;
61 private Map tokens = new HashMap();
63 public PHPPartitionScanner() {
67 * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
69 public IToken nextToken() {
73 * switch (state) { case STATE_TAG: return nextTagToken(); }
77 case ICharacterScanner.EOF:
78 state = STATE_DEFAULT;
79 return getToken(null);
83 case ICharacterScanner.EOF:
84 state = STATE_DEFAULT;
85 return getToken(null);
87 case '?': // <%SCRIPLET <%@DIRECTIVE <%!DECLARATION <%=EXPRESSION <%--COMMENT
89 // if (Character.isWhitespace((char)ch)) {
90 // return nextJSPToken(PHP_SCRIPTING_AREA);
93 case ICharacterScanner.EOF:
94 state = STATE_DEFAULT;
95 return getToken(PHP_SCRIPTING_AREA);
97 // case '-': // <%- <%--COMMENT
99 // case ICharacterScanner.EOF:
101 // return nextCommentToken();
107 return scanUntilPHPEndToken(PHP_SCRIPTING_AREA);
115 case ICharacterScanner.EOF:
116 state = STATE_DEFAULT;
117 return getToken(null);
121 case ICharacterScanner.EOF:
122 state = STATE_DEFAULT;
123 return getToken(null);
138 state = STATE_DEFAULT;
139 return getToken(null);
144 private IToken scanUntilPHPEndToken(String token) {
148 case ICharacterScanner.EOF:
149 state = STATE_DEFAULT;
150 return getToken(token);
151 case '"': // double quoted string
152 // read until end of double quoted string
153 if (!readUntilEscapedDQ()) {
154 state = STATE_DEFAULT;
155 return getToken(token);
158 case '\'': // single quoted string
159 // read until end of single quoted string
160 if (!readUntilEscapedSQ()) {
161 state = STATE_DEFAULT;
162 return getToken(token);
165 case '/': // comment start?
168 case ICharacterScanner.EOF:
171 // read until end of line
175 // read until end of comment
176 readMultiLineComment();
182 case '#': // line comment
183 // read until end of line
189 case ICharacterScanner.EOF:
191 state = STATE_DEFAULT;
192 return getToken(token);
203 // private IToken nextCommentToken() {
205 // loop: while (true) {
207 // case ICharacterScanner.EOF:
210 // case '-': // - --%>
213 // case ICharacterScanner.EOF:
216 // case '-': // -- --%>
219 // case ICharacterScanner.EOF:
222 // case '%': // --% --%>
225 // case ICharacterScanner.EOF:
232 // case '-': // --- ---%>
245 // return getToken(JSP_COMMENT);
248 private IToken getToken(String type) {
249 length = position - offset;
256 return Token.UNDEFINED;
259 IToken token = (IToken) tokens.get(type);
261 token = new Token(type);
262 tokens.put(type, token);
269 if (position >= end) {
270 return ICharacterScanner.EOF;
274 return document.getChar(position++);
275 } catch (BadLocationException e) {
277 return ICharacterScanner.EOF;
281 private boolean readUntilEscapedDQ() {
282 // search last double quoted character
283 if (position >= end) {
289 ch = document.getChar(position++);
291 ch = document.getChar(position++); // ignore escaped character
292 } else if (ch == '"') {
296 } catch (BadLocationException e) {
302 private boolean readUntilEscapedSQ() {
303 // search last single quoted character
304 if (position >= end) {
310 ch = document.getChar(position++);
312 ch = document.getChar(position++); // ignore escaped character
313 } else if (ch == '\'') {
317 } catch (BadLocationException e) {
323 private void readSingleLine() {
324 if (position >= end) {
328 while (document.getChar(position++) != '\n') {
331 } catch (BadLocationException e) {
337 private void readMultiLineComment() {
338 if (position >= end) {
344 ch = document.getChar(position++);
346 if (document.getChar(position) == '/') {
352 } catch (BadLocationException e) {
358 private void unread() {
363 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()
365 public int getTokenOffset() {
366 if (AbstractPartitioner.DEBUG) {
367 Assert.isTrue(offset >= 0, Integer.toString(offset));
373 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()
375 public int getTokenLength() {
380 * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(IDocument, int, int)
382 public void setRange(IDocument document, int offset, int length) {
383 this.document = document;
385 this.end = offset + length;
387 this.offset = offset;
388 this.position = offset;
393 * @see org.eclipse.jface.text.rules.IPartitionTokenScanner
395 public void setPartialRange(IDocument document, int offset, int length, String contentType, int partitionOffset) {
396 state = STATE_DEFAULT;
397 // if (partitionOffset > -1) {
398 // int delta= offset - partitionOffset;
400 // this.setRange(document, partitionOffset, length + delta);
404 setRange(document, partitionOffset, length);
407 // private boolean isContinuationPartition(IDocument document, int offset) {
409 // String type = document.getContentType(offset - 1);
411 // if (type != IDocument.DEFAULT_CONTENT_TYPE) {
414 // } catch (BadLocationException e) {}