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.phpeclipse.IPreferenceConstants;
19 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
20 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
21 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
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.preference.PreferenceConverter;
27 import org.eclipse.jface.text.TextAttribute;
28 import org.eclipse.jface.text.rules.EndOfLineRule;
29 import org.eclipse.jface.text.rules.ICharacterScanner;
30 import org.eclipse.jface.text.rules.IRule;
31 import org.eclipse.jface.text.rules.IToken;
32 import org.eclipse.jface.text.rules.IWordDetector;
33 import org.eclipse.jface.text.rules.MultiLineRule;
34 import org.eclipse.jface.text.rules.RuleBasedScanner;
35 import org.eclipse.jface.text.rules.SingleLineRule;
36 import org.eclipse.jface.text.rules.Token;
37 import org.eclipse.jface.text.rules.WhitespaceRule;
38 import org.eclipse.jface.text.rules.WordRule;
43 public class PHPCodeScanner extends RuleBasedScanner implements IPreferenceConstants {
45 private static Token variable;
46 private static Token keyword;
47 private static Token functionName;
48 private static Token string;
49 private static Token comment;
50 private static Token multi_comment;
51 private static Token other;
53 private class PHPWordRule extends WordRule {
54 private StringBuffer fBuffer = new StringBuffer();
56 public PHPWordRule(IWordDetector detector) {
57 super(detector, Token.UNDEFINED);
60 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
61 super(detector, defaultToken);
64 public IToken evaluate(ICharacterScanner scanner) {
65 int c = scanner.read();
66 boolean isVariable = false;
67 if (fDetector.isWordStart((char) c)) {
71 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
75 fBuffer.append((char) c);
77 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
83 IToken token = (IToken) fWords.get(fBuffer.toString());
87 if (fDefaultToken.isUndefined())
88 unreadBuffer(scanner);
95 return Token.UNDEFINED;
99 private static String[] fgConstants = { "__LINE__", "__FILE__", "true", "false", "null", "object", "array" };
100 // private static TextAttribute fSingleLine;
101 // private static TextAttribute fMultiLine;
102 // private static TextAttribute fKeyword;
103 // private static TextAttribute fFunctionName;
104 // private static TextAttribute fString;
105 // private static TextAttribute fVariable;
106 private PHPColorProvider fColorProvider;
109 * Creates a PHP code scanner
111 public PHPCodeScanner(PHPColorProvider provider) {
112 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
114 variable = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE))));
115 keyword = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD))));
116 functionName = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME))));
117 string = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_STRING))));
118 comment = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT))));
119 multi_comment = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT))));
120 other = new Token(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT))));
122 List rules = new ArrayList();
124 // Add rule for single line comments.
125 rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$
126 rules.add(new EndOfLineRule("#", comment));
128 // Add rule for strings and character constants.
129 rules.add(new MultiLineRule("\"", "\"", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
130 rules.add(new SingleLineRule("'", "'", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
132 // rules.add(new SingleLineRule("//", "//", php_comment));
133 rules.add(new MultiLineRule("/*", "*/", multi_comment));
135 // Add generic whitespace rule.
136 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
138 // Add word rule for keywords, types, and constants.
139 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), other);
141 for (int i = 0; i < PHPKeywords.PHP_KEYWORS.length; i++)
142 wordRule.addWord(PHPKeywords.PHP_KEYWORS[i], keyword);
144 /*Read in the keywords from the XML file*/
145 PHPSyntaxRdr syntaxRdr = new PHPSyntaxRdr();
146 syntaxRdr.readFromFile(
147 "C:\\eclipse\\workspace\\net.sourceforge.phpeclipse\\src\\net\\sourceforge\\phpeclipse\\phpeditor"
148 + java.io.File.separator
151 Vector Vbuffer = syntaxRdr.getKeywords();
152 String VString = null;
154 while ((Vbuffer != null) && (!Vbuffer.isEmpty() && ((VString = (String) Vbuffer.remove(0)) != null))) {
155 wordRule.addWord(VString, keyword);
157 //Read functions - to be tested
158 Vbuffer = syntaxRdr.getFunctions();
159 while ((Vbuffer != null) && (!Vbuffer.isEmpty() && ((VString = (String) Vbuffer.remove(0)) != null))) {
160 wordRule.addWord(VString, functionName);
164 //for (int i = 0; i < PHPFunctionNames.FUNCTION_NAMES.length; i++)
165 // wordRule.addWord(PHPFunctionNames.FUNCTION_NAMES[i], functionName);
167 for (int i = 0; i < fgConstants.length; i++)
168 wordRule.addWord(fgConstants[i], functionName);
171 IRule[] result = new IRule[rules.size()];
172 rules.toArray(result);
176 public void updateToken(PHPColorProvider provider) {
177 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
179 variable.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE))));
180 keyword.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD))));
181 functionName.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME))));
182 string.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_STRING))));
183 comment.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT))));
184 multi_comment.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT))));
185 other.setData(new TextAttribute(provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT))));