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 = {
47 IPreferenceConstants.PHP_MULTILINE_COMMENT,
48 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
49 IPreferenceConstants.PHP_TAG, IPreferenceConstants.PHP_KEYWORD,
50 IPreferenceConstants.PHP_FUNCTIONNAME,
51 IPreferenceConstants.PHP_VARIABLE,
52 IPreferenceConstants.PHP_VARIABLE_DOLLAR,
53 IPreferenceConstants.PHP_STRING_DQ,
54 IPreferenceConstants.PHP_STRING_SQ, IPreferenceConstants.PHP_TYPE,
55 IPreferenceConstants.PHP_CONSTANT,
56 IPreferenceConstants.PHP_DEFAULT,
57 IPreferenceConstants.PHP_OPERATOR,
58 IPreferenceConstants.PHP_BRACE_OPERATOR,
59 IPreferenceConstants.PHP_KEYWORD_RETURN };
62 * @see AbstractJavaScanner#getTokenProperties()
64 protected String[] getTokenProperties() {
65 return fgTokenProperties;
68 private class HTMLWordRule extends WordRule {
69 private StringBuffer fBuffer = new StringBuffer();
71 public HTMLWordRule(IWordDetector detector) {
72 super(detector, Token.UNDEFINED);
75 public HTMLWordRule(IWordDetector detector, IToken defaultToken) {
76 super(detector, defaultToken);
79 public IToken evaluate(ICharacterScanner scanner) {
80 int c = scanner.read();
81 boolean tagBegin = false;
82 if (fDetector.isWordStart((char) c)) {
86 if (fColumn == UNDEFINED
87 || (fColumn == scanner.getColumn() - 1)) {
91 fBuffer.append((char) c);
94 fBuffer.append((char) c);
98 if (c == '/' && (fBuffer.length() > 2)) {
101 } while (c != ICharacterScanner.EOF
102 && fDetector.isWordPart((char) c));
106 return getToken(IPreferenceConstants.PHP_KEYWORD);
108 IToken token = (IToken) fWords.get(fBuffer.toString());
112 if (fDefaultToken.isUndefined())
113 unreadBuffer(scanner);
115 return fDefaultToken;
120 return Token.UNDEFINED;
125 // private static String[] fgConstants = { "__LINE__", "__FILE__", "true",
127 //private TextAttribute fComment;
129 //private TextAttribute fKeyword;
131 //private TextAttribute fType;
133 //private TextAttribute fString;
135 //private PHPColorProvider fColorProvider;
138 * Creates a Java code scanner
140 public HTMLCodeScanner(IColorManager manager, IPreferenceStore store) {
141 super(manager, store);
146 * @see AbstractJavaScanner#createRules()
148 protected List createRules() {
149 List rules = new ArrayList();
151 // keyword = new Token(new
152 // TextAttribute(provider.getColor(PHPColorProvider.KEYWORD)));
153 // IToken type = new Token(new
154 // TextAttribute(provider.getColor(PHPColorProvider.FUNCTION_NAME)));
155 // IToken string = new Token(new
156 // TextAttribute(provider.getColor(PHPColorProvider.STRING_DQ)));
157 // IToken comment = new Token(new
158 // TextAttribute(provider.getColor(PHPColorProvider.SINGLE_LINE_COMMENT)));
159 // IToken multi_comment = new Token(new
160 // TextAttribute(provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT)));
161 // IToken other = new Token(new
162 // TextAttribute(provider.getColor(PHPColorProvider.DEFAULT)));
164 // variable = new Token(new
165 // TextAttribute(provider.getColor(PHPColorProvider.VARIABLE)));
167 // Add rule for single line comments.
168 // rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$
169 // rules.add(new EndOfLineRule("#", comment));
171 // Add rule for strings and character constants.
172 rules.add(new SingleLineRule(
173 "\"", "\"", getToken(IPreferenceConstants.PHP_STRING_DQ))); //$NON-NLS-2$ //$NON-NLS-1$
174 // rules.add(new SingleLineRule("'", "'", string, '\\')); //$NON-NLS-2$
177 // rules.add(new SingleLineRule("//", "//", php_comment));
178 // rules.add(new MultiLineRule("/*", "*/", multi_comment));
180 // Add generic whitespace rule.
181 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
183 // Add word rule for keywords, types, and constants.
184 HTMLWordRule wordRule = new HTMLWordRule(new HTMLWordDetector(),
185 getToken(IPreferenceConstants.PHP_DEFAULT));
186 // for (int i = 0; i < fgKeywords.length; i++)
187 // wordRule.addWord(fgKeywords[i], keyword);
188 // for (int i = 0; i < fgTypes.length; i++)
189 // wordRule.addWord(fgTypes[i], type);
192 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));