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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
18 import org.eclipse.jface.text.Assert;
19 import org.eclipse.jface.text.rules.ICharacterScanner;
20 import org.eclipse.jface.text.rules.IRule;
21 import org.eclipse.jface.text.rules.IToken;
22 import org.eclipse.jface.text.rules.IWordDetector;
23 import org.eclipse.jface.text.rules.Token;
26 * An implementation of <code>IRule</code> capable of detecting words.
28 * Word rules also allow for the association of tokens with specific words. That
29 * is, not only can the rule be used to provide tokens for exact matches, but
30 * also for the generalized notion of a word in the context in which it is used.
31 * A word rules uses a word detector to determine what a word is.
34 * This word rule allows a word detector to be shared among different word
35 * matchers. Its up to the word matchers to decide if a word matches and, in
36 * this a case, which token is associated with that word.
42 public class CombinedWordRule implements IRule {
45 * Word matcher, that associates matched words with tokens.
47 public static class WordMatcher {
49 /** The table of predefined words and token for this matcher */
50 private Map fWords = new HashMap();
53 * Adds a word and the token to be returned if it is detected.
56 * the word this rule will search for, may not be
59 * the token to be returned if the word has been found, may
60 * not be <code>null</code>
62 public void addWord(String word, IToken token) {
63 Assert.isNotNull(word);
64 Assert.isNotNull(token);
66 fWords.put(new CharacterBuffer(word), token);
70 * Returns the token associated to the given word and the scanner state.
76 * @return the token or <code>null</code> if none is associated by
79 public IToken evaluate(ICharacterScanner scanner, CharacterBuffer word) {
80 IToken token = (IToken) fWords.get(word);
83 return Token.UNDEFINED;
89 public void clearWords() {
95 * Character buffer, mutable <b>or</b> suitable for use as key in hash
98 public static class CharacterBuffer {
100 /** Buffer content */
101 private char[] fContent;
103 /** Buffer content size */
104 private int fLength = 0;
106 /** Is hash code cached? */
107 private boolean fIsHashCached = false;
110 private int fHashCode;
113 * Initialize with the given capacity.
116 * the initial capacity
118 public CharacterBuffer(int capacity) {
119 fContent = new char[capacity];
123 * Initialize with the given content.
126 * the initial content
128 public CharacterBuffer(String content) {
129 fContent = content.toCharArray();
130 fLength = content.length();
134 * Empties this buffer.
136 public void clear() {
137 fIsHashCached = false;
142 * Appends the given character to the buffer.
147 public void append(char c) {
148 fIsHashCached = false;
149 if (fLength == fContent.length) {
150 char[] old = fContent;
151 fContent = new char[old.length << 1];
152 System.arraycopy(old, 0, fContent, 0, old.length);
154 fContent[fLength++] = c;
158 * Returns the length of the content.
162 public int length() {
167 * Returns the content as string.
169 * @return the content
171 public String toString() {
172 return new String(fContent, 0, fLength);
176 * Returns the character at the given position.
180 * @return the character at position <code>i</code>
182 public char charAt(int i) {
187 * @see java.lang.Object#hashCode()
189 public int hashCode() {
194 for (int i = 0, n = fLength; i < n; i++)
195 hash = 29 * hash + fContent[i];
197 fIsHashCached = true;
202 * @see java.lang.Object#equals(java.lang.Object)
204 public boolean equals(Object obj) {
207 if (!(obj instanceof CharacterBuffer))
209 CharacterBuffer buffer = (CharacterBuffer) obj;
210 int length = buffer.length();
211 if (length != fLength)
213 for (int i = 0; i < length; i++)
214 if (buffer.charAt(i) != fContent[i])
220 * Is the content equal to the given string?
224 * @return <code>true</code> iff the content is the same character
225 * sequence as in the string
227 public boolean equals(String string) {
228 int length = string.length();
229 if (length != fLength)
231 for (int i = 0; i < length; i++)
232 if (string.charAt(i) != fContent[i])
238 /** Internal setting for the uninitialized column constraint */
239 private static final int UNDEFINED = -1;
241 /** The word detector used by this rule */
242 private IWordDetector fDetector;
245 * The default token to be returned on success and if nothing else has been
248 private IToken fDefaultToken;
250 /** The column constraint */
251 private int fColumn = UNDEFINED;
253 /** Buffer used for pattern detection */
254 private CharacterBuffer fBuffer = new CharacterBuffer(16);
256 /** List of word matchers */
257 private List fMatchers = new ArrayList();
260 * Creates a rule which, with the help of an word detector, will return the
261 * token associated with the detected word. If no token has been associated,
262 * the scanner will be rolled back and an undefined token will be returned
263 * in order to allow any subsequent rules to analyze the characters.
266 * the word detector to be used by this rule, may not be
269 * @see #addWord(String, IToken)
271 public CombinedWordRule(IWordDetector detector) {
272 this(detector, null, Token.UNDEFINED);
276 * Creates a rule which, with the help of an word detector, will return the
277 * token associated with the detected word. If no token has been associated,
278 * the specified default token will be returned.
281 * the word detector to be used by this rule, may not be
283 * @param defaultToken
284 * the default token to be returned on success if nothing else is
285 * specified, may not be <code>null</code>
287 * @see #addWord(String, IToken)
289 public CombinedWordRule(IWordDetector detector, IToken defaultToken) {
290 this(detector, null, defaultToken);
294 * Creates a rule which, with the help of an word detector, will return the
295 * token associated with the detected word. If no token has been associated,
296 * the scanner will be rolled back and an undefined token will be returned
297 * in order to allow any subsequent rules to analyze the characters.
300 * the word detector to be used by this rule, may not be
303 * the initial word matcher
305 * @see #addWord(String, IToken)
307 public CombinedWordRule(IWordDetector detector, WordMatcher matcher) {
308 this(detector, matcher, Token.UNDEFINED);
312 * Creates a rule which, with the help of an word detector, will return the
313 * token associated with the detected word. If no token has been associated,
314 * the specified default token will be returned.
317 * the word detector to be used by this rule, may not be
320 * the initial word matcher
321 * @param defaultToken
322 * the default token to be returned on success if nothing else is
323 * specified, may not be <code>null</code>
325 * @see #addWord(String, IToken)
327 public CombinedWordRule(IWordDetector detector, WordMatcher matcher,
328 IToken defaultToken) {
330 Assert.isNotNull(detector);
331 Assert.isNotNull(defaultToken);
333 fDetector = detector;
334 fDefaultToken = defaultToken;
336 addWordMatcher(matcher);
340 * Adds the given matcher.
345 public void addWordMatcher(WordMatcher matcher) {
346 fMatchers.add(matcher);
350 * Sets a column constraint for this rule. If set, the rule's token will
351 * only be returned if the pattern is detected starting at the specified
352 * column. If the column is smaller then 0, the column constraint is
353 * considered removed.
356 * the column in which the pattern starts
358 public void setColumnConstraint(int column) {
365 * @see IRule#evaluate(ICharacterScanner)
367 public IToken evaluate(ICharacterScanner scanner) {
368 int c = scanner.read();
369 if (fDetector.isWordStart((char) c)) {
370 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
374 fBuffer.append((char) c);
376 } while (c != ICharacterScanner.EOF
377 && fDetector.isWordPart((char) c));
380 for (int i = 0, n = fMatchers.size(); i < n; i++) {
381 IToken token = ((WordMatcher) fMatchers.get(i)).evaluate(
383 if (!token.isUndefined())
387 if (fDefaultToken.isUndefined())
388 unreadBuffer(scanner);
390 return fDefaultToken;
395 return Token.UNDEFINED;
399 * Returns the characters in the buffer to the scanner.
402 * the scanner to be used
404 private void unreadBuffer(ICharacterScanner scanner) {
405 for (int i = fBuffer.length() - 1; i >= 0; i--)