1) Added setting of syntax properties to italic, underline and strike through.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / SmartyCodeScanner.java
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
7
8  Contributors:
9  IBM Corporation - Initial implementation
10  www.phpeclipse.de
11  **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
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;
22
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;
31
32 /**
33  * PHP Code Scanner
34  */
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" };
40
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",
45                         "textformat" };
46
47         private class SmartyWordRule extends WordRule {
48                 private StringBuffer fBuffer = new StringBuffer();
49
50                 public SmartyWordRule(IWordDetector detector) {
51                         super(detector, Token.UNDEFINED);
52                 }
53
54                 public SmartyWordRule(IWordDetector detector, IToken defaultToken) {
55                         super(detector, defaultToken);
56                 }
57
58                 public IToken evaluate(ICharacterScanner scanner) {
59                         int c = scanner.read();
60                         boolean isVariable = false;
61                         if (c == '{') {
62                                 c = scanner.read();
63                                 if (c != '/') {
64                                         scanner.unread();
65                                 }
66                                 return getToken(IPreferenceConstants.PHP_TAG);
67                         }
68                         if (c == '}') {
69                                 return getToken(IPreferenceConstants.PHP_TAG);
70                         }
71                         if (fDetector.isWordStart((char) c)) {
72                                 if (c == '$') {
73                                         isVariable = true;
74                                 }
75                                 if (fColumn == UNDEFINED
76                                                 || (fColumn == scanner.getColumn() - 1)) {
77
78                                         fBuffer.setLength(0);
79                                         do {
80                                                 fBuffer.append((char) c);
81                                                 c = scanner.read();
82                                         } while (c != ICharacterScanner.EOF
83                                                         && fDetector.isWordPart((char) c));
84                                         scanner.unread();
85
86                                         if (isVariable) {
87                                                 return getToken(IPreferenceConstants.PHP_VARIABLE);
88                                         }
89                                         IToken token = (IToken) fWords.get(fBuffer.toString());
90                                         if (token != null)
91                                                 return token;
92
93                                         if (fDefaultToken.isUndefined())
94                                                 unreadBuffer(scanner);
95
96                                         return fDefaultToken;
97                                 }
98                         }
99
100                         scanner.unread();
101                         return Token.UNDEFINED;
102                 }
103         }
104
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 };
118
119         /**
120          * Creates a PHP code scanner
121          */
122         // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store)
123         // {
124         public SmartyCodeScanner(IColorManager manager, IPreferenceStore store) {
125                 super(manager, store);
126                 initialize();
127         }
128
129         /*
130          * @see AbstractJavaScanner#getTokenProperties()
131          */
132         protected String[] getTokenProperties() {
133                 return fgTokenProperties;
134         }
135
136         /*
137          * @see AbstractJavaScanner#createRules()
138          */
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$
144
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);
149
150                 SmartyWordRule wordRule = new SmartyWordRule(new PHPWordDetector(),
151                                 token);
152
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);
157
158                 for (int i = 0; i < SMARTY_KEYWORDS.length; i++) {
159                         wordRule.addWord(SMARTY_KEYWORDS[i], keyword);
160                 }
161
162                 for (int i = 0; i < SMARTY_FUNCTION_NAMES.length; i++) {
163                         wordRule.addWord(SMARTY_FUNCTION_NAMES[i], functionName);
164                 }
165
166                 // ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
167                 // PHPElement elbuffer = null;
168                 // for (int i = 0; i < buffer.size(); i++) {
169                 //
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(),
175                 // functionName);
176                 // if (elbuffer instanceof PHPType)
177                 // wordRule.addWord(elbuffer.getName(), type);
178                 // if (elbuffer instanceof PHPConstant)
179                 // wordRule.addWord(elbuffer.getName(), constant);
180                 // }
181                 rules.add(wordRule);
182                 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));
183                 return rules;
184         }
185 }