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.internal.ui.text.CombinedWordRule;
19 import net.sourceforge.phpdt.ui.text.IColorManager;
20 import net.sourceforge.phpeclipse.IPreferenceConstants;
21 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
22 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
23 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.text.rules.EndOfLineRule;
27 import org.eclipse.jface.text.rules.ICharacterScanner;
28 import org.eclipse.jface.text.rules.IRule;
29 import org.eclipse.jface.text.rules.IToken;
30 import org.eclipse.jface.text.rules.IWordDetector;
31 import org.eclipse.jface.text.rules.MultiLineRule;
32 import org.eclipse.jface.text.rules.Token;
33 import org.eclipse.jface.text.rules.WhitespaceRule;
34 import org.eclipse.jface.text.rules.WordRule;
39 public class PHPCodeScanner extends AbstractJavaScanner {
42 * Rule to detect java operators.
46 protected class OperatorRule implements IRule {
49 private final char[] JAVA_OPERATORS= { ';', '(', ')', '{', '}', '.', '=', '/', '\\', '+', '-', '*', '[', ']', '<', '>', ':', '?', '!', ',', '|', '&', '^', '%', '~', '@'};
50 /** Token to return for this rule */
51 private final IToken fToken;
54 * Creates a new operator rule.
57 * Token to use for this rule
59 public OperatorRule(IToken token) {
64 * Is this character an operator character?
67 * Character to determine whether it is an operator character
68 * @return <code>true</code> iff the character is an operator,
69 * <code>false</code> otherwise.
71 public boolean isOperator(char character) {
72 for (int index= 0; index < JAVA_OPERATORS.length; index++) {
73 if (JAVA_OPERATORS[index] == character)
80 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
82 public IToken evaluate(ICharacterScanner scanner) {
84 int character= scanner.read();
85 if (isOperator((char) character)) {
87 character= scanner.read();
88 } while (isOperator((char) character));
93 return Token.UNDEFINED;
98 private class PHPWordRule extends WordRule {
99 private StringBuffer fBuffer = new StringBuffer();
101 public PHPWordRule(IWordDetector detector) {
102 super(detector, Token.UNDEFINED);
105 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
106 super(detector, defaultToken);
109 public IToken evaluate(ICharacterScanner scanner) {
110 int c = scanner.read();
111 boolean isVariable = false;
129 return getToken(IPreferenceConstants.PHP_TAG);
139 return getToken(IPreferenceConstants.PHP_TAG);
143 if (fDetector.isWordStart((char) c)) {
147 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
149 fBuffer.setLength(0);
151 fBuffer.append((char) c);
153 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
157 return getToken(IPreferenceConstants.PHP_VARIABLE);
159 IToken token = (IToken) fWords.get(fBuffer.toString());
163 if (fDefaultToken.isUndefined())
164 unreadBuffer(scanner);
166 return fDefaultToken;
171 return Token.UNDEFINED;
175 //private PHPColorProvider fColorProvider;
177 private static String[] fgTokenProperties =
179 IPreferenceConstants.PHP_MULTILINE_COMMENT,
180 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
181 IPreferenceConstants.PHP_TAG,
182 IPreferenceConstants.PHP_KEYWORD,
183 IPreferenceConstants.PHP_FUNCTIONNAME,
184 IPreferenceConstants.PHP_VARIABLE,
185 IPreferenceConstants.PHP_STRING,
186 IPreferenceConstants.PHP_TYPE,
187 IPreferenceConstants.PHP_CONSTANT,
188 IPreferenceConstants.PHP_DEFAULT,
189 IPreferenceConstants.PHP_OPERATOR,
190 IPreferenceConstants.PHP_KEYWORD_RETURN};
192 * Creates a PHP code scanner
194 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
195 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
196 super(manager, store);
198 // // final IPreferenceStore store =
199 // PHPeclipsePlugin.getDefault().getPreferenceStore();
200 // Color BackgroundColor =
201 // provider.getColor(PreferenceConverter.getColor(store,
202 // PHP_EDITOR_BACKGROUND));
205 // new TextAttribute(
206 // provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
208 // (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
209 // + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
211 // new Token(new TextAttribute(
212 // 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 Token(new TextAttribute(
219 // provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
222 // (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) +
223 // (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
225 // new Token(new TextAttribute(
226 // provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
229 // (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
230 // + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
232 // new Token(new TextAttribute(
233 // provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
236 // (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE) +
237 // (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
239 // new Token(new TextAttribute(
240 // provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
243 // (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE ) +
244 // (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
246 // new Token(new TextAttribute(
247 // provider.getColor(PreferenceConverter.getColor(store,
248 // PHP_SINGLELINE_COMMENT)),
251 // (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE )
252 // + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC :
255 // new Token(new TextAttribute(
256 // provider.getColor(PreferenceConverter.getColor(store,
257 // PHP_MULTILINE_COMMENT)),
260 // (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
261 // + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC :
264 // new Token(new TextAttribute(
265 // provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
268 // (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE) +
269 // (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
270 // updateWordRules();
274 * @see AbstractJavaScanner#getTokenProperties()
276 protected String[] getTokenProperties() {
277 return fgTokenProperties;
279 // public void updateToken(JavaColorManager provider) {
280 // final IPreferenceStore store =
281 // PHPeclipsePlugin.getDefault().getPreferenceStore();
283 // Color BackgroundColor =
284 // provider.getColor(PreferenceConverter.getColor(store,
285 // PHP_EDITOR_BACKGROUND));
288 // new TextAttribute(
289 // provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
291 // (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
292 // + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
294 // new TextAttribute(
295 // provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
297 // (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE)
298 // + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
300 // new TextAttribute(
301 // provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
303 // (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) +
304 // (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
305 // functionName.setData(
306 // new TextAttribute(
307 // provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
309 // (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
310 // + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
312 // new TextAttribute(
313 // provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
315 // (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE)
316 // + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
318 // new TextAttribute(
319 // provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
321 // (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE) +
322 // (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
324 // new TextAttribute(
325 // provider.getColor(PreferenceConverter.getColor(store,
326 // PHP_SINGLELINE_COMMENT)),
328 // (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
329 // + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC :
331 // multi_comment.setData(
332 // new TextAttribute(
333 // provider.getColor(PreferenceConverter.getColor(store,
334 // PHP_MULTILINE_COMMENT)),
336 // (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
337 // + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC :
340 // new TextAttribute(
341 // provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
343 // (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE)
344 // + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
347 // public void updateWordRules() {
351 * @see AbstractJavaScanner#createRules()
353 protected List createRules() {
354 List rules = new ArrayList();
355 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
356 // Add rule for single line comments.
357 rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
358 rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
359 // Add rule for strings and character constants.
360 token = getToken(IPreferenceConstants.PHP_STRING);
361 rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
362 rules.add(new MultiLineRule("`", "`", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
363 rules.add(new MultiLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
365 // Add rule for operators and brackets
366 token= getToken(IPreferenceConstants.PHP_OPERATOR);
367 rules.add(new OperatorRule(token));
369 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
370 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
371 // Add generic whitespace rule.
372 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
373 // Add word rule for keywords, types, and constants.
374 token = getToken(IPreferenceConstants.PHP_DEFAULT);
375 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
376 CombinedWordRule combinedWordRule= new CombinedWordRule(new PHPWordDetector(), token);
378 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
379 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
380 Token type = getToken(IPreferenceConstants.PHP_TYPE);
381 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
383 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
384 // String strbuffer = null; unused
385 PHPElement elbuffer = null;
387 for (int i = 0; i < buffer.size(); i++) {
388 // while ((buffer != null)
389 // && (!buffer.isEmpty()
390 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
391 elbuffer = (PHPElement) buffer.get(i);
392 if (elbuffer instanceof PHPKeyword) {
393 name = ((PHPKeyword) elbuffer).getName();
394 // if (!name.equals("return")) {
395 wordRule.addWord(name, keyword);
397 } else if (elbuffer instanceof PHPFunction) {
398 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
399 } else if (elbuffer instanceof PHPType) {
400 wordRule.addWord(elbuffer.getName(), type);
401 } else if (elbuffer instanceof PHPConstant) {
402 wordRule.addWord(elbuffer.getName(), constant);
407 // Add word rule for keyword 'return'.
408 // CombinedWordRule.WordMatcher returnWordRule= new CombinedWordRule.WordMatcher();
409 // token= getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
410 // returnWordRule.addWord("return", token); //$NON-NLS-1$
411 // combinedWordRule.addWordMatcher(returnWordRule);
412 // rules.add(combinedWordRule);