first PHP parser version (doesn't work actually)
[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.phpeclipse.phpeditor.util.PHPColorProvider;
18 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
19 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
20 import org.eclipse.jface.text.TextAttribute;
21 import org.eclipse.jface.text.rules.EndOfLineRule;
22 import org.eclipse.jface.text.rules.ICharacterScanner;
23 import org.eclipse.jface.text.rules.IRule;
24 import org.eclipse.jface.text.rules.IToken;
25 import org.eclipse.jface.text.rules.IWordDetector;
26 import org.eclipse.jface.text.rules.MultiLineRule;
27 import org.eclipse.jface.text.rules.RuleBasedScanner;
28 import org.eclipse.jface.text.rules.SingleLineRule;
29 import org.eclipse.jface.text.rules.Token;
30 import org.eclipse.jface.text.rules.WhitespaceRule;
31 import org.eclipse.jface.text.rules.WordRule;
32
33 /**
34  * PHP Code Scanner
35  */
36 public class PHPCodeScanner extends RuleBasedScanner {
37
38         
39
40         
41   private IToken variable;
42   
43         private class PHPWordRule extends WordRule {
44     private StringBuffer fBuffer= new StringBuffer();
45
46                 public PHPWordRule(IWordDetector detector) {
47                         super(detector, Token.UNDEFINED);
48                 }
49
50                 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
51                         super(detector, defaultToken);
52                 }
53
54                 public IToken evaluate(ICharacterScanner scanner) {
55                         int c = scanner.read();
56       boolean isVariable = false;
57                         if (fDetector.isWordStart((char) c)) {
58         if (c=='$') {
59           isVariable = true;
60         }
61                                 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
62
63                                         fBuffer.setLength(0);
64                                         do {
65                                                 fBuffer.append((char) c);
66                                                 c = scanner.read();
67                                         } while (c != scanner.EOF && fDetector.isWordPart((char) c));
68                                         scanner.unread();
69
70           if (isVariable) {
71             return variable;
72           }
73                                         IToken token = (IToken) fWords.get(fBuffer.toString());
74                                         if (token != null)
75                                                 return token;
76
77                                         if (fDefaultToken.isUndefined())
78                                                 unreadBuffer(scanner);
79
80                                         return fDefaultToken;
81                                 }
82                         }
83
84                         scanner.unread();
85                         return Token.UNDEFINED;
86                 }
87         }
88
89         private static String[] fgConstants = { "__LINE__", "__FILE__", "true", "false" };
90         private TextAttribute fComment;
91         private TextAttribute fKeyword;
92         private TextAttribute fType;
93         private TextAttribute fString;
94         private PHPColorProvider fColorProvider;
95
96         /**
97          * Creates a Java code scanner
98          */
99         public PHPCodeScanner(PHPColorProvider provider) {
100
101                 IToken keyword = new Token(new TextAttribute(provider.getColor(PHPColorProvider.KEYWORD)));
102                 IToken type = new Token(new TextAttribute(provider.getColor(PHPColorProvider.TYPE)));
103                 IToken string = new Token(new TextAttribute(provider.getColor(PHPColorProvider.STRING)));
104                 IToken comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.SINGLE_LINE_COMMENT)));
105                 IToken multi_comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT)));
106                 IToken other = new Token(new TextAttribute(provider.getColor(PHPColorProvider.DEFAULT)));
107
108     variable = new Token(new TextAttribute(provider.getColor(PHPColorProvider.VARIABLE)));
109
110     
111                 List rules = new ArrayList();
112
113                 // Add rule for single line comments.
114                 rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$
115                 //    EndOfLineRule endOfLine = new EndOfLineRule("#", comment);
116                 //    endOfLine.setColumnConstraint(0);
117                 rules.add(new EndOfLineRule("#", comment));
118
119                 // Add rule for strings and character constants.
120                 rules.add(new SingleLineRule("\"", "\"", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
121                 rules.add(new SingleLineRule("'", "'", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
122
123                 // rules.add(new SingleLineRule("//", "//", php_comment));
124                 rules.add(new MultiLineRule("/*", "*/", multi_comment));
125
126                 // Add generic whitespace rule.
127                 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
128
129                 // Add word rule for keywords, types, and constants.
130                 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), other);
131                 for (int i = 0; i < PHPKeywords.PHP_KEYWORS.length; i++)
132                         wordRule.addWord(PHPKeywords.PHP_KEYWORS[i], keyword);
133                 for (int i = 0; i < PHPFunctionNames.FUNCTION_NAMES.length; i++)
134                         wordRule.addWord(PHPFunctionNames.FUNCTION_NAMES[i], type);
135                 for (int i = 0; i < fgConstants.length; i++)
136                         wordRule.addWord(fgConstants[i], type);
137                 rules.add(wordRule);
138
139                 IRule[] result = new IRule[rules.size()];
140                 rules.toArray(result);
141                 setRules(result);
142         }
143 }