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.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.Token;
32 import org.eclipse.jface.text.rules.WhitespaceRule;
33 import org.eclipse.jface.text.rules.WordRule;
38 public class PHPCodeScanner extends AbstractJavaScanner {
41 * Rule to detect java operators.
45 protected class OperatorRule implements IRule {
48 private final char[] JAVA_OPERATORS= { ';', '(', ')', '{', '}', '.', '=', '/', '\\', '+', '-', '*', '[', ']', '<', '>', ':', '?', '!', ',', '|', '&', '^', '%', '~', '@'};
49 /** Token to return for this rule */
50 private final IToken fToken;
53 * Creates a new operator rule.
56 * Token to use for this rule
58 public OperatorRule(IToken token) {
63 * Is this character an operator character?
66 * Character to determine whether it is an operator character
67 * @return <code>true</code> iff the character is an operator,
68 * <code>false</code> otherwise.
70 public boolean isOperator(char character) {
71 for (int index= 0; index < JAVA_OPERATORS.length; index++) {
72 if (JAVA_OPERATORS[index] == character)
79 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
81 public IToken evaluate(ICharacterScanner scanner) {
83 int character= scanner.read();
84 if (isOperator((char) character)) {
85 int lastCharacter = character;
86 character= scanner.read();
87 if (!isOperator((char) character)) {
91 if (lastCharacter=='<' && character=='?') {
94 return Token.UNDEFINED;
96 if (lastCharacter=='?' && character=='>') {
99 return Token.UNDEFINED;
102 character= scanner.read();
103 } while (isOperator((char) character));
108 return Token.UNDEFINED;
113 private class PHPWordRule extends WordRule {
114 private StringBuffer fBuffer = new StringBuffer();
116 public PHPWordRule(IWordDetector detector) {
117 super(detector, Token.UNDEFINED);
120 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
121 super(detector, defaultToken);
124 public IToken evaluate(ICharacterScanner scanner) {
125 int c = scanner.read();
126 boolean isVariable = false;
132 return Token.UNDEFINED;
135 if (c != 'p' && c != 'P') {
137 return getToken(IPreferenceConstants.PHP_TAG);
140 if (c != 'h' && c != 'H') {
143 return getToken(IPreferenceConstants.PHP_TAG);
146 if (c != 'p' && c != 'P') {
150 return getToken(IPreferenceConstants.PHP_TAG);
152 return getToken(IPreferenceConstants.PHP_TAG);
161 return getToken(IPreferenceConstants.PHP_TAG);
165 return Token.UNDEFINED;
167 if (fDetector.isWordStart((char) c)) {
171 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
173 fBuffer.setLength(0);
175 fBuffer.append((char) c);
177 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
181 return getToken(IPreferenceConstants.PHP_VARIABLE);
183 IToken token = (IToken) fWords.get(fBuffer.toString());
187 if (fDefaultToken.isUndefined())
188 unreadBuffer(scanner);
190 return fDefaultToken;
195 return Token.UNDEFINED;
199 //private PHPColorProvider fColorProvider;
201 private static String[] fgTokenProperties =
203 IPreferenceConstants.PHP_MULTILINE_COMMENT,
204 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
205 IPreferenceConstants.PHP_TAG,
206 IPreferenceConstants.PHP_KEYWORD,
207 IPreferenceConstants.PHP_FUNCTIONNAME,
208 IPreferenceConstants.PHP_VARIABLE,
209 IPreferenceConstants.PHP_STRING,
210 IPreferenceConstants.PHP_TYPE,
211 IPreferenceConstants.PHP_CONSTANT,
212 IPreferenceConstants.PHP_DEFAULT,
213 IPreferenceConstants.PHP_OPERATOR,
214 IPreferenceConstants.PHP_KEYWORD_RETURN};
216 * Creates a PHP code scanner
218 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
219 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
220 super(manager, store);
222 // // final IPreferenceStore store =
223 // PHPeclipsePlugin.getDefault().getPreferenceStore();
224 // Color BackgroundColor =
225 // provider.getColor(PreferenceConverter.getColor(store,
226 // PHP_EDITOR_BACKGROUND));
229 // new TextAttribute(
230 // provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
232 // (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
233 // + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
235 // new Token(new TextAttribute(
236 // provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
239 // (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE) +
240 // (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
242 // new Token(new TextAttribute(
243 // provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
246 // (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) +
247 // (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
249 // new Token(new TextAttribute(
250 // provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
253 // (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
254 // + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
256 // new Token(new TextAttribute(
257 // provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
260 // (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE) +
261 // (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
263 // new Token(new TextAttribute(
264 // provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
267 // (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE ) +
268 // (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
270 // new Token(new TextAttribute(
271 // provider.getColor(PreferenceConverter.getColor(store,
272 // PHP_SINGLELINE_COMMENT)),
275 // (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE )
276 // + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC :
279 // new Token(new TextAttribute(
280 // provider.getColor(PreferenceConverter.getColor(store,
281 // PHP_MULTILINE_COMMENT)),
284 // (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
285 // + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC :
288 // new Token(new TextAttribute(
289 // provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
292 // (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE) +
293 // (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
294 // updateWordRules();
298 * @see AbstractJavaScanner#getTokenProperties()
300 protected String[] getTokenProperties() {
301 return fgTokenProperties;
303 // public void updateToken(JavaColorManager provider) {
304 // final IPreferenceStore store =
305 // PHPeclipsePlugin.getDefault().getPreferenceStore();
307 // Color BackgroundColor =
308 // provider.getColor(PreferenceConverter.getColor(store,
309 // PHP_EDITOR_BACKGROUND));
312 // new TextAttribute(
313 // provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
315 // (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
316 // + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
318 // new TextAttribute(
319 // provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
321 // (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE)
322 // + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
324 // new TextAttribute(
325 // provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
327 // (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) +
328 // (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
329 // functionName.setData(
330 // new TextAttribute(
331 // provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
333 // (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
334 // + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
336 // new TextAttribute(
337 // provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
339 // (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE)
340 // + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
342 // new TextAttribute(
343 // provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
345 // (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE) +
346 // (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
348 // new TextAttribute(
349 // provider.getColor(PreferenceConverter.getColor(store,
350 // PHP_SINGLELINE_COMMENT)),
352 // (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
353 // + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC :
355 // multi_comment.setData(
356 // new TextAttribute(
357 // provider.getColor(PreferenceConverter.getColor(store,
358 // PHP_MULTILINE_COMMENT)),
360 // (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
361 // + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC :
364 // new TextAttribute(
365 // provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
367 // (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE)
368 // + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
371 // public void updateWordRules() {
375 * @see AbstractJavaScanner#createRules()
377 protected List createRules() {
378 List rules = new ArrayList();
379 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
380 // Add rule for single line comments.
381 rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
382 rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
383 // Add rule for strings and character constants.
384 token = getToken(IPreferenceConstants.PHP_STRING);
385 rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
386 rules.add(new MultiLineRule("`", "`", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
387 rules.add(new MultiLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
389 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
390 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
391 // Add generic whitespace rule.
392 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
393 // Add word rule for keywords, types, and constants.
394 token = getToken(IPreferenceConstants.PHP_DEFAULT);
395 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
397 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
398 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
399 Token type = getToken(IPreferenceConstants.PHP_TYPE);
400 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
402 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
403 // String strbuffer = null; unused
404 PHPElement elbuffer = null;
406 for (int i = 0; i < buffer.size(); i++) {
407 // while ((buffer != null)
408 // && (!buffer.isEmpty()
409 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
410 elbuffer = (PHPElement) buffer.get(i);
411 if (elbuffer instanceof PHPKeyword) {
412 name = ((PHPKeyword) elbuffer).getName();
413 if (!name.equals("return")) {
414 wordRule.addWord(name, keyword);
416 } else if (elbuffer instanceof PHPFunction) {
417 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
418 } else if (elbuffer instanceof PHPType) {
419 wordRule.addWord(elbuffer.getName(), type);
420 } else if (elbuffer instanceof PHPConstant) {
421 wordRule.addWord(elbuffer.getName(), constant);
425 // Add word rule for keyword 'return'.
426 token= getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
427 wordRule.addWord("return", token);
429 // Add rule for operators and brackets (at the end !)
430 token= getToken(IPreferenceConstants.PHP_OPERATOR);
431 rules.add(new OperatorRule(token));
435 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));