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 = { "capture", "config_load",
37 "else", "elseif", "foreach", "foreachelse", "if", "include",
38 "insert", "ldelim", "literal", "php", "rdelim", "section",
39 "sectionelse", "strip" };
41 public static String[] SMARTY_FUNCTION_NAMES = { "assign", "counter",
42 "cycle", "debug", "eval", "fetch", "html_checkboxes", "html_image",
43 "html_options", "html_radios", "html_select_date",
44 "html_select_time", "html_table", "math", "popup", "popup_init",
47 private class SmartyWordRule extends WordRule {
48 private StringBuffer fBuffer = new StringBuffer();
50 public SmartyWordRule(IWordDetector detector) {
51 super(detector, Token.UNDEFINED);
54 public SmartyWordRule(IWordDetector detector, IToken defaultToken) {
55 super(detector, defaultToken);
58 public IToken evaluate(ICharacterScanner scanner) {
59 int c = scanner.read();
60 boolean isVariable = false;
66 return getToken(IPreferenceConstants.PHP_TAG);
69 return getToken(IPreferenceConstants.PHP_TAG);
71 if (fDetector.isWordStart((char) c)) {
75 if (fColumn == UNDEFINED
76 || (fColumn == scanner.getColumn() - 1)) {
80 fBuffer.append((char) c);
82 } while (c != ICharacterScanner.EOF
83 && fDetector.isWordPart((char) c));
87 return getToken(IPreferenceConstants.PHP_VARIABLE);
89 IToken token = (IToken) fWords.get(fBuffer.toString());
93 if (fDefaultToken.isUndefined())
94 unreadBuffer(scanner);
101 return Token.UNDEFINED;
105 private static String[] fgTokenProperties = {
106 IPreferenceConstants.PHP_MULTILINE_COMMENT,
107 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
108 IPreferenceConstants.PHP_TAG, IPreferenceConstants.PHP_KEYWORD,
109 IPreferenceConstants.PHP_FUNCTIONNAME,
110 IPreferenceConstants.PHP_VARIABLE,
111 IPreferenceConstants.PHP_VARIABLE_DOLLAR,
112 IPreferenceConstants.PHP_STRING_DQ,
113 IPreferenceConstants.PHP_STRING_SQ, IPreferenceConstants.PHP_TYPE,
114 IPreferenceConstants.PHP_CONSTANT, IPreferenceConstants.PHP_DEFAULT };
117 * Creates a PHP code scanner
119 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store)
121 public SmartyCodeScanner(IColorManager manager, IPreferenceStore store) {
122 super(manager, store);
127 * @see AbstractJavaScanner#getTokenProperties()
129 protected String[] getTokenProperties() {
130 return fgTokenProperties;
134 * @see AbstractJavaScanner#createRules()
136 protected List createRules() {
137 List rules = new ArrayList();
138 // Add rule for strings and character constants.
139 Token token = getToken(IPreferenceConstants.PHP_STRING_DQ);
140 rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
142 // Add generic whitespace rule.
143 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
144 // Add word rule for keywords, types, and constants.
145 token = getToken(IPreferenceConstants.PHP_DEFAULT);
147 SmartyWordRule wordRule = new SmartyWordRule(new PHPWordDetector(),
150 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
151 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
152 // Token type = getToken(IPreferenceConstants.PHP_TYPE);
153 // Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
155 for (int i = 0; i < SMARTY_KEYWORDS.length; i++) {
156 wordRule.addWord(SMARTY_KEYWORDS[i], keyword);
159 for (int i = 0; i < SMARTY_FUNCTION_NAMES.length; i++) {
160 wordRule.addWord(SMARTY_FUNCTION_NAMES[i], functionName);
163 // ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
164 // PHPElement elbuffer = null;
165 // for (int i = 0; i < buffer.size(); i++) {
167 // elbuffer = (PHPElement) buffer.get(i);
168 // if (elbuffer instanceof PHPKeyword)
169 // wordRule.addWord(((PHPKeyword) elbuffer).getName(), keyword);
170 // // if (elbuffer instanceof PHPFunction)
171 // // wordRule.addWord(((PHPFunction) elbuffer).getName(),
173 // if (elbuffer instanceof PHPType)
174 // wordRule.addWord(elbuffer.getName(), type);
175 // if (elbuffer instanceof PHPConstant)
176 // wordRule.addWord(elbuffer.getName(), constant);
179 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));