colored "operators and brackets"
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / CombinedWordRule.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation 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 API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
12
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.eclipse.jface.text.Assert;
20 import org.eclipse.jface.text.rules.ICharacterScanner;
21 import org.eclipse.jface.text.rules.IRule;
22 import org.eclipse.jface.text.rules.IToken;
23 import org.eclipse.jface.text.rules.IWordDetector;
24 import org.eclipse.jface.text.rules.Token;
25
26
27 /**
28  * An implementation of <code>IRule</code> capable of detecting words.
29  * <p>
30  * Word rules also allow for the association of tokens with specific words. 
31  * That is, not only can the rule be used to provide tokens for exact matches, 
32  * but also for the generalized notion of a word in the context in which it is used.
33  * A word rules uses a word detector to determine what a word is.</p>
34  * <p>
35  * This word rule allows a word detector to be shared among different word matchers.
36  * Its up to the word matchers to decide if a word matches and, in this a case, which
37  * token is associated with that word.
38  * </p>
39  * 
40  * @see IWordDetector
41  * @since 3.0
42  */
43 public class CombinedWordRule implements IRule {
44         
45         /**
46          * Word matcher, that associates matched words with tokens.
47          */
48         public static class WordMatcher {
49                 
50                 /** The table of predefined words and token for this matcher */
51                 private Map fWords= new HashMap();
52
53                 /**
54                  * Adds a word and the token to be returned if it is detected.
55                  *
56                  * @param word the word this rule will search for, may not be <code>null</code>
57                  * @param token the token to be returned if the word has been found, may not be <code>null</code>
58                  */
59                 public void addWord(String word, IToken token) {
60                         Assert.isNotNull(word);
61                         Assert.isNotNull(token);                
62                 
63                         fWords.put(new CharacterBuffer(word), token);
64                 }
65                 
66                 /**
67                  * Returns the token associated to the given word and the scanner state.
68                  * 
69                  * @param scanner the scanner
70                  * @param word the word
71                  * @return the token or <code>null</code> if none is associated by this matcher
72                  */
73                 public IToken evaluate(ICharacterScanner scanner, CharacterBuffer word) {
74                         IToken token= (IToken) fWords.get(word);
75                         if (token != null)
76                                 return token;
77                         return Token.UNDEFINED;
78                 }
79                 
80                 /**
81                  * Removes all words.
82                  */
83                 public void clearWords() {
84                         fWords.clear();
85                 }
86         }
87         
88         /**
89          * Character buffer, mutable <b>or</b> suitable for use as key in hash maps.
90          */
91         public static class CharacterBuffer {
92                 
93                 /** Buffer content */
94                 private char[] fContent;
95                 /** Buffer content size */
96                 private int fLength= 0;
97                 
98                 /** Is hash code cached? */
99                 private boolean fIsHashCached= false;
100                 /** The hash code */
101                 private int fHashCode;
102                 
103                 /**
104                  * Initialize with the given capacity.
105                  *
106                  * @param capacity the initial capacity
107                  */
108                 public CharacterBuffer(int capacity) {
109                         fContent= new char[capacity];
110                 }
111                 
112                 /**
113                  * Initialize with the given content.
114                  *
115                  * @param string the initial content
116                  */
117                 public CharacterBuffer(String content) {
118                         fContent= content.toCharArray();
119                         fLength= content.length();
120                 }
121                 
122                 /**
123                  * Empties this buffer.
124                  */
125                 public void clear() {
126                         fIsHashCached= false;
127                         fLength= 0;
128                 }
129                 
130                 /**
131                  * Appends the given character to the buffer.
132                  * 
133                  * @param c the character
134                  */
135                 public void append(char c) {
136                         fIsHashCached= false;
137                         if (fLength == fContent.length) {
138                                 char[] old= fContent;
139                                 fContent= new char[old.length << 1];
140                                 System.arraycopy(old, 0, fContent, 0, old.length);
141                         }
142                         fContent[fLength++]= c;
143                 }
144                 
145                 /**
146                  * Returns the length of the content.
147                  * 
148                  * @return the length
149                  */
150                 public int length() {
151                         return fLength;
152                 }
153                 
154                 /**
155                  * Returns the content as string.
156                  * 
157                  * @return the content
158                  */
159                 public String toString() {
160                         return new String(fContent, 0, fLength);
161                 }
162                 
163                 /**
164                  * Returns the character at the given position.
165                  * 
166                  * @param i the position
167                  * @return the character at position <code>i</code>
168                  */
169                 public char charAt(int i) {
170                         return fContent[i];
171                 }
172                 
173                 /*
174                  * @see java.lang.Object#hashCode()
175                  */
176                 public int hashCode() {
177                         if (fIsHashCached)
178                                 return fHashCode;
179                         
180                         int hash= 0;
181                         for (int i= 0, n= fLength; i < n; i++)
182                                 hash= 29*hash + fContent[i];
183                         fHashCode= hash;
184                         fIsHashCached= true;
185                         return hash;
186                 }
187                 
188                 
189                 /*
190                  * @see java.lang.Object#equals(java.lang.Object)
191                  */
192                 public boolean equals(Object obj) {
193                         if (obj == this)
194                                 return true;
195                         if (!(obj instanceof CharacterBuffer))
196                                 return false;
197                         CharacterBuffer buffer= (CharacterBuffer) obj;
198                         int length= buffer.length();
199                         if (length != fLength)
200                                 return false;
201                         for (int i= 0; i < length; i++)
202                                 if (buffer.charAt(i) != fContent[i])
203                                         return false;
204                         return true;
205                 }
206                 
207                 /**
208                  * Is the content equal to the given string?
209                  * 
210                  * @param string the string
211                  * @return <code>true</code> iff the content is the same character sequence as in the string
212                  */
213                 public boolean equals(String string) {
214                         int length= string.length();
215                         if (length != fLength)
216                                 return false;
217                         for (int i= 0; i < length; i++)
218                                 if (string.charAt(i) != fContent[i])
219                                         return false;
220                         return true;
221                 }
222         }
223
224         /** Internal setting for the uninitialized column constraint */
225         private static final int UNDEFINED= -1;
226         
227         /** The word detector used by this rule */
228         private IWordDetector fDetector;
229         /** The default token to be returned on success and if nothing else has been specified. */
230         private IToken fDefaultToken;
231         /** The column constraint */
232         private int fColumn= UNDEFINED;
233         /** Buffer used for pattern detection */
234         private CharacterBuffer fBuffer= new CharacterBuffer(16);
235         
236         /** List of word matchers */
237         private List fMatchers= new ArrayList();
238
239         /**
240          * Creates a rule which, with the help of an word detector, will return the token
241          * associated with the detected word. If no token has been associated, the scanner 
242          * will be rolled back and an undefined token will be returned in order to allow 
243          * any subsequent rules to analyze the characters.
244          *
245          * @param detector the word detector to be used by this rule, may not be <code>null</code>
246          *
247          * @see #addWord(String, IToken)
248          */
249         public CombinedWordRule(IWordDetector detector) {
250                 this(detector, null, Token.UNDEFINED);
251         }
252
253         /**
254          * Creates a rule which, with the help of an word detector, will return the token
255          * associated with the detected word. If no token has been associated, the
256          * specified default token will be returned.
257          *
258          * @param detector the word detector to be used by this rule, may not be <code>null</code>
259          * @param defaultToken the default token to be returned on success 
260          *              if nothing else is specified, may not be <code>null</code>
261          *
262          * @see #addWord(String, IToken)
263          */
264         public CombinedWordRule(IWordDetector detector, IToken defaultToken) {
265                 this(detector, null, defaultToken);
266         }
267
268         /**
269          * Creates a rule which, with the help of an word detector, will return the token
270          * associated with the detected word. If no token has been associated, the scanner 
271          * will be rolled back and an undefined token will be returned in order to allow 
272          * any subsequent rules to analyze the characters.
273          *
274          * @param detector the word detector to be used by this rule, may not be <code>null</code>
275          * @param matcher the initial word matcher
276          *
277          * @see #addWord(String, IToken)
278          */
279         public CombinedWordRule(IWordDetector detector, WordMatcher matcher) {
280                 this(detector, matcher, Token.UNDEFINED);
281         }
282
283         /**
284          * Creates a rule which, with the help of an word detector, will return the token
285          * associated with the detected word. If no token has been associated, the
286          * specified default token will be returned.
287          *
288          * @param detector the word detector to be used by this rule, may not be <code>null</code>
289          * @param matcher the initial word matcher
290          * @param defaultToken the default token to be returned on success 
291          *              if nothing else is specified, may not be <code>null</code>
292          *
293          * @see #addWord(String, IToken)
294          */
295         public CombinedWordRule(IWordDetector detector, WordMatcher matcher, IToken defaultToken) {
296                 
297                 Assert.isNotNull(detector);
298                 Assert.isNotNull(defaultToken);
299                 
300                 fDetector= detector;
301                 fDefaultToken= defaultToken;
302                 if (matcher != null)
303                         addWordMatcher(matcher);
304         }
305
306         
307         /**
308          * Adds the given matcher.
309          * 
310          * @param matcher the matcher
311          */
312         public void addWordMatcher(WordMatcher matcher) {
313                 fMatchers.add(matcher);
314         }
315         
316         /**
317          * Sets a column constraint for this rule. If set, the rule's token
318          * will only be returned if the pattern is detected starting at the 
319          * specified column. If the column is smaller then 0, the column
320          * constraint is considered removed.
321          *
322          * @param column the column in which the pattern starts
323          */
324         public void setColumnConstraint(int column) {
325                 if (column < 0)
326                         column= UNDEFINED;
327                 fColumn= column;
328         }
329         
330         /*
331          * @see IRule#evaluate(ICharacterScanner)
332          */
333         public IToken evaluate(ICharacterScanner scanner) {
334                 int c= scanner.read();
335                 if (fDetector.isWordStart((char) c)) {
336                         if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
337                                 
338                                 fBuffer.clear();
339                                 do {
340                                         fBuffer.append((char) c);
341                                         c= scanner.read();
342                                 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
343                                 scanner.unread();
344                                 
345                                 for (int i= 0, n= fMatchers.size(); i < n; i++) {
346                                         IToken token= ((WordMatcher) fMatchers.get(i)).evaluate(scanner, fBuffer);
347                                         if (!token.isUndefined())
348                                                 return token;
349                                 }
350                                         
351                                 if (fDefaultToken.isUndefined())
352                                         unreadBuffer(scanner);
353                                         
354                                 return fDefaultToken;
355                         }
356                 }
357                 
358                 scanner.unread();
359                 return Token.UNDEFINED;
360         }
361         
362         /**
363          * Returns the characters in the buffer to the scanner.
364          *
365          * @param scanner the scanner to be used
366          */
367         private void unreadBuffer(ICharacterScanner scanner) {
368                 for (int i= fBuffer.length() - 1; i >= 0; i--)
369                         scanner.unread();
370         }
371 }