Mercurial > projects > dwt-addons
annotate dwtx/jface/text/rules/RuleBasedScanner.d @ 199:eb98a5cbfd78
Fix: potential problem with synchronized, thx torhu.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 13 Mar 2009 17:03:26 +0100 |
parents | 1a5b8f8129df |
children |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 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 | |
131 | 14 |
151 | 15 module dwtx.jface.text.rules.RuleBasedScanner; |
16 | |
131 | 17 import dwtx.jface.text.rules.FastPartitioner; // packageimport |
18 import dwtx.jface.text.rules.ITokenScanner; // packageimport | |
19 import dwtx.jface.text.rules.Token; // packageimport | |
20 import dwtx.jface.text.rules.EndOfLineRule; // packageimport | |
21 import dwtx.jface.text.rules.WordRule; // packageimport | |
22 import dwtx.jface.text.rules.WhitespaceRule; // packageimport | |
23 import dwtx.jface.text.rules.WordPatternRule; // packageimport | |
24 import dwtx.jface.text.rules.IPredicateRule; // packageimport | |
25 import dwtx.jface.text.rules.DefaultPartitioner; // packageimport | |
26 import dwtx.jface.text.rules.NumberRule; // packageimport | |
27 import dwtx.jface.text.rules.SingleLineRule; // packageimport | |
28 import dwtx.jface.text.rules.PatternRule; // packageimport | |
29 import dwtx.jface.text.rules.RuleBasedDamagerRepairer; // packageimport | |
30 import dwtx.jface.text.rules.ICharacterScanner; // packageimport | |
31 import dwtx.jface.text.rules.IRule; // packageimport | |
32 import dwtx.jface.text.rules.DefaultDamagerRepairer; // packageimport | |
33 import dwtx.jface.text.rules.IToken; // packageimport | |
34 import dwtx.jface.text.rules.IPartitionTokenScanner; // packageimport | |
35 import dwtx.jface.text.rules.MultiLineRule; // packageimport | |
36 import dwtx.jface.text.rules.RuleBasedPartitioner; // packageimport | |
37 import dwtx.jface.text.rules.RuleBasedPartitionScanner; // packageimport | |
38 import dwtx.jface.text.rules.BufferedRuleBasedScanner; // packageimport | |
39 import dwtx.jface.text.rules.IWhitespaceDetector; // packageimport | |
40 | |
129 | 41 import dwt.dwthelper.utils; |
42 | |
43 | |
44 | |
45 import dwtx.core.runtime.Assert; | |
46 import dwtx.jface.text.BadLocationException; | |
47 import dwtx.jface.text.IDocument; | |
48 | |
49 | |
50 /** | |
51 * A generic scanner which can be "programmed" with a sequence of rules. | |
52 * The scanner is used to get the next token by evaluating its rule in sequence until | |
53 * one is successful. If a rule returns a token which is undefined, the scanner will proceed to | |
54 * the next rule. Otherwise the token provided by the rule will be returned by | |
55 * the scanner. If no rule returned a defined token, this scanner returns a token | |
56 * which returns <code>true</code> when calling <code>isOther</code>, unless the end | |
57 * of the file is reached. In this case the token returns <code>true</code> when calling | |
58 * <code>isEOF</code>. | |
59 * | |
60 * @see IRule | |
61 */ | |
62 public class RuleBasedScanner : ICharacterScanner, ITokenScanner { | |
63 | |
64 /** The list of rules of this scanner */ | |
65 protected IRule[] fRules; | |
66 /** The token to be returned by default if no rule fires */ | |
67 protected IToken fDefaultReturnToken; | |
68 /** The document to be scanned */ | |
69 protected IDocument fDocument; | |
70 /** The cached legal line delimiters of the document */ | |
71 protected char[][] fDelimiters; | |
72 /** The offset of the next character to be read */ | |
73 protected int fOffset; | |
74 /** The end offset of the range to be scanned */ | |
75 protected int fRangeEnd; | |
76 /** The offset of the last read token */ | |
77 protected int fTokenOffset; | |
78 /** The cached column of the current scanner position */ | |
79 protected int fColumn; | |
80 /** Internal setting for the un-initialized column cache. */ | |
81 protected static final int UNDEFINED= -1; | |
82 | |
83 /** | |
84 * Creates a new rule based scanner which does not have any rule. | |
85 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
86 public this() { |
129 | 87 } |
88 | |
89 /** | |
90 * Configures the scanner with the given sequence of rules. | |
91 * | |
92 * @param rules the sequence of rules controlling this scanner | |
93 */ | |
94 public void setRules(IRule[] rules) { | |
95 if (rules !is null) { | |
96 fRules= new IRule[rules.length]; | |
162 | 97 SimpleType!(IRule).arraycopy(rules, 0, fRules, 0, rules.length); |
129 | 98 } else |
99 fRules= null; | |
100 } | |
101 | |
102 /** | |
103 * Configures the scanner's default return token. This is the token | |
104 * which is returned when none of the rules fired and EOF has not been | |
105 * reached. | |
106 * | |
107 * @param defaultReturnToken the default return token | |
108 * @since 2.0 | |
109 */ | |
110 public void setDefaultReturnToken(IToken defaultReturnToken) { | |
111 Assert.isNotNull(defaultReturnToken.getData()); | |
112 fDefaultReturnToken= defaultReturnToken; | |
113 } | |
114 | |
115 /* | |
116 * @see ITokenScanner#setRange(IDocument, int, int) | |
117 */ | |
162 | 118 public void setRange(IDocument document, int offset, int length) { |
129 | 119 Assert.isLegal(document !is null); |
120 final int documentLength= document.getLength(); | |
121 checkRange(offset, length, documentLength); | |
122 | |
123 fDocument= document; | |
124 fOffset= offset; | |
125 fColumn= UNDEFINED; | |
126 fRangeEnd= offset + length; | |
127 | |
128 String[] delimiters= fDocument.getLegalLineDelimiters(); | |
162 | 129 fDelimiters= new char[][](delimiters.length); |
129 | 130 for (int i= 0; i < delimiters.length; i++) |
131 fDelimiters[i]= delimiters[i].toCharArray(); | |
132 | |
133 if (fDefaultReturnToken is null) | |
134 fDefaultReturnToken= new Token(null); | |
135 } | |
136 | |
137 /** | |
138 * Checks that the given range is valid. | |
139 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=69292 | |
162 | 140 * |
129 | 141 * @param offset the offset of the document range to scan |
142 * @param length the length of the document range to scan | |
143 * @param documentLength the document's length | |
144 * @since 3.3 | |
145 */ | |
146 private void checkRange(int offset, int length, int documentLength) { | |
147 Assert.isLegal(offset > -1); | |
148 Assert.isLegal(length > -1); | |
149 Assert.isLegal(offset + length <= documentLength); | |
150 } | |
151 | |
152 /* | |
153 * @see ITokenScanner#getTokenOffset() | |
154 */ | |
155 public int getTokenOffset() { | |
156 return fTokenOffset; | |
157 } | |
158 | |
159 /* | |
160 * @see ITokenScanner#getTokenLength() | |
161 */ | |
162 public int getTokenLength() { | |
163 if (fOffset < fRangeEnd) | |
164 return fOffset - getTokenOffset(); | |
165 return fRangeEnd - getTokenOffset(); | |
166 } | |
167 | |
168 | |
169 /* | |
170 * @see ICharacterScanner#getColumn() | |
171 */ | |
172 public int getColumn() { | |
173 if (fColumn is UNDEFINED) { | |
174 try { | |
175 int line= fDocument.getLineOfOffset(fOffset); | |
176 int start= fDocument.getLineOffset(line); | |
177 | |
178 fColumn= fOffset - start; | |
179 | |
180 } catch (BadLocationException ex) { | |
181 } | |
182 } | |
183 return fColumn; | |
184 } | |
185 | |
186 /* | |
187 * @see ICharacterScanner#getLegalLineDelimiters() | |
188 */ | |
189 public char[][] getLegalLineDelimiters() { | |
190 return fDelimiters; | |
191 } | |
192 | |
193 /* | |
194 * @see ITokenScanner#nextToken() | |
195 */ | |
196 public IToken nextToken() { | |
197 | |
198 fTokenOffset= fOffset; | |
199 fColumn= UNDEFINED; | |
200 | |
201 if (fRules !is null) { | |
202 for (int i= 0; i < fRules.length; i++) { | |
203 IToken token= (fRules[i].evaluate(this)); | |
204 if (!token.isUndefined()) | |
205 return token; | |
206 } | |
207 } | |
208 | |
209 if (read() is EOF) | |
210 return Token.EOF; | |
211 return fDefaultReturnToken; | |
212 } | |
213 | |
214 /* | |
215 * @see ICharacterScanner#read() | |
216 */ | |
217 public int read() { | |
218 | |
219 try { | |
220 | |
221 if (fOffset < fRangeEnd) { | |
222 try { | |
223 return fDocument.getChar(fOffset); | |
224 } catch (BadLocationException e) { | |
225 } | |
226 } | |
227 | |
228 return EOF; | |
229 | |
230 } finally { | |
231 ++ fOffset; | |
232 fColumn= UNDEFINED; | |
233 } | |
234 } | |
235 | |
236 /* | |
237 * @see ICharacterScanner#unread() | |
238 */ | |
239 public void unread() { | |
240 --fOffset; | |
241 fColumn= UNDEFINED; | |
242 } | |
243 } | |
244 | |
245 |