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
9 IBM Corporation - Initial implementation
10 **********************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.phpdoc;
14 import java.util.ArrayList;
15 import java.util.List;
17 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
18 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
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;
36 * A rule based PHPDoc scanner.
38 public final class PHPDocCodeScanner extends RuleBasedScanner {
42 * A key word detector.
44 static class PHPDocKeywordDetector implements IWordDetector {
47 * @see IWordDetector#isWordStart
49 public boolean isWordStart(char c) {
54 * @see IWordDetector#isWordPart
56 public boolean isWordPart(char c) {
57 return Character.isLetter(c);
63 * Detector for HTML comment delimiters.
65 static class HTMLCommentDetector implements IWordDetector {
68 * @see IWordDetector#isWordStart
70 public boolean isWordStart(char c) {
71 return (c == '<' || c == '-');
75 * @see IWordDetector#isWordPart
77 public boolean isWordPart(char c) {
78 return (c == '-' || c == '!' || c == '>');
82 class TagRule extends SingleLineRule {
87 public TagRule(IToken token) {
88 super("<", ">", token, (char) 0); //$NON-NLS-2$ //$NON-NLS-1$
94 public TagRule(IToken token, char escapeCharacter) {
95 super("<", ">", token, escapeCharacter); //$NON-NLS-2$ //$NON-NLS-1$
98 private IToken checkForWhitespace(ICharacterScanner scanner) {
102 char c= getDocument().getChar(getTokenOffset() + 1);
103 if (!Character.isWhitespace(c))
106 } catch (BadLocationException x) {
109 return Token.UNDEFINED;
113 * @see PatternRule#evaluate(ICharacterScanner)
115 public IToken evaluate(ICharacterScanner scanner) {
116 IToken result= super.evaluate(scanner);
117 if (result == fToken)
118 return checkForWhitespace(scanner);
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$
126 // private static String[] fgTokenProperties= {
127 // IJavaColorConstants.JAVADOC_KEYWORD,
128 // IJavaColorConstants.JAVADOC_TAG,
129 // IJavaColorConstants.JAVADOC_LINK,
130 // IJavaColorConstants.JAVADOC_DEFAULT
134 public PHPDocCodeScanner(PHPColorProvider provider) { // , IPreferenceStore store) {
135 // super(manager, store);
137 createRules(provider);
140 public IDocument getDocument() {
145 * @see AbstractJavaScanner#getTokenProperties()
147 // protected String[] getTokenProperties() {
148 // return fgTokenProperties;
151 /* create the rules for PHPDoc
153 protected List createRules(PHPColorProvider provider) {
155 List list= new ArrayList();
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)));
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));
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$
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$
182 // Add generic whitespace rule.
183 list.add(new WhitespaceRule(new PHPWhitespaceDetector()));
186 // Add word rule for keywords.
187 token= new Token(new TextAttribute(provider.getColor(PHPColorProvider.PHPDOC_DEFAULT)));
188 wordRule= new WordRule(new PHPDocKeywordDetector(), token);
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);
195 setDefaultReturnToken( new Token(new TextAttribute(provider.getColor(PHPColorProvider.PHPDOC_DEFAULT))) );