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