Fix for ticket #358 Interpreter Tab in Mac OSX from mbowie testing and confirming...
[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, 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 };
115
116         /**
117          * Creates a PHP code scanner
118          */
119         // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store)
120         // {
121         public SmartyCodeScanner(IColorManager manager, IPreferenceStore store) {
122                 super(manager, store);
123                 initialize();
124         }
125
126         /*
127          * @see AbstractJavaScanner#getTokenProperties()
128          */
129         protected String[] getTokenProperties() {
130                 return fgTokenProperties;
131         }
132
133         /*
134          * @see AbstractJavaScanner#createRules()
135          */
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$
141
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);
146
147                 SmartyWordRule wordRule = new SmartyWordRule(new PHPWordDetector(),
148                                 token);
149
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);
154
155                 for (int i = 0; i < SMARTY_KEYWORDS.length; i++) {
156                         wordRule.addWord(SMARTY_KEYWORDS[i], keyword);
157                 }
158
159                 for (int i = 0; i < SMARTY_FUNCTION_NAMES.length; i++) {
160                         wordRule.addWord(SMARTY_FUNCTION_NAMES[i], functionName);
161                 }
162
163                 // ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
164                 // PHPElement elbuffer = null;
165                 // for (int i = 0; i < buffer.size(); i++) {
166                 //
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(),
172                 // functionName);
173                 // if (elbuffer instanceof PHPType)
174                 // wordRule.addWord(elbuffer.getName(), type);
175                 // if (elbuffer instanceof PHPConstant)
176                 // wordRule.addWord(elbuffer.getName(), constant);
177                 // }
178                 rules.add(wordRule);
179                 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));
180                 return rules;
181         }
182 }