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,
109 IPreferenceConstants.PHP_KEYWORD,
110 IPreferenceConstants.PHP_FUNCTIONNAME,
111 IPreferenceConstants.PHP_VARIABLE,
112 IPreferenceConstants.PHP_VARIABLE_DOLLAR,
113 IPreferenceConstants.PHP_STRING_DQ,
114 IPreferenceConstants.PHP_STRING_SQ,
115 IPreferenceConstants.PHP_TYPE,
116 IPreferenceConstants.PHP_CONSTANT,
117 IPreferenceConstants.PHP_DEFAULT };
120 * Creates a PHP code scanner
122 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store)
124 public SmartyCodeScanner(IColorManager manager, IPreferenceStore store) {
125 super(manager, store);
130 * @see AbstractJavaScanner#getTokenProperties()
132 protected String[] getTokenProperties() {
133 return fgTokenProperties;
137 * @see AbstractJavaScanner#createRules()
139 protected List createRules() {
140 List rules = new ArrayList();
141 // Add rule for strings and character constants.
142 Token token = getToken(IPreferenceConstants.PHP_STRING_DQ);
143 rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
145 // Add generic whitespace rule.
146 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
147 // Add word rule for keywords, types, and constants.
148 token = getToken(IPreferenceConstants.PHP_DEFAULT);
150 SmartyWordRule wordRule = new SmartyWordRule(new PHPWordDetector(),
153 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
154 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
155 // Token type = getToken(IPreferenceConstants.PHP_TYPE);
156 // Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
158 for (int i = 0; i < SMARTY_KEYWORDS.length; i++) {
159 wordRule.addWord(SMARTY_KEYWORDS[i], keyword);
162 for (int i = 0; i < SMARTY_FUNCTION_NAMES.length; i++) {
163 wordRule.addWord(SMARTY_FUNCTION_NAMES[i], functionName);
166 // ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
167 // PHPElement elbuffer = null;
168 // for (int i = 0; i < buffer.size(); i++) {
170 // elbuffer = (PHPElement) buffer.get(i);
171 // if (elbuffer instanceof PHPKeyword)
172 // wordRule.addWord(((PHPKeyword) elbuffer).getName(), keyword);
173 // // if (elbuffer instanceof PHPFunction)
174 // // wordRule.addWord(((PHPFunction) elbuffer).getName(),
176 // if (elbuffer instanceof PHPType)
177 // wordRule.addWord(elbuffer.getName(), type);
178 // if (elbuffer instanceof PHPConstant)
179 // wordRule.addWord(elbuffer.getName(), constant);
182 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));