comparison org.eclipse.jface.text/src/org/eclipse/jface/text/rules/WordRule.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children dbfb303e8fb0
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.jface.text.rules.WordRule;
14
15 import org.eclipse.jface.text.rules.FastPartitioner; // packageimport
16 import org.eclipse.jface.text.rules.ITokenScanner; // packageimport
17 import org.eclipse.jface.text.rules.Token; // packageimport
18 import org.eclipse.jface.text.rules.RuleBasedScanner; // packageimport
19 import org.eclipse.jface.text.rules.EndOfLineRule; // packageimport
20 import org.eclipse.jface.text.rules.WhitespaceRule; // packageimport
21 import org.eclipse.jface.text.rules.WordPatternRule; // packageimport
22 import org.eclipse.jface.text.rules.IPredicateRule; // packageimport
23 import org.eclipse.jface.text.rules.DefaultPartitioner; // packageimport
24 import org.eclipse.jface.text.rules.NumberRule; // packageimport
25 import org.eclipse.jface.text.rules.SingleLineRule; // packageimport
26 import org.eclipse.jface.text.rules.PatternRule; // packageimport
27 import org.eclipse.jface.text.rules.IWordDetector; // packageimport
28 import org.eclipse.jface.text.rules.RuleBasedDamagerRepairer; // packageimport
29 import org.eclipse.jface.text.rules.ICharacterScanner; // packageimport
30 import org.eclipse.jface.text.rules.IRule; // packageimport
31 import org.eclipse.jface.text.rules.DefaultDamagerRepairer; // packageimport
32 import org.eclipse.jface.text.rules.IToken; // packageimport
33 import org.eclipse.jface.text.rules.IPartitionTokenScanner; // packageimport
34 import org.eclipse.jface.text.rules.MultiLineRule; // packageimport
35 import org.eclipse.jface.text.rules.RuleBasedPartitioner; // packageimport
36 import org.eclipse.jface.text.rules.RuleBasedPartitionScanner; // packageimport
37 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner; // packageimport
38 import org.eclipse.jface.text.rules.IWhitespaceDetector; // packageimport
39
40
41 import java.lang.all;
42 import java.util.Iterator;
43 import java.util.Map;
44 import java.util.HashMap;
45 import java.util.Set;
46
47
48
49
50
51 import org.eclipse.core.runtime.Assert;
52
53
54
55 /**
56 * An implementation of <code>IRule</code> capable of detecting words
57 * Word rules also allow for the association of tokens with specific words.
58 * That is, not only can the rule be used to provide tokens for exact matches,
59 * but also for the generalized notion of a word in the context in which it is used.
60 * A word rules uses a word detector to determine what a word is.
61 *
62 * @see IWordDetector
63 */
64 public class WordRule : IRule {
65
66 /** Internal setting for the un-initialized column constraint. */
67 protected static const int UNDEFINED= -1;
68
69 /** The word detector used by this rule. */
70 protected IWordDetector fDetector;
71 /** The default token to be returned on success and if nothing else has been specified. */
72 protected IToken fDefaultToken;
73 /** The column constraint. */
74 protected int fColumn= UNDEFINED;
75 /** The table of predefined words and token for this rule. */
76 protected Map fWords;
77 /** Buffer used for pattern detection. */
78 private StringBuffer fBuffer;
79 /**
80 * Tells whether this rule is case sensitive.
81 * @since 3.3
82 */
83 private bool fIgnoreCase= false;
84
85 /**
86 * Creates a rule which, with the help of an word detector, will return the token
87 * associated with the detected word. If no token has been associated, the scanner
88 * will be rolled back and an undefined token will be returned in order to allow
89 * any subsequent rules to analyze the characters.
90 *
91 * @param detector the word detector to be used by this rule, may not be <code>null</code>
92 * @see #addWord(String, IToken)
93 */
94 public this(IWordDetector detector) {
95 this(detector, Token.UNDEFINED, false);
96 }
97
98 /**
99 * Creates a rule which, with the help of a word detector, will return the token
100 * associated with the detected word. If no token has been associated, the
101 * specified default token will be returned.
102 *
103 * @param detector the word detector to be used by this rule, may not be <code>null</code>
104 * @param defaultToken the default token to be returned on success
105 * if nothing else is specified, may not be <code>null</code>
106 * @see #addWord(String, IToken)
107 */
108 public this(IWordDetector detector, IToken defaultToken) {
109 this(detector, defaultToken, false);
110 }
111
112 /**
113 * Creates a rule which, with the help of a word detector, will return the token
114 * associated with the detected word. If no token has been associated, the
115 * specified default token will be returned.
116 *
117 * @param detector the word detector to be used by this rule, may not be <code>null</code>
118 * @param defaultToken the default token to be returned on success
119 * if nothing else is specified, may not be <code>null</code>
120 * @param ignoreCase the case sensitivity associated with this rule
121 * @see #addWord(String, IToken)
122 * @since 3.3
123 */
124 public this(IWordDetector detector, IToken defaultToken, bool ignoreCase) {
125 fWords= new HashMap();
126 fBuffer= new StringBuffer();
127
128 Assert.isNotNull(cast(Object)detector);
129 Assert.isNotNull(cast(Object)defaultToken);
130
131 fDetector= detector;
132 fDefaultToken= defaultToken;
133 fIgnoreCase= ignoreCase;
134 }
135
136 /**
137 * Adds a word and the token to be returned if it is detected.
138 *
139 * @param word the word this rule will search for, may not be <code>null</code>
140 * @param token the token to be returned if the word has been found, may not be <code>null</code>
141 */
142 public void addWord(String word, IToken token) {
143 //Assert.isNotNull(word);
144 Assert.isNotNull(cast(Object)token);
145
146 fWords.put(word, cast(Object)token);
147 }
148
149 /**
150 * Sets a column constraint for this rule. If set, the rule's token
151 * will only be returned if the pattern is detected starting at the
152 * specified column. If the column is smaller then 0, the column
153 * constraint is considered removed.
154 *
155 * @param column the column in which the pattern starts
156 */
157 public void setColumnConstraint(int column) {
158 if (column < 0)
159 column= UNDEFINED;
160 fColumn= column;
161 }
162
163 /*
164 * @see IRule#evaluate(ICharacterScanner)
165 */
166 public IToken evaluate(ICharacterScanner scanner) {
167 int c= scanner.read();
168 if (c !is ICharacterScanner.EOF && fDetector.isWordStart(cast(dchar) c)) {
169 if (fColumn is UNDEFINED || (fColumn is scanner.getColumn() - 1)) {
170
171 fBuffer.truncate(0);
172 do {
173 fBuffer.append(cast(char) c);
174 c= scanner.read();
175 } while (c !is ICharacterScanner.EOF && fDetector.isWordPart(cast(dchar) c));
176 scanner.unread();
177
178 String buffer= fBuffer.toString();
179 IToken token= cast(IToken)fWords.get(buffer);
180
181 if(fIgnoreCase) {
182 Iterator iter= fWords.keySet().iterator();
183 while (iter.hasNext()) {
184 String key= stringcast(iter.next());
185 if(buffer.equalsIgnoreCase(key)) {
186 token= cast(IToken)fWords.get(key);
187 break;
188 }
189 }
190 } else
191 token= cast(IToken)fWords.get(buffer);
192
193 if (token !is null)
194 return token;
195
196 if (fDefaultToken.isUndefined())
197 unreadBuffer(scanner);
198
199 return fDefaultToken;
200 }
201 }
202
203 scanner.unread();
204 return Token.UNDEFINED;
205 }
206
207 /**
208 * Returns the characters in the buffer to the scanner.
209 *
210 * @param scanner the scanner to be used
211 */
212 protected void unreadBuffer(ICharacterScanner scanner) {
213 for (int i= fBuffer.length() - 1; i >= 0; i--)
214 scanner.unread();
215 }
216
217 }