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.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.EndOfLineRule;
26 import org.eclipse.jface.text.rules.ICharacterScanner;
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
38 extends AbstractJavaScanner {
40 // private static Token variable;
41 // private static Token keyword;
42 // private static Token type;
43 // private static Token constant;
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);
75 c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
79 return getToken(IPreferenceConstants.PHP_VARIABLE);
81 IToken token = (IToken) fWords.get(fBuffer.toString());
85 if (fDefaultToken.isUndefined())
86 unreadBuffer(scanner);
93 return Token.UNDEFINED;
97 //private PHPColorProvider fColorProvider;
99 private static String[] fgTokenProperties = {
100 IPreferenceConstants.PHP_MULTILINE_COMMENT,
101 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
102 IPreferenceConstants.PHP_KEYWORD,
103 IPreferenceConstants.PHP_FUNCTIONNAME,
104 IPreferenceConstants.PHP_VARIABLE,
105 IPreferenceConstants.PHP_STRING,
106 IPreferenceConstants.PHP_TYPE,
107 IPreferenceConstants.PHP_CONSTANT,
108 IPreferenceConstants.PHP_DEFAULT };
110 * Creates a PHP code scanner
112 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
113 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
114 super(manager, store);
116 // // final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
117 // Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
120 // new TextAttribute(
121 // provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
123 // (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
124 // + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
126 // new Token(new TextAttribute(
127 // provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
130 // (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
132 // new Token(new TextAttribute(
133 // provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
136 // (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
138 // new Token(new TextAttribute(
139 // provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
142 // (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
143 // + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
145 // new Token(new TextAttribute(
146 // provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
149 // (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
151 // new Token(new TextAttribute(
152 // provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
155 // (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE ) + (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
157 // new Token(new TextAttribute(
158 // provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)),
161 // (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE )
162 // + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
164 // new Token(new TextAttribute(
165 // provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)),
168 // (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
169 // + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
171 // new Token(new TextAttribute(
172 // provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
175 // (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
176 // updateWordRules();
179 // public void updateToken(JavaColorManager provider) {
180 // final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
182 // Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
185 // new TextAttribute(
186 // provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
188 // (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
189 // + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
191 // new TextAttribute(
192 // provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
194 // (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE)
195 // + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
197 // new TextAttribute(
198 // provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
200 // (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
201 // functionName.setData(
202 // new TextAttribute(
203 // provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
205 // (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
206 // + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
208 // new TextAttribute(
209 // provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
211 // (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE)
212 // + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
214 // new TextAttribute(
215 // provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
217 // (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
219 // new TextAttribute(
220 // provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)),
222 // (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
223 // + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
224 // multi_comment.setData(
225 // new TextAttribute(
226 // provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)),
228 // (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
229 // + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
231 // new TextAttribute(
232 // provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
234 // (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE)
235 // + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
238 // public void updateWordRules() {
241 * @see AbstractJavaScanner#getTokenProperties()
243 protected String[] getTokenProperties() {
244 return fgTokenProperties;
247 * @see AbstractJavaScanner#createRules()
249 protected List createRules() {
250 List rules = new ArrayList();
251 Token token= getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
252 // Add rule for single line comments.
253 rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
254 rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
255 // Add rule for strings and character constants.
256 token= getToken(IPreferenceConstants.PHP_STRING);
257 rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
258 rules.add(new MultiLineRule("`", "`", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
259 rules.add(new MultiLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
262 //rules.add(new SingleLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
265 token= getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
266 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
267 // Add generic whitespace rule.
268 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
269 // Add word rule for keywords, types, and constants.
270 token= getToken(IPreferenceConstants.PHP_DEFAULT);
271 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
273 Token keyword= getToken(IPreferenceConstants.PHP_KEYWORD);
274 Token functionName= getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
275 Token type= getToken(IPreferenceConstants.PHP_TYPE);
276 Token constant= getToken(IPreferenceConstants.PHP_CONSTANT);
278 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
279 // String strbuffer = null; unused
280 PHPElement elbuffer = null;
281 for (int i=0;i<buffer.size();i++) {
282 // while ((buffer != null)
283 // && (!buffer.isEmpty()
284 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
285 elbuffer = (PHPElement) buffer.get(i);
286 if (elbuffer instanceof PHPKeyword)
287 wordRule.addWord(((PHPKeyword) elbuffer).getName(), keyword);
288 if (elbuffer instanceof PHPFunction)
289 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
290 if (elbuffer instanceof PHPType)
291 wordRule.addWord(elbuffer.getName(), type);
292 if (elbuffer instanceof PHPConstant)
293 wordRule.addWord(elbuffer.getName(), constant);
296 // IRule[] result = new IRule[rules.size()];unused
297 // rules.toArray(result);