Mercurial > projects > dwt-addons
annotate dwtx/jface/text/rules/BufferedRuleBasedScanner.d @ 162:1a5b8f8129df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 08 Sep 2008 00:51:37 +0200 |
parents | eb21d3dfc767 |
children | eb98a5cbfd78 |
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.BufferedRuleBasedScanner; |
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.RuleBasedScanner; // packageimport | |
21 import dwtx.jface.text.rules.EndOfLineRule; // packageimport | |
22 import dwtx.jface.text.rules.WordRule; // packageimport | |
23 import dwtx.jface.text.rules.WhitespaceRule; // packageimport | |
24 import dwtx.jface.text.rules.WordPatternRule; // packageimport | |
25 import dwtx.jface.text.rules.IPredicateRule; // packageimport | |
26 import dwtx.jface.text.rules.DefaultPartitioner; // packageimport | |
27 import dwtx.jface.text.rules.NumberRule; // packageimport | |
28 import dwtx.jface.text.rules.SingleLineRule; // packageimport | |
29 import dwtx.jface.text.rules.PatternRule; // packageimport | |
30 import dwtx.jface.text.rules.IWordDetector; // packageimport | |
31 import dwtx.jface.text.rules.RuleBasedDamagerRepairer; // packageimport | |
32 import dwtx.jface.text.rules.ICharacterScanner; // packageimport | |
33 import dwtx.jface.text.rules.IRule; // packageimport | |
34 import dwtx.jface.text.rules.DefaultDamagerRepairer; // packageimport | |
35 import dwtx.jface.text.rules.IToken; // packageimport | |
36 import dwtx.jface.text.rules.IPartitionTokenScanner; // packageimport | |
37 import dwtx.jface.text.rules.MultiLineRule; // packageimport | |
38 import dwtx.jface.text.rules.RuleBasedPartitioner; // packageimport | |
39 import dwtx.jface.text.rules.RuleBasedPartitionScanner; // packageimport | |
40 import dwtx.jface.text.rules.IWhitespaceDetector; // packageimport | |
41 | |
129 | 42 import dwt.dwthelper.utils; |
43 | |
44 | |
45 import dwtx.core.runtime.Assert; | |
46 import dwtx.jface.text.BadLocationException; | |
47 import dwtx.jface.text.IDocument; | |
48 | |
49 /** | |
50 * A buffered rule based scanner. The buffer always contains a section | |
51 * of a fixed size of the document to be scanned. Completely adheres to | |
52 * the contract of <code>RuleBasedScanner</code>. | |
53 */ | |
54 public class BufferedRuleBasedScanner : RuleBasedScanner { | |
55 | |
56 /** The default buffer size. Value = 500 */ | |
146 | 57 private const static int DEFAULT_BUFFER_SIZE= 500; |
129 | 58 /** The actual size of the buffer. Initially set to <code>DEFAULT_BUFFER_SIZE</code> */ |
59 private int fBufferSize= DEFAULT_BUFFER_SIZE; | |
60 /** The buffer */ | |
162 | 61 private char[] fBuffer; |
129 | 62 /** The offset of the document at which the buffer starts */ |
63 private int fStart; | |
64 /** The offset of the document at which the buffer ends */ | |
65 private int fEnd; | |
66 /** The cached length of the document */ | |
67 private int fDocumentLength; | |
68 | |
69 | |
70 /** | |
71 * Creates a new buffered rule based scanner which does | |
72 * not have any rule and a default buffer size of 500 characters. | |
73 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
74 protected this() { |
129 | 75 super(); |
162 | 76 fBuffer= new char[DEFAULT_BUFFER_SIZE]; |
77 fBuffer[] = 0; | |
129 | 78 } |
79 | |
80 /** | |
81 * Creates a new buffered rule based scanner which does | |
82 * not have any rule. The buffer size is set to the given | |
83 * number of characters. | |
84 * | |
85 * @param size the buffer size | |
86 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
87 public this(int size) { |
129 | 88 super(); |
162 | 89 fBuffer= new char[DEFAULT_BUFFER_SIZE]; |
90 fBuffer[] = 0; | |
129 | 91 setBufferSize(size); |
92 } | |
93 | |
94 /** | |
95 * Sets the buffer to the given number of characters. | |
96 * | |
97 * @param size the buffer size | |
98 */ | |
99 protected void setBufferSize(int size) { | |
100 Assert.isTrue(size > 0); | |
101 fBufferSize= size; | |
102 fBuffer= new char[size]; | |
162 | 103 fBuffer[] = 0; |
129 | 104 } |
105 | |
106 /** | |
107 * Shifts the buffer so that the buffer starts at the | |
108 * given document offset. | |
109 * | |
110 * @param offset the document offset at which the buffer starts | |
111 */ | |
112 private void shiftBuffer(int offset) { | |
113 | |
114 fStart= offset; | |
115 fEnd= fStart + fBufferSize; | |
116 if (fEnd > fDocumentLength) | |
117 fEnd= fDocumentLength; | |
118 | |
119 try { | |
120 | |
121 String content= fDocument.get(fStart, fEnd - fStart); | |
122 content.getChars(0, fEnd - fStart, fBuffer, 0); | |
123 | |
124 } catch (BadLocationException x) { | |
125 } | |
126 } | |
127 | |
128 /* | |
129 * @see RuleBasedScanner#setRange(IDocument, int, int) | |
130 */ | |
131 public void setRange(IDocument document, int offset, int length) { | |
132 | |
133 super.setRange(document, offset, length); | |
134 | |
135 fDocumentLength= document.getLength(); | |
136 shiftBuffer(offset); | |
137 } | |
138 | |
139 /* | |
140 * @see RuleBasedScanner#read() | |
141 */ | |
142 public int read() { | |
143 fColumn= UNDEFINED; | |
144 if (fOffset >= fRangeEnd) { | |
145 ++ fOffset; | |
146 return EOF; | |
147 } | |
148 | |
149 if (fOffset is fEnd) | |
150 shiftBuffer(fEnd); | |
151 else if (fOffset < fStart || fEnd < fOffset) | |
152 shiftBuffer(fOffset); | |
153 | |
154 return fBuffer[fOffset++ - fStart]; | |
155 } | |
156 | |
157 /* | |
158 * @see RuleBasedScanner#unread() | |
159 */ | |
160 public void unread() { | |
161 | |
162 if (fOffset is fStart) | |
163 shiftBuffer(Math.max(0, fStart - (fBufferSize / 2))); | |
164 | |
165 --fOffset; | |
166 fColumn= UNDEFINED; | |
167 } | |
168 } | |
169 | |
170 |