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
10 Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
14 import java.util.ArrayList;
15 import java.util.List;
17 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
18 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
19 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
20 import org.eclipse.jface.text.TextAttribute;
21 import org.eclipse.jface.text.rules.EndOfLineRule;
22 import org.eclipse.jface.text.rules.ICharacterScanner;
23 import org.eclipse.jface.text.rules.IRule;
24 import org.eclipse.jface.text.rules.IToken;
25 import org.eclipse.jface.text.rules.IWordDetector;
26 import org.eclipse.jface.text.rules.MultiLineRule;
27 import org.eclipse.jface.text.rules.RuleBasedScanner;
28 import org.eclipse.jface.text.rules.SingleLineRule;
29 import org.eclipse.jface.text.rules.Token;
30 import org.eclipse.jface.text.rules.WhitespaceRule;
31 import org.eclipse.jface.text.rules.WordRule;
36 public class PHPCodeScanner extends RuleBasedScanner {
41 private IToken variable;
43 private class PHPWordRule extends WordRule {
44 private StringBuffer fBuffer= new StringBuffer();
46 public PHPWordRule(IWordDetector detector) {
47 super(detector, Token.UNDEFINED);
50 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
51 super(detector, defaultToken);
54 public IToken evaluate(ICharacterScanner scanner) {
55 int c = scanner.read();
56 boolean isVariable = false;
57 if (fDetector.isWordStart((char) c)) {
61 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
65 fBuffer.append((char) c);
67 } while (c != scanner.EOF && fDetector.isWordPart((char) c));
73 IToken token = (IToken) fWords.get(fBuffer.toString());
77 if (fDefaultToken.isUndefined())
78 unreadBuffer(scanner);
85 return Token.UNDEFINED;
89 private static String[] fgConstants = { "__LINE__", "__FILE__", "true", "false" };
90 private TextAttribute fComment;
91 private TextAttribute fKeyword;
92 private TextAttribute fType;
93 private TextAttribute fString;
94 private PHPColorProvider fColorProvider;
97 * Creates a Java code scanner
99 public PHPCodeScanner(PHPColorProvider provider) {
101 IToken keyword = new Token(new TextAttribute(provider.getColor(PHPColorProvider.KEYWORD)));
102 IToken type = new Token(new TextAttribute(provider.getColor(PHPColorProvider.TYPE)));
103 IToken string = new Token(new TextAttribute(provider.getColor(PHPColorProvider.STRING)));
104 IToken comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.SINGLE_LINE_COMMENT)));
105 IToken multi_comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT)));
106 IToken other = new Token(new TextAttribute(provider.getColor(PHPColorProvider.DEFAULT)));
108 variable = new Token(new TextAttribute(provider.getColor(PHPColorProvider.VARIABLE)));
111 List rules = new ArrayList();
113 // Add rule for single line comments.
114 rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$
115 // EndOfLineRule endOfLine = new EndOfLineRule("#", comment);
116 // endOfLine.setColumnConstraint(0);
117 rules.add(new EndOfLineRule("#", comment));
119 // Add rule for strings and character constants.
120 rules.add(new SingleLineRule("\"", "\"", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
121 rules.add(new SingleLineRule("'", "'", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
123 // rules.add(new SingleLineRule("//", "//", php_comment));
124 rules.add(new MultiLineRule("/*", "*/", multi_comment));
126 // Add generic whitespace rule.
127 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
129 // Add word rule for keywords, types, and constants.
130 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), other);
131 for (int i = 0; i < PHPKeywords.PHP_KEYWORS.length; i++)
132 wordRule.addWord(PHPKeywords.PHP_KEYWORS[i], keyword);
133 for (int i = 0; i < PHPFunctionNames.FUNCTION_NAMES.length; i++)
134 wordRule.addWord(PHPFunctionNames.FUNCTION_NAMES[i], type);
135 for (int i = 0; i < fgConstants.length; i++)
136 wordRule.addWord(fgConstants[i], type);
139 IRule[] result = new IRule[rules.size()];
140 rules.toArray(result);