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;
17 import net.sourceforge.phpeclipse.IPreferenceConstants;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
20 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
21 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.preference.PreferenceConverter;
24 import org.eclipse.jface.text.TextAttribute;
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.RuleBasedScanner;
32 import org.eclipse.jface.text.rules.SingleLineRule;
33 import org.eclipse.jface.text.rules.Token;
34 import org.eclipse.jface.text.rules.WhitespaceRule;
35 import org.eclipse.jface.text.rules.WordRule;
40 public class PHPCodeScanner extends RuleBasedScanner implements IPreferenceConstants {
42 private static Token variable;
43 private static Token keyword;
44 private static Token functionName;
45 private static Token string;
46 private static Token comment;
47 private static Token multi_comment;
48 private static Token other;
50 private class PHPWordRule extends WordRule {
51 private StringBuffer fBuffer = new StringBuffer();
53 public PHPWordRule(IWordDetector detector) {
54 super(detector, Token.UNDEFINED);
57 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
58 super(detector, defaultToken);
61 public IToken evaluate(ICharacterScanner scanner) {
62 int c = scanner.read();
63 boolean isVariable = false;
64 if (fDetector.isWordStart((char) c)) {
68 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
72 fBuffer.append((char) c);
74 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
80 IToken token = (IToken) fWords.get(fBuffer.toString());
84 if (fDefaultToken.isUndefined())
85 unreadBuffer(scanner);
92 return Token.UNDEFINED;
96 private static String[] fgConstants = { "__LINE__", "__FILE__", "true", "false", "null", "object", "array" };
97 // private static TextAttribute fSingleLine;
98 // private static TextAttribute fMultiLine;
99 // private static TextAttribute fKeyword;
100 // private static TextAttribute fFunctionName;
101 // private static TextAttribute fString;
102 // private static TextAttribute fVariable;
103 private PHPColorProvider fColorProvider;
106 * Creates a PHP code scanner
108 public PHPCodeScanner(PHPColorProvider provider) {
109 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
111 variable = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE))));
112 keyword = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD))));
113 functionName = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME))));
114 string = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_STRING))));
115 comment = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT))));
116 multi_comment = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT))));
117 other = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT))));
119 List rules = new ArrayList();
121 // Add rule for single line comments.
122 rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$
123 rules.add(new EndOfLineRule("#", comment));
125 // Add rule for strings and character constants.
126 rules.add(new MultiLineRule("\"", "\"", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
127 rules.add(new SingleLineRule("'", "'", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
129 // rules.add(new SingleLineRule("//", "//", php_comment));
130 rules.add(new MultiLineRule("/*", "*/", multi_comment));
132 // Add generic whitespace rule.
133 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
135 // Add word rule for keywords, types, and constants.
136 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), other);
138 // for (int i = 0; i < PHPKeywords.PHP_KEYWORS.length; i++)
139 // wordRule.addWord(PHPKeywords.PHP_KEYWORS[i], keyword);
141 // /*Read in the keywords from the XML file*/
142 // PHPSyntaxRdr syntaxRdr = new PHPSyntaxRdr();
143 // syntaxRdr.readFromFile(
144 // "C:\\eclipse\\workspace\\net.sourceforge.phpeclipse\\src\\net\\sourceforge\\phpeclipse\\phpeditor"
145 // + java.io.File.separator
148 // Vector Vbuffer = syntaxRdr.getKeywords();
149 // String VString = null;
151 // while ((Vbuffer != null) && (!Vbuffer.isEmpty() && ((VString = (String) Vbuffer.remove(0)) != null))) {
152 // wordRule.addWord(VString, keyword);
154 // //Read functions - to be tested
155 // Vbuffer = syntaxRdr.getFunctions();
156 // while ((Vbuffer != null) && (!Vbuffer.isEmpty() && ((VString = (String) Vbuffer.remove(0)) != null))) {
157 // wordRule.addWord(VString, functionName);
161 // //for (int i = 0; i < PHPFunctionNames.FUNCTION_NAMES.length; i++)
162 // // wordRule.addWord(PHPFunctionNames.FUNCTION_NAMES[i], functionName);
164 for (int i = 0; i < PHPKeywords.PHP_KEYWORS.length; i++) {
165 wordRule.addWord(PHPKeywords.PHP_KEYWORS[i], keyword);
167 for (int i = 0; i < PHPFunctionNames.FUNCTION_NAMES.length; i++) {
168 wordRule.addWord(PHPFunctionNames.FUNCTION_NAMES[i], functionName);
170 for (int i = 0; i < fgConstants.length; i++) {
171 wordRule.addWord(fgConstants[i], keyword);
175 IRule[] result = new IRule[rules.size()];
176 rules.toArray(result);
180 public void updateToken(PHPColorProvider provider) {
181 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
183 variable.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE))));
184 keyword.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD))));
185 functionName.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME))));
186 string.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_STRING))));
187 comment.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT))));
188 multi_comment.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT))));
189 other.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT))));