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.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.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
39 extends AbstractJavaScanner {
41 // private static Token variable;
42 // private static Token keyword;
43 // private static Token type;
44 // private static Token constant;
45 // private static Token functionName;
46 // private static Token string;
47 // private static Token comment;
48 // private static Token multi_comment;
49 // private static Token other;
51 private class PHPWordRule extends WordRule {
52 private StringBuffer fBuffer = new StringBuffer();
54 public PHPWordRule(IWordDetector detector) {
55 super(detector, Token.UNDEFINED);
58 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
59 super(detector, defaultToken);
62 public IToken evaluate(ICharacterScanner scanner) {
63 int c = scanner.read();
64 boolean isVariable = false;
65 if (fDetector.isWordStart((char) c)) {
69 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
73 fBuffer.append((char) c);
76 c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
80 return getToken(IPreferenceConstants.PHP_VARIABLE);
82 IToken token = (IToken) fWords.get(fBuffer.toString());
86 if (fDefaultToken.isUndefined())
87 unreadBuffer(scanner);
94 return Token.UNDEFINED;
98 //private PHPColorProvider fColorProvider;
100 private static String[] fgTokenProperties = {
101 IPreferenceConstants.PHP_MULTILINE_COMMENT,
102 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
103 IPreferenceConstants.PHP_KEYWORD,
104 IPreferenceConstants.PHP_FUNCTIONNAME,
105 IPreferenceConstants.PHP_VARIABLE,
106 IPreferenceConstants.PHP_STRING,
107 IPreferenceConstants.PHP_TYPE,
108 IPreferenceConstants.PHP_CONSTANT,
109 IPreferenceConstants.PHP_DEFAULT };
111 * Creates a PHP code scanner
113 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
114 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
115 super(manager, store);
117 // // final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
118 // Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
121 // new TextAttribute(
122 // provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
124 // (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
125 // + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
127 // new Token(new TextAttribute(
128 // provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
131 // (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
133 // new Token(new TextAttribute(
134 // provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
137 // (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
139 // new Token(new TextAttribute(
140 // provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
143 // (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
144 // + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
146 // new Token(new TextAttribute(
147 // provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
150 // (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
152 // new Token(new TextAttribute(
153 // provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
156 // (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE ) + (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
158 // new Token(new TextAttribute(
159 // provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)),
162 // (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE )
163 // + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
165 // new Token(new TextAttribute(
166 // provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)),
169 // (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
170 // + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
172 // new Token(new TextAttribute(
173 // provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
176 // (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
177 // updateWordRules();
180 // public void updateToken(JavaColorManager provider) {
181 // final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
183 // Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
186 // new TextAttribute(
187 // provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
189 // (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
190 // + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
192 // new TextAttribute(
193 // provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
195 // (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE)
196 // + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
198 // new TextAttribute(
199 // provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
201 // (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
202 // functionName.setData(
203 // new TextAttribute(
204 // provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
206 // (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
207 // + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
209 // new TextAttribute(
210 // provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
212 // (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE)
213 // + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
215 // new TextAttribute(
216 // provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
218 // (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
220 // new TextAttribute(
221 // provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)),
223 // (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
224 // + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
225 // multi_comment.setData(
226 // new TextAttribute(
227 // provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)),
229 // (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
230 // + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
232 // new TextAttribute(
233 // provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
235 // (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE)
236 // + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
239 // public void updateWordRules() {
242 * @see AbstractJavaScanner#getTokenProperties()
244 protected String[] getTokenProperties() {
245 return fgTokenProperties;
248 * @see AbstractJavaScanner#createRules()
250 protected List createRules() {
251 List rules = new ArrayList();
252 Token token= getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
253 // Add rule for single line comments.
254 rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
255 rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
256 // Add rule for strings and character constants.
257 token= getToken(IPreferenceConstants.PHP_STRING);
258 rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
259 rules.add(new MultiLineRule("`", "`", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
260 rules.add(new MultiLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
263 //rules.add(new SingleLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
266 token= getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
267 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
268 // Add generic whitespace rule.
269 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
270 // Add word rule for keywords, types, and constants.
271 token= getToken(IPreferenceConstants.PHP_DEFAULT);
272 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
274 Token keyword= getToken(IPreferenceConstants.PHP_KEYWORD);
275 Token functionName= getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
276 Token type= getToken(IPreferenceConstants.PHP_TYPE);
277 Token constant= getToken(IPreferenceConstants.PHP_CONSTANT);
278 PHPSyntaxRdr.readInSyntax();
279 Vector buffer = PHPSyntaxRdr.getsyntaxdata();
280 // String strbuffer = null; unused
281 PHPElement elbuffer = null;
282 while ((buffer != null)
283 && (!buffer.isEmpty()
284 && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
285 if (elbuffer instanceof PHPKeyword)
286 wordRule.addWord(((PHPKeyword) elbuffer).getName(), keyword);
287 if (elbuffer instanceof PHPFunction)
288 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
289 if (elbuffer instanceof PHPType)
290 wordRule.addWord(elbuffer.getName(), type);
291 if (elbuffer instanceof PHPConstant)
292 wordRule.addWord(elbuffer.getName(), constant);
295 // IRule[] result = new IRule[rules.size()];unused
296 // rules.toArray(result);