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.phpdt.internal.ui.text.AbstractJavaScanner;
19 import net.sourceforge.phpdt.internal.ui.text.JavaColorManager;
20 import net.sourceforge.phpdt.ui.text.IColorManager;
21 import net.sourceforge.phpdt.ui.text.IJavaColorConstants;
22 import net.sourceforge.phpeclipse.IPreferenceConstants;
23 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
25 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
26 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
28 import org.eclipse.jface.preference.IPreferenceStore;
29 import org.eclipse.jface.preference.PreferenceConverter;
30 import org.eclipse.jface.text.TextAttribute;
31 import org.eclipse.jface.text.rules.EndOfLineRule;
32 import org.eclipse.jface.text.rules.ICharacterScanner;
33 import org.eclipse.jface.text.rules.IRule;
34 import org.eclipse.jface.text.rules.IToken;
35 import org.eclipse.jface.text.rules.IWordDetector;
36 import org.eclipse.jface.text.rules.MultiLineRule;
37 import org.eclipse.jface.text.rules.SingleLineRule;
38 import org.eclipse.jface.text.rules.Token;
39 import org.eclipse.jface.text.rules.WhitespaceRule;
40 import org.eclipse.jface.text.rules.WordRule;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.graphics.Color;
47 public class PHPCodeScanner
48 extends AbstractJavaScanner {
50 // private static Token variable;
51 // private static Token keyword;
52 // private static Token type;
53 // private static Token constant;
54 // private static Token functionName;
55 // private static Token string;
56 // private static Token comment;
57 // private static Token multi_comment;
58 // private static Token other;
60 private class PHPWordRule extends WordRule {
61 private StringBuffer fBuffer = new StringBuffer();
63 public PHPWordRule(IWordDetector detector) {
64 super(detector, Token.UNDEFINED);
67 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
68 super(detector, defaultToken);
71 public IToken evaluate(ICharacterScanner scanner) {
72 int c = scanner.read();
73 boolean isVariable = false;
74 if (fDetector.isWordStart((char) c)) {
78 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
82 fBuffer.append((char) c);
85 c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
89 return getToken(IJavaColorConstants.PHP_VARIABLE);
91 IToken token = (IToken) fWords.get(fBuffer.toString());
95 if (fDefaultToken.isUndefined())
96 unreadBuffer(scanner);
103 return Token.UNDEFINED;
107 //private PHPColorProvider fColorProvider;
109 private static String[] fgTokenProperties = {
110 IJavaColorConstants.PHP_MULTI_LINE_COMMENT,
111 IJavaColorConstants.PHP_SINGLE_LINE_COMMENT,
112 IJavaColorConstants.PHP_KEYWORD,
113 IJavaColorConstants.PHP_FUNCTIONNAME,
114 IJavaColorConstants.PHP_VARIABLE,
115 IJavaColorConstants.PHP_STRING,
116 IJavaColorConstants.PHP_TYPE,
117 IJavaColorConstants.PHP_CONSTANT,
118 IJavaColorConstants.PHP_DEFAULT };
120 * Creates a PHP code scanner
122 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
123 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
124 super(manager, store);
126 // // final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
127 // Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
130 // new TextAttribute(
131 // provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
133 // (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
134 // + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
136 // new Token(new TextAttribute(
137 // provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
140 // (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
142 // new Token(new TextAttribute(
143 // provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
146 // (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
148 // new Token(new TextAttribute(
149 // provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
152 // (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
153 // + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
155 // new Token(new TextAttribute(
156 // provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
159 // (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
161 // new Token(new TextAttribute(
162 // provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
165 // (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE ) + (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
167 // new Token(new TextAttribute(
168 // provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)),
171 // (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE )
172 // + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
174 // new Token(new TextAttribute(
175 // provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)),
178 // (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
179 // + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
181 // new Token(new TextAttribute(
182 // provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
185 // (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
186 // updateWordRules();
189 // public void updateToken(JavaColorManager provider) {
190 // final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
192 // Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
195 // new TextAttribute(
196 // provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
198 // (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
199 // + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
201 // new TextAttribute(
202 // provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
204 // (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE)
205 // + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
207 // new TextAttribute(
208 // provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
210 // (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
211 // functionName.setData(
212 // new TextAttribute(
213 // provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
215 // (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
216 // + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
218 // new TextAttribute(
219 // provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
221 // (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE)
222 // + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
224 // new TextAttribute(
225 // provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
227 // (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
229 // new TextAttribute(
230 // provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)),
232 // (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
233 // + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
234 // multi_comment.setData(
235 // new TextAttribute(
236 // provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)),
238 // (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
239 // + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
241 // new TextAttribute(
242 // provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
244 // (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE)
245 // + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
248 // public void updateWordRules() {
251 * @see AbstractJavaScanner#getTokenProperties()
253 protected String[] getTokenProperties() {
254 return fgTokenProperties;
257 * @see AbstractJavaScanner#createRules()
259 protected List createRules() {
260 List rules = new ArrayList();
261 Token token= getToken(IJavaColorConstants.PHP_SINGLE_LINE_COMMENT);
262 // Add rule for single line comments.
263 rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
264 rules.add(new EndOfLineRule("#", token));
265 // Add rule for strings and character constants.
266 token= getToken(IJavaColorConstants.PHP_STRING);
267 rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
268 rules.add(new SingleLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
269 token= getToken(IJavaColorConstants.PHP_MULTI_LINE_COMMENT);
270 rules.add(new MultiLineRule("/*", "*/", token));
271 // Add generic whitespace rule.
272 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
273 // Add word rule for keywords, types, and constants.
274 token= getToken(IJavaColorConstants.PHP_DEFAULT);
275 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
277 Token keyword= getToken(IJavaColorConstants.PHP_KEYWORD);
278 Token functionName= getToken(IJavaColorConstants.PHP_FUNCTIONNAME);
279 Token type= getToken(IJavaColorConstants.PHP_TYPE);
280 Token constant= getToken(IJavaColorConstants.PHP_CONSTANT);
281 PHPSyntaxRdr.readInSyntax();
282 Vector buffer = PHPSyntaxRdr.getsyntaxdata();
283 String strbuffer = null;
284 PHPElement elbuffer = null;
285 while ((buffer != null)
286 && (!buffer.isEmpty()
287 && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
288 if (elbuffer instanceof PHPKeyword)
289 wordRule.addWord(((PHPKeyword) elbuffer).getName(), keyword);
290 if (elbuffer instanceof PHPFunction)
291 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
292 if (elbuffer instanceof PHPType)
293 wordRule.addWord(elbuffer.getName(), type);
294 if (elbuffer instanceof PHPConstant)
295 wordRule.addWord(elbuffer.getName(), constant);
298 IRule[] result = new IRule[rules.size()];
299 // rules.toArray(result);