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.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_KEYWORD_RETURN};
63 * @see AbstractJavaScanner#getTokenProperties()
65 protected String[] getTokenProperties() {
66 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 || (fColumn == scanner.getColumn() - 1)) {
90 fBuffer.append((char) c);
93 fBuffer.append((char) c);
97 if (c == '/' && (fBuffer.length() > 2) ) {
100 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
104 return getToken(IPreferenceConstants.PHP_KEYWORD);
106 IToken token = (IToken) fWords.get(fBuffer.toString());
110 if (fDefaultToken.isUndefined())
111 unreadBuffer(scanner);
113 return fDefaultToken;
118 return Token.UNDEFINED;
123 //private static String[] fgConstants = { "__LINE__", "__FILE__", "true", "false" };
124 private TextAttribute fComment;
125 private TextAttribute fKeyword;
126 private TextAttribute fType;
127 private TextAttribute fString;
128 private PHPColorProvider fColorProvider;
131 * Creates a Java code scanner
133 public HTMLCodeScanner(IColorManager manager, IPreferenceStore store) {
134 super(manager, store);
139 * @see AbstractJavaScanner#createRules()
141 protected List createRules() {
142 List rules = new ArrayList();
144 // keyword = new Token(new TextAttribute(provider.getColor(PHPColorProvider.KEYWORD)));
145 // IToken type = new Token(new TextAttribute(provider.getColor(PHPColorProvider.FUNCTION_NAME)));
146 // IToken string = new Token(new TextAttribute(provider.getColor(PHPColorProvider.STRING)));
147 // IToken comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.SINGLE_LINE_COMMENT)));
148 // IToken multi_comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT)));
149 // IToken other = new Token(new TextAttribute(provider.getColor(PHPColorProvider.DEFAULT)));
151 //variable = new Token(new TextAttribute(provider.getColor(PHPColorProvider.VARIABLE)));
154 // Add rule for single line comments.
155 //rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$
156 //rules.add(new EndOfLineRule("#", comment));
158 // Add rule for strings and character constants.
159 rules.add(new SingleLineRule("\"", "\"", getToken(IPreferenceConstants.PHP_STRING))); //$NON-NLS-2$ //$NON-NLS-1$
160 // rules.add(new SingleLineRule("'", "'", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
162 // rules.add(new SingleLineRule("//", "//", php_comment));
163 // rules.add(new MultiLineRule("/*", "*/", multi_comment));
165 // Add generic whitespace rule.
166 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
168 // Add word rule for keywords, types, and constants.
169 HTMLWordRule wordRule = new HTMLWordRule(new HTMLWordDetector(), getToken(IPreferenceConstants.PHP_DEFAULT));
170 // for (int i = 0; i < fgKeywords.length; i++)
171 // wordRule.addWord(fgKeywords[i], keyword);
172 // for (int i = 0; i < fgTypes.length; i++)
173 // wordRule.addWord(fgTypes[i], type);
176 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));