Fixed some bugs in the syntax editor preference page
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / PHPCodeScanner.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     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
13
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Vector;
17
18 import net.sourceforge.phpdt.internal.ui.text.AbstractJavaScanner;
19 import net.sourceforge.phpdt.ui.text.IColorManager;
20 import net.sourceforge.phpeclipse.IPreferenceConstants;
21 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
22 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
23 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
24
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.text.rules.EndOfLineRule;
27 import org.eclipse.jface.text.rules.ICharacterScanner;
28 import org.eclipse.jface.text.rules.IToken;
29 import org.eclipse.jface.text.rules.IWordDetector;
30 import org.eclipse.jface.text.rules.MultiLineRule;
31 import org.eclipse.jface.text.rules.Token;
32 import org.eclipse.jface.text.rules.WhitespaceRule;
33 import org.eclipse.jface.text.rules.WordRule;
34
35 /**
36  * PHP Code Scanner
37  */
38 public class PHPCodeScanner
39   extends AbstractJavaScanner {
40
41 //  private static Token variable;
42 //  private static Token keyword;
43 //  private static Token type;
44 //  private static Token constant;
45 //  private static Token functionName;
46 //  private static Token string;
47 //  private static Token comment;
48 //  private static Token multi_comment;
49 //  private static Token other;
50
51   private class PHPWordRule extends WordRule {
52     private StringBuffer fBuffer = new StringBuffer();
53
54     public PHPWordRule(IWordDetector detector) {
55       super(detector, Token.UNDEFINED);
56     }
57
58     public PHPWordRule(IWordDetector detector, IToken defaultToken) {
59       super(detector, defaultToken);
60     }
61
62     public IToken evaluate(ICharacterScanner scanner) {
63       int c = scanner.read();
64       boolean isVariable = false;
65       if (fDetector.isWordStart((char) c)) {
66         if (c == '$') {
67           isVariable = true;
68         }
69         if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
70
71           fBuffer.setLength(0);
72           do {
73             fBuffer.append((char) c);
74             c = scanner.read();
75           } while (
76             c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
77           scanner.unread();
78
79           if (isVariable) {
80             return getToken(IPreferenceConstants.PHP_VARIABLE);
81           }
82           IToken token = (IToken) fWords.get(fBuffer.toString());
83           if (token != null)
84             return token;
85
86           if (fDefaultToken.isUndefined())
87             unreadBuffer(scanner);
88
89           return fDefaultToken;
90         }
91       }
92
93       scanner.unread();
94       return Token.UNDEFINED;
95     }
96   }
97
98   //private PHPColorProvider fColorProvider;
99
100   private static String[] fgTokenProperties = {
101                 IPreferenceConstants.PHP_MULTILINE_COMMENT,
102                 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
103                 IPreferenceConstants.PHP_KEYWORD,
104                 IPreferenceConstants.PHP_FUNCTIONNAME,
105                 IPreferenceConstants.PHP_VARIABLE,
106                 IPreferenceConstants.PHP_STRING,
107                 IPreferenceConstants.PHP_TYPE,
108                 IPreferenceConstants.PHP_CONSTANT,
109                 IPreferenceConstants.PHP_DEFAULT };
110   /**
111         * Creates a PHP code scanner
112         */
113   // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
114   public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
115     super(manager, store);
116     initialize();
117     //  //  final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
118     //   Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
119     //    variable =
120     //      new Token(
121     //        new TextAttribute(
122     //          provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
123     //          BackgroundColor,
124     //          (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
125     //            + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
126     //      keyword =
127     //        new Token(new TextAttribute(
128     //          provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
129     //          BackgroundColor,
130     //    //SWT.NONE));
131     //   (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
132     //      type =
133     //        new Token(new TextAttribute(
134     //          provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
135     //          BackgroundColor,
136     //    //SWT.NONE));
137     //   (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
138     //      functionName =
139     //        new Token(new TextAttribute(
140     //          provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
141     //          BackgroundColor,
142     //    //SWT.NONE));
143     //  (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
144     //    + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
145     //      constant =
146     //        new Token(new TextAttribute(
147     //          provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
148     //          BackgroundColor,
149     //    //SWT.NONE));
150     //   (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
151     //      string =
152     //        new Token(new TextAttribute(
153     //          provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
154     //          BackgroundColor,
155     //    //SWT.NONE));
156     //   (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE ) + (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
157     //      comment =
158     //        new Token(new TextAttribute(
159     //          provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)),
160     //          BackgroundColor,
161     //    //SWT.NONE));
162     //  (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE )
163     //    + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
164     //      multi_comment =
165     //        new Token(new TextAttribute(
166     //          provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)),
167     //          BackgroundColor,
168     //    //SWT.NONE));
169     //  (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
170     //    + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
171     //      other =
172     //        new Token(new TextAttribute(
173     //          provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
174     //          BackgroundColor,
175     //    //SWT.NONE));
176     //   (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
177     //    updateWordRules();
178   }
179
180   //  public void updateToken(JavaColorManager provider) {
181   //    final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
182   //
183   //    Color BackgroundColor = provider.getColor(PreferenceConverter.getColor(store, PHP_EDITOR_BACKGROUND));
184   //
185   //    variable.setData(
186   //      new TextAttribute(
187   //        provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
188   //        BackgroundColor,
189   //        (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
190   //          + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
191   //    keyword.setData(
192   //      new TextAttribute(
193   //        provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
194   //        BackgroundColor,
195   //        (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE)
196   //          + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
197   //    type.setData(
198   //      new TextAttribute(
199   //        provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
200   //        BackgroundColor,
201   //        (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
202   //    functionName.setData(
203   //      new TextAttribute(
204   //        provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
205   //        BackgroundColor,
206   //        (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
207   //          + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
208   //    constant.setData(
209   //      new TextAttribute(
210   //        provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
211   //        BackgroundColor,
212   //        (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE)
213   //          + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
214   //    string.setData(
215   //      new TextAttribute(
216   //        provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
217   //        BackgroundColor,
218   //        (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE) + (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
219   //    comment.setData(
220   //      new TextAttribute(
221   //        provider.getColor(PreferenceConverter.getColor(store, PHP_SINGLELINE_COMMENT)),
222   //        BackgroundColor,
223   //        (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
224   //          + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
225   //    multi_comment.setData(
226   //      new TextAttribute(
227   //        provider.getColor(PreferenceConverter.getColor(store, PHP_MULTILINE_COMMENT)),
228   //        BackgroundColor,
229   //        (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
230   //          + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
231   //    other.setData(
232   //      new TextAttribute(
233   //        provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
234   //        BackgroundColor,
235   //        (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE)
236   //          + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
237   //  }
238
239   // public void updateWordRules() {
240
241   /*
242    * @see AbstractJavaScanner#getTokenProperties()
243    */
244   protected String[] getTokenProperties() {
245     return fgTokenProperties;
246   }
247   /*
248    * @see AbstractJavaScanner#createRules()
249    */
250   protected List createRules() {
251     List rules = new ArrayList();
252     Token token= getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
253     // Add rule for single line comments.
254     rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
255     rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
256     // Add rule for strings and character constants.
257     token= getToken(IPreferenceConstants.PHP_STRING);
258     rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
259     rules.add(new MultiLineRule("`", "`", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
260     rules.add(new MultiLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
261
262     //previous version
263     //rules.add(new SingleLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
264
265
266     token= getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
267     rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
268     // Add generic whitespace rule.
269     rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
270     // Add word rule for keywords, types, and constants.
271     token= getToken(IPreferenceConstants.PHP_DEFAULT);
272     PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
273
274     Token keyword= getToken(IPreferenceConstants.PHP_KEYWORD);
275     Token functionName= getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
276     Token type= getToken(IPreferenceConstants.PHP_TYPE);
277     Token constant= getToken(IPreferenceConstants.PHP_CONSTANT);
278     PHPSyntaxRdr.readInSyntax();
279     Vector buffer = PHPSyntaxRdr.getsyntaxdata();
280   //  String strbuffer = null;  unused
281     PHPElement elbuffer = null;
282     while ((buffer != null)
283       && (!buffer.isEmpty()
284         && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
285       if (elbuffer instanceof PHPKeyword)
286         wordRule.addWord(((PHPKeyword) elbuffer).getName(), keyword);
287       if (elbuffer instanceof PHPFunction)
288         wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
289       if (elbuffer instanceof PHPType)
290         wordRule.addWord(elbuffer.getName(), type);
291       if (elbuffer instanceof PHPConstant)
292         wordRule.addWord(elbuffer.getName(), constant);
293     }
294     rules.add(wordRule);
295    // IRule[] result = new IRule[rules.size()];unused
296     //    rules.toArray(result);
297     //    setRules(result);
298     return rules;
299   }
300 }