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[] PHP_OPERATORS = {
74 /** Token to return for this rule */
75 private final IToken fToken;
77 /** Token to return for braces */
78 private final IToken fTokenBraces;
81 * Creates a new operator rule.
84 * Token to use for this rule
86 public OperatorRule(IToken token, IToken tokenBraces) {
88 fTokenBraces = tokenBraces;
93 * Is this character an operator character?
96 * Character to determine whether it is an operator character
97 * @return <code>true</code> iff the character is an operator, <code>false</code> otherwise.
99 public boolean isOperator(char character) {
100 for (int index = 0; index < PHP_OPERATORS.length; index++) {
101 if (PHP_OPERATORS[index] == character)
108 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
110 public IToken evaluate(ICharacterScanner scanner) {
112 int character = scanner.read();
113 if (character == '{' || character == '}') {
116 if (isOperator((char) character)) {
117 int lastCharacter = character;
118 character = scanner.read();
119 if (!isOperator((char) character)) {
123 if (checkPHPTag(scanner, lastCharacter, character)) {
124 return Token.UNDEFINED;
127 lastCharacter = character;
128 character = scanner.read();
129 if (checkPHPTag(scanner, lastCharacter, character)) {
132 } while (isOperator((char) character));
137 return Token.UNDEFINED;
142 * Check if lastCharacter/character are a PHP start or end token ( <? ... ?> )
145 * @param lastCharacter
149 private boolean checkPHPTag(ICharacterScanner scanner, int lastCharacter, int character) {
150 if (lastCharacter == '<' && character == '?') {
154 } else if (lastCharacter == '?' && character == '>') {
163 private class PHPWordRule extends WordRule {
164 private StringBuffer fBuffer = new StringBuffer();
166 public PHPWordRule(IWordDetector detector) {
167 super(detector, Token.UNDEFINED);
170 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
171 super(detector, defaultToken);
174 public IToken evaluate(ICharacterScanner scanner) {
175 int c = scanner.read();
176 boolean isVariable = false;
182 return Token.UNDEFINED;
185 if (c == '=') { // <?=
186 return getToken(IPreferenceConstants.PHP_TAG);
188 if (c != 'p' && c != 'P') {
190 return getToken(IPreferenceConstants.PHP_TAG);
193 if (c != 'h' && c != 'H') {
196 return getToken(IPreferenceConstants.PHP_TAG);
199 if (c != 'p' && c != 'P') {
203 return getToken(IPreferenceConstants.PHP_TAG);
205 return getToken(IPreferenceConstants.PHP_TAG);
214 return getToken(IPreferenceConstants.PHP_TAG);
218 return Token.UNDEFINED;
220 if (fDetector.isWordStart((char) c)) {
224 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
226 fBuffer.setLength(0);
228 fBuffer.append((char) c);
230 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
234 return getToken(IPreferenceConstants.PHP_VARIABLE);
236 IToken token = (IToken) fWords.get(fBuffer.toString());
240 if (fDefaultToken.isUndefined())
241 unreadBuffer(scanner);
243 return fDefaultToken;
248 return Token.UNDEFINED;
252 //private PHPColorProvider fColorProvider;
254 private static String[] fgTokenProperties = {
255 IPreferenceConstants.PHP_MULTILINE_COMMENT,
256 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
257 IPreferenceConstants.PHP_TAG,
258 IPreferenceConstants.PHP_KEYWORD,
259 IPreferenceConstants.PHP_FUNCTIONNAME,
260 IPreferenceConstants.PHP_VARIABLE,
261 IPreferenceConstants.PHP_STRING,
262 IPreferenceConstants.PHP_TYPE,
263 IPreferenceConstants.PHP_CONSTANT,
264 IPreferenceConstants.PHP_DEFAULT,
265 IPreferenceConstants.PHP_OPERATOR,
266 IPreferenceConstants.PHP_BRACE_OPERATOR,
267 IPreferenceConstants.PHP_KEYWORD_RETURN };
270 * Creates a PHP code scanner
272 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
273 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
274 super(manager, store);
279 * @see AbstractJavaScanner#getTokenProperties()
281 protected String[] getTokenProperties() {
282 return fgTokenProperties;
286 * @see AbstractJavaScanner#createRules()
288 protected List createRules() {
289 List rules = new ArrayList();
290 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
291 // Add rule for single line comments.
292 rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
293 rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
294 // Add rule for strings and character constants.
295 token = getToken(IPreferenceConstants.PHP_STRING);
296 rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
297 rules.add(new MultiLineRule("`", "`", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
298 rules.add(new MultiLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
300 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
301 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
302 // Add generic whitespace rule.
303 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
304 // Add word rule for keywords, types, and constants.
305 token = getToken(IPreferenceConstants.PHP_DEFAULT);
306 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
308 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
309 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
310 Token type = getToken(IPreferenceConstants.PHP_TYPE);
311 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
313 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
314 // String strbuffer = null; unused
315 PHPElement elbuffer = null;
317 for (int i = 0; i < buffer.size(); i++) {
318 // while ((buffer != null)
319 // && (!buffer.isEmpty()
320 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
321 elbuffer = (PHPElement) buffer.get(i);
322 if (elbuffer instanceof PHPKeyword) {
323 name = ((PHPKeyword) elbuffer).getName();
324 if (!name.equals("return")) {
325 wordRule.addWord(name, keyword);
327 } else if (elbuffer instanceof PHPFunction) {
328 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
329 } else if (elbuffer instanceof PHPType) {
330 wordRule.addWord(elbuffer.getName(), type);
331 } else if (elbuffer instanceof PHPConstant) {
332 wordRule.addWord(elbuffer.getName(), constant);
336 // Add word rule for keyword 'return'.
337 token = getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
338 wordRule.addWord("return", token);
340 // Add rule for operators and brackets (at the end !)
341 rules.add(new OperatorRule(getToken(IPreferenceConstants.PHP_OPERATOR), getToken(IPreferenceConstants.PHP_BRACE_OPERATOR)));
345 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));