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.JavaColorManager;
18 import net.sourceforge.phpeclipse.phpeditor.util.HTMLWordDetector;
19 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
20 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.text.TextAttribute;
24 import org.eclipse.jface.text.rules.ICharacterScanner;
25 import org.eclipse.jface.text.rules.IRule;
26 import org.eclipse.jface.text.rules.IToken;
27 import org.eclipse.jface.text.rules.IWordDetector;
28 import org.eclipse.jface.text.rules.RuleBasedScanner;
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 RuleBasedScanner {
39 private static String[] fgKeywords = {
42 private static String[] fgTypes = {
45 private IToken keyword;
47 private class HTMLWordRule extends WordRule {
48 private StringBuffer fBuffer = new StringBuffer();
50 public HTMLWordRule(IWordDetector detector) {
51 super(detector, Token.UNDEFINED);
54 public HTMLWordRule(IWordDetector detector, IToken defaultToken) {
55 super(detector, defaultToken);
58 public IToken evaluate(ICharacterScanner scanner) {
59 int c = scanner.read();
60 boolean tagBegin = false;
61 if (fDetector.isWordStart((char) c)) {
65 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
69 fBuffer.append((char) c);
72 fBuffer.append((char) c);
76 if (c == '/' && (fBuffer.length() > 2) ) {
79 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
85 IToken token = (IToken) fWords.get(fBuffer.toString());
89 if (fDefaultToken.isUndefined())
90 unreadBuffer(scanner);
97 return Token.UNDEFINED;
102 //private static String[] fgConstants = { "__LINE__", "__FILE__", "true", "false" };
103 private TextAttribute fComment;
104 private TextAttribute fKeyword;
105 private TextAttribute fType;
106 private TextAttribute fString;
107 private PHPColorProvider fColorProvider;
110 * Creates a Java code scanner
112 public HTMLCodeScanner(JavaColorManager provider, IPreferenceStore store) {
114 keyword = new Token(new TextAttribute(provider.getColor(PHPColorProvider.KEYWORD)));
115 IToken type = new Token(new TextAttribute(provider.getColor(PHPColorProvider.FUNCTION_NAME)));
116 IToken string = new Token(new TextAttribute(provider.getColor(PHPColorProvider.STRING)));
117 IToken comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.SINGLE_LINE_COMMENT)));
118 IToken multi_comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT)));
119 IToken other = new Token(new TextAttribute(provider.getColor(PHPColorProvider.DEFAULT)));
121 //variable = new Token(new TextAttribute(provider.getColor(PHPColorProvider.VARIABLE)));
123 List rules = new ArrayList();
125 // Add rule for single line comments.
126 //rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$
127 //rules.add(new EndOfLineRule("#", comment));
129 // Add rule for strings and character constants.
130 rules.add(new SingleLineRule("\"", "\"", string)); //$NON-NLS-2$ //$NON-NLS-1$
131 // rules.add(new SingleLineRule("'", "'", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
133 // rules.add(new SingleLineRule("//", "//", php_comment));
134 // rules.add(new MultiLineRule("/*", "*/", multi_comment));
136 // Add generic whitespace rule.
137 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
139 // Add word rule for keywords, types, and constants.
140 HTMLWordRule wordRule = new HTMLWordRule(new HTMLWordDetector(), other);
141 for (int i = 0; i < fgKeywords.length; i++)
142 wordRule.addWord(fgKeywords[i], keyword);
143 for (int i = 0; i < fgTypes.length; i++)
144 wordRule.addWord(fgTypes[i], type);
145 // for (int i = 0; i < fgConstants.length; i++)
146 // wordRule.addWord(fgConstants[i], type);
149 IRule[] result = new IRule[rules.size()];
150 rules.toArray(result);