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.ICharacterScanner;
26 import org.eclipse.jface.text.rules.IRule;
27 import org.eclipse.jface.text.rules.IToken;
28 import org.eclipse.jface.text.rules.IWordDetector;
29 import org.eclipse.jface.text.rules.MultiLineRule;
30 import org.eclipse.jface.text.rules.Token;
31 import org.eclipse.jface.text.rules.WhitespaceRule;
32 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 == '>') {
163 protected class AccentStringRule implements IRule {
165 /** Token to return for this rule */
166 private final IToken fToken;
168 public AccentStringRule(IToken token) {
174 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
176 public IToken evaluate(ICharacterScanner scanner) {
178 int character = scanner.read();
180 if (character == '`') {
183 character = scanner.read();
184 if (character == '\\') {
185 character = scanner.read();
186 } else if (character == '`') {
193 return Token.UNDEFINED;
199 private class PHPWordRule extends WordRule {
200 private StringBuffer fBuffer = new StringBuffer();
202 public PHPWordRule(IWordDetector detector) {
203 super(detector, Token.UNDEFINED);
206 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
207 super(detector, defaultToken);
210 public IToken evaluate(ICharacterScanner scanner) {
211 int c = scanner.read();
212 boolean isVariable = false;
218 return Token.UNDEFINED;
221 if (c == '=') { // <?=
222 return getToken(IPreferenceConstants.PHP_TAG);
224 if (c != 'p' && c != 'P') {
226 return getToken(IPreferenceConstants.PHP_TAG);
229 if (c != 'h' && c != 'H') {
232 return getToken(IPreferenceConstants.PHP_TAG);
235 if (c != 'p' && c != 'P') {
239 return getToken(IPreferenceConstants.PHP_TAG);
241 return getToken(IPreferenceConstants.PHP_TAG);
250 return getToken(IPreferenceConstants.PHP_TAG);
254 return Token.UNDEFINED;
256 if (fDetector.isWordStart((char) c)) {
260 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
262 fBuffer.setLength(0);
264 fBuffer.append((char) c);
266 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
270 return getToken(IPreferenceConstants.PHP_VARIABLE);
272 IToken token = (IToken) fWords.get(fBuffer.toString());
276 if (fDefaultToken.isUndefined())
277 unreadBuffer(scanner);
279 return fDefaultToken;
284 return Token.UNDEFINED;
288 //private PHPColorProvider fColorProvider;
290 private static String[] fgTokenProperties = {
291 IPreferenceConstants.PHP_MULTILINE_COMMENT,
292 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
293 IPreferenceConstants.PHP_TAG,
294 IPreferenceConstants.PHP_KEYWORD,
295 IPreferenceConstants.PHP_FUNCTIONNAME,
296 IPreferenceConstants.PHP_VARIABLE,
297 IPreferenceConstants.PHP_STRING_DQ,
298 IPreferenceConstants.PHP_STRING_SQ,
299 IPreferenceConstants.PHP_TYPE,
300 IPreferenceConstants.PHP_CONSTANT,
301 IPreferenceConstants.PHP_DEFAULT,
302 IPreferenceConstants.PHP_OPERATOR,
303 IPreferenceConstants.PHP_BRACE_OPERATOR,
304 IPreferenceConstants.PHP_KEYWORD_RETURN };
307 * Creates a PHP code scanner
309 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
310 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
311 super(manager, store);
316 * @see AbstractJavaScanner#getTokenProperties()
318 protected String[] getTokenProperties() {
319 return fgTokenProperties;
323 * @see AbstractJavaScanner#createRules()
325 protected List createRules() {
326 List rules = new ArrayList();
327 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
328 // Add rule for single line comments.
329 // rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
330 // rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
331 // Add rule for strings and character constants.
332 // token = getToken(IPreferenceConstants.PHP_STRING_SQ);
333 // rules.add(new SingleQuoteStringRule(token));
334 // token = getToken(IPreferenceConstants.PHP_STRING_DQ);
335 // rules.add(new DoubleQuoteStringRule(token));
336 rules.add(new AccentStringRule(token));
338 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
339 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
340 // Add generic whitespace rule.
341 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
342 // Add word rule for keywords, types, and constants.
343 token = getToken(IPreferenceConstants.PHP_DEFAULT);
344 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
346 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
347 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
348 Token type = getToken(IPreferenceConstants.PHP_TYPE);
349 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
351 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
352 // String strbuffer = null; unused
353 PHPElement elbuffer = null;
355 for (int i = 0; i < buffer.size(); i++) {
356 // while ((buffer != null)
357 // && (!buffer.isEmpty()
358 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
359 elbuffer = (PHPElement) buffer.get(i);
360 if (elbuffer instanceof PHPKeyword) {
361 name = ((PHPKeyword) elbuffer).getName();
362 if (!name.equals("return")) {
363 wordRule.addWord(name, keyword);
365 } else if (elbuffer instanceof PHPFunction) {
366 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
367 } else if (elbuffer instanceof PHPType) {
368 wordRule.addWord(elbuffer.getName(), type);
369 } else if (elbuffer instanceof PHPConstant) {
370 wordRule.addWord(elbuffer.getName(), constant);
374 // Add word rule for keyword 'return'.
375 token = getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
376 wordRule.addWord("return", token);
378 // Add rule for operators and brackets (at the end !)
379 rules.add(new OperatorRule(getToken(IPreferenceConstants.PHP_OPERATOR), getToken(IPreferenceConstants.PHP_BRACE_OPERATOR)));
383 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));