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.BadLocationException;
26 import org.eclipse.jface.text.rules.EndOfLineRule;
27 import org.eclipse.jface.text.rules.ICharacterScanner;
28 import org.eclipse.jface.text.rules.IRule;
29 import org.eclipse.jface.text.rules.IToken;
30 import org.eclipse.jface.text.rules.IWordDetector;
31 import org.eclipse.jface.text.rules.MultiLineRule;
32 import org.eclipse.jface.text.rules.Token;
33 import org.eclipse.jface.text.rules.WhitespaceRule;
34 import org.eclipse.jface.text.rules.WordRule;
39 public class PHPCodeScanner extends AbstractJavaScanner {
42 * Rule to detect java operators.
46 protected class OperatorRule implements IRule {
49 private final char[] PHP_OPERATORS = {
75 /** Token to return for this rule */
76 private final IToken fToken;
78 /** Token to return for braces */
79 private final IToken fTokenBraces;
82 * Creates a new operator rule.
85 * Token to use for this rule
87 public OperatorRule(IToken token, IToken tokenBraces) {
89 fTokenBraces = tokenBraces;
94 * Is this character an operator character?
97 * Character to determine whether it is an operator character
98 * @return <code>true</code> iff the character is an operator, <code>false</code> otherwise.
100 public boolean isOperator(char character) {
101 for (int index = 0; index < PHP_OPERATORS.length; index++) {
102 if (PHP_OPERATORS[index] == character)
109 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
111 public IToken evaluate(ICharacterScanner scanner) {
113 int character = scanner.read();
114 if (character == '{' || character == '}') {
117 if (isOperator((char) character)) {
118 int lastCharacter = character;
119 character = scanner.read();
120 if (!isOperator((char) character)) {
124 if (checkPHPTag(scanner, lastCharacter, character)) {
125 return Token.UNDEFINED;
128 lastCharacter = character;
129 character = scanner.read();
130 if (checkPHPTag(scanner, lastCharacter, character)) {
133 } while (isOperator((char) character));
138 return Token.UNDEFINED;
143 * Check if lastCharacter/character are a PHP start or end token ( <? ... ?> )
146 * @param lastCharacter
150 private boolean checkPHPTag(ICharacterScanner scanner, int lastCharacter, int character) {
151 if (lastCharacter == '<' && character == '?') {
155 } else if (lastCharacter == '?' && character == '>') {
165 protected class AccentStringRule implements IRule {
167 /** Token to return for this rule */
168 private final IToken fToken;
170 public AccentStringRule(IToken token) {
176 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
178 public IToken evaluate(ICharacterScanner scanner) {
180 int character = scanner.read();
182 if (character == '`') {
185 character = scanner.read();
186 if (character == '\\') {
187 character = scanner.read();
188 } else if (character == '`') {
195 return Token.UNDEFINED;
201 private class PHPWordRule extends WordRule {
202 private StringBuffer fBuffer = new StringBuffer();
204 public PHPWordRule(IWordDetector detector) {
205 super(detector, Token.UNDEFINED);
208 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
209 super(detector, defaultToken);
212 public IToken evaluate(ICharacterScanner scanner) {
213 int c = scanner.read();
214 boolean isVariable = false;
220 return Token.UNDEFINED;
223 if (c == '=') { // <?=
224 return getToken(IPreferenceConstants.PHP_TAG);
226 if (c != 'p' && c != 'P') {
228 return getToken(IPreferenceConstants.PHP_TAG);
231 if (c != 'h' && c != 'H') {
234 return getToken(IPreferenceConstants.PHP_TAG);
237 if (c != 'p' && c != 'P') {
241 return getToken(IPreferenceConstants.PHP_TAG);
243 return getToken(IPreferenceConstants.PHP_TAG);
252 return getToken(IPreferenceConstants.PHP_TAG);
256 return Token.UNDEFINED;
258 if (fDetector.isWordStart((char) c)) {
262 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
264 fBuffer.setLength(0);
266 fBuffer.append((char) c);
268 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
272 return getToken(IPreferenceConstants.PHP_VARIABLE);
274 IToken token = (IToken) fWords.get(fBuffer.toString());
278 if (fDefaultToken.isUndefined())
279 unreadBuffer(scanner);
281 return fDefaultToken;
286 return Token.UNDEFINED;
290 //private PHPColorProvider fColorProvider;
292 private static String[] fgTokenProperties = {
293 IPreferenceConstants.PHP_MULTILINE_COMMENT,
294 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
295 IPreferenceConstants.PHP_TAG,
296 IPreferenceConstants.PHP_KEYWORD,
297 IPreferenceConstants.PHP_FUNCTIONNAME,
298 IPreferenceConstants.PHP_VARIABLE,
299 IPreferenceConstants.PHP_STRING_DQ,
300 IPreferenceConstants.PHP_STRING_SQ,
301 IPreferenceConstants.PHP_TYPE,
302 IPreferenceConstants.PHP_CONSTANT,
303 IPreferenceConstants.PHP_DEFAULT,
304 IPreferenceConstants.PHP_OPERATOR,
305 IPreferenceConstants.PHP_BRACE_OPERATOR,
306 IPreferenceConstants.PHP_KEYWORD_RETURN };
309 * Creates a PHP code scanner
311 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
312 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
313 super(manager, store);
318 * @see AbstractJavaScanner#getTokenProperties()
320 protected String[] getTokenProperties() {
321 return fgTokenProperties;
325 * @see AbstractJavaScanner#createRules()
327 protected List createRules() {
328 List rules = new ArrayList();
329 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
330 // Add rule for single line comments.
331 // rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
332 // rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
333 // Add rule for strings and character constants.
334 // token = getToken(IPreferenceConstants.PHP_STRING_SQ);
335 // rules.add(new SingleQuoteStringRule(token));
336 // token = getToken(IPreferenceConstants.PHP_STRING_DQ);
337 // rules.add(new DoubleQuoteStringRule(token));
338 rules.add(new AccentStringRule(token));
340 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
341 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
342 // Add generic whitespace rule.
343 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
344 // Add word rule for keywords, types, and constants.
345 token = getToken(IPreferenceConstants.PHP_DEFAULT);
346 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
348 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
349 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
350 Token type = getToken(IPreferenceConstants.PHP_TYPE);
351 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
353 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
354 // String strbuffer = null; unused
355 PHPElement elbuffer = null;
357 for (int i = 0; i < buffer.size(); i++) {
358 // while ((buffer != null)
359 // && (!buffer.isEmpty()
360 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
361 elbuffer = (PHPElement) buffer.get(i);
362 if (elbuffer instanceof PHPKeyword) {
363 name = ((PHPKeyword) elbuffer).getName();
364 if (!name.equals("return")) {
365 wordRule.addWord(name, keyword);
367 } else if (elbuffer instanceof PHPFunction) {
368 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
369 } else if (elbuffer instanceof PHPType) {
370 wordRule.addWord(elbuffer.getName(), type);
371 } else if (elbuffer instanceof PHPConstant) {
372 wordRule.addWord(elbuffer.getName(), constant);
376 // Add word rule for keyword 'return'.
377 token = getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
378 wordRule.addWord("return", token);
380 // Add rule for operators and brackets (at the end !)
381 rules.add(new OperatorRule(getToken(IPreferenceConstants.PHP_OPERATOR), getToken(IPreferenceConstants.PHP_BRACE_OPERATOR)));
385 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));