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