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.AbstractJavaScanner;
18 import net.sourceforge.phpdt.ui.text.IColorManager;
19 import net.sourceforge.phpeclipse.IPreferenceConstants;
20 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.text.BadLocationException;
24 import org.eclipse.jface.text.IDocument;
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.SingleLineRule;
29 import org.eclipse.jface.text.rules.Token;
30 import org.eclipse.jface.text.rules.WhitespaceRule;
31 import org.eclipse.jface.text.rules.WordRule;
37 * A rule based PHPDoc scanner.
39 public final class PHPDocCodeScanner extends AbstractJavaScanner {
43 * A key word detector.
45 static class JavaDocKeywordDetector implements IWordDetector {
48 * @see IWordDetector#isWordStart
50 public boolean isWordStart(char c) {
55 * @see IWordDetector#isWordPart
57 public boolean isWordPart(char c) {
58 return Character.isLetter(c);
64 * Detector for HTML comment delimiters.
66 static class HTMLCommentDetector implements IWordDetector {
69 * @see IWordDetector#isWordStart
71 public boolean isWordStart(char c) {
72 return (c == '<' || c == '-');
76 * @see IWordDetector#isWordPart
78 public boolean isWordPart(char c) {
79 return (c == '-' || c == '!' || c == '>');
83 class TagRule extends SingleLineRule {
88 public TagRule(IToken token) {
89 super("<", ">", token, (char) 0); //$NON-NLS-2$ //$NON-NLS-1$
95 public TagRule(IToken token, char escapeCharacter) {
96 super("<", ">", token, escapeCharacter); //$NON-NLS-2$ //$NON-NLS-1$
99 private IToken checkForWhitespace(ICharacterScanner scanner) {
103 char c= getDocument().getChar(getTokenOffset() + 1);
104 if (!Character.isWhitespace(c))
107 } catch (BadLocationException x) {
110 return Token.UNDEFINED;
114 * @see PatternRule#evaluate(ICharacterScanner)
116 public IToken evaluate(ICharacterScanner scanner) {
117 IToken result= super.evaluate(scanner);
118 if (result == fToken)
119 return checkForWhitespace(scanner);
125 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 private static String[] fgTokenProperties= {
128 IPreferenceConstants.PHPDOC_KEYWORD,
129 IPreferenceConstants.PHPDOC_TAG,
130 IPreferenceConstants.PHPDOC_LINK,
131 IPreferenceConstants.PHPDOC_DEFAULT
135 public PHPDocCodeScanner(IColorManager manager, IPreferenceStore store) {
136 super(manager, store);
140 public IDocument getDocument() {
145 * @see AbstractJavaScanner#getTokenProperties()
147 protected String[] getTokenProperties() {
148 return fgTokenProperties;
152 * @see AbstractJavaScanner#createRules()
154 protected List createRules() {
156 List list= new ArrayList();
158 // Add rule for tags.
159 Token token= getToken(IPreferenceConstants.PHPDOC_TAG);
160 list.add(new TagRule(token));
163 // Add rule for HTML comments
164 WordRule wordRule= new WordRule(new HTMLCommentDetector(), token);
165 wordRule.addWord("<!--", token); //$NON-NLS-1$
166 wordRule.addWord("--!>", token); //$NON-NLS-1$
170 // Add rule for links.
171 token= getToken(IPreferenceConstants.PHPDOC_LINK);
172 list.add(new SingleLineRule("{@link", "}", token)); //$NON-NLS-2$ //$NON-NLS-1$
175 // Add generic whitespace rule.
176 list.add(new WhitespaceRule(new PHPWhitespaceDetector()));
179 // Add word rule for keywords.
180 token= getToken(IPreferenceConstants.PHPDOC_DEFAULT);
181 wordRule= new WordRule(new JavaDocKeywordDetector(), token);
183 token= getToken(IPreferenceConstants.PHPDOC_KEYWORD);
184 for (int i= 0; i < fgKeywords.length; i++)
185 wordRule.addWord(fgKeywords[i], token);
188 setDefaultReturnToken(getToken(IPreferenceConstants.PHPDOC_DEFAULT));