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;
16 import java.util.Vector;
18 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
19 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.preference.PreferenceConverter;
23 import org.eclipse.jface.text.TextAttribute;
24 import org.eclipse.jface.text.rules.EndOfLineRule;
25 import org.eclipse.jface.text.rules.ICharacterScanner;
26 import org.eclipse.jface.text.rules.IRule;
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.RuleBasedScanner;
31 import org.eclipse.jface.text.rules.SingleLineRule;
32 import org.eclipse.jface.text.rules.Token;
33 import org.eclipse.jface.text.rules.WhitespaceRule;
34 import org.eclipse.jface.text.rules.WordRule;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.graphics.Color;
38 import net.sourceforge.phpeclipse.IPreferenceConstants;
39 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
40 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
41 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
46 public class PHPCodeScanner extends RuleBasedScanner implements IPreferenceConstants {
48 private static Token variable;
49 private static Token keyword;
50 private static Token type;
51 private static Token constant;
52 private static Token functionName;
53 private static Token string;
54 private static Token comment;
55 private static Token multi_comment;
56 private static Token other;
58 private class PHPWordRule extends WordRule {
59 private StringBuffer fBuffer = new StringBuffer();
61 public PHPWordRule(IWordDetector detector) {
62 super(detector, Token.UNDEFINED);
65 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
66 super(detector, defaultToken);
69 public IToken evaluate(ICharacterScanner scanner) {
70 int c = scanner.read();
71 boolean isVariable = false;
72 if (fDetector.isWordStart((char) c)) {
76 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
80 fBuffer.append((char) c);
82 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
88 IToken token = (IToken) fWords.get(fBuffer.toString());
92 if (fDefaultToken.isUndefined())
93 unreadBuffer(scanner);
100 return Token.UNDEFINED;
104 private PHPColorProvider fColorProvider;
107 * Creates a PHP code scanner
109 public PHPCodeScanner(PHPColorProvider provider) {
110 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
111 Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
115 provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
117 (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
118 + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
120 new Token(new TextAttribute(
121 provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
124 (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
126 new Token(new TextAttribute(
127 provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
130 (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
132 new Token(new TextAttribute(
133 provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
136 (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
137 + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
139 new Token(new TextAttribute(
140 provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
143 (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
145 new Token(new TextAttribute(
146 provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
149 (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE ) + (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
151 new Token(new TextAttribute(
152 provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)),
155 (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE )
156 + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
158 new Token(new TextAttribute(
159 provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)),
162 (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
163 + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
165 new Token(new TextAttribute(
166 provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
169 (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
173 public void updateToken(PHPColorProvider provider) {
174 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
176 Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
180 provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
182 (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
183 + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
186 provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
188 (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE)
189 + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
192 provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
194 (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
195 functionName.setData(
197 provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
199 (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
200 + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
203 provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
205 (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE)
206 + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
209 provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
211 (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
214 provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)),
216 (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
217 + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
218 multi_comment.setData(
220 provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)),
222 (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
223 + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
226 provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
228 (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE)
229 + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
232 public void updateWordRules() {
233 List rules = new ArrayList();
234 // Add rule for single line comments.
235 rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$
236 rules.add(new EndOfLineRule("#", comment));
237 // Add rule for strings and character constants.
238 rules.add(new MultiLineRule("\"", "\"", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
239 rules.add(new SingleLineRule("'", "'", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
240 // rules.add(new SingleLineRule("//", "//", php_comment));
241 rules.add(new MultiLineRule("/*", "*/", multi_comment));
242 // Add generic whitespace rule.
243 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
244 // Add word rule for keywords, types, and constants.
245 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), other);
247 PHPSyntaxRdr.readInSyntax();
248 Vector buffer = PHPSyntaxRdr.getsyntaxdata();
249 String strbuffer = null;
250 PHPElement elbuffer = null;
251 while ((buffer != null) && (!buffer.isEmpty() && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
252 if (elbuffer instanceof PHPKeyword)
253 wordRule.addWord(((PHPKeyword) elbuffer).getName(), keyword);
254 if (elbuffer instanceof PHPFunction)
255 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
256 if (elbuffer instanceof PHPType)
257 wordRule.addWord(elbuffer.getName(), type);
258 if (elbuffer instanceof PHPConstant)
259 wordRule.addWord(elbuffer.getName(), constant);
262 IRule[] result = new IRule[rules.size()];
263 rules.toArray(result);