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 == '`') {
183 character = scanner.read();
184 if (character == '\\') {
185 character = scanner.read();
186 } else if (character == '`') {
193 return Token.UNDEFINED;
199 private class PHPWordRule extends WordRule {
200 private StringBuffer fBuffer = new StringBuffer();
202 public PHPWordRule(IWordDetector detector) {
203 super(detector, Token.UNDEFINED);
206 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
207 super(detector, defaultToken);
210 public IToken evaluate(ICharacterScanner scanner) {
211 int c = scanner.read();
212 boolean isVariable = false;
213 boolean isUnderscore = false;
219 return Token.UNDEFINED;
222 if (c == '=') { // <?=
223 return getToken(IPreferenceConstants.PHP_TAG);
225 if (c != 'p' && c != 'P') {
227 return getToken(IPreferenceConstants.PHP_TAG);
230 if (c != 'h' && c != 'H') {
233 return getToken(IPreferenceConstants.PHP_TAG);
236 if (c != 'p' && c != 'P') {
240 return getToken(IPreferenceConstants.PHP_TAG);
242 return getToken(IPreferenceConstants.PHP_TAG);
251 return getToken(IPreferenceConstants.PHP_TAG);
255 return Token.UNDEFINED;
257 if (fDetector.isWordStart((char) c)) {
261 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
263 fBuffer.setLength(0);
264 fBuffer.append((char) c);
269 while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c)) {
270 fBuffer.append((char) c);
277 return getToken(IPreferenceConstants.PHP_VARIABLE_DOLLAR);
279 return getToken(IPreferenceConstants.PHP_VARIABLE);
281 IToken token = (IToken) fWords.get(fBuffer.toString());
285 if (fDefaultToken.isUndefined())
286 unreadBuffer(scanner);
288 return fDefaultToken;
293 return Token.UNDEFINED;
297 //private PHPColorProvider fColorProvider;
299 private static String[] fgTokenProperties = {
300 IPreferenceConstants.PHP_MULTILINE_COMMENT,
301 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
302 IPreferenceConstants.PHP_TAG,
303 IPreferenceConstants.PHP_KEYWORD,
304 IPreferenceConstants.PHP_FUNCTIONNAME,
305 IPreferenceConstants.PHP_VARIABLE,
306 IPreferenceConstants.PHP_VARIABLE_DOLLAR,
307 IPreferenceConstants.PHP_STRING_DQ,
308 IPreferenceConstants.PHP_STRING_SQ,
309 IPreferenceConstants.PHP_TYPE,
310 IPreferenceConstants.PHP_CONSTANT,
311 IPreferenceConstants.PHP_DEFAULT,
312 IPreferenceConstants.PHP_OPERATOR,
313 IPreferenceConstants.PHP_BRACE_OPERATOR,
314 IPreferenceConstants.PHP_KEYWORD_RETURN };
317 * Creates a PHP code scanner
319 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
320 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
321 super(manager, store);
326 * @see AbstractJavaScanner#getTokenProperties()
328 protected String[] getTokenProperties() {
329 return fgTokenProperties;
333 * @see AbstractJavaScanner#createRules()
335 protected List createRules() {
336 List rules = new ArrayList();
337 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
338 // Add rule for single line comments.
339 // rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
340 // rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
341 // Add rule for strings and character constants.
342 // token = getToken(IPreferenceConstants.PHP_STRING_SQ);
343 // rules.add(new SingleQuoteStringRule(token));
344 // token = getToken(IPreferenceConstants.PHP_STRING_DQ);
345 // rules.add(new DoubleQuoteStringRule(token));
346 rules.add(new AccentStringRule(token));
348 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
349 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
350 // Add generic whitespace rule.
351 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
352 // Add word rule for keywords, types, and constants.
353 token = getToken(IPreferenceConstants.PHP_DEFAULT);
354 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
356 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
357 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
358 Token type = getToken(IPreferenceConstants.PHP_TYPE);
359 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
361 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
362 // String strbuffer = null; unused
363 PHPElement elbuffer = null;
365 for (int i = 0; i < buffer.size(); i++) {
366 // while ((buffer != null)
367 // && (!buffer.isEmpty()
368 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
369 elbuffer = (PHPElement) buffer.get(i);
370 if (elbuffer instanceof PHPKeyword) {
371 name = ((PHPKeyword) elbuffer).getName();
372 if (!name.equals("return")) {
373 wordRule.addWord(name, keyword);
375 } else if (elbuffer instanceof PHPFunction) {
376 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
377 } else if (elbuffer instanceof PHPType) {
378 wordRule.addWord(elbuffer.getName(), type);
379 } else if (elbuffer instanceof PHPConstant) {
380 wordRule.addWord(elbuffer.getName(), constant);
384 // Add word rule for keyword 'return'.
385 token = getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
386 wordRule.addWord("return", token);
388 // Add rule for operators and brackets (at the end !)
389 rules.add(new OperatorRule(getToken(IPreferenceConstants.PHP_OPERATOR), getToken(IPreferenceConstants.PHP_BRACE_OPERATOR)));
393 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));