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 == '>') {
164 protected class SingleQuoteStringRule implements IRule {
166 /** Token to return for this rule */
167 private final IToken fToken;
169 public SingleQuoteStringRule(IToken token) {
175 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
177 public IToken evaluate(ICharacterScanner scanner) {
179 int character = scanner.read();
181 if (character=='\'') {
184 character = scanner.read();
185 if (character == '\\') {
186 character = scanner.read();
187 } else if (character == '\'') {
194 return Token.UNDEFINED;
200 protected class AccentStringRule implements IRule {
202 /** Token to return for this rule */
203 private final IToken fToken;
205 public AccentStringRule(IToken token) {
211 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
213 public IToken evaluate(ICharacterScanner scanner) {
215 int character = scanner.read();
217 if (character=='`') {
220 character = scanner.read();
221 if (character == '\\') {
222 character = scanner.read();
223 } else if (character == '`') {
230 return Token.UNDEFINED;
236 protected class DoubleQuoteStringRule implements IRule {
238 /** Token to return for this rule */
239 private final IToken fToken;
241 public DoubleQuoteStringRule(IToken token) {
247 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
249 public IToken evaluate(ICharacterScanner scanner) {
251 int character = scanner.read();
253 if (character=='"') {
256 character = scanner.read();
257 if (character == '\\') {
258 character = scanner.read();
259 } else if (character == '"') {
266 return Token.UNDEFINED;
272 private class PHPWordRule extends WordRule {
273 private StringBuffer fBuffer = new StringBuffer();
275 public PHPWordRule(IWordDetector detector) {
276 super(detector, Token.UNDEFINED);
279 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
280 super(detector, defaultToken);
283 public IToken evaluate(ICharacterScanner scanner) {
284 int c = scanner.read();
285 boolean isVariable = false;
291 return Token.UNDEFINED;
294 if (c == '=') { // <?=
295 return getToken(IPreferenceConstants.PHP_TAG);
297 if (c != 'p' && c != 'P') {
299 return getToken(IPreferenceConstants.PHP_TAG);
302 if (c != 'h' && c != 'H') {
305 return getToken(IPreferenceConstants.PHP_TAG);
308 if (c != 'p' && c != 'P') {
312 return getToken(IPreferenceConstants.PHP_TAG);
314 return getToken(IPreferenceConstants.PHP_TAG);
323 return getToken(IPreferenceConstants.PHP_TAG);
327 return Token.UNDEFINED;
329 if (fDetector.isWordStart((char) c)) {
333 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
335 fBuffer.setLength(0);
337 fBuffer.append((char) c);
339 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
343 return getToken(IPreferenceConstants.PHP_VARIABLE);
345 IToken token = (IToken) fWords.get(fBuffer.toString());
349 if (fDefaultToken.isUndefined())
350 unreadBuffer(scanner);
352 return fDefaultToken;
357 return Token.UNDEFINED;
361 //private PHPColorProvider fColorProvider;
363 private static String[] fgTokenProperties = {
364 IPreferenceConstants.PHP_MULTILINE_COMMENT,
365 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
366 IPreferenceConstants.PHP_TAG,
367 IPreferenceConstants.PHP_KEYWORD,
368 IPreferenceConstants.PHP_FUNCTIONNAME,
369 IPreferenceConstants.PHP_VARIABLE,
370 IPreferenceConstants.PHP_STRING,
371 IPreferenceConstants.PHP_TYPE,
372 IPreferenceConstants.PHP_CONSTANT,
373 IPreferenceConstants.PHP_DEFAULT,
374 IPreferenceConstants.PHP_OPERATOR,
375 IPreferenceConstants.PHP_BRACE_OPERATOR,
376 IPreferenceConstants.PHP_KEYWORD_RETURN };
379 * Creates a PHP code scanner
381 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
382 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
383 super(manager, store);
388 * @see AbstractJavaScanner#getTokenProperties()
390 protected String[] getTokenProperties() {
391 return fgTokenProperties;
395 * @see AbstractJavaScanner#createRules()
397 protected List createRules() {
398 List rules = new ArrayList();
399 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
400 // Add rule for single line comments.
401 rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
402 rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
403 // Add rule for strings and character constants.
404 token = getToken(IPreferenceConstants.PHP_STRING);
405 // rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
406 // rules.add(new MultiLineRule("`", "`", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
407 // rules.add(new MultiLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
408 rules.add(new SingleQuoteStringRule(token));
409 rules.add(new DoubleQuoteStringRule(token));
410 rules.add(new AccentStringRule(token));
412 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
413 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
414 // Add generic whitespace rule.
415 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
416 // Add word rule for keywords, types, and constants.
417 token = getToken(IPreferenceConstants.PHP_DEFAULT);
418 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
420 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
421 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
422 Token type = getToken(IPreferenceConstants.PHP_TYPE);
423 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
425 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
426 // String strbuffer = null; unused
427 PHPElement elbuffer = null;
429 for (int i = 0; i < buffer.size(); i++) {
430 // while ((buffer != null)
431 // && (!buffer.isEmpty()
432 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
433 elbuffer = (PHPElement) buffer.get(i);
434 if (elbuffer instanceof PHPKeyword) {
435 name = ((PHPKeyword) elbuffer).getName();
436 if (!name.equals("return")) {
437 wordRule.addWord(name, keyword);
439 } else if (elbuffer instanceof PHPFunction) {
440 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
441 } else if (elbuffer instanceof PHPType) {
442 wordRule.addWord(elbuffer.getName(), type);
443 } else if (elbuffer instanceof PHPConstant) {
444 wordRule.addWord(elbuffer.getName(), constant);
448 // Add word rule for keyword 'return'.
449 token = getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
450 wordRule.addWord("return", token);
452 // Add rule for operators and brackets (at the end !)
453 rules.add(new OperatorRule(getToken(IPreferenceConstants.PHP_OPERATOR), getToken(IPreferenceConstants.PHP_BRACE_OPERATOR)));
457 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));