Mercurial > projects > dwt-addons
comparison 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 |
comparison
equal
deleted
inserted
replaced
161:f8d52b926852 | 162:1a5b8f8129df |
---|---|
59 * @see IWordDetector | 59 * @see IWordDetector |
60 */ | 60 */ |
61 public class WordRule : IRule { | 61 public class WordRule : IRule { |
62 | 62 |
63 /** Internal setting for the un-initialized column constraint. */ | 63 /** Internal setting for the un-initialized column constraint. */ |
64 protected static final int UNDEFINED= -1; | 64 protected static const int UNDEFINED= -1; |
65 | 65 |
66 /** The word detector used by this rule. */ | 66 /** The word detector used by this rule. */ |
67 protected IWordDetector fDetector; | 67 protected IWordDetector fDetector; |
68 /** The default token to be returned on success and if nothing else has been specified. */ | 68 /** The default token to be returned on success and if nothing else has been specified. */ |
69 protected IToken fDefaultToken; | 69 protected IToken fDefaultToken; |
70 /** The column constraint. */ | 70 /** The column constraint. */ |
71 protected int fColumn= UNDEFINED; | 71 protected int fColumn= UNDEFINED; |
72 /** The table of predefined words and token for this rule. */ | 72 /** The table of predefined words and token for this rule. */ |
73 protected Map fWords= new HashMap(); | 73 protected Map fWords; |
74 /** Buffer used for pattern detection. */ | 74 /** Buffer used for pattern detection. */ |
75 private StringBuffer fBuffer= new StringBuffer(); | 75 private StringBuffer fBuffer; |
76 /** | 76 /** |
77 * Tells whether this rule is case sensitive. | 77 * Tells whether this rule is case sensitive. |
78 * @since 3.3 | 78 * @since 3.3 |
79 */ | 79 */ |
80 private bool fIgnoreCase= false; | 80 private bool fIgnoreCase= false; |
117 * @param ignoreCase the case sensitivity associated with this rule | 117 * @param ignoreCase the case sensitivity associated with this rule |
118 * @see #addWord(String, IToken) | 118 * @see #addWord(String, IToken) |
119 * @since 3.3 | 119 * @since 3.3 |
120 */ | 120 */ |
121 public this(IWordDetector detector, IToken defaultToken, bool ignoreCase) { | 121 public this(IWordDetector detector, IToken defaultToken, bool ignoreCase) { |
122 Assert.isNotNull(detector); | 122 fWords= new HashMap(); |
123 Assert.isNotNull(defaultToken); | 123 fBuffer= new StringBuffer(); |
124 | |
125 Assert.isNotNull(cast(Object)detector); | |
126 Assert.isNotNull(cast(Object)defaultToken); | |
124 | 127 |
125 fDetector= detector; | 128 fDetector= detector; |
126 fDefaultToken= defaultToken; | 129 fDefaultToken= defaultToken; |
127 fIgnoreCase= ignoreCase; | 130 fIgnoreCase= ignoreCase; |
128 } | 131 } |
132 * | 135 * |
133 * @param word the word this rule will search for, may not be <code>null</code> | 136 * @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> | 137 * @param token the token to be returned if the word has been found, may not be <code>null</code> |
135 */ | 138 */ |
136 public void addWord(String word, IToken token) { | 139 public void addWord(String word, IToken token) { |
137 Assert.isNotNull(word); | 140 //Assert.isNotNull(word); |
138 Assert.isNotNull(token); | 141 Assert.isNotNull(cast(Object)token); |
139 | 142 |
140 fWords.put(word, token); | 143 fWords.put(word, cast(Object)token); |
141 } | 144 } |
142 | 145 |
143 /** | 146 /** |
144 * Sets a column constraint for this rule. If set, the rule's token | 147 * 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 | 148 * will only be returned if the pattern is detected starting at the |
157 /* | 160 /* |
158 * @see IRule#evaluate(ICharacterScanner) | 161 * @see IRule#evaluate(ICharacterScanner) |
159 */ | 162 */ |
160 public IToken evaluate(ICharacterScanner scanner) { | 163 public IToken evaluate(ICharacterScanner scanner) { |
161 int c= scanner.read(); | 164 int c= scanner.read(); |
162 if (c !is ICharacterScanner.EOF && fDetector.isWordStart(cast(wchar) c)) { | 165 if (c !is ICharacterScanner.EOF && fDetector.isWordStart(cast(char) c)) { |
163 if (fColumn is UNDEFINED || (fColumn is scanner.getColumn() - 1)) { | 166 if (fColumn is UNDEFINED || (fColumn is scanner.getColumn() - 1)) { |
164 | 167 |
165 fBuffer.setLength(0); | 168 fBuffer.truncate(0); |
166 do { | 169 do { |
167 fBuffer.append(cast(wchar) c); | 170 fBuffer.append(cast(char) c); |
168 c= scanner.read(); | 171 c= scanner.read(); |
169 } while (c !is ICharacterScanner.EOF && fDetector.isWordPart(cast(wchar) c)); | 172 } while (c !is ICharacterScanner.EOF && fDetector.isWordPart(cast(char) c)); |
170 scanner.unread(); | 173 scanner.unread(); |
171 | 174 |
172 String buffer= fBuffer.toString(); | 175 String buffer= fBuffer.toString(); |
173 IToken token= cast(IToken)fWords.get(buffer); | 176 IToken token= cast(IToken)fWords.get(buffer); |
174 | 177 |
175 if(fIgnoreCase) { | 178 if(fIgnoreCase) { |
176 Iterator iter= fWords.keySet().iterator(); | 179 Iterator iter= fWords.keySet().iterator(); |
177 while (iter.hasNext()) { | 180 while (iter.hasNext()) { |
178 String key= cast(String)iter.next(); | 181 String key= stringcast(iter.next()); |
179 if(buffer.equalsIgnoreCase(key)) { | 182 if(buffer.equalsIgnoreCase(key)) { |
180 token= cast(IToken)fWords.get(key); | 183 token= cast(IToken)fWords.get(key); |
181 break; | 184 break; |
182 } | 185 } |
183 } | 186 } |
184 } else | 187 } else |
185 token= cast(IToken)fWords.get(buffer); | 188 token= cast(IToken)fWords.get(buffer); |
186 | 189 |
187 if (token !is null) | 190 if (token !is null) |
188 return token; | 191 return token; |
189 | 192 |
190 if (fDefaultToken.isUndefined()) | 193 if (fDefaultToken.isUndefined()) |
191 unreadBuffer(scanner); | 194 unreadBuffer(scanner); |