new PartitionScanner version
[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
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.PHPSyntaxRdr;
21 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
22 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
23
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.text.rules.EndOfLineRule;
26 import org.eclipse.jface.text.rules.ICharacterScanner;
27 import org.eclipse.jface.text.rules.IRule;
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 extends AbstractJavaScanner {
39  
40   /**
41    * Rule to detect java operators.
42    * 
43    * @since 3.0
44    */
45         protected class OperatorRule implements IRule {
46         
47                 /** Java operators */
48                 private final char[] JAVA_OPERATORS= { ';', '(', ')', '{', '}', '.', '=', '/', '\\', '+', '-', '*', '[', ']', '<', '>', ':', '?', '!', ',', '|', '&', '^', '%', '~', '@'};
49                 /** Token to return for this rule */
50                 private final IToken fToken;
51         
52                 /**
53      * Creates a new operator rule.
54      * 
55      * @param token
56      *          Token to use for this rule
57      */
58                 public OperatorRule(IToken token) {
59                         fToken= token;
60                 }
61                 
62                 /**
63      * Is this character an operator character?
64      * 
65      * @param character
66      *          Character to determine whether it is an operator character
67      * @return <code>true</code> iff the character is an operator,
68      *         <code>false</code> otherwise.
69      */
70                 public boolean isOperator(char character) {
71                         for (int index= 0; index < JAVA_OPERATORS.length; index++) {
72                                 if (JAVA_OPERATORS[index] == character)
73                                         return true;
74                         }
75                         return false;
76                 }
77         
78                 /*
79      * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
80      */
81                 public IToken evaluate(ICharacterScanner scanner) {
82         
83                         int character= scanner.read();
84                         if (isOperator((char) character)) {
85                             int lastCharacter = character;
86                             character= scanner.read();
87                                 if (!isOperator((char) character)) {
88                                   scanner.unread();
89                               return fToken;
90                                 }
91                                 if (lastCharacter=='<' && character=='?') {
92                                   scanner.unread();
93                                   scanner.unread();
94                           return Token.UNDEFINED;
95                                 }
96                                 if (lastCharacter=='?' && character=='>') {
97                                   scanner.unread();
98                                   scanner.unread();
99                           return Token.UNDEFINED;
100                                 }
101                                 do {
102                                         character= scanner.read();
103                                 } while (isOperator((char) character));
104                                 scanner.unread();
105                                 return fToken;
106                         } else {
107                                 scanner.unread();
108                                 return Token.UNDEFINED;
109                         }
110                 }
111         }
112         
113   private class PHPWordRule extends WordRule {
114     private StringBuffer fBuffer = new StringBuffer();
115
116     public PHPWordRule(IWordDetector detector) {
117       super(detector, Token.UNDEFINED);
118     }
119
120     public PHPWordRule(IWordDetector detector, IToken defaultToken) {
121       super(detector, defaultToken);
122     }
123
124     public IToken evaluate(ICharacterScanner scanner) {
125       int c = scanner.read();
126       boolean isVariable = false;
127       if (c == '<') {
128         c = scanner.read();
129         if (c != '?') {
130           scanner.unread();
131           scanner.unread();
132           return Token.UNDEFINED;
133         } else {
134           c = scanner.read();
135           if (c != 'p' && c != 'P') {
136             scanner.unread();
137             return getToken(IPreferenceConstants.PHP_TAG);
138           } else {
139             c = scanner.read();
140             if (c != 'h' && c != 'H') {
141               scanner.unread();
142               scanner.unread();
143               return getToken(IPreferenceConstants.PHP_TAG);
144             } else {
145               c = scanner.read();
146               if (c != 'p' && c != 'P') {
147                 scanner.unread();
148                 scanner.unread();
149                 scanner.unread();
150                 return getToken(IPreferenceConstants.PHP_TAG);
151               } else {
152                 return getToken(IPreferenceConstants.PHP_TAG);
153               }
154             }
155           }
156         }
157       }
158       if (c == '?') {
159                 c = scanner.read();
160         if (c == '>') {
161           return getToken(IPreferenceConstants.PHP_TAG);
162         } 
163         scanner.unread();
164         scanner.unread();
165         return Token.UNDEFINED;
166       }
167       if (fDetector.isWordStart((char) c)) {
168         if (c == '$') {
169           isVariable = true;
170         }
171         if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
172
173           fBuffer.setLength(0);
174           do {
175             fBuffer.append((char) c);
176             c = scanner.read();
177           } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
178           scanner.unread();
179
180           if (isVariable) {
181             return getToken(IPreferenceConstants.PHP_VARIABLE);
182           }
183           IToken token = (IToken) fWords.get(fBuffer.toString());
184           if (token != null)
185             return token;
186
187           if (fDefaultToken.isUndefined())
188             unreadBuffer(scanner);
189
190           return fDefaultToken;
191         }
192       }
193
194       scanner.unread();
195       return Token.UNDEFINED;
196     }
197   }
198
199   //private PHPColorProvider fColorProvider;
200
201   private static String[] fgTokenProperties =
202     {
203       IPreferenceConstants.PHP_MULTILINE_COMMENT,
204       IPreferenceConstants.PHP_SINGLELINE_COMMENT,
205           IPreferenceConstants.PHP_TAG,
206       IPreferenceConstants.PHP_KEYWORD,
207       IPreferenceConstants.PHP_FUNCTIONNAME,
208       IPreferenceConstants.PHP_VARIABLE,
209       IPreferenceConstants.PHP_STRING,
210       IPreferenceConstants.PHP_TYPE,
211       IPreferenceConstants.PHP_CONSTANT,
212       IPreferenceConstants.PHP_DEFAULT,
213       IPreferenceConstants.PHP_OPERATOR,
214       IPreferenceConstants.PHP_KEYWORD_RETURN};
215   /**
216    * Creates a PHP code scanner
217    */
218   // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
219   public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
220     super(manager, store);
221     initialize();
222     //  // final IPreferenceStore store =
223     // PHPeclipsePlugin.getDefault().getPreferenceStore();
224     //   Color BackgroundColor =
225     // provider.getColor(PreferenceConverter.getColor(store,
226     // PHP_EDITOR_BACKGROUND));
227     //    variable =
228     //      new Token(
229     //        new TextAttribute(
230     //          provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
231     //          BackgroundColor,
232     //          (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
233     //            + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
234     //      keyword =
235     //        new Token(new TextAttribute(
236     //          provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
237     //          BackgroundColor,
238     //    //SWT.NONE));
239     //   (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE) +
240     // (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
241     //      type =
242     //        new Token(new TextAttribute(
243     //          provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
244     //          BackgroundColor,
245     //    //SWT.NONE));
246     //   (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) +
247     // (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
248     //      functionName =
249     //        new Token(new TextAttribute(
250     //          provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
251     //          BackgroundColor,
252     //    //SWT.NONE));
253     //  (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
254     //    + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
255     //      constant =
256     //        new Token(new TextAttribute(
257     //          provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
258     //          BackgroundColor,
259     //    //SWT.NONE));
260     //   (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE) +
261     // (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
262     //      string =
263     //        new Token(new TextAttribute(
264     //          provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
265     //          BackgroundColor,
266     //    //SWT.NONE));
267     //   (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE ) +
268     // (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
269     //      comment =
270     //        new Token(new TextAttribute(
271     //          provider.getColor(PreferenceConverter.getColor(store,
272     // PHP_SINGLELINE_COMMENT)),
273     //          BackgroundColor,
274     //    //SWT.NONE));
275     //  (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE )
276     //    + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC :
277     // SWT.NONE)));
278     //      multi_comment =
279     //        new Token(new TextAttribute(
280     //          provider.getColor(PreferenceConverter.getColor(store,
281     // PHP_MULTILINE_COMMENT)),
282     //          BackgroundColor,
283     //    //SWT.NONE));
284     //  (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
285     //    + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC :
286     // SWT.NONE)));
287     //      other =
288     //        new Token(new TextAttribute(
289     //          provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
290     //          BackgroundColor,
291     //    //SWT.NONE));
292     //   (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE) +
293     // (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
294     //    updateWordRules();
295   }
296
297   /*
298    * @see AbstractJavaScanner#getTokenProperties()
299    */
300   protected String[] getTokenProperties() {
301     return fgTokenProperties;
302   }
303   //  public void updateToken(JavaColorManager provider) {
304   //    final IPreferenceStore store =
305   // PHPeclipsePlugin.getDefault().getPreferenceStore();
306   //
307   //    Color BackgroundColor =
308   // provider.getColor(PreferenceConverter.getColor(store,
309   // PHP_EDITOR_BACKGROUND));
310   //
311   //    variable.setData(
312   //      new TextAttribute(
313   //        provider.getColor(PreferenceConverter.getColor(store, PHP_VARIABLE)),
314   //        BackgroundColor,
315   //        (store.getBoolean(PHP_VARIABLE_BOLD) ? SWT.BOLD : SWT.NONE)
316   //          + (store.getBoolean(PHP_VARIABLE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
317   //    keyword.setData(
318   //      new TextAttribute(
319   //        provider.getColor(PreferenceConverter.getColor(store, PHP_KEYWORD)),
320   //        BackgroundColor,
321   //        (store.getBoolean(PHP_KEYWORD_BOLD) ? SWT.BOLD : SWT.NONE)
322   //          + (store.getBoolean(PHP_KEYWORD_ITALIC) ? SWT.ITALIC : SWT.NONE)));
323   //    type.setData(
324   //      new TextAttribute(
325   //        provider.getColor(PreferenceConverter.getColor(store, PHP_TYPE)),
326   //        BackgroundColor,
327   //        (store.getBoolean(PHP_TYPE_BOLD) ? SWT.BOLD : SWT.NONE) +
328   // (store.getBoolean(PHP_TYPE_ITALIC) ? SWT.ITALIC : SWT.NONE)));
329   //    functionName.setData(
330   //      new TextAttribute(
331   //        provider.getColor(PreferenceConverter.getColor(store, PHP_FUNCTIONNAME)),
332   //        BackgroundColor,
333   //        (store.getBoolean(PHP_FUNCTIONNAME_BOLD) ? SWT.BOLD : SWT.NONE)
334   //          + (store.getBoolean(PHP_FUNCTIONNAME_ITALIC) ? SWT.ITALIC : SWT.NONE)));
335   //    constant.setData(
336   //      new TextAttribute(
337   //        provider.getColor(PreferenceConverter.getColor(store, PHP_CONSTANT)),
338   //        BackgroundColor,
339   //        (store.getBoolean(PHP_CONSTANT_BOLD) ? SWT.BOLD : SWT.NONE)
340   //          + (store.getBoolean(PHP_CONSTANT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
341   //    string.setData(
342   //      new TextAttribute(
343   //        provider.getColor(PreferenceConverter.getColor(store, PHP_STRING)),
344   //        BackgroundColor,
345   //        (store.getBoolean(PHP_STRING_BOLD) ? SWT.BOLD : SWT.NONE) +
346   // (store.getBoolean(PHP_STRING_ITALIC) ? SWT.ITALIC : SWT.NONE)));
347   //    comment.setData(
348   //      new TextAttribute(
349   //        provider.getColor(PreferenceConverter.getColor(store,
350   // PHP_SINGLELINE_COMMENT)),
351   //        BackgroundColor,
352   //        (store.getBoolean(PHP_SINGLELINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
353   //          + (store.getBoolean(PHP_SINGLELINE_COMMENT_ITALIC) ? SWT.ITALIC :
354   // SWT.NONE)));
355   //    multi_comment.setData(
356   //      new TextAttribute(
357   //        provider.getColor(PreferenceConverter.getColor(store,
358   // PHP_MULTILINE_COMMENT)),
359   //        BackgroundColor,
360   //        (store.getBoolean(PHP_MULTILINE_COMMENT_BOLD) ? SWT.BOLD : SWT.NONE)
361   //          + (store.getBoolean(PHP_MULTILINE_COMMENT_ITALIC) ? SWT.ITALIC :
362   // SWT.NONE)));
363   //    other.setData(
364   //      new TextAttribute(
365   //        provider.getColor(PreferenceConverter.getColor(store, PHP_DEFAULT)),
366   //        BackgroundColor,
367   //        (store.getBoolean(PHP_DEFAULT_BOLD) ? SWT.BOLD : SWT.NONE)
368   //          + (store.getBoolean(PHP_DEFAULT_ITALIC) ? SWT.ITALIC : SWT.NONE)));
369   //  }
370
371   // public void updateWordRules() {
372
373
374   /*
375    * @see AbstractJavaScanner#createRules()
376    */
377   protected List createRules() {
378     List rules = new ArrayList();
379     Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
380     // Add rule for single line comments.
381     rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
382     rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
383     // Add rule for strings and character constants.
384     token = getToken(IPreferenceConstants.PHP_STRING);
385     rules.add(new MultiLineRule("\"", "\"", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
386     rules.add(new MultiLineRule("`", "`", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
387     rules.add(new MultiLineRule("'", "'", token, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
388         
389     token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
390     rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
391     // Add generic whitespace rule.
392     rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
393     // Add word rule for keywords, types, and constants.
394     token = getToken(IPreferenceConstants.PHP_DEFAULT);
395     PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
396         
397     Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
398     Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
399     Token type = getToken(IPreferenceConstants.PHP_TYPE);
400     Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
401
402     ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
403     //  String strbuffer = null; unused
404     PHPElement elbuffer = null;
405     String name;
406     for (int i = 0; i < buffer.size(); i++) {
407       //    while ((buffer != null)
408       //      && (!buffer.isEmpty()
409       //        && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
410       elbuffer = (PHPElement) buffer.get(i);
411       if (elbuffer instanceof PHPKeyword) {
412         name = ((PHPKeyword) elbuffer).getName();
413         if (!name.equals("return")) {
414           wordRule.addWord(name, keyword);
415         }
416       } else if (elbuffer instanceof PHPFunction) {
417         wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
418       } else if (elbuffer instanceof PHPType) {
419         wordRule.addWord(elbuffer.getName(), type);
420       } else if (elbuffer instanceof PHPConstant) {
421         wordRule.addWord(elbuffer.getName(), constant);
422       }
423     }
424     
425 // Add word rule for keyword 'return'.
426     token= getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
427     wordRule.addWord("return", token);
428
429 //  Add rule for operators and brackets (at the end !)
430         token= getToken(IPreferenceConstants.PHP_OPERATOR);
431         rules.add(new OperatorRule(token));
432         
433     rules.add(wordRule);
434     
435     setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));
436     return rules;
437   }
438 }