Mercurial > projects > dwt-addons
annotate dwtx/jface/text/rules/WordRule.d @ 162:1a5b8f8129df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 08 Sep 2008 00:51:37 +0200 |
parents | f70d9508c95c |
children | eb98a5cbfd78 |
rev | line source |
---|---|
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 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
44 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
45 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
46 |
129 | 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. */ | |
162 | 64 protected static const int UNDEFINED= -1; |
129 | 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. */ | |
162 | 73 protected Map fWords; |
129 | 74 /** Buffer used for pattern detection. */ |
162 | 75 private StringBuffer fBuffer; |
129 | 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 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
91 public this(IWordDetector detector) { |
129 | 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 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
105 public this(IWordDetector detector, IToken defaultToken) { |
129 | 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 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
121 public this(IWordDetector detector, IToken defaultToken, bool ignoreCase) { |
162 | 122 fWords= new HashMap(); |
123 fBuffer= new StringBuffer(); | |
124 | |
125 Assert.isNotNull(cast(Object)detector); | |
126 Assert.isNotNull(cast(Object)defaultToken); | |
129 | 127 |
128 fDetector= detector; | |
129 fDefaultToken= defaultToken; | |
130 fIgnoreCase= ignoreCase; | |
131 } | |
132 | |
133 /** | |
134 * Adds a word and the token to be returned if it is detected. | |
135 * | |
136 * @param word the word this rule will search for, may not be <code>null</code> | |
137 * @param token the token to be returned if the word has been found, may not be <code>null</code> | |
138 */ | |
139 public void addWord(String word, IToken token) { | |
162 | 140 //Assert.isNotNull(word); |
141 Assert.isNotNull(cast(Object)token); | |
129 | 142 |
162 | 143 fWords.put(word, cast(Object)token); |
129 | 144 } |
145 | |
146 /** | |
147 * Sets a column constraint for this rule. If set, the rule's token | |
148 * will only be returned if the pattern is detected starting at the | |
149 * specified column. If the column is smaller then 0, the column | |
150 * constraint is considered removed. | |
151 * | |
152 * @param column the column in which the pattern starts | |
153 */ | |
154 public void setColumnConstraint(int column) { | |
155 if (column < 0) | |
156 column= UNDEFINED; | |
157 fColumn= column; | |
158 } | |
159 | |
160 /* | |
161 * @see IRule#evaluate(ICharacterScanner) | |
162 */ | |
163 public IToken evaluate(ICharacterScanner scanner) { | |
164 int c= scanner.read(); | |
162 | 165 if (c !is ICharacterScanner.EOF && fDetector.isWordStart(cast(char) c)) { |
129 | 166 if (fColumn is UNDEFINED || (fColumn is scanner.getColumn() - 1)) { |
167 | |
162 | 168 fBuffer.truncate(0); |
129 | 169 do { |
162 | 170 fBuffer.append(cast(char) c); |
129 | 171 c= scanner.read(); |
162 | 172 } while (c !is ICharacterScanner.EOF && fDetector.isWordPart(cast(char) c)); |
129 | 173 scanner.unread(); |
174 | |
175 String buffer= fBuffer.toString(); | |
134 | 176 IToken token= cast(IToken)fWords.get(buffer); |
162 | 177 |
129 | 178 if(fIgnoreCase) { |
179 Iterator iter= fWords.keySet().iterator(); | |
180 while (iter.hasNext()) { | |
162 | 181 String key= stringcast(iter.next()); |
129 | 182 if(buffer.equalsIgnoreCase(key)) { |
134 | 183 token= cast(IToken)fWords.get(key); |
129 | 184 break; |
185 } | |
186 } | |
187 } else | |
134 | 188 token= cast(IToken)fWords.get(buffer); |
162 | 189 |
129 | 190 if (token !is null) |
191 return token; | |
192 | |
193 if (fDefaultToken.isUndefined()) | |
194 unreadBuffer(scanner); | |
195 | |
196 return fDefaultToken; | |
197 } | |
198 } | |
199 | |
200 scanner.unread(); | |
201 return Token.UNDEFINED; | |
202 } | |
203 | |
204 /** | |
205 * Returns the characters in the buffer to the scanner. | |
206 * | |
207 * @param scanner the scanner to be used | |
208 */ | |
209 protected void unreadBuffer(ICharacterScanner scanner) { | |
210 for (int i= fBuffer.length() - 1; i >= 0; i--) | |
211 scanner.unread(); | |
212 } | |
213 | |
214 } |