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;
19 //import org.eclipse.jface.text.Assert;
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.jface.text.rules.ICharacterScanner;
22 import org.eclipse.jface.text.rules.IRule;
23 import org.eclipse.jface.text.rules.IToken;
24 import org.eclipse.jface.text.rules.IWordDetector;
25 import org.eclipse.jface.text.rules.Token;
28 * An implementation of <code>IRule</code> capable of detecting words.
30 * Word rules also allow for the association of tokens with specific words. That
31 * is, not only can the rule be used to provide tokens for exact matches, but
32 * 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.
36 * This word rule allows a word detector to be shared among different word
37 * matchers. Its up to the word matchers to decide if a word matches and, in
38 * this a case, which token is associated with that word.
44 public class CombinedWordRule implements IRule {
47 * Word matcher, that associates matched words with tokens.
49 public static class WordMatcher {
51 /** The table of predefined words and token for this matcher */
52 private Map fWords = new HashMap();
55 * Adds a word and the token to be returned if it is detected.
58 * the word this rule will search for, may not be
61 * the token to be returned if the word has been found, may
62 * not be <code>null</code>
64 public void addWord(String word, IToken token) {
65 Assert.isNotNull(word);
66 Assert.isNotNull(token);
68 fWords.put(new CharacterBuffer(word), token);
72 * Returns the token associated to the given word and the scanner state.
78 * @return the token or <code>null</code> if none is associated by
81 public IToken evaluate(ICharacterScanner scanner, CharacterBuffer word) {
82 IToken token = (IToken) fWords.get(word);
85 return Token.UNDEFINED;
91 public void clearWords() {
97 * Character buffer, mutable <b>or</b> suitable for use as key in hash
100 public static class CharacterBuffer {
102 /** Buffer content */
103 private char[] fContent;
105 /** Buffer content size */
106 private int fLength = 0;
108 /** Is hash code cached? */
109 private boolean fIsHashCached = false;
112 private int fHashCode;
115 * Initialize with the given capacity.
118 * the initial capacity
120 public CharacterBuffer(int capacity) {
121 fContent = new char[capacity];
125 * Initialize with the given content.
128 * the initial content
130 public CharacterBuffer(String content) {
131 fContent = content.toCharArray();
132 fLength = content.length();
136 * Empties this buffer.
138 public void clear() {
139 fIsHashCached = false;
144 * Appends the given character to the buffer.
149 public void append(char c) {
150 fIsHashCached = false;
151 if (fLength == fContent.length) {
152 char[] old = fContent;
153 fContent = new char[old.length << 1];
154 System.arraycopy(old, 0, fContent, 0, old.length);
156 fContent[fLength++] = c;
160 * Returns the length of the content.
164 public int length() {
169 * Returns the content as string.
171 * @return the content
173 public String toString() {
174 return new String(fContent, 0, fLength);
178 * Returns the character at the given position.
182 * @return the character at position <code>i</code>
184 public char charAt(int i) {
189 * @see java.lang.Object#hashCode()
191 public int hashCode() {
196 for (int i = 0, n = fLength; i < n; i++)
197 hash = 29 * hash + fContent[i];
199 fIsHashCached = true;
204 * @see java.lang.Object#equals(java.lang.Object)
206 public boolean equals(Object obj) {
209 if (!(obj instanceof CharacterBuffer))
211 CharacterBuffer buffer = (CharacterBuffer) obj;
212 int length = buffer.length();
213 if (length != fLength)
215 for (int i = 0; i < length; i++)
216 if (buffer.charAt(i) != fContent[i])
222 * Is the content equal to the given string?
226 * @return <code>true</code> iff the content is the same character
227 * sequence as in the string
229 public boolean equals(String string) {
230 int length = string.length();
231 if (length != fLength)
233 for (int i = 0; i < length; i++)
234 if (string.charAt(i) != fContent[i])
240 /** Internal setting for the uninitialized column constraint */
241 private static final int UNDEFINED = -1;
243 /** The word detector used by this rule */
244 private IWordDetector fDetector;
247 * The default token to be returned on success and if nothing else has been
250 private IToken fDefaultToken;
252 /** The column constraint */
253 private int fColumn = UNDEFINED;
255 /** Buffer used for pattern detection */
256 private CharacterBuffer fBuffer = new CharacterBuffer(16);
258 /** List of word matchers */
259 private List fMatchers = new ArrayList();
262 * Creates a rule which, with the help of an word detector, will return the
263 * token associated with the detected word. If no token has been associated,
264 * the scanner will be rolled back and an undefined token will be returned
265 * in order to allow any subsequent rules to analyze the characters.
268 * the word detector to be used by this rule, may not be
271 * @see #addWord(String, IToken)
273 public CombinedWordRule(IWordDetector detector) {
274 this(detector, null, Token.UNDEFINED);
278 * Creates a rule which, with the help of an word detector, will return the
279 * token associated with the detected word. If no token has been associated,
280 * the specified default token will be returned.
283 * the word detector to be used by this rule, may not be
285 * @param defaultToken
286 * the default token to be returned on success if nothing else is
287 * specified, may not be <code>null</code>
289 * @see #addWord(String, IToken)
291 public CombinedWordRule(IWordDetector detector, IToken defaultToken) {
292 this(detector, null, defaultToken);
296 * Creates a rule which, with the help of an word detector, will return the
297 * token associated with the detected word. If no token has been associated,
298 * the scanner will be rolled back and an undefined token will be returned
299 * in order to allow any subsequent rules to analyze the characters.
302 * the word detector to be used by this rule, may not be
305 * the initial word matcher
307 * @see #addWord(String, IToken)
309 public CombinedWordRule(IWordDetector detector, WordMatcher matcher) {
310 this(detector, matcher, Token.UNDEFINED);
314 * Creates a rule which, with the help of an word detector, will return the
315 * token associated with the detected word. If no token has been associated,
316 * the specified default token will be returned.
319 * the word detector to be used by this rule, may not be
322 * the initial word matcher
323 * @param defaultToken
324 * the default token to be returned on success if nothing else is
325 * specified, may not be <code>null</code>
327 * @see #addWord(String, IToken)
329 public CombinedWordRule(IWordDetector detector, WordMatcher matcher,
330 IToken defaultToken) {
332 Assert.isNotNull(detector);
333 Assert.isNotNull(defaultToken);
335 fDetector = detector;
336 fDefaultToken = defaultToken;
338 addWordMatcher(matcher);
342 * Adds the given matcher.
347 public void addWordMatcher(WordMatcher matcher) {
348 fMatchers.add(matcher);
352 * Sets a column constraint for this rule. If set, the rule's token will
353 * only be returned if the pattern is detected starting at the specified
354 * column. If the column is smaller then 0, the column constraint is
355 * considered removed.
358 * the column in which the pattern starts
360 public void setColumnConstraint(int column) {
367 * @see IRule#evaluate(ICharacterScanner)
369 public IToken evaluate(ICharacterScanner scanner) {
370 int c = scanner.read();
371 if (fDetector.isWordStart((char) c)) {
372 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
376 fBuffer.append((char) c);
378 } while (c != ICharacterScanner.EOF
379 && fDetector.isWordPart((char) c));
382 for (int i = 0, n = fMatchers.size(); i < n; i++) {
383 IToken token = ((WordMatcher) fMatchers.get(i)).evaluate(
385 if (!token.isUndefined())
389 if (fDefaultToken.isUndefined())
390 unreadBuffer(scanner);
392 return fDefaultToken;
397 return Token.UNDEFINED;
401 * Returns the characters in the buffer to the scanner.
404 * the scanner to be used
406 private void unreadBuffer(ICharacterScanner scanner) {
407 for (int i = fBuffer.length() - 1; i >= 0; i--)