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;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
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;
28 * An implementation of <code>IRule</code> capable of detecting words.
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>
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.
43 public class CombinedWordRule implements IRule {
46 * Word matcher, that associates matched words with tokens.
48 public static class WordMatcher {
50 /** The table of predefined words and token for this matcher */
51 private Map fWords= new HashMap();
54 * Adds a word and the token to be returned if it is detected.
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>
59 public void addWord(String word, IToken token) {
60 Assert.isNotNull(word);
61 Assert.isNotNull(token);
63 fWords.put(new CharacterBuffer(word), token);
67 * Returns the token associated to the given word and the scanner state.
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
73 public IToken evaluate(ICharacterScanner scanner, CharacterBuffer word) {
74 IToken token= (IToken) fWords.get(word);
77 return Token.UNDEFINED;
83 public void clearWords() {
89 * Character buffer, mutable <b>or</b> suitable for use as key in hash maps.
91 public static class CharacterBuffer {
94 private char[] fContent;
95 /** Buffer content size */
96 private int fLength= 0;
98 /** Is hash code cached? */
99 private boolean fIsHashCached= false;
101 private int fHashCode;
104 * Initialize with the given capacity.
106 * @param capacity the initial capacity
108 public CharacterBuffer(int capacity) {
109 fContent= new char[capacity];
113 * Initialize with the given content.
115 * @param string the initial content
117 public CharacterBuffer(String content) {
118 fContent= content.toCharArray();
119 fLength= content.length();
123 * Empties this buffer.
125 public void clear() {
126 fIsHashCached= false;
131 * Appends the given character to the buffer.
133 * @param c the character
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);
142 fContent[fLength++]= c;
146 * Returns the length of the content.
150 public int length() {
155 * Returns the content as string.
157 * @return the content
159 public String toString() {
160 return new String(fContent, 0, fLength);
164 * Returns the character at the given position.
166 * @param i the position
167 * @return the character at position <code>i</code>
169 public char charAt(int i) {
174 * @see java.lang.Object#hashCode()
176 public int hashCode() {
181 for (int i= 0, n= fLength; i < n; i++)
182 hash= 29*hash + fContent[i];
190 * @see java.lang.Object#equals(java.lang.Object)
192 public boolean equals(Object obj) {
195 if (!(obj instanceof CharacterBuffer))
197 CharacterBuffer buffer= (CharacterBuffer) obj;
198 int length= buffer.length();
199 if (length != fLength)
201 for (int i= 0; i < length; i++)
202 if (buffer.charAt(i) != fContent[i])
208 * Is the content equal to the given string?
210 * @param string the string
211 * @return <code>true</code> iff the content is the same character sequence as in the string
213 public boolean equals(String string) {
214 int length= string.length();
215 if (length != fLength)
217 for (int i= 0; i < length; i++)
218 if (string.charAt(i) != fContent[i])
224 /** Internal setting for the uninitialized column constraint */
225 private static final int UNDEFINED= -1;
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);
236 /** List of word matchers */
237 private List fMatchers= new ArrayList();
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.
245 * @param detector the word detector to be used by this rule, may not be <code>null</code>
247 * @see #addWord(String, IToken)
249 public CombinedWordRule(IWordDetector detector) {
250 this(detector, null, Token.UNDEFINED);
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.
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>
262 * @see #addWord(String, IToken)
264 public CombinedWordRule(IWordDetector detector, IToken defaultToken) {
265 this(detector, null, defaultToken);
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.
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
277 * @see #addWord(String, IToken)
279 public CombinedWordRule(IWordDetector detector, WordMatcher matcher) {
280 this(detector, matcher, Token.UNDEFINED);
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.
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>
293 * @see #addWord(String, IToken)
295 public CombinedWordRule(IWordDetector detector, WordMatcher matcher, IToken defaultToken) {
297 Assert.isNotNull(detector);
298 Assert.isNotNull(defaultToken);
301 fDefaultToken= defaultToken;
303 addWordMatcher(matcher);
308 * Adds the given matcher.
310 * @param matcher the matcher
312 public void addWordMatcher(WordMatcher matcher) {
313 fMatchers.add(matcher);
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.
322 * @param column the column in which the pattern starts
324 public void setColumnConstraint(int column) {
331 * @see IRule#evaluate(ICharacterScanner)
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)) {
340 fBuffer.append((char) c);
342 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
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())
351 if (fDefaultToken.isUndefined())
352 unreadBuffer(scanner);
354 return fDefaultToken;
359 return Token.UNDEFINED;
363 * Returns the characters in the buffer to the scanner.
365 * @param scanner the scanner to be used
367 private void unreadBuffer(ICharacterScanner scanner) {
368 for (int i= fBuffer.length() - 1; i >= 0; i--)