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.phpdt.internal.ui.text.JavaColorManager;
18 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
19 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
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;
38 * A rule based PHPDoc scanner.
40 public final class PHPDocCodeScanner extends RuleBasedScanner {
44 * A key word detector.
46 static class PHPDocKeywordDetector implements IWordDetector {
49 * @see IWordDetector#isWordStart
51 public boolean isWordStart(char c) {
56 * @see IWordDetector#isWordPart
58 public boolean isWordPart(char c) {
59 return Character.isLetter(c);
65 * Detector for HTML comment delimiters.
67 static class HTMLCommentDetector implements IWordDetector {
70 * @see IWordDetector#isWordStart
72 public boolean isWordStart(char c) {
73 return (c == '<' || c == '-');
77 * @see IWordDetector#isWordPart
79 public boolean isWordPart(char c) {
80 return (c == '-' || c == '!' || c == '>');
84 class TagRule extends SingleLineRule {
89 public TagRule(IToken token) {
90 super("<", ">", token, (char) 0); //$NON-NLS-2$ //$NON-NLS-1$
96 public TagRule(IToken token, char escapeCharacter) {
97 super("<", ">", token, escapeCharacter); //$NON-NLS-2$ //$NON-NLS-1$
100 private IToken checkForWhitespace(ICharacterScanner scanner) {
104 char c= getDocument().getChar(getTokenOffset() + 1);
105 if (!Character.isWhitespace(c))
108 } catch (BadLocationException x) {
111 return Token.UNDEFINED;
115 * @see PatternRule#evaluate(ICharacterScanner)
117 public IToken evaluate(ICharacterScanner scanner) {
118 IToken result= super.evaluate(scanner);
119 if (result == fToken)
120 return checkForWhitespace(scanner);
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$
128 // private static String[] fgTokenProperties= {
129 // IJavaColorConstants.JAVADOC_KEYWORD,
130 // IJavaColorConstants.JAVADOC_TAG,
131 // IJavaColorConstants.JAVADOC_LINK,
132 // IJavaColorConstants.JAVADOC_DEFAULT
136 public PHPDocCodeScanner(JavaColorManager provider, IPreferenceStore store) {
137 // super(manager, store);
139 createRules(provider);
142 public IDocument getDocument() {
147 * @see AbstractJavaScanner#getTokenProperties()
149 // protected String[] getTokenProperties() {
150 // return fgTokenProperties;
153 /* create the rules for PHPDoc
155 protected List createRules(JavaColorManager provider) {
157 List list= new ArrayList();
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)));
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));
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$
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$
184 // Add generic whitespace rule.
185 list.add(new WhitespaceRule(new PHPWhitespaceDetector()));
188 // Add word rule for keywords.
189 token= new Token(new TextAttribute(provider.getColor(PHPColorProvider.PHPDOC_DEFAULT)));
190 wordRule= new WordRule(new PHPDocKeywordDetector(), token);
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);
197 setDefaultReturnToken( new Token(new TextAttribute(provider.getColor(PHPColorProvider.PHPDOC_DEFAULT))) );