Change Eclipse-LazyStart: true to Bundle-ActivationPolicy: lazy (upgrade to The Equin...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / SmartyDocCodeScanner.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.phpeclipse.phpeditor.php;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
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;
20
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;
31
32 /**
33  * A rule based SmartyDoc scanner.
34  */
35 public final class SmartyDocCodeScanner extends AbstractJavaScanner {
36
37         /**
38          * A key word detector.
39          */
40         static class JavaDocKeywordDetector implements IWordDetector {
41
42                 /**
43                  * @see IWordDetector#isWordStart
44                  */
45                 public boolean isWordStart(char c) {
46                         return (c == '@');
47                 }
48
49                 /**
50                  * @see IWordDetector#isWordPart
51                  */
52                 public boolean isWordPart(char c) {
53                         return Character.isLetter(c);
54                 }
55         };
56
57         /**
58          * Detector for HTML comment delimiters.
59          */
60         static class HTMLCommentDetector implements IWordDetector {
61
62                 /**
63                  * @see IWordDetector#isWordStart
64                  */
65                 public boolean isWordStart(char c) {
66                         return (c == '<' || c == '-');
67                 }
68
69                 /**
70                  * @see IWordDetector#isWordPart
71                  */
72                 public boolean isWordPart(char c) {
73                         return (c == '-' || c == '!' || c == '>');
74                 }
75         };
76
77         class TagRule extends SingleLineRule {
78
79                 /*
80                  * @see SingleLineRule
81                  */
82                 public TagRule(IToken token) {
83                         super("<", ">", token, (char) 0); //$NON-NLS-2$ //$NON-NLS-1$
84                 }
85
86                 /*
87                  * @see SingleLineRule
88                  */
89                 public TagRule(IToken token, char escapeCharacter) {
90                         super("<", ">", token, escapeCharacter); //$NON-NLS-2$ //$NON-NLS-1$
91                 }
92
93                 private IToken checkForWhitespace(ICharacterScanner scanner) {
94
95                         try {
96
97                                 char c = getDocument().getChar(getTokenOffset() + 1);
98                                 if (!Character.isWhitespace(c))
99                                         return fToken;
100
101                         } catch (BadLocationException x) {
102                         }
103
104                         return Token.UNDEFINED;
105                 }
106
107                 /*
108                  * @see PatternRule#evaluate(ICharacterScanner)
109                  */
110                 public IToken evaluate(ICharacterScanner scanner) {
111                         IToken result = super.evaluate(scanner);
112                         if (result == fToken)
113                                 return checkForWhitespace(scanner);
114                         return result;
115                 }
116         };
117
118         private static String[] fgKeywords = {
119                 "@author", "@deprecated", "@exception", "@link", "@param", "@return", "@see", "@since", "@throws", "@value", "@version", "@license", "@abstract", "@access", "@category",
120                 "@copyright", "@example", "@final", "@filesource", "@global", "@ignore", "@internal", "@link", "@method", "@name", "@package", "@param", "@property", "@static",
121                 "@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
123         private static String[] fgTokenProperties = {
124                         IPreferenceConstants.PHPDOC_KEYWORD,
125                         IPreferenceConstants.PHPDOC_TAG, IPreferenceConstants.PHPDOC_LINK,
126                         IPreferenceConstants.PHPDOC_DEFAULT };
127
128         public SmartyDocCodeScanner(IColorManager manager, IPreferenceStore store) {
129                 super(manager, store);
130                 initialize();
131         }
132
133         public IDocument getDocument() {
134                 return fDocument;
135         }
136
137         /*
138          * @see AbstractJavaScanner#getTokenProperties()
139          */
140         protected String[] getTokenProperties() {
141                 return fgTokenProperties;
142         }
143
144         /*
145          * @see AbstractJavaScanner#createRules()
146          */
147         protected List createRules() {
148
149                 List list = new ArrayList();
150
151                 // Add rule for tags.
152                 Token token = getToken(IPreferenceConstants.PHPDOC_TAG);
153                 list.add(new TagRule(token));
154
155                 // Add rule for HTML comments
156                 WordRule wordRule = new WordRule(new HTMLCommentDetector(), token);
157                 wordRule.addWord("<!--", token); //$NON-NLS-1$
158                 wordRule.addWord("--!>", token); //$NON-NLS-1$
159                 list.add(wordRule);
160
161                 // Add rule for links.
162                 token = getToken(IPreferenceConstants.PHPDOC_LINK);
163                 list.add(new SingleLineRule("{@link", "}", token)); //$NON-NLS-2$ //$NON-NLS-1$
164
165                 // Add generic whitespace rule.
166                 list.add(new WhitespaceRule(new PHPWhitespaceDetector()));
167
168                 // Add word rule for keywords.
169                 token = getToken(IPreferenceConstants.PHPDOC_DEFAULT);
170                 wordRule = new WordRule(new JavaDocKeywordDetector(), token);
171
172                 token = getToken(IPreferenceConstants.PHPDOC_KEYWORD);
173                 for (int i = 0; i < fgKeywords.length; i++)
174                         wordRule.addWord(fgKeywords[i], token);
175                 list.add(wordRule);
176
177                 setDefaultReturnToken(getToken(IPreferenceConstants.PHPDOC_DEFAULT));
178                 return list;
179         }
180 }