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
10 Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
14 import java.util.ArrayList;
15 import java.util.List;
17 import net.sourceforge.phpeclipse.IPreferenceConstants;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
20 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
21 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.preference.PreferenceConverter;
24 import org.eclipse.jface.text.TextAttribute;
25 import org.eclipse.jface.text.rules.EndOfLineRule;
26 import org.eclipse.jface.text.rules.ICharacterScanner;
27 import org.eclipse.jface.text.rules.IRule;
28 import org.eclipse.jface.text.rules.IToken;
29 import org.eclipse.jface.text.rules.IWordDetector;
30 import org.eclipse.jface.text.rules.MultiLineRule;
31 import org.eclipse.jface.text.rules.RuleBasedScanner;
32 import org.eclipse.jface.text.rules.SingleLineRule;
33 import org.eclipse.jface.text.rules.Token;
34 import org.eclipse.jface.text.rules.WhitespaceRule;
35 import org.eclipse.jface.text.rules.WordRule;
40 public class PHPCodeScanner extends RuleBasedScanner implements IPreferenceConstants {
42 private static Token variable;
43 private static Token keyword;
44 private static Token functionName;
45 private static Token string;
46 private static Token comment;
47 private static Token multi_comment;
48 private static Token other;
50 private class PHPWordRule extends WordRule {
51 private StringBuffer fBuffer = new StringBuffer();
53 public PHPWordRule(IWordDetector detector) {
54 super(detector, Token.UNDEFINED);
57 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
58 super(detector, defaultToken);
61 public IToken evaluate(ICharacterScanner scanner) {
62 int c = scanner.read();
63 boolean isVariable = false;
64 if (fDetector.isWordStart((char) c)) {
68 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
72 fBuffer.append((char) c);
74 } while (c != scanner.EOF && fDetector.isWordPart((char) c));
80 IToken token = (IToken) fWords.get(fBuffer.toString());
84 if (fDefaultToken.isUndefined())
85 unreadBuffer(scanner);
92 return Token.UNDEFINED;
96 private static String[] fgConstants = { "__LINE__", "__FILE__", "true", "false", "null", "object", "array" };
97 // private static TextAttribute fSingleLine;
98 // private static TextAttribute fMultiLine;
99 // private static TextAttribute fKeyword;
100 // private static TextAttribute fFunctionName;
101 // private static TextAttribute fString;
102 // private static TextAttribute fVariable;
103 private PHPColorProvider fColorProvider;
106 * Creates a PHP code scanner
108 public PHPCodeScanner(PHPColorProvider provider) {
109 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
111 variable = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE))));
112 keyword = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD))));
113 functionName = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME))));
114 string = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_STRING))));
115 comment = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT))));
116 multi_comment = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT))));
117 other = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT))));
120 List rules = new ArrayList();
122 // Add rule for single line comments.
123 rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$
124 rules.add(new EndOfLineRule("#", comment));
126 // Add rule for strings and character constants.
127 rules.add(new MultiLineRule("\"", "\"", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
128 rules.add(new SingleLineRule("'", "'", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
130 // rules.add(new SingleLineRule("//", "//", php_comment));
131 rules.add(new MultiLineRule("/*", "*/", multi_comment));
133 // Add generic whitespace rule.
134 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
136 // Add word rule for keywords, types, and constants.
137 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), other);
138 for (int i = 0; i < PHPKeywords.PHP_KEYWORS.length; i++)
139 wordRule.addWord(PHPKeywords.PHP_KEYWORS[i], keyword);
140 for (int i = 0; i < PHPFunctionNames.FUNCTION_NAMES.length; i++)
141 wordRule.addWord(PHPFunctionNames.FUNCTION_NAMES[i], functionName);
142 for (int i = 0; i < fgConstants.length; i++)
143 wordRule.addWord(fgConstants[i], functionName);
146 IRule[] result = new IRule[rules.size()];
147 rules.toArray(result);
151 public void updateToken(PHPColorProvider provider) {
152 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
154 variable.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE))));
155 keyword.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD))));
156 functionName.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME))));
157 string.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_STRING))));
158 comment.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT))));
159 multi_comment.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT))));
160 other.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT))));