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.phpeclipse.phpeditor.util.HTMLWordDetector;
18 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
19 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.TextAttribute;
22 import org.eclipse.jface.text.rules.ICharacterScanner;
23 import org.eclipse.jface.text.rules.IRule;
24 import org.eclipse.jface.text.rules.IToken;
25 import org.eclipse.jface.text.rules.IWordDetector;
26 import org.eclipse.jface.text.rules.RuleBasedScanner;
27 import org.eclipse.jface.text.rules.SingleLineRule;
28 import org.eclipse.jface.text.rules.Token;
29 import org.eclipse.jface.text.rules.WhitespaceRule;
30 import org.eclipse.jface.text.rules.WordRule;
33 * A Java code scanner.
35 public class HTMLCodeScanner extends RuleBasedScanner {
37 private static String[] fgKeywords = {
40 private static String[] fgTypes = {
43 private IToken keyword;
45 private class HTMLWordRule extends WordRule {
46 private StringBuffer fBuffer = new StringBuffer();
48 public HTMLWordRule(IWordDetector detector) {
49 super(detector, Token.UNDEFINED);
52 public HTMLWordRule(IWordDetector detector, IToken defaultToken) {
53 super(detector, defaultToken);
56 public IToken evaluate(ICharacterScanner scanner) {
57 int c = scanner.read();
58 boolean tagBegin = false;
59 if (fDetector.isWordStart((char) c)) {
63 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
67 fBuffer.append((char) c);
70 fBuffer.append((char) c);
74 if (c == '/' && (fBuffer.length() > 2) ) {
77 } while (c != scanner.EOF && fDetector.isWordPart((char) c));
83 IToken token = (IToken) fWords.get(fBuffer.toString());
87 if (fDefaultToken.isUndefined())
88 unreadBuffer(scanner);
95 return Token.UNDEFINED;
100 //private static String[] fgConstants = { "__LINE__", "__FILE__", "true", "false" };
101 private TextAttribute fComment;
102 private TextAttribute fKeyword;
103 private TextAttribute fType;
104 private TextAttribute fString;
105 private PHPColorProvider fColorProvider;
108 * Creates a Java code scanner
110 public HTMLCodeScanner(PHPColorProvider provider) {
112 keyword = new Token(new TextAttribute(provider.getColor(PHPColorProvider.KEYWORD)));
113 IToken type = new Token(new TextAttribute(provider.getColor(PHPColorProvider.TYPE)));
114 IToken string = new Token(new TextAttribute(provider.getColor(PHPColorProvider.STRING)));
115 IToken comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.SINGLE_LINE_COMMENT)));
116 IToken multi_comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT)));
117 IToken other = new Token(new TextAttribute(provider.getColor(PHPColorProvider.DEFAULT)));
119 //variable = new Token(new TextAttribute(provider.getColor(PHPColorProvider.VARIABLE)));
121 List rules = new ArrayList();
123 // Add rule for single line comments.
124 //rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$
125 //rules.add(new EndOfLineRule("#", comment));
127 // Add rule for strings and character constants.
128 rules.add(new SingleLineRule("\"", "\"", string)); //$NON-NLS-2$ //$NON-NLS-1$
129 // rules.add(new SingleLineRule("'", "'", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
131 // rules.add(new SingleLineRule("//", "//", php_comment));
132 // rules.add(new MultiLineRule("/*", "*/", multi_comment));
134 // Add generic whitespace rule.
135 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
137 // Add word rule for keywords, types, and constants.
138 HTMLWordRule wordRule = new HTMLWordRule(new HTMLWordDetector(), other);
139 for (int i = 0; i < fgKeywords.length; i++)
140 wordRule.addWord(fgKeywords[i], keyword);
141 for (int i = 0; i < fgTypes.length; i++)
142 wordRule.addWord(fgTypes[i], type);
143 // for (int i = 0; i < fgConstants.length; i++)
144 // wordRule.addWord(fgConstants[i], type);
147 IRule[] result = new IRule[rules.size()];
148 rules.toArray(result);