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