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