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)) {
86 int lastCharacter = character;
87 character= scanner.read();
88 if (!isOperator((char) character)) {
92 if (lastCharacter=='<' && character=='?') {
95 return Token.UNDEFINED;
97 if (lastCharacter=='?' && character=='>') {
100 return Token.UNDEFINED;
103 character= scanner.read();
104 } while (isOperator((char) character));
109 return Token.UNDEFINED;
114 private class PHPWordRule extends WordRule {
115 private StringBuffer fBuffer = new StringBuffer();
117 public PHPWordRule(IWordDetector detector) {
118 super(detector, Token.UNDEFINED);
121 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
122 super(detector, defaultToken);
125 public IToken evaluate(ICharacterScanner scanner) {
126 int c = scanner.read();
127 boolean isVariable = false;
133 return Token.UNDEFINED;
136 if (c != 'p' && c != 'P') {
138 return getToken(IPreferenceConstants.PHP_TAG);
141 if (c != 'h' && c != 'H') {
144 return getToken(IPreferenceConstants.PHP_TAG);
147 if (c != 'p' && c != 'P') {
151 return getToken(IPreferenceConstants.PHP_TAG);
153 return getToken(IPreferenceConstants.PHP_TAG);
162 return getToken(IPreferenceConstants.PHP_TAG);
166 return Token.UNDEFINED;
168 if (fDetector.isWordStart((char) c)) {
172 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
174 fBuffer.setLength(0);
176 fBuffer.append((char) c);
178 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
182 return getToken(IPreferenceConstants.PHP_VARIABLE);
184 IToken token = (IToken) fWords.get(fBuffer.toString());
188 if (fDefaultToken.isUndefined())
189 unreadBuffer(scanner);
191 return fDefaultToken;
196 return Token.UNDEFINED;
200 //private PHPColorProvider fColorProvider;
202 private static String[] fgTokenProperties =
204 IPreferenceConstants.PHP_MULTILINE_COMMENT,
205 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
206 IPreferenceConstants.PHP_TAG,
207 IPreferenceConstants.PHP_KEYWORD,
208 IPreferenceConstants.PHP_FUNCTIONNAME,
209 IPreferenceConstants.PHP_VARIABLE,
210 IPreferenceConstants.PHP_STRING,
211 IPreferenceConstants.PHP_TYPE,
212 IPreferenceConstants.PHP_CONSTANT,
213 IPreferenceConstants.PHP_DEFAULT,
214 IPreferenceConstants.PHP_OPERATOR,
215 IPreferenceConstants.PHP_KEYWORD_RETURN};
217 * Creates a PHP code scanner
219 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
220 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
221 super(manager, store);
223 // // final IPreferenceStore store =
224 // PHPeclipsePlugin.getDefault().getPreferenceStore();
225 // Color BackgroundColor =
226 // provider.getColor(PreferenceConverter.getColor(store,
227 // PHP_EDITOR_BACKGROUND));
230 // new TextAttribute(
231 // provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
233 // (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
234 // + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
236 // new Token(new TextAttribute(
237 // provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
240 // (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE) +
241 // (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
243 // new Token(new TextAttribute(
244 // provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
247 // (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) +
248 // (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
250 // new Token(new TextAttribute(
251 // provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
254 // (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
255 // + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
257 // new Token(new TextAttribute(
258 // provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
261 // (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE) +
262 // (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
264 // new Token(new TextAttribute(
265 // provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
268 // (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE ) +
269 // (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
271 // new Token(new TextAttribute(
272 // provider.getColor(PreferenceConverter.getColor(store,
273 // PHP_SINGLELINE_COMMENT)),
276 // (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE )
277 // + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC :
280 // new Token(new TextAttribute(
281 // provider.getColor(PreferenceConverter.getColor(store,
282 // PHP_MULTILINE_COMMENT)),
285 // (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
286 // + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC :
289 // new Token(new TextAttribute(
290 // provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
293 // (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE) +
294 // (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
295 // updateWordRules();
299 * @see AbstractJavaScanner#getTokenProperties()
301 protected String[] getTokenProperties() {
302 return fgTokenProperties;
304 // public void updateToken(JavaColorManager provider) {
305 // final IPreferenceStore store =
306 // PHPeclipsePlugin.getDefault().getPreferenceStore();
308 // Color BackgroundColor =
309 // provider.getColor(PreferenceConverter.getColor(store,
310 // PHP_EDITOR_BACKGROUND));
313 // new TextAttribute(
314 // provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
316 // (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
317 // + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
319 // new TextAttribute(
320 // provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
322 // (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE)
323 // + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
325 // new TextAttribute(
326 // provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
328 // (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) +
329 // (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
330 // functionName.setData(
331 // new TextAttribute(
332 // provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
334 // (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
335 // + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
337 // new TextAttribute(
338 // provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
340 // (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE)
341 // + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
343 // new TextAttribute(
344 // provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
346 // (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE) +
347 // (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
349 // new TextAttribute(
350 // provider.getColor(PreferenceConverter.getColor(store,
351 // PHP_SINGLELINE_COMMENT)),
353 // (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
354 // + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC :
356 // multi_comment.setData(
357 // new TextAttribute(
358 // provider.getColor(PreferenceConverter.getColor(store,
359 // PHP_MULTILINE_COMMENT)),
361 // (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
362 // + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC :
365 // new TextAttribute(
366 // provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
368 // (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE)
369 // + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
372 // public void updateWordRules() {
376 * @see AbstractJavaScanner#createRules()
378 protected List createRules() {
379 List rules = new ArrayList();
380 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
381 // Add rule for single line comments.
382 rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
383 rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
384 // Add rule for strings and character constants.
385 token = getToken(IPreferenceConstants.PHP_STRING);
386 rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
387 rules.add(new MultiLineRule("`", "`", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
388 rules.add(new MultiLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
390 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
391 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
392 // Add generic whitespace rule.
393 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
394 // Add word rule for keywords, types, and constants.
395 token = getToken(IPreferenceConstants.PHP_DEFAULT);
396 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
398 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
399 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
400 Token type = getToken(IPreferenceConstants.PHP_TYPE);
401 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
403 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
404 // String strbuffer = null; unused
405 PHPElement elbuffer = null;
407 for (int i = 0; i < buffer.size(); i++) {
408 // while ((buffer != null)
409 // && (!buffer.isEmpty()
410 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
411 elbuffer = (PHPElement) buffer.get(i);
412 if (elbuffer instanceof PHPKeyword) {
413 name = ((PHPKeyword) elbuffer).getName();
414 if (!name.equals("return")) {
415 wordRule.addWord(name, keyword);
417 } else if (elbuffer instanceof PHPFunction) {
418 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
419 } else if (elbuffer instanceof PHPType) {
420 wordRule.addWord(elbuffer.getName(), type);
421 } else if (elbuffer instanceof PHPConstant) {
422 wordRule.addWord(elbuffer.getName(), constant);
426 // Add word rule for keyword 'return'.
427 token= getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
428 wordRule.addWord("return", token);
430 // Add rule for operators and brackets (at the end !)
431 token= getToken(IPreferenceConstants.PHP_OPERATOR);
432 rules.add(new OperatorRule(token));
436 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));