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_VARIABLE_DOLLAR,
138 IPreferenceConstants.PHP_STRING_DQ,
139 IPreferenceConstants.PHP_STRING_SQ,
140 IPreferenceConstants.PHP_TYPE,
141 IPreferenceConstants.PHP_CONSTANT,
142 IPreferenceConstants.PHP_DEFAULT };
144 * Creates a PHP code scanner
146 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
147 public SmartyCodeScanner(IColorManager manager, IPreferenceStore store) {
148 super(manager, store);
153 * @see AbstractJavaScanner#getTokenProperties()
155 protected String[] getTokenProperties() {
156 return fgTokenProperties;
159 * @see AbstractJavaScanner#createRules()
161 protected List createRules() {
162 List rules = new ArrayList();
163 // Add rule for strings and character constants.
164 Token token = getToken(IPreferenceConstants.PHP_STRING_DQ);
165 rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
167 // Add generic whitespace rule.
168 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
169 // Add word rule for keywords, types, and constants.
170 token = getToken(IPreferenceConstants.PHP_DEFAULT);
172 SmartyWordRule wordRule = new SmartyWordRule(new PHPWordDetector(), token);
174 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
175 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
176 // Token type = getToken(IPreferenceConstants.PHP_TYPE);
177 // Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
180 for (int i = 0; i < SMARTY_KEYWORDS.length; i++) {
181 wordRule.addWord(SMARTY_KEYWORDS[i], keyword);
184 for (int i = 0; i < SMARTY_FUNCTION_NAMES.length; i++) {
185 wordRule.addWord(SMARTY_FUNCTION_NAMES[i], functionName);
188 // ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
189 // PHPElement elbuffer = null;
190 // for (int i = 0; i < buffer.size(); i++) {
192 // elbuffer = (PHPElement) buffer.get(i);
193 // if (elbuffer instanceof PHPKeyword)
194 // wordRule.addWord(((PHPKeyword) elbuffer).getName(), keyword);
195 // // if (elbuffer instanceof PHPFunction)
196 // // wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
197 // if (elbuffer instanceof PHPType)
198 // wordRule.addWord(elbuffer.getName(), type);
199 // if (elbuffer instanceof PHPConstant)
200 // wordRule.addWord(elbuffer.getName(), constant);
203 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));