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
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.ui.text.IColorManager;
19 import net.sourceforge.phpeclipse.IPreferenceConstants;
20 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
21 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
22 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.text.rules.EndOfLineRule;
26 import org.eclipse.jface.text.rules.ICharacterScanner;
27 import org.eclipse.jface.text.rules.IRule;
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 extends AbstractJavaScanner {
41 * Rule to detect java operators.
45 protected class OperatorRule implements IRule {
48 private final char[] JAVA_OPERATORS= { ';', '(', ')', '.', '=', '/', '\\', '+', '-', '*', '[', ']', '<', '>', ':', '?', '!', ',', '|', '&', '^', '%', '~', '@'};
49 /** Token to return for this rule */
50 private final IToken fToken;
51 /** Token to return for braces */
52 private final IToken fTokenBraces;
54 * Creates a new operator rule.
57 * Token to use for this rule
59 public OperatorRule(IToken token, IToken tokenBraces) {
61 fTokenBraces= tokenBraces;
66 * Is this character an operator character?
69 * Character to determine whether it is an operator character
70 * @return <code>true</code> iff the character is an operator,
71 * <code>false</code> otherwise.
73 public boolean isOperator(char character) {
74 for (int index= 0; index < JAVA_OPERATORS.length; index++) {
75 if (JAVA_OPERATORS[index] == character)
82 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
84 public IToken evaluate(ICharacterScanner scanner) {
86 int character= scanner.read();
87 if (character=='{' || character=='}') {
90 if (isOperator((char) character)) {
91 int lastCharacter = character;
92 character= scanner.read();
93 if (!isOperator((char) character)) {
97 if (lastCharacter=='<' && character=='?') {
100 return Token.UNDEFINED;
101 } else if (lastCharacter=='?' && character=='>') {
104 return Token.UNDEFINED;
107 character= scanner.read();
108 } while (isOperator((char) character));
113 return Token.UNDEFINED;
118 private class PHPWordRule extends WordRule {
119 private StringBuffer fBuffer = new StringBuffer();
121 public PHPWordRule(IWordDetector detector) {
122 super(detector, Token.UNDEFINED);
125 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
126 super(detector, defaultToken);
129 public IToken evaluate(ICharacterScanner scanner) {
130 int c = scanner.read();
131 boolean isVariable = false;
137 return Token.UNDEFINED;
140 if (c != 'p' && c != 'P') {
142 return getToken(IPreferenceConstants.PHP_TAG);
145 if (c != 'h' && c != 'H') {
148 return getToken(IPreferenceConstants.PHP_TAG);
151 if (c != 'p' && c != 'P') {
155 return getToken(IPreferenceConstants.PHP_TAG);
157 return getToken(IPreferenceConstants.PHP_TAG);
166 return getToken(IPreferenceConstants.PHP_TAG);
170 return Token.UNDEFINED;
172 if (fDetector.isWordStart((char) c)) {
176 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
178 fBuffer.setLength(0);
180 fBuffer.append((char) c);
182 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
186 return getToken(IPreferenceConstants.PHP_VARIABLE);
188 IToken token = (IToken) fWords.get(fBuffer.toString());
192 if (fDefaultToken.isUndefined())
193 unreadBuffer(scanner);
195 return fDefaultToken;
200 return Token.UNDEFINED;
204 //private PHPColorProvider fColorProvider;
206 private static String[] fgTokenProperties =
208 IPreferenceConstants.PHP_MULTILINE_COMMENT,
209 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
210 IPreferenceConstants.PHP_TAG,
211 IPreferenceConstants.PHP_KEYWORD,
212 IPreferenceConstants.PHP_FUNCTIONNAME,
213 IPreferenceConstants.PHP_VARIABLE,
214 IPreferenceConstants.PHP_STRING,
215 IPreferenceConstants.PHP_TYPE,
216 IPreferenceConstants.PHP_CONSTANT,
217 IPreferenceConstants.PHP_DEFAULT,
218 IPreferenceConstants.PHP_OPERATOR,
219 IPreferenceConstants.PHP_BRACE_OPERATOR,
220 IPreferenceConstants.PHP_KEYWORD_RETURN};
222 * Creates a PHP code scanner
224 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
225 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
226 super(manager, store);
228 // // final IPreferenceStore store =
229 // PHPeclipsePlugin.getDefault().getPreferenceStore();
230 // Color BackgroundColor =
231 // provider.getColor(PreferenceConverter.getColor(store,
232 // PHP_EDITOR_BACKGROUND));
235 // new TextAttribute(
236 // provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
238 // (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
239 // + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
241 // new Token(new TextAttribute(
242 // provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
245 // (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE) +
246 // (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
248 // new Token(new TextAttribute(
249 // provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
252 // (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) +
253 // (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
255 // new Token(new TextAttribute(
256 // provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
259 // (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
260 // + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
262 // new Token(new TextAttribute(
263 // provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
266 // (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE) +
267 // (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
269 // new Token(new TextAttribute(
270 // provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
273 // (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE ) +
274 // (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
276 // new Token(new TextAttribute(
277 // provider.getColor(PreferenceConverter.getColor(store,
278 // PHP_SINGLELINE_COMMENT)),
281 // (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE )
282 // + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC :
285 // new Token(new TextAttribute(
286 // provider.getColor(PreferenceConverter.getColor(store,
287 // PHP_MULTILINE_COMMENT)),
290 // (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
291 // + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC :
294 // new Token(new TextAttribute(
295 // provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
298 // (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE) +
299 // (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
300 // updateWordRules();
304 * @see AbstractJavaScanner#getTokenProperties()
306 protected String[] getTokenProperties() {
307 return fgTokenProperties;
311 * @see AbstractJavaScanner#createRules()
313 protected List createRules() {
314 List rules = new ArrayList();
315 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
316 // Add rule for single line comments.
317 rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
318 rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
319 // Add rule for strings and character constants.
320 token = getToken(IPreferenceConstants.PHP_STRING);
321 rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
322 rules.add(new MultiLineRule("`", "`", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
323 rules.add(new MultiLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
325 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
326 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
327 // Add generic whitespace rule.
328 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
329 // Add word rule for keywords, types, and constants.
330 token = getToken(IPreferenceConstants.PHP_DEFAULT);
331 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
333 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
334 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
335 Token type = getToken(IPreferenceConstants.PHP_TYPE);
336 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
338 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
339 // String strbuffer = null; unused
340 PHPElement elbuffer = null;
342 for (int i = 0; i < buffer.size(); i++) {
343 // while ((buffer != null)
344 // && (!buffer.isEmpty()
345 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
346 elbuffer = (PHPElement) buffer.get(i);
347 if (elbuffer instanceof PHPKeyword) {
348 name = ((PHPKeyword) elbuffer).getName();
349 if (!name.equals("return")) {
350 wordRule.addWord(name, keyword);
352 } else if (elbuffer instanceof PHPFunction) {
353 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
354 } else if (elbuffer instanceof PHPType) {
355 wordRule.addWord(elbuffer.getName(), type);
356 } else if (elbuffer instanceof PHPConstant) {
357 wordRule.addWord(elbuffer.getName(), constant);
361 // Add word rule for keyword 'return'.
362 token= getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
363 wordRule.addWord("return", token);
365 // Add rule for operators and brackets (at the end !)
366 rules.add(new OperatorRule(getToken(IPreferenceConstants.PHP_OPERATOR), getToken(IPreferenceConstants.PHP_BRACE_OPERATOR)));
370 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));