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
|
|
15 import dwtx.jface.text.rules.FastPartitioner; // packageimport
|
|
16 import dwtx.jface.text.rules.ITokenScanner; // packageimport
|
|
17 import dwtx.jface.text.rules.Token; // packageimport
|
|
18 import dwtx.jface.text.rules.RuleBasedScanner; // packageimport
|
|
19 import dwtx.jface.text.rules.EndOfLineRule; // packageimport
|
|
20 import dwtx.jface.text.rules.WordRule; // packageimport
|
|
21 import dwtx.jface.text.rules.WhitespaceRule; // packageimport
|
|
22 import dwtx.jface.text.rules.WordPatternRule; // packageimport
|
|
23 import dwtx.jface.text.rules.IPredicateRule; // packageimport
|
|
24 import dwtx.jface.text.rules.DefaultPartitioner; // packageimport
|
|
25 import dwtx.jface.text.rules.NumberRule; // packageimport
|
|
26 import dwtx.jface.text.rules.SingleLineRule; // packageimport
|
|
27 import dwtx.jface.text.rules.PatternRule; // packageimport
|
|
28 import dwtx.jface.text.rules.IWordDetector; // 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.IWhitespaceDetector; // packageimport
|
|
39
|
129
|
40 module dwtx.jface.text.rules.BufferedRuleBasedScanner;
|
|
41
|
|
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 */
|
|
57 private final static int DEFAULT_BUFFER_SIZE= 500;
|
|
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 */
|
|
74 protected BufferedRuleBasedScanner() {
|
|
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 */
|
|
85 public BufferedRuleBasedScanner(int size) {
|
|
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
|