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.ui.text.IColorManager;
19 import net.sourceforge.phpeclipse.IPreferenceConstants;
20 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
21 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.jface.text.rules.ICharacterScanner;
25 import org.eclipse.jface.text.rules.IToken;
26 import org.eclipse.jface.text.rules.IWordDetector;
27 import org.eclipse.jface.text.rules.MultiLineRule;
28 import org.eclipse.jface.text.rules.Token;
29 import org.eclipse.jface.text.rules.WhitespaceRule;
30 import org.eclipse.jface.text.rules.WordRule;
35 public class SmartyCodeScanner extends AbstractJavaScanner {
36 public static String[] SMARTY_KEYWORDS =
54 public static String[] SMARTY_FUNCTION_NAMES =
73 private class SmartyWordRule extends WordRule {
74 private StringBuffer fBuffer = new StringBuffer();
76 public SmartyWordRule(IWordDetector detector) {
77 super(detector, Token.UNDEFINED);
80 public SmartyWordRule(IWordDetector detector, IToken defaultToken) {
81 super(detector, defaultToken);
84 public IToken evaluate(ICharacterScanner scanner) {
85 int c = scanner.read();
86 boolean isVariable = false;
92 return getToken(IPreferenceConstants.PHP_TAG);
95 return getToken(IPreferenceConstants.PHP_TAG);
97 if (fDetector.isWordStart((char) c)) {
101 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
103 fBuffer.setLength(0);
105 fBuffer.append((char) c);
107 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
111 return getToken(IPreferenceConstants.PHP_VARIABLE);
113 IToken token = (IToken) fWords.get(fBuffer.toString());
117 if (fDefaultToken.isUndefined())
118 unreadBuffer(scanner);
120 return fDefaultToken;
125 return Token.UNDEFINED;
129 private static String[] fgTokenProperties =
131 IPreferenceConstants.PHP_MULTILINE_COMMENT,
132 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
133 IPreferenceConstants.PHP_TAG,
134 IPreferenceConstants.PHP_KEYWORD,
135 IPreferenceConstants.PHP_FUNCTIONNAME,
136 IPreferenceConstants.PHP_VARIABLE,
137 IPreferenceConstants.PHP_STRING,
138 IPreferenceConstants.PHP_TYPE,
139 IPreferenceConstants.PHP_CONSTANT,
140 IPreferenceConstants.PHP_DEFAULT };
142 * Creates a PHP code scanner
144 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
145 public SmartyCodeScanner(IColorManager manager, IPreferenceStore store) {
146 super(manager, store);
151 * @see AbstractJavaScanner#getTokenProperties()
153 protected String[] getTokenProperties() {
154 return fgTokenProperties;
157 * @see AbstractJavaScanner#createRules()
159 protected List createRules() {
160 List rules = new ArrayList();
161 // Add rule for strings and character constants.
162 Token token = getToken(IPreferenceConstants.PHP_STRING);
163 rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
165 // Add generic whitespace rule.
166 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
167 // Add word rule for keywords, types, and constants.
168 token = getToken(IPreferenceConstants.PHP_DEFAULT);
170 SmartyWordRule wordRule = new SmartyWordRule(new PHPWordDetector(), token);
172 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
173 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
174 // Token type = getToken(IPreferenceConstants.PHP_TYPE);
175 // Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
178 for (int i = 0; i < SMARTY_KEYWORDS.length; i++) {
179 wordRule.addWord(SMARTY_KEYWORDS[i], keyword);
182 for (int i = 0; i < SMARTY_FUNCTION_NAMES.length; i++) {
183 wordRule.addWord(SMARTY_FUNCTION_NAMES[i], functionName);
186 // ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
187 // PHPElement elbuffer = null;
188 // for (int i = 0; i < buffer.size(); i++) {
190 // elbuffer = (PHPElement) buffer.get(i);
191 // if (elbuffer instanceof PHPKeyword)
192 // wordRule.addWord(((PHPKeyword) elbuffer).getName(), keyword);
193 // // if (elbuffer instanceof PHPFunction)
194 // // wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
195 // if (elbuffer instanceof PHPType)
196 // wordRule.addWord(elbuffer.getName(), type);
197 // if (elbuffer instanceof PHPConstant)
198 // wordRule.addWord(elbuffer.getName(), constant);
201 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));