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.internal.ui.text.JavaColorManager;
19 import net.sourceforge.phpdt.ui.text.IColorManager;
20 import net.sourceforge.phpeclipse.IPreferenceConstants;
21 import net.sourceforge.phpeclipse.phpeditor.util.HTMLWordDetector;
22 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
23 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.text.TextAttribute;
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.SingleLineRule;
32 import org.eclipse.jface.text.rules.Token;
33 import org.eclipse.jface.text.rules.WhitespaceRule;
34 import org.eclipse.jface.text.rules.WordRule;
37 * A HTML code scanner.
39 public class HTMLCodeScanner extends AbstractJavaScanner {
41 // private static String[] fgKeywords = {
44 // private static String[] fgTypes = {
47 // private IToken keyword;
48 private static String[] fgTokenProperties =
50 IPreferenceConstants.PHP_MULTILINE_COMMENT,
51 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
52 IPreferenceConstants.PHP_TAG,
53 IPreferenceConstants.PHP_KEYWORD,
54 IPreferenceConstants.PHP_FUNCTIONNAME,
55 IPreferenceConstants.PHP_VARIABLE,
56 IPreferenceConstants.PHP_STRING,
57 IPreferenceConstants.PHP_TYPE,
58 IPreferenceConstants.PHP_CONSTANT,
59 IPreferenceConstants.PHP_DEFAULT,
60 IPreferenceConstants.PHP_OPERATOR,
61 IPreferenceConstants.PHP_BRACE_OPERATOR,
62 IPreferenceConstants.PHP_KEYWORD_RETURN};
64 * @see AbstractJavaScanner#getTokenProperties()
66 protected String[] getTokenProperties() {
67 return fgTokenProperties;
69 private class HTMLWordRule extends WordRule {
70 private StringBuffer fBuffer = new StringBuffer();
72 public HTMLWordRule(IWordDetector detector) {
73 super(detector, Token.UNDEFINED);
76 public HTMLWordRule(IWordDetector detector, IToken defaultToken) {
77 super(detector, defaultToken);
80 public IToken evaluate(ICharacterScanner scanner) {
81 int c = scanner.read();
82 boolean tagBegin = false;
83 if (fDetector.isWordStart((char) c)) {
87 if (fColumn == UNDEFINED || (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 && fDetector.isWordPart((char) c));
105 return getToken(IPreferenceConstants.PHP_KEYWORD);
107 IToken token = (IToken) fWords.get(fBuffer.toString());
111 if (fDefaultToken.isUndefined())
112 unreadBuffer(scanner);
114 return fDefaultToken;
119 return Token.UNDEFINED;
124 //private static String[] fgConstants = { "__LINE__", "__FILE__", "true", "false" };
125 private TextAttribute fComment;
126 private TextAttribute fKeyword;
127 private TextAttribute fType;
128 private TextAttribute fString;
129 private PHPColorProvider fColorProvider;
132 * Creates a Java code scanner
134 public HTMLCodeScanner(IColorManager manager, IPreferenceStore store) {
135 super(manager, store);
140 * @see AbstractJavaScanner#createRules()
142 protected List createRules() {
143 List rules = new ArrayList();
145 // keyword = new Token(new TextAttribute(provider.getColor(PHPColorProvider.KEYWORD)));
146 // IToken type = new Token(new TextAttribute(provider.getColor(PHPColorProvider.FUNCTION_NAME)));
147 // IToken string = new Token(new TextAttribute(provider.getColor(PHPColorProvider.STRING)));
148 // IToken comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.SINGLE_LINE_COMMENT)));
149 // IToken multi_comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT)));
150 // IToken other = new Token(new TextAttribute(provider.getColor(PHPColorProvider.DEFAULT)));
152 //variable = new Token(new TextAttribute(provider.getColor(PHPColorProvider.VARIABLE)));
155 // Add rule for single line comments.
156 //rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$
157 //rules.add(new EndOfLineRule("#", comment));
159 // Add rule for strings and character constants.
160 rules.add(new SingleLineRule("\"", "\"", getToken(IPreferenceConstants.PHP_STRING))); //$NON-NLS-2$ //$NON-NLS-1$
161 // rules.add(new SingleLineRule("'", "'", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
163 // rules.add(new SingleLineRule("//", "//", php_comment));
164 // rules.add(new MultiLineRule("/*", "*/", multi_comment));
166 // Add generic whitespace rule.
167 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
169 // Add word rule for keywords, types, and constants.
170 HTMLWordRule wordRule = new HTMLWordRule(new HTMLWordDetector(), getToken(IPreferenceConstants.PHP_DEFAULT));
171 // for (int i = 0; i < fgKeywords.length; i++)
172 // wordRule.addWord(fgKeywords[i], keyword);
173 // for (int i = 0; i < fgTypes.length; i++)
174 // wordRule.addWord(fgTypes[i], type);
177 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));