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;
37 public class PHPCodeScanner extends AbstractJavaScanner {
40 * Rule to detect java operators.
44 protected class OperatorRule implements IRule {
47 private final char[] PHP_OPERATORS = {
73 /** Token to return for this rule */
74 private final IToken fToken;
76 /** Token to return for braces */
77 private final IToken fTokenBraces;
80 * Creates a new operator rule.
83 * Token to use for this rule
85 public OperatorRule(IToken token, IToken tokenBraces) {
87 fTokenBraces = tokenBraces;
92 * Is this character an operator character?
95 * Character to determine whether it is an operator character
96 * @return <code>true</code> iff the character is an operator, <code>false</code> otherwise.
98 public boolean isOperator(char character) {
99 for (int index = 0; index < PHP_OPERATORS.length; index++) {
100 if (PHP_OPERATORS[index] == character)
107 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
109 public IToken evaluate(ICharacterScanner scanner) {
111 int character = scanner.read();
112 if (character == '{' || character == '}') {
115 if (isOperator((char) character)) {
116 int lastCharacter = character;
117 character = scanner.read();
118 if (!isOperator((char) character)) {
122 if (checkPHPTag(scanner, lastCharacter, character)) {
123 return Token.UNDEFINED;
126 lastCharacter = character;
127 character = scanner.read();
128 if (checkPHPTag(scanner, lastCharacter, character)) {
131 } while (isOperator((char) character));
136 return Token.UNDEFINED;
141 * Check if lastCharacter/character are a PHP start or end token ( <? ... ?> )
144 * @param lastCharacter
148 private boolean checkPHPTag(ICharacterScanner scanner, int lastCharacter, int character) {
149 if (lastCharacter == '<' && character == '?') {
153 } else if (lastCharacter == '?' && character == '>') {
162 private class PHPWordRule extends WordRule {
163 private StringBuffer fBuffer = new StringBuffer();
165 public PHPWordRule(IWordDetector detector) {
166 super(detector, Token.UNDEFINED);
169 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
170 super(detector, defaultToken);
173 public IToken evaluate(ICharacterScanner scanner) {
174 int c = scanner.read();
175 boolean isVariable = false;
181 return Token.UNDEFINED;
184 if (c == '=') { // <?=
185 return getToken(IPreferenceConstants.PHP_TAG);
187 if (c != 'p' && c != 'P') {
189 return getToken(IPreferenceConstants.PHP_TAG);
192 if (c != 'h' && c != 'H') {
195 return getToken(IPreferenceConstants.PHP_TAG);
198 if (c != 'p' && c != 'P') {
202 return getToken(IPreferenceConstants.PHP_TAG);
204 return getToken(IPreferenceConstants.PHP_TAG);
213 return getToken(IPreferenceConstants.PHP_TAG);
217 return Token.UNDEFINED;
219 if (fDetector.isWordStart((char) c)) {
223 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
225 fBuffer.setLength(0);
227 fBuffer.append((char) c);
229 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
233 return getToken(IPreferenceConstants.PHP_VARIABLE);
235 IToken token = (IToken) fWords.get(fBuffer.toString());
239 if (fDefaultToken.isUndefined())
240 unreadBuffer(scanner);
242 return fDefaultToken;
247 return Token.UNDEFINED;
251 //private PHPColorProvider fColorProvider;
253 private static String[] fgTokenProperties = {
254 IPreferenceConstants.PHP_MULTILINE_COMMENT,
255 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
256 IPreferenceConstants.PHP_TAG,
257 IPreferenceConstants.PHP_KEYWORD,
258 IPreferenceConstants.PHP_FUNCTIONNAME,
259 IPreferenceConstants.PHP_VARIABLE,
260 IPreferenceConstants.PHP_STRING,
261 IPreferenceConstants.PHP_TYPE,
262 IPreferenceConstants.PHP_CONSTANT,
263 IPreferenceConstants.PHP_DEFAULT,
264 IPreferenceConstants.PHP_OPERATOR,
265 IPreferenceConstants.PHP_BRACE_OPERATOR,
266 IPreferenceConstants.PHP_KEYWORD_RETURN };
269 * Creates a PHP code scanner
271 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
272 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
273 super(manager, store);
278 * @see AbstractJavaScanner#getTokenProperties()
280 protected String[] getTokenProperties() {
281 return fgTokenProperties;
285 * @see AbstractJavaScanner#createRules()
287 protected List createRules() {
288 List rules = new ArrayList();
289 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
290 // Add rule for single line comments.
291 rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
292 rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
293 // Add rule for strings and character constants.
294 token = getToken(IPreferenceConstants.PHP_STRING);
295 rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
296 rules.add(new MultiLineRule("`", "`", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
297 rules.add(new MultiLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
299 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
300 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
301 // Add generic whitespace rule.
302 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
303 // Add word rule for keywords, types, and constants.
304 token = getToken(IPreferenceConstants.PHP_DEFAULT);
305 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
307 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
308 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
309 Token type = getToken(IPreferenceConstants.PHP_TYPE);
310 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
312 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
313 // String strbuffer = null; unused
314 PHPElement elbuffer = null;
316 for (int i = 0; i < buffer.size(); i++) {
317 // while ((buffer != null)
318 // && (!buffer.isEmpty()
319 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
320 elbuffer = (PHPElement) buffer.get(i);
321 if (elbuffer instanceof PHPKeyword) {
322 name = ((PHPKeyword) elbuffer).getName();
323 if (!name.equals("return")) {
324 wordRule.addWord(name, keyword);
326 } else if (elbuffer instanceof PHPFunction) {
327 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
328 } else if (elbuffer instanceof PHPType) {
329 wordRule.addWord(elbuffer.getName(), type);
330 } else if (elbuffer instanceof PHPConstant) {
331 wordRule.addWord(elbuffer.getName(), constant);
335 // Add word rule for keyword 'return'.
336 token = getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
337 wordRule.addWord("return", token);
339 // Add rule for operators and brackets (at the end !)
340 rules.add(new OperatorRule(getToken(IPreferenceConstants.PHP_OPERATOR), getToken(IPreferenceConstants.PHP_BRACE_OPERATOR)));
344 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));