1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. 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://www.eclipse.org/legal/cpl-v10.html
9 IBM Corporation - Initial implementation
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
14 import java.util.ArrayList;
15 import java.util.List;
17 import net.sourceforge.phpdt.internal.ui.text.AbstractJavaScanner;
18 import net.sourceforge.phpdt.ui.text.IColorManager;
19 import net.sourceforge.phpeclipse.IPreferenceConstants;
20 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
21 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
22 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.text.rules.ICharacterScanner;
26 import org.eclipse.jface.text.rules.IRule;
27 import org.eclipse.jface.text.rules.IToken;
28 import org.eclipse.jface.text.rules.IWordDetector;
29 import org.eclipse.jface.text.rules.MultiLineRule;
30 import org.eclipse.jface.text.rules.Token;
31 import org.eclipse.jface.text.rules.WhitespaceRule;
32 import org.eclipse.jface.text.rules.WordRule;
37 public class PHPCodeScanner extends AbstractJavaScanner {
40 * Rule to detect java operators.
44 protected class OperatorRule implements IRule {
47 private final char[] PHP_OPERATORS = {
73 /** Token to return for this rule */
74 private final IToken fToken;
76 /** Token to return for braces */
77 private final IToken fTokenBraces;
80 * Creates a new operator rule.
83 * Token to use for this rule
85 public OperatorRule(IToken token, IToken tokenBraces) {
87 fTokenBraces = tokenBraces;
92 * Is this character an operator character?
95 * Character to determine whether it is an operator character
96 * @return <code>true</code> iff the character is an operator, <code>false</code> otherwise.
98 public boolean isOperator(char character) {
99 for (int index = 0; index < PHP_OPERATORS.length; index++) {
100 if (PHP_OPERATORS[index] == character)
107 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
109 public IToken evaluate(ICharacterScanner scanner) {
111 int character = scanner.read();
112 if (character == '{' || character == '}') {
115 if (isOperator((char) character)) {
116 int lastCharacter = character;
117 character = scanner.read();
118 if (!isOperator((char) character)) {
122 if (checkPHPTag(scanner, lastCharacter, character)) {
123 return Token.UNDEFINED;
126 lastCharacter = character;
127 character = scanner.read();
128 if (checkPHPTag(scanner, lastCharacter, character)) {
131 } while (isOperator((char) character));
136 return Token.UNDEFINED;
141 * Check if lastCharacter/character are a PHP start or end token ( <? ... ?> )
144 * @param lastCharacter
148 private boolean checkPHPTag(ICharacterScanner scanner, int lastCharacter, int character) {
149 if (lastCharacter == '<' && character == '?') {
153 } else if (lastCharacter == '?' && character == '>') {
163 protected class AccentStringRule implements IRule {
165 /** Token to return for this rule */
166 private final IToken fToken;
168 public AccentStringRule(IToken token) {
174 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
176 public IToken evaluate(ICharacterScanner scanner) {
178 int character = scanner.read();
180 if (character == '`') {
182 while (character != ICharacterScanner.EOF) {
183 character = scanner.read();
184 if (character == '\\') {
185 character = scanner.read();
186 } else if (character == '`') {
191 return Token.UNDEFINED;
194 return Token.UNDEFINED;
200 private class PHPWordRule extends WordRule {
201 private StringBuffer fBuffer = new StringBuffer();
203 public PHPWordRule(IWordDetector detector) {
204 super(detector, Token.UNDEFINED);
207 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
208 super(detector, defaultToken);
211 public IToken evaluate(ICharacterScanner scanner) {
212 int c = scanner.read();
213 boolean isVariable = false;
214 boolean isUnderscore = false;
220 return Token.UNDEFINED;
223 if (c == '=') { // <?=
224 return getToken(IPreferenceConstants.PHP_TAG);
226 if (c != 'p' && c != 'P') {
228 return getToken(IPreferenceConstants.PHP_TAG);
231 if (c != 'h' && c != 'H') {
234 return getToken(IPreferenceConstants.PHP_TAG);
237 if (c != 'p' && c != 'P') {
241 return getToken(IPreferenceConstants.PHP_TAG);
243 return getToken(IPreferenceConstants.PHP_TAG);
252 return getToken(IPreferenceConstants.PHP_TAG);
256 return Token.UNDEFINED;
258 if (fDetector.isWordStart((char) c)) {
262 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
264 fBuffer.setLength(0);
265 fBuffer.append((char) c);
270 while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c)) {
271 fBuffer.append((char) c);
278 return getToken(IPreferenceConstants.PHP_VARIABLE_DOLLAR);
280 return getToken(IPreferenceConstants.PHP_VARIABLE);
282 IToken token = (IToken) fWords.get(fBuffer.toString());
286 if (fDefaultToken.isUndefined())
287 unreadBuffer(scanner);
289 return fDefaultToken;
294 return Token.UNDEFINED;
298 //private PHPColorProvider fColorProvider;
300 private static String[] fgTokenProperties = {
301 IPreferenceConstants.PHP_MULTILINE_COMMENT,
302 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
303 IPreferenceConstants.PHP_TAG,
304 IPreferenceConstants.PHP_KEYWORD,
305 IPreferenceConstants.PHP_FUNCTIONNAME,
306 IPreferenceConstants.PHP_VARIABLE,
307 IPreferenceConstants.PHP_VARIABLE_DOLLAR,
308 IPreferenceConstants.PHP_STRING_DQ,
309 IPreferenceConstants.PHP_STRING_SQ,
310 IPreferenceConstants.PHP_TYPE,
311 IPreferenceConstants.PHP_CONSTANT,
312 IPreferenceConstants.PHP_DEFAULT,
313 IPreferenceConstants.PHP_OPERATOR,
314 IPreferenceConstants.PHP_BRACE_OPERATOR,
315 IPreferenceConstants.PHP_KEYWORD_RETURN };
318 * Creates a PHP code scanner
320 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
321 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
322 super(manager, store);
327 * @see AbstractJavaScanner#getTokenProperties()
329 protected String[] getTokenProperties() {
330 return fgTokenProperties;
334 * @see AbstractJavaScanner#createRules()
336 protected List createRules() {
337 List rules = new ArrayList();
338 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
339 // Add rule for single line comments.
340 // rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
341 // rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
342 // Add rule for strings and character constants.
343 // token = getToken(IPreferenceConstants.PHP_STRING_SQ);
344 // rules.add(new SingleQuoteStringRule(token));
345 // token = getToken(IPreferenceConstants.PHP_STRING_DQ);
346 // rules.add(new DoubleQuoteStringRule(token));
347 rules.add(new AccentStringRule(token));
349 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
350 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
351 // Add generic whitespace rule.
352 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
353 // Add word rule for keywords, types, and constants.
354 token = getToken(IPreferenceConstants.PHP_DEFAULT);
355 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
357 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
358 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
359 Token type = getToken(IPreferenceConstants.PHP_TYPE);
360 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
362 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
363 // String strbuffer = null; unused
364 PHPElement elbuffer = null;
366 for (int i = 0; i < buffer.size(); i++) {
367 // while ((buffer != null)
368 // && (!buffer.isEmpty()
369 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
370 elbuffer = (PHPElement) buffer.get(i);
371 if (elbuffer instanceof PHPKeyword) {
372 name = ((PHPKeyword) elbuffer).getName();
373 if (!name.equals("return")) {
374 wordRule.addWord(name, keyword);
376 } else if (elbuffer instanceof PHPFunction) {
377 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
378 } else if (elbuffer instanceof PHPType) {
379 wordRule.addWord(elbuffer.getName(), type);
380 } else if (elbuffer instanceof PHPConstant) {
381 wordRule.addWord(elbuffer.getName(), constant);
385 // Add word rule for keyword 'return'.
386 token = getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
387 wordRule.addWord("return", token);
389 // Add rule for operators and brackets (at the end !)
390 rules.add(new OperatorRule(getToken(IPreferenceConstants.PHP_OPERATOR), getToken(IPreferenceConstants.PHP_BRACE_OPERATOR)));
394 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));