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;
13 import java.util.ArrayList;
14 import java.util.List;
16 import net.sourceforge.phpdt.internal.ui.text.AbstractJavaScanner;
17 import net.sourceforge.phpdt.ui.text.IColorManager;
18 import net.sourceforge.phpeclipse.IPreferenceConstants;
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.rules.ICharacterScanner;
25 import org.eclipse.jface.text.rules.IToken;
26 import org.eclipse.jface.text.rules.IWordDetector;
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;
33 * A rule based PHPDoc scanner.
35 public final class PHPDocCodeScanner extends AbstractJavaScanner {
38 * A key word detector.
40 static class JavaDocKeywordDetector implements IWordDetector {
43 * @see IWordDetector#isWordStart
45 public boolean isWordStart(char c) {
50 * @see IWordDetector#isWordPart
52 public boolean isWordPart(char c) {
53 return Character.isLetter(c);
58 * Detector for HTML comment delimiters.
60 static class HTMLCommentDetector implements IWordDetector {
63 * @see IWordDetector#isWordStart
65 public boolean isWordStart(char c) {
66 return (c == '<' || c == '-');
70 * @see IWordDetector#isWordPart
72 public boolean isWordPart(char c) {
73 return (c == '-' || c == '!' || c == '>');
77 class TagRule extends SingleLineRule {
82 public TagRule(IToken token) {
83 super("<", ">", token, (char) 0); //$NON-NLS-2$ //$NON-NLS-1$
89 public TagRule(IToken token, char escapeCharacter) {
90 super("<", ">", token, escapeCharacter); //$NON-NLS-2$ //$NON-NLS-1$
93 private IToken checkForWhitespace(ICharacterScanner scanner) {
97 char c = getDocument().getChar(getTokenOffset() + 1);
98 if (!Character.isWhitespace(c))
101 } catch (BadLocationException x) {
104 return Token.UNDEFINED;
108 * @see PatternRule#evaluate(ICharacterScanner)
110 public IToken evaluate(ICharacterScanner scanner) {
111 IToken result = super.evaluate(scanner);
112 if (result == fToken)
113 return checkForWhitespace(scanner);
118 private static String[] fgKeywords = {"@author", "@deprecated", "@exception", "@link", "@param", "@return", "@see", "@since", "@throws", "@value", "@version", "@license", "@abstract", "@access", "@category",
119 "@copyright", "@example", "@final", "@filesource", "@global", "@ignore", "@internal", "@link", "@method", "@name", "@package", "@param", "@property", "@static",
120 "@staticvar", "@subpackage", "@todo", "@tutorial", "@uses", "@var","@id", "inheritdoc", "@property-read", "@property-write", "@source" }; //$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$
122 private static String[] fgTokenProperties = {
123 IPreferenceConstants.PHPDOC_KEYWORD,
124 IPreferenceConstants.PHPDOC_TAG, IPreferenceConstants.PHPDOC_LINK,
125 IPreferenceConstants.PHPDOC_DEFAULT };
127 public PHPDocCodeScanner(IColorManager manager, IPreferenceStore store) {
128 super(manager, store);
132 public IDocument getDocument() {
137 * @see AbstractJavaScanner#getTokenProperties()
139 protected String[] getTokenProperties() {
140 return fgTokenProperties;
144 * @see AbstractJavaScanner#createRules()
146 protected List createRules() {
148 List list = new ArrayList();
150 // Add rule for tags.
151 Token token = getToken(IPreferenceConstants.PHPDOC_TAG);
152 list.add(new TagRule(token));
154 // Add rule for HTML comments
155 WordRule wordRule = new WordRule(new HTMLCommentDetector(), token);
156 wordRule.addWord("<!--", token); //$NON-NLS-1$
157 wordRule.addWord("--!>", token); //$NON-NLS-1$
160 // Add rule for links.
161 token = getToken(IPreferenceConstants.PHPDOC_LINK);
162 list.add(new SingleLineRule("{@link", "}", token)); //$NON-NLS-2$ //$NON-NLS-1$
164 // Add generic whitespace rule.
165 list.add(new WhitespaceRule(new PHPWhitespaceDetector()));
167 // Add word rule for keywords.
168 token = getToken(IPreferenceConstants.PHPDOC_DEFAULT);
169 wordRule = new WordRule(new JavaDocKeywordDetector(), token);
171 token = getToken(IPreferenceConstants.PHPDOC_KEYWORD);
172 for (int i = 0; i < fgKeywords.length; i++)
173 wordRule.addWord(fgKeywords[i], token);
176 setDefaultReturnToken(getToken(IPreferenceConstants.PHPDOC_DEFAULT));