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 extends AbstractJavaScanner {
39 private class PHPWordRule extends WordRule {
40 private StringBuffer fBuffer = new StringBuffer();
42 public PHPWordRule(IWordDetector detector) {
43 super(detector, Token.UNDEFINED);
46 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
47 super(detector, defaultToken);
50 public IToken evaluate(ICharacterScanner scanner) {
51 int c = scanner.read();
52 boolean isVariable = false;
70 return getToken(IPreferenceConstants.PHP_TAG);
80 return getToken(IPreferenceConstants.PHP_TAG);
84 if (fDetector.isWordStart((char) c)) {
88 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
92 fBuffer.append((char) c);
94 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
98 return getToken(IPreferenceConstants.PHP_VARIABLE);
100 IToken token = (IToken) fWords.get(fBuffer.toString());
104 if (fDefaultToken.isUndefined())
105 unreadBuffer(scanner);
107 return fDefaultToken;
112 return Token.UNDEFINED;
116 //private PHPColorProvider fColorProvider;
118 private static String[] fgTokenProperties =
120 IPreferenceConstants.PHP_MULTILINE_COMMENT,
121 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
122 IPreferenceConstants.PHP_TAG,
123 IPreferenceConstants.PHP_KEYWORD,
124 IPreferenceConstants.PHP_FUNCTIONNAME,
125 IPreferenceConstants.PHP_VARIABLE,
126 IPreferenceConstants.PHP_STRING,
127 IPreferenceConstants.PHP_TYPE,
128 IPreferenceConstants.PHP_CONSTANT,
129 IPreferenceConstants.PHP_DEFAULT };
131 * Creates a PHP code scanner
133 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
134 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
135 super(manager, store);
137 // // final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
138 // Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
141 // new TextAttribute(
142 // provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
144 // (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
145 // + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
147 // new Token(new TextAttribute(
148 // provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
151 // (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
153 // new Token(new TextAttribute(
154 // provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
157 // (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
159 // new Token(new TextAttribute(
160 // provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
163 // (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
164 // + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
166 // new Token(new TextAttribute(
167 // provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
170 // (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
172 // new Token(new TextAttribute(
173 // provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
176 // (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE ) + (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
178 // new Token(new TextAttribute(
179 // provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)),
182 // (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE )
183 // + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
185 // new Token(new TextAttribute(
186 // provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)),
189 // (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
190 // + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
192 // new Token(new TextAttribute(
193 // provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
196 // (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
197 // updateWordRules();
200 // public void updateToken(JavaColorManager provider) {
201 // final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
203 // Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
206 // new TextAttribute(
207 // provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
209 // (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
210 // + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
212 // new TextAttribute(
213 // provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
215 // (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE)
216 // + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
218 // new TextAttribute(
219 // provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
221 // (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
222 // functionName.setData(
223 // new TextAttribute(
224 // provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
226 // (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
227 // + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
229 // new TextAttribute(
230 // provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
232 // (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE)
233 // + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
235 // new TextAttribute(
236 // provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
238 // (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
240 // new TextAttribute(
241 // provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)),
243 // (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
244 // + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
245 // multi_comment.setData(
246 // new TextAttribute(
247 // provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)),
249 // (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
250 // + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
252 // new TextAttribute(
253 // provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
255 // (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE)
256 // + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
259 // public void updateWordRules() {
262 * @see AbstractJavaScanner#getTokenProperties()
264 protected String[] getTokenProperties() {
265 return fgTokenProperties;
268 * @see AbstractJavaScanner#createRules()
270 protected List createRules() {
271 List rules = new ArrayList();
272 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
273 // Add rule for single line comments.
274 rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
275 rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
276 // Add rule for strings and character constants.
277 token = getToken(IPreferenceConstants.PHP_STRING);
278 rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
279 rules.add(new MultiLineRule("`", "`", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
280 rules.add(new MultiLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
283 //rules.add(new SingleLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
285 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
286 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
287 // Add generic whitespace rule.
288 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
289 // Add word rule for keywords, types, and constants.
290 token = getToken(IPreferenceConstants.PHP_DEFAULT);
291 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
293 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
294 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
295 Token type = getToken(IPreferenceConstants.PHP_TYPE);
296 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
298 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
299 // String strbuffer = null; unused
300 PHPElement elbuffer = null;
301 for (int i = 0; i < buffer.size(); i++) {
302 // while ((buffer != null)
303 // && (!buffer.isEmpty()
304 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
305 elbuffer = (PHPElement) buffer.get(i);
306 if (elbuffer instanceof PHPKeyword)
307 wordRule.addWord(((PHPKeyword) elbuffer).getName(), keyword);
308 if (elbuffer instanceof PHPFunction)
309 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
310 if (elbuffer instanceof PHPType)
311 wordRule.addWord(elbuffer.getName(), type);
312 if (elbuffer instanceof PHPConstant)
313 wordRule.addWord(elbuffer.getName(), constant);
316 // IRule[] result = new IRule[rules.size()];unused
317 // rules.toArray(result);