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