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.ui.text.IColorManager;
20 import net.sourceforge.phpdt.ui.text.IJavaColorConstants;
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.*;
31 public class PHPCodeScanner
32 extends AbstractJavaScanner {
34 // private static Token variable;
35 // private static Token keyword;
36 // private static Token type;
37 // private static Token constant;
38 // private static Token functionName;
39 // private static Token string;
40 // private static Token comment;
41 // private static Token multi_comment;
42 // private static Token other;
44 private class PHPWordRule extends WordRule {
45 private StringBuffer fBuffer = new StringBuffer();
47 public PHPWordRule(IWordDetector detector) {
48 super(detector, Token.UNDEFINED);
51 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
52 super(detector, defaultToken);
55 public IToken evaluate(ICharacterScanner scanner) {
56 int c = scanner.read();
57 boolean isVariable = false;
58 if (fDetector.isWordStart((char) c)) {
62 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
66 fBuffer.append((char) c);
69 c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
73 return getToken(IJavaColorConstants.PHP_VARIABLE);
75 IToken token = (IToken) fWords.get(fBuffer.toString());
79 if (fDefaultToken.isUndefined())
80 unreadBuffer(scanner);
87 return Token.UNDEFINED;
91 //private PHPColorProvider fColorProvider;
93 private static String[] fgTokenProperties = {
94 IJavaColorConstants.PHP_MULTI_LINE_COMMENT,
95 IJavaColorConstants.PHP_SINGLE_LINE_COMMENT,
96 IJavaColorConstants.PHP_KEYWORD,
97 IJavaColorConstants.PHP_FUNCTIONNAME,
98 IJavaColorConstants.PHP_VARIABLE,
99 IJavaColorConstants.PHP_STRING,
100 IJavaColorConstants.PHP_TYPE,
101 IJavaColorConstants.PHP_CONSTANT,
102 IJavaColorConstants.PHP_DEFAULT };
104 * Creates a PHP code scanner
106 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
107 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
108 super(manager, store);
110 // // final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
111 // Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
114 // new TextAttribute(
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)));
170 // updateWordRules();
173 // public void updateToken(JavaColorManager provider) {
174 // final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
176 // Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
179 // new TextAttribute(
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)));
185 // new TextAttribute(
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)));
191 // new TextAttribute(
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(
196 // new TextAttribute(
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)));
202 // new TextAttribute(
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)));
208 // new TextAttribute(
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)));
213 // new TextAttribute(
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(
219 // new TextAttribute(
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)));
225 // new TextAttribute(
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() {
235 * @see AbstractJavaScanner#getTokenProperties()
237 protected String[] getTokenProperties() {
238 return fgTokenProperties;
241 * @see AbstractJavaScanner#createRules()
243 protected List createRules() {
244 List rules = new ArrayList();
245 Token token= getToken(IJavaColorConstants.PHP_SINGLE_LINE_COMMENT);
246 // Add rule for single line comments.
247 rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
248 rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
249 // Add rule for strings and character constants.
250 token= getToken(IJavaColorConstants.PHP_STRING);
251 rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
252 rules.add(new MultiLineRule("`", "`", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
253 rules.add(new MultiLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
256 //rules.add(new SingleLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
259 token= getToken(IJavaColorConstants.PHP_MULTI_LINE_COMMENT);
260 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
261 // Add generic whitespace rule.
262 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
263 // Add word rule for keywords, types, and constants.
264 token= getToken(IJavaColorConstants.PHP_DEFAULT);
265 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
267 Token keyword= getToken(IJavaColorConstants.PHP_KEYWORD);
268 Token functionName= getToken(IJavaColorConstants.PHP_FUNCTIONNAME);
269 Token type= getToken(IJavaColorConstants.PHP_TYPE);
270 Token constant= getToken(IJavaColorConstants.PHP_CONSTANT);
271 PHPSyntaxRdr.readInSyntax();
272 Vector buffer = PHPSyntaxRdr.getsyntaxdata();
273 // String strbuffer = null; unused
274 PHPElement elbuffer = null;
275 while ((buffer != null)
276 && (!buffer.isEmpty()
277 && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
278 if (elbuffer instanceof PHPKeyword)
279 wordRule.addWord(((PHPKeyword) elbuffer).getName(), keyword);
280 if (elbuffer instanceof PHPFunction)
281 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
282 if (elbuffer instanceof PHPType)
283 wordRule.addWord(elbuffer.getName(), type);
284 if (elbuffer instanceof PHPConstant)
285 wordRule.addWord(elbuffer.getName(), constant);
288 // IRule[] result = new IRule[rules.size()];unused
289 // rules.toArray(result);