Mercurial > projects > dwt-addons
annotate dwtx/jface/text/rules/BufferedRuleBasedScanner.d @ 151:eb21d3dfc767
fix module statement
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 24 Aug 2008 23:55:45 +0200 |
parents | 75302ef3f92f |
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.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 */ | |
61 private char[] fBuffer= new char[DEFAULT_BUFFER_SIZE]; | |
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(); |
76 } | |
77 | |
78 /** | |
79 * Creates a new buffered rule based scanner which does | |
80 * not have any rule. The buffer size is set to the given | |
81 * number of characters. | |
82 * | |
83 * @param size the buffer size | |
84 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
85 public this(int size) { |
129 | 86 super(); |
87 setBufferSize(size); | |
88 } | |
89 | |
90 /** | |
91 * Sets the buffer to the given number of characters. | |
92 * | |
93 * @param size the buffer size | |
94 */ | |
95 protected void setBufferSize(int size) { | |
96 Assert.isTrue(size > 0); | |
97 fBufferSize= size; | |
98 fBuffer= new char[size]; | |
99 } | |
100 | |
101 /** | |
102 * Shifts the buffer so that the buffer starts at the | |
103 * given document offset. | |
104 * | |
105 * @param offset the document offset at which the buffer starts | |
106 */ | |
107 private void shiftBuffer(int offset) { | |
108 | |
109 fStart= offset; | |
110 fEnd= fStart + fBufferSize; | |
111 if (fEnd > fDocumentLength) | |
112 fEnd= fDocumentLength; | |
113 | |
114 try { | |
115 | |
116 String content= fDocument.get(fStart, fEnd - fStart); | |
117 content.getChars(0, fEnd - fStart, fBuffer, 0); | |
118 | |
119 } catch (BadLocationException x) { | |
120 } | |
121 } | |
122 | |
123 /* | |
124 * @see RuleBasedScanner#setRange(IDocument, int, int) | |
125 */ | |
126 public void setRange(IDocument document, int offset, int length) { | |
127 | |
128 super.setRange(document, offset, length); | |
129 | |
130 fDocumentLength= document.getLength(); | |
131 shiftBuffer(offset); | |
132 } | |
133 | |
134 /* | |
135 * @see RuleBasedScanner#read() | |
136 */ | |
137 public int read() { | |
138 fColumn= UNDEFINED; | |
139 if (fOffset >= fRangeEnd) { | |
140 ++ fOffset; | |
141 return EOF; | |
142 } | |
143 | |
144 if (fOffset is fEnd) | |
145 shiftBuffer(fEnd); | |
146 else if (fOffset < fStart || fEnd < fOffset) | |
147 shiftBuffer(fOffset); | |
148 | |
149 return fBuffer[fOffset++ - fStart]; | |
150 } | |
151 | |
152 /* | |
153 * @see RuleBasedScanner#unread() | |
154 */ | |
155 public void unread() { | |
156 | |
157 if (fOffset is fStart) | |
158 shiftBuffer(Math.max(0, fStart - (fBufferSize / 2))); | |
159 | |
160 --fOffset; | |
161 fColumn= UNDEFINED; | |
162 } | |
163 } | |
164 | |
165 |