9b0ec7fe90fdc22b2a6c8f3fd864d7fcdbd6749b
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / phpdoc / PHPDocCodeScanner.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 **********************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.phpdoc;
12
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import net.sourceforge.phpdt.internal.ui.text.JavaColorManager;
18 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
19 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
20
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.TextAttribute;
25 import org.eclipse.jface.text.rules.ICharacterScanner;
26 import org.eclipse.jface.text.rules.IToken;
27 import org.eclipse.jface.text.rules.IWordDetector;
28 import org.eclipse.jface.text.rules.RuleBasedScanner;
29 import org.eclipse.jface.text.rules.SingleLineRule;
30 import org.eclipse.jface.text.rules.Token;
31 import org.eclipse.jface.text.rules.WhitespaceRule;
32 import org.eclipse.jface.text.rules.WordRule;
33
34
35
36
37 /**
38  * A rule based PHPDoc scanner.
39  */
40 public final class PHPDocCodeScanner extends RuleBasedScanner {
41                 
42                 
43         /**
44          * A key word detector.
45          */
46         static class PHPDocKeywordDetector implements IWordDetector {
47
48                 /**
49                  * @see IWordDetector#isWordStart
50                  */
51                 public boolean isWordStart(char c) {
52                         return (c == '@');
53                 }
54
55                 /**
56                  * @see IWordDetector#isWordPart
57                  */
58                 public boolean isWordPart(char c) {
59                         return Character.isLetter(c);
60                 }
61         };
62         
63         
64         /**
65          * Detector for HTML comment delimiters.
66          */
67         static class HTMLCommentDetector implements IWordDetector {
68
69                 /**
70                  * @see IWordDetector#isWordStart
71                  */
72                 public boolean isWordStart(char c) {
73                         return (c == '<' || c == '-');
74                 }
75
76                 /**
77                  * @see IWordDetector#isWordPart
78                  */
79                 public boolean isWordPart(char c) {
80                         return (c == '-' || c == '!' || c == '>');
81                 }
82         };
83         
84         class TagRule extends SingleLineRule {
85                 
86                 /*
87                  * @see SingleLineRule
88                  */
89                 public TagRule(IToken token) {
90                         super("<", ">", token, (char) 0); //$NON-NLS-2$ //$NON-NLS-1$
91                 }
92                 
93                 /*
94                  * @see SingleLineRule 
95                  */
96                 public TagRule(IToken token, char escapeCharacter) {
97                         super("<", ">", token, escapeCharacter); //$NON-NLS-2$ //$NON-NLS-1$
98                 }
99                 
100                 private IToken checkForWhitespace(ICharacterScanner scanner) {
101                         
102                         try {
103                                 
104                                 char c= getDocument().getChar(getTokenOffset() + 1);
105                                 if (!Character.isWhitespace(c)) 
106                                         return fToken;
107                                         
108                         } catch (BadLocationException x) {
109                         }
110                         
111                         return Token.UNDEFINED;
112                 }
113                                 
114                 /*
115                  * @see PatternRule#evaluate(ICharacterScanner)
116                  */
117                 public IToken evaluate(ICharacterScanner scanner) {
118                         IToken result= super.evaluate(scanner);
119                         if (result == fToken)
120                                 return checkForWhitespace(scanner);
121                         return result;
122                 }
123         };
124         
125         
126         private static String[] fgKeywords= {"@author", "@deprecated", "@exception", "@param", "@return", "@see", "@serial", "@serialData", "@serialField", "@since", "@throws", "@version"}; //$NON-NLS-12$ //$NON-NLS-11$ //$NON-NLS-10$ //$NON-NLS-7$ //$NON-NLS-9$ //$NON-NLS-8$ //$NON-NLS-6$ //$NON-NLS-5$ //$NON-NLS-4$ //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
127         
128 //      private static String[] fgTokenProperties= {
129 //              IJavaColorConstants.JAVADOC_KEYWORD,
130 //              IJavaColorConstants.JAVADOC_TAG,
131 //              IJavaColorConstants.JAVADOC_LINK,
132 //              IJavaColorConstants.JAVADOC_DEFAULT
133 //      };                      
134         
135         
136         public PHPDocCodeScanner(JavaColorManager provider, IPreferenceStore store) {
137         //      super(manager, store);
138         //      initialize();  
139       createRules(provider);
140         }
141         
142         public IDocument getDocument() {
143                 return fDocument;
144         }
145         
146         /*
147          * @see AbstractJavaScanner#getTokenProperties()
148          */
149 //      protected String[] getTokenProperties() {
150 //              return fgTokenProperties;
151 //      }
152
153         /* create the rules for PHPDoc
154          */
155         protected List createRules(JavaColorManager provider) {
156                 
157                 List list= new ArrayList();
158                 
159                 
160 //    IToken type = new Token(new TextAttribute(provider.getColor(PHPColorProvider.FUNCTION_NAME)));
161 //    IToken string = new Token(new TextAttribute(provider.getColor(PHPColorProvider.STRING)));
162 //    IToken comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.SINGLE_LINE_COMMENT)));
163 //    IToken multi_comment = new Token(new TextAttribute(provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT)));
164 //    IToken other = new Token(new TextAttribute(provider.getColor(PHPColorProvider.DEFAULT)));
165     
166                 // Add rule for tags.
167         //      Token token= getToken(IJavaColorConstants.JAVADOC_TAG);
168     Token token = new Token(new TextAttribute(provider.getColor(PHPColorProvider.PHPDOC_TAG)));
169                 list.add(new TagRule(token));
170                 
171                 
172                 // Add rule for HTML comments
173                 WordRule wordRule= new WordRule(new HTMLCommentDetector(), token);
174                 wordRule.addWord("<!--", token); //$NON-NLS-1$
175                 wordRule.addWord("--!>", token); //$NON-NLS-1$
176                 list.add(wordRule);
177                 
178                 
179                 // Add rule for links.
180                 token= new Token(new TextAttribute(provider.getColor(PHPColorProvider.PHPDOC_LINK)));
181                 list.add(new SingleLineRule("{@link", "}", token)); //$NON-NLS-2$ //$NON-NLS-1$
182                 
183                 
184                 // Add generic whitespace rule.
185                 list.add(new WhitespaceRule(new PHPWhitespaceDetector()));
186                 
187                 
188                 // Add word rule for keywords.
189                 token= new Token(new TextAttribute(provider.getColor(PHPColorProvider.PHPDOC_DEFAULT)));
190                 wordRule= new WordRule(new PHPDocKeywordDetector(), token);
191                 
192                 token= new Token(new TextAttribute(provider.getColor(PHPColorProvider.PHPDOC_KEYWORD)));
193                 for (int i= 0; i < fgKeywords.length; i++)
194                         wordRule.addWord(fgKeywords[i], token);
195                 list.add(wordRule);
196                 
197                 setDefaultReturnToken( new Token(new TextAttribute(provider.getColor(PHPColorProvider.PHPDOC_DEFAULT))) );
198                 return list;
199         }
200 }
201
202