Commiting more changes to fix RSE issues with PHP projects.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / HTMLCodeScanner.java
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
7
8  Contributors:
9  IBM Corporation - Initial implementation
10  www.phpeclipse.de
11  **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
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;
23
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;
33
34 /**
35  * A HTML code scanner.
36  */
37 public class HTMLCodeScanner extends AbstractJavaScanner {
38
39         // private static String[] fgKeywords = {
40         // };
41         //
42         // private static String[] fgTypes = {
43         // };
44
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 };
60
61         /*
62          * @see AbstractJavaScanner#getTokenProperties()
63          */
64         protected String[] getTokenProperties() {
65                 return fgTokenProperties;
66         }
67
68         private class HTMLWordRule extends WordRule {
69                 private StringBuffer fBuffer = new StringBuffer();
70
71                 public HTMLWordRule(IWordDetector detector) {
72                         super(detector, Token.UNDEFINED);
73                 }
74
75                 public HTMLWordRule(IWordDetector detector, IToken defaultToken) {
76                         super(detector, defaultToken);
77                 }
78
79                 public IToken evaluate(ICharacterScanner scanner) {
80                         int c = scanner.read();
81                         boolean tagBegin = false;
82                         if (fDetector.isWordStart((char) c)) {
83                                 if (c == '<') {
84                                         tagBegin = true;
85                                 }
86                                 if (fColumn == UNDEFINED
87                                                 || (fColumn == scanner.getColumn() - 1)) {
88
89                                         fBuffer.setLength(0);
90                                         do {
91                                                 fBuffer.append((char) c);
92                                                 c = scanner.read();
93                                                 if (c == '>') {
94                                                         fBuffer.append((char) c);
95                                                         c = scanner.read();
96                                                         break;
97                                                 }
98                                                 if (c == '/' && (fBuffer.length() > 2)) {
99                                                         break;
100                                                 }
101                                         } while (c != ICharacterScanner.EOF
102                                                         && fDetector.isWordPart((char) c));
103                                         scanner.unread();
104
105                                         if (tagBegin) {
106                                                 return getToken(IPreferenceConstants.PHP_KEYWORD);
107                                         }
108                                         IToken token = (IToken) fWords.get(fBuffer.toString());
109                                         if (token != null)
110                                                 return token;
111
112                                         if (fDefaultToken.isUndefined())
113                                                 unreadBuffer(scanner);
114
115                                         return fDefaultToken;
116                                 }
117                         }
118
119                         scanner.unread();
120                         return Token.UNDEFINED;
121                 }
122
123         }
124
125         // private static String[] fgConstants = { "__LINE__", "__FILE__", "true",
126         // "false" };
127         private TextAttribute fComment;
128
129         private TextAttribute fKeyword;
130
131         private TextAttribute fType;
132
133         private TextAttribute fString;
134
135         private PHPColorProvider fColorProvider;
136
137         /**
138          * Creates a Java code scanner
139          */
140         public HTMLCodeScanner(IColorManager manager, IPreferenceStore store) {
141                 super(manager, store);
142                 initialize();
143         }
144
145         /*
146          * @see AbstractJavaScanner#createRules()
147          */
148         protected List createRules() {
149                 List rules = new ArrayList();
150
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)));
163
164                 // variable = new Token(new
165                 // TextAttribute(provider.getColor(PHPColorProvider.VARIABLE)));
166
167                 // Add rule for single line comments.
168                 // rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$
169                 // rules.add(new EndOfLineRule("#", comment));
170
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$
175                 // //$NON-NLS-1$
176
177                 // rules.add(new SingleLineRule("//", "//", php_comment));
178                 // rules.add(new MultiLineRule("/*", "*/", multi_comment));
179
180                 // Add generic whitespace rule.
181                 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
182
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);
190                 rules.add(wordRule);
191
192                 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));
193                 return rules;
194         }
195 }