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