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.util.HTMLWordDetector;
21 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
22 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.text.TextAttribute;
26 import org.eclipse.jface.text.rules.ICharacterScanner;
27 import org.eclipse.jface.text.rules.IToken;
28 import org.eclipse.jface.text.rules.IWordDetector;
29 import org.eclipse.jface.text.rules.SingleLineRule;
30 import org.eclipse.jface.text.rules.Token;
31 import org.eclipse.jface.text.rules.WhitespaceRule;
32 import org.eclipse.jface.text.rules.WordRule;
35 * A HTML code scanner.
37 public class HTMLCodeScanner extends AbstractJavaScanner {
39 // private static String[] fgKeywords = {
42 // private static String[] fgTypes = {
45 // private IToken keyword;
46 private static String[] fgTokenProperties =
48 IPreferenceConstants.PHP_MULTILINE_COMMENT,
49 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
50 IPreferenceConstants.PHP_TAG,
51 IPreferenceConstants.PHP_KEYWORD,
52 IPreferenceConstants.PHP_FUNCTIONNAME,
53 IPreferenceConstants.PHP_VARIABLE,
54 IPreferenceConstants.PHP_STRING,
55 IPreferenceConstants.PHP_TYPE,
56 IPreferenceConstants.PHP_CONSTANT,
57 IPreferenceConstants.PHP_DEFAULT,
58 IPreferenceConstants.PHP_OPERATOR,
59 IPreferenceConstants.PHP_BRACE_OPERATOR,
60 IPreferenceConstants.PHP_KEYWORD_RETURN};
62 * @see AbstractJavaScanner#getTokenProperties()
64 protected String[] getTokenProperties() {
65 return fgTokenProperties;
67 private class HTMLWordRule extends WordRule {
68 private StringBuffer fBuffer = new StringBuffer();
70 public HTMLWordRule(IWordDetector detector) {
71 super(detector, Token.UNDEFINED);
74 public HTMLWordRule(IWordDetector detector, IToken defaultToken) {
75 super(detector, defaultToken);
78 public IToken evaluate(ICharacterScanner scanner) {
79 int c = scanner.read();
80 boolean tagBegin = false;
81 if (fDetector.isWordStart((char) c)) {
85 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
89 fBuffer.append((char) c);
92 fBuffer.append((char) c);
96 if (c == '/' && (fBuffer.length() > 2) ) {
99 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
103 return getToken(IPreferenceConstants.PHP_KEYWORD);
105 IToken token = (IToken) fWords.get(fBuffer.toString());
109 if (fDefaultToken.isUndefined())
110 unreadBuffer(scanner);
112 return fDefaultToken;
117 return Token.UNDEFINED;
122 //private static String[] fgConstants = { "__LINE__", "__FILE__", "true", "false" };
123 private TextAttribute fComment;
124 private TextAttribute fKeyword;
125 private TextAttribute fType;
126 private TextAttribute fString;
127 private PHPColorProvider fColorProvider;
130 * Creates a Java code scanner
132 public HTMLCodeScanner(IColorManager manager, IPreferenceStore store) {
133 super(manager, store);
138 * @see AbstractJavaScanner#createRules()
140 protected List createRules() {
141 List rules = new ArrayList();
143 // keyword = new Token(new TextAttribute(provider.getColor(PHPColorProvider.KEYWORD)));
144 // IToken type = new Token(new TextAttribute(provider.getColor(PHPColorProvider.FUNCTION_NAME)));
145 // IToken string = new Token(new TextAttribute(provider.getColor(PHPColorProvider.STRING)));
146 // IToken comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.SINGLE_LINE_COMMENT)));
147 // IToken multi_comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT)));
148 // IToken other = new Token(new TextAttribute(provider.getColor(PHPColorProvider.DEFAULT)));
150 //variable = new Token(new TextAttribute(provider.getColor(PHPColorProvider.VARIABLE)));
153 // Add rule for single line comments.
154 //rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$
155 //rules.add(new EndOfLineRule("#", comment));
157 // Add rule for strings and character constants.
158 rules.add(new SingleLineRule("\"", "\"", getToken(IPreferenceConstants.PHP_STRING))); //$NON-NLS-2$ //$NON-NLS-1$
159 // rules.add(new SingleLineRule("'", "'", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
161 // rules.add(new SingleLineRule("//", "//", php_comment));
162 // rules.add(new MultiLineRule("/*", "*/", multi_comment));
164 // Add generic whitespace rule.
165 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
167 // Add word rule for keywords, types, and constants.
168 HTMLWordRule wordRule = new HTMLWordRule(new HTMLWordDetector(), getToken(IPreferenceConstants.PHP_DEFAULT));
169 // for (int i = 0; i < fgKeywords.length; i++)
170 // wordRule.addWord(fgKeywords[i], keyword);
171 // for (int i = 0; i < fgTypes.length; i++)
172 // wordRule.addWord(fgTypes[i], type);
175 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));